Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ plugins {
// loom plugin
id "fabric-loom" version "${loom_version}"
id "ploceus" version "${loom_version}"
id "com.palantir.git-version" version "4.2.0"
id "com.diffplug.spotless" version "8.1.0"
id "com.diffplug.spotless" version "8.6.0"
}

// set basic properties
def hash = ""
if(project.release=="false") {
hash = "-SNAPSHOT_"+versionDetails().gitHash.substring(0,7)
hash = "-SNAPSHOT_"+getGitCommitHash()
}
version = project.version+hash
group = project.group
Expand Down Expand Up @@ -109,4 +108,22 @@ spotless {
eclipse().configFile("formatter/TASmodFormatter.xml")
}
enforceCheck false
}

def getGitCommitHash() {
def gitFolder = "$projectDir/.git/"
def takeFromHash = 7
/*
* '.git/HEAD' contains either
* in case of detached head: the currently checked out commit hash
* otherwise: a reference to a file containing the current commit hash
*/
def head = new File(gitFolder + "HEAD").text.split(":") // .git/HEAD
def isCommit = head.length == 1
// def isRef = head.length > 1 // ref: refs/heads/master

if(isCommit) return head[0].trim().take(takeFromHash) // e5a7c79edabb

def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master
refHead.text.trim().take takeFromHash
}
19 changes: 17 additions & 2 deletions src/main/java/com/minecrafttas/tasmod/TASmod.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.minecrafttas.tasmod.ktrng.builtin.MathRNG;
import com.minecrafttas.tasmod.ktrng.builtin.WorldSeedRNG;
import com.minecrafttas.tasmod.ktrng.events.KillTheRNGMonitor;
import com.minecrafttas.tasmod.ktrng.handlers.UUIDHandler;
import com.minecrafttas.tasmod.playback.PlaybackControllerServer;
import com.minecrafttas.tasmod.playback.metadata.builtin.StartpositionMetadataExtension;
import com.minecrafttas.tasmod.registries.TASmodAPIRegistry;
Expand All @@ -41,6 +42,9 @@
import com.minecrafttas.tasmod.savestates.handlers.SavestateGuiHandlerServer;
import com.minecrafttas.tasmod.savestates.handlers.SavestateResourcePackHandler;
import com.minecrafttas.tasmod.savestates.storage.builtin.ClientMotionStorage;
import com.minecrafttas.tasmod.savestates.storage.builtin.EntityBatSpawnPositionStorage;
import com.minecrafttas.tasmod.savestates.storage.builtin.EntitySquidRotationStorage;
import com.minecrafttas.tasmod.savestates.storage.builtin.EntityTickTimersStorage;
import com.minecrafttas.tasmod.savestates.storage.builtin.KTRNGSeedStorage;
import com.minecrafttas.tasmod.tickratechanger.TickrateChangerServer;
import com.minecrafttas.tasmod.ticksync.TickSyncServer;
Expand Down Expand Up @@ -98,13 +102,18 @@ public class TASmod implements ModInitializer, EventServerStart, EventServerInit
public static GlobalRNG globalRandomness;

public static KTRNGSeedStorage seedStorage = new KTRNGSeedStorage();
public static EntityTickTimersStorage entityTickTimers = new EntityTickTimersStorage();
public static EntityBatSpawnPositionStorage entityBatSpawnPositionStorage = new EntityBatSpawnPositionStorage();
public static EntitySquidRotationStorage entitySquidRotationStorage = new EntitySquidRotationStorage();

public static MathRNG mathRandomness = new MathRNG(0);

public static WorldSeedRNG worldSeedRandomness = new WorldSeedRNG(0);

public static KillTheRNGMonitor debugRand = new KillTheRNGMonitor();

public static UUIDHandler uuidHandler = new UUIDHandler();

public static Configuration config;

@Override
Expand Down Expand Up @@ -151,15 +160,18 @@ public void onInitialize() {
EventListenerRegistry.register(resourcepackHandler);
PacketHandlerRegistry.register(playUntil);
EventListenerRegistry.register(playUntil);
EventListenerRegistry.register(uuidHandler);
EventListenerRegistry.register(TASmodAPIRegistry.SAVESTATE_STORAGE);

registerSavestateStorage();
}

