Skip to content

Commit

Permalink
Created new selection service that will auto revert mod-set selections
Browse files Browse the repository at this point in the history
  • Loading branch information
fr1kin committed Oct 17, 2018
1 parent 5e88557 commit 7e8316e
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 60 deletions.
66 changes: 14 additions & 52 deletions src/main/java/com/matt/forgehax/mods/AutoTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<Long> waiting = Predicates.alwaysTrue();

public AutoTool() {
super(Category.PLAYER, "AutoTool", false, "Automatically switch to the best tool");
instance = this;
Expand All @@ -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);
Expand Down Expand Up @@ -164,7 +153,7 @@ private InvItem getBestTool(BlockPos pos) {
.max(
Comparator.<InvItem>comparingDouble(item -> getDigSpeed(item, state, pos))
.thenComparing(this::isInvincible)
.thenComparing(item -> closestInHotbar(item, current)))
.thenComparing(LocalPlayerInventory::getHotbarDistance))
.orElse(current);
}

Expand All @@ -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());
}
}
10 changes: 8 additions & 2 deletions src/main/java/com/matt/forgehax/mods/Scaffold.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Long> resetCondition = Predicates.alwaysTrue();

public HotbarSelectionService() {
super("HotbarSelectionService");
instance = this;
}

public ResetFunction setSelected(final int index, boolean reset, Predicate<Long> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Long> condition) {
return HotbarSelectionService.getInstance().setSelected(index, reset, condition);
}

public static ResetFunction setSelected(InvItem inv, boolean reset, Predicate<Long> condition) {
return setSelected(inv.getIndex(), reset, condition);
}

public static ResetFunction setSelected(int index, Predicate<Long> condition) {
Objects.requireNonNull(condition);
return setSelected(index, true, condition);
}

public static ResetFunction setSelected(InvItem inv, Predicate<Long> 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<InvItem> {
Expand Down

0 comments on commit 7e8316e

Please sign in to comment.