Created a backbone for saving DimDoors specific worldsave data

This commit is contained in:
Mathijs Riezebos 2017-01-12 15:15:53 +01:00
parent 30e8051306
commit 204e430be8
12 changed files with 526 additions and 7 deletions

View file

@ -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();
}

View file

@ -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);
}
}

View file

@ -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();
}

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -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/");
}
}

View file

@ -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<String> playerUUIDs;
private final List<Integer> 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;
}
}

View file

@ -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<Integer, Pocket> 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<Integer, Pocket> 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);
}
}

View file

@ -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);
}
}
}
}

View file

@ -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.
}
}

View file

@ -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<Integer, Rift> 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<Integer, Rift> 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);
}
}

View file

@ -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);
}
}
}
}