77import net .minecraft .util .Mth ;
88import net .minecraft .world .entity .player .Inventory ;
99import net .minecraft .world .entity .player .Player ;
10- import net .minecraft .world .food .FoodProperties ;
1110import net .minecraft .world .inventory .ContainerData ;
1211import net .minecraft .world .item .ItemStack ;
1312import net .minecraft .world .level .Level ;
1817import top .ctnstudio .futurefood .api .adapter .ModItemStackHandler ;
1918import top .ctnstudio .futurefood .common .block .GluttonyEntityBlock ;
2019import top .ctnstudio .futurefood .common .menu .GluttonyMenu ;
21- import top .ctnstudio .futurefood .core .init .ModItem ;
2220import top .ctnstudio .futurefood .core .init .ModTileEntity ;
21+ import top .ctnstudio .futurefood .core .recipe .GluttonyRecipe ;
22+ import top .ctnstudio .futurefood .core .recipe_manager .GluttonyRecipeManager ;
2323import top .ctnstudio .futurefood .util .EnergyUtil ;
2424
25- import java .util .Objects ;
26-
2725// TODO 添加配置功能
2826// TODO 根据方向调整物品抽入
2927public class GluttonyBlockEntity extends BaseEnergyStorageBlockEntity <GluttonyMenu > {
28+ public static final int MAX_WORK_TICK = 20 * 5 ;
3029 private int remainingTick ;
3130 private int maxWorkTick ;
3231 private final ContainerData workProgress ;
3332 // 缓存
34- private RecipeEntry cacheRecipeEntry ;
33+ private GluttonyRecipe recipe ;
3534
3635 public GluttonyBlockEntity (BlockPos pos , BlockState blockState ) {
3736 super (ModTileEntity .GLUTTONY .get (), pos , blockState ,
@@ -85,11 +84,11 @@ private void working(@NotNull Level level, @NotNull BlockPos pos) {
8584
8685 // 此次是避免直接修改工作进度导致配方为空
8786 // 获取缓存配方
88- if (cacheRecipeEntry == null ) {
89- ItemStack stackInSlot = itemHandler .getStackInSlot (1 ).copy ();
87+ if (recipe == null ) {
88+ final var stackInSlot = itemHandler .getStackInSlot (1 ).copy ();
9089 if (!stackInSlot .isEmpty ()) {
9190 produceProducts (stackInSlot );
92- if (cacheRecipeEntry == null ) {
91+ if (recipe == null ) {
9392 resetProducts ();
9493 resetProgress ();
9594 return ;
@@ -102,8 +101,8 @@ private void working(@NotNull Level level, @NotNull BlockPos pos) {
102101 }
103102
104103 // 存储值避免移除输入槽导致值为空
105- ItemStack outputItem = cacheRecipeEntry .outputItem .copy ();
106- int outputEnergy = cacheRecipeEntry .outputEnergy ;
104+ ItemStack outputItem = recipe .outputItem () .copy ();
105+ int outputEnergy = recipe .outputEnergy () ;
107106 // 如果输入槽为空,则重置进度
108107 if (itemHandler .extractItem (1 , 1 , true ).isEmpty ()) {
109108 resetProducts ();
@@ -156,7 +155,7 @@ public void resetProgress() {
156155 }
157156
158157 public void resetProducts () {
159- cacheRecipeEntry = null ;
158+ recipe = null ;
160159 }
161160
162161 @ Override
@@ -185,36 +184,15 @@ protected void produceProducts(ItemStack item) {
185184 resetProgress ();
186185 return ;
187186 }
188- if (cacheRecipeEntry != null
189- && ItemStack .isSameItem (item , cacheRecipeEntry .inputItem )
190- && item .get (DataComponents .FOOD ).equals (cacheRecipeEntry .inputItem .get (DataComponents .FOOD ))) {
187+ if (recipe != null
188+ && ItemStack .isSameItem (item , recipe .inputItem () )
189+ && item .get (DataComponents .FOOD ).equals (recipe .inputItem () .get (DataComponents .FOOD ))) {
191190 return ;
192191 }
193192
194- maxWorkTick = 20 * 5 ;
193+ maxWorkTick = MAX_WORK_TICK ;
195194 remainingTick = maxWorkTick ;
196- cacheRecipeEntry = getProduct (item );
197- }
198-
199- /**
200- * 计算产物
201- *
202- * @return 产物
203- */
204- protected RecipeEntry getProduct (ItemStack item ) {
205- FoodProperties food = item .get (DataComponents .FOOD );
206- int nutritionFactor = food .nutrition ();
207- int saturationFactor = (int ) (food .saturation () * 5 );
208- int effectFactor = food .effects ().stream ()
209- .map (e -> e .probability () >= 1 ? e .effect () : null )
210- .filter (Objects ::nonNull )
211- .mapToInt (e -> (int ) (Math .max (1 , (e .getDuration () / 20.0 )) * e .getAmplifier () + 1 )).sum ();
212-
213- ItemStack outputItem = ModItem .FOOD_ESSENCE .get ().getDefaultInstance ();
214- int count = Math .max (1 , Math .min (outputItem .getMaxStackSize (), (int ) (((nutritionFactor * saturationFactor ) / 3.5f + effectFactor / 5.0f ) / 10.0f )));
215- outputItem .setCount (count );
216- int outputEnergy = ((nutritionFactor * saturationFactor ) * 5 + effectFactor * 10 ) * 2 ;
217- return new RecipeEntry (item , outputEnergy , outputItem );
195+ recipe = GluttonyRecipeManager .findRecipe (item );
218196 }
219197
220198 public boolean isWorking () {
@@ -249,9 +227,6 @@ protected void saveAdditional(CompoundTag nbt, HolderLookup.Provider provider) {
249227 return new GluttonyMenu (containerId , playerInventory , itemHandler , energyData , workProgress );
250228 }
251229
252- public record RecipeEntry (ItemStack inputItem , int outputEnergy , ItemStack outputItem ) {
253- }
254-
255230 public record Data (GluttonyBlockEntity blockEntity ) implements ContainerData {
256231 @ Override
257232 public int get (int index ) {
0 commit comments