/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  meteordevelopment.orbit.EventHandler
 *  net.minecraft.class_1109
 *  net.minecraft.class_1113
 *  net.minecraft.class_1297
 *  net.minecraft.class_1506
 *  net.minecraft.class_1604
 *  net.minecraft.class_1792
 *  net.minecraft.class_1802
 *  net.minecraft.class_1923
 *  net.minecraft.class_2246
 *  net.minecraft.class_2338
 *  net.minecraft.class_2350$class_2351
 *  net.minecraft.class_243
 *  net.minecraft.class_2680
 *  net.minecraft.class_2741
 *  net.minecraft.class_2769
 *  net.minecraft.class_2791
 *  net.minecraft.class_2818
 *  net.minecraft.class_3414
 *  net.minecraft.class_3417
 *  net.minecraft.class_368
 *  net.minecraft.class_3986
 *  net.minecraft.class_3989
 *  net.minecraft.class_4481
 */
package meteordevelopment.meteorclient.systems.modules.donut;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import meteordevelopment.meteorclient.events.render.Render3DEvent;
import meteordevelopment.meteorclient.events.world.BlockUpdateEvent;
import meteordevelopment.meteorclient.events.world.ChunkDataEvent;
import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.renderer.ShapeMode;
import meteordevelopment.meteorclient.settings.BoolSetting;
import meteordevelopment.meteorclient.settings.IntSetting;
import meteordevelopment.meteorclient.settings.Setting;
import meteordevelopment.meteorclient.settings.SettingGroup;
import meteordevelopment.meteorclient.systems.modules.Categories;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.utils.Utils;
import meteordevelopment.meteorclient.utils.render.MeteorToast;
import meteordevelopment.meteorclient.utils.render.RenderUtils;
import meteordevelopment.meteorclient.utils.render.color.Color;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.class_1109;
import net.minecraft.class_1113;
import net.minecraft.class_1297;
import net.minecraft.class_1506;
import net.minecraft.class_1604;
import net.minecraft.class_1792;
import net.minecraft.class_1802;
import net.minecraft.class_1923;
import net.minecraft.class_2246;
import net.minecraft.class_2338;
import net.minecraft.class_2350;
import net.minecraft.class_243;
import net.minecraft.class_2680;
import net.minecraft.class_2741;
import net.minecraft.class_2769;
import net.minecraft.class_2791;
import net.minecraft.class_2818;
import net.minecraft.class_3414;
import net.minecraft.class_3417;
import net.minecraft.class_368;
import net.minecraft.class_3986;
import net.minecraft.class_3989;
import net.minecraft.class_4481;

