Skip to content

Commit

Permalink
Update registry of living beings (unstable)
Browse files Browse the repository at this point in the history
  • Loading branch information
OldSerpskiStalker committed Jan 8, 2025
1 parent d3e922e commit 66419e8
Showing 1 changed file with 99 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
import net.minecraft.init.Blocks;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.management.PlayerList;
import net.minecraft.util.ClassInheritanceMultiMap;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.DimensionType;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
Expand Down Expand Up @@ -60,15 +64,17 @@ public final class OnEventSandBox implements IDebug
private static final File SAVE_FILE = new File(
DimensionManager.getCurrentSaveRootDirectory(), "entity_registry.json");

private static final Map<UUID, EntityData> ENTITY_DATA_MAP = new HashMap<>();
private static final Map<String, EntityData> ENTITY_DATA_MAP = new HashMap<>();

@SubscribeEvent
public void onWorldLoad(WorldEvent.Load event) {
public void onWorldLoad(WorldEvent.Load event)
{
loadEntityData();
}

@SubscribeEvent
public void onWorldSave(WorldEvent.Save event) {
public void onWorldSave(WorldEvent.Save event)
{
saveEntityData();
}

Expand All @@ -83,14 +89,21 @@ public void onChunkLoad(ChunkEvent.Load event)
{
for (Entity entity : entityList)
{
if (!(entity instanceof EntityPlayer))
String key = getEntityKey(entity);
ENTITY_DATA_MAP.remove(key);
}
}

DimensionType dimensionType = DimensionManager.getProviderType(world.provider.getDimension());
for (String key : ENTITY_DATA_MAP.keySet())
{
if (key.startsWith(dimensionType.getName()))
{
Entity recreatedEntity = recreateEntity(world, key);
if (recreatedEntity != null)
{
UUID entityId = entity.getUniqueID();
if (!ENTITY_DATA_MAP.containsKey(entityId))
{
saveEntityState(entity);
entity.setDead();
}
Log.writeDataToLogFile(0,"Spawning entity: " + recreatedEntity.getName() + " at " + recreatedEntity.getPosition());
world.spawnEntity(recreatedEntity);
}
}
}
Expand All @@ -111,12 +124,39 @@ public void onChunkUnload(ChunkEvent.Unload event)
if (!(entity instanceof EntityPlayer))
{
saveEntityState(entity);
if (isOutsideRenderDistance(entity))
{
entity.setDead();
}
}
}
}
}
}

private boolean isOutsideRenderDistance(Entity entity)
{
MinecraftServer server = entity.getServer();
if (server == null) return false;

PlayerList playerList = server.getPlayerList();
int renderDistance = playerList.getViewDistance();

BlockPos pos = entity.getPosition();

for (EntityPlayer player : entity.world.playerEntities)
{
BlockPos playerPos = player.getPosition();

if (Math.abs(playerPos.getX() - pos.getX()) <= renderDistance * 16 &&
Math.abs(playerPos.getZ() - pos.getZ()) <= renderDistance * 16)
{
return false;
}
}
return true;
}

@SubscribeEvent
public void onChunkEnter(PlayerEvent.StartTracking event)
{
Expand All @@ -126,28 +166,66 @@ public void onChunkEnter(PlayerEvent.StartTracking event)
World world = entity.world;
if (!world.isRemote)
{
UUID entityId = entity.getUniqueID();
Entity recreatedEntity = recreateEntity(world, entityId);
String key = getEntityKey(entity);
Entity recreatedEntity = recreateEntity(world, key);
if (recreatedEntity != null)
{
LogHelper.logInfo("Spawning entity: " + recreatedEntity.getName() + " at " + recreatedEntity.getPosition());
Log.writeDataToLogFile(0,"Spawning entity: " + recreatedEntity.getName() + " at " + recreatedEntity.getPosition());
world.spawnEntity(recreatedEntity);
}
else
{
LogHelper.logInfo("Entity not found in saved data: " + entityId);
Log.writeDataToLogFile(0,"Entity not found in saved data: " + key);
}
}
}
}

