From 7e8316eda5381255d413d0f1104ed1c434a9f43b Mon Sep 17 00:00:00 2001 From: fr1kin Date: Tue, 16 Oct 2018 23:46:35 -0500 Subject: [PATCH] Created new selection service that will auto revert mod-set selections --- .../java/com/matt/forgehax/mods/AutoTool.java | 66 +++-------- .../java/com/matt/forgehax/mods/Scaffold.java | 10 +- .../mods/services/HotbarSelectionService.java | 112 ++++++++++++++++++ .../util/entity/LocalPlayerInventory.java | 47 +++++++- 4 files changed, 175 insertions(+), 60 deletions(-) create mode 100644 src/main/java/com/matt/forgehax/mods/services/HotbarSelectionService.java diff --git a/src/main/java/com/matt/forgehax/mods/AutoTool.java b/src/main/java/com/matt/forgehax/mods/AutoTool.java index c1c9dd04d..b5b7116b4 100644 --- a/src/main/java/com/matt/forgehax/mods/AutoTool.java +++ b/src/main/java/com/matt/forgehax/mods/AutoTool.java @@ -11,6 +11,7 @@ import com.matt.forgehax.util.command.Setting; import com.matt.forgehax.util.entity.LocalPlayerInventory; import com.matt.forgehax.util.entity.LocalPlayerInventory.InvItem; +import com.matt.forgehax.util.entity.LocalPlayerUtils; import com.matt.forgehax.util.mod.Category; import com.matt.forgehax.util.mod.ToggleMod; import com.matt.forgehax.util.mod.loader.RegisterMod; @@ -29,7 +30,6 @@ import net.minecraft.inventory.EntityEquipmentSlot; import net.minecraft.util.math.BlockPos; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent; @RegisterMod public class AutoTool extends ToggleMod { @@ -78,12 +78,6 @@ public static AutoTool getInstance() { .max((int) Short.MAX_VALUE) .build(); - private int previousIndex = -1; - private int lastSetIndex = -1; - private long ticksSinceChanged = 0; - - private Predicate waiting = Predicates.alwaysTrue(); - public AutoTool() { super(Category.PLAYER, "AutoTool", false, "Automatically switch to the best tool"); instance = this; @@ -100,11 +94,6 @@ private boolean isDurabilityGood(InvItem item) { > durability_threshold.get(); } - private int closestInHotbar(InvItem item, InvItem current) { - return (LocalPlayerInventory.getHotbarSize() - 1) - - Math.abs(current.getIndex() - item.getIndex()); - } - private double getDigSpeed(InvItem item, IBlockState state, BlockPos pos) { double str = item.getItemStack().getStrVsBlock(state); int eff = getEnchantmentLevel(EFFICIENCY, item); @@ -164,7 +153,7 @@ private InvItem getBestTool(BlockPos pos) { .max( Comparator.comparingDouble(item -> getDigSpeed(item, state, pos)) .thenComparing(this::isInvincible) - .thenComparing(item -> closestInHotbar(item, current))) + .thenComparing(LocalPlayerInventory::getHotbarDistance)) .orElse(current); } @@ -178,57 +167,30 @@ private InvItem getBestWeapon(Entity target) { .thenComparing(item -> getEnchantmentLevel(Enchantments.FIRE_ASPECT, item)) .thenComparing(item -> getEnchantmentLevel(Enchantments.SWEEPING, item)) .thenComparing(this::isInvincible) - .thenComparing(item -> closestInHotbar(item, current))) + .thenComparing(LocalPlayerInventory::getHotbarDistance)) .orElse(current); } - public InvItem selectBestTool(BlockPos pos) { - InvItem previous = LocalPlayerInventory.getSelected(); - if (isEnabled() && tools.get()) LocalPlayerInventory.setSelected(getBestTool(pos)); - return previous; + public void selectBestTool(BlockPos pos) { + if (isEnabled() && tools.get()) + LocalPlayerInventory.setSelected(getBestTool(pos), revert_back.get(), ticks -> ticks > 5); } - public InvItem selectBestWeapon(Entity target) { - InvItem previous = LocalPlayerInventory.getSelected(); - if (isEnabled() && weapons.get()) LocalPlayerInventory.setSelected(getBestWeapon(target)); - return previous; - } - - @SubscribeEvent - public void onClientTick(ClientTickEvent event) { - switch (event.phase) { - case START: - { - if (previousIndex != -1 && waiting.test(ticksSinceChanged) && revert_back.get()) { - if (lastSetIndex == LocalPlayerInventory.getSelected().getIndex()) - LocalPlayerInventory.setSelected(previousIndex); - previousIndex = lastSetIndex = -1; - } - ++ticksSinceChanged; - break; - } - } + public void selectBestWeapon(Entity target) { + if (isEnabled() && weapons.get()) + LocalPlayerInventory.setSelected( + getBestWeapon(target), + revert_back.get(), + ticks -> getLocalPlayer().getCooledAttackStrength(0.f) >= 1.f && ticks > 30); } @SubscribeEvent public void onBlockBreak(PlayerDamageBlockEvent event) { - InvItem previous = selectBestTool(event.getPos()); - - if (previousIndex == -1) previousIndex = previous.getIndex(); - lastSetIndex = LocalPlayerInventory.getSelected().getIndex(); - - ticksSinceChanged = 0; - waiting = ticks -> ticks > 5; + selectBestTool(event.getPos()); } @SubscribeEvent public void onAttackEntity(PlayerAttackEntityEvent event) { - InvItem previous = selectBestWeapon(event.getVictim()); - - if (previousIndex == -1) previousIndex = previous.getIndex(); - lastSetIndex = LocalPlayerInventory.getSelected().getIndex(); - - ticksSinceChanged = 0; - waiting = ticks -> getLocalPlayer().getCooledAttackStrength(0.f) >= 1.f && ticks > 30; + selectBestWeapon(event.getVictim()); } } diff --git a/src/main/java/com/matt/forgehax/mods/Scaffold.java b/src/main/java/com/matt/forgehax/mods/Scaffold.java index faf27c67d..9eb9b3edb 100644 --- a/src/main/java/com/matt/forgehax/mods/Scaffold.java +++ b/src/main/java/com/matt/forgehax/mods/Scaffold.java @@ -8,6 +8,7 @@ import com.matt.forgehax.asm.reflection.FastReflection.Fields; import com.matt.forgehax.mods.managers.PositionRotationManager; import com.matt.forgehax.mods.managers.PositionRotationManager.RotationState.Local; +import com.matt.forgehax.mods.services.HotbarSelectionService.ResetFunction; import com.matt.forgehax.util.BlockHelper; import com.matt.forgehax.util.BlockHelper.BlockTraceInfo; import com.matt.forgehax.util.Utils; @@ -20,6 +21,7 @@ import com.matt.forgehax.util.mod.Category; import com.matt.forgehax.util.mod.ToggleMod; import com.matt.forgehax.util.mod.loader.RegisterMod; +import java.util.Comparator; import java.util.EnumSet; import java.util.Objects; import net.minecraft.item.ItemBlock; @@ -72,7 +74,7 @@ public void onLocalPlayerMovementUpdate(Local state) { .stream() .filter(InvItem::nonNull) .filter(item -> item.getItem() instanceof ItemBlock) - .findFirst() + .max(Comparator.comparingInt(LocalPlayerInventory::getHotbarDistance)) .orElse(InvItem.EMPTY); if (items.isNull()) return; @@ -102,7 +104,8 @@ public void onLocalPlayerMovementUpdate(Local state) { final BlockTraceInfo tr = trace; state.invokeLater( rs -> { - LocalPlayerInventory.setSelected(items); + ResetFunction func = LocalPlayerInventory.setSelected(items); + getPlayerController() .processRightClickBlock( getLocalPlayer(), @@ -111,6 +114,9 @@ public void onLocalPlayerMovementUpdate(Local state) { tr.getOppositeSide(), hit, EnumHand.MAIN_HAND); + + func.revert(); + getNetworkManager().sendPacket(new CPacketAnimation(EnumHand.MAIN_HAND)); Fields.Minecraft_rightClickDelayTimer.set(MC, 4); placing = true; diff --git a/src/main/java/com/matt/forgehax/mods/services/HotbarSelectionService.java b/src/main/java/com/matt/forgehax/mods/services/HotbarSelectionService.java new file mode 100644 index 000000000..04d68168b --- /dev/null +++ b/src/main/java/com/matt/forgehax/mods/services/HotbarSelectionService.java @@ -0,0 +1,112 @@ +package com.matt.forgehax.mods.services; + +import static com.matt.forgehax.Helper.getLocalPlayer; +import static com.matt.forgehax.Helper.getWorld; + +import com.google.common.base.MoreObjects; +import com.google.common.base.Predicates; +import com.matt.forgehax.util.entity.LocalPlayerInventory; +import com.matt.forgehax.util.mod.ServiceMod; +import com.matt.forgehax.util.mod.loader.RegisterMod; +import java.util.function.Predicate; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent.Phase; + +@RegisterMod +public class HotbarSelectionService extends ServiceMod { + private static HotbarSelectionService instance = null; + + public static HotbarSelectionService getInstance() { + return instance; + } + + private int originalIndex = -1; + private long ticksElapsed = -1; + + private int lastSetIndex = -1; + private Predicate resetCondition = Predicates.alwaysTrue(); + + public HotbarSelectionService() { + super("HotbarSelectionService"); + instance = this; + } + + public ResetFunction setSelected(final int index, boolean reset, Predicate condition) { + if (index < 0 || index > LocalPlayerInventory.getHotbarSize() - 1) + throw new IllegalArgumentException( + "index must be between 0 and " + (LocalPlayerInventory.getHotbarSize() - 1)); + + final int orig = selected(); + + if (!reset) { + select(index); + if (originalIndex != -1) originalIndex = index; + + return () -> select(index); + } else { + if ((originalIndex == -1 ? orig : originalIndex) != index) { + if (originalIndex == -1) originalIndex = orig; + + lastSetIndex = index; + resetCondition = MoreObjects.firstNonNull(condition, Predicates.alwaysTrue()); + + select(index); + } + ticksElapsed = 0; + + return () -> { + if(index == selected() && lastSetIndex == index) { + select(orig); + reset(); + } + }; + } + } + + public void resetSelected() { + if (originalIndex != -1 && selected() == lastSetIndex) select(originalIndex); + reset(); + } + + private void reset() { + originalIndex = -1; + ticksElapsed = -1; + lastSetIndex = -1; + resetCondition = Predicates.alwaysTrue(); + } + + @SubscribeEvent + public void onClientTick(ClientTickEvent event) { + if (getWorld() == null || getLocalPlayer() == null) { + reset(); + return; + } + + switch (event.phase) { + case START: + { + if (originalIndex != -1 && resetCondition.test(ticksElapsed)) resetSelected(); + if(ticksElapsed != -1) ++ticksElapsed; + break; + } + } + } + + // + // + // + + private static void select(int index) { + if (getLocalPlayer() == null) return; + LocalPlayerInventory.getInventory().currentItem = index; + } + + private static int selected() { + return getLocalPlayer() == null ? -1 : LocalPlayerInventory.getSelected().getIndex(); + } + + public interface ResetFunction { + void revert(); + } +} diff --git a/src/main/java/com/matt/forgehax/util/entity/LocalPlayerInventory.java b/src/main/java/com/matt/forgehax/util/entity/LocalPlayerInventory.java index df028008f..7829a2797 100644 --- a/src/main/java/com/matt/forgehax/util/entity/LocalPlayerInventory.java +++ b/src/main/java/com/matt/forgehax/util/entity/LocalPlayerInventory.java @@ -2,9 +2,13 @@ import static com.matt.forgehax.Helper.getLocalPlayer; +import com.google.common.base.Predicates; +import com.matt.forgehax.mods.services.HotbarSelectionService; +import com.matt.forgehax.mods.services.HotbarSelectionService.ResetFunction; import java.util.List; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Predicate; import java.util.stream.Collectors; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; @@ -49,15 +53,46 @@ public static InvItem getSelected() { return getMainInventory().get(getInventory().currentItem); } - public static void setSelected(int index) { - if (index < 0 || index > 8) - throw new IndexOutOfBoundsException("Can only select index in hot bar (0-8)"); + public static ResetFunction setSelected(int index, boolean reset, Predicate condition) { + return HotbarSelectionService.getInstance().setSelected(index, reset, condition); + } + + public static ResetFunction setSelected(InvItem inv, boolean reset, Predicate condition) { + return setSelected(inv.getIndex(), reset, condition); + } + + public static ResetFunction setSelected(int index, Predicate condition) { + Objects.requireNonNull(condition); + return setSelected(index, true, condition); + } + + public static ResetFunction setSelected(InvItem inv, Predicate condition) { + return setSelected(inv.getIndex(), condition); + } + + public static ResetFunction setSelected(int index) { + return setSelected(index, Predicates.alwaysTrue()); + } + + public static ResetFunction setSelected(InvItem invItem) { + return setSelected(invItem.getIndex()); + } + + public static ResetFunction forceSelected(int index) { + return setSelected(index, false, null); + } + + public static ResetFunction forceSelected(InvItem inv) { + return forceSelected(inv.getIndex()); + } - getInventory().currentItem = index; + public static void resetSelected() { + HotbarSelectionService.getInstance().resetSelected(); } - public static void setSelected(InvItem invItem) { - setSelected(invItem.getIndex()); + public static int getHotbarDistance(InvItem item) { + int max = LocalPlayerInventory.getHotbarSize() - 1; + return item.getIndex() > max ? 0 : max - Math.abs(getSelected().getIndex() - item.getIndex()); } public static class InvItem implements Comparable {