public class SusEsp
extends Module {
    private static final int BEE_SCAN_EVERY_TICKS = 20;
    private static final int ENTITY_SCAN_EVERY_TICKS = 10;
    private static final int ROTATED_BG_SCAN_EVERY_TICKS = 2;
    private static final int ROTATED_CHUNKS_PER_BG_TICK = 2;
    private static final long ROTATED_RESCAN_COOLDOWN_MS = 2500L;
    private static final int ROT_MIN_Y = -64;
    private static final int ROT_MAX_Y = 320;
    private final SettingGroup sgGeneral;
    private final Setting<Boolean> beeHives;
    private final Setting<Boolean> wanderingESP;
    private final Setting<Boolean> llamaESP;
    private final Setting<Boolean> pillagerESP;
    private final Setting<Boolean> skeletonHorseESP;
    private final Setting<Boolean> fullKelpChunk;
    private final Setting<Boolean> tracers;
    private final Setting<Boolean> rotatedDeepslate;
    private final Setting<Boolean> rotatedTracer;
    private final Setting<Integer> rotatedAlpha;
    private final Setting<Integer> alpha;
    private final Set<class_2338> detectedBeeHives;
    private final Set<class_2338> notifiedBeeHives;
    private final Set<UUID> detectedTraders;
    private final Set<UUID> detectedLlamas;
    private final Set<UUID> detectedPillagers;
    private final Set<UUID> detectedSkeletonHorses;
    private final Set<UUID> notifiedTraders;
    private final Set<UUID> notifiedLlamas;
    private final Set<UUID> notifiedPillagers;
    private final Set<UUID> notifiedSkeletonHorses;
    private final Set<class_1923> detectedKelpChunks;
    private final Set<class_1923> notifiedKelpChunks;
    private final Set<class_2338> rotatedPositions;
    private final Set<Long> scanningChunks;
    private final ConcurrentHashMap<Long, Long> lastChunkScanMs;
    private ExecutorService scanPool;
    private int tickCounter;

    public SusEsp() {
        super(Categories.Donut, "Sus esp", "Detects bee hives, wandering entities, kelp chunks and rotated deepslate.");
        this.sgGeneral = this.settings.getDefaultGroup();
        this.beeHives = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("bee-hives")).defaultValue(true)).build());
        this.wanderingESP = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("wandering-esp")).defaultValue(true)).build());
        this.llamaESP = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("llama-esp")).defaultValue(true)).build());
        this.pillagerESP = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("pillager-esp")).defaultValue(true)).build());
        this.skeletonHorseESP = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("skeleton-horse-esp")).defaultValue(true)).build());
        this.fullKelpChunk = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("kelp")).defaultValue(true)).build());
        this.tracers = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("tracers")).defaultValue(true)).build());
        this.rotatedDeepslate = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("rotated-deepslate")).defaultValue(true)).build());
        this.rotatedTracer = this.sgGeneral.add(((BoolSetting.Builder)((BoolSetting.Builder)new BoolSetting.Builder().name("rotated-tracer")).defaultValue(false)).build());
        this.rotatedAlpha = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("rotated-alpha")).defaultValue(60)).range(0, 255).sliderRange(0, 255).build());
        this.alpha = this.sgGeneral.add(((IntSetting.Builder)((IntSetting.Builder)new IntSetting.Builder().name("alpha")).defaultValue(140)).range(0, 255).sliderRange(0, 255).build());
        this.detectedBeeHives = ConcurrentHashMap.newKeySet();
        this.notifiedBeeHives = ConcurrentHashMap.newKeySet();
        this.detectedTraders = ConcurrentHashMap.newKeySet();
        this.detectedLlamas = ConcurrentHashMap.newKeySet();
        this.detectedPillagers = ConcurrentHashMap.newKeySet();
        this.detectedSkeletonHorses = ConcurrentHashMap.newKeySet();
        this.notifiedTraders = ConcurrentHashMap.newKeySet();
        this.notifiedLlamas = ConcurrentHashMap.newKeySet();
        this.notifiedPillagers = ConcurrentHashMap.newKeySet();
        this.notifiedSkeletonHorses = ConcurrentHashMap.newKeySet();
        this.detectedKelpChunks = ConcurrentHashMap.newKeySet();
        this.notifiedKelpChunks = ConcurrentHashMap.newKeySet();
        this.rotatedPositions = ConcurrentHashMap.newKeySet();
        this.scanningChunks = ConcurrentHashMap.newKeySet();
        this.lastChunkScanMs = new ConcurrentHashMap();
        this.tickCounter = 0;
    }

    @Override
    public void onActivate() {
        if (this.mc.field_1687 == null) {
            return;
        }
        this.detectedBeeHives.clear();
        this.detectedTraders.clear();
        this.detectedLlamas.clear();
        this.detectedPillagers.clear();
        this.detectedSkeletonHorses.clear();
        this.notifiedBeeHives.clear();
        this.notifiedTraders.clear();
        this.notifiedLlamas.clear();
        this.notifiedPillagers.clear();
        this.notifiedSkeletonHorses.clear();
        this.rotatedPositions.clear();
        this.scanningChunks.clear();
        this.lastChunkScanMs.clear();
        this.detectedKelpChunks.clear();
        this.notifiedKelpChunks.clear();
        this.tickCounter = 0;
        if (this.scanPool != null && !this.scanPool.isShutdown()) {
            this.scanPool.shutdownNow();
        }
        this.scanPool = Executors.newFixedThreadPool(2);
        if (this.mc.field_1724 != null && this.rotatedDeepslate.get().booleanValue()) {
            this.scanAllLoadedChunksForRotated();
        }
    }

    @Override
    public void onDeactivate() {
        this.detectedBeeHives.clear();
        this.detectedTraders.clear();
        this.detectedLlamas.clear();
        this.detectedPillagers.clear();
        this.detectedSkeletonHorses.clear();
        this.rotatedPositions.clear();
        this.scanningChunks.clear();
        this.lastChunkScanMs.clear();
        if (this.scanPool != null && !this.scanPool.isShutdown()) {
            this.scanPool.shutdownNow();
        }
        this.scanPool = null;
    }

    @EventHandler
    private void onTick(TickEvent.Post event) {
        if (this.mc.field_1687 == null || this.mc.field_1724 == null) {
            return;
        }
        ++this.tickCounter;
        if (this.beeHives.get().booleanValue() && this.tickCounter % 20 == 0) {
            this.scanForBeeHives();
        }
        if (this.tickCounter % 10 == 0) {
            this.scanForEntities();
        }
        if (this.rotatedDeepslate.get().booleanValue() && this.tickCounter % 2 == 0) {
            this.backgroundScanRotatedDeepslate();
            this.pruneRotatedByChunkRange();
        }
        if (this.fullKelpChunk.get().booleanValue() && this.tickCounter % 20 == 0) {
            this.scanForKelpChunks();
        }
    }

    @EventHandler
    private void onBlockUpdate(BlockUpdateEvent event) {
        if (!this.rotatedDeepslate.get().booleanValue() || this.mc.field_1687 == null) {
            return;
        }
        int y = event.pos.method_10264();
        if (y < -64 || y > 320) {
            this.rotatedPositions.remove(event.pos);
            return;
        }
        if (this.isRotatedDeepslate(event.newState)) {
            this.rotatedPositions.add(event.pos.method_10062());
        } else {
            this.rotatedPositions.remove(event.pos);
        }
    }

    @EventHandler
    private void onChunkData(ChunkDataEvent event) {
        if (!this.rotatedDeepslate.get().booleanValue() || this.mc.field_1687 == null || this.mc.field_1724 == null) {
            return;
        }
        int cx = event.chunk().method_12004().field_9181;
        int cz = event.chunk().method_12004().field_9180;
        class_1923 pc = new class_1923(this.mc.field_1724.method_24515());
        int vd = this.getViewDistanceChunks();
        if (Math.abs(cx - pc.field_9181) > vd || Math.abs(cz - pc.field_9180) > vd) {
            return;
        }
        this.scanChunkAsync(event.chunk());
    }

    private void scanForBeeHives() {
        if (this.mc.field_1687 == null) {
            return;
        }
        HashSet<class_2338> current = new HashSet<class_2338>();
        for (class_2818 chunk : this.getLoadedWorldChunks()) {
            for (class_2338 pos : chunk.method_12021()) {
                class_2680 state = this.mc.field_1687.method_8320(pos);
                if (!state.method_27852(class_2246.field_20421) && !state.method_27852(class_2246.field_20422)) continue;
                try {
                    if ((Integer)state.method_11654((class_2769)class_4481.field_20420) != 5) {
                    }
                }
                catch (Throwable t) {}
                continue;
                class_2338 p = pos.method_10062();
                current.add(p);
                if (!this.notifiedBeeHives.add(p)) continue;
                this.mc.execute(() -> {
                    this.mc.method_1566().method_1999((class_368)new MeteorToast.Builder("Beehive ESP").icon(class_1802.field_20416).text("Found full beehive!").build());
                    this.mc.method_1483().method_4873((class_1113)class_1109.method_4757((class_3414)class_3417.field_14627, (float)0.7f, (float)0.8f));
                });
            }
        }
        this.detectedBeeHives.clear();
        this.detectedBeeHives.addAll(current);
    }

    private void scanForEntities() {
        if (this.mc.field_1687 == null) {
            return;
        }
        HashSet<UUID> traders = new HashSet<UUID>();
        HashSet<UUID> llamas = new HashSet<UUID>();
        HashSet<UUID> pillagers = new HashSet<UUID>();
        HashSet<UUID> skeletons = new HashSet<UUID>();
        for (class_1297 entity : this.mc.field_1687.method_18112()) {
            if (entity == null) continue;
            UUID uuid = entity.method_5667();
            if (this.wanderingESP.get().booleanValue() && entity instanceof class_3989) {
                traders.add(uuid);
                if (this.notifiedTraders.add(uuid)) {
                    this.notifyEntity("Wandering ESP", "Found wandering trader!", class_1802.field_8687);
                }
            }
            if (this.llamaESP.get().booleanValue() && entity instanceof class_3986) {
                llamas.add(uuid);
                if (this.notifiedLlamas.add(uuid)) {
                    this.notifyEntity("Llama ESP", "Found trader llama!", class_1802.field_8687);
                }
            }
            if (this.pillagerESP.get().booleanValue() && entity instanceof class_1604) {
                pillagers.add(uuid);
                if (this.notifiedPillagers.add(uuid)) {
                    this.notifyEntity("Pillager ESP", "Found pillager!", class_1802.field_8399);
                }
            }
            if (!this.skeletonHorseESP.get().booleanValue() || !(entity instanceof class_1506)) continue;
            skeletons.add(uuid);
            if (!this.notifiedSkeletonHorses.add(uuid)) continue;
            this.notifyEntity("Skeleton Horse ESP", "Found skeleton horse!", class_1802.field_8398);
        }
        this.detectedTraders.clear();
        this.detectedTraders.addAll(traders);
        this.detectedLlamas.clear();
        this.detectedLlamas.addAll(llamas);
        this.detectedPillagers.clear();
        this.detectedPillagers.addAll(pillagers);
        this.detectedSkeletonHorses.clear();
        this.detectedSkeletonHorses.addAll(skeletons);
    }

    private void notifyEntity(String title, String text, class_1792 icon) {
        this.mc.execute(() -> {
            this.mc.method_1566().method_1999((class_368)new MeteorToast.Builder(title).icon(icon).text(text).build());
            this.mc.method_1483().method_4873((class_1113)class_1109.method_4757((class_3414)class_3417.field_14627, (float)0.7f, (float)0.8f));
        });
    }

    @EventHandler
    private void onRender3D(Render3DEvent event) {
        if (this.mc.field_1687 == null || this.mc.field_1724 == null) {
            return;
        }
        int aDefault = this.alpha.get();
        int aRot = this.rotatedAlpha.get();
        float tickDelta = event.tickDelta;
        if (this.beeHives.get().booleanValue() && !this.detectedBeeHives.isEmpty()) {
            this.renderBlocks(event, this.detectedBeeHives, new Color(255, 255, 0, aDefault), this.tracers.get());
        }
        if (this.rotatedDeepslate.get().booleanValue() && !this.rotatedPositions.isEmpty()) {
            this.renderRotated(event, new Color(255, 0, 255, aRot), this.rotatedTracer.get());
        }
        if (this.wanderingESP.get().booleanValue()) {
            this.renderEntities(event, this.detectedTraders, new Color(0, 150, 255, aDefault), 0.3, 2.0, tickDelta);
        }
        if (this.llamaESP.get().booleanValue()) {
            this.renderEntities(event, this.detectedLlamas, new Color(0, 150, 255, aDefault), 0.4, 1.8, tickDelta);
        }
        if (this.pillagerESP.get().booleanValue()) {
            this.renderEntities(event, this.detectedPillagers, new Color(255, 0, 0, aDefault), 0.3, 2.0, tickDelta);
        }
        if (this.skeletonHorseESP.get().booleanValue()) {
            this.renderEntities(event, this.detectedSkeletonHorses, new Color(200, 200, 200, aDefault), 0.4, 1.8, tickDelta);
        }
        if (this.fullKelpChunk.get().booleanValue() && !this.detectedKelpChunks.isEmpty()) {
            Color c = new Color(0, 0, 255, 120);
            int vd = this.getViewDistanceChunks();
            class_1923 pc = new class_1923(this.mc.field_1724.method_24515());
            for (class_1923 cp : this.detectedKelpChunks) {
                if (Math.abs(cp.field_9181 - pc.field_9181) > vd || Math.abs(cp.field_9180 - pc.field_9180) > vd) continue;
                event.renderer.box(cp.method_8326(), 45.0, cp.method_8328(), (double)cp.method_8326() + 16.0, 45.5, (double)cp.method_8328() + 16.0, c, new Color(c.r, c.g, c.b, 255), ShapeMode.Both, 0);
            }
        }
    }

    private void renderBlocks(Render3DEvent event, Set<class_2338> positions, Color fill, boolean tracer) {
        Color line = new Color(fill.r, fill.g, fill.b, 255);
        for (class_2338 pos : positions) {
            event.renderer.box(pos, fill, line, ShapeMode.Both, 0);
            if (!tracer) continue;
            event.renderer.line(RenderUtils.center.field_1352, RenderUtils.center.field_1351, RenderUtils.center.field_1350, (double)pos.method_10263() + 0.5, (double)pos.method_10264() + 0.5, (double)pos.method_10260() + 0.5, line);
        }
    }

    private void renderRotated(Render3DEvent event, Color fill, boolean tracer) {
        Color line = new Color(255, 0, 255, 255);
        int vd = this.getViewDistanceChunks();
        double renderDistSq = Math.max(16.0, (double)vd * 16.0);
        renderDistSq *= renderDistSq;
        double px = this.mc.field_1724.method_23317();
        double py = this.mc.field_1724.method_23318();
        double pz = this.mc.field_1724.method_23321();
        int rendered = 0;
        for (class_2338 pos : this.rotatedPositions) {
            double dz;
            double dy;
            if (rendered++ >= 900) break;
            double dx = (double)pos.method_10263() + 0.5 - px;
            if (dx * dx + (dy = (double)pos.method_10264() + 0.5 - py) * dy + (dz = (double)pos.method_10260() + 0.5 - pz) * dz > renderDistSq) continue;
            event.renderer.box(pos, fill, line, ShapeMode.Both, 0);
            if (!tracer) continue;
            event.renderer.line(RenderUtils.center.field_1352, RenderUtils.center.field_1351, RenderUtils.center.field_1350, (double)pos.method_10263() + 0.5, (double)pos.method_10264() + 0.5, (double)pos.method_10260() + 0.5, line);
        }
    }

    private void renderEntities(Render3DEvent event, Set<UUID> uuids, Color fill, double radius, double height, float tickDelta) {
        Color line = new Color(fill.r, fill.g, fill.b, 255);
        for (UUID uuid : uuids) {
            class_1297 entity = this.findEntityByUuid(uuid);
            if (entity == null) continue;
            class_243 ePos = entity.method_30950(tickDelta);
            double x = ePos.field_1352;
            double y = ePos.field_1351;
            double z = ePos.field_1350;
            event.renderer.box(x - radius, y, z - radius, x + radius, y + height, z + radius, fill, line, ShapeMode.Both, 0);
            if (!this.tracers.get().booleanValue()) continue;
            event.renderer.line(RenderUtils.center.field_1352, RenderUtils.center.field_1351, RenderUtils.center.field_1350, x, y + height / 2.0, z, line);
        }
    }

    private void scanAllLoadedChunksForRotated() {
        if (this.mc.field_1687 == null || this.mc.field_1724 == null || this.scanPool == null || this.scanPool.isShutdown()) {
            return;
        }
        int vd = this.getViewDistanceChunks();
        class_1923 pc = new class_1923(this.mc.field_1724.method_24515());
        long now = System.currentTimeMillis();
        int submitted = 0;
        for (class_2818 chunk : this.getLoadedWorldChunks()) {
            class_1923 cp = chunk.method_12004();
            if (Math.abs(cp.field_9181 - pc.field_9181) > vd || Math.abs(cp.field_9180 - pc.field_9180) > vd) continue;
            this.scanChunkAsync(chunk);
            this.lastChunkScanMs.put(cp.method_8324(), now);
            if (++submitted < 400) continue;
            break;
        }
    }

    private void backgroundScanRotatedDeepslate() {
        if (this.mc.field_1687 == null || this.mc.field_1724 == null || this.scanPool == null || this.scanPool.isShutdown()) {
            return;
        }
        int vd = this.getViewDistanceChunks();
        class_1923 pc = new class_1923(this.mc.field_1724.method_24515());
        long now = System.currentTimeMillis();
        int submitted = 0;
        for (class_2818 chunk : this.getLoadedWorldChunks()) {
            long key;
            long last;
            if (submitted >= 2) break;
            class_1923 cp = chunk.method_12004();
            if (Math.abs(cp.field_9181 - pc.field_9181) > vd || Math.abs(cp.field_9180 - pc.field_9180) > vd || (last = this.lastChunkScanMs.getOrDefault(key = cp.method_8324(), 0L).longValue()) != 0L && now - last < 2500L) continue;
            this.scanChunkAsync(chunk);
            this.lastChunkScanMs.put(key, now);
            ++submitted;
        }
    }

    private void scanChunkAsync(class_2818 chunk) {
        if (chunk == null || this.scanPool == null || this.scanPool.isShutdown()) {
            return;
        }
        long key = chunk.method_12004().method_8324();
        if (!this.scanningChunks.add(key)) {
            return;
        }
        this.scanPool.submit(() -> {
            try {
                this.scanChunkForRotatedDeepslate(chunk);
            }
            catch (Throwable throwable) {
            }
            finally {
                this.scanningChunks.remove(key);
            }
        });
    }

    private void scanChunkForRotatedDeepslate(class_2818 chunk) {
        if (this.mc.field_1687 == null) {
            return;
        }
        class_1923 cp = chunk.method_12004();
        int startX = cp.method_8326();
        int startZ = cp.method_8328();
        int yMin = Math.max(-64, this.mc.field_1687.method_31607());
        int yMax = Math.min(320, this.mc.field_1687.method_31600());
        if (yMax < yMin) {
            return;
        }
        for (int x = 0; x < 16; ++x) {
            for (int z = 0; z < 16; ++z) {
                if (this.mc.field_1687 == null) {
                    return;
                }
                for (int y = yMin; y <= yMax; ++y) {
                    class_2338 pos = new class_2338(startX + x, y, startZ + z);
                    if (!this.isRotatedDeepslate(chunk.method_8320(pos))) continue;
                    this.rotatedPositions.add(pos.method_10062());
                }
            }
        }
    }

    private void pruneRotatedByChunkRange() {
        if (this.mc.field_1724 == null) {
            return;
        }
        int vd = this.getViewDistanceChunks() + 1;
        class_1923 pc = new class_1923(this.mc.field_1724.method_24515());
        Iterator<class_2338> it = this.rotatedPositions.iterator();
        while (it.hasNext()) {
            class_2338 pos = it.next();
            class_1923 cp = new class_1923(pos);
            if (Math.abs(cp.field_9181 - pc.field_9181) <= vd && Math.abs(cp.field_9180 - pc.field_9180) <= vd) continue;
            it.remove();
        }
    }

    private int getViewDistanceChunks() {
        int vd = 8;
        try {
            vd = (Integer)this.mc.field_1690.method_42503().method_41753();
        }
        catch (Throwable throwable) {
            // empty catch block
        }
        return Math.max(2, vd);
    }

    private boolean isRotatedDeepslate(class_2680 state) {
        boolean isType;
        if (state == null || state.method_26215()) {
            return false;
        }
        boolean bl = isType = state.method_27852(class_2246.field_28888) || state.method_27852(class_2246.field_28900) || state.method_27852(class_2246.field_28896);
        if (!isType) {
            return false;
        }
        if (state.method_28498((class_2769)class_2741.field_12496)) {
            return state.method_11654((class_2769)class_2741.field_12496) != class_2350.class_2351.field_11052;
        }
        return state.method_28498((class_2769)class_2741.field_12529);
    }

    private class_1297 findEntityByUuid(UUID uuid) {
        if (this.mc.field_1687 == null) {
            return null;
        }
        for (class_1297 e : this.mc.field_1687.method_18112()) {
            if (e == null || !e.method_5667().equals(uuid)) continue;
            return e;
        }
        return null;
    }

    private void scanForKelpChunks() {
        if (this.mc.field_1687 == null || this.scanPool == null || this.scanPool.isShutdown()) {
            return;
        }
        this.scanPool.submit(() -> {
            for (class_2818 chunk : this.getLoadedWorldChunks()) {
                if (this.checkKelp(chunk)) {
                    class_1923 cp = chunk.method_12004();
                    this.detectedKelpChunks.add(cp);
                    this.notifiedKelpChunks.add(cp);
                    continue;
                }
                this.detectedKelpChunks.remove(chunk.method_12004());
            }
        });
    }

    private boolean checkKelp(class_2818 chunk) {
        class_1923 cp = chunk.method_12004();
        int startX = cp.method_8326();
        int startZ = cp.method_8328();
        int kelpPlantsFound = 0;
        int fullKelpPlants = 0;
        HashSet<class_2338> processed = new HashSet<class_2338>();
        for (int x = 0; x < 16; ++x) {
            for (int z = 0; z < 16; ++z) {
                for (int y = -64; y < 320; ++y) {
                    class_2338 pos = new class_2338(startX + x, y, startZ + z);
                    class_2680 state = chunk.method_8320(pos);
                    if (!state.method_27852(class_2246.field_9993) && !state.method_27852(class_2246.field_10463) || processed.contains(pos)) continue;
                    class_2338 cur = pos;
                    while (chunk.method_8320(cur.method_10074()).method_27852(class_2246.field_9993) || chunk.method_8320(cur.method_10074()).method_27852(class_2246.field_10463)) {
                        cur = cur.method_10074();
                    }
                    class_2338 base = cur;
                    if (processed.contains(base)) continue;
                    processed.add(base);
                    class_2338 it = base;
                    class_2338 top = base;
                    while (it.method_10264() <= 320 && (chunk.method_8320(it).method_27852(class_2246.field_9993) || chunk.method_8320(it).method_27852(class_2246.field_10463))) {
                        top = it;
                        it = it.method_10084();
                    }
                    ++kelpPlantsFound;
                    if (chunk.method_8320(top.method_10084()).method_26215()) {
                        ++fullKelpPlants;
                    }
                    y = top.method_10264();
                }
            }
        }
        if (kelpPlantsFound < 7) {
            return false;
        }
        return kelpPlantsFound - fullKelpPlants <= 1;
    }

    private Iterable<class_2818> getLoadedWorldChunks() {
        HashSet<class_2818> chunks = new HashSet<class_2818>();
        for (class_2791 c : Utils.chunks()) {
            if (!(c instanceof class_2818)) continue;
            class_2818 wc = (class_2818)c;
            chunks.add(wc);
        }
        return chunks;
    }
}

