Skip to content

Commit 6b1e8e5

Browse files
authored
[KillTheRNG] Made WorldRNG more robust (#293)
Fixed a multitude of WorldRNG desyncs by fixing savestates, playback and sorting the PlayerChunkMap.entries list, so that tick updates are always consistent - **[Savestates] Fix config option PauseOnTempSavestate being reversed** - **[Savestates] Fix motion being reset on temp savestates** - **[KillTheRNG] Add seed display to more entities** This mainly adds the seed display to entityitems - **[KillTheRNG] Fix globalTimer not setting the seed properly** - **[KillTheRNG] Replace Math.random() with instanced randoms** This essentially removes Math.random from the code. I will keep this until the client RNG is also fixed - **[Gui] Add blockHitDelay to infohud** Another possible desync reason. Will be stored in the savestate in the future - **[Playback] Fix playback being one tick delayed** - **[Savestates] Remove loadstates disabeling temp savestates** - **[KillTheRNG] Add updateLCG to monitoring** Update LCG is a weird additional source of random in World. - **[KillTheRNG] Add stacktrace offset to rng monitor** - **[Savestates] Add sorting by chunk to PlayerChunkMap.entries** Sorts chunks as soon as they are added to the chunk map. - **[KillTheRNG] Rename "Randomness" files to RNG**
2 parents f7fbccd + c8b2133 commit 6b1e8e5

39 files changed

Lines changed: 379 additions & 160 deletions

src/main/java/com/minecrafttas/tasmod/TASmod.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
import com.minecrafttas.tasmod.commands.CommandTickrate;
3030
import com.minecrafttas.tasmod.config.TASmodServerConfig;
3131
import com.minecrafttas.tasmod.handlers.PlayUntilHandler;
32-
import com.minecrafttas.tasmod.ktrng.GlobalRandomnessTimer;
33-
import com.minecrafttas.tasmod.ktrng.builtin.MathRandomness;
34-
import com.minecrafttas.tasmod.ktrng.builtin.WorldSeedRandomness;
32+
import com.minecrafttas.tasmod.ktrng.GlobalRNG;
33+
import com.minecrafttas.tasmod.ktrng.builtin.MathRNG;
34+
import com.minecrafttas.tasmod.ktrng.builtin.WorldSeedRNG;
3535
import com.minecrafttas.tasmod.ktrng.events.KillTheRNGMonitor;
3636
import com.minecrafttas.tasmod.playback.PlaybackControllerServer;
3737
import com.minecrafttas.tasmod.playback.metadata.builtin.StartpositionMetadataExtension;
@@ -95,13 +95,13 @@ public class TASmod implements ModInitializer, EventServerStart, EventServerInit
9595

9696
public static ClientMotionStorage motionStorage = new ClientMotionStorage();
9797

98-
public static GlobalRandomnessTimer globalRandomness;
98+
public static GlobalRNG globalRandomness;
9999

100100
public static KTRNGSeedStorage seedStorage = new KTRNGSeedStorage();
101101

102-
public static MathRandomness mathRandomness = new MathRandomness(0);
102+
public static MathRNG mathRandomness = new MathRNG(0);
103103

104-
public static WorldSeedRandomness worldSeedRandomness = new WorldSeedRandomness(0);
104+
public static WorldSeedRNG worldSeedRandomness = new WorldSeedRNG(0);
105105

106106
public static KillTheRNGMonitor debugRand = new KillTheRNGMonitor();
107107

@@ -158,9 +158,9 @@ public void onInitialize() {
158158

159159
@Override
160160
public void onServerStart(MinecraftServer server) {
161-
globalRandomness = new GlobalRandomnessTimer();
161+
globalRandomness = new GlobalRNG();
162162
EventListenerRegistry.register(globalRandomness);
163-
mathRandomness = new MathRandomness(0);
163+
mathRandomness = new MathRNG(0);
164164
}
165165

166166
@Override

src/main/java/com/minecrafttas/tasmod/events/EventKillTheRNGServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public interface EventKillTheRNGServer {
77

88
@FunctionalInterface
99
public interface EventRNG extends EventBase {
10-
public void onRNGCall(RNGSide side, String eventType, long seed, String value, String rngClass);
10+
public void onRNGCall(RNGSide side, String eventType, long seed, String value, String rngClass, int offset);
1111
}
1212
}

src/main/java/com/minecrafttas/tasmod/gui/InfoHud.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,14 @@ public boolean checkInit() {
329329
}));
330330

