2015-11-02 12:39:44 -08:00
package WayofTime.bloodmagic.tile ;
2015-10-29 20:22:14 -07:00
2016-09-07 17:46:06 -07:00
import WayofTime.bloodmagic.tile.base.TileBase ;
2017-08-15 21:30:48 -07:00
import WayofTime.bloodmagic.util.helper.TextHelper ;
2019-09-22 12:55:43 -07:00
import net.minecraft.entity.player.PlayerEntity ;
2015-10-29 20:22:14 -07:00
import net.minecraft.inventory.IInventory ;
2016-04-13 17:58:06 -04:00
import net.minecraft.inventory.ISidedInventory ;
2015-11-03 09:11:39 -08:00
import net.minecraft.inventory.InventoryHelper ;
2015-10-29 20:22:14 -07:00
import net.minecraft.item.ItemStack ;
2019-09-22 12:55:43 -07:00
import net.minecraft.nbt.CompoundNBT ;
import net.minecraft.nbt.ListNBT ;
import net.minecraft.util.Direction ;
2017-01-02 01:18:29 -08:00
import net.minecraft.util.NonNullList ;
2016-03-17 13:00:44 -07:00
import net.minecraft.util.text.ITextComponent ;
2019-09-22 12:55:43 -07:00
import net.minecraft.util.text.StringTextComponent ;
2016-04-13 17:58:06 -04:00
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-10-29 20:22:14 -07:00
2017-08-15 21:30:48 -07:00
public class TileInventory extends TileBase implements IInventory {
2015-11-28 18:25:46 -08:00
protected int [ ] syncedSlots = new int [ 0 ] ;
2017-01-02 01:18:29 -08:00
protected NonNullList < ItemStack > inventory ;
2017-08-15 21:30:48 -07:00
IItemHandler handlerDown ;
IItemHandler handlerUp ;
IItemHandler handlerNorth ;
IItemHandler handlerSouth ;
IItemHandler handlerWest ;
IItemHandler handlerEast ;
2015-10-29 20:22:14 -07:00
private int size ;
2017-08-15 21:30:48 -07:00
// IInventory
2015-10-29 20:22:14 -07:00
private String name ;
2017-08-15 21:30:48 -07:00
public TileInventory ( int size , String name ) {
2017-01-02 01:18:29 -08:00
this . inventory = NonNullList . withSize ( size , ItemStack . EMPTY ) ;
2015-10-29 20:22:14 -07:00
this . size = size ;
this . name = name ;
2016-04-13 17:58:06 -04:00
initializeItemHandlers ( ) ;
2015-10-29 20:22:14 -07:00
}
2017-08-15 21:30:48 -07:00
protected boolean isSyncedSlot ( int slot ) {
for ( int s : this . syncedSlots ) {
if ( s = = slot ) {
2015-11-27 20:15:19 -05:00
return true ;
}
}
return false ;
}
2015-11-03 08:42:38 -08:00
@Override
2019-09-22 12:55:43 -07:00
public void deserialize ( CompoundNBT tagCompound ) {
2016-09-07 17:49:28 -07:00
super . deserialize ( tagCompound ) ;
2019-09-22 12:55:43 -07:00
ListNBT tags = tagCompound . getTagList ( " Items " , 10 ) ;
2017-01-02 01:18:29 -08:00
inventory = NonNullList . withSize ( size , ItemStack . EMPTY ) ;
2015-11-03 08:42:38 -08:00
2017-08-15 21:30:48 -07:00
for ( int i = 0 ; i < tags . tagCount ( ) ; i + + ) {
if ( ! isSyncedSlot ( i ) ) {
2019-09-22 12:55:43 -07:00
CompoundNBT data = tags . getCompoundTagAt ( i ) ;
2015-11-28 18:25:46 -08:00
byte j = data . getByte ( " Slot " ) ;
2015-11-03 08:42:38 -08:00
2017-08-15 21:30:48 -07:00
if ( j > = 0 & & j < inventory . size ( ) ) {
2017-02-25 17:59:10 -08:00
inventory . set ( j , new ItemStack ( data ) ) ; // No matter how much an i looks like a j, it is not one. They are drastically different characters and cause drastically different things to happen. Apparently I didn't know this at one point. - TehNut
2015-11-27 20:15:19 -05:00
}
2015-11-03 08:42:38 -08:00
}
}
}
@Override
2019-09-22 12:55:43 -07:00
public CompoundNBT serialize ( CompoundNBT tagCompound ) {
2016-09-07 17:49:28 -07:00
super . serialize ( tagCompound ) ;
2019-09-22 12:55:43 -07:00
ListNBT tags = new ListNBT ( ) ;
2015-11-03 08:42:38 -08:00
2017-08-15 21:30:48 -07:00
for ( int i = 0 ; i < inventory . size ( ) ; i + + ) {
if ( ( ! inventory . get ( i ) . isEmpty ( ) ) & & ! isSyncedSlot ( i ) ) {
2019-09-22 12:55:43 -07:00
CompoundNBT data = new CompoundNBT ( ) ;
2015-11-03 08:42:38 -08:00
data . setByte ( " Slot " , ( byte ) i ) ;
2017-01-02 01:18:29 -08:00
inventory . get ( i ) . writeToNBT ( data ) ;
2015-11-03 08:42:38 -08:00
tags . appendTag ( data ) ;
}
}
tagCompound . setTag ( " Items " , tags ) ;
2016-05-19 17:43:33 -07:00
return tagCompound ;
2015-11-03 08:42:38 -08:00
}
2017-08-15 21:30:48 -07:00
public void dropItems ( ) {
2015-11-03 09:11:39 -08:00
InventoryHelper . dropInventoryItems ( getWorld ( ) , getPos ( ) , this ) ;
2015-10-29 20:22:14 -07:00
}
@Override
2017-08-15 21:30:48 -07:00
public int getSizeInventory ( ) {
2015-10-29 20:22:14 -07:00
return size ;
}
@Override
2017-08-15 21:30:48 -07:00
public ItemStack getStackInSlot ( int index ) {
2017-01-02 01:18:29 -08:00
return inventory . get ( index ) ;
2015-10-29 20:22:14 -07:00
}
@Override
2017-08-15 21:30:48 -07:00
public ItemStack decrStackSize ( int index , int count ) {
if ( ! getStackInSlot ( index ) . isEmpty ( ) ) {
2016-03-18 13:05:57 -07:00
if ( ! getWorld ( ) . isRemote )
getWorld ( ) . notifyBlockUpdate ( getPos ( ) , getWorld ( ) . getBlockState ( getPos ( ) ) , getWorld ( ) . getBlockState ( getPos ( ) ) , 3 ) ;
2017-08-15 21:30:48 -07:00
if ( getStackInSlot ( index ) . getCount ( ) < = count ) {
2017-01-02 01:18:29 -08:00
ItemStack itemStack = inventory . get ( index ) ;
inventory . set ( index , ItemStack . EMPTY ) ;
2015-11-27 20:15:19 -05:00
markDirty ( ) ;
return itemStack ;
}
2017-01-02 01:18:29 -08:00
ItemStack itemStack = inventory . get ( index ) . splitStack ( count ) ;
2015-11-27 20:15:19 -05:00
markDirty ( ) ;
return itemStack ;
}
2015-10-29 20:22:14 -07:00
2017-01-02 01:18:29 -08:00
return ItemStack . EMPTY ;
2015-10-29 20:22:14 -07:00
}
@Override
2017-08-15 21:30:48 -07:00
public ItemStack removeStackFromSlot ( int slot ) {
if ( ! inventory . get ( slot ) . isEmpty ( ) ) {
2017-01-02 01:18:29 -08:00
ItemStack itemStack = inventory . get ( slot ) ;
setInventorySlotContents ( slot , ItemStack . EMPTY ) ;
2015-11-27 20:15:19 -05:00
return itemStack ;
}
2017-01-02 01:18:29 -08:00
return ItemStack . EMPTY ;
2015-10-29 20:22:14 -07:00
}
@Override
2017-08-15 21:30:48 -07:00
public void setInventorySlotContents ( int slot , ItemStack stack ) {
2017-01-02 01:18:29 -08:00
inventory . set ( slot , stack ) ;
2016-12-12 19:56:36 -08:00
if ( ! stack . isEmpty ( ) & & stack . getCount ( ) > getInventoryStackLimit ( ) )
stack . setCount ( getInventoryStackLimit ( ) ) ;
2015-11-27 20:15:19 -05:00
markDirty ( ) ;
2016-03-18 13:05:57 -07:00
if ( ! getWorld ( ) . isRemote )
getWorld ( ) . notifyBlockUpdate ( getPos ( ) , getWorld ( ) . getBlockState ( getPos ( ) ) , getWorld ( ) . getBlockState ( getPos ( ) ) , 3 ) ;
2015-10-29 20:22:14 -07:00
}
@Override
2017-08-15 21:30:48 -07:00
public int getInventoryStackLimit ( ) {
2015-10-29 20:22:14 -07:00
return 64 ;
}
@Override
2019-09-22 12:55:43 -07:00
public void openInventory ( PlayerEntity player ) {
2015-10-29 20:22:14 -07:00
}
@Override
2019-09-22 12:55:43 -07:00
public void closeInventory ( PlayerEntity player ) {
2015-10-29 20:22:14 -07:00
}
@Override
2017-08-15 21:30:48 -07:00
public boolean isItemValidForSlot ( int index , ItemStack stack ) {
2015-10-29 20:22:14 -07:00
return true ;
}
2017-08-15 21:30:48 -07:00
// IWorldNameable
2015-10-29 20:22:14 -07:00
@Override
2017-08-15 21:30:48 -07:00
public int getField ( int id ) {
2015-10-29 20:22:14 -07:00
return 0 ;
}
@Override
2017-08-15 21:30:48 -07:00
public void setField ( int id , int value ) {
2015-10-29 20:22:14 -07:00
}
@Override
2017-08-15 21:30:48 -07:00
public int getFieldCount ( ) {
2015-10-29 20:22:14 -07:00
return 0 ;
}
@Override
2017-08-15 21:30:48 -07:00
public void clear ( ) {
2017-01-02 01:18:29 -08:00
this . inventory = NonNullList . withSize ( size , ItemStack . EMPTY ) ;
2015-10-29 20:22:14 -07:00
}
2016-12-12 19:56:36 -08:00
@Override
public boolean isEmpty ( ) {
for ( ItemStack stack : inventory )
if ( ! stack . isEmpty ( ) )
return false ;
return true ;
}
@Override
2019-09-22 12:55:43 -07:00
public boolean isUsableByPlayer ( PlayerEntity player ) {
2016-12-12 19:56:36 -08:00
return true ;
}
2015-10-29 20:22:14 -07:00
@Override
2017-08-15 21:30:48 -07:00
public String getName ( ) {
2017-01-02 01:18:29 -08:00
return TextHelper . localize ( " tile.bloodmagic. " + name + " .name " ) ;
2015-10-29 20:22:14 -07:00
}
@Override
2017-08-15 21:30:48 -07:00
public boolean hasCustomName ( ) {
2015-10-29 20:22:14 -07:00
return true ;
}
@Override
2017-08-15 21:30:48 -07:00
public ITextComponent getDisplayName ( ) {
2019-09-22 12:55:43 -07:00
return new StringTextComponent ( getName ( ) ) ;
2015-10-29 20:22:14 -07:00
}
2016-04-13 17:58:06 -04:00
2017-08-15 21:30:48 -07:00
protected void initializeItemHandlers ( ) {
if ( this instanceof ISidedInventory ) {
2019-09-22 12:55:43 -07:00
handlerDown = new SidedInvWrapper ( ( ISidedInventory ) this , Direction . DOWN ) ;
handlerUp = new SidedInvWrapper ( ( ISidedInventory ) this , Direction . UP ) ;
handlerNorth = new SidedInvWrapper ( ( ISidedInventory ) this , Direction . NORTH ) ;
handlerSouth = new SidedInvWrapper ( ( ISidedInventory ) this , Direction . SOUTH ) ;
handlerWest = new SidedInvWrapper ( ( ISidedInventory ) this , Direction . WEST ) ;
handlerEast = new SidedInvWrapper ( ( ISidedInventory ) this , Direction . EAST ) ;
2017-08-15 21:30:48 -07:00
} else {
2016-04-13 17:58:06 -04:00
handlerDown = new InvWrapper ( this ) ;
handlerUp = handlerDown ;
handlerNorth = handlerDown ;
handlerSouth = handlerDown ;
handlerWest = handlerDown ;
handlerEast = handlerDown ;
}
}
@SuppressWarnings ( " unchecked " )
@Override
2019-09-22 12:55:43 -07:00
public < T > T getCapability ( Capability < T > capability , Direction facing ) {
2017-08-15 21:30:48 -07:00
if ( facing ! = null & & capability = = CapabilityItemHandler . ITEM_HANDLER_CAPABILITY ) {
switch ( facing ) {
case DOWN :
return ( T ) handlerDown ;
case EAST :
return ( T ) handlerEast ;
case NORTH :
return ( T ) handlerNorth ;
case SOUTH :
return ( T ) handlerSouth ;
case UP :
return ( T ) handlerUp ;
case WEST :
return ( T ) handlerWest ;
2016-04-13 17:58:06 -04:00
}
2017-08-15 21:30:48 -07:00
} else if ( facing = = null & & capability = = CapabilityItemHandler . ITEM_HANDLER_CAPABILITY ) {
2016-06-29 08:26:24 -04:00
return ( T ) handlerDown ;
2016-04-13 17:58:06 -04:00
}
return super . getCapability ( capability , facing ) ;
}
@Override
2019-09-22 12:55:43 -07:00
public boolean hasCapability ( Capability < ? > capability , Direction facing ) {
2016-04-13 17:58:06 -04:00
return capability = = CapabilityItemHandler . ITEM_HANDLER_CAPABILITY | | super . hasCapability ( capability , facing ) ;
}
2015-10-29 20:22:14 -07:00
}