| 1 | package com.jellypudding.offlineclient.mixin;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.OfflineClient;
|
| 4 | import com.jellypudding.offlineclient.modules.player.ChestStealer;
|
| 5 | import com.jellypudding.offlineclient.modules.player.InventoryTweaks;
|
| 6 | import com.jellypudding.offlineclient.modules.render.BetterTooltips;
|
| 7 | import com.jellypudding.offlineclient.modules.render.ItemHighlight;
|
| 8 | import com.jellypudding.offlineclient.util.Modules;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.gui.components.Button;
|
| 11 | import net.minecraft.client.gui.screens.Screen;
|
| 12 | import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
|
| 13 | import net.minecraft.client.input.KeyEvent;
|
| 14 | import net.minecraft.client.input.MouseButtonEvent;
|
| 15 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 16 | import net.minecraft.world.inventory.ContainerInput;
|
| 17 | import net.minecraft.world.inventory.Slot;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import org.lwjgl.glfw.GLFW;
|
| 20 | import org.spongepowered.asm.mixin.Mixin;
|
| 21 | import org.spongepowered.asm.mixin.Shadow;
|
| 22 | import org.spongepowered.asm.mixin.injection.At;
|
| 23 | import org.spongepowered.asm.mixin.injection.Inject;
|
| 24 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
| 25 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
| 26 |
|
| 27 | @Mixin(AbstractContainerScreen.class)
|
| 28 | public abstract class AbstractContainerScreenMixin extends Screen {
|
| 29 |
|
| 30 | private static final int BUTTON_WIDTH = 40;
|
| 31 | private static final int BUTTON_HEIGHT = 14;
|
| 32 |
|
| 33 | private AbstractContainerScreenMixin(Component title) {
|
| 34 | super(title);
|
| 35 | }
|
| 36 |
|
| 37 | @Shadow
|
| 38 | protected Slot hoveredSlot;
|
| 39 |
|
| 40 | @Shadow
|
| 41 | protected int leftPos;
|
| 42 |
|
| 43 | @Shadow
|
| 44 | protected int topPos;
|
| 45 |
|
| 46 | @Shadow
|
| 47 | public abstract AbstractContainerMenu getMenu();
|
| 48 |
|
| 49 | // Two buttons above the container that run one pass on click.
|
| 50 | @Inject(method = "init", at = @At("TAIL"))
|
| 51 | private void onInit(CallbackInfo ci) {
|
| 52 | ChestStealer stealer = Modules.get(ChestStealer.class);
|
| 53 | if (stealer == null || !stealer.showsButtons()
|
| 54 | || !stealer.handles((Screen) (Object) this)) {
|
| 55 | return;
|
| 56 | }
|
| 57 | int y = topPos - BUTTON_HEIGHT - 2;
|
| 58 | addRenderableWidget(Button.builder(Component.literal("Steal"),
|
| 59 | button -> stealer.stealNow())
|
| 60 | .bounds(leftPos, y, BUTTON_WIDTH, BUTTON_HEIGHT).build());
|
| 61 | addRenderableWidget(Button.builder(Component.literal("Dump"),
|
| 62 | button -> stealer.dumpNow())
|
| 63 | .bounds(leftPos + BUTTON_WIDTH + 2, y, BUTTON_WIDTH, BUTTON_HEIGHT).build());
|
| 64 | }
|
| 65 |
|
| 66 | // The paint goes down before the item and sits under it.
|
| 67 | @Inject(method = "extractSlot", at = @At("HEAD"))
|
| 68 | private void onExtractSlot(GuiGraphicsExtractor graphics, Slot slot, int mouseX, int mouseY,
|
| 69 | CallbackInfo ci) {
|
| 70 | ItemHighlight highlight = Modules.get(ItemHighlight.class);
|
| 71 | if (highlight == null) {
|
| 72 | return;
|
| 73 | }
|
| 74 | int color = highlight.colorFor(slot.getItem());
|
| 75 | if (color != 0) {
|
| 76 | graphics.fill(slot.x, slot.y, slot.x + 16, slot.y + 16, color);
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | @Inject(method = "mouseClicked(Lnet/minecraft/client/input/MouseButtonEvent;Z)Z",
|
| 81 | at = @At("HEAD"), cancellable = true)
|
| 82 | private void onMouseClicked(MouseButtonEvent event, boolean doubleClick, CallbackInfoReturnable<Boolean> cir) {
|
| 83 | BetterTooltips tooltips = Modules.get(BetterTooltips.class);
|
| 84 | if (tooltips != null && tooltips.wantsToOpen(event) && openHovered(tooltips)) {
|
| 85 | cir.setReturnValue(true);
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | @Inject(method = "keyPressed(Lnet/minecraft/client/input/KeyEvent;)Z",
|
| 90 | at = @At("HEAD"), cancellable = true)
|
| 91 | private void onKeyPressed(KeyEvent event, CallbackInfoReturnable<Boolean> cir) {
|
| 92 | BetterTooltips tooltips = Modules.get(BetterTooltips.class);
|
| 93 | if (tooltips != null && tooltips.wantsToOpen(event) && openHovered(tooltips)) {
|
| 94 | cir.setReturnValue(true);
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | // Shift and a held left button move every stack the cursor is dragged over.
|
| 99 | @Inject(method = "mouseDragged(Lnet/minecraft/client/input/MouseButtonEvent;DD)Z",
|
| 100 | at = @At("HEAD"))
|
| 101 | private void onMouseDragged(MouseButtonEvent event, double dragX, double dragY,
|
| 102 | CallbackInfoReturnable<Boolean> cir) {
|
| 103 | InventoryTweaks tweaks = Modules.get(InventoryTweaks.class);
|
| 104 | if (tweaks == null || !tweaks.dragsStacks()
|
| 105 | || event.button() != GLFW.GLFW_MOUSE_BUTTON_LEFT || !event.hasShiftDown()) {
|
| 106 | return;
|
| 107 | }
|
| 108 | if (hoveredSlot == null || !hoveredSlot.hasItem() || !getMenu().getCarried().isEmpty()) {
|
| 109 | return;
|
| 110 | }
|
| 111 | OfflineClient.MC.gameMode.handleContainerInput(getMenu().containerId, hoveredSlot.index, 0,
|
| 112 | ContainerInput.QUICK_MOVE, OfflineClient.MC.player);
|
| 113 | }
|
| 114 |
|
| 115 | // Nothing opens whilst an item is on the cursor. A drop still lands.
|
| 116 | private boolean openHovered(BetterTooltips tooltips) {
|
| 117 | if (hoveredSlot == null || hoveredSlot.getItem().isEmpty() || !getMenu().getCarried().isEmpty()) {
|
| 118 | return false;
|
| 119 | }
|
| 120 | return tooltips.openContents(hoveredSlot.getItem());
|
| 121 | }
|
| 122 | }
|
| 123 |
|