| 1 | package net.minecraft;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Locale;
|
| 6 | import java.util.Objects;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.SectionPos;
|
| 9 | import net.minecraft.world.level.LevelHeightAccessor;
|
| 10 | import net.minecraft.world.level.block.state.BlockState;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public class CrashReportCategory {
|
| 14 | private final String title;
|
| 15 | private final List<CrashReportCategory.Entry> entries = Lists.newArrayList();
|
| 16 | private StackTraceElement[] stackTrace = new StackTraceElement[0];
|
| 17 |
|
| 18 | public CrashReportCategory(final String title) {
|
| 19 | this.title = title;
|
| 20 | }
|
| 21 |
|
| 22 | public static String formatLocation(final LevelHeightAccessor levelHeightAccessor, final double x, final double y, final double z) {
|
| 23 | return String.format(Locale.ROOT, "%.2f,%.2f,%.2f - %s", x, y, z, formatLocation(levelHeightAccessor, BlockPos.containing(x, y, z)));
|
| 24 | }
|
| 25 |
|
| 26 | public static String formatLocation(final LevelHeightAccessor levelHeightAccessor, final BlockPos pos) {
|
| 27 | return formatLocation(levelHeightAccessor, pos.getX(), pos.getY(), pos.getZ());
|
| 28 | }
|
| 29 |
|
| 30 | public static String formatLocation(final LevelHeightAccessor levelHeightAccessor, final int x, final int y, final int z) {
|
| 31 | StringBuilder result = new StringBuilder();
|
| 32 |
|
| 33 | try {
|
| 34 | result.append(String.format(Locale.ROOT, "World: (%d,%d,%d)", x, y, z));
|
| 35 | } catch (Throwable ignored) {
|
| 36 | result.append("(Error finding world loc)");
|
| 37 | }
|
| 38 |
|
| 39 | result.append(", ");
|
| 40 |
|
| 41 | try {
|
| 42 | int sectionX = SectionPos.blockToSectionCoord(x);
|
| 43 | int sectionY = SectionPos.blockToSectionCoord(y);
|
| 44 | int sectionZ = SectionPos.blockToSectionCoord(z);
|
| 45 | int relativeX = x & 15;
|
| 46 | int relativeY = y & 15;
|
| 47 | int relativeZ = z & 15;
|
| 48 | int minBlockX = SectionPos.sectionToBlockCoord(sectionX);
|
| 49 | int minBlockY = levelHeightAccessor.getMinY();
|
| 50 | int minBlockZ = SectionPos.sectionToBlockCoord(sectionZ);
|
| 51 | int maxBlockX = SectionPos.sectionToBlockCoord(sectionX + 1) - 1;
|
| 52 | int maxBlockY = levelHeightAccessor.getMaxY();
|
| 53 | int maxBlockZ = SectionPos.sectionToBlockCoord(sectionZ + 1) - 1;
|
| 54 | result.append(
|
| 55 | String.format(
|
| 56 | Locale.ROOT,
|
| 57 | "Section: (at %d,%d,%d in %d,%d,%d; chunk contains blocks %d,%d,%d to %d,%d,%d)",
|
| 58 | relativeX,
|
| 59 | relativeY,
|
| 60 | relativeZ,
|
| 61 | sectionX,
|
| 62 | sectionY,
|
| 63 | sectionZ,
|
| 64 | minBlockX,
|
| 65 | minBlockY,
|
| 66 | minBlockZ,
|
| 67 | maxBlockX,
|
| 68 | maxBlockY,
|
| 69 | maxBlockZ
|
| 70 | )
|
| 71 | );
|
| 72 | } catch (Throwable ignored) {
|
| 73 | result.append("(Error finding chunk loc)");
|
| 74 | }
|
| 75 |
|
| 76 | result.append(", ");
|
| 77 |
|
| 78 | try {
|
| 79 | int regionX = x >> 9;
|
| 80 | int regionZ = z >> 9;
|
| 81 | int minChunkX = regionX << 5;
|
| 82 | int minChunkZ = regionZ << 5;
|
| 83 | int maxChunkX = (regionX + 1 << 5) - 1;
|
| 84 | int maxChunkZ = (regionZ + 1 << 5) - 1;
|
| 85 | int minBlockX = regionX << 9;
|
| 86 | int minBlockY = levelHeightAccessor.getMinY();
|
| 87 | int minBlockZ = regionZ << 9;
|
| 88 | int maxBlockX = (regionX + 1 << 9) - 1;
|
| 89 | int maxBlockY = levelHeightAccessor.getMaxY();
|
| 90 | int maxBlockZ = (regionZ + 1 << 9) - 1;
|
| 91 | result.append(
|
| 92 | String.format(
|
| 93 | Locale.ROOT,
|
| 94 | "Region: (%d,%d; contains chunks %d,%d to %d,%d, blocks %d,%d,%d to %d,%d,%d)",
|
| 95 | regionX,
|
| 96 | regionZ,
|
| 97 | minChunkX,
|
| 98 | minChunkZ,
|
| 99 | maxChunkX,
|
| 100 | maxChunkZ,
|
| 101 | minBlockX,
|
| 102 | minBlockY,
|
| 103 | minBlockZ,
|
| 104 | maxBlockX,
|
| 105 | maxBlockY,
|
| 106 | maxBlockZ
|
| 107 | )
|
| 108 | );
|
| 109 | } catch (Throwable ignored) {
|
| 110 | result.append("(Error finding world loc)");
|
| 111 | }
|
| 112 |
|
| 113 | return result.toString();
|
| 114 | }
|
| 115 |
|
| 116 | public CrashReportCategory setDetail(final String key, final CrashReportDetail<String> callback) {
|
| 117 | try {
|
| 118 | this.setDetail(key, callback.call());
|
| 119 | } catch (Throwable t) {
|
| 120 | this.setDetailError(key, t);
|
| 121 | }
|
| 122 |
|
| 123 | return this;
|
| 124 | }
|
| 125 |
|
| 126 | public CrashReportCategory setDetail(final String key, final @Nullable Object value) {
|
| 127 | this.entries.add(new CrashReportCategory.Entry(key, value));
|
| 128 | return this;
|
| 129 | }
|
| 130 |
|
| 131 | public void setDetailError(final String key, final Throwable t) {
|
| 132 | this.setDetail(key, t);
|
| 133 | }
|
| 134 |
|
| 135 | public int fillInStackTrace(final int nestedOffset) {
|
| 136 | StackTraceElement[] full = Thread.currentThread().getStackTrace();
|
| 137 | if (full.length <= 0) {
|
| 138 | return 0;
|
| 139 | }
|
| 140 |
|
| 141 | this.stackTrace = new StackTraceElement[full.length - 3 - nestedOffset];
|
| 142 | System.arraycopy(full, 3 + nestedOffset, this.stackTrace, 0, this.stackTrace.length);
|
| 143 | return this.stackTrace.length;
|
| 144 | }
|
| 145 |
|
| 146 | public boolean validateStackTrace(final @Nullable StackTraceElement source, final @Nullable StackTraceElement next) {
|
| 147 | if (this.stackTrace.length != 0 && source != null) {
|
| 148 | StackTraceElement current = this.stackTrace[0];
|
| 149 | if (current.isNativeMethod() == source.isNativeMethod()
|
| 150 | && Objects.equals(current.getClassName(), source.getClassName())
|
| 151 | && Objects.equals(current.getFileName(), source.getFileName())
|
| 152 | && Objects.equals(current.getMethodName(), source.getMethodName())) {
|
| 153 | if (next != null != this.stackTrace.length > 1) {
|
| 154 | return false;
|
| 155 | }
|
| 156 |
|
| 157 | if (next != null && !this.stackTrace[1].equals(next)) {
|
| 158 | return false;
|
| 159 | }
|
| 160 |
|
| 161 | this.stackTrace[0] = source;
|
| 162 | return true;
|
| 163 | } else {
|
| 164 | return false;
|
| 165 | }
|
| 166 | } else {
|
| 167 | return false;
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | public void getDetails(final StringBuilder builder) {
|
| 172 | builder.append("-- ").append(this.title).append(" --\n");
|
| 173 | builder.append("Details:");
|
| 174 |
|
| 175 | for (CrashReportCategory.Entry entry : this.entries) {
|
| 176 | builder.append("\n\t");
|
| 177 | builder.append(entry.key());
|
| 178 | builder.append(": ");
|
| 179 | builder.append(entry.value());
|
| 180 | }
|
| 181 |
|
| 182 | if (this.stackTrace.length > 0) {
|
| 183 | builder.append("\nStacktrace:");
|
| 184 |
|
| 185 | for (StackTraceElement element : this.stackTrace) {
|
| 186 | builder.append("\n\tat ");
|
| 187 | builder.append(element);
|
| 188 | }
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | public StackTraceElement[] getStacktrace() {
|
| 193 | return this.stackTrace;
|
| 194 | }
|
| 195 |
|
| 196 | public static void populateBlockDetails(
|
| 197 | final CrashReportCategory category, final LevelHeightAccessor levelHeightAccessor, final BlockPos pos, final BlockState state
|
| 198 | ) {
|
| 199 | category.setDetail("Block", state::toString);
|
| 200 | populateBlockLocationDetails(category, levelHeightAccessor, pos);
|
| 201 | }
|
| 202 |
|
| 203 | public static CrashReportCategory populateBlockLocationDetails(
|
| 204 | final CrashReportCategory category, final LevelHeightAccessor levelHeightAccessor, final BlockPos pos
|
| 205 | ) {
|
| 206 | return category.setDetail("Block location", () -> formatLocation(levelHeightAccessor, pos));
|
| 207 | }
|
| 208 |
|
| 209 | public record Entry(String key, String value) {
|
| 210 | public Entry(final String key, final @Nullable Object rawValue) {
|
| 211 | String value;
|
| 212 | if (rawValue == null) {
|
| 213 | value = "~~NULL~~";
|
| 214 | } else if (rawValue instanceof Throwable t) {
|
| 215 | value = "~~ERROR~~ " + t.getClass().getSimpleName() + ": " + t.getMessage();
|
| 216 | } else {
|
| 217 | value = rawValue.toString();
|
| 218 | }
|
| 219 |
|
| 220 | this(key, value);
|
| 221 | }
|
| 222 | }
|
| 223 | }
|
| 224 |
|