|
| 1 | +package com.cak.pattern_schematics.mixin; |
| 2 | + |
| 3 | +import com.cak.pattern_schematics.registry.PatternSchematicsRegistry; |
| 4 | +import com.simibubi.create.AllItems; |
| 5 | +import com.simibubi.create.content.schematics.cannon.SchematicannonBlockEntity; |
| 6 | +import com.simibubi.create.content.schematics.cannon.SchematicannonInventory; |
| 7 | +import net.minecraft.world.item.Item; |
| 8 | +import net.minecraft.world.item.ItemStack; |
| 9 | +import org.spongepowered.asm.mixin.Mixin; |
| 10 | +import org.spongepowered.asm.mixin.Shadow; |
| 11 | +import org.spongepowered.asm.mixin.Unique; |
| 12 | +import org.spongepowered.asm.mixin.injection.At; |
| 13 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 14 | +import org.spongepowered.asm.mixin.injection.Redirect; |
| 15 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 16 | + |
| 17 | +/**Change the second inventory set item call to be the pattern schematic if there was a pattern schematic previously present*/ |
| 18 | +@Mixin(value = SchematicannonBlockEntity.class, remap = false) |
| 19 | +public class SchematicannonBlockEntityMixin { |
| 20 | + |
| 21 | + @Shadow public SchematicannonInventory inventory; |
| 22 | + @Unique |
| 23 | + private boolean pattern_schematics$currentThreadIsOfPatternSchematic = false; |
| 24 | + |
| 25 | + @Inject(method = "finishedPrinting", at = @At("HEAD")) |
| 26 | + private void finishedPrinting(CallbackInfo ci) { |
| 27 | + pattern_schematics$currentThreadIsOfPatternSchematic = !inventory.getStackInSlot(0).isEmpty() && |
| 28 | + inventory.getStackInSlot(0).is(PatternSchematicsRegistry.PATTERN_SCHEMATIC.get()); |
| 29 | + } |
| 30 | + |
| 31 | + @Redirect(method = "finishedPrinting", at = @At(value = "INVOKE", target = "Lcom/simibubi/create/content/schematics/cannon/SchematicannonInventory;setStackInSlot(ILnet/minecraft/world/item/ItemStack;)V", remap = true)) |
| 32 | + private void setStackInSlot(SchematicannonInventory instance, int slot, ItemStack stack) { |
| 33 | + if (slot != 1) { |
| 34 | + instance.setStackInSlot(slot, stack); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + Item resultItem = pattern_schematics$currentThreadIsOfPatternSchematic ? |
| 39 | + PatternSchematicsRegistry.EMPTY_PATTERN_SCHEMATIC.get() : |
| 40 | + AllItems.EMPTY_SCHEMATIC.get(); |
| 41 | + |
| 42 | + //Check if the result slot is either empty or matching |
| 43 | + ItemStack current = inventory.getStackInSlot(1); |
| 44 | + boolean resultSlotIsAvailable = current.isEmpty() || |
| 45 | + current.is(resultItem); |
| 46 | + |
| 47 | + int putSlot = resultSlotIsAvailable ? 1 : 0; |
| 48 | + inventory.setStackInSlot(putSlot, resultItem.getDefaultInstance()); |
| 49 | + } |
| 50 | + |
| 51 | +} |
0 commit comments