2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
2015-05-16 20:48:32 +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.facade;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2014-12-29 21:59:05 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-12-29 21:59:05 +01:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
import appeng.api.AEApi;
|
|
|
|
import appeng.api.parts.IFacadeContainer;
|
|
|
|
import appeng.api.parts.IFacadePart;
|
|
|
|
import appeng.api.parts.IPartHost;
|
2015-05-16 20:48:32 +02:00
|
|
|
import appeng.integration.IntegrationRegistry;
|
2014-09-24 02:26:27 +02:00
|
|
|
import appeng.integration.IntegrationType;
|
2015-06-08 01:41:35 +02:00
|
|
|
import appeng.integration.abstraction.IBuildCraftTransport;
|
2014-09-24 02:26:27 +02:00
|
|
|
import appeng.items.parts.ItemFacade;
|
|
|
|
import appeng.parts.CableBusStorage;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
public class FacadeContainer implements IFacadeContainer
|
|
|
|
{
|
|
|
|
|
|
|
|
final int facades = 6;
|
|
|
|
final CableBusStorage storage;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
public FacadeContainer( CableBusStorage cbs )
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.storage = cbs;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-09-28 20:34:00 +02:00
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public boolean addFacade( IFacadePart a )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.getFacade( a.getSide() ) == null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.storage.setFacade( a.getSide().ordinal(), a );
|
|
|
|
return true;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
|
|
|
public void removeFacade( IPartHost host, ForgeDirection side )
|
|
|
|
{
|
|
|
|
if( side != null && side != ForgeDirection.UNKNOWN )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.storage.getFacade( side.ordinal() ) != null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.storage.setFacade( side.ordinal(), null );
|
|
|
|
if( host != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
host.markForUpdate();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 20:34:00 +02:00
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public IFacadePart getFacade( ForgeDirection s )
|
|
|
|
{
|
|
|
|
return this.storage.getFacade( s.ordinal() );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void rotateLeft()
|
|
|
|
{
|
|
|
|
IFacadePart[] newFacades = new FacadePart[6];
|
|
|
|
|
|
|
|
newFacades[ForgeDirection.UP.ordinal()] = this.storage.getFacade( ForgeDirection.UP.ordinal() );
|
|
|
|
newFacades[ForgeDirection.DOWN.ordinal()] = this.storage.getFacade( ForgeDirection.DOWN.ordinal() );
|
|
|
|
|
|
|
|
newFacades[ForgeDirection.EAST.ordinal()] = this.storage.getFacade( ForgeDirection.NORTH.ordinal() );
|
|
|
|
newFacades[ForgeDirection.SOUTH.ordinal()] = this.storage.getFacade( ForgeDirection.EAST.ordinal() );
|
|
|
|
|
|
|
|
newFacades[ForgeDirection.WEST.ordinal()] = this.storage.getFacade( ForgeDirection.SOUTH.ordinal() );
|
|
|
|
newFacades[ForgeDirection.NORTH.ordinal()] = this.storage.getFacade( ForgeDirection.WEST.ordinal() );
|
|
|
|
|
|
|
|
for( int x = 0; x < this.facades; x++ )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.storage.setFacade( x, newFacades[x] );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToNBT( NBTTagCompound c )
|
|
|
|
{
|
|
|
|
for( int x = 0; x < this.facades; x++ )
|
|
|
|
{
|
|
|
|
if( this.storage.getFacade( x ) != null )
|
|
|
|
{
|
|
|
|
NBTTagCompound data = new NBTTagCompound();
|
|
|
|
this.storage.getFacade( x ).getItemStack().writeToNBT( data );
|
|
|
|
c.setTag( "facade:" + x, data );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean readFromStream( ByteBuf out ) throws IOException
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
int facadeSides = out.readByte();
|
|
|
|
|
|
|
|
boolean changed = false;
|
|
|
|
|
2014-12-30 22:42:25 +01:00
|
|
|
int[] ids = new int[2];
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = 0; x < this.facades; x++ )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
ForgeDirection side = ForgeDirection.getOrientation( x );
|
2015-04-03 08:54:31 +02:00
|
|
|
int ix = ( 1 << x );
|
|
|
|
if( ( facadeSides & ix ) == ix )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
ids[0] = out.readInt();
|
|
|
|
ids[1] = out.readInt();
|
|
|
|
boolean isBC = ids[0] < 0;
|
|
|
|
ids[0] = Math.abs( ids[0] );
|
|
|
|
|
2015-06-08 01:41:35 +02:00
|
|
|
if( isBC && IntegrationRegistry.INSTANCE.isEnabled( IntegrationType.BuildCraftTransport ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-06-08 01:41:35 +02:00
|
|
|
final IBuildCraftTransport bc = (IBuildCraftTransport) IntegrationRegistry.INSTANCE.getInstance( IntegrationType.BuildCraftTransport );
|
|
|
|
final IFacadePart created = bc.createFacadePart( (Block) Block.blockRegistry.getObjectById( ids[0] ), ids[1], side );
|
2014-12-29 15:13:47 +01:00
|
|
|
changed = changed || this.storage.getFacade( x ) == null;
|
2015-06-08 01:41:35 +02:00
|
|
|
|
|
|
|
this.storage.setFacade( x, created );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
else if( !isBC )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( Item facadeItem : AEApi.instance().definitions().items().facade().maybeItem().asSet() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-01-03 02:53:14 +01:00
|
|
|
ItemFacade ifa = (ItemFacade) facadeItem;
|
|
|
|
ItemStack facade = ifa.createFromIDs( ids );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( facade != null )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
|
|
|
changed = changed || this.storage.getFacade( x ) == null;
|
|
|
|
this.storage.setFacade( x, ifa.createPartFromItemStack( facade, side ) );
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
changed = changed || this.storage.getFacade( x ) != null;
|
|
|
|
this.storage.setFacade( x, null );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2014-09-28 20:34:00 +02:00
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public void readFromNBT( NBTTagCompound c )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = 0; x < this.facades; x++ )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.storage.setFacade( x, null );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
NBTTagCompound t = c.getCompoundTag( "facade:" + x );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( t != null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
ItemStack is = ItemStack.loadItemStackFromNBT( t );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( is != null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
Item i = is.getItem();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( i instanceof IFacadeItem )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.storage.setFacade( x, ( (IFacadeItem) i ).createPartFromItemStack( is, ForgeDirection.getOrientation( x ) ) );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
else
|
|
|
|
{
|
2015-06-08 01:41:35 +02:00
|
|
|
if( IntegrationRegistry.INSTANCE.isEnabled( IntegrationType.BuildCraftTransport ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-06-08 01:41:35 +02:00
|
|
|
final IBuildCraftTransport bc = (IBuildCraftTransport) IntegrationRegistry.INSTANCE.getInstance( IntegrationType.BuildCraftTransport );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( bc.isFacade( is ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.storage.setFacade( x, bc.createFacadePart( is, ForgeDirection.getOrientation( x ) ) );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 20:34:00 +02:00
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public void writeToStream( ByteBuf out ) throws IOException
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
int facadeSides = 0;
|
|
|
|
for( int x = 0; x < this.facades; x++ )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.getFacade( ForgeDirection.getOrientation( x ) ) != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
facadeSides |= ( 1 << x );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
out.writeByte( (byte) facadeSides );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = 0; x < this.facades; x++ )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
IFacadePart part = this.getFacade( ForgeDirection.getOrientation( x ) );
|
|
|
|
if( part != null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
int itemID = Item.getIdFromItem( part.getItem() );
|
|
|
|
int dmgValue = part.getItemDamage();
|
2015-06-08 01:41:35 +02:00
|
|
|
out.writeInt( itemID * ( part.notAEFacade() ? -1 : 1 ) );
|
2015-04-03 08:54:31 +02:00
|
|
|
out.writeInt( dmgValue );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 20:34:00 +02:00
|
|
|
@Override
|
2014-09-24 02:26:27 +02:00
|
|
|
public boolean isEmpty()
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = 0; x < this.facades; x++ )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.storage.getFacade( x ) != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|