add AutoTotem module

Ported from Sydney-Legacy AutoTotemModule:
- offhand modes Totem/Crystal/Gapple with health, elytra, fall,
  gapple, lethal-override, tick-abort, smart-mine and anti-mace checks
- own-totem pop resets the tick-abort interval via entity event 35
- three-click pickup move into offhand container slot 45
- ServerManager.getPingDelay() added for lag-scaled action pacing

Substitutions: no Suicide/SpeedMine modules in fros, so those branches
are dropped; SmartMine uses vanilla isDestroying() as the mining proxy;
sword/axe check via ItemTags.SWORDS/AXES (no SwordItem/AxeItem classes)
This commit is contained in:
froslanz
2026-10-30 15:38:12 -04:00
parent f60c384613
commit 0a8957b477
3 changed files with 165 additions and 0 deletions
@@ -0,0 +1,158 @@
package cc.fros.features.modules.combat;
import cc.fros.Fros;
import cc.fros.event.impl.network.PacketEvent;
import cc.fros.event.system.Subscribe;
import cc.fros.features.modules.Module;
import cc.fros.features.settings.Setting;
import cc.fros.util.inventory.InventoryUtil;
import cc.fros.util.inventory.Result;
import cc.fros.util.inventory.ResultType;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.screens.inventory.InventoryScreen;
import net.minecraft.network.protocol.game.ClientboundEntityEventPacket;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.ClickType;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import java.util.EnumSet;
/**
* AutoTotem — keeps a chosen item (totem, crystal or enchanted golden apple) in your offhand,
* swapping one in whenever safety conditions are met.
* Ported from Sydney's {@code AutoTotemModule}.
*/
public class AutoTotemModule extends Module {
private static final byte TOTEM_POP_EVENT_ID = 35;
private static final int OFFHAND_SLOT = 45;
private final Setting<Mode> item = mode("Item", Mode.Totem);
private final Setting<Integer> health = num("Health", 16, 0, 36);
private final Setting<Boolean> elytraCheck = bool("ElytraCheck", true);
private final Setting<Float> fallDistance = num("FallDistance", 20f, 0f, 80f);
private final Setting<Boolean> useGapple = bool("UseGapple", true);
private final Setting<Boolean> lethalOverride = bool("LethalOverride", false);
private final Setting<Boolean> tickAbort = bool("TickAbort", true);
private final Setting<Boolean> smartMine = bool("SmartMine", false);
private final Setting<Boolean> antiMace = bool("AntiMace", false);
private final Setting<Float> maceRange = num("MaceRange", 12f, 0f, 24f);
private int totemCount;
private int ticks;
public AutoTotemModule() {
super("AutoTotem", "Automatically puts a specified item in your offhand slot.", Category.COMBAT);
health.setVisibility(v -> item.getValue() != Mode.Totem);
smartMine.setVisibility(v -> item.getValue() == Mode.Crystal);
lethalOverride.setVisibility(v -> useGapple.getValue());
maceRange.setVisibility(v -> antiMace.getValue());
}
@Override
public void onTick() {
if (nullCheck()) return;
totemCount = InventoryUtil.count(Items.TOTEM_OF_UNDYING);
if (ticks > 0 && tickAbort.getValue()) {
ticks--;
return;
}
// don't shuffle items around while some other container screen is open
if (!(mc.screen instanceof InventoryScreen) && mc.screen instanceof AbstractContainerScreen<?>) return;
Item target = getItem();
if (mc.player.getOffhandItem().is(target)) return;
Result slot = InventoryUtil.find(target, EnumSet.of(ResultType.INVENTORY));
if (!slot.found()) slot = InventoryUtil.find(target, EnumSet.of(ResultType.HOTBAR, ResultType.INVENTORY));
if (!slot.found() && target == Items.TOTEM_OF_UNDYING) {
// no spare totem anywhere: move an empty slot over the offhand to clear it
slot = InventoryUtil.find(ItemStack::isEmpty, EnumSet.of(ResultType.HOTBAR, ResultType.INVENTORY));
}
if (!slot.found()) return;
// three-click "pick up, place, put back" move into the offhand
int src = containerSlot(slot);
InventoryUtil.click(src, 0, ClickType.PICKUP);
InventoryUtil.click(OFFHAND_SLOT, 0, ClickType.PICKUP);
InventoryUtil.click(src, 0, ClickType.PICKUP);
ticks = 2 + Fros.serverManager.getPingDelay();
}
@Subscribe
public void onPacketReceive(PacketEvent.Receive event) {
// popping your own totem restarts the tick-abort interval; runs on the network thread
if (!(event.getPacket() instanceof ClientboundEntityEventPacket packet)) return;
if (packet.getEventId() != TOTEM_POP_EVENT_ID) return;
if (mc.level == null) return;
if (packet.getEntity(mc.level) == mc.player) ticks = 0;
}
private Item getItem() {
ItemStack mainhand = mc.player.getMainHandItem();
if (useGapple.getValue() && mc.options.keyUse.isDown()
&& (lethalOverride.getValue() || !needsTotem())
&& (mainhand.is(ItemTags.SWORDS) || mainhand.is(ItemTags.AXES))
&& hasItem(Items.ENCHANTED_GOLDEN_APPLE)) {
return Items.ENCHANTED_GOLDEN_APPLE;
}
if (hasItem(Items.TOTEM_OF_UNDYING)) {
if (needsTotem()) return Items.TOTEM_OF_UNDYING;
// crystal mode: hold the totem unless a block is actually being mined
if (item.getValue() == Mode.Crystal && smartMine.getValue() && !mc.gameMode.isDestroying()) {
return Items.TOTEM_OF_UNDYING;
}
}
return switch (item.getValue()) {
case Crystal -> hasItem(Items.END_CRYSTAL) ? Items.END_CRYSTAL : Items.TOTEM_OF_UNDYING;
case Gapple -> hasItem(Items.ENCHANTED_GOLDEN_APPLE) ? Items.ENCHANTED_GOLDEN_APPLE : Items.TOTEM_OF_UNDYING;
default -> Items.TOTEM_OF_UNDYING;
};
}
private boolean needsTotem() {
if (mc.player.getHealth() + mc.player.getAbsorptionAmount() <= health.getValue()) return true;
if (mc.player.fallDistance > fallDistance.getValue()) return true;
if (elytraCheck.getValue() && mc.player.getItemBySlot(EquipmentSlot.CHEST).is(Items.ELYTRA)) return true;
return antiMace.getValue() && maceThreat();
}
private boolean maceThreat() {
float range = maceRange.getValue();
for (Player player : mc.level.players()) {
if (player == mc.player) continue;
if (Fros.friendManager.isFriend(player)) continue;
if (mc.player.distanceToSqr(player) > range * range) continue;
if (player.fallDistance >= 1.5f && player.getMainHandItem().is(Items.MACE)) return true;
}
return false;
}
private boolean hasItem(Item target) {
return InventoryUtil.find(target, EnumSet.of(ResultType.HOTBAR, ResultType.INVENTORY)).found()
|| mc.player.getOffhandItem().is(target);
}
/** Container-menu slot id for an inventory result (hotbar indices 0-8 live at 36-44). */
private static int containerSlot(Result result) {
return result.type() == ResultType.HOTBAR ? result.slot() + 36 : result.slot();
}
@Override
public String getDisplayInfo() {
return String.valueOf(totemCount);
}
private enum Mode {
Totem, Crystal, Gapple
}
}
@@ -14,6 +14,7 @@ import cc.fros.features.modules.Module;
import cc.fros.features.modules.client.ClickGuiModule;
import cc.fros.features.modules.client.HudEditorModule;
import cc.fros.features.modules.client.NotificationsModule;
import cc.fros.features.modules.combat.AutoTotemModule;
import cc.fros.features.modules.combat.KillAuraModule;
import cc.fros.features.modules.combat.CrystalAuraModule;
import cc.fros.features.modules.combat.CriticalsModule;
@@ -108,6 +109,7 @@ public class ModuleManager implements Jsonable, Util {
register(new KillAuraModule());
register(new CrystalAuraModule());
register(new SurroundModule());
register(new AutoTotemModule());
register(new ExpThrowerModule());
register(new FastAccelModule());
@@ -85,4 +85,9 @@ public class ServerManager
return 0;
}
}
/** Ticks to wait after an inventory action so the server can catch up, scaled by ping. */
public int getPingDelay() {
return (int) (getPing() / 25.0f);
}
}