| 1 | package net.minecraft.client.renderer.debug;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import java.util.Collections;
|
| 5 | import java.util.List;
|
| 6 | import java.util.function.DoubleSupplier;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.Minecraft;
|
| 10 | import net.minecraft.client.renderer.culling.Frustum;
|
| 11 | import net.minecraft.core.BlockPos;
|
| 12 | import net.minecraft.gizmos.GizmoStyle;
|
| 13 | import net.minecraft.gizmos.Gizmos;
|
| 14 | import net.minecraft.util.ARGB;
|
| 15 | import net.minecraft.util.Util;
|
| 16 | import net.minecraft.util.debug.DebugValueAccess;
|
| 17 | import net.minecraft.world.entity.Entity;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.phys.AABB;
|
| 20 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 21 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class SupportBlockRenderer implements DebugRenderer.SimpleDebugRenderer {
|
| 25 | private final Minecraft minecraft;
|
| 26 | private double lastUpdateTime = Double.MIN_VALUE;
|
| 27 | private List<Entity> surroundEntities = Collections.emptyList();
|
| 28 |
|
| 29 | public SupportBlockRenderer(final Minecraft minecraft) {
|
| 30 | this.minecraft = minecraft;
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public void emitGizmos(
|
| 35 | final double camX, final double camY, final double camZ, final DebugValueAccess debugValues, final Frustum frustum, final float partialTicks
|
| 36 | ) {
|
| 37 | double time = Util.getNanos();
|
| 38 | if (time - this.lastUpdateTime > 1.0E8) {
|
| 39 | this.lastUpdateTime = time;
|
| 40 | Entity cameraEntity = this.minecraft.getCameraEntity();
|
| 41 | this.surroundEntities = ImmutableList.copyOf(cameraEntity.level().getEntities(cameraEntity, cameraEntity.getBoundingBox().inflate(16.0)));
|
| 42 | }
|
| 43 |
|
| 44 | Player player = this.minecraft.player;
|
| 45 | if (player != null && player.mainSupportingBlockPos.isPresent()) {
|
| 46 | this.drawHighlights(player, () -> 0.0, -65536);
|
| 47 | }
|
| 48 |
|
| 49 | for (Entity entity : this.surroundEntities) {
|
| 50 | if (entity != player && !entity.isInvisible()) {
|
| 51 | this.drawHighlights(entity, () -> this.getBias(entity), -16711936);
|
| 52 | }
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | private void drawHighlights(final Entity entity, final DoubleSupplier biasGetter, final int color) {
|
| 57 | entity.mainSupportingBlockPos.ifPresent(bp -> {
|
| 58 | double bias = biasGetter.getAsDouble();
|
| 59 | BlockPos supportingBlock = entity.getOnPos();
|
| 60 | this.highlightPosition(supportingBlock, 0.02 + bias, color);
|
| 61 | BlockPos effect = entity.getOnPosLegacy();
|
| 62 | if (!effect.equals(supportingBlock)) {
|
| 63 | this.highlightPosition(effect, 0.04 + bias, -16711681);
|
| 64 | }
|
| 65 | });
|
| 66 | }
|
| 67 |
|
| 68 | private double getBias(final Entity entity) {
|
| 69 | return 0.02 * (String.valueOf(entity.getId() + 0.132453657).hashCode() % 1000) / 1000.0;
|
| 70 | }
|
| 71 |
|
| 72 | private void highlightPosition(final BlockPos pos, final double offset, final int color) {
|
| 73 | double fromX = pos.getX() - 2.0 * offset;
|
| 74 | double fromY = pos.getY() - 2.0 * offset;
|
| 75 | double fromZ = pos.getZ() - 2.0 * offset;
|
| 76 | double toX = fromX + 1.0 + 4.0 * offset;
|
| 77 | double toY = fromY + 1.0 + 4.0 * offset;
|
| 78 | double toZ = fromZ + 1.0 + 4.0 * offset;
|
| 79 | Gizmos.cuboid(new AABB(fromX, fromY, fromZ, toX, toY, toZ), GizmoStyle.stroke(ARGB.color(0.4F, color)));
|
| 80 | VoxelShape shape = this.minecraft.level.getBlockState(pos).getCollisionShape(this.minecraft.level, pos, CollisionContext.empty()).move(pos);
|
| 81 | GizmoStyle style = GizmoStyle.stroke(color);
|
| 82 |
|
| 83 | for (AABB aabb : shape.toAabbs()) {
|
| 84 | Gizmos.cuboid(aabb, style);
|
| 85 | }
|
| 86 | }
|
| 87 | }
|
| 88 |
|