| 1 | package net.minecraft.world.level.block.state;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.LinkedHashMap;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Map.Entry;
|
| 8 | import java.util.stream.Collectors;
|
| 9 | import net.minecraft.SharedConstants;
|
| 10 | import net.minecraft.core.Holder;
|
| 11 | import net.minecraft.world.level.block.Block;
|
| 12 | import org.slf4j.Logger;
|
| 13 |
|
| 14 | public class SolidDebugger {
|
| 15 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 16 | private static final ScopedValue<SolidDebugger> STATUS = ScopedValue.newInstance();
|
| 17 | private final Map<Holder<Block>, SolidDebugger.Reason> reasonByBlock = new LinkedHashMap<>();
|
| 18 |
|
| 19 | public static boolean logAndGet(final Holder<Block> block, final SolidDebugger.Reason reason) {
|
| 20 | if (STATUS.isBound()) {
|
| 21 | SolidDebugger.Reason existingReason = STATUS.get().reasonByBlock.putIfAbsent(block, reason);
|
| 22 | if (existingReason != null && existingReason.causesSolid != reason.causesSolid) {
|
| 23 | LOGGER.debug(
|
| 24 | "Block {} has multiple values for solid. Existing: {} ({}) New: {} ({})",
|
| 25 | block,
|
| 26 | existingReason.causesSolid,
|
| 27 | existingReason.message,
|
| 28 | reason.causesSolid,
|
| 29 | reason.message
|
| 30 | );
|
| 31 | }
|
| 32 | }
|
| 33 |
|
| 34 | return reason.causesSolid;
|
| 35 | }
|
| 36 |
|
| 37 | public void dump() {
|
| 38 | Map<SolidDebugger.Reason, List<Holder<Block>>> blocksByReason = this.reasonByBlock
|
| 39 | .entrySet()
|
| 40 | .stream()
|
| 41 | .collect(Collectors.groupingBy(Entry::getValue, Collectors.mapping(Entry::getKey, Collectors.toList())));
|
| 42 | blocksByReason.forEach((reason, blocks) -> {
|
| 43 | if (!blocks.isEmpty()) {
|
| 44 | String blockNames = blocks.stream().map(b -> "\t" + b.getRegisteredName()).collect(Collectors.joining("\n"));
|
| 45 | LOGGER.debug("The following blocks are {} because: {}:\n {}", reason.causesSolid ? "solid" : "not solid", reason.message, blockNames);
|
| 46 | }
|
| 47 | });
|
| 48 | }
|
| 49 |
|
| 50 | public static void runAndDump(final Runnable task) {
|
| 51 | if (SharedConstants.DEBUG_CALCULATE_SOLID) {
|
| 52 | SolidDebugger solidDebugger = new SolidDebugger();
|
| 53 | ScopedValue.where(STATUS, solidDebugger).run(task);
|
| 54 | solidDebugger.dump();
|
| 55 | } else {
|
| 56 | task.run();
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | public enum Reason {
|
| 61 | FORCE_SOLID_ON(true, "forceSolidOn called on properties"),
|
| 62 | LARGE_ENOUGH_COLLISION_SHAPE(true, "collision shape bounds size is large enough"),
|
| 63 | HIGH_ENOUGH_COLLISION_SHAPE(true, "collision shape bounds are higher than 1"),
|
| 64 | FORCE_SOLID_OFF(false, "forceSolidOff called on properties"),
|
| 65 | NULL_CACHE(false, "cache is null"),
|
| 66 | EMPTY_COLLISION_SHAPE(false, "collision shape is empty"),
|
| 67 | FALLTHROUGH(false, "fallthrough");
|
| 68 |
|
| 69 | private final boolean causesSolid;
|
| 70 | private final String message;
|
| 71 |
|
| 72 | Reason(final boolean causesSolid, final String message) {
|
| 73 | this.causesSolid = causesSolid;
|
| 74 | this.message = message;
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 |
|