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

156 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
{
private final long myID;
private final NBTTagCompound data;
private final GridStorageSearch mySearchEntry; // keep myself in the list until I'm
private final WeakHashMap<GridStorage, Boolean> divided = new WeakHashMap<GridStorage, Boolean>();
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( final long id, final 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( final String input, final long id, final 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
{
final byte[] byteData = javax.xml.bind.DatatypeConverter.parseBase64Binary( input );
myTag = CompressedStreamTools.readCompressed( new ByteArrayInputStream( byteData ) );
2014-09-24 02:26:27 +02:00
}
catch( final 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()
{
final 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
{
final 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( final IOException e )
2014-09-24 02:26:27 +02:00
{
AELog.debug( e );
2014-09-24 02:26:27 +02:00
}
return "";
}
public IGrid getGrid()
{
return this.internalGrid == null ? null : this.internalGrid.get();
}
void setGrid( final 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
}
void addDivided( final 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
}
boolean hasDivided( final 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
}
void remove()
2014-09-24 02:26:27 +02:00
{
WorldData.instance().storageData().destroyGridStorage( this.myID );
2014-09-24 02:26:27 +02:00
}
}