mod for the islanders smp
1package net.radsteve.islanders.mixin;
2
3import net.minecraft.entity.player.PlayerEntity;
4import net.minecraft.inventory.EnderChestInventory;
5import net.minecraft.item.ItemStack;
6import net.minecraft.registry.tag.ItemTags;
7import net.minecraft.screen.CraftingScreenHandler;
8import net.minecraft.screen.GenericContainerScreenHandler;
9import net.minecraft.screen.PlayerScreenHandler;
10import net.minecraft.screen.ScreenHandler;
11import net.minecraft.screen.slot.Slot;
12import net.minecraft.screen.slot.SlotActionType;
13import net.minecraft.util.collection.DefaultedList;
14import net.radsteve.islanders.SpecialItem;
15import org.spongepowered.asm.mixin.Final;
16import org.spongepowered.asm.mixin.Mixin;
17import org.spongepowered.asm.mixin.Shadow;
18import org.spongepowered.asm.mixin.injection.At;
19import org.spongepowered.asm.mixin.injection.Inject;
20import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
21
22@Mixin(ScreenHandler.class)
23public abstract class ScreenHandlerMixin {
24 @Final
25 @Shadow
26 public DefaultedList<Slot> slots;
27
28 @Shadow
29 public abstract ItemStack getCursorStack();
30
31 @Inject(method = "internalOnSlotClick", at = @At("HEAD"), cancellable = true)
32 private void islanders$internalOnSlotClick$shulkerBoxes(int slotIndex, int button, SlotActionType actionType, PlayerEntity player, CallbackInfo ci) {
33 if (!(ScreenHandler.class.cast(this) instanceof GenericContainerScreenHandler handler)) {
34 return;
35 }
36
37 if (!(handler.getInventory() instanceof EnderChestInventory)) {
38 return;
39 }
40
41 if (actionType == SlotActionType.QUICK_MOVE) {
42 final Slot slot = slots.get(slotIndex);
43 if (slot.getStack().getRegistryEntry().isIn(ItemTags.SHULKER_BOXES)) {
44 ci.cancel();
45 }
46
47 return;
48 }
49
50 final int actualScreenSlots = slots.size() - player.playerScreenHandler.slots.size() + 9 /* hotbar */;
51
52 if (actionType == SlotActionType.SWAP) {
53 final ItemStack stack = player.getInventory().getStack(button);
54 if (stack.getRegistryEntry().isIn(ItemTags.SHULKER_BOXES)) {
55 ci.cancel();
56 }
57
58 return;
59 }
60
61 if (slotIndex < 0 || slotIndex > actualScreenSlots) {
62 return;
63 }
64
65 if (getCursorStack().getRegistryEntry().isIn(ItemTags.SHULKER_BOXES)) {
66 ci.cancel();
67 }
68 }
69
70 @Inject(method = "internalOnSlotClick", at = @At("HEAD"), cancellable = true)
71 private void islanders$internalOnSlotClick$specialItems(int slotIndex, int button, SlotActionType actionType, PlayerEntity player, CallbackInfo ci) {
72 if ((Object) this instanceof PlayerScreenHandler) {
73 return;
74 }
75
76 if ((Object) this instanceof CraftingScreenHandler) {
77 return;
78 }
79
80 if (actionType == SlotActionType.QUICK_MOVE) {
81 final Slot slot = slots.get(slotIndex);
82 if (SpecialItem.Companion.find(slot.getStack()) != null) {
83 ci.cancel();
84 }
85
86 return;
87 }
88
89 final int actualScreenSlots = slots.size() - player.playerScreenHandler.slots.size() + 9 /* hotbar */;
90
91 if (actionType == SlotActionType.SWAP) {
92 final ItemStack stack = player.getInventory().getStack(button);
93 if (SpecialItem.Companion.find(stack) != null) {
94 ci.cancel();
95 }
96
97 return;
98 }
99
100 if (slotIndex < 0 || slotIndex > actualScreenSlots) {
101 return;
102 }
103
104 if (SpecialItem.Companion.find(getCursorStack()) != null) {
105 ci.cancel();
106 }
107 }
108}