Applied-Energistics-2-tiler.../src/main/java/appeng/me/GridStorage.java

142 lines
2.6 KiB
Java
Raw Normal View History

2014-09-24 02:26:27 +02:00
package appeng.me;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
2014-09-27 19:25:52 +02:00
import java.lang.ref.WeakReference;
import java.util.WeakHashMap;
2014-09-24 02:26:27 +02:00
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridStorage;
import appeng.core.AELog;
import appeng.core.WorldSettings;
public class GridStorage implements IGridStorage
{
2014-09-27 19:25:52 +02:00
private WeakReference<IGrid> internalGrid = null;
2014-09-24 02:26:27 +02:00
final long myID;
final NBTTagCompound data;
public boolean isDirty = false;
2014-09-27 19:25:52 +02:00
private WeakHashMap<GridStorage,Boolean> divlist = new WeakHashMap();
2014-09-24 02:26:27 +02:00
final GridStorageSearch mySearchEntry; // keep myself in the list until I'm
// lost...
/**
* for use with world settings
*
* @param id
* @param gss
*/
public GridStorage(long id, GridStorageSearch gss) {
myID = id;
mySearchEntry = gss;
data = new NBTTagCompound();
}
/**
* for use with world settings
*
* @param input
* @param id
* @param gss
*/
public GridStorage(String input, long id, GridStorageSearch gss) {
myID = id;
mySearchEntry = gss;
NBTTagCompound myTag = null;
try
{
byte[] dbata = javax.xml.bind.DatatypeConverter.parseBase64Binary( input );
myTag = CompressedStreamTools.readCompressed( new ByteArrayInputStream( dbata ) );
}
catch (Throwable t)
{
myTag = new NBTTagCompound();
}
data = myTag;
}
/**
* fake storage.
*/
public GridStorage() {
myID = 0;
mySearchEntry = null;
data = new NBTTagCompound();
}
public String getValue()
{
isDirty = false;
2014-09-27 19:25:52 +02:00
Grid currentGrid = (Grid) getGrid();
if ( currentGrid != null )
2014-09-24 02:26:27 +02:00
{
2014-09-27 19:25:52 +02:00
currentGrid.saveState();
2014-09-24 02:26:27 +02:00
}
try
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
CompressedStreamTools.writeCompressed( data, out );
return javax.xml.bind.DatatypeConverter.printBase64Binary( out.toByteArray() );
}
catch (IOException e)
{
AELog.error( e );
}
return "";
}
@Override
public NBTTagCompound dataObject()
{
return data;
}
@Override
public long getID()
{
return myID;
}
public void markDirty()
{
isDirty = true;
}
public IGrid getGrid()
{
2014-09-27 19:25:52 +02:00
return internalGrid == null ? null : internalGrid.get();
2014-09-24 02:26:27 +02:00
}
public void setGrid(Grid grid)
{
2014-09-27 19:25:52 +02:00
internalGrid = new WeakReference<IGrid>( grid );
2014-09-24 02:26:27 +02:00
}
public void addDivided(GridStorage gs)
{
2014-09-27 19:25:52 +02:00
divlist.put( gs, true );
2014-09-24 02:26:27 +02:00
}
public boolean hasDivided(GridStorage myStorage)
{
2014-09-27 19:25:52 +02:00
return divlist.containsKey( myStorage );
2014-09-24 02:26:27 +02:00
}
public void remove()
{
WorldSettings.getInstance().destroyGridStorage( getID() );
}
}