|
| 1 | +package dev.dfonline.codeclient.dev; |
| 2 | + |
| 3 | +import dev.dfonline.codeclient.CodeClient; |
| 4 | +import dev.dfonline.codeclient.config.Config; |
| 5 | +import dev.dfonline.codeclient.dev.menu.customchest.CustomChestField; |
| 6 | +import dev.dfonline.codeclient.dev.menu.customchest.CustomChestMenu; |
| 7 | +import dev.dfonline.codeclient.hypercube.item.*; |
| 8 | +import dev.dfonline.codeclient.hypercube.item.Number; |
| 9 | +import net.minecraft.client.gui.DrawContext; |
| 10 | +import net.minecraft.client.gui.screen.ingame.HandledScreen; |
| 11 | +import net.minecraft.item.ItemStack; |
| 12 | +import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket; |
| 13 | +import net.minecraft.screen.slot.Slot; |
| 14 | +import net.minecraft.screen.slot.SlotActionType; |
| 15 | +import net.minecraft.text.Text; |
| 16 | +import net.minecraft.util.Identifier; |
| 17 | +import org.lwjgl.glfw.GLFW; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +public class InsertOverlay { |
| 23 | + private static AddWidget selectedSlot = null; |
| 24 | + public static boolean isCodeChest = false; |
| 25 | + private static int screenX = 0; |
| 26 | + private static int screenY = 0; |
| 27 | + |
| 28 | + public static void reset() { |
| 29 | + selectedSlot = null; |
| 30 | + isCodeChest = false; |
| 31 | + screenX = 0; |
| 32 | + screenY = 0; |
| 33 | + } |
| 34 | + |
| 35 | + private static boolean overlayOpen() { |
| 36 | + return Config.getConfig().InsertOverlay && isCodeChest && selectedSlot != null; |
| 37 | + } |
| 38 | + |
| 39 | + public static void render(DrawContext context, int mouseX, int mouseY, HandledScreen<?> handledScreen, int x, int y) { |
| 40 | + screenX = x; |
| 41 | + screenY = y; |
| 42 | + if (overlayOpen()) |
| 43 | + selectedSlot.render(context, mouseX, mouseY, handledScreen); |
| 44 | + } |
| 45 | + |
| 46 | + public static boolean mouseClicked(double mouseX, double mouseY, int button, HandledScreen<?> screen) { |
| 47 | + if (overlayOpen()) { |
| 48 | + boolean stayOpen = selectedSlot.mouseClicked(mouseX, mouseY, button, screen); |
| 49 | + if (!stayOpen) { |
| 50 | + selectedSlot.close(); |
| 51 | + } |
| 52 | + return true; |
| 53 | + } |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + public static boolean keyPressed(int keyCode, int scanCode, int modifiers) { |
| 58 | + if (overlayOpen()) { |
| 59 | + return selectedSlot.keyPressed(keyCode, scanCode, modifiers); |
| 60 | + } |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + public static boolean keyReleased(int keyCode, int scanCode, int modifiers) { |
| 65 | + if (overlayOpen()) return selectedSlot.keyReleased(keyCode, scanCode, modifiers); |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + public static boolean charTyped(char chr, int modifiers) { |
| 70 | + if (overlayOpen()) return selectedSlot.charTyped(chr, modifiers); |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + public static boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) { |
| 75 | + if(overlayOpen()) return selectedSlot.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount); |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + public static void clickSlot(Slot slot, SlotActionType actionType) { |
| 80 | + if (Config.getConfig().InsertOverlay && isCodeChest && actionType == SlotActionType.PICKUP && !slot.hasStack() && CodeClient.MC.player.currentScreenHandler.getCursorStack().isEmpty()) |
| 81 | + selectedSlot = new AddWidget(slot, () -> selectedSlot = null); |
| 82 | + else if (selectedSlot != null) selectedSlot.close(); |
| 83 | + } |
| 84 | + |
| 85 | + private static class AddWidget { |
| 86 | + private int x; |
| 87 | + private int y; |
| 88 | + private int width; |
| 89 | + private int height = 26; |
| 90 | + private List<Option> options; |
| 91 | + private Slot slot; |
| 92 | + private CustomChestField<?> field; |
| 93 | + private VarItem varItem = null; |
| 94 | + private final Runnable close; |
| 95 | + |
| 96 | + public AddWidget(Slot slot, Runnable close) { |
| 97 | + this.slot = slot; |
| 98 | + this.close = close; |
| 99 | + this.x = slot.x + 16; |
| 100 | + this.y = slot.y - 5; |
| 101 | + var opt = new ArrayList<Option>(); |
| 102 | + |
| 103 | + int i = 0; |
| 104 | + for (var type : new VarItem[]{ |
| 105 | + new dev.dfonline.codeclient.hypercube.item.Text(), |
| 106 | + new Component(), |
| 107 | + new Number(), |
| 108 | + new Location(), |
| 109 | + new Vector(), |
| 110 | + new Sound(), |
| 111 | +// new Particle(), |
| 112 | + new Potion(), |
| 113 | + }) { |
| 114 | + opt.add(new Option(type, x + 5 + i++ * 18, y + 5)); |
| 115 | + } |
| 116 | + width = i * 18 + 8; |
| 117 | + height = 2 * 18 + 8; |
| 118 | + i = 0; |
| 119 | + for (var type : new VarItem[]{ |
| 120 | + new Variable(), |
| 121 | +// new GameValue(), |
| 122 | +// new Parameter(), |
| 123 | + }) { |
| 124 | + opt.add(new Option(type, x + 5 + i++ * 18, y + 18 + 5)); |
| 125 | + } |
| 126 | + |
| 127 | + options = opt; |
| 128 | + } |
| 129 | + |
| 130 | + private void setSlot(ItemStack stack) { |
| 131 | + var mc = CodeClient.MC; |
| 132 | + var currentScreen = mc.currentScreen; |
| 133 | + var manager = mc.interactionManager; |
| 134 | + if (manager == null || mc.getNetworkHandler() == null) return; |
| 135 | + if (currentScreen instanceof HandledScreen<?> screen) { |
| 136 | + var player = mc.player; |
| 137 | + var sync = screen.getScreenHandler().syncId; |
| 138 | + manager.clickSlot(sync, slot.id, 0, SlotActionType.SWAP, player); |
| 139 | + mc.getNetworkHandler().sendPacket(new CreativeInventoryActionC2SPacket(36, stack)); |
| 140 | + manager.clickSlot(sync, slot.id, 0, SlotActionType.SWAP, player); |
| 141 | + manager.clickSlot(sync, 54, 0, SlotActionType.QUICK_CRAFT, player); |
| 142 | + slot.setStack(stack); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + public void close() { |
| 147 | + if (field != null && varItem != null) { |
| 148 | + setSlot(varItem.toStack()); |
| 149 | + } |
| 150 | + this.close.run(); |
| 151 | + } |
| 152 | + |
| 153 | + public void render(DrawContext context, int mouseX, int mouseY, HandledScreen<?> handledScreen) { |
| 154 | + context.getMatrices().push(); |
| 155 | + context.getMatrices().translate(0.0F, 0.0F, 900.0F); |
| 156 | + context.drawGuiTexture(new Identifier("recipe_book/overlay_recipe"), x, y, width, height); |
| 157 | + if (field != null) { |
| 158 | + field.render(context, mouseX, mouseY, 0); |
| 159 | + } else { |
| 160 | + for (var option : options) { |
| 161 | + context.drawItem(option.type.getIcon(), option.x, option.y); |
| 162 | + } |
| 163 | + } |
| 164 | + context.getMatrices().pop(); |
| 165 | +// new Identifier("recipe_book/crafting_overlay_highlighted") |
| 166 | +// new Identifier("recipe_book/crafting_overlay") |
| 167 | +// RecipeAlternativesWidget.CRAFTING_OVERLAY_HIGHLIGHTED_TEXTURE : RecipeAlternativesWidget.CRAFTING_OVERLAY_TEXTURE |
| 168 | + } |
| 169 | + |
| 170 | + public boolean mouseClicked(double mouseX, double mouseY, int button, HandledScreen<?> screen) { |
| 171 | + int x = screenX; |
| 172 | + int y = screenY; |
| 173 | + if (mouseX > this.x + x && mouseY > this.y + y && mouseX < this.x + x + width && mouseY < this.y + y + height) { |
| 174 | + if (field != null) { |
| 175 | + boolean b = field.mouseClicked(mouseX - x, mouseY - y, button); |
| 176 | + setSlot(varItem.toStack()); |
| 177 | + return b; |
| 178 | + } |
| 179 | + for (var option : options) { |
| 180 | + if (mouseX > option.x + x && mouseY > option.y + y && mouseX < option.x + x + 16 && mouseY < option.y + y + 21) { |
| 181 | + varItem = option.type; |
| 182 | + var stack = varItem.toStack(); |
| 183 | + setSlot(stack); |
| 184 | + if (screen instanceof CustomChestMenu customChestMenu) { |
| 185 | + close(); |
| 186 | + customChestMenu.update(); |
| 187 | + return true; |
| 188 | + } |
| 189 | + this.width = 150; |
| 190 | + this.height = 26; |
| 191 | + field = new CustomChestField<>(CodeClient.MC.textRenderer, this.x + 5, this.y + 5, this.width - 10, this.height - 10, Text.literal(""), varItem); |
| 192 | + return true; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + return false; |
| 197 | + } |
| 198 | + |
| 199 | + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { |
| 200 | + if (field != null) { |
| 201 | + if(keyCode != GLFW.GLFW_KEY_ENTER) field.keyPressed(keyCode, scanCode, modifiers); |
| 202 | + return true; |
| 203 | + } |
| 204 | + return false; |
| 205 | + } |
| 206 | + |
| 207 | + public boolean keyReleased(int keyCode, int scanCode, int modifiers) { |
| 208 | + if (field != null) field.keyReleased(keyCode, scanCode, modifiers); |
| 209 | + if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_ESCAPE) { |
| 210 | + close(); |
| 211 | + return true; |
| 212 | + } |
| 213 | + return false; |
| 214 | + } |
| 215 | + |
| 216 | + public boolean charTyped(char chr, int modifiers) { |
| 217 | + if (field != null) return field.charTyped(chr, modifiers); |
| 218 | + return false; |
| 219 | + } |
| 220 | + |
| 221 | + public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) { |
| 222 | + if (field != null) return field.mouseScrolled(mouseX - screenX, mouseY - screenY, horizontalAmount, verticalAmount); |
| 223 | + return false; |
| 224 | + } |
| 225 | + |
| 226 | + |
| 227 | + private record Option(VarItem type, int x, int y) { |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments