-
Notifications
You must be signed in to change notification settings - Fork 6
/
WorldSeed.java
96 lines (73 loc) · 2.95 KB
/
WorldSeed.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package kaptainwutax.seedutils.mc.seed;
import kaptainwutax.seedutils.prng.SeedMixer;
import kaptainwutax.seedutils.prng.lcg.LCG;
import kaptainwutax.seedutils.util.SeedIterator;
import kaptainwutax.seedutils.util.StringUnhasher;
import kaptainwutax.seedutils.util.math.Mth;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public final class WorldSeed {
public static long toPillarSeed(long worldSeed) {
return StructureSeed.toPillarSeed(toStructureSeed(worldSeed));
}
public static long toStructureSeed(long worldSeed) {
return worldSeed & Mth.MASK_48;
}
public static long getShadowSeed(long worldSeed) {
long nextSeed = SeedMixer.mixSeed(worldSeed, 0L);
return SeedMixer.solveRoot(-nextSeed, ~worldSeed & 1);
}
public static SeedIterator getSisterSeeds(long worldSeed) {
return StructureSeed.getWorldSeeds(toStructureSeed(worldSeed));
}
public static boolean isString(long worldSeed) {
return (int)worldSeed == worldSeed;
}
public static void toString(long worldSeed, StringUnhasher.Config config, Predicate<String> continueSearching) {
if(isString(worldSeed)) {
StringUnhasher.unhash((int)worldSeed, config, continueSearching);
}
}
public static boolean isRandom(long worldSeed) {
long upperBits = worldSeed >>> 32;
long lowerBits = worldSeed & Mth.MASK_32;
long a = (24667315L * upperBits + 18218081L * lowerBits + 67552711L) >> 32;
long b = (-4824621L * upperBits + 7847617L * lowerBits + 7847617L) >> 32;
long seed = 7847617L * a - 18218081L * b;
//Compute the nextLong() call fast without creating a JRand object.
long nextLong = (seed >>> 16 << 32) + (LCG.JAVA.nextSeed(seed) >>> 16);
return nextLong == worldSeed;
}
public static List<Long> fromHash(long structureSeed, long hashedWorldSeed) {
List<Long> worldSeeds = new ArrayList<>();
StructureSeed.getWorldSeeds(structureSeed).forEachRemaining(worldSeed -> {
if(WorldSeed.toHash(worldSeed) == hashedWorldSeed) {
worldSeeds.add(worldSeed);
}
});
return worldSeeds;
}
public static long toHash(long worldSeed) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return worldSeed;
}
byte[] bytes = new byte[8];
for(int i = 0; i < 8; i++) {
bytes[i] = (byte)(worldSeed & 0xFFL);
worldSeed >>>= 8;
}
bytes = digest.digest(bytes);
long hashedWorldSeed = bytes[0] & 0xFFL;
for(int i = 1; i < 8; i++) {
hashedWorldSeed |= (bytes[i] & 0xFFL) << (i << 3);
}
return hashedWorldSeed;
}
}