77import net .minecraft .world .entity .player .Inventory ;
88import net .minecraft .world .entity .player .Player ;
99import net .minecraft .world .inventory .ContainerData ;
10+ import net .minecraft .world .item .ItemStack ;
1011import net .minecraft .world .level .Level ;
1112import net .minecraft .world .level .block .state .BlockState ;
1213import net .neoforged .neoforge .energy .IEnergyStorage ;
2021import software .bernie .geckolib .util .GeckoLibUtil ;
2122import top .ctnstudio .futurefood .api .adapter .ModEnergyStorage ;
2223import top .ctnstudio .futurefood .api .block .IUnlimitedEntityReceive ;
24+ import top .ctnstudio .futurefood .api .recipe .ParticleColliderRecipe ;
25+ import top .ctnstudio .futurefood .api .recipe .ParticleColliderRecipeManager ;
2326import top .ctnstudio .futurefood .common .menu .ParticleColliderMenu ;
2427import top .ctnstudio .futurefood .core .init .ModTileEntity ;
2528
29+ import java .util .Optional ;
30+
2631// TODO 自定义配方
2732public class ParticleColliderBlockEntity extends EnergyStorageBlockEntity <ParticleColliderMenu >
2833 implements GeoBlockEntity , IUnlimitedEntityReceive {
2934 protected static final RawAnimation DEPLOY_ANIM = RawAnimation .begin ();
3035
3136 private final AnimatableInstanceCache cache = GeckoLibUtil .createInstanceCache (this );
3237
38+ // 槽位
39+ public static final int INPUT_SLOT_1 = 1 ;
40+ public static final int INPUT_SLOT_2 = 2 ;
41+ public static final int OUTPUT_SLOT = 3 ;
42+
3343 private final WorkTick workTick ;
3444 // 使用包装类以实现同步
3545 private final WorkProgress workProgress ;
@@ -46,6 +56,7 @@ public void tick(@NotNull Level level, @NotNull BlockPos pos, @NotNull BlockStat
4656 return ;
4757 }
4858 controlItemEnergy (itemHandler , false );
59+ this .processRecipe ();
4960 }
5061
5162 @ Override
@@ -81,7 +92,7 @@ public IEnergyStorage getEnergyStorage() {
8192 @ Override
8293 protected void loadAdditional (CompoundTag nbt , HolderLookup .Provider provider ) {
8394 super .loadAdditional (nbt , provider );
84- if (nbt .contains ("remainingTick " )) setRemainingTick (nbt .getInt ("remainingTick " ));
95+ if (nbt .contains ("processTick " )) setProcessTick (nbt .getInt ("processTick " ));
8596 if (nbt .contains ("maxWorkTick" )) setMaxWorkTick (nbt .getInt ("maxWorkTick" ));
8697 }
8798
@@ -91,15 +102,15 @@ protected void loadAdditional(CompoundTag nbt, HolderLookup.Provider provider) {
91102 @ Override
92103 protected void saveAdditional (CompoundTag nbt , HolderLookup .Provider provider ) {
93104 super .saveAdditional (nbt , provider );
94- nbt .putInt ("remainingTick " , getRemainingTick ());
105+ nbt .putInt ("processTick " , getProcessTick ());
95106 nbt .putInt ("maxWorkTick" , getMaxWorkTick ());
96107 }
97108
98- public int getRemainingTick () {
99- return workTick .getRemainingTick ();
109+ public int getProcessTick () {
110+ return workTick .getProcessTick ();
100111 }
101112
102- public void setRemainingTick (int remainingTick ) {
113+ public void setProcessTick (int remainingTick ) {
103114 this .workTick .setMaxWorkTick (remainingTick );
104115 }
105116
@@ -123,7 +134,7 @@ public record WorkProgress(WorkTick workTick) implements ContainerData {
123134 @ Override
124135 public int get (int index ) {
125136 return switch (index ) {
126- case 0 -> workTick .getRemainingTick ();
137+ case 0 -> workTick .getProcessTick ();
127138 case 1 -> workTick .getMaxWorkTick ();
128139 default -> 0 ;
129140 };
@@ -132,7 +143,7 @@ public int get(int index) {
132143 @ Override
133144 public void set (int index , int value ) {
134145 switch (index ) {
135- case 0 -> workTick .setRemainingTick (value );
146+ case 0 -> workTick .setProcessTick (value );
136147 case 1 -> workTick .setMaxWorkTick (value );
137148 }
138149 }
@@ -144,15 +155,15 @@ public int getCount() {
144155 }
145156
146157 public static class WorkTick {
147- private int remainingTick ;
158+ private int processTick ;
148159 private int maxWorkTick ;
149160
150- public int getRemainingTick () {
151- return remainingTick ;
161+ public int getProcessTick () {
162+ return processTick ;
152163 }
153164
154- public void setRemainingTick (int remainingTick ) {
155- this .remainingTick = remainingTick ;
165+ public void setProcessTick (int processTick ) {
166+ this .processTick = processTick ;
156167 }
157168
158169 public int getMaxWorkTick () {
@@ -163,4 +174,62 @@ public void setMaxWorkTick(int maxWorkTick) {
163174 this .maxWorkTick = maxWorkTick ;
164175 }
165176 }
177+
178+ private void processRecipe () {
179+ ItemStack input1 = itemHandler .getStackInSlot (INPUT_SLOT_1 );
180+ ItemStack input2 = itemHandler .getStackInSlot (INPUT_SLOT_2 );
181+
182+ Optional <ParticleColliderRecipe > recipe = ParticleColliderRecipeManager .findRecipe (input1 , input2 );
183+
184+ if (recipe .isPresent ()) {
185+ ParticleColliderRecipe currentRecipe = recipe .get ();
186+ if (canCraft (currentRecipe )){
187+ workTick .setMaxWorkTick (currentRecipe .getProcessingTime ());
188+ int energyPerTick = currentRecipe .getEnergyPerTick ();
189+
190+ if (energyStorage .getEnergyStored () >= energyPerTick ){
191+ energyStorage .extractEnergy (energyPerTick , false );
192+ workTick .processTick ++;
193+ if (this .getProcessTick () >= this .getMaxWorkTick ()){
194+ craftItem (currentRecipe );
195+ workTick .setProcessTick (0 );
196+ }
197+ setChanged ();
198+ }
199+ }else {
200+ workTick .setProcessTick (0 );
201+ }
202+ }else {
203+ workTick .setProcessTick (0 );
204+ }
205+ }
206+
207+ private boolean canCraft (ParticleColliderRecipe recipe ) {
208+ if (recipe == null )return false ;
209+ ItemStack output = itemHandler .getStackInSlot (OUTPUT_SLOT );
210+ ItemStack result = recipe .getOutput ();
211+
212+ if (output .isEmpty ())
213+ return true ;
214+ if (!ItemStack .isSameItemSameComponents (output , result ))
215+ return false ;
216+ return output .getCount () + result .getCount () <= output .getMaxStackSize ();
217+ }
218+
219+ private void craftItem (ParticleColliderRecipe recipe ) {
220+ if (!canCraft (recipe ))return ;
221+ ItemStack result = recipe .getOutput ();
222+ ItemStack output = itemHandler .getStackInSlot (OUTPUT_SLOT );
223+
224+ if (output .isEmpty ()){
225+ itemHandler .setStackInSlot (OUTPUT_SLOT , result );
226+ }else {
227+ output .grow (result .getCount ());
228+ }
229+
230+ itemHandler .extractItem (INPUT_SLOT_1 , 1 , false );
231+ itemHandler .extractItem (INPUT_SLOT_2 , 1 , false );
232+
233+ workTick .setProcessTick (0 );
234+ }
166235}
0 commit comments