2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
2015-05-28 20:01:21 +02:00
|
|
|
* 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;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
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;
|
2015-05-28 20:01:21 +02:00
|
|
|
import appeng.core.worlddata.WorldData;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +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
|
2015-04-03 08:54:31 +02:00
|
|
|
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
|
|
|
|
2015-04-03 08:54:31 +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
|
|
|
*/
|
2015-09-25 23:10:56 +02:00
|
|
|
public GridStorage( final long id, final GridStorageSearch gss )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
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
|
|
|
*/
|
2015-09-25 23:10:56 +02:00
|
|
|
public GridStorage( final String input, final long id, final GridStorageSearch gss )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
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
|
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final byte[] byteData = javax.xml.bind.DatatypeConverter.parseBase64Binary( input );
|
2014-09-28 11:47:17 +02:00
|
|
|
myTag = CompressedStreamTools.readCompressed( new ByteArrayInputStream( byteData ) );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
2015-09-25 23:10:56 +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.
|
|
|
|
*/
|
2015-04-03 08:54:31 +02:00
|
|
|
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
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
final Grid currentGrid = (Grid) this.getGrid();
|
2015-04-03 08:54:31 +02:00
|
|
|
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
|
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
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() );
|
|
|
|
}
|
2015-09-25 23:10:56 +02:00
|
|
|
catch( final IOException e )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
AELog.error( e );
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
public IGrid getGrid()
|
|
|
|
{
|
|
|
|
return this.internalGrid == null ? null : this.internalGrid.get();
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public void setGrid( final Grid grid )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public 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
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public 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
|
|
|
}
|
|
|
|
|
|
|
|
public void remove()
|
|
|
|
{
|
2015-05-28 20:01:21 +02:00
|
|
|
WorldData.instance().storageData().destroyGridStorage( this.myID );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|