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
12 changes: 10 additions & 2 deletions src/main/java/com/minecrafttas/tasmod/TASmod.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
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.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,6 +100,8 @@ 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 MathRNG mathRandomness = new MathRNG(0);

Expand Down Expand Up @@ -158,8 +162,10 @@ public void onInitialize() {

@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 +233,8 @@ 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);
}

public static MinecraftServer getServerInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class RandomBase extends Random implements Registerable {
public RandomBase() {
super(TASmod.globalRandomness.getCurrentSeed());
initialSeed = TASmod.globalRandomness.getCurrentSeed();
jrand = new JRand(initialSeed);
jrand = new JRand(initialSeed, false);
}

public RandomBase(long seed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public EntityRNG(long seed) {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,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
@@ -1,16 +1,22 @@
package com.minecrafttas.tasmod.mixin.killtherng;

import java.util.List;
import java.util.Random;
import java.util.UUID;

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

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.injector.ModifyReceiver;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
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.WorldRNG;
import com.minecrafttas.tasmod.util.SortedList;

import net.minecraft.entity.Entity;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;

Expand All @@ -34,4 +40,19 @@ public Random modify_worldSetRNGRandom(Random original, long seed) {
public Random modify_worldSetRNGReturn(Random original) {
return TASmod.worldSeedRandomness;
}

@WrapOperation(method = "<init>", at = @At(value = "FIELD", target = "Lnet/minecraft/world/World;loadedEntityList:Ljava/util/List;"))
private <E> void modify_loadedEntityList(World owner, List<E> list, Operation<Void> operation) {
operation.call(owner, new SortedList<Entity>((entity, entity2) -> {
if (entity == null || entity2 == null)
return 0;

UUID uuid = entity.getUniqueID();
UUID uuid2 = entity.getUniqueID();

if (uuid == null || uuid2 == null)
return 0;
return uuid.toString().compareTo(uuid2.toString());
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import com.minecrafttas.mctcommon.events.EventListenerRegistry;
import com.minecrafttas.tasmod.TASmod;
import com.minecrafttas.tasmod.events.EventKillTheRNGServer;
import com.minecrafttas.tasmod.ktrng.RandomBase.RNGSide;

import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
Expand All @@ -22,12 +18,12 @@ public class MixinWorldServer {

@Inject(method = "updateBlocks", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/chunk/Chunk;enqueueRelightChecks()V"))
private void modify_extendedBlockStorage(CallbackInfo ci, @Local Chunk chunk) {
TASmod.debugRand.writeDebug(String.format("(%s, %s)", chunk.x, chunk.z));
// TASmod.debugRand.writeDebug(String.format("(%s, %s)", chunk.x, chunk.z));
}

@WrapOperation(method = "updateBlocks", at = @At(value = "FIELD", target = "Lnet/minecraft/world/WorldServer;updateLCG:I", opcode = Opcodes.PUTFIELD))
private void modify_updateLCG(WorldServer world, int original, Operation<Void> operation) {
EventListenerRegistry.fireEvent(EventKillTheRNGServer.EventRNG.class, RNGSide.Server, String.format("updateLCG"), (long) world.updateLCG, Integer.toString(original), "UpdateLCG", 7);
// EventListenerRegistry.fireEvent(EventKillTheRNGServer.EventRNG.class, RNGSide.Server, String.format("updateLCG"), (long) world.updateLCG, Integer.toString(original), "UpdateLCG", 7);
operation.call(world, original);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.minecrafttas.tasmod.savestates.handlers.SavestateWorldHandler;
import com.minecrafttas.tasmod.util.Ducks.PlayerChunkMapDuck;
import com.minecrafttas.tasmod.util.SortedArrayList;
import com.minecrafttas.tasmod.util.SortedList;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.management.PlayerChunkMap;
Expand Down Expand Up @@ -54,11 +54,11 @@ public List<EntityPlayerMP> getPlayers() {
}

/**
* Replaces the type of PlayerChunkMap.entries with a {@link SortedArrayList}
* Replaces the type of PlayerChunkMap.entries with a {@link SortedList}
*/
@WrapOperation(method = "<init>", at = @At(value = "FIELD", target = "Lnet/minecraft/server/management/PlayerChunkMap;entries:Ljava/util/List;"))
private <E> void modify_entries(PlayerChunkMap owner, List<E> list, Operation<Void> operation) {
operation.call(owner, new SortedArrayList<PlayerChunkMapEntry>((playerChunkMapEntry, playerChunkMapEntry2) -> {
operation.call(owner, new SortedList<PlayerChunkMapEntry>((playerChunkMapEntry, playerChunkMapEntry2) -> {
if (playerChunkMapEntry == null || playerChunkMapEntry == null)
return 0;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.minecrafttas.tasmod.savestates.storage.builtin;

import java.util.UUID;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.minecrafttas.tasmod.savestates.storage.SavestateStorageExtensionBase;

import net.minecraft.entity.Entity;
import net.minecraft.entity.passive.EntityBat;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldServer;

public class EntityBatSpawnPositionStorage extends SavestateStorageExtensionBase {

public EntityBatSpawnPositionStorage() {
super("entityBatSpawnPosition.json");
}

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

@Override
public JsonObject onSavestate(MinecraftServer server, JsonObject dataToSave) {
for (WorldServer worldServer : server.worlds) {
for (Entity entity : worldServer.loadedEntityList) {
if (entity instanceof EntityBat) {
EntityBat bat = (EntityBat) entity;
BlockPos spawnPos = bat.spawnPosition;
if (spawnPos == null)
continue;
UUID entityUUID = entity.getUniqueID();
dataToSave.addProperty(entityUUID.toString(), Long.toString(bat.spawnPosition.toLong()));
}
}
}
return dataToSave;
}

@Override
public void onLoadstatePost(MinecraftServer server, JsonObject loadedData) {
for (WorldServer worldServer : server.worlds) {
for (Entity entity : worldServer.loadedEntityList) {
if (entity instanceof EntityBat) {
EntityBat bat = (EntityBat) entity;
UUID entityUUID = entity.getUniqueID();
JsonElement element = loadedData.get(entityUUID.toString());
if (element == null)
continue;
BlockPos spawnPos = BlockPos.fromLong(element.getAsLong());
bat.spawnPosition = spawnPos;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.minecrafttas.tasmod.savestates.storage.builtin;

import java.util.UUID;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.minecrafttas.tasmod.savestates.storage.SavestateStorageExtensionBase;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.ai.EntityAITasks;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.WorldServer;

/**
* Stores the {@link EntityAITasks#tickCount} in a savestate.
*
* AI tasks are only updated every third tick and the tickCount keeps track of that.
*
* @author Scribble
*/
public class EntityTickTimersStorage extends SavestateStorageExtensionBase {

public EntityTickTimersStorage() {
super("entityTickTimers.json");
}

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

@Override
public JsonObject onSavestate(MinecraftServer server, JsonObject dataToSave) {
for (WorldServer worldServer : server.worlds) {
for (Entity entity : worldServer.loadedEntityList) {
if (entity instanceof EntityLiving) {
UUID entityUUID = entity.getUniqueID();
EntityLiving entityLiving = (EntityLiving) entity;
EntityAITasks tasks = entityLiving.tasks;
EntityAITasks targetTasks = entityLiving.targetTasks;
int tickCount = tasks.tickCount;
int tickCountTarget = targetTasks.tickCount;
int tickCountSound = entityLiving.livingSoundTime;
JsonObject tickCountList = new JsonObject();
tickCountList.addProperty("aitasks", tickCount);
tickCountList.addProperty("aitargetTasks", tickCountTarget);
tickCountList.addProperty("livingSoundTime", tickCountSound);
dataToSave.add(entityUUID.toString(), tickCountList);
}
}
}
return dataToSave;
}

@Override
public void onLoadstatePost(MinecraftServer server, JsonObject loadedData) {
for (WorldServer worldServer : server.worlds) {
for (Entity entity : worldServer.loadedEntityList) {
UUID entityUUID = entity.getUniqueID();
JsonElement tickCountElement = loadedData.get(entityUUID.toString());
if (tickCountElement == null)
continue;
JsonObject tickCountList = tickCountElement.getAsJsonObject();

int tickCount = tickCountList.get("aitasks").getAsInt();
int tickCountTarget = tickCountList.get("aitargetTasks").getAsInt();
int tickCountSound = tickCountList.get("livingSoundTime").getAsInt();

if (entity instanceof EntityLiving) {
EntityLiving entityLiving = (EntityLiving) entity;
EntityAITasks tasks = entityLiving.tasks;
EntityAITasks targetTasks = entityLiving.targetTasks;
tasks.tickCount = tickCount;
targetTasks.tickCount = tickCountTarget;
entityLiving.livingSoundTime = tickCountSound;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
package com.minecrafttas.tasmod.util;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.ListIterator;

/**
* Sorts the ArrayList every time an element is added/removed.
* @param <E> Type The type of the ArrayList
* @author Scribble
*/
public class SortedArrayList<E> extends ArrayList<E> {
public class SortedList<E> extends LinkedList<E> {

private final Comparator<E> comparable;

public SortedArrayList(Comparator<E> comparable) {
public SortedList(Comparator<E> comparable) {
this.comparable = comparable;
}

@Override
public boolean add(E newElement) {
for (int i = 0; i < this.size(); i++) {
E element = this.get(i);
ListIterator<E> iterator = (ListIterator<E>) iterator();
while (iterator.hasNext()) {
E element = iterator.next();
if (comparable.compare(element, newElement) >= 0) {
super.add(i, newElement);
iterator.set(newElement);
iterator.add(element);
return true;
}
}
return super.add(newElement);
super.add(newElement);
return true;
}
}
5 changes: 4 additions & 1 deletion src/main/resources/tasmod.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ accessible field net/minecraft/client/settings/KeyBinding CATEGORY_ORDER Ljava/u
accessible field net/minecraft/entity/Entity rand Ljava/util/Random;
accessible field net/minecraft/world/World updateLCG I
mutable field net/minecraft/world/World rand Ljava/util/Random;

accessible field net/minecraft/entity/EntityLiving tasks Lnet/minecraft/entity/ai/EntityAITasks;
accessible field net/minecraft/entity/EntityLiving targetTasks Lnet/minecraft/entity/ai/EntityAITasks;
accessible field net/minecraft/entity/ai/EntityAITasks tickCount I
accessible field net/minecraft/entity/passive/EntityBat spawnPosition Lnet/minecraft/util/math/BlockPos;
Loading
Loading