|
| 1 | +package com.shaybox.rusher.tweaks; |
| 2 | + |
| 3 | +import net.minecraft.client.Minecraft; |
| 4 | +import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; |
| 5 | +import net.minecraft.client.gui.screens.inventory.InventoryScreen; |
| 6 | +import net.minecraft.client.player.LocalPlayer; |
| 7 | +import net.minecraft.world.entity.EquipmentSlot; |
| 8 | +import net.minecraft.world.entity.player.Inventory; |
| 9 | +import net.minecraft.world.inventory.Slot; |
| 10 | +import net.minecraft.world.item.Equipable; |
| 11 | +import net.minecraft.world.item.Item; |
| 12 | +import net.minecraft.world.item.ItemStack; |
| 13 | +import net.minecraft.world.item.Items; |
| 14 | +import net.minecraft.world.item.enchantment.EnchantmentHelper; |
| 15 | +import net.minecraft.world.item.enchantment.Enchantments; |
| 16 | +import org.rusherhack.client.api.RusherHackAPI; |
| 17 | +import org.rusherhack.client.api.events.client.EventUpdate; |
| 18 | +import org.rusherhack.client.api.events.player.EventPlayerUpdate; |
| 19 | +import org.rusherhack.client.api.feature.module.IModule; |
| 20 | +import org.rusherhack.client.api.feature.module.ToggleableModule; |
| 21 | +import org.rusherhack.client.api.setting.BindSetting; |
| 22 | +import org.rusherhack.client.api.utils.InventoryUtils; |
| 23 | +import org.rusherhack.core.bind.key.IKey; |
| 24 | +import org.rusherhack.core.bind.key.NullKey; |
| 25 | +import org.rusherhack.core.event.listener.EventListener; |
| 26 | +import org.rusherhack.core.event.subscribe.Subscribe; |
| 27 | +import org.rusherhack.core.feature.IFeatureManager; |
| 28 | +import org.rusherhack.core.setting.BooleanSetting; |
| 29 | + |
| 30 | +import java.util.Comparator; |
| 31 | +import java.util.List; |
| 32 | +import java.util.function.Consumer; |
| 33 | +import java.util.function.Supplier; |
| 34 | + |
| 35 | +public class ArmorPriority implements EventListener { |
| 36 | + |
| 37 | + /* Minecraft */ |
| 38 | + private final Minecraft minecraft = Minecraft.getInstance(); |
| 39 | + |
| 40 | + /* RusherHackAPI Managers & Modules */ |
| 41 | + private final IFeatureManager<IModule> moduleManager = RusherHackAPI.getModuleManager(); |
| 42 | + private final ToggleableModule autoArmor = (ToggleableModule) moduleManager.getFeature("AutoArmor").orElseThrow(); |
| 43 | + |
| 44 | + /* AutoArmor Settings */ |
| 45 | + private final BooleanSetting soft = (BooleanSetting) autoArmor.getSetting("Soft"); |
| 46 | + private final BooleanSetting inventory = (BooleanSetting) autoArmor.getSetting("Inventory"); |
| 47 | + private final BooleanSetting elytraPriority = (BooleanSetting) autoArmor.getSetting("ElytraPriority"); |
| 48 | + private final BooleanSetting repairPriority = new BooleanSetting("RepairPriority", "Prioritize low durability armor with mending", false); |
| 49 | + private final BooleanSetting goldenPriority = new BooleanSetting("GoldenPriority", "Prioritize one piece of golden armor for piglins", false); |
| 50 | + private final BindSetting elytraPriorityBind = new BindSetting("Bind", NullKey.INSTANCE); |
| 51 | + private final BindSetting repairPriorityBind = new BindSetting("Bind", NullKey.INSTANCE); |
| 52 | + private final BindSetting goldenPriorityBind = new BindSetting("Bind", NullKey.INSTANCE); |
| 53 | + |
| 54 | + /* Equipment Slots & Golden Armor Priority */ |
| 55 | + private final EquipmentSlot[] equipmentSlots = {EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS, EquipmentSlot.FEET}; |
| 56 | + private final List<Item> goldenArmorPriority = List.of(Items.GOLDEN_BOOTS, Items.GOLDEN_HELMET, Items.GOLDEN_LEGGINGS, Items.GOLDEN_CHESTPLATE); |
| 57 | + |
| 58 | + /* Previous State */ |
| 59 | + private boolean isPaused = false; |
| 60 | + private boolean isElytraDown = false; |
| 61 | + private boolean isRepairDown = false; |
| 62 | + private boolean isGoldenDown = false; |
| 63 | + private boolean lastSoft = this.soft.getValue(); |
| 64 | + |
| 65 | + /* Initialize */ |
| 66 | + public ArmorPriority() { |
| 67 | + this.elytraPriority.addSubSettings(this.elytraPriorityBind); |
| 68 | + this.repairPriority.addSubSettings(this.repairPriorityBind); |
| 69 | + this.goldenPriority.addSubSettings(this.goldenPriorityBind); |
| 70 | + this.autoArmor.registerSettings(this.repairPriority, this.goldenPriority); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean isListening() { |
| 75 | + IKey elytraBind = this.elytraPriorityBind.getValue(); |
| 76 | + IKey repairBind = this.repairPriorityBind.getValue(); |
| 77 | + IKey goldenBind = this.goldenPriorityBind.getValue(); |
| 78 | + |
| 79 | + return this.isPaused || this.autoArmor.isToggled() && ( |
| 80 | + (this.repairPriority.getValue() || this.goldenPriority.getValue()) || ( |
| 81 | + (this.isElytraDown || this.isRepairDown || this.isGoldenDown) || ( |
| 82 | + (elytraBind.isKeyDown() || repairBind.isKeyDown() || goldenBind.isKeyDown()) |
| 83 | + ) |
| 84 | + ) |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + @Subscribe |
| 89 | + @SuppressWarnings("unused") |
| 90 | + private void onUpdate(EventUpdate event) { |
| 91 | + if (minecraft.screen != null) return; |
| 92 | + |
| 93 | + handleKey(this.elytraPriorityBind::getValue, this.isElytraDown, (value) -> this.isElytraDown = value, this.elytraPriority::getValue, this.elytraPriority::setValue); |
| 94 | + handleKey(this.repairPriorityBind::getValue, this.isRepairDown, (value) -> this.isRepairDown = value, this.repairPriority::getValue, this.repairPriority::setValue); |
| 95 | + handleKey(this.goldenPriorityBind::getValue, this.isGoldenDown, (value) -> this.isGoldenDown = value, this.goldenPriority::getValue, this.goldenPriority::setValue); |
| 96 | + } |
| 97 | + |
| 98 | + private void handleKey(Supplier<IKey> key, boolean isDown, Consumer<Boolean> setIsDown, Supplier<Boolean> getPriority, Consumer<Boolean> setPriority) { |
| 99 | + if (key.get().isKeyDown()) { |
| 100 | + if (!isDown) { |
| 101 | + setIsDown.accept(true); |
| 102 | + setPriority.accept(!getPriority.get()); |
| 103 | + } |
| 104 | + } else { |
| 105 | + setIsDown.accept(false); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Subscribe |
| 110 | + @SuppressWarnings("unused") |
| 111 | + private void onPlayerUpdate(EventPlayerUpdate event) { |
| 112 | + LocalPlayer player = event.getPlayer(); |
| 113 | + Inventory inventory = player.getInventory(); |
| 114 | + |
| 115 | + /* Prevent moving items in other containers */ |
| 116 | + if (minecraft.screen instanceof InventoryScreen) if (!this.inventory.getValue()) return; |
| 117 | + else if (minecraft.screen instanceof AbstractContainerScreen) return; |
| 118 | + |
| 119 | + /* There can only be one enabled at a time */ |
| 120 | + boolean prioritizeRepair = this.repairPriority.getValue(); |
| 121 | + boolean prioritizeGolden = this.goldenPriority.getValue(); |
| 122 | + |
| 123 | + if (prioritizeRepair || prioritizeGolden) { |
| 124 | + if (!isPaused) { |
| 125 | + isPaused = true; |
| 126 | + this.lastSoft = this.soft.getValue(); |
| 127 | + } |
| 128 | + |
| 129 | + this.soft.setValue(true); |
| 130 | + |
| 131 | + if (prioritizeRepair) { |
| 132 | + this.goldenPriority.setValue(false); |
| 133 | + |
| 134 | + for (EquipmentSlot equipmentSlot : equipmentSlots) { |
| 135 | + int armorSlotId = InventoryUtils.getInventorySlot(equipmentSlot); |
| 136 | + |
| 137 | + player.inventoryMenu.slots.stream() |
| 138 | + .filter(slot -> slot.getItem().getItem() instanceof Equipable e && e.getEquipmentSlot() == equipmentSlot) |
| 139 | + .filter(slot -> EnchantmentHelper.getItemEnchantmentLevel(Enchantments.MENDING, slot.getItem()) > 0) |
| 140 | + .min(Comparator.comparingInt(slot -> slot.getItem().getDamageValue())) |
| 141 | + .ifPresent(slot -> { |
| 142 | + Slot armorSlot = player.inventoryMenu.getSlot(armorSlotId); |
| 143 | + ItemStack armorStack = armorSlot.getItem(); |
| 144 | + ItemStack itemStack = slot.getItem(); |
| 145 | + |
| 146 | + int itemDamage = itemStack.getDamageValue(); |
| 147 | + int armorDamage = armorStack.getDamageValue(); |
| 148 | + if (itemDamage > armorDamage) swapSlotId(player, slot, armorSlotId); |
| 149 | + }); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if (prioritizeGolden) { |
| 154 | + this.repairPriority.setValue(false); |
| 155 | + |
| 156 | + for (Item goldArmor : this.goldenArmorPriority) |
| 157 | + player.inventoryMenu.slots.stream() |
| 158 | + .filter(slot -> slot.getItem().getItem() instanceof Equipable e && e == goldArmor) |
| 159 | + .filter(slot -> { /* Only one piece of gold armor */ |
| 160 | + for (Item item : this.goldenArmorPriority) |
| 161 | + if (inventory.armor.stream().map(ItemStack::getItem).toList().contains(item)) |
| 162 | + return false; |
| 163 | + |
| 164 | + return true; |
| 165 | + }).findFirst().ifPresent(slot -> { |
| 166 | + Equipable equipable = (Equipable) slot.getItem().getItem(); |
| 167 | + EquipmentSlot equipmentSlot = equipable.getEquipmentSlot(); |
| 168 | + int armorSlotId = InventoryUtils.getInventorySlot(equipmentSlot); |
| 169 | + this.swapSlotId(player, slot, armorSlotId); |
| 170 | + }); |
| 171 | + } |
| 172 | + } else if (isPaused) { |
| 173 | + isPaused = false; |
| 174 | + this.soft.setValue(this.lastSoft); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private void swapSlotId(LocalPlayer player, Slot slot, int armorSlotId) { |
| 179 | + InventoryUtils.clickSlot(slot.index, false); |
| 180 | + InventoryUtils.clickSlot(armorSlotId, false); |
| 181 | + InventoryUtils.clickSlot(slot.index, false); |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments