From dfd5eb3df9f7a76f5f34b8a873d37326fa12d14b Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Thu, 29 Jun 2023 22:32:57 +0200 Subject: [PATCH] interact with villagers in range --- .../github/sebseb7/autotrade/InitHandler.java | 3 ++ .../sebseb7/autotrade/config/Hotkeys.java | 2 +- .../autotrade/event/KeybindCallbacks.java | 45 ++++++++++++++++++- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/sebseb7/autotrade/InitHandler.java b/src/main/java/com/github/sebseb7/autotrade/InitHandler.java index 4684833..342c13a 100644 --- a/src/main/java/com/github/sebseb7/autotrade/InitHandler.java +++ b/src/main/java/com/github/sebseb7/autotrade/InitHandler.java @@ -5,6 +5,7 @@ import com.github.sebseb7.autotrade.event.InputHandler; import com.github.sebseb7.autotrade.event.KeybindCallbacks; import fi.dy.masa.malilib.config.ConfigManager; import fi.dy.masa.malilib.event.InputEventHandler; +import fi.dy.masa.malilib.event.TickHandler; import fi.dy.masa.malilib.interfaces.IInitializationHandler; public class InitHandler implements IInitializationHandler { @@ -15,6 +16,8 @@ public class InitHandler implements IInitializationHandler { InputHandler handler = new InputHandler(); InputEventHandler.getKeybindManager().registerKeybindProvider(handler); + TickHandler.getInstance().registerClientTickHandler(KeybindCallbacks.getInstance()); + KeybindCallbacks.getInstance().setCallbacks(); } } 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 d61131d..faf7a71 100644 --- a/src/main/java/com/github/sebseb7/autotrade/config/Hotkeys.java +++ b/src/main/java/com/github/sebseb7/autotrade/config/Hotkeys.java @@ -14,7 +14,7 @@ public class Hotkeys { 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 bought) container"); + "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"); 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 942aec1..8307047 100644 --- a/src/main/java/com/github/sebseb7/autotrade/event/KeybindCallbacks.java +++ b/src/main/java/com/github/sebseb7/autotrade/event/KeybindCallbacks.java @@ -9,14 +9,21 @@ import fi.dy.masa.malilib.gui.Message; import fi.dy.masa.malilib.hotkeys.IHotkeyCallback; import fi.dy.masa.malilib.hotkeys.IKeybind; 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.Vector; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.ingame.HandledScreen; +import net.minecraft.entity.Entity; +import net.minecraft.entity.passive.VillagerEntity; +import net.minecraft.util.Hand; -public class KeybindCallbacks implements IHotkeyCallback { +public class KeybindCallbacks implements IHotkeyCallback, IClientTickHandler { private static final KeybindCallbacks INSTANCE = new KeybindCallbacks(); + private Vector villagersInRange = new Vector(); + public static KeybindCallbacks getInstance() { return INSTANCE; } @@ -65,4 +72,40 @@ public class KeybindCallbacks implements IHotkeyCallback { return false; } + + @Override + public void onClientTick(MinecraftClient mc) { + if (this.functionalityEnabled() == false || mc.player == null) { + return; + } + mc.inGameHud.getChatHud().addToMessageHistory("here"); + + if (GuiUtils.getCurrentScreen() instanceof HandledScreen) { + return; + } + + boolean found = false; + + Vector newVillagersInRange = new Vector(villagersInRange); + + for (Entity entity : mc.player.clientWorld.getEntities()) { + if (entity instanceof VillagerEntity) { + if (entity.getPos().distanceTo(mc.player.getPos()) < 3) { + if (found == false) { + if (newVillagersInRange.contains(entity) == false) { + found = true; + newVillagersInRange.add(entity); + mc.interactionManager.interactEntity(mc.player, entity, Hand.MAIN_HAND); + } + } + } + } + } + for (Entity entity : villagersInRange) { + if ((entity.getPos().distanceTo(mc.player.getPos()) < 4) == false) { + newVillagersInRange.remove(entity); + } + } + villagersInRange = newVillagersInRange; + } }