| 1 | package com.jellypudding.offlineclient.modules.player;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.event.Subscribe;
|
| 4 | import com.jellypudding.offlineclient.event.events.PacketSendEvent;
|
| 5 | import com.jellypudding.offlineclient.event.events.TickEvent;
|
| 6 | import com.jellypudding.offlineclient.module.Category;
|
| 7 | import com.jellypudding.offlineclient.module.Module;
|
| 8 | import com.jellypudding.offlineclient.setting.BoolSetting;
|
| 9 | import com.jellypudding.offlineclient.setting.EnumSetting;
|
| 10 | import com.jellypudding.offlineclient.setting.NumberSetting;
|
| 11 | import com.jellypudding.offlineclient.setting.RegistryListSetting;
|
| 12 | import com.jellypudding.offlineclient.util.ChatUtil;
|
| 13 | import com.jellypudding.offlineclient.util.InventoryUtil;
|
| 14 | import com.jellypudding.offlineclient.util.ItemUtil;
|
| 15 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 16 | import net.minecraft.network.protocol.game.ServerboundUseItemPacket;
|
| 17 | import net.minecraft.world.InteractionHand;
|
| 18 | import net.minecraft.world.entity.EquipmentSlot;
|
| 19 | import net.minecraft.world.item.Item;
|
| 20 | import net.minecraft.world.item.ItemStack;
|
| 21 | import net.minecraft.world.item.Items;
|
| 22 | import net.minecraft.world.item.enchantment.Enchantments;
|
| 23 |
|
| 24 | import java.util.List;
|
| 25 |
|
| 26 | // Repairs mending gear. Bottles only reach the pieces that are worn or held.
|
| 27 | // The offhand mode is the way to mend items that sit in the inventory.
|
| 28 | public final class AutoMend extends Module {
|
| 29 |
|
| 30 | // Straight down. The throw direction rides on the use packet.
|
| 31 | private static final float DOWN_PITCH = 90;
|
| 32 |
|
| 33 | private static final EquipmentSlot[] REPAIRABLE = {
|
| 34 | EquipmentSlot.MAINHAND, EquipmentSlot.OFFHAND,
|
| 35 | EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS, EquipmentSlot.FEET
|
| 36 | };
|
| 37 |
|
| 38 | public enum Mode { BOTTLES, ALL_BOTTLES, OFFHAND }
|
| 39 |
|
| 40 | private final EnumSetting<Mode> mode = new EnumSetting<>("Mode",
|
| 41 | "How the gear is repaired.", Mode.BOTTLES)
|
| 42 | .describe(Mode.BOTTLES, "Throws experience bottles down whilst worn or held mending gear is damaged.")
|
| 43 | .describe(Mode.ALL_BOTTLES,
|
| 44 | "Throws every experience bottle in the hotbar whatever state your gear is in. "
|
| 45 | + "Good for levels or for gear kept in the inventory.")
|
| 46 | .describe(Mode.OFFHAND,
|
| 47 | "Moves damaged mending items from your inventory into the offhand one at a time "
|
| 48 | + "so any experience you pick up repairs them.");
|
| 49 | private final NumberSetting threshold = new NumberSetting("Threshold",
|
| 50 | "Start throwing once a piece drops below this much durability.", 99, 1, 100, 1, "%")
|
| 51 | .min(1).max(100)
|
| 52 | .under(mode, Mode.BOTTLES);
|
| 53 | private final NumberSetting delay = new NumberSetting("Delay",
|
| 54 | "Ticks between throws.", 4, 0, 20, 1, " ticks")
|
| 55 | .under(mode, Mode.BOTTLES, Mode.ALL_BOTTLES);
|
| 56 | private final BoolSetting groundOnly = new BoolSetting("Ground only",
|
| 57 | "Only throw whilst standing on the ground.", true)
|
| 58 | .under(mode, Mode.BOTTLES, Mode.ALL_BOTTLES);
|
| 59 | private final RegistryListSetting<Item> skip = new RegistryListSetting<>("Skip",
|
| 60 | "Items that never go into the offhand. Click to pick them.", BuiltInRegistries.ITEM, List.of())
|
| 61 | .under(mode, Mode.OFFHAND);
|
| 62 | private final BoolSetting force = new BoolSetting("Force",
|
| 63 | "Puts the damaged item in the offhand even when something else is already there. "
|
| 64 | + "Off waits until the offhand is empty or holds mending gear.", false)
|
| 65 | .under(mode, Mode.OFFHAND);
|
| 66 | private final BoolSetting autoDisable = new BoolSetting("Auto off",
|
| 67 | "Switches off once the gear is repaired or once no bottles are left to throw.", true);
|
| 68 |
|
| 69 | private final InventoryUtil.SlotSwap slots = new InventoryUtil.SlotSwap();
|
| 70 | private int timer;
|
| 71 | private int bottles;
|
| 72 | // Minus one whilst nothing worn or held carries Mending.
|
| 73 | private int worstPercent = -1;
|
| 74 | // True once the offhand mode has put something in the offhand.
|
| 75 | private boolean moved;
|
| 76 | // Set for the one packet that carries the downward throw.
|
| 77 | // Read from the packet thread.
|
| 78 | private volatile boolean throwing;
|
| 79 |
|
| 80 | public AutoMend() {
|
| 81 | super("AutoMend", "Repairs your mending gear with experience.", Category.PLAYER);
|
| 82 | addSettings(mode, threshold, delay, groundOnly, skip, force, autoDisable);
|
| 83 | searchTags("mending", "xp", "experience bottle", "repair", "exp thrower");
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public String getSuffix() {
|
| 88 | if (mode.is(Mode.OFFHAND)) {
|
| 89 | return worstPercent < 0 ? "nothing to mend" : worstPercent + "%";
|
| 90 | }
|
| 91 | if (mode.is(Mode.BOTTLES) && worstPercent < 0) {
|
| 92 | return "no mending gear";
|
| 93 | }
|
| 94 | if (bottles == 0) {
|
| 95 | return "no bottles";
|
| 96 | }
|
| 97 | return mode.is(Mode.ALL_BOTTLES) ? bottles + " left" : worstPercent + "% " + bottles + " left";
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | protected void onEnable() {
|
| 102 | timer = 0;
|
| 103 | moved = false;
|
| 104 | throwing = false;
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | protected void onDisable() {
|
| 109 | slots.restoreIfMine();
|
| 110 | throwing = false;
|
| 111 | }
|
| 112 |
|
| 113 | @Subscribe
|
| 114 | private void onTick(TickEvent event) {
|
| 115 | if (!inGame() || mc.player.isSpectator()) {
|
| 116 | return;
|
| 117 | }
|
| 118 | if (mode.is(Mode.OFFHAND)) {
|
| 119 | slots.restoreIfMine();
|
| 120 | tickOffhand();
|
| 121 | return;
|
| 122 | }
|
| 123 | worstPercent = lowestDurability();
|
| 124 | bottles = countBottles();
|
| 125 | if (mode.is(Mode.ALL_BOTTLES) ? bottles == 0 : worstPercent < 0) {
|
| 126 | slots.restoreIfMine();
|
| 127 | if (mode.is(Mode.ALL_BOTTLES) && autoDisable.isOn()) {
|
| 128 | setEnabled(false);
|
| 129 | }
|
| 130 | return;
|
| 131 | }
|
| 132 | if (mode.is(Mode.BOTTLES) && worstPercent >= threshold.getInt()) {
|
| 133 | slots.restoreIfMine();
|
| 134 | if (autoDisable.isOn()) {
|
| 135 | if (bottles > 0) {
|
| 136 | ChatUtil.message("§aEverything is repaired.");
|
| 137 | }
|
| 138 | setEnabled(false);
|
| 139 | }
|
| 140 | return;
|
| 141 | }
|
| 142 | if (!InventoryUtil.inventoryFree()) {
|
| 143 | return;
|
| 144 | }
|
| 145 | if (groundOnly.isOn() && !mc.player.onGround()) {
|
| 146 | return;
|
| 147 | }
|
| 148 | if (timer > 0) {
|
| 149 | timer--;
|
| 150 | return;
|
| 151 | }
|
| 152 | throwBottle();
|
| 153 | }
|
| 154 |
|
| 155 | // A bottle already in the offhand is thrown from there with no swap.
|
| 156 | private void throwBottle() {
|
| 157 | InteractionHand hand = InteractionHand.OFF_HAND;
|
| 158 | if (!mc.player.getOffhandItem().is(Items.EXPERIENCE_BOTTLE)) {
|
| 159 | int slot = findBottle();
|
| 160 | if (slot == -1) {
|
| 161 | slots.restore();
|
| 162 | return;
|
| 163 | }
|
| 164 | slots.select(slot);
|
| 165 | hand = InteractionHand.MAIN_HAND;
|
| 166 | }
|
| 167 | throwing = true;
|
| 168 | try {
|
| 169 | mc.gameMode.useItem(mc.player, hand);
|
| 170 | } finally {
|
| 171 | throwing = false;
|
| 172 | }
|
| 173 | timer = delay.getInt();
|
| 174 | }
|
| 175 |
|
| 176 | // The use packet carries the rotation the server throws along.
|
| 177 | @Subscribe(priority = 100)
|
| 178 | private void onPacketSend(PacketSendEvent event) {
|
| 179 | if (!throwing || !(event.getPacket() instanceof ServerboundUseItemPacket packet)) {
|
| 180 | return;
|
| 181 | }
|
| 182 | event.setPacket(new ServerboundUseItemPacket(packet.getHand(), packet.getSequence(),
|
| 183 | packet.getYRot(), DOWN_PITCH));
|
| 184 | }
|
| 185 |
|
| 186 | // Keeps one damaged mending item in the offhand until it is whole and
|
| 187 | // then brings in the next. The last one goes home once nothing is left.
|
| 188 | private void tickOffhand() {
|
| 189 | ItemStack offhand = mc.player.getOffhandItem();
|
| 190 | worstPercent = isMending(offhand) ? percentLeft(offhand) : -1;
|
| 191 | if (!InventoryUtil.inventoryFree()) {
|
| 192 | return;
|
| 193 | }
|
| 194 | if (!offhand.isEmpty()) {
|
| 195 | if (isMending(offhand)) {
|
| 196 | if (offhand.getDamageValue() > 0) {
|
| 197 | return;
|
| 198 | }
|
| 199 | } else if (!force.isOn()) {
|
| 200 | return;
|
| 201 | }
|
| 202 | }
|
| 203 | int slot = findDamaged();
|
| 204 | if (slot != -1) {
|
| 205 | InventoryUtil.swap(InventoryUtil.networkSlot(slot), InventoryUtil.OFFHAND_SLOT);
|
| 206 | moved = true;
|
| 207 | return;
|
| 208 | }
|
| 209 | if (!autoDisable.isOn()) {
|
| 210 | return;
|
| 211 | }
|
| 212 | if (moved) {
|
| 213 | int empty = InventoryUtil.findSlot(ItemStack::isEmpty, InventoryUtil.WHOLE_INVENTORY);
|
| 214 | if (empty != -1) {
|
| 215 | InventoryUtil.swap(InventoryUtil.OFFHAND_SLOT, InventoryUtil.networkSlot(empty));
|
| 216 | }
|
| 217 | }
|
| 218 | ChatUtil.message("§aEverything is repaired.");
|
| 219 | setEnabled(false);
|
| 220 | }
|
| 221 |
|
| 222 | // The first damaged mending item in the inventory that is not skipped.
|
| 223 | private int findDamaged() {
|
| 224 | return InventoryUtil.findSlot(stack -> isMending(stack) && stack.getDamageValue() > 0
|
| 225 | && !skip.contains(stack.getItem()), InventoryUtil.WHOLE_INVENTORY);
|
| 226 | }
|
| 227 |
|
| 228 | // Minus one when no equipped piece carries Mending.
|
| 229 | private int lowestDurability() {
|
| 230 | int lowest = -1;
|
| 231 | for (EquipmentSlot slot : REPAIRABLE) {
|
| 232 | ItemStack stack = slot == EquipmentSlot.MAINHAND ? heldTool() : mc.player.getItemBySlot(slot);
|
| 233 | if (!isMending(stack)) {
|
| 234 | continue;
|
| 235 | }
|
| 236 | int percent = percentLeft(stack);
|
| 237 | lowest = lowest < 0 ? percent : Math.min(lowest, percent);
|
| 238 | }
|
| 239 | return lowest;
|
| 240 | }
|
| 241 |
|
| 242 | private static boolean isMending(ItemStack stack) {
|
| 243 | if (stack.isEmpty() || !stack.isDamageableItem() || stack.getMaxDamage() <= 0) {
|
| 244 | return false;
|
| 245 | }
|
| 246 | return ItemUtil.enchantLevel(Enchantments.MENDING, stack) > 0;
|
| 247 | }
|
| 248 |
|
| 249 | private static int percentLeft(ItemStack stack) {
|
| 250 | return (int) (100 - stack.getDamageValue() * 100.0 / stack.getMaxDamage());
|
| 251 | }
|
| 252 |
|
| 253 | // The tool stays in the slot the bottle swap came from.
|
| 254 | private ItemStack heldTool() {
|
| 255 | int home = slots.previousSlot();
|
| 256 | return home == -1 ? mc.player.getMainHandItem() : mc.player.getInventory().getItem(home);
|
| 257 | }
|
| 258 |
|
| 259 | // Hotbar bottles plus any in the offhand.
|
| 260 | private int countBottles() {
|
| 261 | int count = InventoryUtil.count(Items.EXPERIENCE_BOTTLE, InventoryUtil.HOTBAR_SIZE);
|
| 262 | ItemStack offhand = mc.player.getOffhandItem();
|
| 263 | return offhand.is(Items.EXPERIENCE_BOTTLE) ? count + offhand.getCount() : count;
|
| 264 | }
|
| 265 |
|
| 266 | private int findBottle() {
|
| 267 | return InventoryUtil.findSlot(Items.EXPERIENCE_BOTTLE, InventoryUtil.HOTBAR_SIZE);
|
| 268 | }
|
| 269 | }
|
| 270 |
|