1515import net .minecraft .util .ProblemReporter ;
1616import net .minecraft .world .entity .Entity ;
1717import net .minecraft .world .entity .EntityType ;
18+ import net .minecraft .world .level .Level ;
1819import net .minecraft .world .level .levelgen .LegacyRandomSource ;
1920import net .minecraft .world .level .levelgen .RandomSupport ;
2021import net .minecraft .world .level .storage .TagValueOutput ;
4950import java .util .ArrayList ;
5051import java .util .List ;
5152import java .util .Vector ;
53+ import java .util .function .Supplier ;
5254import java .util .zip .GZIPInputStream ;
5355import java .util .zip .GZIPOutputStream ;
5456
5557public class DebugRandom extends LegacyRandomSource {
5658 static final Logger LOGGER = LogUtils .getLogger ();
5759
5860 public static final EntityType <?> DEBUG_ENTITY_TYPE ;
61+ public static final ResourceLocation DEBUG_DIMENSION ;
62+ public static final boolean SAVE_ENTITY_TAG = Boolean .parseBoolean (System .getProperty ("clientcommands.debugEntityRng.saveTag" , "true" ));
63+
5964 static {
6065 String debugEntityType = System .getProperty ("clientcommands.debugEntityRng" );
6166 if (debugEntityType == null ) {
6267 DEBUG_ENTITY_TYPE = null ;
6368 } else {
6469 DEBUG_ENTITY_TYPE = BuiltInRegistries .ENTITY_TYPE .getValue (ResourceLocation .parse (debugEntityType ));
6570 }
71+
72+ String debugDimensionStr = System .getProperty ("clientcommands.debugDimensionRng" );
73+ if (debugDimensionStr == null ) {
74+ DEBUG_DIMENSION = null ;
75+ } else {
76+ DEBUG_DIMENSION = ResourceLocation .tryParse (debugDimensionStr );
77+ }
6678 }
6779
6880 private static final Object2IntMap <String > stackTraceIds = new Object2IntOpenHashMap <>();
6981 static final List <String > stackTraceById = new ArrayList <>();
7082
71- private final Entity entity ;
7283 private boolean firstTick = true ;
84+ private final Supplier <CompoundTag > tagToSaveSupplier ;
85+ private boolean supplyingTagToSave = false ;
86+ private final Supplier <String > idSupplier ;
7387
7488 private final List <IntList > stackTraces = new ArrayList <>();
7589 private IntList stackTracesThisTick = new IntArrayList ();
@@ -79,7 +93,39 @@ public class DebugRandom extends LegacyRandomSource {
7993
8094 public DebugRandom (Entity entity ) {
8195 super (RandomSupport .generateUniqueSeed ());
82- this .entity = entity ;
96+
97+ if (SAVE_ENTITY_TAG ) {
98+ tagToSaveSupplier = () -> {
99+ if (firstTick ) {
100+ return new CompoundTag ();
101+ } else {
102+ try (ProblemReporter .ScopedCollector collector = new ProblemReporter .ScopedCollector (LOGGER )) {
103+ TagValueOutput output = TagValueOutput .createWithContext (collector , entity .level ().registryAccess ());
104+ entity .saveWithoutId (output );
105+ return output .buildResult ();
106+ }
107+ }
108+ };
109+ } else {
110+ tagToSaveSupplier = CompoundTag ::new ;
111+ }
112+
113+ this .idSupplier = entity ::getStringUUID ;
114+
115+ this .stackTraces .add (this .stackTracesThisTick );
116+ try {
117+ this .nbtStream = new DataOutputStream (new GZIPOutputStream (gzippedNbt ));
118+ } catch (IOException e ) {
119+ throw new AssertionError (e );
120+ }
121+ }
122+
123+ public DebugRandom (Level level ) {
124+ super (RandomSupport .generateUniqueSeed ());
125+
126+ this .tagToSaveSupplier = CompoundTag ::new ;
127+ this .idSupplier = () -> level .dimension ().location ().toString ();
128+
83129 this .stackTraces .add (this .stackTracesThisTick );
84130 try {
85131 this .nbtStream = new DataOutputStream (new GZIPOutputStream (gzippedNbt ));
@@ -108,14 +154,17 @@ public void tick() {
108154
109155 private void handleStackTrace (int stackTrace ) {
110156 this .stackTracesThisTick .add (stackTrace );
111- try (ProblemReporter .ScopedCollector collector = new ProblemReporter .ScopedCollector (LOGGER )) {
157+ try {
158+ boolean wasSupplyingTagToSave = supplyingTagToSave ;
159+ supplyingTagToSave = true ;
112160 CompoundTag tagToSave ;
113- if (firstTick ) {
114- tagToSave = new CompoundTag ();
115- } else {
116- TagValueOutput output = TagValueOutput .createWithContext (collector , entity .level ().registryAccess ());
117- entity .saveWithoutId (output );
118- tagToSave = output .buildResult ();
161+ try {
162+ tagToSave = tagToSaveSupplier .get ();
163+ } finally {
164+ supplyingTagToSave = wasSupplyingTagToSave ;
165+ }
166+ if (supplyingTagToSave ) {
167+ tagToSave .putBoolean ("clientcommands:causedByTagSaving" , true );
119168 }
120169 NbtIo .writeUnnamedTagWithFallback (tagToSave , nbtStream );
121170 } catch (IOException e ) {
@@ -128,7 +177,7 @@ public void writeToFile() {
128177 this .nbtStream .close ();
129178 Path debugDir = ClientCommands .CONFIG_DIR .resolve ("debug" );
130179 Files .createDirectories (debugDir );
131- try (DataOutputStream dataOutput = new DataOutputStream (new GZIPOutputStream (Files .newOutputStream (debugDir .resolve (this .entity . getStringUUID () + ".dat" ))))) {
180+ try (DataOutputStream dataOutput = new DataOutputStream (new GZIPOutputStream (Files .newOutputStream (debugDir .resolve (this .idSupplier . get () + ".dat" ))))) {
132181 dataOutput .writeInt (stackTraceById .size ());
133182 for (String st : stackTraceById ) {
134183 dataOutput .writeUTF (st );
@@ -142,7 +191,7 @@ public void writeToFile() {
142191 }
143192 dataOutput .write (this .gzippedNbt .toByteArray ());
144193 }
145- LOGGER .info ("Written debug random for " + this .entity . getStringUUID () + " to file" );
194+ LOGGER .info ("Written debug random for {} to file" , this .idSupplier . get () );
146195 } catch (IOException e ) {
147196 LOGGER .error ("Error saving debug source to file" , e );
148197 }
@@ -193,7 +242,7 @@ public static void main(String[] args) {
193242 return ;
194243 }
195244
196- JFrame frame = new JFrame ("Debug Entity RNG" );
245+ JFrame frame = new JFrame ("Debug RNG" );
197246 frame .add (new DebugRandomSourcePanel (randomCalls ));
198247 frame .setDefaultCloseOperation (JFrame .DISPOSE_ON_CLOSE );
199248 frame .pack ();
@@ -251,6 +300,16 @@ class DebugRandomSourcePanel extends JPanel {
251300 if (value .stackTrace () == selectedStackTrace ) {
252301 textArea .setBackground (Color .YELLOW );
253302 }
303+
304+ if (value .nbt ().contains ("clientcommands:causedByTagSaving" )) {
305+ JPanel panel = new JPanel (new BorderLayout ());
306+ panel .add (textArea , BorderLayout .CENTER );
307+ JLabel warningLabel = new JLabel ("This stack trace was caused by saving the NBT tag during debug" );
308+ warningLabel .setForeground (Color .RED );
309+ panel .add (warningLabel , BorderLayout .NORTH );
310+ return panel ;
311+ }
312+
254313 return textArea ;
255314 });
256315 callsInTickList .addListSelectionListener (e -> {
0 commit comments