Applied-Energistics-2-tiler.../src/main/java/appeng/tile/AEBaseInvTile.java

257 lines
6.5 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 - 2014, AlgorithmX2, All rights reserved.
*
* 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.tile;
import java.util.EnumMap;
import javax.annotation.Nullable;
2014-09-24 02:26:27 +02:00
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.wrapper.InvWrapper;
import net.minecraftforge.items.wrapper.SidedInvWrapper;
2015-09-29 15:47:55 +02:00
2014-09-24 02:26:27 +02:00
import appeng.block.AEBaseBlock;
import appeng.tile.events.TileEventType;
import appeng.tile.inventory.IAEAppEngInventory;
import appeng.tile.inventory.InvOperation;
2014-09-24 02:26:27 +02:00
public abstract class AEBaseInvTile extends AEBaseTile implements ISidedInventory, IAEAppEngInventory
{
private EnumMap<EnumFacing, IItemHandler> sidedItemHandler = new EnumMap<>( EnumFacing.class );
private IItemHandler itemHandler;
2015-09-29 15:47:55 +02:00
@Override
public String getName()
2015-09-29 15:47:55 +02:00
{
return this.getCustomName();
}
2014-09-24 02:26:27 +02:00
@TileEvent( TileEventType.WORLD_NBT_READ )
2015-09-30 14:24:40 +02:00
public void readFromNBT_AEBaseInvTile( final net.minecraft.nbt.NBTTagCompound data )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final IInventory inv = this.getInternalInventory();
final NBTTagCompound opt = data.getCompoundTag( "inv" );
for( int x = 0; x < inv.getSizeInventory(); x++ )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final NBTTagCompound item = opt.getCompoundTag( "item" + x );
2016-12-08 12:42:00 +01:00
inv.setInventorySlotContents( x, new ItemStack( item ) );
2014-09-24 02:26:27 +02:00
}
}
public abstract IInventory getInternalInventory();
@TileEvent( TileEventType.WORLD_NBT_WRITE )
2015-09-30 14:24:40 +02:00
public void writeToNBT_AEBaseInvTile( final net.minecraft.nbt.NBTTagCompound data )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final IInventory inv = this.getInternalInventory();
final NBTTagCompound opt = new NBTTagCompound();
for( int x = 0; x < inv.getSizeInventory(); x++ )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final NBTTagCompound item = new NBTTagCompound();
final ItemStack is = this.getStackInSlot( x );
if( is != null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
is.writeToNBT( item );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
opt.setTag( "item" + x, item );
}
data.setTag( "inv", opt );
}
@Override
public int getSizeInventory()
{
2014-12-29 15:13:47 +01:00
return this.getInternalInventory().getSizeInventory();
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack getStackInSlot( final int i )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
return this.getInternalInventory().getStackInSlot( i );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack decrStackSize( final int i, final int j )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
return this.getInternalInventory().decrStackSize( i, j );
2014-09-24 02:26:27 +02:00
}
@Override
public ItemStack removeStackFromSlot( final int i )
2014-09-24 02:26:27 +02:00
{
return ItemStack.EMPTY;
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public void setInventorySlotContents( final int i, @Nullable final ItemStack itemstack )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.getInternalInventory().setInventorySlotContents( i, itemstack );
2014-09-24 02:26:27 +02:00
}
/**
* Returns if the inventory is named
*/
2014-09-24 02:26:27 +02:00
@Override
2015-06-16 02:44:59 +02:00
public boolean hasCustomName()
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:26:54 +02:00
return this.getInternalInventory().hasCustomName();
2014-09-24 02:26:27 +02:00
}
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
2016-12-08 12:42:00 +01:00
public boolean isUsableByPlayer( final EntityPlayer p )
2014-09-24 02:26:27 +02:00
{
final double squaredMCReach = 64.0D;
2016-12-08 12:42:00 +01:00
return this.world.getTileEntity( this.pos ) == this && p.getDistanceSq( this.pos.getX() + 0.5D, this.pos.getY() + 0.5D, this.pos.getZ() + 0.5D ) <= squaredMCReach;
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public void openInventory( final EntityPlayer player )
2015-09-29 15:47:55 +02:00
{
}
;
2014-09-24 02:26:27 +02:00
@Override
2015-09-30 14:24:40 +02:00
public void closeInventory( final EntityPlayer player )
2014-09-24 02:26:27 +02:00
{
2015-09-29 15:47:55 +02:00
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean isItemValidForSlot( final int i, final ItemStack itemstack )
2014-09-24 02:26:27 +02:00
{
return true;
}
@Override
public abstract void onChangeInventory( IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added );
2014-09-24 02:26:27 +02:00
@Override
2015-09-30 14:24:40 +02:00
public int[] getSlotsForFace( final EnumFacing side )
2014-09-24 02:26:27 +02:00
{
2016-12-08 12:42:00 +01:00
final Block blk = this.world.getBlockState( this.pos ).getBlock();
if( blk instanceof AEBaseBlock )
2014-09-24 02:26:27 +02:00
{
2015-06-16 02:44:59 +02:00
return this.getAccessibleSlotsBySide( ( (AEBaseBlock) blk ).mapRotation( this, side ) );
2014-09-24 02:26:27 +02:00
}
2015-06-16 02:44:59 +02:00
return this.getAccessibleSlotsBySide( side );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean canInsertItem( final int slotIndex, final ItemStack insertingItem, final EnumFacing side )
2014-09-24 02:26:27 +02:00
{
return this.isItemValidForSlot( slotIndex, insertingItem );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean canExtractItem( final int slotIndex, final ItemStack extractedItem, final EnumFacing side )
2014-09-24 02:26:27 +02:00
{
return true;
2014-09-24 02:26:27 +02:00
}
2015-09-29 15:47:55 +02:00
2015-06-16 02:44:59 +02:00
@Override
public void clear()
{
this.getInternalInventory().clear();
}
2015-09-29 15:47:55 +02:00
2015-06-16 02:44:59 +02:00
@Override
2015-09-30 14:24:40 +02:00
public int getField( final int id )
2015-06-16 02:44:59 +02:00
{
return 0;
}
2015-09-29 15:47:55 +02:00
2015-06-16 02:44:59 +02:00
@Override
2015-09-30 14:24:40 +02:00
public void setField( final int id, final int value )
2015-06-16 02:44:59 +02:00
{
2015-09-29 15:47:55 +02:00
2015-06-16 02:44:59 +02:00
}
2015-09-29 15:47:55 +02:00
2015-06-16 02:44:59 +02:00
@Override
public int getFieldCount()
{
return 0;
}
2015-09-29 15:47:55 +02:00
2015-06-16 02:44:59 +02:00
@Override
public ITextComponent getDisplayName()
2015-06-16 02:44:59 +02:00
{
2015-09-30 14:26:54 +02:00
if( this.hasCustomName() )
2015-09-29 15:47:55 +02:00
{
return new TextComponentString( this.getCustomName() );
2015-09-29 15:47:55 +02:00
}
return new TextComponentTranslation( this.getBlockType().getUnlocalizedName() );
2015-06-16 02:44:59 +02:00
}
2014-09-24 02:26:27 +02:00
2015-06-16 02:44:59 +02:00
public abstract int[] getAccessibleSlotsBySide( EnumFacing whichSide );
@Override
public boolean hasCapability( Capability<?> capability, EnumFacing facing )
{
return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability( capability, facing );
}
@SuppressWarnings( "unchecked" )
@Override
public <T> T getCapability( Capability<T> capability, @Nullable EnumFacing facing )
{
if( capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY )
{
if( facing == null )
{
if( itemHandler == null )
{
itemHandler = new InvWrapper( getInternalInventory() );
}
return (T) itemHandler;
}
else
{
return (T) sidedItemHandler.computeIfAbsent( facing, side -> new SidedInvWrapper( this, side ) );
}
}
return super.getCapability( capability, facing );
}
2014-09-24 02:26:27 +02:00
}