diff --git a/.gitignore b/.gitignore index 7f6823b..5290444 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .gradle build/ +.jdk21/ \ No newline at end of file diff --git a/build.gradle b/build.gradle index 372c7c3..7116e11 100644 --- a/build.gradle +++ b/build.gradle @@ -1,16 +1,27 @@ plugins { - id 'fabric-loom' version '1.11-SNAPSHOT' + id 'net.fabricmc.fabric-loom' version '1.16-SNAPSHOT' id 'com.diffplug.spotless' version '6.19.0' id "com.modrinth.minotaur" version "2.+" } java { - sourceCompatibility = JavaVersion.VERSION_21 - targetCompatibility = JavaVersion.VERSION_21 + toolchain { + languageVersion = JavaLanguageVersion.of(25) + } } repositories { - maven { url 'https://masa.dy.fi/maven' } + exclusiveContent { + forRepository { + maven { + name = 'Modrinth' + url = 'https://api.modrinth.com/maven' + } + } + filter { + includeGroup 'maven.modrinth' + } + } maven { url 'https://maven.terraformersmc.com/releases/' } maven { url 'https://jitpack.io' } flatDir { @@ -20,10 +31,9 @@ repositories { dependencies { minecraft "com.mojang:minecraft:${project.minecraft_version}" - mappings "net.fabricmc:yarn:${project.mappings_version}:v2" - modImplementation "net.fabricmc:fabric-loader:${project.fabric_loader_version}" - modImplementation "fi.dy.masa.malilib:malilib-fabric-${project.minecraft_version_out}:${project.malilib_version}" - modCompileOnly "com.terraformersmc:modmenu:${project.mod_menu_version}" + implementation "net.fabricmc:fabric-loader:${project.fabric_loader_version}" + implementation "maven.modrinth:malilib:${project.malilib_version}" + compileOnly "com.terraformersmc:modmenu:${project.mod_menu_version}" } group = project.group + "." + project.mod_id @@ -50,7 +60,6 @@ processResources { tasks.withType(JavaCompile).configureEach { it.options.encoding = "UTF-8" - it.options.release = 21 } spotless { @@ -63,13 +72,13 @@ spotless { } } -import com.modrinth.minotaur.dependencies.ModDependency modrinth { token = System.getenv("MODRINTH_TOKEN") syncBodyFrom = rootProject.file("README.md").text projectId = 'C1naQCmt' - uploadFile = remapJar - gameVersions = ['1.21.11'] + // Loom 1.16+ with net.fabricmc.fabric-loom (unobfuscated MC) does not create remapJar; ship the standard jar + uploadFile = tasks.jar + gameVersions = ['26.1.2'] loaders = ['fabric'] dependencies = [] } diff --git a/gradle.properties b/gradle.properties index a70474a..3e2de67 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,7 @@ # Done to increase the memory available to gradle. org.gradle.jvmargs=-Xmx1G +org.gradle.parallel=true +org.gradle.configuration-cache=false group = com.github.sebseb7 mod_id = autotrade @@ -7,14 +9,13 @@ mod_name = AutoTrade author = sebseb7 mod_file_name = autotrade-fabric -mod_version = 0.0.11 +mod_version = 0.0.12 -malilib_version = 0.21.0 -minecraft_version_min = 1.21.11 -minecraft_version_out = 1.21.1 +malilib_version = 0.28.2 +minecraft_version_min = 26.1.2 +minecraft_version_out = 26.1.2 -minecraft_version = 1.21.11 +minecraft_version = 26.1.2 -mappings_version = 1.21.11+build.2 -fabric_loader_version = 0.16.10 -mod_menu_version = 15.0.0 +fabric_loader_version = 0.19.2 +mod_menu_version = 18.0.0-alpha.8 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 3ae1e2f..c61a118 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.1-bin.zip +networkTimeout=10000 +validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/settings.gradle b/settings.gradle index 027b233..c167b5a 100644 --- a/settings.gradle +++ b/settings.gradle @@ -4,6 +4,14 @@ pluginManagement { name = 'Fabric' url = 'https://maven.fabricmc.net/' } + mavenCentral() gradlePluginPortal() } } + +// Lets Gradle 21 run the build while a JDK 25 toolchain is used to compile (required by 26.1 Mod Menu, etc.) +plugins { + id 'org.gradle.toolchains.foojay-resolver-convention' version '1.0.0' +} + +rootProject.name = 'autotrade-fabric' diff --git a/src/main/java/com/github/sebseb7/autotrade/config/Configs.java b/src/main/java/com/github/sebseb7/autotrade/config/Configs.java index c1f0d49..6a21462 100644 --- a/src/main/java/com/github/sebseb7/autotrade/config/Configs.java +++ b/src/main/java/com/github/sebseb7/autotrade/config/Configs.java @@ -68,7 +68,7 @@ public class Configs implements IConfigHandler { File configFile = new File(getConfigDirectory(), CONFIG_FILE_NAME); if (configFile.exists() && configFile.isFile() && configFile.canRead()) { - JsonElement element = JsonUtils.parseJsonFile(configFile); + JsonElement element = JsonUtils.parseJsonFile(configFile.toPath()); if (element != null && element.isJsonObject()) { JsonObject root = element.getAsJsonObject(); @@ -88,7 +88,7 @@ public class Configs implements IConfigHandler { ConfigUtils.writeConfigBase(root, "Generic", Generic.OPTIONS); ConfigUtils.writeConfigBase(root, "Hotkeys", Hotkeys.HOTKEY_LIST); - JsonUtils.writeJsonToFile(root, new File(dir, CONFIG_FILE_NAME)); + JsonUtils.writeJsonToFile(root, new File(dir, CONFIG_FILE_NAME).toPath()); } } diff --git a/src/main/java/com/github/sebseb7/autotrade/event/KeybindCallbacks.java b/src/main/java/com/github/sebseb7/autotrade/event/KeybindCallbacks.java index 097a49a..7506bf2 100644 --- a/src/main/java/com/github/sebseb7/autotrade/event/KeybindCallbacks.java +++ b/src/main/java/com/github/sebseb7/autotrade/event/KeybindCallbacks.java @@ -14,40 +14,41 @@ import fi.dy.masa.malilib.interfaces.IClientTickHandler; import fi.dy.masa.malilib.util.GuiUtils; import fi.dy.masa.malilib.util.InfoUtils; import java.util.HashMap; +import java.util.List; import java.util.Vector; -import net.minecraft.block.Blocks; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.screen.ingame.GenericContainerScreen; -import net.minecraft.client.gui.screen.ingame.MerchantScreen; -import net.minecraft.client.gui.screen.ingame.ShulkerBoxScreen; -import net.minecraft.entity.Entity; -import net.minecraft.entity.decoration.ItemFrameEntity; -import net.minecraft.entity.passive.VillagerEntity; -import net.minecraft.entity.passive.WanderingTraderEntity; -import net.minecraft.entity.player.PlayerInventory; -import net.minecraft.item.ItemStack; -import net.minecraft.network.packet.c2s.play.SelectMerchantTradeC2SPacket; -import net.minecraft.predicate.entity.EntityPredicates; -import net.minecraft.registry.Registries; -import net.minecraft.screen.GenericContainerScreenHandler; -import net.minecraft.screen.MerchantScreenHandler; -import net.minecraft.screen.ScreenHandler; -import net.minecraft.screen.ShulkerBoxScreenHandler; -import net.minecraft.screen.slot.Slot; -import net.minecraft.screen.slot.SlotActionType; -import net.minecraft.util.Hand; -import net.minecraft.util.hit.BlockHitResult; -import net.minecraft.util.hit.HitResult; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Box; -import net.minecraft.util.math.Direction; -import net.minecraft.village.TradeOffer; -import net.minecraft.village.TradeOfferList; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.screens.inventory.ContainerScreen; +import net.minecraft.client.gui.screens.inventory.MerchantScreen; +import net.minecraft.client.gui.screens.inventory.ShulkerBoxScreen; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.protocol.game.ServerboundSelectTradePacket; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.decoration.ItemFrame; +import net.minecraft.world.entity.npc.villager.Villager; +import net.minecraft.world.entity.npc.wanderingtrader.WanderingTrader; +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.inventory.ContainerInput; +import net.minecraft.world.inventory.MerchantMenu; +import net.minecraft.world.inventory.ShulkerBoxMenu; +import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.trading.MerchantOffer; +import net.minecraft.world.item.trading.MerchantOffers; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.phys.AABB; +import net.minecraft.world.phys.BlockHitResult; +import net.minecraft.world.phys.EntityHitResult; +import net.minecraft.world.phys.HitResult; +import net.minecraft.world.phys.Vec3; public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { private static final KeybindCallbacks INSTANCE = new KeybindCallbacks(); - private Vector villagersInRange = new Vector(); + private Vector villagersInRange = new Vector<>(); private int villagerActive = 0; private boolean state = false; @@ -78,80 +79,78 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { @Override public boolean onKeyAction(KeyAction action, IKeybind key) { - boolean cancel = this.onKeyActionImpl(action, key); - return cancel; + return this.onKeyActionImpl(action, key); } - private void processOutput(ScreenHandler handler) { - outputOpened = false; + private static String id(net.minecraft.world.item.Item item) { + return BuiltInRegistries.ITEM.getKey(item).toString(); + } + private void quickMoveResultSlot(Minecraft mc, AbstractContainerMenu menu, int slotIndex) { + Slot slot = menu.getSlot(slotIndex); + mc.gameMode.handleContainerInput(menu.containerId, slot.index, 0, ContainerInput.QUICK_MOVE, mc.player); + } + + private void processOutput(AbstractContainerMenu menu, Inventory playerInv) { + outputOpened = false; String itemToPlace = "minecraft:emerald"; if (Configs.Generic.ENABLE_BUY.getBooleanValue()) { itemToPlace = Configs.Generic.BUY_ITEM.getStringValue(); } - - for (int i = 0; i < handler.slots.size(); i++) { - if (handler.getSlot(i).inventory instanceof PlayerInventory) { - if (Registries.ITEM.getId(handler.getSlot(i).getStack().getItem()).toString().equals(itemToPlace)) { + Minecraft mc = Minecraft.getInstance(); + for (int i = 0; i < menu.slots.size(); i++) { + Slot s = menu.getSlot(i); + if (s.container == playerInv) { + if (id(s.getItem().getItem()).equals(itemToPlace)) { try { - MinecraftClient.getInstance().interactionManager.clickSlot(handler.syncId, - handler.getSlot(i).id, 0, SlotActionType.QUICK_MOVE, - MinecraftClient.getInstance().player); + quickMoveResultSlot(mc, menu, i); } catch (Exception e) { - System.out.println("err " + e.toString()); + System.out.println("err " + e); } } } } - } - private void processInput(ScreenHandler handler) { + private void processInput(AbstractContainerMenu menu, Inventory playerInv) { inputOpened = false; - - HashMap inventory = new HashMap(); - - for (int i = 0; i < handler.slots.size(); i++) { - if (handler.getSlot(i).inventory instanceof PlayerInventory) { - inventory.put(Registries.ITEM.getId(handler.getSlot(i).getStack().getItem()).toString(), - handler.getSlot(i).getStack().getCount() + inventory.getOrDefault( - Registries.ITEM.getId(handler.getSlot(i).getStack().getItem()).toString(), 0)); + HashMap inventory = new HashMap<>(); + for (int i = 0; i < menu.slots.size(); i++) { + Slot s = menu.getSlot(i); + if (s.container == playerInv) { + String k = id(s.getItem().getItem()); + inventory.put(k, s.getItem().getCount() + inventory.getOrDefault(k, 0)); } } - String itemToTake = "minecraft:emerald"; if (Configs.Generic.ENABLE_SELL.getBooleanValue()) { itemToTake = Configs.Generic.SELL_ITEM.getStringValue(); } - int inputCount = inventory.getOrDefault(itemToTake, 0); - - for (int i = 0; i < handler.slots.size(); i++) { - if ((handler.getSlot(i).inventory instanceof PlayerInventory) == false) { - if (Registries.ITEM.getId(handler.getSlot(i).getStack().getItem()).toString().equals(itemToTake)) { - if (inputCount < (Configs.Generic.MAX_INPUT_ITEMS.getIntegerValue() * 64)) { - inputCount += handler.getSlot(i).getStack().getCount(); - try { - MinecraftClient.getInstance().interactionManager.clickSlot(handler.syncId, - handler.getSlot(i).id, 0, SlotActionType.QUICK_MOVE, - MinecraftClient.getInstance().player); - } catch (Exception e) { - System.out.println("err " + e.toString()); - } + Minecraft mc = Minecraft.getInstance(); + for (int i = 0; i < menu.slots.size(); i++) { + Slot s = menu.getSlot(i); + if (s.container == playerInv) { + continue; + } + if (id(s.getItem().getItem()).equals(itemToTake)) { + if (inputCount < (Configs.Generic.MAX_INPUT_ITEMS.getIntegerValue() * 64)) { + inputCount += s.getItem().getCount(); + try { + quickMoveResultSlot(mc, menu, i); + } catch (Exception e) { + System.out.println("err " + e); } } } } - } private boolean onKeyActionImpl(KeyAction action, IKeybind key) { - MinecraftClient mc = MinecraftClient.getInstance(); - - if (mc.player == null || mc.world == null) { + Minecraft mc = Minecraft.getInstance(); + if (mc.player == null || mc.level == null) { return false; } - if (key == Hotkeys.TOGGLE_KEY.getKeybind()) { Configs.Generic.ENABLED.toggleBooleanValue(); String msg = this.functionalityEnabled() @@ -167,85 +166,75 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { GuiBase.openGui(new GuiConfigs()); return true; } else if (key == Hotkeys.SET_INPUT_KEY.getKeybind()) { - HitResult result = mc.player.raycast(20.0D, 0.0F, false); + HitResult result = mc.player.pick(20.0D, 0.0F, false); if (result.getType() == HitResult.Type.BLOCK) { BlockHitResult blockHit = (BlockHitResult) result; - Configs.Generic.INPUT_CONTAINER_X.setIntegerValue(blockHit.getBlockPos().getX()); - Configs.Generic.INPUT_CONTAINER_Y.setIntegerValue(blockHit.getBlockPos().getY()); - Configs.Generic.INPUT_CONTAINER_Z.setIntegerValue(blockHit.getBlockPos().getZ()); + BlockPos p = blockHit.getBlockPos(); + Configs.Generic.INPUT_CONTAINER_X.setIntegerValue(p.getX()); + Configs.Generic.INPUT_CONTAINER_Y.setIntegerValue(p.getY()); + Configs.Generic.INPUT_CONTAINER_Z.setIntegerValue(p.getZ()); InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, "autotrade.message.input_container_set", - blockHit.getBlockPos().getX(), blockHit.getBlockPos().getY(), blockHit.getBlockPos().getZ()); - + p.getX(), p.getY(), p.getZ()); } } else if (key == Hotkeys.SET_OUTPUT_KEY.getKeybind()) { - HitResult result = mc.player.raycast(20.0D, 0.0F, false); + HitResult result = mc.player.pick(20.0D, 0.0F, false); if (result.getType() == HitResult.Type.BLOCK) { BlockHitResult blockHit = (BlockHitResult) result; - Configs.Generic.OUTPUT_CONTAINER_X.setIntegerValue(blockHit.getBlockPos().getX()); - Configs.Generic.OUTPUT_CONTAINER_Y.setIntegerValue(blockHit.getBlockPos().getY()); - Configs.Generic.OUTPUT_CONTAINER_Z.setIntegerValue(blockHit.getBlockPos().getZ()); + BlockPos p = blockHit.getBlockPos(); + Configs.Generic.OUTPUT_CONTAINER_X.setIntegerValue(p.getX()); + Configs.Generic.OUTPUT_CONTAINER_Y.setIntegerValue(p.getY()); + Configs.Generic.OUTPUT_CONTAINER_Z.setIntegerValue(p.getZ()); InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, "autotrade.message.output_container_set", - blockHit.getBlockPos().getX(), blockHit.getBlockPos().getY(), blockHit.getBlockPos().getZ()); + p.getX(), p.getY(), p.getZ()); } } else if (key == Hotkeys.SET_BUY_KEY.getKeybind()) { - String buyItem = Registries.ITEM.getId(mc.player.getMainHandStack().getItem()).toString(); + String buyItem = id(mc.player.getItemInHand(InteractionHand.MAIN_HAND).getItem()); InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, "autotrade.message.buy_item_set", buyItem); Configs.Generic.BUY_ITEM.setValueFromString(buyItem); } else if (key == Hotkeys.SET_SELL_KEY.getKeybind()) { - String sellItem = Registries.ITEM.getId(mc.player.getMainHandStack().getItem()).toString(); + String sellItem = id(mc.player.getItemInHand(InteractionHand.MAIN_HAND).getItem()); InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, "autotrade.message.sell_item_set", sellItem); Configs.Generic.SELL_ITEM.setValueFromString(sellItem); } - return false; } @Override - public void onClientTick(MinecraftClient mc) { - + public void onClientTick(Minecraft mc) { if (voidDelay > 0) { - if (Configs.Generic.VOID_TRADING_DELAY_AFTER_TELEPORT.getBooleanValue()) { boolean found = false; - for (Entity entity : mc.world.getEntities()) { + for (Entity entity : mc.level.entitiesForRendering()) { if (entity.getId() == villagerActive) { found = true; } } - if (!found) { voidDelay--; } } else { voidDelay--; } - return; } - if (containerDelay > 0) { - containerDelay--; } - if (this.functionalityEnabled() == false || mc.player == null) { return; } - + Inventory plInv = mc.player.getInventory(); if (Configs.Generic.GLASS_BLOCK.getBooleanValue()) { - int playerX = (int) mc.player.getX(); int playerZ = (int) mc.player.getZ(); int playerY = (int) mc.player.getY(); - int selectorOffset = Configs.Generic.SELECTOR_OFFSET.getIntegerValue(); int absSelectorOffset = Math.abs(selectorOffset); - for (int x = playerX - (absSelectorOffset + 3); x < playerX + (absSelectorOffset + 3); x += 1) { for (int z = playerZ - (absSelectorOffset + 3); z < playerZ + (absSelectorOffset + 3); z += 1) { for (int y = playerY - (absSelectorOffset + 3); y < playerY + (absSelectorOffset + 3); y += 1) { BlockPos pos = new BlockPos(x, y, z); - if (mc.world.getBlockState(pos).isOf(Blocks.RED_STAINED_GLASS)) { + if (mc.level.getBlockState(pos).getBlock() == Blocks.RED_STAINED_GLASS) { if ((x != Configs.Generic.INPUT_CONTAINER_X.getIntegerValue()) || ((y - selectorOffset) != Configs.Generic.INPUT_CONTAINER_Y.getIntegerValue()) || (z != Configs.Generic.INPUT_CONTAINER_Z.getIntegerValue())) { @@ -257,7 +246,7 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { } break; } - if (mc.world.getBlockState(pos).isOf(Blocks.BLUE_STAINED_GLASS)) { + if (mc.level.getBlockState(pos).getBlock() == Blocks.BLUE_STAINED_GLASS) { if ((x != Configs.Generic.OUTPUT_CONTAINER_X.getIntegerValue()) || ((y - selectorOffset) != Configs.Generic.OUTPUT_CONTAINER_Y.getIntegerValue()) || (z != Configs.Generic.OUTPUT_CONTAINER_Z.getIntegerValue())) { @@ -273,17 +262,17 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { } } } - if (Configs.Generic.ITEM_FRAME.getBooleanValue()) { - - for (ItemFrameEntity entity : mc.world.getEntitiesByClass( - ItemFrameEntity.class, new Box(mc.player.getX() - 3, mc.player.getY() - 3, mc.player.getZ() - 3, - mc.player.getX() + 3, mc.player.getY() + 3, mc.player.getZ() + 3), - EntityPredicates.VALID_ENTITY)) { - ItemStack stack = entity.getHeldItemStack(); - String customName = stack.getName().getString(); + Vec3 pm = new Vec3(mc.player.getX(), mc.player.getY(), mc.player.getZ()); + AABB box = new AABB(pm.subtract(3, 3, 3), pm.add(3, 3, 3)); + @SuppressWarnings("unchecked") + List frames = (List) (List) mc.level.getEntities((Entity) null, box, + e -> e instanceof ItemFrame && e.isAlive()); + for (ItemFrame entity : frames) { + ItemStack stack = entity.getItem(); + String customName = stack.getHoverName().getString(); if ("sell".equalsIgnoreCase(customName) || "\"sell\"".equals(customName)) { - String sellItem = Registries.ITEM.getId(stack.getItem()).toString(); + String sellItem = id(stack.getItem()); if (!Configs.Generic.SELL_ITEM.getStringValue().equals(sellItem)) { InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, "autotrade.message.sell_item_set", sellItem); @@ -292,7 +281,7 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { } } if ("buy".equalsIgnoreCase(customName) || "\"buy\"".equals(customName)) { - String buyItem = Registries.ITEM.getId(stack.getItem()).toString(); + String buyItem = id(stack.getItem()); if (!Configs.Generic.BUY_ITEM.getStringValue().equals(buyItem)) { InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, "autotrade.message.buy_item_set", buyItem); @@ -300,106 +289,84 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { break; } } - } } - - if (GuiUtils.getCurrentScreen() instanceof MerchantScreen) { - MerchantScreen screen = (MerchantScreen) GuiUtils.getCurrentScreen(); - if (state == false) { + if (GuiUtils.getCurrentScreen() instanceof MerchantScreen screen) { + if (!state) { String sellItemStr = Configs.Generic.SELL_ITEM.getStringValue(); String buyItemStr = Configs.Generic.BUY_ITEM.getStringValue(); state = true; - MerchantScreenHandler handler = screen.getScreenHandler(); - TradeOfferList offers = handler.getRecipes(); + MerchantMenu menu = screen.getMenu(); + MerchantOffers offers = menu.getOffers(); for (int i = 0; i < offers.size(); i++) { - TradeOffer offer = offers.get(i); - ItemStack sellItem = offer.getSellItem(); - ItemStack buyItem = offer.getDisplayedFirstBuyItem(); - String sellId = Registries.ITEM.getId(sellItem.getItem()).toString(); - String buyId = Registries.ITEM.getId(buyItem.getItem()).toString(); - - if (sellId.equals(buyItemStr) && Configs.Generic.ENABLE_BUY.getBooleanValue() - && buyItem.getCount() <= Configs.Generic.BUY_LIMIT.getIntegerValue()) { - Slot slot = handler.getSlot(2); - handler.switchTo(i); - handler.setRecipeIndex(i); - mc.getNetworkHandler().sendPacket(new SelectMerchantTradeC2SPacket(i)); + MerchantOffer offer = offers.get(i); + String costA = id(offer.getCostA().getItem()); + String resultI = id(offer.getResult().getItem()); + // buying from villager: configured buy item matches the trade result + if (resultI.equals(buyItemStr) && Configs.Generic.ENABLE_BUY.getBooleanValue() + && offer.getResult().getCount() <= Configs.Generic.BUY_LIMIT.getIntegerValue()) { + Slot slot = menu.getSlot(2); + menu.setSelectionHint(i); + mc.player.connection.send(new ServerboundSelectTradePacket(i)); AutoTrade.bought += offer.getMaxUses(); try { - /* - * if (slot.hasStack()) { System.out.println("buy " + - * slot.getStack().getCount()); } - */ - mc.interactionManager.clickSlot(handler.syncId, slot.id, 0, SlotActionType.QUICK_MOVE, - mc.player); + quickMoveResultSlot(mc, menu, slot.index); } catch (Exception e) { - System.out.println("err " + e.toString()); + System.out.println("err " + e); } } - if (buyId.equals(sellItemStr) && Configs.Generic.ENABLE_SELL.getBooleanValue() - && buyItem.getCount() <= Configs.Generic.SELL_LIMIT.getIntegerValue()) { - Slot slot = handler.getSlot(2); - handler.switchTo(i); - handler.setRecipeIndex(i); + // "sell" to villager: cost matches configured sell list + if (costA.equals(sellItemStr) && Configs.Generic.ENABLE_SELL.getBooleanValue() + && offer.getCostA().getCount() <= Configs.Generic.SELL_LIMIT.getIntegerValue()) { + Slot slot = menu.getSlot(2); + menu.setSelectionHint(i); AutoTrade.sold += offer.getMaxUses(); - mc.getNetworkHandler().sendPacket(new SelectMerchantTradeC2SPacket(i)); + mc.player.connection.send(new ServerboundSelectTradePacket(i)); try { - /* - * if (slot.hasStack()) { System.out.println("sell " + - * slot.getStack().getCount()); } - */ - mc.interactionManager.clickSlot(handler.syncId, slot.id, 0, SlotActionType.QUICK_MOVE, - mc.player); + quickMoveResultSlot(mc, menu, slot.index); } catch (Exception e) { - System.out.println("err " + e.toString()); + System.out.println("err " + e); } } } } - screen.close(); + screen.onClose(); inputInRange = false; outputInRange = false; return; } - - if (GuiUtils.getCurrentScreen() instanceof ShulkerBoxScreen) { - ShulkerBoxScreen screen = (ShulkerBoxScreen) GuiUtils.getCurrentScreen(); - ShulkerBoxScreenHandler handler = screen.getScreenHandler(); + if (GuiUtils.getCurrentScreen() instanceof ShulkerBoxScreen sbs) { + ShulkerBoxMenu m = sbs.getMenu(); if ((containerDelay == 0) && inputOpened) { - processInput(handler); - screen.close(); + processInput(m, plInv); + sbs.onClose(); } if ((containerDelay == 0) && outputOpened) { - processOutput(handler); - screen.close(); + processOutput(m, plInv); + sbs.onClose(); } - } - if (GuiUtils.getCurrentScreen() instanceof GenericContainerScreen) { - GenericContainerScreen screen = (GenericContainerScreen) GuiUtils.getCurrentScreen(); - GenericContainerScreenHandler handler = screen.getScreenHandler(); + } else if (GuiUtils.getCurrentScreen() instanceof ContainerScreen cs) { + AbstractContainerMenu m = cs.getMenu(); if ((containerDelay == 0) && inputOpened) { - processInput(handler); - screen.close(); + processInput(m, plInv); + cs.onClose(); } if ((containerDelay == 0) && outputOpened) { - processOutput(handler); - screen.close(); + processOutput(m, plInv); + cs.onClose(); } } - boolean found = false; - - Vector newVillagersInRange = new Vector(villagersInRange); - - for (Entity entity : mc.world.getEntities()) { - if (entity instanceof VillagerEntity || entity instanceof WanderingTraderEntity) { - if (entity.squaredDistanceTo(mc.player) < (2.5f * 2.5f)) { - if (found == false) { - if (newVillagersInRange.contains(entity) == false) { + Vector newVillagersInRange = new Vector<>(villagersInRange); + for (Entity entity : mc.level.entitiesForRendering()) { + if (entity instanceof Villager || entity instanceof WanderingTrader) { + if (entity.distanceToSqr(mc.player) < (2.5f * 2.5f)) { + if (!found) { + if (!newVillagersInRange.contains(entity)) { found = true; newVillagersInRange.add(entity); - mc.interactionManager.interactEntity(mc.player, entity, Hand.MAIN_HAND); + EntityHitResult ehr = new EntityHitResult(entity, entity.position()); + mc.gameMode.interact(mc.player, entity, ehr, InteractionHand.MAIN_HAND); voidDelay = Configs.Generic.VOID_TRADING_DELAY.getIntegerValue(); villagerActive = entity.getId(); state = false; @@ -409,9 +376,8 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { } } } - for (Entity entity : villagersInRange) { - if ((entity.squaredDistanceTo(mc.player) < 16.0D) == false) { + if (entity.distanceToSqr(mc.player) >= 16.0D) { newVillagersInRange.remove(entity); } } @@ -419,57 +385,51 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { if (found) { return; } - BlockPos input = new BlockPos(Configs.Generic.INPUT_CONTAINER_X.getIntegerValue(), Configs.Generic.INPUT_CONTAINER_Y.getIntegerValue(), Configs.Generic.INPUT_CONTAINER_Z.getIntegerValue()); - BlockPos output = new BlockPos(Configs.Generic.OUTPUT_CONTAINER_X.getIntegerValue(), Configs.Generic.OUTPUT_CONTAINER_Y.getIntegerValue(), Configs.Generic.OUTPUT_CONTAINER_Z.getIntegerValue()); - - if ((mc.player.squaredDistanceTo(input.toCenterPos()) < 16.0D) && (inputInRange == false)) { + Vec3 ic = input.getCenter(); + Vec3 oc = output.getCenter(); + if ((mc.player.distanceToSqr(ic) < 16.0D) && (inputInRange == false)) { inputInRange = true; - mc.interactionManager.interactBlock(mc.player, Hand.MAIN_HAND, - new BlockHitResult(input.toCenterPos(), Direction.UP, input, false)); + mc.gameMode.useItemOn(mc.player, InteractionHand.MAIN_HAND, + new BlockHitResult(ic, Direction.UP, input, false)); containerDelay = Configs.Generic.CONTAINER_CLOSE_DELAY.getIntegerValue(); inputOpened = true; return; } - if ((mc.player.squaredDistanceTo(output.toCenterPos()) < 16.0D) && (outputInRange == false)) { + if ((mc.player.distanceToSqr(oc) < 16.0D) && (outputInRange == false)) { outputInRange = true; - mc.interactionManager.interactBlock(mc.player, Hand.MAIN_HAND, - new BlockHitResult(output.toCenterPos(), Direction.UP, output, false)); + mc.gameMode.useItemOn(mc.player, InteractionHand.MAIN_HAND, + new BlockHitResult(oc, Direction.UP, output, false)); containerDelay = Configs.Generic.CONTAINER_CLOSE_DELAY.getIntegerValue(); outputOpened = true; return; } - - if (mc.player.squaredDistanceTo(input.toCenterPos()) > 25.0D) { + if (mc.player.distanceToSqr(ic) > 25.0D) { inputOpened = false; inputInRange = false; } - if (mc.player.squaredDistanceTo(output.toCenterPos()) > 25.0D) { + if (mc.player.distanceToSqr(oc) > 25.0D) { outputOpened = false; outputInRange = false; } - tickCount++; if (tickCount > 200) { tickCount = 0; - villagersInRange = new Vector(); + villagersInRange = new Vector<>(); inputInRange = false; outputInRange = false; - if (GuiUtils.getCurrentScreen() instanceof MerchantScreen) { - GuiUtils.getCurrentScreen().close(); - } - if (GuiUtils.getCurrentScreen() instanceof ShulkerBoxScreen) { - GuiUtils.getCurrentScreen().close(); - } - if (GuiUtils.getCurrentScreen() instanceof GenericContainerScreen) { - GuiUtils.getCurrentScreen().close(); + var cur = GuiUtils.getCurrentScreen(); + if (cur != null) { + if (cur instanceof MerchantScreen || cur instanceof ShulkerBoxScreen + || cur instanceof ContainerScreen) { + cur.onClose(); + } } } - } }