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

160 lines
3.5 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
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;
private final WeakHashMap<GridStorage,Boolean> divided = new WeakHashMap<GridStorage,Boolean>();
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
*
2014-09-27 23:17:47 +02:00
* @param id ID of grid storage
* @param gss grid storage search
2014-09-24 02:26:27 +02:00
*/
public GridStorage(long id, GridStorageSearch gss) {
myID = id;
mySearchEntry = gss;
data = new NBTTagCompound();
}
/**
* for use with world settings
*
2014-09-27 23:17:47 +02:00
* @param input array of bytes string
* @param id ID of grid storage
* @param gss grid storage search
2014-09-24 02:26:27 +02:00
*/
public GridStorage(String input, long id, GridStorageSearch gss) {
myID = id;
mySearchEntry = gss;
NBTTagCompound myTag = null;
try
{
byte[] byteData = javax.xml.bind.DatatypeConverter.parseBase64Binary( input );
myTag = CompressedStreamTools.readCompressed( new ByteArrayInputStream( byteData ) );
2014-09-24 02:26:27 +02:00
}
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)
{
divided.put( gs, true );
2014-09-24 02:26:27 +02:00
}
public boolean hasDivided(GridStorage myStorage)
{
return divided.containsKey( myStorage );
2014-09-24 02:26:27 +02:00
}
public void remove()
{
WorldSettings.getInstance().destroyGridStorage( getID() );
}
}