diff --git a/src/main/java/me/alpha432/oyvey/features/modules/combat/ClickPearl.java b/src/main/java/me/alpha432/oyvey/features/modules/combat/ClickPearl.java new file mode 100644 index 00000000..cb0853e0 --- /dev/null +++ b/src/main/java/me/alpha432/oyvey/features/modules/combat/ClickPearl.java @@ -0,0 +1,34 @@ +package me.alpha432.oyvey.features.modules.combat; + +import me.alpha432.oyvey.features.modules.Module; +import me.alpha432.oyvey.features.settings.Setting; +import me.alpha432.oyvey.util.inventory.InventoryUtil; +import me.alpha432.oyvey.util.inventory.Result; +import net.minecraft.world.item.Items; + +import static me.alpha432.oyvey.util.inventory.InventoryUtil.FULL_SCOPE; +import static me.alpha432.oyvey.util.inventory.InventoryUtil.HOTBAR_SCOPE; + +public class ClickPearl extends Module { + private final Setting inventory = bool("Inventory", false); + + public ClickPearl() { + super("ClickPearl", "Throws a pearl when enabled.", Category.COMBAT); + } + + @Override + public void onEnable() { + disable(); + + if (nullCheck()) return; + + int last = InventoryUtil.selected(); + Result result = InventoryUtil.find(Items.ENDER_PEARL, inventory.getValue() ? FULL_SCOPE : HOTBAR_SCOPE); + if (!result.found()) + return; + + InventoryUtil.swap(result); + mc.gameMode.useItem(mc.player, result.hand()); + InventoryUtil.swapBack(last, result); + } +} diff --git a/src/main/java/me/alpha432/oyvey/manager/ModuleManager.java b/src/main/java/me/alpha432/oyvey/manager/ModuleManager.java index 0213772d..ba437fca 100644 --- a/src/main/java/me/alpha432/oyvey/manager/ModuleManager.java +++ b/src/main/java/me/alpha432/oyvey/manager/ModuleManager.java @@ -9,6 +9,7 @@ import me.alpha432.oyvey.features.modules.client.ClickGui; import me.alpha432.oyvey.features.modules.client.HudEditor; import me.alpha432.oyvey.features.modules.client.Notifications; +import me.alpha432.oyvey.features.modules.combat.ClickPearl; import me.alpha432.oyvey.features.modules.combat.Criticals; import me.alpha432.oyvey.features.modules.hud.Coordinates; import me.alpha432.oyvey.features.modules.hud.Watermark; @@ -43,6 +44,7 @@ public void init() { register(new Velocity()); register(new BlockHighlight()); register(new NoFall()); + register(new ClickPearl()); } public void register(Module module) { diff --git a/src/main/java/me/alpha432/oyvey/util/EnchantmentUtil.java b/src/main/java/me/alpha432/oyvey/util/EnchantmentUtil.java index 19bc7c7a..d4533326 100644 --- a/src/main/java/me/alpha432/oyvey/util/EnchantmentUtil.java +++ b/src/main/java/me/alpha432/oyvey/util/EnchantmentUtil.java @@ -11,7 +11,7 @@ public final class EnchantmentUtil implements Util { private EnchantmentUtil() { - throw new IllegalArgumentException("пошел нахуй"); + throw new IllegalArgumentException(); } public static int getLevel(ResourceKey key, ItemStack stack) { diff --git a/src/main/java/me/alpha432/oyvey/util/inventory/InventoryUtil.java b/src/main/java/me/alpha432/oyvey/util/inventory/InventoryUtil.java new file mode 100644 index 00000000..dac9d53c --- /dev/null +++ b/src/main/java/me/alpha432/oyvey/util/inventory/InventoryUtil.java @@ -0,0 +1,98 @@ +package me.alpha432.oyvey.util.inventory; + +import me.alpha432.oyvey.util.inventory.strategy.HoldingStrategy; +import me.alpha432.oyvey.util.inventory.strategy.HotbarStrategy; +import me.alpha432.oyvey.util.inventory.strategy.InventoryStrategy; +import me.alpha432.oyvey.util.inventory.strategy.SwapStrategy; +import me.alpha432.oyvey.util.traits.Util; +import net.minecraft.world.inventory.ClickType; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; + +import java.util.EnumSet; +import java.util.List; +import java.util.function.BiPredicate; +import java.util.function.Predicate; + +public final class InventoryUtil implements Util { + public static final Result NONE = new Result(-1, ItemStack.EMPTY, ResultType.NONE); + + public static final EnumSet HOTBAR_SCOPE = EnumSet.of(ResultType.OFFHAND, ResultType.HOTBAR); + public static final EnumSet INVENTORY_SCOPE = EnumSet.of(ResultType.OFFHAND, ResultType.INVENTORY); + public static final EnumSet FULL_SCOPE = EnumSet.of(ResultType.OFFHAND, ResultType.HOTBAR, ResultType.INVENTORY); + + private static final List STRATEGIES = List.of( + HoldingStrategy.INSTANCE, + HotbarStrategy.INSTANCE, + InventoryStrategy.INSTANCE + ); + + + private InventoryUtil() { + throw new AssertionError(); + } + + public static ItemStack cursor() { + return mc.player.containerMenu.getCarried(); + } + + public static int selected() { + return mc.player.getInventory().getSelectedSlot(); + } + + public static void click(int slot, int button, ClickType type) { + int id = mc.player.containerMenu.containerId; + mc.gameMode.handleInventoryMouseClick(id, slot, button, type, mc.player); + } + + public static void swap(int to) { + if (to < 0 || to > 8) return; + mc.player.getInventory().setSelectedSlot(to); + mc.gameMode.ensureHasSentCarriedItem(); + } + + public static void swap(Result result) { + for (SwapStrategy strategy : STRATEGIES) { + if (strategy.swap(result)) + return; + } + } + + public static void swapBack(int last, Result result) { + for (SwapStrategy strategy : STRATEGIES) { + if (strategy.swapBack(last, result)) + return; + } + } + + public static Result find(Item target, EnumSet scopes) { + return find(stack -> stack.is(target), scopes); + } + + public static Result find(Predicate predicate, EnumSet scopes) { + return find((item, scope) -> scopes.contains(scope) && predicate.test(item)); + } + + public static Result find(BiPredicate predicate) { + ItemStack offhand = mc.player.getOffhandItem(); + if (predicate.test(offhand, ResultType.OFFHAND)) { + return Result.fromOffhand(offhand); + } + + for (int i = 0; i < 9; i++) { + ItemStack item = mc.player.getInventory().getItem(i); + if (predicate.test(item, ResultType.HOTBAR)) { + return new Result(i, item, ResultType.HOTBAR); + } + } + + for (int i = 9; i < 36; i++) { + ItemStack item = mc.player.getInventory().getItem(i); + if (predicate.test(item, ResultType.INVENTORY)) { + return new Result(i, item, ResultType.INVENTORY); + } + } + + return NONE; + } +} diff --git a/src/main/java/me/alpha432/oyvey/util/inventory/Result.java b/src/main/java/me/alpha432/oyvey/util/inventory/Result.java new file mode 100644 index 00000000..a16a1592 --- /dev/null +++ b/src/main/java/me/alpha432/oyvey/util/inventory/Result.java @@ -0,0 +1,26 @@ +package me.alpha432.oyvey.util.inventory; + +import net.minecraft.world.InteractionHand; +import net.minecraft.world.item.ItemStack; + +public record Result(int slot, ItemStack stack, ResultType type, boolean holding) { + public Result(int slot, ItemStack stack, ResultType type) { + this(slot, stack, type, isHolding(type, slot)); + } + + static Result fromOffhand(ItemStack stack) { + return new Result(-1, stack, ResultType.OFFHAND); + } + + public InteractionHand hand() { + return type == ResultType.OFFHAND ? InteractionHand.OFF_HAND : InteractionHand.MAIN_HAND; + } + + public boolean found() { + return type != ResultType.NONE; + } + + private static boolean isHolding(ResultType type, int slot) { + return type == ResultType.OFFHAND || type == ResultType.HOTBAR && slot == InventoryUtil.selected(); + } +} diff --git a/src/main/java/me/alpha432/oyvey/util/inventory/ResultType.java b/src/main/java/me/alpha432/oyvey/util/inventory/ResultType.java new file mode 100644 index 00000000..f73c447c --- /dev/null +++ b/src/main/java/me/alpha432/oyvey/util/inventory/ResultType.java @@ -0,0 +1,8 @@ +package me.alpha432.oyvey.util.inventory; + +public enum ResultType { + HOTBAR, + INVENTORY, + OFFHAND, + NONE +} diff --git a/src/main/java/me/alpha432/oyvey/util/inventory/strategy/HoldingStrategy.java b/src/main/java/me/alpha432/oyvey/util/inventory/strategy/HoldingStrategy.java new file mode 100644 index 00000000..63ff6a01 --- /dev/null +++ b/src/main/java/me/alpha432/oyvey/util/inventory/strategy/HoldingStrategy.java @@ -0,0 +1,20 @@ +package me.alpha432.oyvey.util.inventory.strategy; + +import me.alpha432.oyvey.util.inventory.Result; + +public final class HoldingStrategy implements SwapStrategy{ + public static final HoldingStrategy INSTANCE = new HoldingStrategy(); + + private HoldingStrategy() { + } + + @Override + public boolean swap(Result result) { + return result.holding(); + } + + @Override + public boolean swapBack(int last, Result result) { + return result.holding(); + } +} diff --git a/src/main/java/me/alpha432/oyvey/util/inventory/strategy/HotbarStrategy.java b/src/main/java/me/alpha432/oyvey/util/inventory/strategy/HotbarStrategy.java new file mode 100644 index 00000000..ef29c0e3 --- /dev/null +++ b/src/main/java/me/alpha432/oyvey/util/inventory/strategy/HotbarStrategy.java @@ -0,0 +1,30 @@ +package me.alpha432.oyvey.util.inventory.strategy; + +import me.alpha432.oyvey.util.inventory.InventoryUtil; +import me.alpha432.oyvey.util.inventory.Result; +import me.alpha432.oyvey.util.inventory.ResultType; + +public final class HotbarStrategy implements SwapStrategy { + public static final HotbarStrategy INSTANCE = new HotbarStrategy(); + + private HotbarStrategy() { + } + + @Override + public boolean swap(Result result) { + if (result.type() == ResultType.HOTBAR) { + InventoryUtil.swap(result.slot()); + return true; + } + return false; + } + + @Override + public boolean swapBack(int last, Result result) { + if (result.type() == ResultType.HOTBAR) { + InventoryUtil.swap(last); + return true; + } + return false; + } +} diff --git a/src/main/java/me/alpha432/oyvey/util/inventory/strategy/InventoryStrategy.java b/src/main/java/me/alpha432/oyvey/util/inventory/strategy/InventoryStrategy.java new file mode 100644 index 00000000..431b155b --- /dev/null +++ b/src/main/java/me/alpha432/oyvey/util/inventory/strategy/InventoryStrategy.java @@ -0,0 +1,31 @@ +package me.alpha432.oyvey.util.inventory.strategy; + +import me.alpha432.oyvey.util.inventory.InventoryUtil; +import me.alpha432.oyvey.util.inventory.Result; +import me.alpha432.oyvey.util.inventory.ResultType; +import net.minecraft.world.inventory.ClickType; + +public final class InventoryStrategy implements SwapStrategy { + public static final InventoryStrategy INSTANCE = new InventoryStrategy(); + + private InventoryStrategy() { + } + + @Override + public boolean swap(Result result) { + if (result.type() != ResultType.INVENTORY && result.type() != ResultType.HOTBAR) + return false; + int slot = inventorySlot(result); + InventoryUtil.click(slot, InventoryUtil.selected(), ClickType.SWAP); + return true; + } + + @Override + public boolean swapBack(int last, Result result) { + return swap(result); + } + + private static int inventorySlot(Result result) { + return result.type() == ResultType.HOTBAR ? result.slot() + 36 : result.slot(); + } +} diff --git a/src/main/java/me/alpha432/oyvey/util/inventory/strategy/SwapStrategy.java b/src/main/java/me/alpha432/oyvey/util/inventory/strategy/SwapStrategy.java new file mode 100644 index 00000000..3f571c3d --- /dev/null +++ b/src/main/java/me/alpha432/oyvey/util/inventory/strategy/SwapStrategy.java @@ -0,0 +1,9 @@ +package me.alpha432.oyvey.util.inventory.strategy; + +import me.alpha432.oyvey.util.inventory.Result; + +public interface SwapStrategy { + boolean swap(Result result); + + boolean swapBack(int last, Result result); +} diff --git a/src/main/resources/oyvey.accesswidener b/src/main/resources/oyvey.accesswidener index 8b9085f1..a60449a6 100644 --- a/src/main/resources/oyvey.accesswidener +++ b/src/main/resources/oyvey.accesswidener @@ -5,3 +5,4 @@ accessible field net/minecraft/network/protocol/game/ServerboundInteractPacket e accessible method net/minecraft/network/protocol/game/ServerboundInteractPacket$Action getType ()Lnet/minecraft/network/protocol/game/ServerboundInteractPacket$ActionType; accessible class net/minecraft/network/protocol/game/ServerboundInteractPacket$ActionType accessible field net/minecraft/network/protocol/game/ServerboundInteractPacket action Lnet/minecraft/network/protocol/game/ServerboundInteractPacket$Action; +accessible method net/minecraft/client/multiplayer/MultiPlayerGameMode ensureHasSentCarriedItem ()V \ No newline at end of file