From e67249947eade3badb1957787a6b2d95b67597af Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Sat, 1 Jul 2023 04:36:16 +0200 Subject: [PATCH] alpha release --- .github/workflows/build.yml | 16 ++ .../sebseb7/autotrade/config/Configs.java | 25 +- .../sebseb7/autotrade/config/Hotkeys.java | 10 +- .../autotrade/event/KeybindCallbacks.java | 232 +++++++++++++++++- .../assets/autotrade/lang/en_us.json | 7 +- 5 files changed, 262 insertions(+), 28 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b856c56..fc25ecc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -15,3 +15,19 @@ jobs: uses: gradle/gradle-build-action@v2 - name: Execute Gradle build run: ./gradlew build + - run: mkdir staging && cp build/libs/*.jar staging + - run: cd target && md5sum *.jar > ../md5sum.txt + - run: echo "filename=`ls build/libs/*.jar |xargs basename`" >> $GITHUB_ENV + - uses: actions/upload-artifact@v3 + with: + name: ${{ env.filename }} + path: staging + - uses: actions/upload-artifact@v3 + with: + name: md5sum.txt + path: md5sum.txt + - name: Release + uses: softprops/action-gh-release@v1 + if: startsWith(github.ref, 'refs/tags/') + with: + files: build/libs/*.jar 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 99bb757..dbb0845 100644 --- a/src/main/java/com/github/sebseb7/autotrade/config/Configs.java +++ b/src/main/java/com/github/sebseb7/autotrade/config/Configs.java @@ -21,12 +21,12 @@ public class Configs implements IConfigHandler { public static final ConfigBoolean ENABLED = new ConfigBoolean("enabled", false, "Do auto trading with villagers in range"); public static final ConfigBoolean ENABLE_SELL = new ConfigBoolean("enableSell", false, - "Enable buying (if disabled emeralds from the emerald container are taken instead)"); - public static final ConfigString BUY_ITEM = new ConfigString("buyItem", "minecraft:gold_ingot", + "Enable selling (if disabled emeralds are taken from the input container)"); + public static final ConfigString SELL_ITEM = new ConfigString("sellItem", "minecraft:gold_ingot", "The item to sell for emerald."); public static final ConfigBoolean ENABLE_BUY = new ConfigBoolean("enableBuy", false, - "Enable selling (if disabled emeralds are placed in the emerald container)"); - public static final ConfigString SELL_ITEM = new ConfigString("sellItem", "minecraft:redstone", + "Enable buying (if disabled emeralds are placed in the output container)"); + public static final ConfigString BUY_ITEM = new ConfigString("buyItem", "minecraft:redstone", "The item to buy using emerald."); public static final ConfigInteger MAX_INPUT_ITEMS = new ConfigInteger("maxInputStacks", 9, 1, 35, "stacks to take from input container (or emerald container in buy-only mode)"); @@ -34,25 +34,18 @@ public class Configs implements IConfigHandler { 30000000, "Input container X (not used when sell disabled)"); public static final ConfigInteger INPUT_CONTAINER_Y = new ConfigInteger("inputContainerY", 0, -64, 320, "Input container Y (not used when sell disabled)"); - public static final ConfigInteger INPUT_CONTAINER_Z = new ConfigInteger("outputContainerZ", 0, -30000000, + public static final ConfigInteger INPUT_CONTAINER_Z = new ConfigInteger("inputContainerZ", 0, -30000000, 30000000, "Input container Z (not used when sell disabled)"); public static final ConfigInteger OUTPUT_CONTAINER_X = new ConfigInteger("outputContainerX", 0, -30000000, - 30000000, "Input container X (not used when buy disabled)"); + 30000000, "Output container X (not used when buy disabled)"); public static final ConfigInteger OUTPUT_CONTAINER_Y = new ConfigInteger("outputContainerY", 0, -64, 320, "Output container Y (not used when buy disabled)"); public static final ConfigInteger OUTPUT_CONTAINER_Z = new ConfigInteger("outputContainerZ", 0, -30000000, 30000000, "Output container Z (not used when buy disabled)"); - public static final ConfigInteger EMERALD_CONTAINER_X = new ConfigInteger("emeraldContainerX", 0, -30000000, - 30000000, "Emerald container X (not used when buy&sell enabled)"); - public static final ConfigInteger EMERALD_CONTAINER_Y = new ConfigInteger("emeraldContainerY", 0, -64, 320, - "Emerald container Y (not used when buy&sell enabled)"); - public static final ConfigInteger EMERALD_CONTAINER_Z = new ConfigInteger("emeraldContainerZ", 0, -30000000, - 30000000, "Emerald container Z (not used when buy&sell enabled)"); - public static final ImmutableList OPTIONS = ImmutableList.of(ENABLED, ENABLE_BUY, BUY_ITEM, - ENABLE_SELL, SELL_ITEM, MAX_INPUT_ITEMS, INPUT_CONTAINER_X, INPUT_CONTAINER_Y, INPUT_CONTAINER_Z, - OUTPUT_CONTAINER_X, OUTPUT_CONTAINER_Y, OUTPUT_CONTAINER_Z, EMERALD_CONTAINER_X, EMERALD_CONTAINER_Y, - EMERALD_CONTAINER_Z); + public static final ImmutableList OPTIONS = ImmutableList.of(ENABLED, ENABLE_SELL, SELL_ITEM, + ENABLE_BUY, BUY_ITEM, MAX_INPUT_ITEMS, INPUT_CONTAINER_X, INPUT_CONTAINER_Y, INPUT_CONTAINER_Z, + OUTPUT_CONTAINER_X, OUTPUT_CONTAINER_Y, OUTPUT_CONTAINER_Z); } public static void loadFromFile() { diff --git a/src/main/java/com/github/sebseb7/autotrade/config/Hotkeys.java b/src/main/java/com/github/sebseb7/autotrade/config/Hotkeys.java index faf7a71..34fa1e2 100644 --- a/src/main/java/com/github/sebseb7/autotrade/config/Hotkeys.java +++ b/src/main/java/com/github/sebseb7/autotrade/config/Hotkeys.java @@ -7,18 +7,16 @@ import java.util.List; public class Hotkeys { public static final ConfigHotkey TOGGLE_KEY = new ConfigHotkey("toggleTrading", "", "Enables / disables auto trading"); - public static final ConfigHotkey SET_BUY_KEY = new ConfigHotkey("setSellItem", "", + public static final ConfigHotkey SET_SELL_KEY = new ConfigHotkey("setSellItem", "", "Sets the item to sell from hotbar"); - public static final ConfigHotkey SET_SELL_KEY = new ConfigHotkey("setBuyItem", "", + public static final ConfigHotkey SET_BUY_KEY = new ConfigHotkey("setBuyItem", "", "Sets the item to buy from hotbar"); public static final ConfigHotkey SET_INPUT_KEY = new ConfigHotkey("setInputContainer", "", "Sets the input (item to sell) container"); public static final ConfigHotkey SET_OUTPUT_KEY = new ConfigHotkey("setOutputContainer", "", "Sets the output (item to buy) container"); - public static final ConfigHotkey SET_EMERALD_KEY = new ConfigHotkey("setEmeraldContainer", "", - "Set the emerald container"); public static final ConfigHotkey OPEN_GUI_SETTINGS = new ConfigHotkey("openGuiSettings", "", "Open the Config GUI"); - public static final List HOTKEY_LIST = ImmutableList.of(TOGGLE_KEY, SET_BUY_KEY, SET_SELL_KEY, - SET_INPUT_KEY, SET_OUTPUT_KEY, SET_EMERALD_KEY, OPEN_GUI_SETTINGS); + public static final List HOTKEY_LIST = ImmutableList.of(TOGGLE_KEY, SET_SELL_KEY, SET_BUY_KEY, + SET_INPUT_KEY, SET_OUTPUT_KEY, OPEN_GUI_SETTINGS); } 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 8307047..54b50b4 100644 --- a/src/main/java/com/github/sebseb7/autotrade/event/KeybindCallbacks.java +++ b/src/main/java/com/github/sebseb7/autotrade/event/KeybindCallbacks.java @@ -12,18 +12,45 @@ import fi.dy.masa.malilib.hotkeys.KeyAction; 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.Vector; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.ingame.GenericContainerScreen; import net.minecraft.client.gui.screen.ingame.HandledScreen; +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.passive.VillagerEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.network.packet.c2s.play.SelectMerchantTradeC2SPacket; +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.ActionResult; 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.Direction; +import net.minecraft.village.TradeOffer; +import net.minecraft.village.TradeOfferList; public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { private static final KeybindCallbacks INSTANCE = new KeybindCallbacks(); private Vector villagersInRange = new Vector(); + private boolean state = false; + private boolean inputInRange = false; + private boolean inputOpened = false; + private boolean outputInRange = false; + private boolean outputOpened = false; + public static KeybindCallbacks getInstance() { return INSTANCE; } @@ -47,6 +74,69 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { return cancel; } + private void processOutput(ScreenHandler handler) { + 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)) { + 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()); + } + } + } + } + + } + + private void processInput(ScreenHandler handler) { + 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)); + } + } + + 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()); + } + } + } + } + } + + } + private boolean onKeyActionImpl(KeyAction action, IKeybind key) { MinecraftClient mc = MinecraftClient.getInstance(); @@ -60,14 +150,40 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { ? "autotrade.message.toggled_mod_on" : "autotrade.message.toggled_mod_off"; InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, msg); - return true; } else if (key == Hotkeys.OPEN_GUI_SETTINGS.getKeybind()) { GuiBase.openGui(new GuiConfigs()); return true; - } + } else if (key == Hotkeys.SET_INPUT_KEY.getKeybind()) { + HitResult result = mc.player.raycast(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()); + InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, "autotrade.message.input_container_set", + blockHit.getBlockPos().getX(), blockHit.getBlockPos().getY(), blockHit.getBlockPos().getZ()); - if (this.functionalityEnabled() == false || (GuiUtils.getCurrentScreen() instanceof HandledScreen) == false) { - return false; + } + } else if (key == Hotkeys.SET_OUTPUT_KEY.getKeybind()) { + HitResult result = mc.player.raycast(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()); + InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, "autotrade.message.output_container_set", + blockHit.getBlockPos().getX(), blockHit.getBlockPos().getY(), blockHit.getBlockPos().getZ()); + } + } else if (key == Hotkeys.SET_BUY_KEY.getKeybind()) { + + String buyItem = Registries.ITEM.getId(mc.player.getInventory().getMainHandStack().getItem()).toString(); + 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.getInventory().getMainHandStack().getItem()).toString(); + InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, "autotrade.message.sell_item_set", sellItem); + Configs.Generic.SELL_ITEM.setValueFromString(sellItem); } return false; @@ -78,7 +194,76 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { if (this.functionalityEnabled() == false || mc.player == null) { return; } - mc.inGameHud.getChatHud().addToMessageHistory("here"); + + if (GuiUtils.getCurrentScreen() instanceof MerchantScreen) { + if (state == false) { + String sellItemStr = Configs.Generic.SELL_ITEM.getStringValue(); + String buyItemStr = Configs.Generic.BUY_ITEM.getStringValue(); + state = true; + MerchantScreen screen = (MerchantScreen) GuiUtils.getCurrentScreen(); + MerchantScreenHandler handler = screen.getScreenHandler(); + TradeOfferList offers = handler.getRecipes(); + for (int i = 0; i < offers.size(); i++) { + TradeOffer offer = offers.get(i); + ItemStack sellItem = offer.getSellItem(); + ItemStack buyItem = offer.getAdjustedFirstBuyItem(); + 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()) { + Slot slot = handler.getSlot(2); + handler.switchTo(i); + handler.setRecipeIndex(i); + mc.getNetworkHandler().sendPacket(new SelectMerchantTradeC2SPacket(i)); + try { + mc.interactionManager.clickSlot(handler.syncId, slot.id, 0, SlotActionType.QUICK_MOVE, + mc.player); + } catch (Exception e) { + System.out.println("err " + e.toString()); + } + } + if (buyId.equals(sellItemStr) && Configs.Generic.ENABLE_SELL.getBooleanValue()) { + Slot slot = handler.getSlot(2); + handler.switchTo(i); + handler.setRecipeIndex(i); + mc.getNetworkHandler().sendPacket(new SelectMerchantTradeC2SPacket(i)); + try { + mc.interactionManager.clickSlot(handler.syncId, slot.id, 0, SlotActionType.QUICK_MOVE, + mc.player); + } catch (Exception e) { + System.out.println("err " + e.toString()); + } + } + } + screen.close(); + } + return; + } + + if (GuiUtils.getCurrentScreen() instanceof ShulkerBoxScreen) { + ShulkerBoxScreen screen = (ShulkerBoxScreen) GuiUtils.getCurrentScreen(); + ShulkerBoxScreenHandler handler = screen.getScreenHandler(); + if (inputOpened) { + processInput(handler); + screen.close(); + } + if (outputOpened) { + processOutput(handler); + screen.close(); + } + } + if (GuiUtils.getCurrentScreen() instanceof GenericContainerScreen) { + GenericContainerScreen screen = (GenericContainerScreen) GuiUtils.getCurrentScreen(); + GenericContainerScreenHandler handler = screen.getScreenHandler(); + if (inputOpened) { + processInput(handler); + screen.close(); + } + if (outputOpened) { + processOutput(handler); + screen.close(); + } + } if (GuiUtils.getCurrentScreen() instanceof HandledScreen) { return; @@ -96,16 +281,53 @@ public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { found = true; newVillagersInRange.add(entity); mc.interactionManager.interactEntity(mc.player, entity, Hand.MAIN_HAND); + state = false; } } } } } + for (Entity entity : villagersInRange) { if ((entity.getPos().distanceTo(mc.player.getPos()) < 4) == false) { newVillagersInRange.remove(entity); } } villagersInRange = newVillagersInRange; + + 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 ((input.toCenterPos().distanceTo(mc.player.getPos()) < 4) && (inputInRange == false)) { + inputInRange = true; + ActionResult result = mc.interactionManager.interactBlock(mc.player, Hand.MAIN_HAND, + new BlockHitResult(input.toCenterPos(), Direction.UP, input, false)); + if (result.isAccepted()) { + inputOpened = true; + } + } + if ((output.toCenterPos().distanceTo(mc.player.getPos()) < 4) && (outputInRange == false)) { + outputInRange = true; + ActionResult result = mc.interactionManager.interactBlock(mc.player, Hand.MAIN_HAND, + new BlockHitResult(output.toCenterPos(), Direction.UP, output, false)); + if (result.isAccepted()) { + outputOpened = true; + } + } + + if (input.toCenterPos().distanceTo(mc.player.getPos()) > 5) { + inputOpened = false; + inputInRange = false; + } + if (output.toCenterPos().distanceTo(mc.player.getPos()) > 5) { + outputOpened = false; + outputInRange = false; + } + } } diff --git a/src/main/resources/assets/autotrade/lang/en_us.json b/src/main/resources/assets/autotrade/lang/en_us.json index 8da8191..821d7ef 100644 --- a/src/main/resources/assets/autotrade/lang/en_us.json +++ b/src/main/resources/assets/autotrade/lang/en_us.json @@ -6,5 +6,10 @@ "autotrade.gui.title.configs": "AutoTrade", "autotrade.message.toggled_mod_off": "Toggled Auto Trade §cOFF", - "autotrade.message.toggled_mod_on": "Toggled Auto Trade §aON" + "autotrade.message.toggled_mod_on": "Toggled Auto Trade §aON", + + "autotrade.message.sell_item_set": "Item to sell: %s", + "autotrade.message.buy_item_set": "Item to buy: %s", + "autotrade.message.input_container_set": "Input Container now at: %d %d %d", + "autotrade.message.output_container_set": "Output Container now at: %d %d %d" }