| 1 | package thunder.hack.utility.player;
|
| 2 |
|
| 3 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 4 | import net.minecraft.core.registries.Registries;
|
| 5 | import net.minecraft.network.protocol.game.ServerboundSetCarriedItemPacket;
|
| 6 | import net.minecraft.resources.ResourceKey;
|
| 7 | import net.minecraft.tags.ItemTags;
|
| 8 | import net.minecraft.world.damagesource.CombatRules;
|
| 9 | import net.minecraft.world.effect.MobEffects;
|
| 10 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 11 | import net.minecraft.world.entity.player.Player;
|
| 12 | import net.minecraft.world.item.*;
|
| 13 | import net.minecraft.world.item.enchantment.Enchantment;
|
| 14 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 15 | import net.minecraft.world.item.enchantment.Enchantments;
|
| 16 | import net.minecraft.world.level.block.Block;
|
| 17 | import org.jetbrains.annotations.NotNull;
|
| 18 | import thunder.hack.core.Managers;
|
| 19 | import thunder.hack.core.manager.client.ModuleManager;
|
| 20 | import thunder.hack.injection.accesors.IInteractionManager;
|
| 21 |
|
| 22 | import java.util.Arrays;
|
| 23 | import java.util.List;
|
| 24 | import java.util.Objects;
|
| 25 |
|
| 26 | import static thunder.hack.features.modules.Module.mc;
|
| 27 |
|
| 28 | public final class InventoryUtility {
|
| 29 | private static int cachedSlot = -1;
|
| 30 |
|
| 31 | public static int getItemCount(Item item) {
|
| 32 | if (mc.player == null) return 0;
|
| 33 |
|
| 34 | int counter = 0;
|
| 35 |
|
| 36 | for (int i = 0; i <= 44; ++i) {
|
| 37 | ItemStack itemStack = mc.player.getInventory().getItem(i);
|
| 38 | if (itemStack.getItem() != item) continue;
|
| 39 | counter += itemStack.getCount();
|
| 40 | }
|
| 41 |
|
| 42 | return counter;
|
| 43 | }
|
| 44 |
|
| 45 | public static SearchInvResult getAxe() {
|
| 46 | if (mc.player == null) return SearchInvResult.notFound();
|
| 47 | int slot = -1;
|
| 48 | float f = 1.0F;
|
| 49 |
|
| 50 | for (int b1 = 9; b1 < 45; b1++) {
|
| 51 | ItemStack itemStack = mc.player.getInventory().getItem(b1 >= 36 ? b1 - 36 : b1);
|
| 52 | if (itemStack != null && itemStack.getItem() instanceof AxeItem) {
|
| 53 | float f1 = itemStack.getMaxDamage();
|
| 54 | f1 += getEnchantmentLevel(Enchantments.SHARPNESS, itemStack);
|
| 55 | if (f1 > f) {
|
| 56 | f = f1;
|
| 57 | slot = b1;
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | if (slot >= 36) slot = slot - 36;
|
| 63 |
|
| 64 | if (slot == -1) return SearchInvResult.notFound();
|
| 65 | return new SearchInvResult(slot, true, mc.player.getInventory().getItem(slot));
|
| 66 | }
|
| 67 |
|
| 68 | public static SearchInvResult getPickAxeHotbar() {
|
| 69 | if (mc.player == null) return SearchInvResult.notFound();
|
| 70 |
|
| 71 | int slot = -1;
|
| 72 | float f = 1.0F;
|
| 73 | for (int b1 = 0; b1 < 9; b1++) {
|
| 74 | ItemStack itemStack = mc.player.getInventory().getItem(b1);
|
| 75 | if (itemStack != null && itemStack.is(ItemTags.PICKAXES)) {
|
| 76 | float f1 = 0;
|
| 77 | f1 += getEnchantmentLevel(Enchantments.EFFICIENCY, itemStack);
|
| 78 | if (f1 > f) {
|
| 79 | f = f1;
|
| 80 | slot = b1;
|
| 81 | }
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | if (slot == -1) return SearchInvResult.notFound();
|
| 86 | return new SearchInvResult(slot, true, mc.player.getInventory().getItem(slot));
|
| 87 | }
|
| 88 |
|
| 89 | public static SearchInvResult getPickAxe() {
|
| 90 | if (mc.player == null) return SearchInvResult.notFound();
|
| 91 |
|
| 92 | int slot = -1;
|
| 93 | float f = 1.0F;
|
| 94 | for (int b1 = 9; b1 < 45; b1++) {
|
| 95 | ItemStack itemStack = mc.player.getInventory().getItem(b1);
|
| 96 | if (itemStack != null && itemStack.is(ItemTags.PICKAXES)) {
|
| 97 | float f1 = 0;
|
| 98 | f1 += getEnchantmentLevel(Enchantments.EFFICIENCY, itemStack);
|
| 99 | if (f1 > f) {
|
| 100 | f = f1;
|
| 101 | slot = b1;
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | if (slot == -1) return SearchInvResult.notFound();
|
| 107 | return new SearchInvResult(slot, true, mc.player.getInventory().getItem(slot));
|
| 108 | }
|
| 109 |
|
| 110 | public static SearchInvResult getPickAxeHotBar() {
|
| 111 | if (mc.player == null) return SearchInvResult.notFound();
|
| 112 |
|
| 113 | int slot = -1;
|
| 114 | float f = 1.0F;
|
| 115 | for (int b1 = 0; b1 < 9; b1++) {
|
| 116 | ItemStack itemStack = mc.player.getInventory().getItem(b1);
|
| 117 | if (itemStack != null && itemStack.is(ItemTags.PICKAXES)) {
|
| 118 | float f1 = 0;
|
| 119 | f1 += getEnchantmentLevel(Enchantments.EFFICIENCY, itemStack);
|
| 120 | if (f1 > f) {
|
| 121 | f = f1;
|
| 122 | slot = b1;
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | if (slot == -1) return SearchInvResult.notFound();
|
| 128 | return new SearchInvResult(slot, true, mc.player.getInventory().getItem(slot));
|
| 129 | }
|
| 130 |
|
| 131 | public static SearchInvResult getSkull() {
|
| 132 | if (mc.player == null) return SearchInvResult.notFound();
|
| 133 | int slot = -1;
|
| 134 | for (int b1 = 0; b1 < 9; b1++) {
|
| 135 | ItemStack itemStack = mc.player.getInventory().getItem(b1);
|
| 136 | if (itemStack != null &&
|
| 137 | (itemStack.getItem().equals(Items.SKELETON_SKULL)
|
| 138 | || itemStack.getItem().equals(Items.WITHER_SKELETON_SKULL)
|
| 139 | || itemStack.getItem().equals(Items.CREEPER_HEAD)
|
| 140 | || itemStack.getItem().equals(Items.PLAYER_HEAD)
|
| 141 | || itemStack.getItem().equals(Items.ZOMBIE_HEAD))) {
|
| 142 | slot = b1;
|
| 143 | break;
|
| 144 | }
|
| 145 | }
|
| 146 | if (slot == -1) return SearchInvResult.notFound();
|
| 147 | return new SearchInvResult(slot, true, mc.player.getInventory().getItem(slot));
|
| 148 | }
|
| 149 |
|
| 150 | public static SearchInvResult getSword() {
|
| 151 | if (mc.player == null) return SearchInvResult.notFound();
|
| 152 |
|
| 153 | int slot = -1;
|
| 154 | float f = 1.0F;
|
| 155 | for (int b1 = 9; b1 < 45; b1++) {
|
| 156 | ItemStack itemStack = mc.player.getInventory().getItem(b1);
|
| 157 | if (itemStack != null && itemStack.is(ItemTags.SWORDS)) {
|
| 158 | float f1 = itemStack.getMaxDamage();
|
| 159 | f1 += getEnchantmentLevel(Enchantments.SHARPNESS, itemStack);
|
| 160 | if (f1 > f) {
|
| 161 | f = f1;
|
| 162 | slot = b1;
|
| 163 | }
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | if (slot == -1) return SearchInvResult.notFound();
|
| 168 | return new SearchInvResult(slot, true, mc.player.getInventory().getItem(slot));
|
| 169 | }
|
| 170 |
|
| 171 | public static SearchInvResult getSwordHotBar() {
|
| 172 | if (mc.player == null) return SearchInvResult.notFound();
|
| 173 |
|
| 174 | int slot = -1;
|
| 175 | float f = 1.0F;
|
| 176 | for (int b1 = 0; b1 < 9; b1++) {
|
| 177 | ItemStack itemStack = mc.player.getInventory().getItem(b1);
|
| 178 | if (itemStack != null && itemStack.is(ItemTags.SWORDS)) {
|
| 179 | float f1 = itemStack.getMaxDamage();
|
| 180 | f1 += getEnchantmentLevel(Enchantments.SHARPNESS, itemStack);
|
| 181 | if (f1 > f) {
|
| 182 | f = f1;
|
| 183 | slot = b1;
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | if (slot == -1) return SearchInvResult.notFound();
|
| 189 | return new SearchInvResult(slot, true, mc.player.getInventory().getItem(slot));
|
| 190 | }
|
| 191 |
|
| 192 | // TODO check
|
| 193 | public static SearchInvResult getAxeHotBar() {
|
| 194 | if (mc.player == null) return SearchInvResult.notFound();
|
| 195 |
|
| 196 | int slot = -1;
|
| 197 | float f = 1.0F;
|
| 198 | for (int b1 = 0; b1 < 9; b1++) {
|
| 199 | ItemStack itemStack = mc.player.getInventory().getItem(b1);
|
| 200 | if (itemStack != null && itemStack.getItem() instanceof AxeItem) {
|
| 201 | float f1 = itemStack.getMaxDamage();
|
| 202 | f1 += getEnchantmentLevel(Enchantments.SHARPNESS, itemStack);
|
| 203 | if (f1 > f) {
|
| 204 | f = f1;
|
| 205 | slot = b1;
|
| 206 | }
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | if (slot == -1) return SearchInvResult.notFound();
|
| 211 | return new SearchInvResult(slot, true, mc.player.getInventory().getItem(slot));
|
| 212 | }
|
| 213 |
|
| 214 |
|
| 215 | public static int getElytra() {
|
| 216 | for (ItemStack stack : ArmorUtility.getArmorItems(mc.player))
|
| 217 | if (stack.getItem() == Items.ELYTRA && stack.getDamageValue() < 430)
|
| 218 | return -2;
|
| 219 |
|
| 220 | int slot = -1;
|
| 221 | for (int i = 0; i < 36; i++) {
|
| 222 | ItemStack s = mc.player.getInventory().getItem(i);
|
| 223 | if (s.getItem() == Items.ELYTRA && s.getDamageValue() < 430) {
|
| 224 | slot = i;
|
| 225 | break;
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | if (slot < 9 && slot != -1)
|
| 230 | slot = slot + 36;
|
| 231 |
|
| 232 | return slot;
|
| 233 | }
|
| 234 |
|
| 235 | public static SearchInvResult findInHotBar(Searcher searcher) {
|
| 236 | if (mc.player != null) {
|
| 237 | for (int i = 0; i < 9; ++i) {
|
| 238 | ItemStack stack = mc.player.getInventory().getItem(i);
|
| 239 | if (searcher.isValid(stack)) {
|
| 240 | return new SearchInvResult(i, true, stack);
|
| 241 | }
|
| 242 | }
|
| 243 | }
|
| 244 |
|
| 245 | return SearchInvResult.notFound();
|
| 246 | }
|
| 247 |
|
| 248 | public static SearchInvResult findItemInHotBar(List<Item> items) {
|
| 249 | return findInHotBar(stack -> items.contains(stack.getItem()));
|
| 250 | }
|
| 251 |
|
| 252 | public static SearchInvResult findItemInHotBar(Item... items) {
|
| 253 | return findItemInHotBar(Arrays.asList(items));
|
| 254 | }
|
| 255 |
|
| 256 | public static SearchInvResult findInInventory(Searcher searcher) {
|
| 257 | if (mc.player != null) {
|
| 258 | for (int i = 36; i >= 0; i--) {
|
| 259 | ItemStack stack = mc.player.getInventory().getItem(i);
|
| 260 | if (searcher.isValid(stack)) {
|
| 261 | if (i < 9) i += 36;
|
| 262 | return new SearchInvResult(i, true, stack);
|
| 263 | }
|
| 264 | }
|
| 265 | }
|
| 266 |
|
| 267 | return SearchInvResult.notFound();
|
| 268 | }
|
| 269 |
|
| 270 | public static SearchInvResult findItemInInventory(List<Item> items) {
|
| 271 | return findInInventory(stack -> items.contains(stack.getItem()));
|
| 272 | }
|
| 273 |
|
| 274 | public static SearchInvResult findItemInInventory(Item... items) {
|
| 275 | return findItemInInventory(Arrays.asList(items));
|
| 276 | }
|
| 277 |
|
| 278 | public static SearchInvResult findBlockInHotBar(@NotNull List<Block> blocks) {
|
| 279 | return findItemInHotBar(blocks.stream().map(Block::asItem).toList());
|
| 280 | }
|
| 281 |
|
| 282 | public static SearchInvResult findBlockInHotBar(Block... blocks) {
|
| 283 | return findItemInHotBar(Arrays.stream(blocks).map(Block::asItem).toList());
|
| 284 | }
|
| 285 |
|
| 286 | public static SearchInvResult findBlockInInventory(@NotNull List<Block> blocks) {
|
| 287 | return findItemInInventory(blocks.stream().map(Block::asItem).toList());
|
| 288 | }
|
| 289 |
|
| 290 | public static SearchInvResult findBlockInInventory(Block... blocks) {
|
| 291 | return findItemInInventory(Arrays.stream(blocks).map(Block::asItem).toList());
|
| 292 | }
|
| 293 |
|
| 294 | public static void saveSlot() {
|
| 295 | cachedSlot = mc.player.getInventory().getSelectedSlot();
|
| 296 | }
|
| 297 |
|
| 298 | public static void returnSlot() {
|
| 299 | if (cachedSlot != -1)
|
| 300 | switchTo(cachedSlot);
|
| 301 | cachedSlot = -1;
|
| 302 | }
|
| 303 |
|
| 304 | public static void saveAndSwitchTo(int slot) {
|
| 305 | saveSlot();
|
| 306 | if (mc.player == null || mc.getConnection() == null) return;
|
| 307 | if (mc.player.getInventory().getSelectedSlot() == slot && Managers.PLAYER.serverSideSlot == slot)
|
| 308 | return;
|
| 309 | mc.player.getInventory().setSelectedSlot(slot);
|
| 310 | ((IInteractionManager) mc.gameMode).syncSlot();
|
| 311 | }
|
| 312 |
|
| 313 | public static void switchTo(int slot) {
|
| 314 | if (mc.player == null || mc.getConnection() == null) return;
|
| 315 | if (mc.player.getInventory().getSelectedSlot() == slot && Managers.PLAYER.serverSideSlot == slot)
|
| 316 | return;
|
| 317 | mc.player.getInventory().setSelectedSlot(slot);
|
| 318 | ((IInteractionManager) mc.gameMode).syncSlot();
|
| 319 | }
|
| 320 |
|
| 321 | public static void switchToSilent(int slot) {
|
| 322 | if (mc.player == null || mc.getConnection() == null) return;
|
| 323 | mc.getConnection().send(new ServerboundSetCarriedItemPacket(slot));
|
| 324 | }
|
| 325 |
|
| 326 | public static SearchInvResult getAntiWeaknessItem() {
|
| 327 | if (mc.player == null) return SearchInvResult.notFound();
|
| 328 |
|
| 329 | Item mainHand = mc.player.getMainHandItem().getItem();
|
| 330 | if (mc.player.getMainHandItem().is(ItemTags.SWORDS)
|
| 331 | || mc.player.getMainHandItem().is(ItemTags.PICKAXES)
|
| 332 | || mainHand instanceof AxeItem
|
| 333 | || mainHand instanceof ShovelItem) {
|
| 334 | return new SearchInvResult(mc.player.getInventory().getSelectedSlot(), true, mc.player.getMainHandItem());
|
| 335 | }
|
| 336 |
|
| 337 | return findInHotBar(
|
| 338 | itemStack -> itemStack.is(ItemTags.SWORDS)
|
| 339 | || itemStack.is(ItemTags.PICKAXES)
|
| 340 | || itemStack.getItem() instanceof AxeItem
|
| 341 | || itemStack.getItem() instanceof ShovelItem
|
| 342 | );
|
| 343 | }
|
| 344 |
|
| 345 | public static float getHitDamage(@NotNull ItemStack weapon, Player ent) {
|
| 346 | if (mc.player == null) return 0;
|
| 347 | float baseDamage = 1f;
|
| 348 |
|
| 349 | if (weapon.is(ItemTags.SWORDS))
|
| 350 | baseDamage = 7;
|
| 351 |
|
| 352 | if (weapon.getItem() instanceof AxeItem)
|
| 353 | baseDamage = 9;
|
| 354 |
|
| 355 | if (mc.player.fallDistance > 0 || ModuleManager.criticals.isEnabled())
|
| 356 | baseDamage += baseDamage / 2f;
|
| 357 |
|
| 358 | if (mc.player.hasEffect(MobEffects.STRENGTH)) {
|
| 359 | int strength = Objects.requireNonNull(mc.player.getEffect(MobEffects.STRENGTH)).getAmplifier() + 1;
|
| 360 | baseDamage += 3 * strength;
|
| 361 | }
|
| 362 |
|
| 363 | // Reduce by armour
|
| 364 | baseDamage = CombatRules.getDamageAfterAbsorb(ent, baseDamage, mc.level.damageSources().generic(), ent.getArmorValue(), (float) ent.getAttribute(Attributes.ARMOR_TOUGHNESS).getValue());
|
| 365 | return baseDamage;
|
| 366 | }
|
| 367 |
|
| 368 | public static SearchInvResult findBedInHotBar() {
|
| 369 | if (mc.player == null) return SearchInvResult.notFound();
|
| 370 | for (int b1 = 0; b1 < 9; b1++) {
|
| 371 | ItemStack itemStack = mc.player.getInventory().getItem(b1);
|
| 372 | if (itemStack != null && itemStack.getItem() instanceof BedItem)
|
| 373 | return new SearchInvResult(b1, true, mc.player.getInventory().getItem(b1));
|
| 374 | }
|
| 375 | return SearchInvResult.notFound();
|
| 376 | }
|
| 377 |
|
| 378 | public static SearchInvResult findBed() {
|
| 379 | if (mc.player == null) return SearchInvResult.notFound();
|
| 380 | for (int b1 = 9; b1 < 45; b1++) {
|
| 381 | ItemStack itemStack = mc.player.getInventory().getItem(b1 >= 36 ? b1 - 36 : b1);
|
| 382 | if (itemStack != null && itemStack.getItem() instanceof BedItem)
|
| 383 | return new SearchInvResult(b1, true, mc.player.getInventory().getItem(b1));
|
| 384 | }
|
| 385 | return SearchInvResult.notFound();
|
| 386 | }
|
| 387 |
|
| 388 | public static Item getItem(String Name) {
|
| 389 | if (Name == null) return Items.AIR;
|
| 390 | for (Block block : BuiltInRegistries.BLOCK)
|
| 391 | if (block.getDescriptionId().replace("block.minecraft.", "").equals(Name.toLowerCase()))
|
| 392 | return block.asItem();
|
| 393 | for (Item item : BuiltInRegistries.ITEM)
|
| 394 | if (item.getDescriptionId().replace("item.minecraft.", "").equals(Name.toLowerCase()))
|
| 395 | return item;
|
| 396 | return Items.DIRT;
|
| 397 | }
|
| 398 |
|
| 399 | public static int getBedsCount() {
|
| 400 | if (mc.player == null) return 0;
|
| 401 |
|
| 402 | int counter = 0;
|
| 403 |
|
| 404 | for (int i = 0; i <= 44; ++i) {
|
| 405 | ItemStack itemStack = mc.player.getInventory().getItem(i);
|
| 406 | if (!(itemStack.getItem() instanceof BedItem)) continue;
|
| 407 | counter += itemStack.getCount();
|
| 408 | }
|
| 409 |
|
| 410 | return counter;
|
| 411 | }
|
| 412 |
|
| 413 | private static int getEnchantmentLevel(ResourceKey<Enchantment> enchantment, ItemStack stack) {
|
| 414 | return EnchantmentHelper.getItemEnchantmentLevel(mc.level.registryAccess().lookupOrThrow(Registries.ENCHANTMENT).getOrThrow(enchantment), stack);
|
| 415 | }
|
| 416 |
|
| 417 | public interface Searcher {
|
| 418 | boolean isValid(ItemStack stack);
|
| 419 | }
|
| 420 | }
|
| 421 |
|