From 204e430be813515a8552d8c0bf5fb751cac2ef2a Mon Sep 17 00:00:00 2001 From: Mathijs Riezebos Date: Thu, 12 Jan 2017 15:15:53 +0100 Subject: [PATCH] Created a backbone for saving DimDoors specific worldsave data --- .../{CommonProxy.java => DDProxyCommon.java} | 7 +- .../java/com/zixiken/dimdoors/DimDoors.java | 33 +++++++- .../java/com/zixiken/dimdoors/IDDProxy.java | 26 ++++++ .../{ClientProxy.java => DDProxyClient.java} | 24 +++++- .../dimdoors/server/DDProxyServer.java | 34 ++++++++ .../zixiken/dimdoors/shared/DDSavedData.java | 29 +++++++ .../com/zixiken/dimdoors/shared/Pocket.java | 55 +++++++++++++ .../dimdoors/shared/PocketRegistry.java | 80 +++++++++++++++++++ .../dimdoors/shared/PocketSavedData.java | 70 ++++++++++++++++ .../com/zixiken/dimdoors/shared/Rift.java | 25 ++++++ .../zixiken/dimdoors/shared/RiftRegistry.java | 80 +++++++++++++++++++ .../dimdoors/shared/RiftSavedData.java | 70 ++++++++++++++++ 12 files changed, 526 insertions(+), 7 deletions(-) rename src/main/java/com/zixiken/dimdoors/{CommonProxy.java => DDProxyCommon.java} (94%) create mode 100644 src/main/java/com/zixiken/dimdoors/IDDProxy.java rename src/main/java/com/zixiken/dimdoors/client/{ClientProxy.java => DDProxyClient.java} (54%) create mode 100644 src/main/java/com/zixiken/dimdoors/server/DDProxyServer.java create mode 100644 src/main/java/com/zixiken/dimdoors/shared/DDSavedData.java create mode 100644 src/main/java/com/zixiken/dimdoors/shared/Pocket.java create mode 100644 src/main/java/com/zixiken/dimdoors/shared/PocketRegistry.java create mode 100644 src/main/java/com/zixiken/dimdoors/shared/PocketSavedData.java create mode 100644 src/main/java/com/zixiken/dimdoors/shared/Rift.java create mode 100644 src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java create mode 100644 src/main/java/com/zixiken/dimdoors/shared/RiftSavedData.java diff --git a/src/main/java/com/zixiken/dimdoors/CommonProxy.java b/src/main/java/com/zixiken/dimdoors/DDProxyCommon.java similarity index 94% rename from src/main/java/com/zixiken/dimdoors/CommonProxy.java rename to src/main/java/com/zixiken/dimdoors/DDProxyCommon.java index 8d963761..b09e1c60 100644 --- a/src/main/java/com/zixiken/dimdoors/CommonProxy.java +++ b/src/main/java/com/zixiken/dimdoors/DDProxyCommon.java @@ -18,8 +18,9 @@ import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.registry.GameRegistry; -public class CommonProxy { +public abstract class DDProxyCommon implements IDDProxy { + @Override public void onPreInitialization(FMLPreInitializationEvent event) { MinecraftForge.EVENT_BUS.register(new EventHookContainer()); ModBlocks.registerBlocks(); @@ -34,6 +35,7 @@ public class CommonProxy { GameRegistry.registerTileEntity(TileEntityDimDoorGold.class, "TileEntityDimDoorGold"); } + @Override public void onInitialization(FMLInitializationEvent event) { CraftingManager.registerRecipes(); ModelManager.registerModels(); @@ -51,4 +53,7 @@ public class CommonProxy { dimTile.lockStatus = 0; } } + + @Override + public abstract boolean isClient(); } \ No newline at end of file diff --git a/src/main/java/com/zixiken/dimdoors/DimDoors.java b/src/main/java/com/zixiken/dimdoors/DimDoors.java index c6cf4e9c..c63c5250 100644 --- a/src/main/java/com/zixiken/dimdoors/DimDoors.java +++ b/src/main/java/com/zixiken/dimdoors/DimDoors.java @@ -1,8 +1,12 @@ package com.zixiken.dimdoors; import com.zixiken.dimdoors.items.ModItems; +import com.zixiken.dimdoors.shared.PocketSavedData; +import com.zixiken.dimdoors.shared.RiftSavedData; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.Item; +import net.minecraft.world.World; +import net.minecraftforge.fml.common.FMLLog; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.SidedProxy; import net.minecraftforge.fml.common.event.*; @@ -14,9 +18,9 @@ public class DimDoors { public static final String VERSION = "3.0.0-a1"; public static final String MODID = "dimdoors"; - @SidedProxy(clientSide = "com.zixiken.dimdoors.client.ClientProxy", - serverSide = "com.zixiken.dimdoors.CommonProxy") - public static CommonProxy proxy; + @SidedProxy(clientSide = "com.zixiken.dimdoors.client.DDProxyClient", + serverSide = "com.zixiken.dimdoors.server.DDProxyServer") + public static DDProxyCommon proxy; @Mod.Instance(DimDoors.MODID) public static DimDoors instance; @@ -36,4 +40,27 @@ public class DimDoors { public void onInitialization(FMLInitializationEvent event) { proxy.onInitialization(event); } + + @Mod.EventHandler + public void serverLoad( FMLServerStartingEvent event ) { + //@todo event.registerServerCommand( new DDCommand() ); //to register commands that this mod offers? + PocketSavedData.get(getDefWorld()); + RiftSavedData.get(getDefWorld()); + } + + public static boolean isClient() { + return proxy.isClient(); + } + + public static boolean isServer() { + return !isClient(); + } + + public static World getDefWorld() { + return proxy.getDefWorld(); //gets the server or client world dim 0 handler + } + + public static void log( String text ) { + FMLLog.info("[DimDoors] " + text, 0); + } } diff --git a/src/main/java/com/zixiken/dimdoors/IDDProxy.java b/src/main/java/com/zixiken/dimdoors/IDDProxy.java new file mode 100644 index 00000000..868f144f --- /dev/null +++ b/src/main/java/com/zixiken/dimdoors/IDDProxy.java @@ -0,0 +1,26 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.zixiken.dimdoors; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.world.World; +import net.minecraftforge.fml.common.event.FMLInitializationEvent; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; + +/** + * + * @author Robijnvogel + */ +public interface IDDProxy +{ + public boolean isClient(); + public void onPreInitialization(FMLPreInitializationEvent event); + public void onInitialization(FMLInitializationEvent event); + + public EntityPlayer getLocalPlayer(); + + public World getDefWorld(); +} diff --git a/src/main/java/com/zixiken/dimdoors/client/ClientProxy.java b/src/main/java/com/zixiken/dimdoors/client/DDProxyClient.java similarity index 54% rename from src/main/java/com/zixiken/dimdoors/client/ClientProxy.java rename to src/main/java/com/zixiken/dimdoors/client/DDProxyClient.java index 6df37cd0..77c115b0 100644 --- a/src/main/java/com/zixiken/dimdoors/client/ClientProxy.java +++ b/src/main/java/com/zixiken/dimdoors/client/DDProxyClient.java @@ -1,14 +1,17 @@ package com.zixiken.dimdoors.client; -import com.zixiken.dimdoors.CommonProxy; +import com.zixiken.dimdoors.DDProxyCommon; import com.zixiken.dimdoors.tileentities.TileEntityDimDoor; import com.zixiken.dimdoors.tileentities.TileEntityTransTrapdoor; +import net.minecraft.client.Minecraft; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.world.World; import net.minecraftforge.fml.client.registry.ClientRegistry; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; -public class ClientProxy extends CommonProxy { +public class DDProxyClient extends DDProxyCommon { @Override public void onPreInitialization(FMLPreInitializationEvent event) { @@ -19,5 +22,20 @@ public class ClientProxy extends CommonProxy { public void registerRenderers() { ClientRegistry.bindTileEntitySpecialRenderer(TileEntityDimDoor.class, new RenderDimDoor()); ClientRegistry.bindTileEntitySpecialRenderer(TileEntityTransTrapdoor.class, new RenderTransTrapdoor()); - } + } + + @Override + public boolean isClient() { + return true; + } + + @Override + public EntityPlayer getLocalPlayer() { + return Minecraft.getMinecraft().player; + } + + @Override + public World getDefWorld() { + return Minecraft.getMinecraft().getIntegratedServer().worldServerForDimension(0); //gets the client world dim 0 handler + } } \ No newline at end of file diff --git a/src/main/java/com/zixiken/dimdoors/server/DDProxyServer.java b/src/main/java/com/zixiken/dimdoors/server/DDProxyServer.java new file mode 100644 index 00000000..b999a33e --- /dev/null +++ b/src/main/java/com/zixiken/dimdoors/server/DDProxyServer.java @@ -0,0 +1,34 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.zixiken.dimdoors.server; + +import com.zixiken.dimdoors.DDProxyCommon; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.world.World; +import net.minecraftforge.common.DimensionManager; + +/** + * + * @author Robijnvogel + */ +public class DDProxyServer extends DDProxyCommon{ + + @Override + public boolean isClient() { + return false; + } + + @Override + public EntityPlayer getLocalPlayer() { + return null; + } + + @Override + public World getDefWorld() { + return DimensionManager.getWorld(0); //gets the server world dim 0 handler + } + +} diff --git a/src/main/java/com/zixiken/dimdoors/shared/DDSavedData.java b/src/main/java/com/zixiken/dimdoors/shared/DDSavedData.java new file mode 100644 index 00000000..7f56fb7b --- /dev/null +++ b/src/main/java/com/zixiken/dimdoors/shared/DDSavedData.java @@ -0,0 +1,29 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.zixiken.dimdoors.shared; + +import com.zixiken.dimdoors.DimDoors; +import java.io.File; +import net.minecraft.world.World; +import net.minecraft.world.WorldSavedData; +import net.minecraftforge.fml.common.FMLCommonHandler; + +/** + * + * @author Robijnvogel + */ +abstract class DDSavedData extends WorldSavedData { + + public DDSavedData(String name) { + super(name); + } + + public File getSaveLocation(World world) { + File saveDir = world.getSaveHandler().getWorldDirectory(); + return new File(saveDir, "dimdoors/"); + } + +} diff --git a/src/main/java/com/zixiken/dimdoors/shared/Pocket.java b/src/main/java/com/zixiken/dimdoors/shared/Pocket.java new file mode 100644 index 00000000..56b1a6dc --- /dev/null +++ b/src/main/java/com/zixiken/dimdoors/shared/Pocket.java @@ -0,0 +1,55 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.zixiken.dimdoors.shared; + +import java.util.ArrayList; +import java.util.List; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; + +/** + * + * @author Robijnvogel + */ +class Pocket { + + private int ID; + private int size; //in chunks + private int depth; + private int typeID; + private Object coords; //0,0 should be 0,0, 1,1 should be 128,128 etc + private final List playerUUIDs; + private final List doorIDs; + + Pocket() { + playerUUIDs = new ArrayList(); + doorIDs = new ArrayList(); + } + + static Pocket readFromNBT(int ID, NBTTagCompound pocketNBT) { + Pocket pocket = new Pocket(); + pocket.ID = ID; + pocket.size = pocketNBT.getInteger("size"); + pocket.depth = pocketNBT.getInteger("depth"); + pocket.typeID = pocketNBT.getInteger("typeID"); + + //@todo pocket.coords = pocketNBT.get; + NBTTagCompound playersNBT = pocketNBT.getCompoundTag("players"); + NBTTagCompound doorsNBT = pocketNBT.getCompoundTag("doors"); + //@todo iterate through above two compound tags + + return pocket; + } + + static NBTBase writeToNBT(Pocket pocket) { + NBTTagCompound pocketNBT = new NBTTagCompound(); + + //@todo implement shit; + + return pocketNBT; + } + +} diff --git a/src/main/java/com/zixiken/dimdoors/shared/PocketRegistry.java b/src/main/java/com/zixiken/dimdoors/shared/PocketRegistry.java new file mode 100644 index 00000000..7f570e61 --- /dev/null +++ b/src/main/java/com/zixiken/dimdoors/shared/PocketRegistry.java @@ -0,0 +1,80 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.zixiken.dimdoors.shared; + +import java.util.HashMap; +import java.util.Map; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; + +/** + * + * @author Robijnvogel + */ +public class PocketRegistry { + + public static final PocketRegistry Instance = new PocketRegistry(); + + // Privates + private int nextUnusedID; + private final Map pocketList; + + // Methods + public PocketRegistry() { + nextUnusedID = 0; + pocketList = new HashMap(); + } + + public void reset() { + nextUnusedID = 0; + pocketList.clear(); + } + + public void readFromNBT(NBTTagCompound nbt) { + nextUnusedID = nbt.getInteger("nextUnusedID"); + if (nbt.hasKey("pocketData")) { + NBTTagCompound pocketsNBT = nbt.getCompoundTag("pocketData"); + int i = 1; + String tag = "" + i; + while (pocketsNBT.hasKey(tag)) { + NBTTagCompound pocketNBT = pocketsNBT.getCompoundTag(tag); + Pocket pocket = Pocket.readFromNBT(i, pocketNBT); + pocketList.put(i, pocket); + + i++; + tag = "" + i; + } + } + } + + public void writeToNBT(NBTTagCompound nbt) { + nbt.setInteger("nextUnusedID", nextUnusedID); + NBTTagCompound pocketsNBT = new NBTTagCompound(); + for (Map.Entry entry : pocketList.entrySet()) { + pocketsNBT.setTag("" + entry.getKey(), Pocket.writeToNBT(entry.getValue())); + } + nbt.setTag("pocketData", pocketsNBT); + } + + public int registerNewPocket(Pocket pocket, World world) { + pocketList.put(nextUnusedID, pocket); + + nextUnusedID++; + PocketSavedData.get(world).markDirty(); //Notify that this needs to be saved on world save + return nextUnusedID -1; + } + + public void removePocket(int pocketID, World world) { + if (pocketList.containsKey(pocketID)) { + pocketList.remove(pocketID); + PocketSavedData.get(world).markDirty(); //Notify that this needs to be saved on world save + } + } + + public Pocket getPocket(int ID) { + return pocketList.get(ID); + } +} diff --git a/src/main/java/com/zixiken/dimdoors/shared/PocketSavedData.java b/src/main/java/com/zixiken/dimdoors/shared/PocketSavedData.java new file mode 100644 index 00000000..95eb37a9 --- /dev/null +++ b/src/main/java/com/zixiken/dimdoors/shared/PocketSavedData.java @@ -0,0 +1,70 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.zixiken.dimdoors.shared; + +import com.zixiken.dimdoors.DimDoors; +import java.io.File; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; +import net.minecraft.world.storage.MapStorage; + +/** + * + * @author Robijnvogel + */ +public class PocketSavedData extends DDSavedData { + + private static final String DATA_NAME = "dimdoors_PocketSavedData"; + + public PocketSavedData() { + super(DATA_NAME); + } + + public PocketSavedData(String s) { + super(s); + } + + @Override + public File getSaveLocation(World world) { + return new File(super.getSaveLocation(world), "pockets.nbt"); + } + + public static PocketSavedData get(World world) { + MapStorage storage = world.getMapStorage(); + PocketSavedData instance = (PocketSavedData) storage.getOrLoadData(PocketSavedData.class, DATA_NAME); + + if (instance == null) { + instance = new PocketSavedData(); + storage.setData(DATA_NAME, instance); + } + return instance; + } + + @Override + public NBTTagCompound writeToNBT(NBTTagCompound pocketnbt) { + + NBTTagCompound pockets = new NBTTagCompound(); + PocketRegistry.Instance.writeToNBT(pockets); + pocketnbt.setTag("pockets", pockets); + + //@todo? saveNBTToPath(getSaveLocation(DimDoors.getDefWorld()), pocketnbt); + return pocketnbt; + } + + @Override + public void readFromNBT(NBTTagCompound pocketnbt) { + // Reset + PocketRegistry.Instance.reset(); + + // Load NBT + if (pocketnbt != null) { + if (pocketnbt.hasKey("pockets")) { + NBTTagCompound pockets = pocketnbt.getCompoundTag("pockets"); + PocketRegistry.Instance.readFromNBT(pockets); + } + } + } +} diff --git a/src/main/java/com/zixiken/dimdoors/shared/Rift.java b/src/main/java/com/zixiken/dimdoors/shared/Rift.java new file mode 100644 index 00000000..a7e8281d --- /dev/null +++ b/src/main/java/com/zixiken/dimdoors/shared/Rift.java @@ -0,0 +1,25 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.zixiken.dimdoors.shared; + +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; + +/** + * + * @author Robijnvogel + */ +class Rift { + + static Rift readFromNBT(int i, NBTTagCompound riftNBT) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + static NBTBase writeToNBT(Rift value) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + +} diff --git a/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java b/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java new file mode 100644 index 00000000..86e436b5 --- /dev/null +++ b/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java @@ -0,0 +1,80 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.zixiken.dimdoors.shared; + +import java.util.HashMap; +import java.util.Map; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; + +/** + * + * @author Robijnvogel + */ +public class RiftRegistry { + + public static final RiftRegistry Instance = new RiftRegistry(); + + // Privates + private int nextUnusedID; + private final Map riftList; + + // Methods + public RiftRegistry() { + nextUnusedID = 0; + riftList = new HashMap(); + } + + public void reset() { + nextUnusedID = 0; + riftList.clear(); + } + + public void readFromNBT(NBTTagCompound nbt) { + nextUnusedID = nbt.getInteger("nextUnusedID"); + if (nbt.hasKey("riftData")) { + NBTTagCompound riftsNBT = nbt.getCompoundTag("riftData"); + int i = 1; + String tag = "" + i; + while (riftsNBT.hasKey(tag)) { + NBTTagCompound riftNBT = riftsNBT.getCompoundTag(tag); + Rift rift = Rift.readFromNBT(i, riftNBT); + riftList.put(i, rift); + + i++; + tag = "" + i; + } + } + } + + public void writeToNBT(NBTTagCompound nbt) { + nbt.setInteger("nextUnusedID", nextUnusedID); + NBTTagCompound riftsNBT = new NBTTagCompound(); + for (Map.Entry entry : riftList.entrySet()) { + riftsNBT.setTag("" + entry.getKey(), Rift.writeToNBT(entry.getValue())); + } + nbt.setTag("riftData", riftsNBT); + } + + public int registerNewRift(Rift rift, World world) { + riftList.put(nextUnusedID, rift); + + nextUnusedID++; + RiftSavedData.get(world).markDirty(); //Notify that this needs to be saved on world save + return nextUnusedID -1; + } + + public void removeRift(int riftID, World world) { + if (riftList.containsKey(riftID)) { + riftList.remove(riftID); + RiftSavedData.get(world).markDirty(); //Notify that this needs to be saved on world save + } + } + + public Rift getRift(int ID) { + return riftList.get(ID); + } +} diff --git a/src/main/java/com/zixiken/dimdoors/shared/RiftSavedData.java b/src/main/java/com/zixiken/dimdoors/shared/RiftSavedData.java new file mode 100644 index 00000000..34ffbeca --- /dev/null +++ b/src/main/java/com/zixiken/dimdoors/shared/RiftSavedData.java @@ -0,0 +1,70 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.zixiken.dimdoors.shared; + +import com.zixiken.dimdoors.DimDoors; +import java.io.File; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; +import net.minecraft.world.storage.MapStorage; + +/** + * + * @author Robijnvogel + */ +public class RiftSavedData extends DDSavedData { + + private static final String DATA_NAME = "dimdoors_RiftSavedData"; + + public RiftSavedData() { + super(DATA_NAME); + } + + public RiftSavedData(String s) { + super(s); + } + + @Override + public File getSaveLocation(World world) { + return new File(super.getSaveLocation(world), "rifts.nbt"); + } + + public static RiftSavedData get(World world) { + MapStorage storage = world.getMapStorage(); + RiftSavedData instance = (RiftSavedData) storage.getOrLoadData(RiftSavedData.class, DATA_NAME); + + if (instance == null) { + instance = new RiftSavedData(); + storage.setData(DATA_NAME, instance); + } + return instance; + } + + @Override + public NBTTagCompound writeToNBT(NBTTagCompound riftnbt) { + + NBTTagCompound rifts = new NBTTagCompound(); + RiftRegistry.Instance.writeToNBT(rifts); + riftnbt.setTag("rifts", rifts); + + //@todo? saveNBTToPath(getSaveLocation(DimDoors.getDefWorld()), riftnbt); + return riftnbt; + } + + @Override + public void readFromNBT(NBTTagCompound riftnbt) { + // Reset + RiftRegistry.Instance.reset(); + + // Load NBT + if (riftnbt != null) { + if (riftnbt.hasKey("rifts")) { + NBTTagCompound rifts = riftnbt.getCompoundTag("rifts"); + RiftRegistry.Instance.readFromNBT(rifts); + } + } + } +}