From fb7ae6934fe6d5db6f056d8e183287d7f2f45305 Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Sat, 13 Jan 2024 04:55:42 +0100 Subject: [PATCH] new item/container selection methods --- .../sebseb7/autotrade/config/Configs.java | 12 ++- .../autotrade/event/KeybindCallbacks.java | 88 +++++++++++++++++++ 2 files changed, 96 insertions(+), 4 deletions(-) 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 bf0bbeb..620e7c7 100644 --- a/src/main/java/com/github/sebseb7/autotrade/config/Configs.java +++ b/src/main/java/com/github/sebseb7/autotrade/config/Configs.java @@ -20,6 +20,10 @@ public class Configs implements IConfigHandler { public static class Generic { public static final ConfigBoolean ENABLED = new ConfigBoolean("enabled", false, "Do auto trading with villagers in range"); + public static final ConfigBoolean ITEM_FRAME = new ConfigBoolean("selectUsingItemFrame", true, + "Select buy/sell items with item frames (max. distance 3) with items nametagged \"buy\" or \"sell\""); + public static final ConfigBoolean GLASS_BLOCK = new ConfigBoolean("selectUsingGlassBlock", false, + "Select input and output containers by placing red (input) and blue (output) stained glass blocks 3 blocks above them"); public static final ConfigBoolean ENABLE_SELL = new ConfigBoolean("enableSell", false, "Enable selling (if disabled emeralds are taken from the input container)"); public static final ConfigString SELL_ITEM = new ConfigString("sellItem", "minecraft:gold_ingot", @@ -51,10 +55,10 @@ public class Configs implements IConfigHandler { true, "true: Start the delay after the villager was unloaded; false: Start the delay after the trade has been completed"); - public static final ImmutableList OPTIONS = ImmutableList.of(ENABLED, ENABLE_SELL, SELL_ITEM, - SELL_LIMIT, ENABLE_BUY, BUY_ITEM, BUY_LIMIT, MAX_INPUT_ITEMS, INPUT_CONTAINER_X, INPUT_CONTAINER_Y, - INPUT_CONTAINER_Z, OUTPUT_CONTAINER_X, OUTPUT_CONTAINER_Y, OUTPUT_CONTAINER_Z, VOID_TRADING_DELAY, - VOID_TRADING_DELAY_AFTER_TELEPORT); + public static final ImmutableList OPTIONS = ImmutableList.of(ENABLED, ITEM_FRAME, GLASS_BLOCK, + ENABLE_SELL, SELL_ITEM, SELL_LIMIT, ENABLE_BUY, BUY_ITEM, BUY_LIMIT, MAX_INPUT_ITEMS, INPUT_CONTAINER_X, + INPUT_CONTAINER_Y, INPUT_CONTAINER_Z, OUTPUT_CONTAINER_X, OUTPUT_CONTAINER_Y, OUTPUT_CONTAINER_Z, + VOID_TRADING_DELAY, VOID_TRADING_DELAY_AFTER_TELEPORT); } public static void loadFromFile() { 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 09b2ca1..dcbeb5b 100644 --- a/src/main/java/com/github/sebseb7/autotrade/event/KeybindCallbacks.java +++ b/src/main/java/com/github/sebseb7/autotrade/event/KeybindCallbacks.java @@ -15,16 +15,20 @@ import fi.dy.masa.malilib.util.GuiUtils; import fi.dy.masa.malilib.util.InfoUtils; import java.util.HashMap; 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.nbt.NbtCompound; 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; @@ -37,6 +41,7 @@ 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; @@ -226,6 +231,81 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { return; } + if (Configs.Generic.GLASS_BLOCK.getBooleanValue()) { + + int playerX = (int) mc.player.getPos().getX(); + int playerZ = (int) mc.player.getPos().getZ(); + int playerY = (int) mc.player.getPos().getY(); + + for (int x = playerX - 6; x < playerX + 6; x += 1) { + for (int z = playerZ - 6; z < playerZ + 6; z += 1) { + for (int y = playerY - 6; y < playerY + 6; y += 1) { + BlockPos pos = new BlockPos(x, y, z); + if (mc.player.clientWorld.getBlockState(pos).isOf(Blocks.RED_STAINED_GLASS)) { + if ((x != Configs.Generic.INPUT_CONTAINER_X.getIntegerValue()) + || ((y - 3) != Configs.Generic.INPUT_CONTAINER_Y.getIntegerValue()) + || (z != Configs.Generic.INPUT_CONTAINER_Z.getIntegerValue())) { + Configs.Generic.INPUT_CONTAINER_X.setIntegerValue(x); + Configs.Generic.INPUT_CONTAINER_Y.setIntegerValue(y - 3); + Configs.Generic.INPUT_CONTAINER_Z.setIntegerValue(z); + InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, + "autotrade.message.input_container_set", x, y - 3, z); + } + break; + } + if (mc.player.clientWorld.getBlockState(pos).isOf(Blocks.BLUE_STAINED_GLASS)) { + if ((x != Configs.Generic.OUTPUT_CONTAINER_X.getIntegerValue()) + || ((y - 3) != Configs.Generic.OUTPUT_CONTAINER_Y.getIntegerValue()) + || (z != Configs.Generic.OUTPUT_CONTAINER_Z.getIntegerValue())) { + Configs.Generic.OUTPUT_CONTAINER_X.setIntegerValue(x); + Configs.Generic.OUTPUT_CONTAINER_Y.setIntegerValue(y - 3); + Configs.Generic.OUTPUT_CONTAINER_Z.setIntegerValue(z); + InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, + "autotrade.message.output_container_set", x, y - 3, z); + } + break; + } + } + } + } + } + + if (Configs.Generic.ITEM_FRAME.getBooleanValue()) { + + for (ItemFrameEntity entity : mc.player.clientWorld.getEntitiesByClass(ItemFrameEntity.class, + new Box(mc.player.getPos().getX() - 3, mc.player.getPos().getY() - 3, mc.player.getPos().getZ() - 3, + mc.player.getPos().getX() + 3, mc.player.getPos().getY() + 3, + mc.player.getPos().getZ() + 3), + EntityPredicates.VALID_ENTITY)) { + ItemStack stack = entity.getHeldItemStack(); + if (stack.hasNbt()) { + NbtCompound tag = stack.getNbt(); + NbtCompound elem = tag.getCompound("display"); + if (elem != null) { + if (elem.getString("Name").equals("\"sell\"")) { + String sellItem = Registries.ITEM.getId(stack.getItem()).toString(); + if (!Configs.Generic.SELL_ITEM.getStringValue().equals(sellItem)) { + InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, + "autotrade.message.sell_item_set", sellItem); + Configs.Generic.SELL_ITEM.setValueFromString(sellItem); + break; + } + } + if (elem.getString("Name").equals("\"buy\"")) { + String buyItem = Registries.ITEM.getId(stack.getItem()).toString(); + if (!Configs.Generic.BUY_ITEM.getStringValue().equals(buyItem)) { + InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, + "autotrade.message.buy_item_set", buyItem); + Configs.Generic.BUY_ITEM.setValueFromString(buyItem); + break; + } + } + } + } + + } + } + if (GuiUtils.getCurrentScreen() instanceof MerchantScreen) { MerchantScreen screen = (MerchantScreen) GuiUtils.getCurrentScreen(); if (state == false) { @@ -249,6 +329,10 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { mc.getNetworkHandler().sendPacket(new SelectMerchantTradeC2SPacket(i)); AutoTrade.sold += 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); } catch (Exception e) { @@ -263,6 +347,10 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { AutoTrade.bought += offer.getMaxUses(); mc.getNetworkHandler().sendPacket(new SelectMerchantTradeC2SPacket(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); } catch (Exception e) {