|
| 1 | +/* |
| 2 | + * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client). |
| 3 | + * Copyright (c) Meteor Development. |
| 4 | + */ |
| 5 | + |
| 6 | +package meteordevelopment.meteorclient.systems.modules.player; |
| 7 | + |
| 8 | +import meteordevelopment.meteorclient.events.entity.player.AttackEntityEvent; |
| 9 | +import meteordevelopment.meteorclient.events.entity.player.StartBreakingBlockEvent; |
| 10 | +import meteordevelopment.meteorclient.settings.*; |
| 11 | +import meteordevelopment.meteorclient.systems.modules.Categories; |
| 12 | +import meteordevelopment.meteorclient.systems.modules.Module; |
| 13 | +import meteordevelopment.meteorclient.systems.modules.Modules; |
| 14 | +import meteordevelopment.meteorclient.utils.Utils; |
| 15 | +import meteordevelopment.meteorclient.utils.world.BlockUtils; |
| 16 | +import meteordevelopment.orbit.EventHandler; |
| 17 | +import meteordevelopment.orbit.EventPriority; |
| 18 | +import net.minecraft.enchantment.Enchantments; |
| 19 | +import net.minecraft.item.*; |
| 20 | +import net.minecraft.registry.tag.ItemTags; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +public class ToolSaver extends Module { |
| 25 | + private final SettingGroup sgGeneral = settings.getDefaultGroup(); |
| 26 | + |
| 27 | + private final Setting<Integer> durability = sgGeneral.add(new IntSetting.Builder() |
| 28 | + .name("percentage") |
| 29 | + .description("The durability percentage to stop using a tool at") |
| 30 | + .defaultValue(10) |
| 31 | + .range(1, 100) |
| 32 | + .sliderRange(1, 100) |
| 33 | + .build() |
| 34 | + ); |
| 35 | + |
| 36 | + private final Setting<Boolean> onlyMending = sgGeneral.add(new BoolSetting.Builder() |
| 37 | + .name("only-mending") |
| 38 | + .description("Only avoid breaking tools which have mending") |
| 39 | + .build() |
| 40 | + ); |
| 41 | + |
| 42 | + private final Setting<Boolean> onlyUnique = sgGeneral.add(new BoolSetting.Builder() |
| 43 | + .name("only-last-tool") |
| 44 | + .description("Only avoid breaking the last tool of a type unless the other tools are all worse") |
| 45 | + .build() |
| 46 | + ); |
| 47 | + |
| 48 | + private final Setting<Boolean> allowAttack = sgGeneral.add(new BoolSetting.Builder() |
| 49 | + .name("allow-manual-attack") |
| 50 | + .description("Never prevent manual attacks") |
| 51 | + .build() |
| 52 | + ); |
| 53 | + |
| 54 | + private final Setting<ListMode> listMode = sgGeneral.add(new EnumSetting.Builder<ListMode>() |
| 55 | + .name("list-mode") |
| 56 | + .description("Selection mode") |
| 57 | + .defaultValue(ListMode.Blacklist) |
| 58 | + .build() |
| 59 | + ); |
| 60 | + |
| 61 | + private final Setting<List<Item>> whitelist = sgGeneral.add(new ItemListSetting.Builder() |
| 62 | + .name("whitelist") |
| 63 | + .description("Do not allow breaking these tools") |
| 64 | + .visible(() -> listMode.get() == ListMode.Whitelist) |
| 65 | + .filter(ToolSaver::isTool) |
| 66 | + .build() |
| 67 | + ); |
| 68 | + |
| 69 | + private final Setting<List<Item>> blacklist = sgGeneral.add(new ItemListSetting.Builder() |
| 70 | + .name("blacklist") |
| 71 | + .description("Allow breaking these tools") |
| 72 | + .visible(() -> listMode.get() == ListMode.Blacklist) |
| 73 | + .filter(ToolSaver::isTool) |
| 74 | + .build() |
| 75 | + ); |
| 76 | + |
| 77 | + public ToolSaver() { |
| 78 | + super(Categories.Player, "tool-saver", "Prevents breaking tools"); |
| 79 | + } |
| 80 | + |
| 81 | + @EventHandler(priority = EventPriority.HIGH - 1) |
| 82 | + private void onStartBreakingBlock(StartBreakingBlockEvent event) { |
| 83 | + if (!_canUse(mc.player.getMainHandStack()) && mc.world.getBlockState(event.blockPos).getHardness(mc.world, event.blockPos) > 0) { |
| 84 | + mc.options.attackKey.setPressed(false); |
| 85 | + event.cancel(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + @EventHandler(priority = EventPriority.HIGH) |
| 91 | + private void onAttackEntity(AttackEntityEvent event) { |
| 92 | + if (!allowAttack.get() && !_canUse(mc.player.getMainHandStack())) event.cancel(); |
| 93 | + } |
| 94 | + |
| 95 | + private static ToolType toolType(ItemStack is) { |
| 96 | + if (is.isIn(ItemTags.AXES)) return ToolType.AXE; |
| 97 | + if (is.isIn(ItemTags.HOES)) return ToolType.HOE; |
| 98 | + if (is.isIn(ItemTags.PICKAXES)) return ToolType.PICKAXE; |
| 99 | + if (is.isIn(ItemTags.SHOVELS)) return ToolType.SHOVEL; |
| 100 | + if (is.isIn(ItemTags.SWORDS)) return ToolType.SWORD; |
| 101 | + if (is.getItem() instanceof ShearsItem) return ToolType.SHEAR; |
| 102 | + if (is.getItem() instanceof MaceItem) return ToolType.MACE; |
| 103 | + return ToolType.NONE; |
| 104 | + } |
| 105 | + |
| 106 | + public static boolean isTool(Item item) { |
| 107 | + return isTool(item.getDefaultStack()); |
| 108 | + } |
| 109 | + |
| 110 | + public static boolean isTool(ItemStack is) { |
| 111 | + return toolType(is) != ToolType.NONE; |
| 112 | + } |
| 113 | + |
| 114 | + private boolean isWorse(ItemStack a, ItemStack b) { |
| 115 | + // lower material tier (no-one cares about gold) |
| 116 | + if (a.getMaxDamage() < b.getMaxDamage()) return true; |
| 117 | + |
| 118 | + // non-mending tools can always be broken if only-mending is set |
| 119 | + if (onlyMending.get() && Utils.getEnchantmentLevel(a, Enchantments.MENDING) < Utils.getEnchantmentLevel(b, Enchantments.MENDING)) |
| 120 | + return true; |
| 121 | + |
| 122 | + // don't break the more enchanted tool |
| 123 | + if (Utils.getEnchantmentLevel(a, Enchantments.FORTUNE) > 0 && Utils.getEnchantmentLevel(a, Enchantments.FORTUNE) < Utils.getEnchantmentLevel(b, Enchantments.FORTUNE)) |
| 124 | + return true; |
| 125 | + return Utils.getEnchantmentLevel(a, Enchantments.EFFICIENCY) < Utils.getEnchantmentLevel(b, Enchantments.EFFICIENCY); |
| 126 | + } |
| 127 | + |
| 128 | + private boolean canBreak(ItemStack is) { |
| 129 | + if (!isTool(is)) return true; |
| 130 | + if (onlyMending.get() && Utils.getEnchantmentLevel(is, Enchantments.MENDING) == 0) return true; |
| 131 | + if (!onlyUnique.get()) return false; |
| 132 | + |
| 133 | + ToolType tt = toolType(is); |
| 134 | + |
| 135 | + boolean silkTouch = Utils.getEnchantmentLevel(is, Enchantments.SILK_TOUCH) > 0; |
| 136 | + |
| 137 | + int counter = 0; |
| 138 | + |
| 139 | + for (int i = 0; i < 40; i++) { |
| 140 | + ItemStack isi = mc.player.getInventory().getStack(i); |
| 141 | + |
| 142 | + if (tt != toolType(isi)) continue; |
| 143 | + if (silkTouch != Utils.getEnchantmentLevel(isi, Enchantments.SILK_TOUCH) > 0) continue; |
| 144 | + |
| 145 | + if (!isWorse(isi, is)) counter++; |
| 146 | + if (counter > 1) return true; |
| 147 | + } |
| 148 | + |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + private boolean isBroken(ItemStack tool) { |
| 153 | + return tool.getMaxDamage() - tool.getDamage() < tool.getMaxDamage() * durability.get() / 100; |
| 154 | + } |
| 155 | + |
| 156 | + private boolean isIgnored(ItemStack tool) { |
| 157 | + if (listMode.get() == ListMode.Whitelist) return !whitelist.get().contains(tool.getItem()); |
| 158 | + return blacklist.get().contains(tool.getItem()); |
| 159 | + } |
| 160 | + |
| 161 | + private boolean _canUse(ItemStack tool) { |
| 162 | + ToolSaver ts = Modules.get().get(ToolSaver.class); |
| 163 | + return !ts.isActive() || ts.isIgnored(tool) || !ts.isBroken(tool) || ts.canBreak(tool); |
| 164 | + } |
| 165 | + |
| 166 | + public static boolean canUse(ItemStack tool) { |
| 167 | + return Modules.get().get(ToolSaver.class)._canUse(tool); |
| 168 | + } |
| 169 | + |
| 170 | + public enum ListMode { |
| 171 | + Whitelist, |
| 172 | + Blacklist |
| 173 | + } |
| 174 | + |
| 175 | + public enum ToolType { |
| 176 | + NONE, |
| 177 | + AXE, |
| 178 | + HOE, |
| 179 | + PICKAXE, |
| 180 | + SHOVEL, |
| 181 | + SWORD, |
| 182 | + MACE, |
| 183 | + SHEAR, |
| 184 | + } |
| 185 | +} |
0 commit comments