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

164 lines
3.6 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 - 2015, AlgorithmX2, All rights reserved.
2014-11-14 12:02:52 +01:00
*
* 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;
2014-09-24 02:26:27 +02:00
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;
2014-12-29 21:59:05 +01:00
2014-09-24 02:26:27 +02:00
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridStorage;
import appeng.core.AELog;
import appeng.core.worlddata.WorldData;
2014-09-24 02:26:27 +02:00
2014-09-24 02:26:27 +02:00
public class GridStorage implements IGridStorage
{
final long myID;
final NBTTagCompound data;
final GridStorageSearch mySearchEntry; // keep myself in the list until I'm
private final WeakHashMap<GridStorage, Boolean> divided = new WeakHashMap<GridStorage, Boolean>();
public boolean isDirty = false;
private WeakReference<IGrid> internalGrid = null;
2015-08-06 18:49:57 +02:00
// lost...
2014-09-24 02:26:27 +02:00
/**
* for use with world settings
2015-02-03 12:04:13 +01:00
*
2015-08-06 18:49:57 +02:00
* @param id ID of grid storage
2014-09-27 23:17:47 +02:00
* @param gss grid storage search
2014-09-24 02:26:27 +02:00
*/
public GridStorage( long id, GridStorageSearch gss )
{
2014-12-29 15:13:47 +01:00
this.myID = id;
this.mySearchEntry = gss;
this.data = new NBTTagCompound();
2014-09-24 02:26:27 +02:00
}
/**
* for use with world settings
2015-02-03 12:04:13 +01:00
*
2014-09-27 23:17:47 +02:00
* @param input array of bytes string
2015-08-06 18:49:57 +02:00
* @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 )
{
2014-12-29 15:13:47 +01:00
this.myID = id;
this.mySearchEntry = gss;
2014-09-24 02:26:27 +02:00
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 )
2014-09-24 02:26:27 +02:00
{
myTag = new NBTTagCompound();
}
2014-12-29 15:13:47 +01:00
this.data = myTag;
2014-09-24 02:26:27 +02:00
}
/**
* fake storage.
*/
public GridStorage()
{
2014-12-29 15:13:47 +01:00
this.myID = 0;
this.mySearchEntry = null;
this.data = new NBTTagCompound();
2014-09-24 02:26:27 +02:00
}
public String getValue()
{
2014-12-29 15:13:47 +01:00
this.isDirty = false;
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
Grid currentGrid = (Grid) this.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();
2014-12-29 15:13:47 +01:00
CompressedStreamTools.writeCompressed( this.data, out );
2014-09-24 02:26:27 +02:00
return javax.xml.bind.DatatypeConverter.printBase64Binary( out.toByteArray() );
}
catch( IOException e )
2014-09-24 02:26:27 +02:00
{
AELog.error( e );
}
return "";
}
public IGrid getGrid()
{
return this.internalGrid == null ? null : this.internalGrid.get();
}
public void setGrid( Grid grid )
{
this.internalGrid = new WeakReference<IGrid>( grid );
}
2014-09-24 02:26:27 +02:00
@Override
public NBTTagCompound dataObject()
{
2014-12-29 15:13:47 +01:00
return this.data;
2014-09-24 02:26:27 +02:00
}
@Override
public long getID()
{
2014-12-29 15:13:47 +01:00
return this.myID;
2014-09-24 02:26:27 +02:00
}
public void markDirty()
{
2014-12-29 15:13:47 +01:00
this.isDirty = true;
2014-09-24 02:26:27 +02:00
}
public void addDivided( GridStorage gs )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.divided.put( gs, true );
2014-09-24 02:26:27 +02:00
}
public boolean hasDivided( GridStorage myStorage )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
return this.divided.containsKey( myStorage );
2014-09-24 02:26:27 +02:00
}
public void remove()
{
WorldData.instance().storageData().destroyGridStorage( this.myID );
2014-09-24 02:26:27 +02:00
}
}