331331
y += 14;
332-
title = "ktrng_mathseed";
332+
title = "ktrng_blockHitDelay";
333333
if (configuration.getProperty(title + "_x", "err").equals("err"))
334334
setDefaults(title, y);
335335
lists.add(new InfoLabel(title, Integer.parseInt(configuration.getProperty(title + "_x")), Integer.parseInt(configuration.getProperty(title + "_y")), Boolean.parseBoolean(configuration.getProperty(title
336336
+ "_visible")), Boolean.parseBoolean(configuration.getProperty(title + "_rect")), () -> {
337337
if (Minecraft.getMinecraft().currentScreen == this)
338-
return "MathRNG";
339-
return "MathRNG: " + TASmod.mathRandomness.getSeed();
338+
return "BlockHitDelay";
339+
return "BlockHitDelay: " + Minecraft.getMinecraft().playerController.blockHitDelay;
340340
}));
341341

342342
title = "facing";

src/main/java/com/minecrafttas/tasmod/ktrng/GlobalRandomnessTimer.java renamed to src/main/java/com/minecrafttas/tasmod/ktrng/GlobalRNG.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,33 @@
55
import kaptainwutax.seedutils.rand.JRand;
66
import net.minecraft.server.MinecraftServer;
77

8-
public class GlobalRandomnessTimer implements EventServer.EventServerTick {
8+
public class GlobalRNG implements EventServer.EventServerTick {
99

10-
private final JRand globalRandomness;
10+
private final JRand globalRNG;
1111

1212
private long currentSeed = 0L;
1313

14-
public GlobalRandomnessTimer() {
15-
globalRandomness = new JRand(0L);
14+
public GlobalRNG() {
15+
globalRNG = new JRand(0L, false);
1616
}
1717

1818
@Override
1919
public void onServerTick(MinecraftServer server) {
20-
globalRandomness.advance(1);
21-
currentSeed = globalRandomness.getSeed();
20+
globalRNG.advance(1);
21+
currentSeed = globalRNG.getSeed();
2222
}
2323

2424
public long getCurrentSeed() {
2525
return currentSeed;
2626
}
2727

2828
public void setSeed(long newSeed) {
29-
globalRandomness.setSeed(newSeed);
29+
globalRNG.setSeed(newSeed, false);
3030
currentSeed = newSeed;
3131
}
32+
33+
@Override
34+
public String toString() {
35+
return Long.toString(globalRNG.getSeed());
36+
}
3237
}

src/main/java/com/minecrafttas/tasmod/ktrng/GlobalRandomnessTimerClient.java renamed to src/main/java/com/minecrafttas/tasmod/ktrng/GlobalRNGClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
import kaptainwutax.seedutils.rand.JRand;
66
import net.minecraft.client.Minecraft;
77

8-
public class GlobalRandomnessTimerClient implements EventClient.EventClientTick {
8+
public class GlobalRNGClient implements EventClient.EventClientTick {
99

10-
private final JRand globalRandomness;
10+
private final JRand globalRNGClient;
1111
private final JRand uuidRandomness;
1212

1313
private long currentSeed = 0L;
1414

15-
public GlobalRandomnessTimerClient() {
16-
globalRandomness = new JRand(0L);
15+
public GlobalRNGClient() {
16+
globalRNGClient = new JRand(0L);
1717
uuidRandomness = new JRand(0L);
1818
}
1919

2020
@Override
2121
public void onClientTick(Minecraft mc) {
22-
currentSeed = globalRandomness.nextLong();
22+
currentSeed = globalRNGClient.nextLong();
2323
uuidRandomness.setSeed(currentSeed);
2424
}
2525

@@ -32,7 +32,7 @@ public JRand getUUIDRandom() {
3232
}
3333

3434
public void setSeed(long newSeed) {
35-
globalRandomness.setSeed(newSeed);
35+
globalRNGClient.setSeed(newSeed, false);
3636
currentSeed = newSeed;
3737
}
3838

src/main/java/com/minecrafttas/tasmod/ktrng/RandomBase.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public abstract class RandomBase extends Random implements Registerable {
1818

1919
public RandomBase() {
2020
super(TASmod.globalRandomness.getCurrentSeed());
21-
jrand = new JRand(TASmod.globalRandomness.getCurrentSeed());
21+
initialSeed = TASmod.globalRandomness.getCurrentSeed();
22+
jrand = new JRand(initialSeed);
2223
}
2324

2425
public RandomBase(long seed) {
@@ -150,15 +151,15 @@ public void fireSetEvent(String eventType, long seed, String value, int stackTra
150151
}
151152

152153
public void fireGetEvent(String eventType, long seed, String value) {
153-
fireRNGEvent(eventType, seed, value, 3);
154+
fireRNGEvent(eventType, seed, value, 10);
154155
}
155156

156157
public long getInitialSeed() {
157158
return initialSeed;
158159
}
159160

160161
public void fireRNGEvent(String eventType, long seed, String value, int stackTraceOffset) {
161-
EventListenerRegistry.fireEvent(EventKillTheRNGServer.EventRNG.class, side, eventType, seed, value, this.getClass().getSimpleName());
162+
EventListenerRegistry.fireEvent(EventKillTheRNGServer.EventRNG.class, side, eventType, seed, value, this.getClass().getSimpleName(), stackTraceOffset);
162163
}
163164

164165
public enum RNGSide {

src/main/java/com/minecrafttas/tasmod/ktrng/builtin/EntityRandomness.java renamed to src/main/java/com/minecrafttas/tasmod/ktrng/builtin/EntityRNG.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
import com.minecrafttas.tasmod.ktrng.RandomBase;
44

5-
public class EntityRandomness extends RandomBase {
5+
public class EntityRNG extends RandomBase {
66

7-
public EntityRandomness() {
7+
public EntityRNG() {
88
super();
99
}
1010

11-
public EntityRandomness(long seed) {
11+
public EntityRNG(long seed) {
1212
super(seed);
1313
}
1414

1515
@Override
1616
public void fireRNGEvent(String eventType, long seed, String value, int stackTraceOffset) {
17-
// super.fireEvent(eventType, seed, value, stackTraceOffset);
1817
}
1918

2019
@Override

src/main/java/com/minecrafttas/tasmod/ktrng/builtin/MathRandomness.java renamed to src/main/java/com/minecrafttas/tasmod/ktrng/builtin/MathRNG.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,16 @@
77
*
88
* @author Scribble
99
*/
10-
public class MathRandomness extends RandomBase {
10+
public class MathRNG extends RandomBase {
1111

12-
public MathRandomness() {
12+
public MathRNG() {
1313
super();
1414
}
1515

16-
public MathRandomness(long seed) {
16+
public MathRNG(long seed) {
1717
super(seed);
1818
}
1919

20-
@Override
21-
public void fireRNGEvent(String eventType, long seed, String value, int stackTraceOffset) {
22-
// super.fireEvent(eventType, seed, value, 6);
23-
}
24-
2520
@Override
2621
public String getExtensionName() {
2722
return "MathRNG";

src/main/java/com/minecrafttas/tasmod/ktrng/builtin/WorldRandomness.java renamed to src/main/java/com/minecrafttas/tasmod/ktrng/builtin/WorldRNG.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
import com.minecrafttas.tasmod.ktrng.RandomBase;
44

5-
public class WorldRandomness extends RandomBase {
5+
public class WorldRNG extends RandomBase {
66

7-
public WorldRandomness() {
7+
public WorldRNG() {
88
super();
99
}
1010

11-
public WorldRandomness(long seed) {
11+
public WorldRNG(long seed) {
1212
super(seed);
1313
}
1414

1515
@Override
1616
public void fireRNGEvent(String val, long seed, String value, int offset) {
17-
super.fireRNGEvent(val, seed, value, 5);
17+
super.fireRNGEvent(val, seed, value, 9);
1818
}
1919

2020
@Override

src/main/java/com/minecrafttas/tasmod/ktrng/builtin/WorldSeedRandomness.java renamed to src/main/java/com/minecrafttas/tasmod/ktrng/builtin/WorldSeedRNG.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import com.minecrafttas.tasmod.ktrng.RandomBase;
44

5-
public class WorldSeedRandomness extends RandomBase {
5+
public class WorldSeedRNG extends RandomBase {
66

7-
public WorldSeedRandomness() {
7+
public WorldSeedRNG() {
88
super();
99
}
1010

11-
public WorldSeedRandomness(long seed) {
11+
public WorldSeedRNG(long seed) {
1212
super(seed);
1313
}
1414

0 commit comments

Comments
 (0)