mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-22 04:52:25 +00:00
Rewrote multiblock data saving, changed simple side names
This commit is contained in:
1 parent
99d6684425
commit
f83d481fd6
11 files changed
+103
-207
No files matched your search
@@ -8,53 +8,22 @@ import java.util.Set;
|
||||
|
||||
import mekanism.api.Coord4D;
|
||||
import mekanism.common.tile.TileEntityMultiblock;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.WorldSavedData;
|
||||
import net.minecraftforge.common.util.Constants.NBT;
|
||||
|
||||
public class MultiblockManager<T extends SynchronizedData<T>>
|
||||
{
|
||||
private static Set<MultiblockManager> managers = new HashSet<MultiblockManager>();
|
||||
|
||||
public static boolean loaded;
|
||||
|
||||
public Class<? extends MultiblockCache<T>> cacheClass;
|
||||
|
||||
public DataHandler dataHandler;
|
||||
|
||||
public String name;
|
||||
|
||||
/** A map containing references to all multiblock inventory caches. */
|
||||
public Map<Integer, MultiblockCache<T>> inventories = new HashMap<Integer, MultiblockCache<T>>();
|
||||
|
||||
public MultiblockManager(String s, Class<? extends MultiblockCache<T>> cache)
|
||||
public MultiblockManager(String s)
|
||||
{
|
||||
name = s;
|
||||
managers.add(this);
|
||||
cacheClass = cache;
|
||||
}
|
||||
|
||||
public void createOrLoad(World world)
|
||||
{
|
||||
if(dataHandler == null)
|
||||
{
|
||||
dataHandler = (DataHandler)world.perWorldStorage.loadData(DataHandler.class, name);
|
||||
|
||||
if(dataHandler == null)
|
||||
{
|
||||
dataHandler = new DataHandler(name);
|
||||
dataHandler.setManager(this);
|
||||
world.perWorldStorage.setData(name, dataHandler);
|
||||
}
|
||||
else {
|
||||
dataHandler.setManager(this);
|
||||
dataHandler.syncManager();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,50 +34,25 @@ public class MultiblockManager<T extends SynchronizedData<T>>
|
||||
*/
|
||||
public MultiblockCache<T> pullInventory(World world, int id)
|
||||
{
|
||||
if(!loaded)
|
||||
{
|
||||
load(world);
|
||||
}
|
||||
|
||||
MultiblockCache<T> toReturn = inventories.get(id);
|
||||
|
||||
for(Coord4D obj : inventories.get(id).locations)
|
||||
{
|
||||
TileEntityMultiblock<T> tileEntity = (TileEntityMultiblock<T>)obj.getTileEntity(world);
|
||||
|
||||
if(tileEntity != null)
|
||||
{
|
||||
System.out.println("RESET " + Coord4D.get(tileEntity));
|
||||
tileEntity.cachedData = tileEntity.getNewCache();
|
||||
tileEntity.cachedID = -1;
|
||||
}
|
||||
}
|
||||
|
||||
inventories.remove(id);
|
||||
dataHandler.markDirty();
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a multiblock cache with the defined inventory ID with the parameterized values.
|
||||
* @param multiblock - multiblock TileEntity
|
||||
*/
|
||||
public void updateCache(IMultiblock<T> multiblock)
|
||||
{
|
||||
try {
|
||||
if(!loaded)
|
||||
{
|
||||
load(((TileEntity)multiblock).getWorldObj());
|
||||
}
|
||||
|
||||
if(!inventories.containsKey(multiblock.getSynchronizedData().inventoryID))
|
||||
{
|
||||
MultiblockCache<T> cache = cacheClass.newInstance();
|
||||
cache.sync((T)multiblock.getSynchronizedData());
|
||||
cache.locations.add(Coord4D.get((TileEntity)multiblock));
|
||||
|
||||
inventories.put(multiblock.getSynchronizedData().inventoryID, cache);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
inventories.get(multiblock.getSynchronizedData().inventoryID).sync((T)multiblock.getSynchronizedData());
|
||||
inventories.get(multiblock.getSynchronizedData().inventoryID).locations.add(Coord4D.get((TileEntity)multiblock));
|
||||
dataHandler.markDirty();
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Grabs a unique inventory ID for a multiblock.
|
||||
* @return unique inventory ID
|
||||
@@ -134,11 +78,6 @@ public class MultiblockManager<T extends SynchronizedData<T>>
|
||||
|
||||
public static void tick(World world)
|
||||
{
|
||||
if(!loaded)
|
||||
{
|
||||
load(world);
|
||||
}
|
||||
|
||||
for(MultiblockManager manager : managers)
|
||||
{
|
||||
manager.tickSelf(world);
|
||||
@@ -183,14 +122,12 @@ public class MultiblockManager<T extends SynchronizedData<T>>
|
||||
for(Coord4D obj : entry.getValue())
|
||||
{
|
||||
inventories.get(entry.getKey()).locations.remove(obj);
|
||||
dataHandler.markDirty();
|
||||
}
|
||||
}
|
||||
|
||||
for(int inventoryID : idsToKill)
|
||||
{
|
||||
inventories.remove(inventoryID);
|
||||
dataHandler.markDirty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,29 +146,17 @@ public class MultiblockManager<T extends SynchronizedData<T>>
|
||||
return ((TileEntityMultiblock)tile1).getManager() == ((TileEntityMultiblock)tile2).getManager();
|
||||
}
|
||||
|
||||
public int getInventoryId(TileEntityMultiblock<T> tile)
|
||||
public void updateCache(TileEntityMultiblock<T> tile)
|
||||
{
|
||||
Coord4D coord = Coord4D.get(tile);
|
||||
|
||||
for(Map.Entry<Integer, MultiblockCache<T>> entry : inventories.entrySet())
|
||||
if(!inventories.containsKey(tile.cachedID))
|
||||
{
|
||||
if(entry.getValue().locations.contains(coord))
|
||||
{
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static void load(World world)
|
||||
{
|
||||
loaded = true;
|
||||
|
||||
for(MultiblockManager manager : managers)
|
||||
{
|
||||
manager.createOrLoad(world);
|
||||
tile.cachedData.locations.add(Coord4D.get(tile));
|
||||
inventories.put(tile.cachedID, tile.cachedData);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
inventories.get(tile.cachedID).locations.add(Coord4D.get(tile));
|
||||
}
|
||||
|
||||
public static void reset()
|
||||
@@ -239,91 +164,6 @@ public class MultiblockManager<T extends SynchronizedData<T>>
|
||||
for(MultiblockManager manager : managers)
|
||||
{
|
||||
manager.inventories.clear();
|
||||
manager.dataHandler = null;
|
||||
}
|
||||
|
||||
loaded = false;
|
||||
}
|
||||
|
||||
public static class DataHandler extends WorldSavedData
|
||||
{
|
||||
public MultiblockManager manager;
|
||||
|
||||
public Map<Integer, MultiblockCache> loadedInventories;
|
||||
|
||||
public DataHandler(String tagName)
|
||||
{
|
||||
super(tagName);
|
||||
}
|
||||
|
||||
public void setManager(MultiblockManager m)
|
||||
{
|
||||
manager = m;
|
||||
}
|
||||
|
||||
public void syncManager()
|
||||
{
|
||||
if(loadedInventories != null)
|
||||
{
|
||||
manager.inventories = loadedInventories;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound nbtTags)
|
||||
{
|
||||
try {
|
||||
String cacheClass = nbtTags.getString("cacheClass");
|
||||
|
||||
NBTTagList list = nbtTags.getTagList("invList", NBT.TAG_COMPOUND);
|
||||
|
||||
loadedInventories = new HashMap<Integer, MultiblockCache>();
|
||||
|
||||
for(int i = 0; i < list.tagCount(); i++)
|
||||
{
|
||||
NBTTagCompound compound = list.getCompoundTagAt(i);
|
||||
MultiblockCache cache = (MultiblockCache)Class.forName(cacheClass).newInstance();
|
||||
cache.load(compound);
|
||||
|
||||
NBTTagList coordsList = compound.getTagList("coordsList", NBT.TAG_COMPOUND);
|
||||
|
||||
for(int j = 0; j < coordsList.tagCount(); j++)
|
||||
{
|
||||
cache.locations.add(Coord4D.read(coordsList.getCompoundTagAt(j)));
|
||||
}
|
||||
|
||||
loadedInventories.put(compound.getInteger("id"), cache);
|
||||
}
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(NBTTagCompound nbtTags)
|
||||
{
|
||||
nbtTags.setString("cacheClass", manager.cacheClass.getName());
|
||||
|
||||
NBTTagList list = new NBTTagList();
|
||||
|
||||
for(Map.Entry<Integer, MultiblockCache> entry : ((Map<Integer, MultiblockCache>)manager.inventories).entrySet())
|
||||
{
|
||||
NBTTagCompound compound = new NBTTagCompound();
|
||||
compound.setInteger("id", entry.getKey());
|
||||
entry.getValue().save(compound);
|
||||
|
||||
NBTTagList coordsList = new NBTTagList();
|
||||
|
||||
for(Coord4D coord : (Set<Coord4D>)entry.getValue().locations)
|
||||
{
|
||||
coordsList.appendTag(coord.write(new NBTTagCompound()));
|
||||
}
|
||||
|
||||
compound.setTag("coordsList", coordsList);
|
||||
list.appendTag(compound);
|
||||
}
|
||||
|
||||
nbtTags.setTag("invList", list);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user