| 1 | package net.minecraft.client.gui.components.debug;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.ChatFormatting;
|
| 8 | import net.minecraft.SharedConstants;
|
| 9 | import net.minecraft.client.Minecraft;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.TypedInstance;
|
| 12 | import net.minecraft.resources.Identifier;
|
| 13 | import net.minecraft.world.entity.Entity;
|
| 14 | import net.minecraft.world.level.Level;
|
| 15 | import net.minecraft.world.level.block.Block;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.block.state.StateHolder;
|
| 18 | import net.minecraft.world.level.block.state.properties.Property.Value;
|
| 19 | import net.minecraft.world.level.chunk.LevelChunk;
|
| 20 | import net.minecraft.world.level.material.Fluid;
|
| 21 | import net.minecraft.world.level.material.FluidState;
|
| 22 | import net.minecraft.world.phys.BlockHitResult;
|
| 23 | import net.minecraft.world.phys.HitResult;
|
| 24 | import net.minecraft.world.phys.HitResult.Type;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | @Environment(EnvType.CLIENT)
|
| 28 | public abstract class DebugEntryLookingAt implements DebugScreenEntry {
|
| 29 | private static final int RANGE = 20;
|
| 30 | private static final Identifier BLOCK_GROUP = Identifier.withDefaultNamespace("looking_at_block");
|
| 31 | private static final Identifier FLUID_GROUP = Identifier.withDefaultNamespace("looking_at_fluid");
|
| 32 |
|
| 33 | @Override
|
| 34 | public void display(
|
| 35 | final DebugScreenDisplayer displayer,
|
| 36 | final @Nullable Level serverOrClientLevel,
|
| 37 | final @Nullable LevelChunk clientChunk,
|
| 38 | final @Nullable LevelChunk serverChunk
|
| 39 | ) {
|
| 40 | Entity cameraEntity = Minecraft.getInstance().getCameraEntity();
|
| 41 | Level clientOrServerLevel = SharedConstants.DEBUG_SHOW_SERVER_DEBUG_VALUES ? serverOrClientLevel : Minecraft.getInstance().level;
|
| 42 | if (cameraEntity != null && clientOrServerLevel != null) {
|
| 43 | HitResult block = this.getHitResult(cameraEntity);
|
| 44 | List<String> result = new ArrayList<>();
|
| 45 | if (block.getType() == Type.BLOCK) {
|
| 46 | BlockPos pos = ((BlockHitResult)block).getBlockPos();
|
| 47 | this.extractInfo(result, clientOrServerLevel, pos);
|
| 48 | }
|
| 49 |
|
| 50 | displayer.addToGroup(this.group(), result);
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | public abstract HitResult getHitResult(final Entity cameraEntity);
|
| 55 |
|
| 56 | public abstract void extractInfo(List<String> result, Level level, BlockPos pos);
|
| 57 |
|
| 58 | public abstract Identifier group();
|
| 59 |
|
| 60 | public static void addTagEntries(final List<String> result, final TypedInstance<?> instance) {
|
| 61 | instance.tags().map(e -> "#" + e.location()).forEach(result::add);
|
| 62 | }
|
| 63 |
|
| 64 | @Environment(EnvType.CLIENT)
|
| 65 | public static class BlockStateInfo extends DebugEntryLookingAt.DebugEntryLookingAtState<Block, BlockState> {
|
| 66 | protected BlockStateInfo() {
|
| 67 | super("Targeted Block");
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public HitResult getHitResult(final Entity cameraEntity) {
|
| 72 | return cameraEntity.pick(20.0, 0.0F, false);
|
| 73 | }
|
| 74 |
|
| 75 | public BlockState getInstance(final Level level, final BlockPos pos) {
|
| 76 | return level.getBlockState(pos);
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public Identifier group() {
|
| 81 | return DebugEntryLookingAt.BLOCK_GROUP;
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | @Environment(EnvType.CLIENT)
|
| 86 | public static class BlockTagInfo extends DebugEntryLookingAt.DebugEntryLookingAtTags<BlockState> {
|
| 87 | @Override
|
| 88 | public HitResult getHitResult(final Entity cameraEntity) {
|
| 89 | return cameraEntity.pick(20.0, 0.0F, false);
|
| 90 | }
|
| 91 |
|
| 92 | public BlockState getInstance(final Level level, final BlockPos pos) {
|
| 93 | return level.getBlockState(pos);
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public Identifier group() {
|
| 98 | return DebugEntryLookingAt.BLOCK_GROUP;
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | @Environment(EnvType.CLIENT)
|
| 103 | public abstract static class DebugEntryLookingAtState<OwnerType, StateType extends StateHolder<OwnerType, StateType> & TypedInstance<OwnerType>>
|
| 104 | extends DebugEntryLookingAt {
|
| 105 | private final String prefix;
|
| 106 |
|
| 107 | protected DebugEntryLookingAtState(final String prefix) {
|
| 108 | this.prefix = prefix;
|
| 109 | }
|
| 110 |
|
| 111 | protected abstract StateType getInstance(Level level, BlockPos pos);
|
| 112 |
|
| 113 | @Override
|
| 114 | public void extractInfo(final List<String> result, final Level level, final BlockPos pos) {
|
| 115 | StateType stateInstance = this.getInstance(level, pos);
|
| 116 | result.add(ChatFormatting.UNDERLINE + this.prefix + ": " + pos.getX() + ", " + pos.getY() + ", " + pos.getZ());
|
| 117 | result.add(stateInstance.typeHolder().getRegisteredName());
|
| 118 | addStateProperties(result, stateInstance);
|
| 119 | }
|
| 120 |
|
| 121 | private static void addStateProperties(final List<String> result, final StateHolder<?, ?> stateHolder) {
|
| 122 | stateHolder.getValues().forEach(entry -> result.add(getPropertyValueString((Value<?>)entry)));
|
| 123 | }
|
| 124 |
|
| 125 | private static String getPropertyValueString(final Value<?> entry) {
|
| 126 | String valueString = entry.valueName();
|
| 127 | if (Boolean.TRUE.equals(entry.value())) {
|
| 128 | valueString = ChatFormatting.GREEN + valueString;
|
| 129 | } else if (Boolean.FALSE.equals(entry.value())) {
|
| 130 | valueString = ChatFormatting.RED + valueString;
|
| 131 | }
|
| 132 |
|
| 133 | return entry.property().getName() + ": " + valueString;
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | @Environment(EnvType.CLIENT)
|
| 138 | public abstract static class DebugEntryLookingAtTags<T extends TypedInstance<?>> extends DebugEntryLookingAt {
|
| 139 | protected abstract T getInstance(Level level, BlockPos pos);
|
| 140 |
|
| 141 | @Override
|
| 142 | public void extractInfo(final List<String> result, final Level level, final BlockPos pos) {
|
| 143 | T instance = this.getInstance(level, pos);
|
| 144 | addTagEntries(result, instance);
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | @Environment(EnvType.CLIENT)
|
| 149 | public static class FluidStateInfo extends DebugEntryLookingAt.DebugEntryLookingAtState<Fluid, FluidState> {
|
| 150 | protected FluidStateInfo() {
|
| 151 | super("Targeted Fluid");
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | public HitResult getHitResult(final Entity cameraEntity) {
|
| 156 | return cameraEntity.pick(20.0, 0.0F, true);
|
| 157 | }
|
| 158 |
|
| 159 | public FluidState getInstance(final Level level, final BlockPos pos) {
|
| 160 | return level.getFluidState(pos);
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public Identifier group() {
|
| 165 | return DebugEntryLookingAt.FLUID_GROUP;
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | @Environment(EnvType.CLIENT)
|
| 170 | public static class FluidTagInfo extends DebugEntryLookingAt.DebugEntryLookingAtTags<FluidState> {
|
| 171 | @Override
|
| 172 | public HitResult getHitResult(final Entity cameraEntity) {
|
| 173 | return cameraEntity.pick(20.0, 0.0F, true);
|
| 174 | }
|
| 175 |
|
| 176 | public FluidState getInstance(final Level level, final BlockPos pos) {
|
| 177 | return level.getFluidState(pos);
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | public Identifier group() {
|
| 182 | return DebugEntryLookingAt.FLUID_GROUP;
|
| 183 | }
|
| 184 | }
|
| 185 | }
|
| 186 |
|