private void saveEntityState(Entity entity)
{
UUID entityId = entity.getUniqueID();
String key = getEntityKey(entity);
if (key.equals("unknown")) return;

BlockPos pos = entity.getPosition();
NBTTagCompound nbt = new NBTTagCompound();
entity.writeToNBT(nbt);
ENTITY_DATA_MAP.put(entityId, new EntityData(pos, nbt));
ENTITY_DATA_MAP.put(key, new EntityData(pos, nbt));
}

private Entity recreateEntity(World world, String key)
{
EntityData data = ENTITY_DATA_MAP.get(key);
if (data != null)
{
String entityTypeKey = key.split("_")[0];
Entity entity = EntityList.createEntityByIDFromName(new ResourceLocation(entityTypeKey), world);
if (entity != null)
{
entity.readFromNBT(data.nbt);
entity.setPosition(data.pos.getX() + 0.5, data.pos.getY(), data.pos.getZ() + 0.5);
return entity;
}
else
{
Log.writeDataToLogFile(0,"Failed to recreate entity of type: " + entityTypeKey);
}
}
return null;
}

private String getEntityKey(Entity entity)
{
ResourceLocation entityType = EntityList.getKey(entity.getClass());
if (entityType != null)
{
BlockPos pos = entity.getPosition();
return entityType.toString();
}
else
{
Log.writeDataToLogFile(0,"Entity type not found for: " + entity);
return "unknown";
}
}

private Entity recreateEntity(World world, UUID entityId)
Expand All @@ -163,7 +241,7 @@ private Entity recreateEntity(World world, UUID entityId)
}
else
{
LogHelper.logError("Failed to recreate entity from NBT: " + entityId);
Log.writeDataToLogFile(0,"Failed to recreate entity from NBT: " + entityId);
}
}
return null;
Expand All @@ -176,10 +254,10 @@ private void saveEntityData()
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonArray jsonArray = new JsonArray();

for (Map.Entry<UUID, EntityData> entry : ENTITY_DATA_MAP.entrySet())
for (Map.Entry<String, EntityData> entry : ENTITY_DATA_MAP.entrySet())
{
JsonObject jsonEntity = new JsonObject();
jsonEntity.addProperty("uuid", entry.getKey().toString());
jsonEntity.addProperty("key", entry.getKey());
jsonEntity.addProperty("x", entry.getValue().pos.getX());
jsonEntity.addProperty("y", entry.getValue().pos.getY());
jsonEntity.addProperty("z", entry.getValue().pos.getZ());
Expand All @@ -188,7 +266,6 @@ private void saveEntityData()
}

String jsonOutput = gson.toJson(jsonArray);

writer.write(jsonOutput);
}
catch (IOException e)
Expand All @@ -213,15 +290,15 @@ private void loadEntityData()
if (element.isJsonObject())
{
JsonObject jsonEntity = element.getAsJsonObject();
UUID entityId = UUID.fromString(jsonEntity.get("uuid").getAsString());
String key = jsonEntity.get("key").getAsString();
BlockPos pos = new BlockPos(
jsonEntity.get("x").getAsInt(),
jsonEntity.get("y").getAsInt(),
jsonEntity.get("z").getAsInt()
);
JsonObject nbtObject = jsonEntity.getAsJsonObject("nbt");
NBTTagCompound nbt = JsonUtils.convertJsonToNBT(nbtObject);
ENTITY_DATA_MAP.put(entityId, new EntityData(pos, nbt));
ENTITY_DATA_MAP.put(key, new EntityData(pos, nbt));
}
}
}
Expand All @@ -245,19 +322,6 @@ public EntityData(BlockPos pos, NBTTagCompound nbt)
}
}

private static class LogHelper
{
public static void logInfo(String message)
{
Log.writeDataToLogFile(1,"[INFO] " + message);
}

public static void logError(String message)
{
Log.writeDataToLogFile(2,"[ERROR] " + message);
}
}

private static class JsonUtils
{
public static JsonObject convertNBTToJson(NBTTagCompound nbt)
Expand Down

0 comments on commit 66419e8

Please sign in to comment.