|
| 1 | +package com.ref.aea.mixin.aae.crafting.blocked; |
| 2 | + |
| 3 | +import appeng.api.crafting.IPatternDetails; |
| 4 | +import appeng.api.networking.crafting.ICraftingProvider; |
| 5 | +import appeng.api.networking.energy.IEnergyService; |
| 6 | +import appeng.api.stacks.AEKey; |
| 7 | +import appeng.api.stacks.KeyCounter; |
| 8 | +import appeng.me.service.CraftingService; |
| 9 | +import com.ref.aea.api.mixin.ae.crafting.blocked.IMixinCraftingCpuLogicBlocked; |
| 10 | +import net.minecraft.world.level.Level; |
| 11 | +import net.pedroksl.advanced_ae.common.logic.AdvCraftingCPULogic; |
| 12 | +import org.spongepowered.asm.mixin.Mixin; |
| 13 | +import org.spongepowered.asm.mixin.Shadow; |
| 14 | +import org.spongepowered.asm.mixin.Unique; |
| 15 | +import org.spongepowered.asm.mixin.injection.At; |
| 16 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 17 | +import org.spongepowered.asm.mixin.injection.ModifyVariable; |
| 18 | +import org.spongepowered.asm.mixin.injection.Redirect; |
| 19 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 20 | + |
| 21 | +@Mixin(value = AdvCraftingCPULogic.class, remap = false) |
| 22 | +public abstract class AdvCraftingCPULogicMixin implements IMixinCraftingCpuLogicBlocked { |
| 23 | + |
| 24 | + @Shadow |
| 25 | + protected abstract void postChange(AEKey key); |
| 26 | + |
| 27 | + @Unique private final KeyCounter AEA$blockedTasks = new KeyCounter(); |
| 28 | + @Unique private IPatternDetails AEA$currentDetails; |
| 29 | + @Unique private boolean AEA$pushedSuccessThisPattern; |
| 30 | + |
| 31 | + @Override |
| 32 | + public long AEA$getBlocked(AEKey key) { |
| 33 | + return AEA$blockedTasks.get(key); |
| 34 | + } |
| 35 | + |
| 36 | + @Inject(method = "executeCrafting", at = @At("HEAD")) |
| 37 | + private void AEA$clearBlockedAtStart( |
| 38 | + int maxPatterns, |
| 39 | + CraftingService craftingService, |
| 40 | + IEnergyService energyService, |
| 41 | + Level level, |
| 42 | + CallbackInfoReturnable<Integer> cir) { |
| 43 | + AEA$blockedTasks.reset(); |
| 44 | + AEA$currentDetails = null; |
| 45 | + } |
| 46 | + |
| 47 | + @ModifyVariable(method = "executeCrafting", at = @At(value = "STORE"), name = "details") |
| 48 | + private IPatternDetails AEA$capturePatternAndResetFlag(IPatternDetails details) { |
| 49 | + this.AEA$currentDetails = details; |
| 50 | + this.AEA$pushedSuccessThisPattern = false; |
| 51 | + return details; |
| 52 | + } |
| 53 | + |
| 54 | + @Redirect( |
| 55 | + method = "executeCrafting", |
| 56 | + at = |
| 57 | + @At( |
| 58 | + value = "INVOKE", |
| 59 | + target = |
| 60 | + "Lappeng/api/networking/crafting/ICraftingProvider;pushPattern(Lappeng/api/crafting/IPatternDetails;[Lappeng/api/stacks/KeyCounter;)Z")) |
| 61 | + private boolean AEA$detectPushSuccess( |
| 62 | + ICraftingProvider instance, IPatternDetails iPatternDetails, KeyCounter[] keyCounters) { |
| 63 | + boolean success = instance.pushPattern(iPatternDetails, keyCounters); |
| 64 | + if (success) { |
| 65 | + this.AEA$pushedSuccessThisPattern = true; |
| 66 | + } |
| 67 | + return success; |
| 68 | + } |
| 69 | + |
| 70 | + @Inject( |
| 71 | + method = "executeCrafting", |
| 72 | + at = |
| 73 | + @At( |
| 74 | + value = "INVOKE", |
| 75 | + target = |
| 76 | + "Lappeng/crafting/execution/CraftingCpuHelper;reinjectPatternInputs(Lappeng/crafting/inv/ICraftingInventory;[Lappeng/api/stacks/KeyCounter;)V")) |
| 77 | + private void AEA$handleReinjectAndBlock( |
| 78 | + int maxPatterns, |
| 79 | + CraftingService craftingService, |
| 80 | + IEnergyService energyService, |
| 81 | + Level level, |
| 82 | + CallbackInfoReturnable<Integer> cir) { |
| 83 | + if (!this.AEA$pushedSuccessThisPattern && this.AEA$currentDetails != null) { |
| 84 | + for (var output : this.AEA$currentDetails.getOutputs()) { |
| 85 | + this.AEA$blockedTasks.add(output.what(), output.amount()); |
| 86 | + this.postChange(output.what()); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments