| 1 | /*
|
| 2 | * This file is part of the Combatant Client distribution.
|
| 3 | * Copyright (c) 2026 pivosos2007.
|
| 4 | *
|
| 5 | * Licensed under the GNU General Public License v3.0.
|
| 6 | */
|
| 7 |
|
| 8 | package combatant.client.features.module.modules.visuals;
|
| 9 |
|
| 10 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 11 | import combatant.client.config.values.*;
|
| 12 | import combatant.client.features.module.*;
|
| 13 | import combatant.client.features.module.Module;
|
| 14 | import combatant.client.util.block.*;
|
| 15 | import net.minecraft.client.Minecraft;
|
| 16 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 17 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 18 | import net.minecraft.core.BlockPos;
|
| 19 | import net.minecraft.core.SectionPos;
|
| 20 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 21 | import net.minecraft.world.level.block.state.BlockState;
|
| 22 | import net.minecraft.world.phys.AABB;
|
| 23 | import net.minecraft.world.phys.Vec3;
|
| 24 | import combatant.client.config.common.CommonSettingSchemas;
|
| 25 | import combatant.client.config.values.*;
|
| 26 | import combatant.client.features.gui.clickgui.settings.TextListSetting;
|
| 27 | import combatant.client.features.module.*;
|
| 28 | import combatant.client.features.module.modules.misc.DefineTarget;
|
| 29 | import combatant.client.render.engine.RenderState;
|
| 30 | import combatant.client.render.engine.renderer.Renderer3D;
|
| 31 | import combatant.client.util.block.*;
|
| 32 | import combatant.client.util.block.bed.BedBlockUtil;
|
| 33 | import combatant.client.util.block.bed.BedwarsTeamColorUtil;
|
| 34 | import combatant.client.util.block.bed.SelfBedMode;
|
| 35 | import combatant.client.util.block.bed.SelfBedTracker;
|
| 36 | import combatant.client.util.logging.DebugLog;
|
| 37 |
|
| 38 | import java.util.*;
|
| 39 | import java.util.concurrent.ConcurrentHashMap;
|
| 40 |
|
| 41 | //todo Description
|
| 42 | @ModuleInfo(
|
| 43 | id = "bedwarsesp",
|
| 44 | displayName = "BedwarsESP",
|
| 45 | category = ModuleCategory.VISUALS
|
| 46 | )
|
| 47 | public class BedwarsESP extends Module {
|
| 48 | //todo не доделан
|
| 49 |
|
| 50 | private static final int SECTION_SCAN_BUDGET_PER_TICK = 4;
|
| 51 | private static final int SECTION_SCAN_REFRESH_TICKS = 40;
|
| 52 |
|
| 53 | private final Minecraft mc = Minecraft.getInstance();
|
| 54 | private final WorldSectionBlockScanner sectionScanner = new WorldSectionBlockScanner();
|
| 55 |
|
| 56 | private final EnumValue<BlockScanMode> modeValue =
|
| 57 | enumCommon(
|
| 58 | "bedwars_esp_mode",
|
| 59 | "mode",
|
| 60 | CommonSettingSchemas.ESP_SCAN_MODE,
|
| 61 | BlockScanMode.LOS,
|
| 62 | BlockScanMode.LOS,
|
| 63 | BlockScanMode.UNRESTRICTED
|
| 64 | );
|
| 65 |
|
| 66 | private final BooleanValue limitDistanceValue =
|
| 67 | boolCommon(
|
| 68 | "bedwars_esp_limit_distance",
|
| 69 | "limit_distance",
|
| 70 | CommonSettingSchemas.ESP_LIMIT_DISTANCE,
|
| 71 | true
|
| 72 | );
|
| 73 |
|
| 74 | private final NumberValue<Integer> maxDistanceValue =
|
| 75 | visibleWhen(numCommon(
|
| 76 | "bedwars_esp_max_distance",
|
| 77 | "max_distance",
|
| 78 | CommonSettingSchemas.ESP_MAX_DISTANCE,
|
| 79 | 64,
|
| 80 | 1,
|
| 81 | 256
|
| 82 | ), limitDistanceValue::get);
|
| 83 |
|
| 84 | private final NumberValue<Integer> maxTargetsValue =
|
| 85 | numCommon(
|
| 86 | "bedwars_esp_max_targets",
|
| 87 | "max_targets",
|
| 88 | CommonSettingSchemas.ESP_MAX_TARGETS,
|
| 89 | 32,
|
| 90 | 1,
|
| 91 | 256
|
| 92 | );
|
| 93 |
|
| 94 | private final BlockScanContext scanContext = new BlockScanContext(mc, null);
|
| 95 |
|
| 96 | private final BooleanValue useTracersValue =
|
| 97 | boolCommon(
|
| 98 | "bedwars_esp_use_tracers",
|
| 99 | "use_tracers",
|
| 100 | CommonSettingSchemas.ESP_USE_TRACERS,
|
| 101 | false
|
| 102 | );
|
| 103 |
|
| 104 | private final NumberValue<Float> lineWidth =
|
| 105 | numCommon(
|
| 106 | "bedwars_esp_line_width",
|
| 107 | "line_width",
|
| 108 | CommonSettingSchemas.ESP_LINE_WIDTH,
|
| 109 | 1.5f,
|
| 110 | 0.5f,
|
| 111 | 4.0f
|
| 112 | );
|
| 113 |
|
| 114 | private final EnumValue<BlockOutlineShapeMode> outlineShapeMode =
|
| 115 | enumCommon(
|
| 116 | "bedwars_esp_outline_shape",
|
| 117 | "outline_shape",
|
| 118 | CommonSettingSchemas.ESP_OUTLINE_SHAPE,
|
| 119 | BlockOutlineShapeMode.FULL_BLOCK,
|
| 120 | BlockOutlineShapeMode.values()
|
| 121 | );
|
| 122 |
|
| 123 | private final EnumValue<SelfBedMode> selfBedMode =
|
| 124 | enumMode(
|
| 125 | "bedwars_esp_self_bed",
|
| 126 | SelfBedMode.NONE,
|
| 127 | SelfBedMode.values()
|
| 128 | );
|
| 129 | private final NumberValue<Float> spawnBedDistance =
|
| 130 | visibleWhen(num(
|
| 131 | "bedwars_esp_spawn_bed_distance",
|
| 132 | "spawn_bed_distance",
|
| 133 | 24.0f,
|
| 134 | 16.0f,
|
| 135 | 48.0f
|
| 136 | ), () -> selfBedMode.get() == SelfBedMode.SPAWN_LOCATION);
|
| 137 |
|
| 138 | private final NumberValue<Integer> maxLayers =
|
| 139 | num(
|
| 140 | "bedwars_esp_max_layers",
|
| 141 | "max_layers",
|
| 142 | 1,
|
| 143 | 1,
|
| 144 | 5
|
| 145 | );
|
| 146 |
|
| 147 | private final BooleanValue showOpeningsValue =
|
| 148 | bool(
|
| 149 | "bedwars_esp_show_shell",
|
| 150 | "show_openings",
|
| 151 | true
|
| 152 | );
|
| 153 |
|
| 154 | private final RGBAColorValue selfBedColor =
|
| 155 | color(
|
| 156 | "bedwars_esp_self_bed_color",
|
| 157 | "self_bed_color",
|
| 158 | "#8026FF71"
|
| 159 | );
|
| 160 |
|
| 161 | private final RGBAColorValue otherBedColor =
|
| 162 | color(
|
| 163 | "bedwars_esp_other_bed_color",
|
| 164 | "other_bed_color",
|
| 165 | "#80FF6262"
|
| 166 | );
|
| 167 |
|
| 168 | private final RGBAColorValue selfOpeningColor =
|
| 169 | visibleWhen(color(
|
| 170 | "bedwars_esp_self_shell_color",
|
| 171 | "self_opening_color",
|
| 172 | "#4026BFFF"
|
| 173 | ), showOpeningsValue::get);
|
| 174 |
|
| 175 | private final RGBAColorValue otherOpeningColor =
|
| 176 | visibleWhen(color(
|
| 177 | "bedwars_esp_other_shell_color",
|
| 178 | "other_opening_color",
|
| 179 | "#40FFC247"
|
| 180 | ), showOpeningsValue::get);
|
| 181 | private final Map<BlockPos, ObservedBed> observedBedStates = new ConcurrentHashMap<>();
|
| 182 | private final Map<BlockPos, BlockPos> bedPartToKey = new ConcurrentHashMap<>();
|
| 183 | private final Set<Long> loggedBeds = ConcurrentHashMap.newKeySet();
|
| 184 | private volatile Map<BlockPos, BedRenderTarget> renderTargets = new ConcurrentHashMap<>();
|
| 185 | private ClientLevel lastWorld;
|
| 186 | private BlockScanMode lastMode;
|
| 187 | private int lastScanSignature = Integer.MIN_VALUE;
|
| 188 | private int sectionScanRefreshTicks;
|
| 189 |
|
| 190 | private static AABB createFaceBox(BlockPos bedPos, net.minecraft.core.Direction face) {
|
| 191 | final double inset = 0.18;
|
| 192 | final double depth = 0.045;
|
| 193 | final double eps = 0.0025;
|
| 194 | double minX = bedPos.getX();
|
| 195 | double minY = bedPos.getY();
|
| 196 | double minZ = bedPos.getZ();
|
| 197 | double maxX = minX + 1.0;
|
| 198 | double maxY = minY + 1.0;
|
| 199 | double maxZ = minZ + 1.0;
|
| 200 |
|
| 201 | return switch (face) {
|
| 202 | case DOWN -> new AABB(minX + inset, minY - eps, minZ + inset, maxX - inset, minY + depth, maxZ - inset);
|
| 203 | case UP -> new AABB(minX + inset, maxY - depth, minZ + inset, maxX - inset, maxY + eps, maxZ - inset);
|
| 204 | case NORTH -> new AABB(minX + inset, minY + inset, minZ - eps, maxX - inset, maxY - inset, minZ + depth);
|
| 205 | case SOUTH -> new AABB(minX + inset, minY + inset, maxZ - depth, maxX - inset, maxY - inset, maxZ + eps);
|
| 206 | case WEST -> new AABB(minX - eps, minY + inset, minZ + inset, minX + depth, maxY - inset, maxZ - inset);
|
| 207 | case EAST -> new AABB(maxX - depth, minY + inset, minZ + inset, maxX + eps, maxY - inset, maxZ - inset);
|
| 208 | };
|
| 209 | }
|
| 210 |
|
| 211 | private static int multiplyAlpha(int argb, float factor) {
|
| 212 | int alpha = (argb >>> 24) & 0xFF;
|
| 213 | int nextAlpha = Math.max(0, Math.min(255, Math.round(alpha * factor)));
|
| 214 | return (argb & 0x00FFFFFF) | (nextAlpha << 24);
|
| 215 | }
|
| 216 |
|
| 217 | private static int emphasizeAlpha(int argb) {
|
| 218 | int alpha = (argb >>> 24) & 0xFF;
|
| 219 | int nextAlpha = Math.max(alpha, 170);
|
| 220 | return (argb & 0x00FFFFFF) | (Math.min(255, nextAlpha) << 24);
|
| 221 | }
|
| 222 |
|
| 223 | private static void addFilledBox(Renderer3D renderer, AABB box, int argb) {
|
| 224 | int a = (argb >>> 24) & 0xFF;
|
| 225 | int r = (argb >>> 16) & 0xFF;
|
| 226 | int g = (argb >>> 8) & 0xFF;
|
| 227 | int b = argb & 0xFF;
|
| 228 |
|
| 229 | double minX = box.minX;
|
| 230 | double minY = box.minY;
|
| 231 | double minZ = box.minZ;
|
| 232 | double maxX = box.maxX;
|
| 233 | double maxY = box.maxY;
|
| 234 | double maxZ = box.maxZ;
|
| 235 |
|
| 236 | renderer.quad(minX, minY, minZ, maxX, minY, minZ, maxX, minY, maxZ, minX, minY, maxZ, r, g, b, a);
|
| 237 | renderer.quad(minX, maxY, minZ, minX, maxY, maxZ, maxX, maxY, maxZ, maxX, maxY, minZ, r, g, b, a);
|
| 238 | renderer.quad(minX, minY, maxZ, maxX, minY, maxZ, maxX, maxY, maxZ, minX, maxY, maxZ, r, g, b, a);
|
| 239 | renderer.quad(minX, minY, minZ, minX, maxY, minZ, maxX, maxY, minZ, maxX, minY, minZ, r, g, b, a);
|
| 240 | renderer.quad(maxX, minY, minZ, maxX, maxY, minZ, maxX, maxY, maxZ, maxX, minY, maxZ, r, g, b, a);
|
| 241 | renderer.quad(minX, minY, minZ, minX, minY, maxZ, minX, maxY, maxZ, minX, maxY, minZ, r, g, b, a);
|
| 242 | }
|
| 243 |
|
| 244 | private static void addOutlineBox(Renderer3D renderer, AABB box, int argb) {
|
| 245 | int a = (argb >>> 24) & 0xFF;
|
| 246 | int r = (argb >>> 16) & 0xFF;
|
| 247 | int g = (argb >>> 8) & 0xFF;
|
| 248 | int b = argb & 0xFF;
|
| 249 |
|
| 250 | double minX = box.minX;
|
| 251 | double minY = box.minY;
|
| 252 | double minZ = box.minZ;
|
| 253 | double maxX = box.maxX;
|
| 254 | double maxY = box.maxY;
|
| 255 | double maxZ = box.maxZ;
|
| 256 |
|
| 257 | renderer.line(minX, minY, minZ, maxX, minY, minZ, r, g, b, a);
|
| 258 | renderer.line(maxX, minY, minZ, maxX, minY, maxZ, r, g, b, a);
|
| 259 | renderer.line(maxX, minY, maxZ, minX, minY, maxZ, r, g, b, a);
|
| 260 | renderer.line(minX, minY, maxZ, minX, minY, minZ, r, g, b, a);
|
| 261 |
|
| 262 | renderer.line(minX, maxY, minZ, maxX, maxY, minZ, r, g, b, a);
|
| 263 | renderer.line(maxX, maxY, minZ, maxX, maxY, maxZ, r, g, b, a);
|
| 264 | renderer.line(maxX, maxY, maxZ, minX, maxY, maxZ, r, g, b, a);
|
| 265 | renderer.line(minX, maxY, maxZ, minX, maxY, minZ, r, g, b, a);
|
| 266 |
|
| 267 | renderer.line(minX, minY, minZ, minX, maxY, minZ, r, g, b, a);
|
| 268 | renderer.line(maxX, minY, minZ, maxX, maxY, minZ, r, g, b, a);
|
| 269 | renderer.line(maxX, minY, maxZ, maxX, maxY, maxZ, r, g, b, a);
|
| 270 | renderer.line(minX, minY, maxZ, minX, maxY, maxZ, r, g, b, a);
|
| 271 | }
|
| 272 |
|
| 273 | @Override
|
| 274 | public void onEnable() {
|
| 275 | resetRuntimeState();
|
| 276 | scanContext.refresh();
|
| 277 | BlockObservationHub.requestSodiumRebuild(mc, scanBlockRadius());
|
| 278 | requestLocalSectionScan();
|
| 279 | }
|
| 280 |
|
| 281 | @Override
|
| 282 | public void onTick() {
|
| 283 | if (!isEnabled() || mc.level == null) {
|
| 284 | return;
|
| 285 | }
|
| 286 |
|
| 287 | if (lastWorld != mc.level) {
|
| 288 | if (lastWorld != null) {
|
| 289 | resetRuntimeState();
|
| 290 | RenderSectionBlockScanner.clear();
|
| 291 | }
|
| 292 | lastWorld = mc.level;
|
| 293 | }
|
| 294 |
|
| 295 | scanContext.refresh();
|
| 296 | if (mc.player == null) {
|
| 297 | return;
|
| 298 | }
|
| 299 |
|
| 300 | BlockScanMode mode = modeValue.get();
|
| 301 | if (lastMode != mode) {
|
| 302 | clearRenderedBeds();
|
| 303 | lastMode = mode;
|
| 304 | requestLocalSectionScan();
|
| 305 | }
|
| 306 |
|
| 307 | maintainSectionScanQueue();
|
| 308 | drainSectionScanQueue();
|
| 309 |
|
| 310 | reconcileObservedBeds("cached");
|
| 311 | }
|
| 312 |
|
| 313 | @Override
|
| 314 | public void onDisable() {
|
| 315 | resetRuntimeState();
|
| 316 | }
|
| 317 |
|
| 318 | private void resetRuntimeState() {
|
| 319 | clearTargets();
|
| 320 | lastMode = null;
|
| 321 | lastWorld = mc.level;
|
| 322 | lastScanSignature = Integer.MIN_VALUE;
|
| 323 | sectionScanRefreshTicks = 0;
|
| 324 | sectionScanner.reset();
|
| 325 | }
|
| 326 |
|
| 327 | private void clearTargets() {
|
| 328 | clearRenderedBeds();
|
| 329 | observedBedStates.clear();
|
| 330 | }
|
| 331 |
|
| 332 | private void clearRenderedBeds() {
|
| 333 | renderTargets = new ConcurrentHashMap<>();
|
| 334 | bedPartToKey.clear();
|
| 335 | loggedBeds.clear();
|
| 336 | }
|
| 337 |
|
| 338 | @Override
|
| 339 | public WorldPhase getWorldPhase() {
|
| 340 | return WorldPhase.END_MAIN;
|
| 341 | }
|
| 342 |
|
| 343 | @Override
|
| 344 | public void onRenderWorld(PoseStack matrices, SubmitNodeCollector consumers) {
|
| 345 | }
|
| 346 |
|
| 347 | @Override
|
| 348 | public void onRenderWorldEngine(Renderer3D renderer, Renderer3D depthRenderer, float tickDelta) {
|
| 349 | if (!isEnabled() || mc.level == null || mc.player == null) return;
|
| 350 |
|
| 351 | float previousLineWidth = RenderState.lineWidth;
|
| 352 | RenderState.lineWidth = Math.max(0.5f, lineWidth.get());
|
| 353 | try {
|
| 354 | DefineTarget defineTarget = Modules.get(DefineTarget.class);
|
| 355 | boolean useTeamColors = defineTarget != null && defineTarget.useBedwarsTeamColors();
|
| 356 | for (BedRenderTarget target : renderTargets.values()) {
|
| 357 | int bedColor = target.self() ? selfBedColor.getArgb() : otherBedColor.getArgb();
|
| 358 | if (useTeamColors && target.teamColorRgb() != -1) {
|
| 359 | bedColor = BedwarsTeamColorUtil.replaceRgb(bedColor, target.teamColorRgb());
|
| 360 | }
|
| 361 |
|
| 362 | for (BlockPos bedPart : target.bedParts()) {
|
| 363 | BlockState state = mc.level.getBlockState(bedPart);
|
| 364 | if (!BedBlockUtil.isBed(state)) {
|
| 365 | continue;
|
| 366 | }
|
| 367 | BlockOutlineRenderer.render(renderer, mc.level, bedPart, state, outlineShapeMode.get(), emphasizeAlpha(bedColor));
|
| 368 | }
|
| 369 |
|
| 370 | if (!showOpeningsValue.get()) {
|
| 371 | continue;
|
| 372 | }
|
| 373 |
|
| 374 | int openingColor = target.self() ? selfOpeningColor.getArgb() : otherOpeningColor.getArgb();
|
| 375 | if (useTeamColors && target.teamColorRgb() != -1) {
|
| 376 | openingColor = BedwarsTeamColorUtil.replaceRgb(openingColor, target.teamColorRgb());
|
| 377 | }
|
| 378 |
|
| 379 | for (FaceMarker opening : target.openingMarkers()) {
|
| 380 | addFilledBox(renderer, opening.box(), multiplyAlpha(openingColor, 0.85f));
|
| 381 | addOutlineBox(renderer, opening.box(), emphasizeAlpha(openingColor));
|
| 382 | }
|
| 383 | }
|
| 384 | } finally {
|
| 385 | RenderState.lineWidth = previousLineWidth;
|
| 386 | }
|
| 387 |
|
| 388 | renderTracers(renderer, tickDelta);
|
| 389 | }
|
| 390 |
|
| 391 | public void acceptSodiumBlockState(int x, int y, int z, BlockState state) {
|
| 392 | if (!isEnabled() || !BedBlockUtil.isBed(state)) {
|
| 393 | return;
|
| 394 | }
|
| 395 | recordObservedBlock(new BlockPos(x, y, z), state, false, RenderStatus.HIDDEN);
|
| 396 | }
|
| 397 |
|
| 398 | public void acceptSodiumRenderedBlockState(BlockPos pos, BlockState state) {
|
| 399 | if (!isEnabled() || !BedBlockUtil.isBed(state)) {
|
| 400 | return;
|
| 401 | }
|
| 402 | recordObservedBlock(pos, state, false, RenderStatus.VISIBLE);
|
| 403 | }
|
| 404 |
|
| 405 | public void acceptWorldBlockUpdate(BlockPos pos, BlockState state) {
|
| 406 | if (!isEnabled()) {
|
| 407 | return;
|
| 408 | }
|
| 409 | recordObservedBlock(pos, state, true);
|
| 410 | }
|
| 411 |
|
| 412 | private void recordObservedBlock(BlockPos rawPos, BlockState state) {
|
| 413 | recordObservedBlock(rawPos, state, false, RenderStatus.PRESERVE);
|
| 414 | }
|
| 415 |
|
| 416 | private void recordObservedBlock(BlockPos rawPos, BlockState state, boolean forgetScannedSection) {
|
| 417 | recordObservedBlock(rawPos, state, forgetScannedSection, RenderStatus.PRESERVE);
|
| 418 | }
|
| 419 |
|
| 420 | private void recordObservedBlock(BlockPos rawPos,
|
| 421 | BlockState state,
|
| 422 | boolean forgetScannedSection,
|
| 423 | RenderStatus renderStatus) {
|
| 424 | if (rawPos == null || state == null) {
|
| 425 | return;
|
| 426 | }
|
| 427 |
|
| 428 | BlockPos pos = rawPos.immutable();
|
| 429 | if (forgetScannedSection) {
|
| 430 | sectionScanner.forget(pos);
|
| 431 | }
|
| 432 | if (!BedBlockUtil.isBed(state)) {
|
| 433 | observedBedStates.remove(pos);
|
| 434 | removeBedByPart(pos);
|
| 435 | return;
|
| 436 | }
|
| 437 |
|
| 438 | RenderSectionBlockScanner.recordBlock(pos, state);
|
| 439 | observedBedStates.compute(pos, (ignored, previous) -> {
|
| 440 | boolean rendered = switch (renderStatus) {
|
| 441 | case PRESERVE -> previous != null && previous.rendered();
|
| 442 | case HIDDEN -> false;
|
| 443 | case VISIBLE -> true;
|
| 444 | };
|
| 445 | return new ObservedBed(state, rendered);
|
| 446 | });
|
| 447 | }
|
| 448 |
|
| 449 | private void updateObservedBed(BlockPos pos, BlockState state, String source) {
|
| 450 | if (mc.level == null || mc.player == null || !withinDistance(pos)) {
|
| 451 | removeBedByPart(pos);
|
| 452 | return;
|
| 453 | }
|
| 454 |
|
| 455 | ObservedBed observed = observedBedStates.get(pos);
|
| 456 | boolean visible = observed != null && observed.rendered();
|
| 457 | if (!acceptsMode(pos, visible)) {
|
| 458 | removeBedByPart(pos);
|
| 459 | return;
|
| 460 | }
|
| 461 |
|
| 462 | BlockPos key = BedBlockUtil.canonicalBedPos(pos, state);
|
| 463 | if (key == null) {
|
| 464 | removeBedByPart(pos);
|
| 465 | return;
|
| 466 | }
|
| 467 | key = key.immutable();
|
| 468 | if (!renderTargets.containsKey(key) && renderTargets.size() >= maxTargetsValue.get()) {
|
| 469 | return;
|
| 470 | }
|
| 471 |
|
| 472 | Map<BlockPos, BedRenderTarget> built = buildBedTargets(List.of(
|
| 473 | new RenderSectionBlockScanner.ScanResult(pos, state, visible)
|
| 474 | ));
|
| 475 | BedRenderTarget target = built.get(key);
|
| 476 | if (target == null) {
|
| 477 | return;
|
| 478 | }
|
| 479 |
|
| 480 | BedRenderTarget previous = renderTargets.put(key, target);
|
| 481 | if (previous != null) {
|
| 482 | for (BlockPos bedPart : previous.bedParts()) {
|
| 483 | bedPartToKey.remove(bedPart);
|
| 484 | }
|
| 485 | }
|
| 486 | for (BlockPos bedPart : target.bedParts()) {
|
| 487 | bedPartToKey.put(bedPart, key);
|
| 488 | }
|
| 489 |
|
| 490 | if (loggedBeds.add(key.asLong())) {
|
| 491 | DebugLog.info(
|
| 492 | "[BedwarsESP] detected %s at %s visible=%s source=%s mode=%s",
|
| 493 | BuiltInRegistries.BLOCK.getKey(state.getBlock()),
|
| 494 | key.toShortString(),
|
| 495 | visible,
|
| 496 | source,
|
| 497 | modeValue.get().getId()
|
| 498 | );
|
| 499 | }
|
| 500 | }
|
| 501 |
|
| 502 | private void reconcileObservedBeds(String source) {
|
| 503 | for (Map.Entry<BlockPos, ObservedBed> entry : observedBedStates.entrySet()) {
|
| 504 | BlockPos pos = entry.getKey();
|
| 505 | ObservedBed observed = entry.getValue();
|
| 506 | BlockState state = observed.state();
|
| 507 | if (mc.level != null && withinDistance(pos)) {
|
| 508 | BlockState currentState = mc.level.getBlockState(pos);
|
| 509 | if (BedBlockUtil.isBed(currentState)) {
|
| 510 | if (currentState != state) {
|
| 511 | observedBedStates.put(pos, new ObservedBed(currentState, observed.rendered()));
|
| 512 | state = currentState;
|
| 513 | }
|
| 514 | } else {
|
| 515 | observedBedStates.remove(pos);
|
| 516 | removeBedByPart(pos);
|
| 517 | continue;
|
| 518 | }
|
| 519 | }
|
| 520 | updateObservedBed(pos, state, source);
|
| 521 | }
|
| 522 | }
|
| 523 |
|
| 524 | private void removeBedByPart(BlockPos pos) {
|
| 525 | BlockPos key = bedPartToKey.remove(pos);
|
| 526 | if (key == null) {
|
| 527 | return;
|
| 528 | }
|
| 529 |
|
| 530 | BedRenderTarget removed = renderTargets.remove(key);
|
| 531 | loggedBeds.remove(key.asLong());
|
| 532 | if (removed != null) {
|
| 533 | for (BlockPos bedPart : removed.bedParts()) {
|
| 534 | bedPartToKey.remove(bedPart);
|
| 535 | }
|
| 536 | }
|
| 537 | }
|
| 538 |
|
| 539 | private void maintainSectionScanQueue() {
|
| 540 | if (mc.level == null || mc.player == null) {
|
| 541 | return;
|
| 542 | }
|
| 543 |
|
| 544 | int signature = currentScanSignature();
|
| 545 | if (signature != lastScanSignature || ++sectionScanRefreshTicks >= SECTION_SCAN_REFRESH_TICKS) {
|
| 546 | lastScanSignature = signature;
|
| 547 | sectionScanRefreshTicks = 0;
|
| 548 | requestLocalSectionScan();
|
| 549 | }
|
| 550 | }
|
| 551 |
|
| 552 | private void requestLocalSectionScan() {
|
| 553 | if (mc.level == null || mc.player == null) {
|
| 554 | return;
|
| 555 | }
|
| 556 |
|
| 557 | int radius = scanBlockRadius();
|
| 558 | int minY;
|
| 559 | int maxY;
|
| 560 | if (limitDistanceValue.get()) {
|
| 561 | int eyeY = (int) Math.floor(mc.player.getEyePosition().y);
|
| 562 | minY = eyeY - radius;
|
| 563 | maxY = eyeY + radius;
|
| 564 | } else {
|
| 565 | minY = mc.level.getMinY();
|
| 566 | maxY = mc.level.getMaxY();
|
| 567 | }
|
| 568 |
|
| 569 | sectionScanner.enqueueAround(mc, radius, minY, maxY);
|
| 570 | }
|
| 571 |
|
| 572 | private void drainSectionScanQueue() {
|
| 573 | sectionScanner.drain(
|
| 574 | mc,
|
| 575 | SECTION_SCAN_BUDGET_PER_TICK,
|
| 576 | BedBlockUtil::isBed,
|
| 577 | this::recordObservedBlock
|
| 578 | );
|
| 579 | }
|
| 580 |
|
| 581 | private int scanBlockRadius() {
|
| 582 | return limitDistanceValue.get() ? Math.max(1, maxDistanceValue.get()) : 256;
|
| 583 | }
|
| 584 |
|
| 585 | private int currentScanSignature() {
|
| 586 | int centerChunkX = SectionPos.blockToSectionCoord(mc.player.getBlockX());
|
| 587 | int centerChunkZ = SectionPos.blockToSectionCoord(mc.player.getBlockZ());
|
| 588 | int centerSectionY = SectionPos.blockToSectionCoord(mc.player.getBlockY());
|
| 589 | int result = 17;
|
| 590 | result = 31 * result + centerChunkX;
|
| 591 | result = 31 * result + centerChunkZ;
|
| 592 | result = 31 * result + centerSectionY;
|
| 593 | result = 31 * result + (limitDistanceValue.get() ? 1 : 0);
|
| 594 | result = 31 * result + maxDistanceValue.get();
|
| 595 | result = 31 * result + maxLayers.get();
|
| 596 | return result;
|
| 597 | }
|
| 598 |
|
| 599 | private boolean withinDistance(BlockPos pos) {
|
| 600 | if (!limitDistanceValue.get() || mc.player == null) {
|
| 601 | return true;
|
| 602 | }
|
| 603 |
|
| 604 | Vec3 eye = mc.player.getEyePosition();
|
| 605 | double dx = pos.getX() + 0.5 - eye.x;
|
| 606 | double dy = pos.getY() + 0.5 - eye.y;
|
| 607 | double dz = pos.getZ() + 0.5 - eye.z;
|
| 608 | int maxDistance = maxDistanceValue.get();
|
| 609 | return dx * dx + dy * dy + dz * dz <= (double) maxDistance * (double) maxDistance;
|
| 610 | }
|
| 611 |
|
| 612 | private boolean acceptsMode(BlockPos pos, boolean visible) {
|
| 613 | return switch (modeValue.get()) {
|
| 614 | case LOS -> visible;
|
| 615 | case UNRESTRICTED -> true;
|
| 616 | };
|
| 617 | }
|
| 618 |
|
| 619 | private Map<BlockPos, BedRenderTarget> buildBedTargets(Collection<RenderSectionBlockScanner.ScanResult> scanResults) {
|
| 620 | Map<BlockPos, MutableBedTarget> grouped = new HashMap<>();
|
| 621 | for (RenderSectionBlockScanner.ScanResult result : scanResults) {
|
| 622 | BlockState state = result.state();
|
| 623 | BlockPos key = BedBlockUtil.canonicalBedPos(result.pos(), state);
|
| 624 | if (key == null) {
|
| 625 | continue;
|
| 626 | }
|
| 627 |
|
| 628 | MutableBedTarget target = grouped.computeIfAbsent(key, ignored -> new MutableBedTarget());
|
| 629 | target.sourcePos = result.pos();
|
| 630 | target.sourceState = state;
|
| 631 | target.visible |= result.visible();
|
| 632 | Integer teamColorRgb = BedwarsTeamColorUtil.getBedTeamRgb(state);
|
| 633 | if (teamColorRgb != null) {
|
| 634 | target.teamColorRgb = teamColorRgb;
|
| 635 | }
|
| 636 | target.self |= SelfBedTracker.INSTANCE.isSelfBed(
|
| 637 | selfBedMode.get(),
|
| 638 | state,
|
| 639 | result.pos(),
|
| 640 | mc.player,
|
| 641 | spawnBedDistance.get()
|
| 642 | );
|
| 643 | target.bedParts.add(result.pos().immutable());
|
| 644 |
|
| 645 | net.minecraft.core.Direction direction = BedBlockUtil.anotherBedPartDirection(state);
|
| 646 | if (direction != null) {
|
| 647 | target.bedParts.add(result.pos().relative(direction).immutable());
|
| 648 | }
|
| 649 | }
|
| 650 |
|
| 651 | Map<BlockPos, BedRenderTarget> next = new HashMap<>(grouped.size());
|
| 652 | for (Map.Entry<BlockPos, MutableBedTarget> entry : grouped.entrySet()) {
|
| 653 | MutableBedTarget value = entry.getValue();
|
| 654 | if (value.sourcePos == null || value.sourceState == null || value.bedParts.isEmpty()) {
|
| 655 | continue;
|
| 656 | }
|
| 657 |
|
| 658 | List<AABB> bedBoxes = new ArrayList<>(value.bedParts.size());
|
| 659 | double cx = 0.0;
|
| 660 | double cy = 0.0;
|
| 661 | double cz = 0.0;
|
| 662 | for (BlockPos bedPart : value.bedParts) {
|
| 663 | bedBoxes.add(new AABB(bedPart));
|
| 664 | Vec3 center = Vec3.atCenterOf(bedPart);
|
| 665 | cx += center.x;
|
| 666 | cy += center.y;
|
| 667 | cz += center.z;
|
| 668 | }
|
| 669 | Vec3 tracerCenter = new Vec3(cx / value.bedParts.size(), cy / value.bedParts.size(), cz / value.bedParts.size());
|
| 670 |
|
| 671 | List<FaceMarker> openingMarkers = List.of();
|
| 672 | if (showOpeningsValue.get()) {
|
| 673 | LinkedHashSet<BlockPos> shellPositions = new LinkedHashSet<>();
|
| 674 | List<BedBlockUtil.LayeredBlockPos> layerPositions = BedBlockUtil.searchBedLayer(
|
| 675 | value.sourcePos,
|
| 676 | value.sourceState,
|
| 677 | maxLayers.get()
|
| 678 | );
|
| 679 | layerPositions.sort(Comparator.comparingInt(BedBlockUtil.LayeredBlockPos::layer));
|
| 680 | for (BedBlockUtil.LayeredBlockPos layerPos : layerPositions) {
|
| 681 | shellPositions.add(layerPos.pos());
|
| 682 | }
|
| 683 | openingMarkers = collectOpeningMarkers(value.bedParts, shellPositions);
|
| 684 | }
|
| 685 |
|
| 686 | next.put(entry.getKey(), new BedRenderTarget(
|
| 687 | List.copyOf(value.bedParts),
|
| 688 | List.copyOf(bedBoxes),
|
| 689 | openingMarkers,
|
| 690 | tracerCenter,
|
| 691 | value.self,
|
| 692 | value.visible,
|
| 693 | value.teamColorRgb
|
| 694 | ));
|
| 695 | }
|
| 696 | return next;
|
| 697 | }
|
| 698 |
|
| 699 | private List<FaceMarker> collectOpeningMarkers(Collection<BlockPos> bedParts, Collection<BlockPos> shellPositions) {
|
| 700 | if (bedParts.isEmpty()) {
|
| 701 | return List.of();
|
| 702 | }
|
| 703 |
|
| 704 | Set<BlockPos> bedSet = new HashSet<>(bedParts);
|
| 705 | int minX = Integer.MAX_VALUE;
|
| 706 | int minY = Integer.MAX_VALUE;
|
| 707 | int minZ = Integer.MAX_VALUE;
|
| 708 | int maxX = Integer.MIN_VALUE;
|
| 709 | int maxY = Integer.MIN_VALUE;
|
| 710 | int maxZ = Integer.MIN_VALUE;
|
| 711 |
|
| 712 | for (BlockPos pos : bedParts) {
|
| 713 | minX = Math.min(minX, pos.getX());
|
| 714 | minY = Math.min(minY, pos.getY());
|
| 715 | minZ = Math.min(minZ, pos.getZ());
|
| 716 | maxX = Math.max(maxX, pos.getX());
|
| 717 | maxY = Math.max(maxY, pos.getY());
|
| 718 | maxZ = Math.max(maxZ, pos.getZ());
|
| 719 | }
|
| 720 | for (BlockPos pos : shellPositions) {
|
| 721 | minX = Math.min(minX, pos.getX());
|
| 722 | minY = Math.min(minY, pos.getY());
|
| 723 | minZ = Math.min(minZ, pos.getZ());
|
| 724 | maxX = Math.max(maxX, pos.getX());
|
| 725 | maxY = Math.max(maxY, pos.getY());
|
| 726 | maxZ = Math.max(maxZ, pos.getZ());
|
| 727 | }
|
| 728 |
|
| 729 | minX--;
|
| 730 | minY--;
|
| 731 | minZ--;
|
| 732 | maxX++;
|
| 733 | maxY++;
|
| 734 | maxZ++;
|
| 735 |
|
| 736 | ArrayDeque<BlockPos> queue = new ArrayDeque<>();
|
| 737 | Set<BlockPos> reachable = new HashSet<>();
|
| 738 | BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos();
|
| 739 |
|
| 740 | for (int x = minX; x <= maxX; x++) {
|
| 741 | for (int y = minY; y <= maxY; y++) {
|
| 742 | seedOpeningBoundary(queue, reachable, bedSet, mutable.set(x, y, minZ));
|
| 743 | seedOpeningBoundary(queue, reachable, bedSet, mutable.set(x, y, maxZ));
|
| 744 | }
|
| 745 | }
|
| 746 | for (int x = minX; x <= maxX; x++) {
|
| 747 | for (int z = minZ; z <= maxZ; z++) {
|
| 748 | seedOpeningBoundary(queue, reachable, bedSet, mutable.set(x, minY, z));
|
| 749 | seedOpeningBoundary(queue, reachable, bedSet, mutable.set(x, maxY, z));
|
| 750 | }
|
| 751 | }
|
| 752 | for (int y = minY; y <= maxY; y++) {
|
| 753 | for (int z = minZ; z <= maxZ; z++) {
|
| 754 | seedOpeningBoundary(queue, reachable, bedSet, mutable.set(minX, y, z));
|
| 755 | seedOpeningBoundary(queue, reachable, bedSet, mutable.set(maxX, y, z));
|
| 756 | }
|
| 757 | }
|
| 758 |
|
| 759 | while (!queue.isEmpty()) {
|
| 760 | BlockPos current = queue.removeFirst();
|
| 761 | for (net.minecraft.core.Direction direction : BlockScanContext.DIRECTIONS) {
|
| 762 | mutable.set(current).move(direction);
|
| 763 | if (mutable.getX() < minX || mutable.getX() > maxX
|
| 764 | || mutable.getY() < minY || mutable.getY() > maxY
|
| 765 | || mutable.getZ() < minZ || mutable.getZ() > maxZ) {
|
| 766 | continue;
|
| 767 | }
|
| 768 | seedOpeningBoundary(queue, reachable, bedSet, mutable);
|
| 769 | }
|
| 770 | }
|
| 771 |
|
| 772 | LinkedHashSet<FaceKey> openings = new LinkedHashSet<>();
|
| 773 | for (BlockPos airPos : reachable) {
|
| 774 | for (net.minecraft.core.Direction direction : BlockScanContext.DIRECTIONS) {
|
| 775 | BlockPos neighbor = airPos.relative(direction);
|
| 776 | if (!bedSet.contains(neighbor)) {
|
| 777 | continue;
|
| 778 | }
|
| 779 | openings.add(new FaceKey(neighbor.immutable(), direction.getOpposite()));
|
| 780 | }
|
| 781 | }
|
| 782 |
|
| 783 | if (openings.isEmpty()) {
|
| 784 | return List.of();
|
| 785 | }
|
| 786 |
|
| 787 | List<FaceMarker> result = new ArrayList<>(openings.size());
|
| 788 | for (FaceKey opening : openings) {
|
| 789 | result.add(new FaceMarker(createFaceBox(opening.pos(), opening.face())));
|
| 790 | }
|
| 791 | return List.copyOf(result);
|
| 792 | }
|
| 793 |
|
| 794 | private void seedOpeningBoundary(ArrayDeque<BlockPos> queue,
|
| 795 | Set<BlockPos> reachable,
|
| 796 | Set<BlockPos> bedSet,
|
| 797 | BlockPos pos) {
|
| 798 | if (bedSet.contains(pos) || !scanContext.isPassable(pos)) {
|
| 799 | return;
|
| 800 | }
|
| 801 | BlockPos immutable = pos.immutable();
|
| 802 | if (reachable.add(immutable)) {
|
| 803 | queue.addLast(immutable);
|
| 804 | }
|
| 805 | }
|
| 806 |
|
| 807 | private void renderTracers(Renderer3D renderer, float tickDelta) {
|
| 808 | Tracers tracers = Modules.get(Tracers.class);
|
| 809 | if (!useTracersValue.get() || tracers == null) return;
|
| 810 |
|
| 811 | DefineTarget defineTarget = Modules.get(DefineTarget.class);
|
| 812 | boolean useTeamColors = defineTarget != null && defineTarget.useBedwarsTeamColors();
|
| 813 | for (BedRenderTarget target : renderTargets.values()) {
|
| 814 | int color = target.self() ? selfBedColor.getArgb() : otherBedColor.getArgb();
|
| 815 | if (useTeamColors && target.teamColorRgb() != -1) {
|
| 816 | color = BedwarsTeamColorUtil.replaceRgb(color, target.teamColorRgb());
|
| 817 | }
|
| 818 | tracers.render3DTracersRaw(renderer, tickDelta, List.of(target.tracerCenter()), color);
|
| 819 | }
|
| 820 | }
|
| 821 |
|
| 822 | private static final class MutableBedTarget {
|
| 823 | private final LinkedHashSet<BlockPos> bedParts = new LinkedHashSet<>();
|
| 824 | private BlockPos sourcePos;
|
| 825 | private BlockState sourceState;
|
| 826 | private boolean self;
|
| 827 | private boolean visible;
|
| 828 | private int teamColorRgb = -1;
|
| 829 | }
|
| 830 |
|
| 831 | private enum RenderStatus {
|
| 832 | PRESERVE,
|
| 833 | HIDDEN,
|
| 834 | VISIBLE
|
| 835 | }
|
| 836 |
|
| 837 | private record ObservedBed(BlockState state, boolean rendered) {
|
| 838 | }
|
| 839 |
|
| 840 | private record FaceKey(BlockPos pos, net.minecraft.core.Direction face) {
|
| 841 | }
|
| 842 |
|
| 843 | private record FaceMarker(AABB box) {
|
| 844 | }
|
| 845 |
|
| 846 | private record BedRenderTarget(
|
| 847 | List<BlockPos> bedParts,
|
| 848 | List<AABB> bedBoxes,
|
| 849 | List<FaceMarker> openingMarkers,
|
| 850 | Vec3 tracerCenter,
|
| 851 | boolean self,
|
| 852 | boolean visible,
|
| 853 | int teamColorRgb
|
| 854 | ) {
|
| 855 | }
|
| 856 | }
|
| 857 |
|