BloodMagic/src/main/java/WayofTime/bloodmagic/tile/TileInventory.java

262 lines
7.9 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.tile;
import WayofTime.bloodmagic.tile.base.TileBase;
2017-08-15 21:30:48 -07:00
import WayofTime.bloodmagic.util.helper.TextHelper;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
2015-11-03 09:11:39 -08:00
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack;
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;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
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;
2017-08-15 21:30:48 -07:00
public class TileInventory extends TileBase implements IInventory {
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;
private int size;
2017-08-15 21:30:48 -07:00
// IInventory
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);
this.size = size;
this.name = name;
initializeItemHandlers();
}
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;
}
@Override
public void deserialize(CompoundNBT tagCompound) {
super.deserialize(tagCompound);
ListNBT tags = tagCompound.getTagList("Items", 10);
2017-01-02 01:18:29 -08:00
inventory = NonNullList.withSize(size, ItemStack.EMPTY);
2017-08-15 21:30:48 -07:00
for (int i = 0; i < tags.tagCount(); i++) {
if (!isSyncedSlot(i)) {
CompoundNBT data = tags.getCompoundTagAt(i);
byte j = data.getByte("Slot");
2017-08-15 21:30:48 -07:00
if (j >= 0 && j < inventory.size()) {
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
}
}
}
}
@Override
public CompoundNBT serialize(CompoundNBT tagCompound) {
super.serialize(tagCompound);
ListNBT tags = new ListNBT();
2017-08-15 21:30:48 -07:00
for (int i = 0; i < inventory.size(); i++) {
if ((!inventory.get(i).isEmpty()) && !isSyncedSlot(i)) {
CompoundNBT data = new CompoundNBT();
data.setByte("Slot", (byte) i);
2017-01-02 01:18:29 -08:00
inventory.get(i).writeToNBT(data);
tags.appendTag(data);
}
}
tagCompound.setTag("Items", tags);
return tagCompound;
}
2017-08-15 21:30:48 -07:00
public void dropItems() {
2015-11-03 09:11:39 -08:00
InventoryHelper.dropInventoryItems(getWorld(), getPos(), this);
}
@Override
2017-08-15 21:30:48 -07:00
public int getSizeInventory() {
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);
}
@Override
2017-08-15 21:30:48 -07:00
public ItemStack decrStackSize(int index, int count) {
if (!getStackInSlot(index).isEmpty()) {
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;
}
2017-01-02 01:18:29 -08:00
return ItemStack.EMPTY;
}
@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;
}
@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();
if (!getWorld().isRemote)
getWorld().notifyBlockUpdate(getPos(), getWorld().getBlockState(getPos()), getWorld().getBlockState(getPos()), 3);
}
@Override
2017-08-15 21:30:48 -07:00
public int getInventoryStackLimit() {
return 64;
}
@Override
public void openInventory(PlayerEntity player) {
}
@Override
public void closeInventory(PlayerEntity player) {
}
@Override
2017-08-15 21:30:48 -07:00
public boolean isItemValidForSlot(int index, ItemStack stack) {
return true;
}
2017-08-15 21:30:48 -07:00
// IWorldNameable
@Override
2017-08-15 21:30:48 -07:00
public int getField(int id) {
return 0;
}
@Override
2017-08-15 21:30:48 -07:00
public void setField(int id, int value) {
}
@Override
2017-08-15 21:30:48 -07:00
public int getFieldCount() {
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);
}
2016-12-12 19:56:36 -08:00
@Override
public boolean isEmpty() {
for (ItemStack stack : inventory)
if (!stack.isEmpty())
return false;
return true;
}
@Override
public boolean isUsableByPlayer(PlayerEntity player) {
2016-12-12 19:56:36 -08:00
return true;
}
@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");
}
@Override
2017-08-15 21:30:48 -07:00
public boolean hasCustomName() {
return true;
}
@Override
2017-08-15 21:30:48 -07:00
public ITextComponent getDisplayName() {
return new StringTextComponent(getName());
}
2017-08-15 21:30:48 -07:00
protected void initializeItemHandlers() {
if (this instanceof ISidedInventory) {
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 {
handlerDown = new InvWrapper(this);
handlerUp = handlerDown;
handlerNorth = handlerDown;
handlerSouth = handlerDown;
handlerWest = handlerDown;
handlerEast = handlerDown;
}
}
@SuppressWarnings("unchecked")
@Override
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;
}
2017-08-15 21:30:48 -07:00
} else if (facing == null && capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
return (T) handlerDown;
}
return super.getCapability(capability, facing);
}
@Override
public boolean hasCapability(Capability<?> capability, Direction facing) {
return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
}
}