@Override
public void onServerStart(MinecraftServer server) {
globalRandomness = new GlobalRNG();
EventListenerRegistry.register(globalRandomness);
if (globalRandomness == null) {
globalRandomness = new GlobalRNG();
EventListenerRegistry.register(globalRandomness);
}
mathRandomness = new MathRNG(0);
}

Expand Down Expand Up @@ -227,6 +239,9 @@ public void onServerStop(MinecraftServer mcserver) {
private void registerSavestateStorage() {
TASmodAPIRegistry.SAVESTATE_STORAGE.register(motionStorage);
TASmodAPIRegistry.SAVESTATE_STORAGE.register(seedStorage);
TASmodAPIRegistry.SAVESTATE_STORAGE.register(entityTickTimers);
TASmodAPIRegistry.SAVESTATE_STORAGE.register(entityBatSpawnPositionStorage);
TASmodAPIRegistry.SAVESTATE_STORAGE.register(entitySquidRotationStorage);
}

public static MinecraftServer getServerInstance() {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/minecrafttas/tasmod/ktrng/GlobalRNG.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
import kaptainwutax.seedutils.rand.JRand;
import net.minecraft.server.MinecraftServer;

/**
* <p>Special RNG generating seeds for each new rng instance.
* <p>This RNG advances to it's next seed every server tick. Any new RNG created then uses the {@link #currentSeed},<br>
* which makes it have a deterministic starting seed
*
* @author Scribble
*/
public class GlobalRNG implements EventServer.EventServerTick {

private final JRand globalRNG;
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/com/minecrafttas/tasmod/ktrng/RandomBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@
import kaptainwutax.seedutils.lcg.LCG;
import kaptainwutax.seedutils.rand.JRand;

/**
* Base RNG class extending the {@link Random} class, but designed to be easily modifyable and monitorable.
*
* @author Scribble
*/
public abstract class RandomBase extends Random implements Registerable {

RNGSide side;
private long initialSeed;
private JRand jrand;
protected RNGSide side;
protected long initialSeed;
protected JRand jrand;

public RandomBase() {
super(TASmod.globalRandomness.getCurrentSeed());
initialSeed = TASmod.globalRandomness.getCurrentSeed();
jrand = new JRand(initialSeed);
jrand = new JRand(initialSeed, false);
}

public RandomBase(long seed) {
Expand All @@ -31,9 +36,9 @@ public RandomBase(long seed) {
@Override
public void setSeed(long seedIn) {
super.setSeed(seedIn);
if (jrand != null)
if (jrand != null) {
jrand.setSeed(seedIn, false);
// super.setSeed(seedIn ^ 0x5deece66dL);
}
}

public long getSeed() {
Expand Down Expand Up @@ -146,8 +151,8 @@ public String toString() {
return Long.toString(getSeed());
}

public void fireSetEvent(String eventType, long seed, String value, int stackTraceOffset) {
fireRNGEvent(eventType, seed, value, stackTraceOffset);
public void fireSetEvent(String eventType, long seed, String value) {
// fireRNGEvent(eventType, seed, value, stackTraceOffset);
}

public void fireGetEvent(String eventType, long seed, String value) {
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/minecrafttas/tasmod/ktrng/builtin/EntityRNG.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
package com.minecrafttas.tasmod.ktrng.builtin;

import com.minecrafttas.mctcommon.events.EventListenerRegistry;
import com.minecrafttas.tasmod.events.EventKillTheRNGServer;
import com.minecrafttas.tasmod.ktrng.RandomBase;

import net.minecraft.entity.Entity;

/**
* Custom RNG making {@link Entity#rand} deterministic
*
* @author Scribble
*/
public class EntityRNG extends RandomBase {

public EntityRNG() {
private Entity entity;

public EntityRNG(Entity entity) {
super();
this.entity = entity;
}

public EntityRNG(long seed) {
public EntityRNG(long seed, Entity entity) {
super(seed);
this.entity = entity;
}

@Override
public void fireRNGEvent(String eventType, long seed, String value, int stackTraceOffset) {
String rngType = String.format("%s(%s)", entity.getClass().getSimpleName(), entity.getUniqueID());
EventListenerRegistry.fireEvent(EventKillTheRNGServer.EventRNG.class, super.side, eventType, seed, value, rngType, 9);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.minecrafttas.tasmod.ktrng.RandomBase;

/**
* <p>Randomness instance for hooking into {@link Math#random()}
* Custom RNG making {@link Math#random()} deterministic
*
* @author Scribble
*/
Expand All @@ -17,6 +17,11 @@ public MathRNG(long seed) {
super(seed);
}

@Override
public void fireRNGEvent(String eventType, long seed, String value, int stackTraceOffset) {
// super.fireRNGEvent(eventType, seed, value, stackTraceOffset);
}

@Override
public String getExtensionName() {
return "MathRNG";
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/minecrafttas/tasmod/ktrng/builtin/UUIDRNG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.minecrafttas.tasmod.ktrng.builtin;

import com.minecrafttas.tasmod.TASmod;
import com.minecrafttas.tasmod.ktrng.RandomBase;

import net.minecraft.entity.Entity;

/**
* Custom RNG making {@link Entity#entityUniqueID} deterministic
*
* @author Scribble
*/
public class UUIDRNG extends RandomBase {

public static int uuidcounter;

public UUIDRNG(int shift) {
super(TASmod.globalRandomness.getCurrentSeed() + shift);
}

@Override
public void fireRNGEvent(String eventType, long seed, String value, int stackTraceOffset) {
// super.fireRNGEvent(eventType, seed, value, stackTraceOffset);
}

@Override
public String getExtensionName() {
return "UUIDRNG";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import com.minecrafttas.tasmod.ktrng.RandomBase;

import net.minecraft.world.World;

/**
* Custom RNG making {@link World#rand} deterministic
*
* @author Scribble
*/
public class WorldRNG extends RandomBase {

public WorldRNG() {
Expand All @@ -14,7 +21,7 @@ public WorldRNG(long seed) {

@Override
public void fireRNGEvent(String val, long seed, String value, int offset) {
super.fireRNGEvent(val, seed, value, 9);
// super.fireRNGEvent(val, seed, value, 9);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import com.minecrafttas.tasmod.ktrng.RandomBase;

import net.minecraft.world.World;

/**
* Custom RNG separating {@link World#setRandomSeed(int, int, int) worldseed setting behaviour} from the normal RNG
*
* @author Scribble
*/
public class WorldSeedRNG extends RandomBase {

public WorldSeedRNG() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.entity.Entity;
import net.minecraft.world.WorldServer;

@Deprecated
public class KTRNGEntityHandler {

public static Map<UUID, EntityRNG> getRandomnessList() {
Expand All @@ -24,16 +25,4 @@ public static Map<UUID, EntityRNG> getRandomnessList() {
}
return out;
}

public static void setRandomnessList(Map<UUID, EntityRNG> randomnessList) {
WorldServer[] worlds = TASmod.getServerInstance().worlds;
for (WorldServer worldServer : worlds) {
for (Entity entity : worldServer.loadedEntityList) {
UUID uuid = entity.getUniqueID();
EntityRNG rand = randomnessList.get(uuid);
if (rand != null)
entity.rand = rand;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import net.minecraft.world.WorldServer;

@Deprecated
public class KTRNGWorldHandler {

public static Map<Integer, WorldRNG> getWorldRandomnessMap() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.minecrafttas.tasmod.ktrng.handlers;

import java.util.UUID;

import com.minecrafttas.mctcommon.events.EventServer.EventServerTick;
import com.minecrafttas.tasmod.ktrng.builtin.UUIDRNG;

import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.MathHelper;

/**
* <p>Generates and distributes UUIDs deterministically
* <p>This is necessary since the RNG is deterministic and would result in duplicated UUIDs
*
* @author Scribble
*/
public class UUIDHandler implements EventServerTick {

private int uuidIndex;

public UUIDHandler() {
}

@Override
public void onServerTick(MinecraftServer server) {
uuidIndex = -1;
}

public UUIDRNG getNewUUIDRNG() {
return new UUIDRNG(uuidIndex++);
}

public UUID getNewUUID() {
return MathHelper.getRandomUUID(getNewUUIDRNG());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.minecrafttas.tasmod.TASmod;
import com.minecrafttas.tasmod.ktrng.builtin.EntityRNG;

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

@WrapOperation(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/MathHelper;getRandomUUID(Ljava/util/Random;)Ljava/util/UUID;"))
private UUID wrap_getRandomUUID(Random rand, Operation<UUID> original) {
private UUID wrap_getRandomUUID(Random rand, Operation<UUID> original, World world) {
// return original.call(TASmod.uuidHandler.getNewUUID());
if (!world.isRemote)
return TASmod.uuidHandler.getNewUUID();
return original.call(new Random());
}
}
Loading
Loading