-
-
Save DiabolicaTrix/d849d0b351197871e6a689cc2de048c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xyz.diabolicatrixlab.showcasemod.tileentity; | |
import net.minecraft.client.resources.I18n; | |
import net.minecraft.entity.player.EntityPlayer; | |
import net.minecraft.inventory.IInventory; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.nbt.NBTTagCompound; | |
import net.minecraft.tileentity.TileEntity; | |
import net.minecraft.util.EnumFacing; | |
import net.minecraftforge.common.capabilities.Capability; | |
import net.minecraftforge.items.CapabilityItemHandler; | |
import net.minecraftforge.items.ItemStackHandler; | |
public class TileEntityBase extends TileEntity implements IInventory { | |
private String defaultName; | |
private String customName; | |
private int size; | |
private ItemStackHandlerBase handler = new ItemStackHandlerBase(size){ | |
@Override | |
protected void onContentsChanged(int slot) { | |
TileEntityBase.this.markDirty(); | |
} | |
}; | |
public TileEntityBase(String defaultName, int size) { | |
this.defaultName = defaultName; | |
this.size = size; | |
this.handler.setSize(size); | |
} | |
@Override | |
public void readFromNBT(NBTTagCompound compound) { | |
super.readFromNBT(compound); | |
if(compound.hasKey("items")){ | |
handler.deserializeNBT((NBTTagCompound) compound.getTag("items")); | |
} | |
if(compound.hasKey("CustomName")){ | |
this.customName = compound.getString("CustomName"); | |
} | |
} | |
@Override | |
public NBTTagCompound writeToNBT(NBTTagCompound compound) { | |
super.writeToNBT(compound); | |
compound.setTag("items", handler.serializeNBT()); | |
if(this.hasCustomName()) { | |
compound.setString("CustomName", this.customName); | |
} | |
return compound; | |
} | |
@Override | |
public String getName() { | |
return this.hasCustomName() ? this.getName() : I18n.format(defaultName); | |
} | |
@Override | |
public boolean hasCustomName() { | |
return this.customName != null && !this.customName.isEmpty(); | |
} | |
@Override | |
public int getSizeInventory() { | |
return this.size; | |
} | |
@Override | |
public boolean isEmpty() { | |
for (int i = 0; i< this.handler.getSlots(); i++) | |
{ | |
if(!this.getStackInSlot(i).isEmpty()) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
@Override | |
public ItemStack getStackInSlot(int index) { | |
return this.handler.getStackInSlot(index); | |
} | |
@Override | |
public ItemStack decrStackSize(int index, int count) { | |
ItemStack is = this.handler.extractItem(index, count, false); | |
if(!is.isEmpty()){ | |
this.markDirty(); | |
} | |
return is; | |
} | |
@Override | |
public ItemStack removeStackFromSlot(int index) { | |
return this.handler.extractItem(index, this.handler.getStackInSlot(index).getCount(), false); | |
} | |
@Override | |
public void setInventorySlotContents(int index, ItemStack stack) { | |
this.handler.setStackInSlot(index, stack); | |
if(stack.getCount() > this.getInventoryStackLimit()){ | |
stack.setCount(this.getInventoryStackLimit()); | |
} | |
this.markDirty(); | |
} | |
@Override | |
public int getInventoryStackLimit() { | |
return 64; | |
} | |
@Override | |
public boolean hasCapability(Capability<?> capability, EnumFacing facing) { | |
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { | |
return true; | |
} | |
return super.hasCapability(capability, facing); | |
} | |
@Override | |
public <T> T getCapability(Capability<T> capability, EnumFacing facing) { | |
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { | |
return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(handler); | |
} | |
return super.getCapability(capability, facing); | |
} | |
@Override | |
public boolean isUsableByPlayer(EntityPlayer player) { | |
return !isInvalid() && player.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D; | |
} | |
@Override | |
public void openInventory(EntityPlayer player) { | |
} | |
@Override | |
public void closeInventory(EntityPlayer player) { | |
} | |
@Override | |
public boolean isItemValidForSlot(int index, ItemStack stack) { | |
return true; | |
} | |
@Override | |
public int getField(int id) { | |
return 0; | |
} | |
@Override | |
public void setField(int id, int value) { | |
} | |
@Override | |
public int getFieldCount() { | |
return 0; | |
} | |
@Override | |
public void clear() { | |
} | |
public ItemStackHandler getHandler(){ | |
return this.handler; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment