Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public interface EventBase {
*/
public static void register(EventBase eventListener) {
if (eventListener == null) {
throw new NullPointerException("Tried to register a packethandler with value null");
throw new NullPointerException("Tried to register an eventListener with value null");
}

for (Class<?> type : searchForInterfaces(eventListener.getClass())) {
Expand Down Expand Up @@ -153,7 +153,7 @@ public static void register(List<? extends EventBase> eventListeners) {
*/
public static void unregister(EventBase eventListener) {
if (eventListener == null) {
throw new NullPointerException("Tried to unregister a packethandler with value null");
throw new NullPointerException("Tried to unregister an eventListener with value null");
}
for (Class<?> type : searchForInterfaces(eventListener.getClass())) {
if (EventBase.class.isAssignableFrom(type)) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/minecrafttas/mctcommon/events/EventServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
*/
public interface EventServer {

/**
* Fired, just before the server initialised, for both integrated and dedicated server.
*/
@FunctionalInterface
public static interface EventServerStart extends EventBase {

/**
* Fired, when the server is initialised, for both integrated and dedicated server.
* @param server The server
*/
public void onServerStart(MinecraftServer server);
}

/**
* Fired, when the server is initialised, for both integrated and dedicated server.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.minecrafttas.mctcommon.events.EventListenerRegistry;
import com.minecrafttas.mctcommon.events.EventServer.EventServerGameLoop;
import com.minecrafttas.mctcommon.events.EventServer.EventServerInit;
import com.minecrafttas.mctcommon.events.EventServer.EventServerStart;
import com.minecrafttas.mctcommon.events.EventServer.EventServerStop;
import com.minecrafttas.mctcommon.events.EventServer.EventServerTick;

Expand All @@ -17,6 +18,11 @@
@Mixin(MinecraftServer.class)
public class MixinMinecraftServer {

@Inject(method = "run", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;init()Z"))
public void inject_initStart(CallbackInfo ci) {
EventListenerRegistry.fireEvent(EventServerStart.class, (MinecraftServer) (Object) this);
}

@Inject(method = "run", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;init()Z", shift = Shift.AFTER))
public void inject_init(CallbackInfo ci) {
EventListenerRegistry.fireEvent(EventServerInit.class, (MinecraftServer) (Object) this);
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/minecrafttas/tasmod/TASmod.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.minecrafttas.mctcommon.CommandRegistry;
import com.minecrafttas.mctcommon.events.EventListenerRegistry;
import com.minecrafttas.mctcommon.events.EventServer.EventServerInit;
import com.minecrafttas.mctcommon.events.EventServer.EventServerStart;
import com.minecrafttas.mctcommon.events.EventServer.EventServerStop;
import com.minecrafttas.mctcommon.networking.PacketHandlerRegistry;
import com.minecrafttas.mctcommon.networking.Server;
Expand All @@ -25,12 +26,13 @@
import com.minecrafttas.tasmod.commands.CommandTickrate;
import com.minecrafttas.tasmod.commands.TabCompletionUtils;
import com.minecrafttas.tasmod.handlers.PlayUntilHandler;
import com.minecrafttas.tasmod.ktrng.GlobalRandomnessTimer;
import com.minecrafttas.tasmod.playback.PlaybackControllerServer;
import com.minecrafttas.tasmod.playback.metadata.builtin.StartpositionMetadataExtension;
import com.minecrafttas.tasmod.registries.TASmodPackets;
import com.minecrafttas.tasmod.savestates.SavestateHandlerServer;
import com.minecrafttas.tasmod.savestates.handlers.SavestateResourcePackHandler;
import com.minecrafttas.tasmod.savestates.storage.builtin.SavestateMotionStorage;
import com.minecrafttas.tasmod.savestates.storage.builtin.KTRNGSeedStorage;
import com.minecrafttas.tasmod.tickratechanger.TickrateChangerServer;
import com.minecrafttas.tasmod.ticksync.TickSyncServer;
import com.minecrafttas.tasmod.util.LoggerMarkers;
Expand All @@ -46,7 +48,7 @@
*
* @author Scribble
*/
public class TASmod implements ModInitializer, EventServerInit, EventServerStop {
public class TASmod implements ModInitializer, EventServerStart, EventServerInit, EventServerStop {

public static final Logger LOGGER = LogManager.getLogger("TASmod");

Expand Down Expand Up @@ -81,6 +83,10 @@ public class TASmod implements ModInitializer, EventServerInit, EventServerStop

public static final PlayUntilHandler playUntil = new PlayUntilHandler();

public static GlobalRandomnessTimer globalRandomness;

public static KTRNGSeedStorage seedStorage = new KTRNGSeedStorage();

@Override
public void onInitialize() {

Expand Down Expand Up @@ -125,6 +131,13 @@ public void onInitialize() {
EventListenerRegistry.register(resourcepackHandler);
PacketHandlerRegistry.register(playUntil);
EventListenerRegistry.register(playUntil);
EventListenerRegistry.register(seedStorage);
}

@Override
public void onServerStart(MinecraftServer server) {
globalRandomness = new GlobalRandomnessTimer();
EventListenerRegistry.register(globalRandomness);
}

@Override
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/minecrafttas/tasmod/ktrng/EntityRandomness.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.minecrafttas.tasmod.ktrng;

import com.minecrafttas.tasmod.TASmod;

public class EntityRandomness extends RandomBase {

private static long entityCounter = 0L;

public EntityRandomness() {
super(TASmod.globalRandomness.getCurrentSeed() + (entityCounter++));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.minecrafttas.tasmod.ktrng;

import com.minecrafttas.mctcommon.events.EventServer;

import net.minecraft.server.MinecraftServer;

public class GlobalRandomnessTimer implements EventServer.EventServerTick {

private RandomBase globalRandomness;

private long currentSeed = 0L;

public GlobalRandomnessTimer() {
globalRandomness = new RandomBase(0L);
}

@Override
public void onServerTick(MinecraftServer server) {
currentSeed = globalRandomness.nextLong();
}

public long getCurrentSeed() {
return currentSeed;
}

public void setSeed(long newSeed) {
globalRandomness.setSeed(newSeed);
currentSeed = newSeed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class KillTheRNGHandler implements EventServerTick, EventPlayerJoinedClie
* @param isLoaded If the KillTheRNG mod is loaded
*/
public KillTheRNGHandler(boolean isLoaded) {

this.isLoaded = isLoaded;

if (isLoaded) {
Expand Down
183 changes: 183 additions & 0 deletions src/main/java/com/minecrafttas/tasmod/ktrng/RandomBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package com.minecrafttas.tasmod.ktrng;

import java.util.Random;

import kaptainwutax.seedutils.lcg.LCG;
import kaptainwutax.seedutils.rand.JRand;

public class RandomBase extends Random {

private String name;
private String description;

private long timesCalled = 0;

private boolean enabled;
private boolean client;

private long initialSeed;

public RandomBase(long seed) {
super(seed);
this.initialSeed = seed;
}

public void setSeed(long seedIn) {
timesCalled = 0;
super.setSeed(seedIn ^ 0x5deece66dL);
}

public void setSeed(long seedIn, boolean shouldIncrease) {
timesCalled++;
super.setSeed(seedIn ^ 0x5deece66dL);
}

public long getSeed() {
long saved = timesCalled;
long seed = reverse(super.nextLong()) ^ 0x5deece66dL;
super.setSeed(seed);
timesCalled = saved;
return seed ^ 0x5deece66dL;
}

public static long reverse(long in) {
return (((7847617 * ((24667315 * (in >>> 32) + 18218081 * (in & 0xffffffffL) + 67552711) >> 32) - 18218081 * ((-4824621 * (in >>> 32) + 7847617 * (in & 0xffffffffL) + 7847617) >> 32)) - 11) * 246154705703781L) & 0xffffffffffffL;
}

public String getName() {
return this.name;
}

public String getDescription() {
return description;
}

public long getTimesCalled() {
return timesCalled;
}

public boolean isEnabled() {
return enabled;
}

public boolean isClient() {
return client;
}

@Override
public long nextLong() {
timesCalled++;
long seedstored = getSeed();
long value = super.nextLong();
fireEvent(seedstored, Long.toString(value));
return value;
}

@Override
public double nextDouble() {
timesCalled++;
long seedstored = getSeed();
double value = super.nextDouble();
fireEvent(seedstored, Double.toString(value));
return value;
}

@Override
public boolean nextBoolean() {
timesCalled++;
long seedstored = getSeed();
boolean value = super.nextBoolean();
fireEvent(seedstored, Boolean.toString(value));
return value;
}

@Override
public int nextInt() {
timesCalled++;
long seedstored = getSeed();
int value = super.nextInt();
fireEvent(seedstored, Integer.toString(value));
return value;
}

@Override
public int nextInt(int bound) {
timesCalled++;
long seedstored = getSeed();
int value = super.nextInt(bound);
fireEvent(seedstored, Integer.toString(value));
return value;
}

@Override
public float nextFloat() {
return super.nextFloat();
}

@Override
public void nextBytes(byte[] bytes) {
super.nextBytes(bytes);
}

@Override
public double nextGaussian() {
timesCalled++;
double value = 0;
value = super.nextGaussian();
return value;
}

public void advance() {
advance(1);
}

public void advance(long i) {
JRand thing = JRand.ofInternalSeed(getSeed());
thing.advance(i);
setSeed(thing.getSeed());
}

public long getSeedAt(int steps) {
JRand thing = new JRand(getSeed()).combine(steps);
return thing.getSeed();
}

public long distance(RandomBase random) {
return RandomBase.distance(this.getSeed(), random.getSeed());
}

public long distance(long seed) {
return RandomBase.distance(this.getSeed(), seed);
}

public static long distance(RandomBase random1, RandomBase random2) {
return RandomBase.distance(random1.getSeed(), random2.getSeed());
}

public static long distance(long seed, long seed2) {
return LCG.JAVA.distance(seed, seed2);
}

@Override
public String toString() {
return name + ": " + enabled;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof RandomBase) {
RandomBase custom = (RandomBase) obj;
return custom.name.equals(name);
} else {
return super.equals(obj);
}
}

public void fireEvent(long seed, String value) {
// TODO Implement
}

public long getInitialSeed() {
return initialSeed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.minecrafttas.tasmod.mixin.killtherng;

import java.util.Random;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.minecrafttas.tasmod.ktrng.EntityRandomness;

import net.minecraft.entity.Entity;

@Mixin(Entity.class)
public class MixinEntity {

@ModifyExpressionValue(method = "<init>", at = @At(value = "NEW", target = "Ljava/util/Random;"))
public Random modify_entityRandom(Random original) {
return new EntityRandomness();
}
}
Loading
Loading