AutoArmor re-write

This commit is contained in:
noil 2020-12-18 20:25:17 -05:00
parent 74648349ee
commit 73d190398e
1 changed files with 59 additions and 73 deletions

View File

@ -6,7 +6,9 @@ import me.rigamortis.seppuku.api.module.Module;
import me.rigamortis.seppuku.api.util.Timer;
import me.rigamortis.seppuku.api.value.Value;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.gui.inventory.GuiInventory;
import net.minecraft.client.renderer.InventoryEffectRenderer;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.init.Enchantments;
import net.minecraft.init.Items;
@ -22,10 +24,10 @@ import team.stiff.pomelo.impl.annotated.handler.annotation.Listener;
*/
public final class AutoArmorModule extends Module {
public final Value<Float> delay = new Value("Delay", new String[]{"Del"}, "The amount of delay in milliseconds.", 50.0f, 0.0f, 1000.0f, 1.0f);
public final Value<Boolean> curse = new Value("Curse", new String[]{"Curses"}, "Prevents you from equipping armor with cursed enchantments.", false);
public final Value<Float> delay = new Value<Float>("Delay", new String[]{"Del"}, "The amount of delay in milliseconds.", 250.0f, 0.0f, 1000.0f, 1.0f);
public final Value<Boolean> curse = new Value<Boolean>("Curse", new String[]{"Curses"}, "Prevents you from equipping armor with cursed enchantments.", false);
private Timer timer = new Timer();
private final Timer equipTimer = new Timer();
public AutoArmorModule() {
super("AutoArmor", new String[]{"AutoArm", "AutoArmour"}, "Automatically equips armor", "NONE", -1, ModuleType.COMBAT);
@ -35,84 +37,68 @@ public final class AutoArmorModule extends Module {
public void onUpdate(EventPlayerUpdate event) {
if (event.getStage() == EventStageable.EventStage.PRE) {
final Minecraft mc = Minecraft.getMinecraft();
if (mc.currentScreen instanceof GuiInventory) {
return;
}
final ItemStack helm = mc.player.inventoryContainer.getSlot(5).getStack();
if (helm.getItem() == Items.AIR) {
final int slot = this.findArmorSlot(EntityEquipmentSlot.HEAD);
if (slot != -1) {
this.clickSlot(slot, 0, ClickType.QUICK_MOVE);
}
}
final ItemStack chest = mc.player.inventoryContainer.getSlot(6).getStack();
if (chest.getItem() == Items.AIR) {
final int slot = this.findArmorSlot(EntityEquipmentSlot.CHEST);
if (slot != -1) {
this.clickSlot(slot, 0, ClickType.QUICK_MOVE);
}
}
final ItemStack legging = mc.player.inventoryContainer.getSlot(7).getStack();
if (legging.getItem() == Items.AIR) {
final int slot = this.findArmorSlot(EntityEquipmentSlot.LEGS);
if (slot != -1) {
this.clickSlot(slot, 0, ClickType.QUICK_MOVE);
}
}
final ItemStack feet = mc.player.inventoryContainer.getSlot(8).getStack();
if (feet.getItem() == Items.AIR) {
final int slot = this.findArmorSlot(EntityEquipmentSlot.FEET);
if (slot != -1) {
this.clickSlot(slot, 0, ClickType.QUICK_MOVE);
}
if (shouldEquip(mc)) {
equip(mc);
equipTimer.reset();
}
}
}
private void clickSlot(int slot, int mouse, ClickType type) {
if (this.timer.passed(this.delay.getValue())) {
Minecraft.getMinecraft().playerController.windowClick(Minecraft.getMinecraft().player.inventoryContainer.windowId, slot, mouse, type, Minecraft.getMinecraft().player);
this.timer.reset();
}
}
private void equip(final Minecraft mc) {
int[] armorSlots = new int[]{-1, -1, -1, -1}, armorValues = new int[]{-1, -1, -1, -1};
private int findArmorSlot(EntityEquipmentSlot type) {
int slot = -1;
float damage = 0;
for (int i = 9; i < 45; i++) {
final ItemStack s = Minecraft.getMinecraft().player.inventoryContainer.getSlot(i).getStack();
if (s != null && s.getItem() != Items.AIR) {
if (s.getItem() instanceof ItemArmor) {
final ItemArmor armor = (ItemArmor) s.getItem();
if (armor.armorType == type) {
final float currentDamage = (armor.damageReduceAmount + EnchantmentHelper.getEnchantmentLevel(Enchantments.PROTECTION, s));
final boolean cursed = this.curse.getValue() ? (EnchantmentHelper.hasBindingCurse(s)) : false;
if (currentDamage > damage && !cursed) {
damage = currentDamage;
slot = i;
}
}
}
for (int type = 0; type < 4; type++) {
ItemStack current = mc.player.inventory.armorItemInSlot(type);
if (!current.isEmpty() && current.getItem() instanceof ItemArmor) {
armorValues[type] = getArmorValue(current);
}
}
return slot;
for (int slot = 9; slot < 45; slot++) {
ItemStack itemStack = mc.player.inventoryContainer.getSlot(slot).getStack();
if (itemStack.isEmpty() || !(itemStack.getItem() instanceof ItemArmor) || itemStack.getCount() > 1 || (this.curse.getValue() && EnchantmentHelper.hasBindingCurse(itemStack)))
continue;
ItemArmor armor = (ItemArmor) itemStack.getItem();
int type = armor.armorType.ordinal() - 2;
if (type == 2 && mc.player.inventory.armorItemInSlot(type).getItem().equals(Items.ELYTRA))
continue;
int value = getArmorValue(itemStack);
if (value > armorValues[type]) {
armorSlots[type] = slot;
armorValues[type] = value;
}
}
for (int type = 0; type < 4; type++) {
int slot = armorSlots[type];
if (slot == -1)
continue;
ItemStack current = mc.player.inventory.armorItemInSlot(type);
if (!current.isEmpty() || mc.player.inventory.getFirstEmptyStack() != -1) {
mc.playerController.windowClick(0, 8 - type, 0, ClickType.QUICK_MOVE, mc.player);
mc.playerController.windowClick(0, slot, 0, ClickType.QUICK_MOVE, mc.player);
break;
}
}
}
private boolean shouldEquip(final Minecraft mc) {
boolean inInventory = mc.currentScreen instanceof GuiContainer && !(mc.currentScreen instanceof InventoryEffectRenderer);
boolean hasDelayFinished = this.equipTimer.passed(this.delay.getValue());
boolean isCreative = mc.player.isCreative();
return !isCreative && hasDelayFinished && !inInventory;
}
private int getArmorValue(ItemStack itemStack) {
if (!itemStack.isEmpty() && itemStack.getItem() instanceof ItemArmor) {
int reductionAmount = ((ItemArmor) itemStack.getItem()).damageReduceAmount;
int enchantmentLevel = EnchantmentHelper.getEnchantmentLevel(Enchantments.PROTECTION, itemStack);
return reductionAmount + enchantmentLevel;
}
return -1;
}
}