Skip to content

Commit ce393b1

Browse files
committed
[KillTheRNG] Make UUIDs deterministic
1 parent a83ebf5 commit ce393b1

10 files changed

Lines changed: 99 additions & 26 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.minecrafttas.tasmod.ktrng.builtin.MathRNG;
3434
import com.minecrafttas.tasmod.ktrng.builtin.WorldSeedRNG;
3535
import com.minecrafttas.tasmod.ktrng.events.KillTheRNGMonitor;
36+
import com.minecrafttas.tasmod.ktrng.handlers.UUIDHandler;
3637
import com.minecrafttas.tasmod.playback.PlaybackControllerServer;
3738
import com.minecrafttas.tasmod.playback.metadata.builtin.StartpositionMetadataExtension;
3839
import com.minecrafttas.tasmod.registries.TASmodAPIRegistry;
@@ -109,6 +110,8 @@ public class TASmod implements ModInitializer, EventServerStart, EventServerInit
109110

110111
public static KillTheRNGMonitor debugRand = new KillTheRNGMonitor();
111112

113+
public static UUIDHandler uuidHandler = new UUIDHandler();
114+
112115
public static Configuration config;
113116

114117
@Override
@@ -155,6 +158,7 @@ public void onInitialize() {
155158
EventListenerRegistry.register(resourcepackHandler);
156159
PacketHandlerRegistry.register(playUntil);
157160
EventListenerRegistry.register(playUntil);
161+
EventListenerRegistry.register(uuidHandler);
158162
EventListenerRegistry.register(TASmodAPIRegistry.SAVESTATE_STORAGE);
159163

160164
registerSavestateStorage();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
public abstract class RandomBase extends Random implements Registerable {
1414

15-
RNGSide side;
16-
private long initialSeed;
17-
private JRand jrand;
15+
protected RNGSide side;
16+
protected long initialSeed;
17+
protected JRand jrand;
1818

1919
public RandomBase() {
2020
super(TASmod.globalRandomness.getCurrentSeed());

src/main/java/com/minecrafttas/tasmod/ktrng/builtin/EntityRNG.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
package com.minecrafttas.tasmod.ktrng.builtin;
22

3+
import com.minecrafttas.mctcommon.events.EventListenerRegistry;
4+
import com.minecrafttas.tasmod.events.EventKillTheRNGServer;
35
import com.minecrafttas.tasmod.ktrng.RandomBase;
46

7+
import net.minecraft.entity.Entity;
8+
59
public class EntityRNG extends RandomBase {
610

7-
public EntityRNG() {
11+
private Entity entity;
12+
13+
public EntityRNG(Entity entity) {
814
super();
15+
this.entity = entity;
916
}
1017

11-
public EntityRNG(long seed) {
18+
public EntityRNG(long seed, Entity entity) {
1219
super(seed);
20+
this.entity = entity;
1321
}
1422

1523
@Override
1624
public void fireRNGEvent(String eventType, long seed, String value, int stackTraceOffset) {
17-
super.fireRNGEvent(eventType, seed, value, stackTraceOffset);
25+
String rngType = String.format("%s(%s)", entity.getClass().getSimpleName(), entity.getUniqueID());
26+
EventListenerRegistry.fireEvent(EventKillTheRNGServer.EventRNG.class, super.side, eventType, seed, value, rngType, stackTraceOffset);
1827
}
1928

2029
@Override
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.minecrafttas.tasmod.ktrng.builtin;
2+
3+
import com.minecrafttas.tasmod.TASmod;
4+
import com.minecrafttas.tasmod.ktrng.RandomBase;
5+
6+
public class UUIDRNG extends RandomBase {
7+
8+
public static int uuidcounter;
9+
10+
public UUIDRNG(int shift) {
11+
super(TASmod.globalRandomness.getCurrentSeed() + shift);
12+
}
13+
14+
@Override
15+
public void fireRNGEvent(String eventType, long seed, String value, int stackTraceOffset) {
16+
// TODO Auto-generated method stub
17+
// super.fireRNGEvent(eventType, seed, value, stackTraceOffset);
18+
}
19+
20+
@Override
21+
public String getExtensionName() {
22+
return "UUIDRNG";
23+
}
24+
25+
}

src/main/java/com/minecrafttas/tasmod/ktrng/handlers/KTRNGEntityHandler.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,4 @@ public static Map<UUID, EntityRNG> getRandomnessList() {
2424
}
2525
return out;
2626
}
27-
28-
public static void setRandomnessList(Map<UUID, EntityRNG> randomnessList) {
29-
WorldServer[] worlds = TASmod.getServerInstance().worlds;
30-
for (WorldServer worldServer : worlds) {
31-
for (Entity entity : worldServer.loadedEntityList) {
32-
UUID uuid = entity.getUniqueID();
33-
EntityRNG rand = randomnessList.get(uuid);
34-
if (rand != null)
35-
entity.rand = rand;
36-
}
37-
}
38-
}
3927
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.minecrafttas.tasmod.ktrng.handlers;
2+
3+
import java.util.UUID;
4+
5+
import com.minecrafttas.mctcommon.events.EventServer.EventServerTick;
6+
import com.minecrafttas.tasmod.ktrng.builtin.UUIDRNG;
7+
8+
import net.minecraft.server.MinecraftServer;
9+
import net.minecraft.util.math.MathHelper;
10+
11+
/**
12+
* <p>Generates and distributes UUIDs deterministically
13+
* <p>This is necessary since the RNG is deterministic and would result in duplicated UUIDs
14+
*
15+
* @author Scribble
16+
*/
17+
public class UUIDHandler implements EventServerTick {
18+
19+
private int uuidIndex;
20+
21+
public UUIDHandler() {
22+
}
23+
24+
@Override
25+
public void onServerTick(MinecraftServer server) {
26+
uuidIndex = -1;
27+
}
28+
29+
public UUIDRNG getNewUUIDRNG() {
30+
return new UUIDRNG(uuidIndex++);
31+
}
32+
33+
public UUID getNewUUID() {
34+
return MathHelper.getRandomUUID(getNewUUIDRNG());
35+
}
36+
}

src/main/java/com/minecrafttas/tasmod/mixin/killtherng/MixinEntity.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
1010
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
1111
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
12+
import com.minecrafttas.tasmod.TASmod;
1213
import com.minecrafttas.tasmod.ktrng.builtin.EntityRNG;
1314

1415
import net.minecraft.entity.Entity;
@@ -20,13 +21,16 @@ public class MixinEntity {
2021
@ModifyExpressionValue(method = "<init>", at = @At(value = "NEW", target = "Ljava/util/Random;"))
2122
public Random modify_entityRandom(Random original, World world) {
2223
if (!world.isRemote) {
23-
return new EntityRNG();
24+
return new EntityRNG((Entity) (Object) this);
2425
}
2526
return original;
2627
}
2728

2829
@WrapOperation(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/MathHelper;getRandomUUID(Ljava/util/Random;)Ljava/util/UUID;"))
29-
private UUID wrap_getRandomUUID(Random rand, Operation<UUID> original) {
30+
private UUID wrap_getRandomUUID(Random rand, Operation<UUID> original, World world) {
31+
// return original.call(TASmod.uuidHandler.getNewUUID());
32+
if (!world.isRemote)
33+
return TASmod.uuidHandler.getNewUUID();
3034
return original.call(new Random());
3135
}
3236
}

src/main/java/com/minecrafttas/tasmod/mixin/killtherng/MixinRenderLivingBase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void inject_renderName(EntityLivingBase entity, double d, double e, doubl
4141
this.renderEntityName(entity, d, e + 0.69D, f, Long.toString(random.getInitialSeed()), 64);
4242
this.renderEntityName(entity, d, e + 0.46D, f, Long.toString(seed), 64);
4343
this.renderEntityName(entity, d, e + 0.23D, f, Long.toString(distance), 64);
44+
this.renderEntityName(entity, d, e, f, entity.getUniqueID().toString(), 64);
4445
}
4546
}
4647
}

src/main/java/com/minecrafttas/tasmod/savestates/storage/builtin/KTRNGSeedStorage.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import com.minecrafttas.tasmod.ktrng.handlers.KTRNGWorldHandler;
1515
import com.minecrafttas.tasmod.savestates.storage.SavestateStorageExtensionBase;
1616

17+
import net.minecraft.entity.Entity;
1718
import net.minecraft.server.MinecraftServer;
19+
import net.minecraft.world.WorldServer;
1820

1921
public class KTRNGSeedStorage extends SavestateStorageExtensionBase {
2022

@@ -74,16 +76,19 @@ public void onLoadstatePost(MinecraftServer server, JsonObject loadedData) {
7476

7577
JsonObject entityRandomListJson = entityRandomDataJson.get("entityList").getAsJsonObject();
7678

77-
Map<UUID, EntityRNG> randomList = new HashMap<>();
7879
for (Entry<String, JsonElement> entry : entityRandomListJson.entrySet()) {
80+
WorldServer[] worlds = TASmod.getServerInstance().worlds;
7981
UUID uuid = UUID.fromString(entry.getKey().toString());
80-
EntityRNG entityRandomness = new EntityRNG(entry.getValue().getAsLong());
82+
for (WorldServer worldServer : worlds) {
83+
Entity entity = worldServer.getEntityFromUuid(uuid);
84+
if (entity == null)
85+
continue;
86+
EntityRNG entityRandomness = new EntityRNG(entry.getValue().getAsLong(), entity);
87+
entity.rand = entityRandomness;
88+
}
8189

82-
randomList.put(uuid, entityRandomness);
8390
}
8491

85-
KTRNGEntityHandler.setRandomnessList(randomList);
86-
8792
JsonObject worldRandomJson = loadedData.get("worldRandom").getAsJsonObject();
8893
JsonObject worldListJson = worldRandomJson.get("worldList").getAsJsonObject();
8994

src/main/resources/tasmod.accesswidener

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ mutable field net/minecraft/world/World rand Ljava/util/Random;
3131
accessible field net/minecraft/entity/EntityLiving tasks Lnet/minecraft/entity/ai/EntityAITasks;
3232
accessible field net/minecraft/entity/EntityLiving targetTasks Lnet/minecraft/entity/ai/EntityAITasks;
3333
accessible field net/minecraft/entity/ai/EntityAITasks tickCount I
34-
accessible field net/minecraft/entity/passive/EntityBat spawnPosition Lnet/minecraft/util/math/BlockPos;
34+
accessible field net/minecraft/entity/passive/EntityBat spawnPosition Lnet/minecraft/util/math/BlockPos;
35+
accessible field net/minecraft/entity/Entity nextEntityID I

0 commit comments

Comments
 (0)