Skip to content

Commit 821096e

Browse files
committed
[KillTheRNG] Add entity randomness saving and loading
1 parent d7a15e2 commit 821096e

File tree

5 files changed

+90
-11
lines changed

5 files changed

+90
-11
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,15 @@ public boolean checkInit() {
297297
y += 14;
298298

299299
// if (TASmod.ktrngHandler.isLoaded()) {
300-
// title = "ktrng_randomseed";
301-
// if (configuration.getProperty(title + "_x", "err").equals("err"))
302-
// setDefaults(title, y);
303-
// lists.add(new InfoLabel(title, Integer.parseInt(configuration.getProperty(title + "_x")), Integer.parseInt(configuration.getProperty(title + "_y")), Boolean.parseBoolean(configuration.getProperty(title
304-
// + "_visible")), Boolean.parseBoolean(configuration.getProperty(title + "_rect")), () -> {
305-
// if (Minecraft.getMinecraft().currentScreen == this)
306-
// return "KTRNG";
307-
// return "RandomSeed: " + TASmod.ktrngHandler.getGlobalSeedClient();
308-
// }));
300+
title = "ktrng_randomseed";
301+
if (configuration.getProperty(title + "_x", "err").equals("err"))
302+
setDefaults(title, y);
303+
lists.add(new InfoLabel(title, Integer.parseInt(configuration.getProperty(title + "_x")), Integer.parseInt(configuration.getProperty(title + "_y")), Boolean.parseBoolean(configuration.getProperty(title
304+
+ "_visible")), Boolean.parseBoolean(configuration.getProperty(title + "_rect")), () -> {
305+
if (Minecraft.getMinecraft().currentScreen == this)
306+
return "KTRNG";
307+
return "Global RandomSeed: " + TASmod.globalRandomness.getCurrentSeed();
308+
}));
309309
// }
310310

311311
title = "facing";

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
public class EntityRandomness extends RandomBase {
66

7-
private static long entityCounter = 0L;
7+
public static long entityCounter = 0L;
88

99
public EntityRandomness() {
1010
super(TASmod.globalRandomness.getCurrentSeed() + (entityCounter++));
1111
}
12+
13+
public EntityRandomness(long seed) {
14+
super(seed);
15+
}
1216
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.minecrafttas.tasmod.ktrng;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.UUID;
6+
7+
import com.minecrafttas.tasmod.TASmod;
8+
9+
import net.minecraft.entity.Entity;
10+
import net.minecraft.world.WorldServer;
11+
12+
public class KTRNGEntityHandler {
13+
14+
public static Map<UUID, EntityRandomness> getRandomnessList() {
15+
Map<UUID, EntityRandomness> out = new HashMap<>();
16+
WorldServer[] worlds = TASmod.getServerInstance().worlds;
17+
for (WorldServer worldServer : worlds) {
18+
for (Entity entity : worldServer.loadedEntityList) {
19+
UUID entityUUID = entity.getUniqueID();
20+
EntityRandomness entityRandomness = (EntityRandomness) entity.rand;
21+
out.put(entityUUID, entityRandomness);
22+
}
23+
}
24+
return out;
25+
}
26+
27+
public static void setRandomnessList(Map<UUID, EntityRandomness> randomnessList) {
28+
WorldServer[] worlds = TASmod.getServerInstance().worlds;
29+
for (WorldServer worldServer : worlds) {
30+
for (Entity entity : worldServer.loadedEntityList) {
31+
UUID uuid = entity.getUniqueID();
32+
EntityRandomness rand = randomnessList.get(uuid);
33+
if (rand != null)
34+
entity.rand = rand;
35+
}
36+
}
37+
}
38+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public static long distance(long seed, long seed2) {
160160

161161
@Override
162162
public String toString() {
163-
return name + ": " + enabled;
163+
return Long.toString(getSeed());
164164
}
165165

166166
@Override

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package com.minecrafttas.tasmod.savestates.storage.builtin;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Map.Entry;
6+
import java.util.UUID;
7+
8+
import com.google.gson.JsonElement;
39
import com.google.gson.JsonObject;
410
import com.minecrafttas.tasmod.TASmod;
11+
import com.minecrafttas.tasmod.ktrng.EntityRandomness;
12+
import com.minecrafttas.tasmod.ktrng.KTRNGEntityHandler;
513
import com.minecrafttas.tasmod.savestates.storage.SavestateStorageExtensionBase;
614

715
import net.minecraft.server.MinecraftServer;
@@ -16,13 +24,42 @@ public KTRNGSeedStorage() {
1624
public JsonObject onSavestate(MinecraftServer server, JsonObject dataToSave) {
1725
long currentSeed = TASmod.globalRandomness.getCurrentSeed();
1826
dataToSave.addProperty("globalSeed", currentSeed);
27+
28+
JsonObject entityRandomDataJson = new JsonObject();
29+
entityRandomDataJson.addProperty("entityCount", EntityRandomness.entityCounter);
30+
31+
JsonObject entityRandomListJson = new JsonObject();
32+
Map<UUID, EntityRandomness> randomList = KTRNGEntityHandler.getRandomnessList();
33+
for (Entry<UUID, EntityRandomness> entry : randomList.entrySet()) {
34+
entityRandomListJson.addProperty(entry.getKey().toString(), entry.getValue().getSeed());
35+
}
36+
37+
entityRandomDataJson.add("entityList", entityRandomListJson);
38+
39+
dataToSave.add("entityRandom", entityRandomDataJson);
40+
1941
return dataToSave;
2042
}
2143

2244
@Override
2345
public void onLoadstate(MinecraftServer server, JsonObject loadedData) {
2446
long newSeed = loadedData.get("globalSeed").getAsLong();
2547
TASmod.globalRandomness.setSeed(newSeed);
48+
49+
JsonObject entityRandomDataJson = loadedData.get("entityRandom").getAsJsonObject();
50+
EntityRandomness.entityCounter = entityRandomDataJson.get("entityCount").getAsLong();
51+
52+
JsonObject entityRandomListJson = entityRandomDataJson.get("entityList").getAsJsonObject();
53+
54+
Map<UUID, EntityRandomness> randomList = new HashMap<>();
55+
for (Entry<String, JsonElement> entry : entityRandomListJson.entrySet()) {
56+
UUID uuid = UUID.fromString(entry.getKey().toString());
57+
EntityRandomness entityRandomness = new EntityRandomness(entry.getValue().getAsLong());
58+
59+
randomList.put(uuid, entityRandomness);
60+
}
61+
62+
KTRNGEntityHandler.setRandomnessList(randomList);
2663
}
2764

2865
@Override

0 commit comments

Comments
 (0)