Applied-Energistics-2-tiler.../src/main/java/appeng/items/storage/ItemSpatialStorageCell.java

207 lines
5.7 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>.
*/
package appeng.items.storage;
import java.util.EnumSet;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
import appeng.api.implementations.TransitionResult;
import appeng.api.implementations.items.ISpatialStorageCell;
import appeng.api.util.WorldCoord;
import appeng.core.features.AEFeature;
import appeng.core.localization.GuiText;
import appeng.core.worlddata.WorldData;
import appeng.items.AEBaseItem;
import appeng.spatial.StorageHelper;
import appeng.spatial.StorageWorldProvider;
import appeng.util.Platform;
2015-06-16 02:44:59 +02:00
import com.google.common.base.Optional;
public class ItemSpatialStorageCell extends AEBaseItem implements ISpatialStorageCell
{
private final int maxRegion;
2014-01-20 17:41:37 +01:00
public ItemSpatialStorageCell( int spatialScale )
{
super( Optional.of( spatialScale + "Cubed" ) );
2014-12-29 15:13:47 +01:00
this.setFeature( EnumSet.of( AEFeature.SpatialIO ) );
this.setMaxStackSize( 1 );
this.maxRegion = spatialScale;
}
@Override
public void addCheckedInformation( ItemStack stack, EntityPlayer player, List<String> lines, boolean displayMoreInfo )
{
2014-12-29 15:13:47 +01:00
WorldCoord wc = this.getStoredSize( stack );
if( wc.x > 0 )
2015-04-29 02:30:53 +02:00
{
lines.add( GuiText.StoredSize.getLocal() + ": " + wc.x + " x " + wc.y + " x " + wc.z );
2015-04-29 02:30:53 +02:00
}
}
@Override
public boolean isSpatialStorage( ItemStack is )
{
return true;
}
@Override
public int getMaxStoredDim( ItemStack is )
{
2014-12-29 15:13:47 +01:00
return this.maxRegion;
}
@Override
public World getWorld( ItemStack is )
{
if( is.hasTagCompound() )
{
NBTTagCompound c = is.getTagCompound();
int dim = c.getInteger( "StorageDim" );
World w = DimensionManager.getWorld( dim );
if( w == null )
{
DimensionManager.initDimension( dim );
w = DimensionManager.getWorld( dim );
}
if( w != null )
{
if( w.provider instanceof StorageWorldProvider )
{
return w;
}
}
}
return null;
}
@Override
public WorldCoord getStoredSize( ItemStack is )
{
if( is.hasTagCompound() )
{
NBTTagCompound c = is.getTagCompound();
if( Platform.isServer() )
{
int dim = c.getInteger( "StorageDim" );
return WorldData.instance().dimensionData().getStoredSize( dim );
}
else
2015-04-29 02:30:53 +02:00
{
return new WorldCoord( c.getInteger( "sizeX" ), c.getInteger( "sizeY" ), c.getInteger( "sizeZ" ) );
2015-04-29 02:30:53 +02:00
}
}
return new WorldCoord( 0, 0, 0 );
}
@Override
public WorldCoord getMin( ItemStack is )
{
2014-12-29 15:13:47 +01:00
World w = this.getWorld( is );
if( w != null )
{
NBTTagCompound info = (NBTTagCompound) w.getWorldInfo().getAdditionalProperty( "storageCell" );
if( info != null )
{
return new WorldCoord( info.getInteger( "minX" ), info.getInteger( "minY" ), info.getInteger( "minZ" ) );
}
}
return new WorldCoord( 0, 0, 0 );
}
@Override
public WorldCoord getMax( ItemStack is )
{
2014-12-29 15:13:47 +01:00
World w = this.getWorld( is );
if( w != null )
{
NBTTagCompound info = (NBTTagCompound) w.getWorldInfo().getAdditionalProperty( "storageCell" );
if( info != null )
{
return new WorldCoord( info.getInteger( "maxX" ), info.getInteger( "maxY" ), info.getInteger( "maxZ" ) );
}
}
return new WorldCoord( 0, 0, 0 );
}
@Override
public TransitionResult doSpatialTransition( ItemStack is, World w, WorldCoord min, WorldCoord max, boolean doTransition )
{
2014-12-29 15:13:47 +01:00
WorldCoord scale = this.getStoredSize( is );
int targetX = max.x - min.x - 1;
int targetY = max.y - min.y - 1;
int targetZ = max.z - min.z - 1;
2014-12-29 15:13:47 +01:00
int maxSize = this.getMaxStoredDim( is );
int floorBuffer = 64;
2014-12-29 15:13:47 +01:00
World destination = this.getWorld( is );
if( ( scale.x == 0 && scale.y == 0 && scale.z == 0 ) || ( scale.x == targetX && scale.y == targetY && scale.z == targetZ ) )
{
if( targetX <= maxSize && targetY <= maxSize && targetZ <= maxSize )
{
if( destination == null )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
destination = this.createNewWorld( is );
2015-04-29 02:30:53 +02:00
}
StorageHelper.getInstance().swapRegions( w, destination, min.x + 1, min.y + 1, min.z + 1, 1, floorBuffer + 1, 1, targetX - 1, targetY - 1, targetZ - 1 );
2014-12-29 15:13:47 +01:00
this.setStoredSize( is, targetX, targetY, targetZ );
return new TransitionResult( true, 0 );
}
}
return new TransitionResult( false, 0 );
}
public World createNewWorld( ItemStack is )
{
NBTTagCompound c = Platform.openNbtData( is );
int newDim = DimensionManager.getNextFreeDimId();
c.setInteger( "StorageDim", newDim );
WorldData.instance().dimensionData().addStorageCell( newDim );
DimensionManager.initDimension( newDim );
return DimensionManager.getWorld( newDim );
}
private void setStoredSize( ItemStack is, int targetX, int targetY, int targetZ )
{
if( is.hasTagCompound() )
{
NBTTagCompound c = is.getTagCompound();
int dim = c.getInteger( "StorageDim" );
c.setInteger( "sizeX", targetX );
c.setInteger( "sizeY", targetY );
c.setInteger( "sizeZ", targetZ );
WorldData.instance().dimensionData().setStoredSize( dim, targetX, targetY, targetZ );
}
}
}