|
| 1 | +package dev.dfonline.codeclient.dev.menu; |
| 2 | + |
| 3 | +import dev.dfonline.codeclient.ChestFeature; |
| 4 | +import dev.dfonline.codeclient.Feature; |
| 5 | +import dev.dfonline.codeclient.Utility; |
| 6 | +import dev.dfonline.codeclient.config.Config; |
| 7 | +import net.minecraft.client.MinecraftClient; |
| 8 | +import net.minecraft.client.gui.screen.Screen; |
| 9 | +import net.minecraft.client.gui.screen.ingame.HandledScreen; |
| 10 | +import net.minecraft.client.network.ClientPlayerEntity; |
| 11 | +import net.minecraft.item.ItemStack; |
| 12 | +import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket; |
| 13 | +import net.minecraft.screen.ScreenHandler; |
| 14 | +import net.minecraft.screen.slot.Slot; |
| 15 | +import net.minecraft.screen.slot.SlotActionType; |
| 16 | + |
| 17 | +import java.util.Optional; |
| 18 | + |
| 19 | +public class AdvancedMiddleClickFeature extends Feature { |
| 20 | + |
| 21 | + @Override |
| 22 | + public boolean enabled() { |
| 23 | + return activated(); |
| 24 | + } |
| 25 | + |
| 26 | + public static boolean activated() { |
| 27 | + return Config.getConfig().AdvancedMiddleClick; |
| 28 | + } |
| 29 | + |
| 30 | + public ChestFeature makeChestFeature(HandledScreen<?> screen) { |
| 31 | + return new AdvancedMiddleClick(screen); |
| 32 | + } |
| 33 | + |
| 34 | + public static ItemStack getCopy(Slot slot, ItemStack cursor) { |
| 35 | + int max = slot.getStack().getMaxCount(); |
| 36 | + if (cursor.isEmpty() && slot.hasStack()) { |
| 37 | + int count = 1; |
| 38 | + if (Screen.hasShiftDown()) count = max; |
| 39 | + return slot.getStack().copyWithCount(count); |
| 40 | + } else if (!cursor.isEmpty() && compare(slot.getStack(), cursor)) { |
| 41 | + int count = Math.min(cursor.getCount()+1, max); |
| 42 | + if (Screen.hasShiftDown()) count = max; |
| 43 | + return slot.getStack().copyWithCount(count); |
| 44 | + } |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + private static boolean compare(ItemStack a, ItemStack b) { |
| 49 | + if (a.itemMatches(b.getRegistryEntry())) { |
| 50 | + if (a.getName().equals(b.getName())) { |
| 51 | + return a.getComponents().equals(b.getComponents()); |
| 52 | + } |
| 53 | + } |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + private static class AdvancedMiddleClick extends ChestFeature { |
| 58 | + public AdvancedMiddleClick(HandledScreen<?> screen) { |
| 59 | + super(screen); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean clickSlot(Slot slot, int button, SlotActionType actionType, int syncId, int revision) { |
| 64 | + ClientPlayerEntity player = MinecraftClient.getInstance().player; |
| 65 | + var manager = MinecraftClient.getInstance().interactionManager; |
| 66 | + var network = MinecraftClient.getInstance().getNetworkHandler(); |
| 67 | + if (!(manager == null || network == null || player == null) && actionType == SlotActionType.CLONE) { |
| 68 | + ScreenHandler handler = player.currentScreenHandler; |
| 69 | + |
| 70 | + // find an empty slot to use |
| 71 | + var emptySlotId = player.getInventory().getEmptySlot(); |
| 72 | + int external = -1; // if the code had to use an external slot |
| 73 | + if (emptySlotId == -1) { |
| 74 | + // no empty slot, try to create an available slot temporarily. |
| 75 | + Optional<Slot> candidate = handler.slots.stream().filter(s -> !s.hasStack()).findFirst(); |
| 76 | + if (candidate.isPresent()) { |
| 77 | + Slot selected = candidate.get(); |
| 78 | + external = selected.id; |
| 79 | + manager.clickSlot(syncId, external, 0, SlotActionType.SWAP, player); |
| 80 | + emptySlotId = 0; |
| 81 | + } else { |
| 82 | + // not possible to run the following code, return to vanilla behaviour |
| 83 | + return false; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + int convertedSlotId = Utility.getRemoteSlot(emptySlotId)-9+(handler.slots.size()-36); |
| 88 | + Slot emptySlot = handler.getSlot(convertedSlotId); |
| 89 | + var stack = getCopy(slot, handler.getCursorStack()); // get the stack to set the slot to |
| 90 | + if (stack == null) return false; |
| 91 | + |
| 92 | + if (!handler.getCursorStack().isEmpty()) { |
| 93 | + manager.clickSlot(syncId, convertedSlotId, 0, SlotActionType.PICKUP, player); |
| 94 | + emptySlot.getStack().setCount(0); // hides the temporary item in the empty slot. |
| 95 | + } |
| 96 | + |
| 97 | + // create the item for the server |
| 98 | + network.sendPacket(new CreativeInventoryActionC2SPacket( |
| 99 | + Utility.getRemoteSlot(emptySlotId), stack) |
| 100 | + ); |
| 101 | + |
| 102 | + manager.clickSlot(syncId, convertedSlotId, 0, SlotActionType.PICKUP, player); |
| 103 | + |
| 104 | + if (external != -1) { |
| 105 | + manager.clickSlot(syncId, external, 0, SlotActionType.SWAP, player); |
| 106 | + } else { |
| 107 | + emptySlot.getStack().setCount(0); // hides the temporary item in the empty slot. |
| 108 | + } |
| 109 | + |
| 110 | + handler.setCursorStack(stack); // shows the item in your cursor before the server agrees. |
| 111 | + |
| 112 | + |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + } |
| 121 | +} |
0 commit comments