Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better chestswap then whatever the pr #4572 is #4690

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,41 @@

package meteordevelopment.meteorclient.systems.modules.player;

import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import meteordevelopment.meteorclient.settings.BoolSetting;
import meteordevelopment.meteorclient.settings.EnumSetting;
import meteordevelopment.meteorclient.settings.Setting;
import meteordevelopment.meteorclient.settings.SettingGroup;
import meteordevelopment.meteorclient.systems.config.Config;
import meteordevelopment.meteorclient.systems.modules.Categories;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.utils.Utils;
import meteordevelopment.meteorclient.utils.player.InvUtils;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.packet.c2s.play.CloseHandledScreenC2SPacket;
import net.minecraft.registry.entry.RegistryEntry;

public class ChestSwap extends Module {
private final SettingGroup sgGeneral = settings.getDefaultGroup();

private final Setting<Chestplate> chestplate = sgGeneral.add(new EnumSetting.Builder<Chestplate>()
.name("chestplate")
.description("Which type of chestplate to swap to.")
.defaultValue(Chestplate.PreferNetherite)
.defaultValue(Chestplate.Best)
.build()
);

private final Setting<Boolean> enchants = sgGeneral.add(new BoolSetting.Builder()
.name("enchantments")
.description("Whether to handle enchantments when swapping chestplate.")
.defaultValue(true)
.build()
);

Expand Down Expand Up @@ -72,49 +86,45 @@ public void swap() {

private boolean equipChestplate() {
int bestSlot = -1;
boolean breakLoop = false;

int score = 0;
for (int i = 0; i < mc.player.getInventory().main.size(); i++) {
Item item = mc.player.getInventory().main.get(i).getItem();
ItemStack itemStack = mc.player.getInventory().main.get(i);

switch (chestplate.get()) {
case Diamond:
if (item == Items.DIAMOND_CHESTPLATE) {
bestSlot = i;
breakLoop = true;
}
break;
case Netherite:
if (item == Items.NETHERITE_CHESTPLATE) {
bestSlot = i;
breakLoop = true;
}
break;
case PreferDiamond:
if (item == Items.DIAMOND_CHESTPLATE) {
bestSlot = i;
breakLoop = true;
} else if (item == Items.NETHERITE_CHESTPLATE) {
bestSlot = i;
}
break;
case PreferNetherite:
if (item == Items.DIAMOND_CHESTPLATE) {
bestSlot = i;
} else if (item == Items.NETHERITE_CHESTPLATE) {
bestSlot = i;
breakLoop = true;
}
break;
}
if (chestplate.get().item == itemStack.getItem() || (chestplate.get() == Chestplate.Best && itemStack.getItem() instanceof ArmorItem armor && armor.getSlotType() == EquipmentSlot.CHEST)) {
int newScore = getScore(itemStack);

if (breakLoop) break;
if (newScore > score) {
score = newScore;
bestSlot = i;
}
}
}

if (bestSlot != -1) equip(bestSlot);
return bestSlot != -1;
}

// modified AutoArmor getScore
private int getScore(ItemStack itemStack) {
if (itemStack.isEmpty()) return 0;

int score = 0;
Object2IntMap<RegistryEntry<Enchantment>> enchantments = new Object2IntOpenHashMap<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this map a private static final field instead to avoid an allocation. The Utils.getEnchantments() method already clears the map.

Utils.getEnchantments(itemStack, enchantments);
if (enchants.get()) {
score += Utils.getEnchantmentLevel(enchantments, Enchantments.PROTECTION);
score += Utils.getEnchantmentLevel(enchantments, Enchantments.BLAST_PROTECTION);
score += Utils.getEnchantmentLevel(enchantments, Enchantments.FIRE_PROTECTION);
score += Utils.getEnchantmentLevel(enchantments, Enchantments.PROJECTILE_PROTECTION);
score += Utils.getEnchantmentLevel(enchantments, Enchantments.UNBREAKING);
score += 2 * Utils.getEnchantmentLevel(enchantments, Enchantments.MENDING);
}
score += itemStack.getItem() instanceof ArmorItem armorItem ? armorItem.getProtection() : 0;
score += itemStack.getItem() instanceof ArmorItem armorItem ? (int) armorItem.getToughness() : 0;

return score;
}

private void equipElytra() {
for (int i = 0; i < mc.player.getInventory().main.size(); i++) {
Item item = mc.player.getInventory().main.get(i).getItem();
Expand All @@ -141,9 +151,17 @@ public void sendToggledMsg() {
}

public enum Chestplate {
Diamond,
Netherite,
PreferDiamond,
PreferNetherite
Leather(Items.LEATHER_CHESTPLATE),
Chainmail(Items.CHAINMAIL_CHESTPLATE),
Gold(Items.GOLDEN_CHESTPLATE),
Iron(Items.IRON_CHESTPLATE),
Diamond(Items.DIAMOND_CHESTPLATE),
Netherite(Items.NETHERITE_CHESTPLATE),
Best(null);

final Item item;
Chestplate(Item item) {
this.item = item;
}
}
}
Loading