diff --git a/src/main/java/club/mcams/carpet/mixin/rule/largeShulkerBox/ShulkerBoxBlockEntityMixin.java b/src/main/java/club/mcams/carpet/mixin/rule/largeShulkerBox/ShulkerBoxBlockEntityMixin.java index 6db45c8e..15722652 100644 --- a/src/main/java/club/mcams/carpet/mixin/rule/largeShulkerBox/ShulkerBoxBlockEntityMixin.java +++ b/src/main/java/club/mcams/carpet/mixin/rule/largeShulkerBox/ShulkerBoxBlockEntityMixin.java @@ -21,13 +21,16 @@ package club.mcams.carpet.mixin.rule.largeShulkerBox; import club.mcams.carpet.AmsServerSettings; +import club.mcams.carpet.screen.LargeShulkerBoxScreenHandler; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.block.entity.LootableContainerBlockEntity; import net.minecraft.block.entity.ShulkerBoxBlockEntity; +import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.SidedInventory; import net.minecraft.item.ItemStack; +import net.minecraft.screen.ScreenHandler; import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; @@ -92,4 +95,12 @@ private void getAvailableSlots(Direction side, CallbackInfoReturnable cir cir.cancel(); } } + + @Inject(method = "createScreenHandler", at = @At("HEAD"), cancellable = true) + private void createLargeShulkerBoxScreen(int syncId, PlayerInventory playerInventory, CallbackInfoReturnable cir) { + if (AmsServerSettings.largeShulkerBox) { + cir.setReturnValue(new LargeShulkerBoxScreenHandler(syncId, playerInventory, this)); + cir.cancel(); + } + } } \ No newline at end of file diff --git a/src/main/java/club/mcams/carpet/mixin/rule/largeShulkerBox/ShulkerBoxScreenHandlerMixin.java b/src/main/java/club/mcams/carpet/mixin/rule/largeShulkerBox/ShulkerBoxScreenHandlerMixin.java index 09058cf8..617bc6a4 100644 --- a/src/main/java/club/mcams/carpet/mixin/rule/largeShulkerBox/ShulkerBoxScreenHandlerMixin.java +++ b/src/main/java/club/mcams/carpet/mixin/rule/largeShulkerBox/ShulkerBoxScreenHandlerMixin.java @@ -42,41 +42,41 @@ public abstract class ShulkerBoxScreenHandlerMixin extends ScreenHandler { protected ShulkerBoxScreenHandlerMixin(@Nullable ScreenHandlerType type, int syncId) { super(type, syncId); } +// +// @ModifyArg( +// method = "(ILnet/minecraft/entity/player/PlayerInventory;Lnet/minecraft/inventory/Inventory;)V", +// at = @At( +// value = "INVOKE", +// target = "Lnet/minecraft/screen/ScreenHandler;(Lnet/minecraft/screen/ScreenHandlerType;I)V" +// ), +// index = 0 +// ) +// private static ScreenHandlerType getScreenHandlerType(ScreenHandlerType type) { +// if (!AmsServerSettings.largeShulkerBox) { +// return type; +// } +// return ScreenHandlerType.GENERIC_9X6; +// } - @ModifyArg( - method = "(ILnet/minecraft/entity/player/PlayerInventory;Lnet/minecraft/inventory/Inventory;)V", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/screen/ScreenHandler;(Lnet/minecraft/screen/ScreenHandlerType;I)V" - ), - index = 0 - ) - private static ScreenHandlerType getScreenHandlerType(ScreenHandlerType type) { - if (!AmsServerSettings.largeShulkerBox) { - return type; - } - return ScreenHandlerType.GENERIC_9X6; - } - - @Inject( - method = "(ILnet/minecraft/entity/player/PlayerInventory;Lnet/minecraft/inventory/Inventory;)V", - at = @At( - value = "JUMP", - opcode = Opcodes.GOTO, - ordinal = 1, - shift = At.Shift.BY, - by = 2 - ) - ) - private void init(int syncId, PlayerInventory playerInventory, Inventory inventory, CallbackInfo ci) { - if (!AmsServerSettings.largeShulkerBox || this.slots.size() != 9 * 3) { - return; - } - checkSize(inventory, 9 * 6); - for (int row = 3; row < 6; ++row) { - for (int column = 0; column < 9; ++column) { - this.addSlot(new ShulkerBoxSlot(inventory, column + row * 9, 8 + column * 18, 18 + row * 18)); - } - } - } +// @Inject( +// method = "(ILnet/minecraft/entity/player/PlayerInventory;Lnet/minecraft/inventory/Inventory;)V", +// at = @At( +// value = "JUMP", +// opcode = Opcodes.GOTO, +// ordinal = 1, +// shift = At.Shift.BY, +// by = 2 +// ) +// ) +// private void init(int syncId, PlayerInventory playerInventory, Inventory inventory, CallbackInfo ci) { +// if (!AmsServerSettings.largeShulkerBox || this.slots.size() != 9 * 3) { +// return; +// } +// checkSize(inventory, 9 * 6); +// for (int row = 3; row < 6; ++row) { +// for (int column = 0; column < 9; ++column) { +// this.addSlot(new ShulkerBoxSlot(inventory, column + row * 9, 8 + column * 18, 18 + row * 18)); +// } +// } +// } } \ No newline at end of file diff --git a/src/main/java/club/mcams/carpet/screen/LargeShulkerBoxScreenHandler.java b/src/main/java/club/mcams/carpet/screen/LargeShulkerBoxScreenHandler.java new file mode 100644 index 00000000..d56d52a1 --- /dev/null +++ b/src/main/java/club/mcams/carpet/screen/LargeShulkerBoxScreenHandler.java @@ -0,0 +1,70 @@ +package club.mcams.carpet.screen; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.inventory.Inventory; +import net.minecraft.item.ItemStack; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.screen.ScreenHandlerType; +import net.minecraft.screen.slot.ShulkerBoxSlot; +import net.minecraft.screen.slot.Slot; + +public class LargeShulkerBoxScreenHandler extends ScreenHandler { + private final Inventory inventory; + public LargeShulkerBoxScreenHandler(int syncId, PlayerInventory playerInventory, Inventory inventory) { + super(ScreenHandlerType.GENERIC_9X6, syncId); + checkSize(inventory, 9 * 6); + this.inventory = inventory; + inventory.onOpen(playerInventory.player); + + int i; + int j; + + for (i = 0; i < 6; ++i) { + for (j = 0; j < 9; ++j) { + this.addSlot(new ShulkerBoxSlot(inventory, j + i * 9, 8 + j * 18, 18 + i * 18)); + } + } + + for (i = 0; i < 3; ++i) { + for (j = 0; j < 9; ++j) { + this.addSlot(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 140 + i * 18)); + } + } + + for (i = 0; i < 9; ++i) { + this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 198)); + } + } + + public boolean canUse(PlayerEntity player) { + return this.inventory.canPlayerUse(player); + } + + public ItemStack transferSlot(PlayerEntity player, int index) { + ItemStack itemStack = ItemStack.EMPTY; + Slot slot = this.slots.get(index); + if (slot.hasStack()) { + ItemStack itemStack2 = slot.getStack(); + itemStack = itemStack2.copy(); + if (index < this.inventory.size()) { + if (!this.insertItem(itemStack2, this.inventory.size(), this.slots.size(), true)) { + return ItemStack.EMPTY; + } + } else if (!this.insertItem(itemStack2, 0, this.inventory.size(), false)) { + return ItemStack.EMPTY; + } + if (itemStack2.isEmpty()) { + slot.setStack(ItemStack.EMPTY); + } else { + slot.markDirty(); + } + } + return itemStack; + } + + public void close(PlayerEntity player) { + super.close(player); + this.inventory.onClose(player); + } +}