package dev.crucifixbeta.mod.modules.impl.misc;

import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.UpdateEvent;
import dev.crucifixbeta.api.utils.math.Timer;
import dev.crucifixbeta.api.utils.player.InventoryUtil;
import dev.crucifixbeta.api.utils.world.BlockUtil;
import dev.crucifixbeta.core.Manager;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.impl.player.PacketMine;
import dev.crucifixbeta.mod.modules.settings.impl.BooleanSetting;
import dev.crucifixbeta.mod.modules.settings.impl.EnumSetting;
import dev.crucifixbeta.mod.modules.settings.impl.SliderSetting;
import dev.crucifixbeta.mod.modules.settings.impl.StringSetting;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.ints.IntListIterator;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.BedBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.PistonBlock;
import net.minecraft.block.ShulkerBoxBlock;
import net.minecraft.client.gui.screen.ingame.ShulkerBoxScreen;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.FireworkExplosionComponent;
import net.minecraft.component.type.FireworksComponent;
import net.minecraft.component.type.PotionContentsComponent;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.PotionItem;
import net.minecraft.item.SplashPotionItem;
import net.minecraft.registry.Registries;
import net.minecraft.screen.ShulkerBoxScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;

@Environment(EnvType.CLIENT)
public class ChestStealerPlus extends Module {
   public static ChestStealerPlus INSTANCE;
   public final BooleanSetting rotate = this.add(new BooleanSetting("Rotate", true));
   public final Timer timeoutTimer = new Timer();
   final int[] stealCountList = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
   private final BooleanSetting autoDisable = this.add(new BooleanSetting("AutoDisable", true));
   private final SliderSetting disableTime = this.add(new SliderSetting("DisableTime", 500.0, 0.0, 1000.0, 50.0));
   private final BooleanSetting place = this.add(new BooleanSetting("Place", true));
   private final BooleanSetting inventory = this.add(new BooleanSetting("InventorySwap", true));
   private final BooleanSetting preferOpen = this.add(new BooleanSetting("PerferOpen", true));
   private final BooleanSetting open = this.add(new BooleanSetting("Open", true));
   private final SliderSetting range = this.add(new SliderSetting("MaxRange", 4.0, 0.0, 6.0, 0.1));
   private final SliderSetting minRange = this.add(new SliderSetting("MinRange", 1.0, 0.0, 3.0, 0.1));
   private final BooleanSetting mine = this.add(new BooleanSetting("Mine", true));
   private final EnumSetting<ChestStealerPlus.Mode> mode = this.add(new EnumSetting<>("Mode", ChestStealerPlus.Mode.SMART));
   private final BooleanSetting take = this.add(new BooleanSetting("Take", true, () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART));
   private final BooleanSetting smart = this.add(
         new BooleanSetting("Smart", true, () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue())
      )
      .setParent();
   private final BooleanSetting forceMove = this.add(
      new BooleanSetting(
         "ForceQuickMove", true, () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting crystal = this.add(
      new SliderSetting(
         "Crystal",
         256.0,
         0.0,
         512.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting exp = this.add(
      new SliderSetting(
         "Exp",
         256.0,
         0.0,
         512.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting totem = this.add(
      new SliderSetting(
         "Totem",
         6.0,
         0.0,
         36.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting gapple = this.add(
      new SliderSetting(
         "Gapple",
         128.0,
         0.0,
         512.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting obsidian = this.add(
      new SliderSetting(
         "Obsidian",
         64.0,
         0.0,
         512.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting web = this.add(
      new SliderSetting(
         "Web",
         64.0,
         0.0,
         512.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting glowstone = this.add(
      new SliderSetting(
         "Glowstone",
         128.0,
         0.0,
         512.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting anchor = this.add(
      new SliderSetting(
         "Anchor",
         128.0,
         0.0,
         512.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting pearl = this.add(
      new SliderSetting(
         "Pearl",
         16.0,
         0.0,
         64.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting piston = this.add(
      new SliderSetting(
         "Piston",
         64.0,
         0.0,
         512.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting redstone = this.add(
      new SliderSetting(
         "RedStone",
         64.0,
         0.0,
         512.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting bed = this.add(
      new SliderSetting(
         "Bed",
         256.0,
         0.0,
         512.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting speed = this.add(
      new SliderSetting(
         "Speed",
         1.0,
         0.0,
         8.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting resistance = this.add(
      new SliderSetting(
         "Resistance",
         1.0,
         0.0,
         8.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final SliderSetting strength = this.add(
      new SliderSetting(
         "Strength",
         1.0,
         0.0,
         8.0,
         1.0,
         () -> this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.take.getValue() && this.smart.isOpen()
      )
   );
   private final BooleanSetting closeAfterRegear = this.add(
      new BooleanSetting("CloseAfter", false, () -> this.mode.getValue() == ChestStealerPlus.Mode.REGEAR)
   );
   private final SliderSetting regearSpeed = this.add(
      new SliderSetting("RegearSpeed", 1.0, 1.0, 1000.0, 1.0, () -> this.mode.getValue() == ChestStealerPlus.Mode.REGEAR)
   );
   private final SliderSetting regearClicks = this.add(
      new SliderSetting("ClicksPerTick", 1.0, 1.0, 36.0, 1.0, () -> this.mode.getValue() == ChestStealerPlus.Mode.REGEAR)
   );
   private final StringSetting kitName = this.add(
      new StringSetting("KitName", "kit1", () -> this.mode.getValue() == ChestStealerPlus.Mode.REGEAR)
   );
   private final Timer timer = new Timer();
   private final Timer regearTimer = new Timer();
   private final List<BlockPos> openList = new ArrayList<>();
   private final Map<Integer, String> regearExpectedInv = new HashMap<>();
   private boolean regearSetupDone = false;
   public BlockPos placePos = null;
   private BlockPos openPos;
   private boolean opend = false;

   public ChestStealerPlus() {
      super("ChestStealer+", Module.Category.Misc);
      this.setChinese("\u81ea\u52a8\u8865\u7ed9+");
      INSTANCE = this;
      this.initChineseTranslations();
   }

   private void initChineseTranslations() {
      this.rotate.setChinese("\u65cb\u8f6c");
      this.autoDisable.setChinese("\u81ea\u52a8\u5173\u95ed");
      this.disableTime.setChinese("\u5173\u95ed\u65f6\u95f4");
      this.place.setChinese("\u653e\u7f6e\u5bb9\u5668");
      this.inventory.setChinese("\u80cc\u5305\u4ea4\u6362");
      this.preferOpen.setChinese("\u4f18\u5148\u5f00\u542f");
      this.open.setChinese("\u81ea\u52a8\u5f00\u542f");
      this.range.setChinese("\u6700\u5927\u8303\u56f4");
      this.minRange.setChinese("\u6700\u5c0f\u8303\u56f4");
      this.mine.setChinese("\u6316\u6398\u5bb9\u5668");
      this.mode.setChinese("\u6a21\u5f0f");
      this.take.setChinese("\u62ff\u53d6\u7269\u54c1");
      this.smart.setChinese("\u667a\u80fd\u7b5b\u9009");
      this.smart.setParent();
      this.forceMove.setChinese("\u5feb\u901f\u79fb\u52a8");
      this.crystal.setChinese("\u6c34\u6676\u6570\u91cf");
      this.exp.setChinese("\u7ecf\u9a8c\u74f6\u6570\u91cf");
      this.totem.setChinese("\u56fe\u817e\u6570\u91cf");
      this.gapple.setChinese("\u91d1\u82f9\u679c\u6570\u91cf");
      this.obsidian.setChinese("\u9ed1\u66dc\u77f3\u6570\u91cf");
      this.web.setChinese("\u8718\u86db\u7f51\u6570\u91cf");
      this.glowstone.setChinese("\u8424\u77f3\u6570\u91cf");
      this.anchor.setChinese("\u91cd\u751f\u951a\u6570\u91cf");
      this.pearl.setChinese("\u672b\u5f71\u73cd\u73e0\u6570\u91cf");
      this.piston.setChinese("\u6d3b\u585e\u6570\u91cf");
      this.redstone.setChinese("\u7ea2\u77f3\u6570\u91cf");
      this.bed.setChinese("\u5e8a\u6570\u91cf");
      this.speed.setChinese("\u901f\u5ea6\u836f\u6c34");
      this.resistance.setChinese("\u6297\u6027\u836f\u6c34");
      this.strength.setChinese("\u529b\u91cf\u836f\u6c34");
      this.closeAfterRegear.setChinese("\u8865\u7ed9\u540e\u5173\u95ed");
      this.regearSpeed.setChinese("\u8865\u7ed9\u901f\u5ea6");
      this.regearClicks.setChinese("\u6bcftick\u70b9\u51fb\u6570");
      this.kitName.setChinese("\u5de5\u5177\u5305\u540d\u79f0");
   }

   public void loadCurrentKit() {
      try {
         String var1 = this.kitName.getValue();
         if (var1 == null || var1.isEmpty()) {
            this.sendMessage(Formatting.RED + "No kit name specified!");
            return;
         }

         File var2 = Manager.getFile(var1 + ".kit");
         if (!var2.exists()) {
            this.sendMessage(Formatting.RED + "Kit '" + var1 + "' not found! Use /kit create " + var1);
            return;
         }

         this.regearExpectedInv.clear();

         for (String var5 : Files.readAllLines(var2.toPath(), StandardCharsets.UTF_8)) {
            var5 = var5.trim();
            if (!var5.isEmpty() && !var5.startsWith("#")) {
               String[] var6 = var5.split(":", 2);
               if (var6.length == 2) {
                  try {
                     int var7 = Integer.parseInt(var6[0].trim());
                     this.regearExpectedInv.put(var7, var6[1].trim());
                  } catch (NumberFormatException var8) {
                  }
               }
            }
         }

         this.regearSetupDone = true;
      } catch (Exception var9) {
         this.sendMessage(Formatting.RED + "Error loading kit: " + var9.getMessage());
      }
   }

   public String getCurrentKitName() {
      return this.kitName.getValue();
   }

   public void setupRegear(String kitItems) {
      if (kitItems.isEmpty()) {
         this.sendMessage(Formatting.RED + "Invalid kit! Create it again.");
      } else {
         this.regearExpectedInv.clear();
         String[] var2 = kitItems.split(" ");

         for (String var6 : var2) {
            if (var6.contains("=")) {
               String[] var7 = var6.split("=");
               if (var7.length == 2) {
                  try {
                     int var8 = Integer.parseInt(var7[0]);
                     this.regearExpectedInv.put(var8, var7[1]);
                  } catch (NumberFormatException var9) {
                  }
               }
            }
         }

         this.regearSetupDone = true;
      }
   }

   private String getPotionKey(ItemStack stack) {
      if (!(stack.getItem() instanceof PotionItem)) {
         return null;
      } else {
         PotionContentsComponent var2 = stack.get(DataComponentTypes.POTION_CONTENTS);
         if (var2 == null) {
            return null;
         } else if (!var2.getEffects().iterator().hasNext()) {
            return null;
         } else {
            StatusEffectInstance var3 = var2.getEffects().iterator().next();
            String var4 = Registries.STATUS_EFFECT.getId(var3.getEffectType().value()).toString();
            String var5 = var4.replace("minecraft:", "");
            boolean var6 = stack.getItem() instanceof SplashPotionItem;
            boolean var7 = var3.getAmplifier() >= 1;
            boolean var8 = var3.getDuration() >= 480;
            String var9 = this.getPotionTypeName(var5, var7, var8);
            if (var9 == null) {
               return null;
            } else {
               return var6 ? "minecraft:splash_potion{Potion:" + var9 + "}" : "minecraft:potion{Potion:" + var9 + "}";
            }
         }
      }
   }

   private String getPotionFullKey(ItemStack stack) {
      if (!(stack.getItem() instanceof PotionItem)) {
         return null;
      } else {
         PotionContentsComponent var2 = stack.get(DataComponentTypes.POTION_CONTENTS);
         if (var2 != null && var2.getEffects().iterator().hasNext()) {
            StringBuilder var3 = new StringBuilder();
            boolean var4 = stack.getItem() instanceof SplashPotionItem;
            var3.append(var4 ? "minecraft:splash_potion[" : "minecraft:potion[");

            for (StatusEffectInstance var6 : var2.getEffects()) {
               String var7 = Registries.STATUS_EFFECT.getId(var6.getEffectType().value()).toString();
               var3.append(var7).append(":").append(var6.getAmplifier()).append(":").append(var6.getDuration());
               var3.append(";");
            }

            var3.append("]");
            return var3.toString();
         } else {
            return null;
         }
      }
   }

   private String getFireworkKey(ItemStack stack) {
      if (stack.getItem() != Items.FIREWORK_ROCKET) {
         return null;
      } else {
         FireworksComponent var2 = stack.get(DataComponentTypes.FIREWORKS);
         if (var2 == null) {
            return null;
         } else {
            int var3 = var2.flightDuration();
            StringBuilder var4 = new StringBuilder();
            var4.append("flight:").append(var3);
            if (var2.explosions() != null) {
               for (FireworkExplosionComponent var6 : var2.explosions()) {
                  var4.append("|type:").append(var6.shape().getName());
                  IntList var7 = var6.colors();
                  if (var7 != null && !var7.isEmpty()) {
                     var4.append(":colors:");
                     IntListIterator var8 = var7.iterator();

                     while (var8.hasNext()) {
                        int var9 = (Integer)var8.next();
                        var4.append(var9).append(",");
                     }

                     var4.deleteCharAt(var4.length() - 1);
                  }

                  IntList var11 = var6.fadeColors();
                  if (var11 != null && !var11.isEmpty()) {
                     var4.append(":fade:");
                     IntListIterator var12 = var11.iterator();

                     while (var12.hasNext()) {
                        int var10 = (Integer)var12.next();
                        var4.append(var10).append(",");
                     }

                     var4.deleteCharAt(var4.length() - 1);
                  }

                  if (var6.hasTrail()) {
                     var4.append(":trail");
                  }

                  if (var6.hasTwinkle()) {
                     var4.append(":twinkle");
                  }
               }
            }

            return "minecraft:firework_rocket{" + var4.toString() + "}";
         }
      }
   }

   private String getPotionTypeName(String effectName, boolean strong, boolean longDur) {
      switch (effectName) {
         case "speed":
            return strong ? "strong_swiftness" : (longDur ? "long_swiftness" : "swiftness");
         case "haste":
            return strong ? "strong_haste" : (longDur ? "long_haste" : "haste");
         case "strength":
            return strong ? "strength" : (longDur ? "long_strength" : "strength");
         case "leaping":
            return strong ? "strong_leaping" : (longDur ? "long_leaping" : "leaping");
         case "regeneration":
            return strong ? "strong_regeneration" : (longDur ? "long_regeneration" : "regeneration");
         case "resistance":
            return longDur ? "long_resistance" : "resistance";
         case "fire_resistance":
            return longDur ? "long_fire_resistance" : "fire_resistance";
         case "water_breathing":
            return longDur ? "long_water_breathing" : "water_breathing";
         case "invisibility":
            return longDur ? "long_invisibility" : "invisibility";
         case "night_vision":
            return longDur ? "long_night_vision" : "night_vision";
         case "slow_falling":
            return longDur ? "long_slow_falling" : "slow_falling";
         case "poison":
            return strong ? "strong_poison" : (longDur ? "long_poison" : "poison");
         case "weakness":
            return longDur ? "long_weakness" : "weakness";
         case "slowness":
            return strong ? "strong_slowness" : (longDur ? "long_slowness" : "slowness");
         case "instant_health":
            return strong ? "strong_healing" : "healing";
         case "instant_damage":
            return strong ? "strong_harming" : "harming";
         default:
            return effectName;
      }
   }

   private boolean isSlotCorrect(int slot, String expectedItemKey, ItemStack currentStack) {
      if (currentStack.isEmpty()) {
         return false;
      } else if ((expectedItemKey.startsWith("minecraft:potion[") || expectedItemKey.startsWith("minecraft:splash_potion["))
         && currentStack.getItem() instanceof PotionItem) {
         String var6 = this.getPotionFullKey(currentStack);
         return var6 == null ? false : var6.equals(expectedItemKey);
      } else if (expectedItemKey.startsWith("minecraft:firework_rocket{") && currentStack.getItem() == Items.FIREWORK_ROCKET) {
         String var5 = this.getFireworkKey(currentStack);
         return var5 == null ? false : var5.equals(expectedItemKey);
      } else {
         String var4 = Registries.ITEM.getId(currentStack.getItem()).toString();
         return var4.equals(expectedItemKey);
      }
   }

   private boolean needToTakeItem(ItemStack currentStack, String expectedItem) {
      return currentStack.isEmpty() ? true : !this.isSlotCorrect(0, expectedItem, currentStack);
   }

   private int findItemInShulker(String itemName, ShulkerBoxScreenHandler handler, int excludeSlot) {
      int var4 = handler.slots.size() == 63 ? 27 : 54;
      int var5 = -1;
      int var6 = 0;

      for (int var7 = 0; var7 < var4; var7++) {
         if (var7 != excludeSlot) {
            ItemStack var8 = handler.slots.get(var7).getStack();
            if (!var8.isEmpty()) {
               if ((itemName.startsWith("minecraft:potion[") || itemName.startsWith("minecraft:splash_potion["))
                  && var8.getItem() instanceof PotionItem) {
                  String var11 = this.getPotionFullKey(var8);
                  if (var11 != null && var11.equals(itemName) && var8.getCount() > var6) {
                     var6 = var8.getCount();
                     var5 = var7;
                  }
               } else if (itemName.startsWith("minecraft:firework_rocket{") && var8.getItem() == Items.FIREWORK_ROCKET) {
                  String var10 = this.getFireworkKey(var8);
                  if (var10 != null && var10.equals(itemName) && var8.getCount() > var6) {
                     var6 = var8.getCount();
                     var5 = var7;
                  }
               } else {
                  String var9 = Registries.ITEM.getId(var8.getItem()).toString();
                  if (var9.equals(itemName) && var8.getCount() > var6) {
                     var6 = var8.getCount();
                     var5 = var7;
                     if (var6 >= 64) {
                        break;
                     }
                  }
               }
            }
         }
      }

      return var5;
   }

   private int findEmptySlotInShulker(ShulkerBoxScreenHandler handler) {
      int var2 = handler.slots.size() == 63 ? 27 : 54;

      for (int var3 = 0; var3 < var2; var3++) {
         if (handler.slots.get(var3).getStack().isEmpty()) {
            return var3;
         }
      }

      return -1;
   }

   private int findMergeableStack(String itemName, ShulkerBoxScreenHandler handler, int targetSlot) {
      int var4 = handler.slots.size() == 63 ? 27 : 54;
      ItemStack var5 = handler.slots.get(targetSlot).getStack();
      if (!var5.isEmpty() && var5.getCount() < var5.getMaxCount()) {
         for (int var6 = 0; var6 < var4; var6++) {
            if (var6 != targetSlot) {
               ItemStack var7 = handler.slots.get(var6).getStack();
               if (!var7.isEmpty() && var7.getItem() == var5.getItem()) {
                  if (var7.getItem() instanceof PotionItem) {
                     boolean var8 = var7.getItem() instanceof SplashPotionItem;
                     boolean var9 = var5.getItem() instanceof SplashPotionItem;
                     if (var8 == var9) {
                        return var6;
                     }
                  } else {
                     String var10 = Registries.ITEM.getId(var7.getItem()).toString();
                     String var11 = Registries.ITEM.getId(var5.getItem()).toString();
                     if (var10.equals(var11)) {
                        return var6;
                     }
                  }
               }
            }
         }

         return -1;
      } else {
         return -1;
      }
   }

   private int getTargetSlot(int playerSlot, ShulkerBoxScreenHandler handler) {
      boolean var3 = handler.slots.size() == 63;
      return playerSlot < 9 ? playerSlot + (var3 ? 54 : 81) : playerSlot + (var3 ? 18 : 45);
   }

   private void performPickupAction(ShulkerBoxScreenHandler handler, int sourceSlot, int targetSlot) {
      mc.interactionManager.clickSlot(handler.syncId, sourceSlot, 0, SlotActionType.PICKUP, mc.player);
      mc.interactionManager.clickSlot(handler.syncId, targetSlot, 0, SlotActionType.PICKUP, mc.player);
      ItemStack var4 = handler.getCursorStack();
      if (!var4.isEmpty()) {
         mc.interactionManager.clickSlot(handler.syncId, sourceSlot, 0, SlotActionType.PICKUP, mc.player);
      }
   }

   private void performMergeAction(ShulkerBoxScreenHandler handler, int sourceSlot, int targetSlot) {
      ItemStack var4 = handler.slots.get(sourceSlot).getStack();
      ItemStack var5 = handler.slots.get(targetSlot).getStack();
      if (!var4.isEmpty() && !var5.isEmpty()) {
         int var6 = var5.getMaxCount() - var5.getCount();
         int var7 = Math.min(var6, var4.getCount());
         if (var7 > 0) {
            if (var4.getCount() == var7) {
               mc.interactionManager.clickSlot(handler.syncId, sourceSlot, 0, SlotActionType.QUICK_MOVE, mc.player);
            } else {
               mc.interactionManager.clickSlot(handler.syncId, sourceSlot, 0, SlotActionType.PICKUP, mc.player);
               mc.interactionManager.clickSlot(handler.syncId, targetSlot, 0, SlotActionType.PICKUP, mc.player);
               ItemStack var8 = handler.getCursorStack();
               if (!var8.isEmpty()) {
                  mc.interactionManager.clickSlot(handler.syncId, sourceSlot, 0, SlotActionType.PICKUP, mc.player);
               }
            }
         }
      }
   }

   private boolean checkAllItemsCorrect(ShulkerBoxScreenHandler handler) {
      for (Entry var3 : this.regearExpectedInv.entrySet()) {
         int var4 = this.getTargetSlot((Integer)var3.getKey(), handler);
         if (var4 >= 0 && var4 < handler.slots.size()) {
            ItemStack var5 = handler.slots.get(var4).getStack();
            String var6 = (String)var3.getValue();
            if (var6.contains("shulker_box")) {
               if (!var5.isEmpty()) {
                  return false;
               }
            } else {
               if (var5.isEmpty()) {
                  return false;
               }

               if (!this.isSlotCorrect((Integer)var3.getKey(), var6, var5)) {
                  return false;
               }
            }
         }
      }

      return true;
   }

   private void doRegear(ShulkerBoxScreenHandler handler) {
      if (this.regearTimer.passed((long)(1000.0 / this.regearSpeed.getValue()))) {
         boolean var2 = true;
         int var3 = 0;
         ArrayList<Entry<Integer, String>> var4 = new ArrayList<>(this.regearExpectedInv.entrySet());
         var4.sort((a, b) -> {
            int var4x = (Integer)a.getKey();
            int var5 = (Integer)b.getKey();
            String var6x = (String)a.getValue();
            String var7x = (String)b.getValue();
            if (var6x.contains("shulker_box") && !var7x.contains("shulker_box")) {
               return 1;
            } else if (!var6x.contains("shulker_box") && var7x.contains("shulker_box")) {
               return -1;
            } else {
               ItemStack var8x = handler.slots.get(this.getTargetSlot(var4x, handler)).getStack();
               ItemStack var9x = handler.slots.get(this.getTargetSlot(var5, handler)).getStack();
               boolean var10x = var8x.isEmpty();
               boolean var11x = var9x.isEmpty();
               boolean var12x = this.isSlotCorrect(var4x, var6x, var8x);
               boolean var13x = this.isSlotCorrect(var5, var7x, var9x);
               if (var10x && !var11x) {
                  return -1;
               } else if (!var10x && var11x) {
                  return 1;
               } else if (!var12x && var13x) {
                  return -1;
               } else {
                  return var12x && !var13x ? 1 : 0;
               }
            }
         });

         for (Entry<Integer, String> var6 : var4) {
            if (var3 >= this.regearClicks.getValueInt()) {
               break;
            }

            int var7 = (Integer)var6.getKey();
            String var8 = (String)var6.getValue();
            int var9 = this.getTargetSlot(var7, handler);
            if (var9 >= 0 && var9 < handler.slots.size()) {
               ItemStack var10 = handler.slots.get(var9).getStack();
               if (var8.contains("shulker_box")) {
                  if (!var10.isEmpty()) {
                     int var11 = this.findEmptySlotInShulker(handler);
                     if (var11 != -1) {
                        mc.interactionManager.clickSlot(handler.syncId, var9, 0, SlotActionType.PICKUP, mc.player);
                        mc.interactionManager.clickSlot(handler.syncId, var11, 0, SlotActionType.PICKUP, mc.player);
                        if (!handler.getCursorStack().isEmpty()) {
                           mc.interactionManager.clickSlot(handler.syncId, var9, 0, SlotActionType.PICKUP, mc.player);
                        }

                        var3++;
                        var2 = false;
                     }
                  }
               } else if (var10.isEmpty()) {
                  int var16 = this.findItemInShulker(var8, handler, var9);
                  if (var16 != -1) {
                     this.performPickupAction(handler, var16, var9);
                     var3++;
                     var2 = false;
                  }
               } else if (!this.isSlotCorrect(var7, var8, var10)) {
                  int var17 = this.findItemInShulker(var8, handler, var9);
                  if (var17 != -1) {
                     mc.interactionManager.clickSlot(handler.syncId, var9, 0, SlotActionType.PICKUP, mc.player);
                     ItemStack var12 = handler.getCursorStack().copy();
                     mc.interactionManager.clickSlot(handler.syncId, var17, 0, SlotActionType.PICKUP, mc.player);
                     mc.interactionManager.clickSlot(handler.syncId, var9, 0, SlotActionType.PICKUP, mc.player);
                     if (!handler.getCursorStack().isEmpty()) {
                        int var13 = this.findCorrectSlotForItem(var12, handler, var9);
                        if (var13 != -1) {
                           int var14 = this.getTargetSlot(var13, handler);
                           if (var14 != -1) {
                              mc.interactionManager.clickSlot(handler.syncId, var14, 0, SlotActionType.PICKUP, mc.player);
                              mc.interactionManager.clickSlot(handler.syncId, var14, 0, SlotActionType.PICKUP, mc.player);
                           } else {
                              int var15 = this.findEmptySlotInShulker(handler);
                              if (var15 != -1) {
                                 mc.interactionManager.clickSlot(handler.syncId, var15, 0, SlotActionType.PICKUP, mc.player);
                              } else {
                                 mc.interactionManager.clickSlot(handler.syncId, var9, 0, SlotActionType.PICKUP, mc.player);
                              }
                           }
                        } else {
                           int var19 = this.findEmptySlotInShulker(handler);
                           if (var19 != -1) {
                              mc.interactionManager.clickSlot(handler.syncId, var19, 0, SlotActionType.PICKUP, mc.player);
                           } else {
                              mc.interactionManager.clickSlot(handler.syncId, var9, 0, SlotActionType.PICKUP, mc.player);
                           }
                        }
                     }

                     var3++;
                     var2 = false;
                  }
               } else if (var10.getCount() < var10.getMaxCount()) {
                  int var18 = this.findMergeableStack(var8, handler, var9);
                  if (var18 != -1) {
                     this.performMergeAction(handler, var18, var9);
                     var3++;
                  }
               }
            }
         }

         this.regearTimer.reset();
         if ((var2 || this.checkAllItemsCorrect(handler)) && this.closeAfterRegear.getValue()) {
            mc.currentScreen.close();
         }
      }
   }

   private int findCorrectSlotForItem(ItemStack item, ShulkerBoxScreenHandler handler, int currentSlot) {
      if (item.isEmpty()) {
         return -1;
      } else {
         String var4 = Registries.ITEM.getId(item.getItem()).toString();

         for (Entry var6 : this.regearExpectedInv.entrySet()) {
            int var7 = (Integer)var6.getKey();
            String var8 = (String)var6.getValue();
            if (var8.equals(var4)) {
               int var13 = this.getTargetSlot(var7, handler);
               if (var13 != currentSlot && var13 >= 0 && var13 < handler.slots.size()) {
                  ItemStack var15 = handler.slots.get(var13).getStack();
                  if (this.needToTakeItem(var15, var8)) {
                     return var7;
                  }
               }
            } else if ((var8.startsWith("minecraft:potion[") || var8.startsWith("minecraft:splash_potion[")) && item.getItem() instanceof PotionItem) {
               String var12 = this.getPotionFullKey(item);
               if (var12 != null && var12.equals(var8)) {
                  int var14 = this.getTargetSlot(var7, handler);
                  if (var14 != currentSlot && var14 >= 0 && var14 < handler.slots.size()) {
                     ItemStack var16 = handler.slots.get(var14).getStack();
                     if (this.needToTakeItem(var16, var8)) {
                        return var7;
                     }
                  }
               }
            } else if (var8.startsWith("minecraft:firework_rocket{") && item.getItem() == Items.FIREWORK_ROCKET) {
               String var9 = this.getFireworkKey(item);
               if (var9 != null && var9.equals(var8)) {
                  int var10 = this.getTargetSlot(var7, handler);
                  if (var10 != currentSlot && var10 >= 0 && var10 < handler.slots.size()) {
                     ItemStack var11 = handler.slots.get(var10).getStack();
                     if (this.needToTakeItem(var11, var8)) {
                        return var7;
                     }
                  }
               }
            }
         }

         return -1;
      }
   }

   public int findShulker() {
      if (this.inventory.getValue()) {
         for (int var1 = 0; var1 < 36; var1++) {
            ItemStack var2 = mc.player.getInventory().getStack(var1);
            if (!var2.isEmpty() && var2.getItem() instanceof BlockItem var4 && var4.getBlock() instanceof ShulkerBoxBlock) {
               return var1 < 9 ? var1 + 36 : var1;
            }
         }

         return -1;
      } else {
         return InventoryUtil.findClass(ShulkerBoxBlock.class);
      }
   }

   @Override
   public void onEnable() {
      this.opend = false;
      this.openPos = null;
      this.timeoutTimer.reset();
      this.placePos = null;
      this.regearSetupDone = false;
      if (!nullCheck() && this.place.getValue()) {
         this.doPlace();
      }
   }

   private void doPlace() {
      int var1 = mc.player.getInventory().selectedSlot;
      double var2 = 100.0;
      BlockPos var4 = null;

      for (BlockPos var6 : BlockUtil.getSphere((float)this.range.getValue())) {
         if (mc.world.isAir(var6) && mc.world.isAir(var6.up())) {
            BlockState var7 = mc.world.getBlockState(var6.down());
            if (this.isValidFloor(var7, var6.down())) {
               double var8 = (double)MathHelper.sqrt((float)mc.player.squaredDistanceTo(var6.toCenterPos()));
               if (!(var8 < this.minRange.getValue()) && !(var8 > this.range.getValue())) {
                  if (this.preferOpen.getValue() && mc.world.getBlockState(var6.down()).getBlock() instanceof ShulkerBoxBlock) {
                     var4 = var6;
                     break;
                  }

                  if (var4 == null || var8 < var2) {
                     var2 = var8;
                     var4 = var6;
                  }
               }
            }
         }
      }

      if (var4 != null) {
         if (this.findShulker() == -1) {
            this.sendMessage("\u00a74No shulkerbox found.");
            return;
         }

         Direction var16 = Direction.UP;
         if (this.inventory.getValue()) {
            int var17 = this.findShulker();
            ItemStack var19 = mc.player.getInventory().getStack(var1 < 9 ? var1 + 36 : var1);
            String var10 = var19.isEmpty() ? "\u7a7a" : var19.getName().getString();
            InventoryUtil.inventorySwap(var17, var1);
            this.placeBlock(var4, var16);
            this.placePos = var4;

            try {
               Thread.sleep(50L);
            } catch (InterruptedException var15) {
               Thread.currentThread().interrupt();
            }

            InventoryUtil.inventorySwap(var17, var1);
            ItemStack var12 = mc.player.getInventory().getStack(var1 < 9 ? var1 + 36 : var1);
            String var13 = var12.isEmpty() ? "\u7a7a" : var12.getName().getString();
            if (!var10.equals(var13)) {
               this.sendMessage("§6[ChestStealer+] §e物品位置已修正: §f" + var10 + " -> " + var13);
            }

            this.sendMessage("§6[ChestStealer+] §a潜影盒已放置 (" + var4.toShortString() + ")");
         } else {
            int var18 = this.findShulker();
            InventoryUtil.switchToSlot(var18);
            this.placeBlock(var4, var16);
            this.placePos = var4;

            try {
               Thread.sleep(50L);
            } catch (InterruptedException var14) {
               Thread.currentThread().interrupt();
            }

            InventoryUtil.switchToSlot(var1);
            ItemStack var20 = mc.player.getInventory().getStack(var1);
            this.sendMessage(
               "§6[ChestStealer+] §a潜影盒已放置 ("
                  + var4.toShortString()
                  + ") 当前手持: "
                  + (var20.isEmpty() ? "\u00a77\u7a7a" : "§f" + var20.getName().getString())
            );
         }

         this.timer.reset();
      } else {
         this.sendMessage("\u00a74No valid place position found.");
      }
   }

   private boolean isValidFloor(BlockState state, BlockPos pos) {
      if (state.isAir()) {
         return false;
      } else {
         return state.getCollisionShape(mc.world, pos).isEmpty() ? false : state.isSolid();
      }
   }

   private void update() {
      this.stealCountList[0] = (int)(this.crystal.getValue() - (double)InventoryUtil.getItemCount(Items.END_CRYSTAL));
      this.stealCountList[1] = (int)(this.exp.getValue() - (double)InventoryUtil.getItemCount(Items.EXPERIENCE_BOTTLE));
      this.stealCountList[2] = (int)(this.totem.getValue() - (double)InventoryUtil.getItemCount(Items.TOTEM_OF_UNDYING));
      this.stealCountList[3] = (int)(this.gapple.getValue() - (double)InventoryUtil.getItemCount(Items.ENCHANTED_GOLDEN_APPLE));
      this.stealCountList[4] = (int)(this.obsidian.getValue() - (double)InventoryUtil.getItemCount(Blocks.OBSIDIAN.asItem()));
      this.stealCountList[5] = (int)(this.web.getValue() - (double)InventoryUtil.getItemCount(Blocks.COBWEB.asItem()));
      this.stealCountList[6] = (int)(this.glowstone.getValue() - (double)InventoryUtil.getItemCount(Blocks.GLOWSTONE.asItem()));
      this.stealCountList[7] = (int)(this.anchor.getValue() - (double)InventoryUtil.getItemCount(Blocks.RESPAWN_ANCHOR.asItem()));
      this.stealCountList[8] = (int)(this.pearl.getValue() - (double)InventoryUtil.getItemCount(Items.ENDER_PEARL));
      this.stealCountList[9] = (int)(
         this.piston.getValue()
            - (double)InventoryUtil.getItemCount(Blocks.PISTON.asItem())
            - (double)InventoryUtil.getItemCount(Blocks.STICKY_PISTON.asItem())
      );
      this.stealCountList[10] = (int)(this.redstone.getValue() - (double)InventoryUtil.getItemCount(Blocks.REDSTONE_BLOCK.asItem()));
      this.stealCountList[11] = (int)(this.bed.getValue() - (double)InventoryUtil.getItemCount(BedBlock.class));
      this.stealCountList[12] = (int)(this.speed.getValue() - (double)InventoryUtil.getPotionCount(StatusEffects.SPEED.value()));
      this.stealCountList[13] = (int)(this.resistance.getValue() - (double)InventoryUtil.getPotionCount(StatusEffects.RESISTANCE.value()));
      this.stealCountList[14] = (int)(this.strength.getValue() - (double)InventoryUtil.getPotionCount(StatusEffects.STRENGTH.value()));
   }

   @Override
   public void onDisable() {
      this.opend = false;
      this.regearSetupDone = false;
      if (this.mine.getValue() && this.placePos != null && PacketMine.INSTANCE != null) {
         PacketMine.INSTANCE.mine(this.placePos);
      }
   }

   @EventListener
   public void onUpdate(UpdateEvent event) {
      if (this.mode.getValue() == ChestStealerPlus.Mode.SMART && this.smart.getValue()) {
         this.update();
      }

      if (mc.world != null) {
         this.openList.removeIf(pos -> !(mc.world.getBlockState(pos).getBlock() instanceof ShulkerBoxBlock));
      }

      if (!(mc.currentScreen instanceof ShulkerBoxScreen)) {
         if (this.opend) {
            this.opend = false;
            if (this.autoDisable.getValue()) {
               this.timeoutToDisable();
            }

            if (this.mine.getValue() && this.openPos != null) {
               if (mc.world.getBlockState(this.openPos).getBlock() instanceof ShulkerBoxBlock) {
                  if (PacketMine.INSTANCE != null) {
                     PacketMine.INSTANCE.mine(this.openPos);
                  }
               } else {
                  this.openPos = null;
               }
            }
         } else if (this.open.getValue()) {
            if (this.placePos == null
               || !((double)MathHelper.sqrt((float)mc.player.squaredDistanceTo(this.placePos.toCenterPos())) <= this.range.getValue())
               || !mc.world.isAir(this.placePos)
               || this.timer.passed(500L) && !(mc.world.getBlockState(this.placePos).getBlock() instanceof ShulkerBoxBlock)) {
               boolean var2 = false;

               for (BlockPos var4 : BlockUtil.getSphere((float)this.range.getValue())) {
                  if (!this.openList.contains(var4)
                     && (mc.world.isAir(var4.up()) || BlockUtil.canReplace(var4.up()))
                     && mc.world.getBlockState(var4).getBlock() instanceof ShulkerBoxBlock) {
                     this.openPos = var4;
                     BlockUtil.clickBlock(var4, BlockUtil.getClickSide(var4), this.rotate.getValue());
                     var2 = true;
                     break;
                  }
               }

               if (!var2 && this.autoDisable.getValue()) {
                  this.timeoutToDisable();
               }
            } else if (mc.world.getBlockState(this.placePos).getBlock() instanceof ShulkerBoxBlock) {
               this.openPos = this.placePos;
               BlockUtil.clickBlock(this.placePos, BlockUtil.getClickSide(this.placePos), this.rotate.getValue());
            }
         } else if (this.mode.getValue() == ChestStealerPlus.Mode.SMART && !this.take.getValue() && this.autoDisable.getValue()) {
            this.timeoutToDisable();
         }
      } else {
         this.opend = true;
         if (this.openPos != null) {
            this.openList.add(this.openPos);
         }

         if (this.mode.getValue() == ChestStealerPlus.Mode.REGEAR) {
            if (mc.player.currentScreenHandler instanceof ShulkerBoxScreenHandler var10) {
               if (!this.regearSetupDone) {
                  this.loadCurrentKit();
                  this.regearSetupDone = true;
               }

               if (this.regearSetupDone && !this.regearExpectedInv.isEmpty()) {
                  this.doRegear(var10);
               } else {
                  String var13 = this.getCurrentKitName();
                  if (!var13.equals("none")) {
                     this.sendMessage(Formatting.RED + "Failed to load kit: " + var13);
                  } else {
                     this.sendMessage(Formatting.RED + "No kit selected!");
                  }
               }

               if (this.autoDisable.getValue() && this.closeAfterRegear.getValue()) {
                  this.timeoutToDisable();
               }
            }
         } else if (this.mode.getValue() == ChestStealerPlus.Mode.SMART) {
            if (!this.take.getValue()) {
               if (this.autoDisable.getValue()) {
                  this.timeoutToDisable();
               }
            } else {
               boolean var11 = false;
               if (mc.player.currentScreenHandler instanceof ShulkerBoxScreenHandler var14) {
                  for (Slot var5 : var14.slots) {
                     if (var5.id < 27 && !var5.getStack().isEmpty()) {
                        ChestStealerPlus.Type var6 = this.needSteal(var5.getStack());
                        if (!this.smart.getValue()
                           || var6 == ChestStealerPlus.Type.QuickMove
                           || var6 == ChestStealerPlus.Type.Stack && this.forceMove.getValue()) {
                           mc.interactionManager.clickSlot(var14.syncId, var5.id, 0, SlotActionType.QUICK_MOVE, mc.player);
                           var11 = true;
                        } else if (var6 == ChestStealerPlus.Type.Stack) {
                           for (int var7 = 0; var7 < 36; var7++) {
                              ItemStack var8 = mc.player.getInventory().getStack(var7);
                              if (!var8.isEmpty() && var8.isStackable() && var8.getItem() == var5.getStack().getItem() && var8.getCount() < var8.getMaxCount()) {
                                 int var9 = (var7 < 9 ? var7 + 36 : var7) + 18;
                                 mc.interactionManager.clickSlot(var14.syncId, var5.id, 0, SlotActionType.PICKUP, mc.player);
                                 mc.interactionManager.clickSlot(var14.syncId, var9, 0, SlotActionType.PICKUP, mc.player);
                                 mc.interactionManager.clickSlot(var14.syncId, var5.id, 0, SlotActionType.PICKUP, mc.player);
                                 var11 = true;
                                 break;
                              }
                           }
                        }
                     }
                  }
               }

               if (this.autoDisable.getValue() && !var11) {
                  this.timeoutToDisable();
               }
            }
         }
      }
   }

   private void timeoutToDisable() {
      if (this.timeoutTimer.passedMs(this.disableTime.getValue())) {
         this.disable();
      }
   }

   private ChestStealerPlus.Type needSteal(ItemStack i) {
      if (i.getItem().equals(Items.END_CRYSTAL) && this.stealCountList[0] > 0) {
         this.stealCountList[0] = this.stealCountList[0] - i.getCount();
         return this.stealCountList[0] < 0 ? ChestStealerPlus.Type.Stack : ChestStealerPlus.Type.QuickMove;
      } else if (i.getItem().equals(Items.EXPERIENCE_BOTTLE) && this.stealCountList[1] > 0) {
         this.stealCountList[1] = this.stealCountList[1] - i.getCount();
         return this.stealCountList[1] < 0 ? ChestStealerPlus.Type.Stack : ChestStealerPlus.Type.QuickMove;
      } else if (i.getItem().equals(Items.TOTEM_OF_UNDYING) && this.stealCountList[2] > 0) {
         this.stealCountList[2] = this.stealCountList[2] - i.getCount();
         return this.stealCountList[2] < 0 ? ChestStealerPlus.Type.Stack : ChestStealerPlus.Type.QuickMove;
      } else if (i.getItem().equals(Items.ENCHANTED_GOLDEN_APPLE) && this.stealCountList[3] > 0) {
         this.stealCountList[3] = this.stealCountList[3] - i.getCount();
         return this.stealCountList[3] < 0 ? ChestStealerPlus.Type.Stack : ChestStealerPlus.Type.QuickMove;
      } else if (i.getItem().equals(Blocks.OBSIDIAN.asItem()) && this.stealCountList[4] > 0) {
         this.stealCountList[4] = this.stealCountList[4] - i.getCount();
         return this.stealCountList[4] < 0 ? ChestStealerPlus.Type.Stack : ChestStealerPlus.Type.QuickMove;
      } else if (i.getItem().equals(Blocks.COBWEB.asItem()) && this.stealCountList[5] > 0) {
         this.stealCountList[5] = this.stealCountList[5] - i.getCount();
         return this.stealCountList[5] < 0 ? ChestStealerPlus.Type.Stack : ChestStealerPlus.Type.QuickMove;
      } else if (i.getItem().equals(Blocks.GLOWSTONE.asItem()) && this.stealCountList[6] > 0) {
         this.stealCountList[6] = this.stealCountList[6] - i.getCount();
         return this.stealCountList[6] < 0 ? ChestStealerPlus.Type.Stack : ChestStealerPlus.Type.QuickMove;
      } else if (i.getItem().equals(Blocks.RESPAWN_ANCHOR.asItem()) && this.stealCountList[7] > 0) {
         this.stealCountList[7] = this.stealCountList[7] - i.getCount();
         return this.stealCountList[7] < 0 ? ChestStealerPlus.Type.Stack : ChestStealerPlus.Type.QuickMove;
      } else if (i.getItem().equals(Items.ENDER_PEARL) && this.stealCountList[8] > 0) {
         this.stealCountList[8] = this.stealCountList[8] - i.getCount();
         return this.stealCountList[8] < 0 ? ChestStealerPlus.Type.Stack : ChestStealerPlus.Type.QuickMove;
      } else if (i.getItem() instanceof BlockItem && ((BlockItem)i.getItem()).getBlock() instanceof PistonBlock && this.stealCountList[9] > 0) {
         this.stealCountList[9] = this.stealCountList[9] - i.getCount();
         return this.stealCountList[9] < 0 ? ChestStealerPlus.Type.Stack : ChestStealerPlus.Type.QuickMove;
      } else if (i.getItem().equals(Blocks.REDSTONE_BLOCK.asItem()) && this.stealCountList[10] > 0) {
         this.stealCountList[10] = this.stealCountList[10] - i.getCount();
         return this.stealCountList[10] < 0 ? ChestStealerPlus.Type.Stack : ChestStealerPlus.Type.QuickMove;
      } else if (i.getItem() instanceof BlockItem && ((BlockItem)i.getItem()).getBlock() instanceof BedBlock && this.stealCountList[11] > 0) {
         this.stealCountList[11] = this.stealCountList[11] - i.getCount();
         return this.stealCountList[11] < 0 ? ChestStealerPlus.Type.Stack : ChestStealerPlus.Type.QuickMove;
      } else {
         if (Item.getRawId(i.getItem()) == Item.getRawId(Items.SPLASH_POTION)) {
            PotionContentsComponent var2 = i.getOrDefault(DataComponentTypes.POTION_CONTENTS, PotionContentsComponent.DEFAULT);

            for (StatusEffectInstance var4 : var2.getEffects()) {
               if (var4.getEffectType().value() == StatusEffects.SPEED.value()) {
                  if (this.stealCountList[12] > 0) {
                     this.stealCountList[12] = this.stealCountList[12] - i.getCount();
                     if (this.stealCountList[12] < 0) {
                        return ChestStealerPlus.Type.Stack;
                     }

                     return ChestStealerPlus.Type.QuickMove;
                  }
               } else if (var4.getEffectType().value() == StatusEffects.RESISTANCE.value()) {
                  if (this.stealCountList[13] > 0) {
                     this.stealCountList[13] = this.stealCountList[13] - i.getCount();
                     if (this.stealCountList[13] < 0) {
                        return ChestStealerPlus.Type.Stack;
                     }

                     return ChestStealerPlus.Type.QuickMove;
                  }
               } else if (var4.getEffectType().value() == StatusEffects.STRENGTH.value() && this.stealCountList[14] > 0) {
                  this.stealCountList[14] = this.stealCountList[14] - i.getCount();
                  if (this.stealCountList[14] < 0) {
                     return ChestStealerPlus.Type.Stack;
                  }

                  return ChestStealerPlus.Type.QuickMove;
               }
            }
         }

         return ChestStealerPlus.Type.None;
      }
   }

   private void placeBlock(BlockPos pos, Direction side) {
      BlockPos var3;
      Direction var4;
      if (side == Direction.UP) {
         var3 = pos.down();
         var4 = Direction.UP;
      } else {
         var3 = pos.offset(side);
         var4 = side.getOpposite();
      }

      BlockUtil.clickBlock(var3, var4, this.rotate.getValue());
   }
@Environment(EnvType.CLIENT)
   public static enum Mode {
      SMART,
      REGEAR;

}

   @Environment(EnvType.CLIENT)
   private static enum Type {
      None,
      Stack,
      QuickMove;

}
}
