Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.modmcdl.magitech.tileenitity;
- import com.modmcdl.magitech.recipe.PestleRecipe;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.ItemStackHelper;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.NonNullList;
- import net.minecraft.util.text.ITextComponent;
- import net.minecraft.util.text.TextComponentString;
- import net.minecraft.util.text.TextComponentTranslation;
- public class TileEntityPestle extends TileEntity implements IInventory{
- private NonNullList<ItemStack> inventory = NonNullList.<ItemStack>withSize(3, ItemStack.EMPTY);
- private String customName;
- @Override
- public String getName() {
- return this.hasCustomName() ? this.customName : "container:pestle";
- }
- @Override
- public boolean hasCustomName() {
- return this.customName != null && !this.customName.isEmpty();
- }
- public void setCustomName(String customName) {
- this.customName = customName;
- }
- @Override
- public ITextComponent getDisplayName() {
- return this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName());
- }
- @Override
- public int getSizeInventory() {
- return this.inventory.size();
- }
- @Override
- public boolean isEmpty() {
- for(ItemStack stack: this.inventory)
- if(!stack.isEmpty())
- return false;
- return true;
- }
- @Override
- public ItemStack getStackInSlot(int index) {
- return (ItemStack)this.inventory.get(index);
- }
- @Override
- public ItemStack decrStackSize(int index, int count) {
- return ItemStackHelper.getAndSplit(this.inventory, index, count);
- }
- @Override
- public ItemStack removeStackFromSlot(int index) {
- return ItemStackHelper.getAndRemove(this.inventory, index);
- }
- @Override
- public void setInventorySlotContents(int index, ItemStack stack) {
- ItemStack itemstack = (ItemStack)this.inventory.get(index);
- boolean flag = !stack.isEmpty() && stack.isItemEqual(itemstack) && ItemStack.areItemsEqual(stack, itemstack);
- this.inventory.set(index, stack);
- if(stack.getCount() > this.getInventoryStackLimit())
- stack.setCount(this.getInventoryStackLimit());
- if(index == 0 && index + 1 == 1 && !flag) {
- ItemStack stack1 = (ItemStack)this.inventory.get(index + 1);
- this.markDirty();
- }
- }
- @Override
- public void readFromNBT(NBTTagCompound compound) {
- super.readFromNBT(compound);
- this.inventory = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
- ItemStackHelper.loadAllItems(compound, this.inventory);
- if(compound.hasKey("CustomName", 8))
- this.setCustomName(compound.getString("CustomName"));
- }
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound compound) {
- super.writeToNBT(compound);
- ItemStackHelper.saveAllItems(compound, this.inventory);
- if(this.hasCustomName())
- compound.setString("CustomName", this.customName);
- return compound;
- }
- private boolean canCraft() {
- if(((ItemStack)this.inventory.get(0)).isEmpty() || ((ItemStack)this.inventory.get(1)).isEmpty())
- return false;
- else {
- ItemStack result = PestleRecipe.instance().getPestleResult((ItemStack)this.inventory.get(0), (ItemStack)this.inventory.get(1));
- if(result.isEmpty())
- return false;
- else {
- ItemStack output = (ItemStack)this.inventory.get(3);
- if(output.isEmpty()) return true;
- if(!output.isItemEqual(result)) return false;
- int res = output.getCount() + result.getCount();
- return res <= getInventoryStackLimit() && res <= output.getMaxStackSize();
- }
- }
- }
- public void craftItem() {
- if(this.canCraft()) {
- ItemStack input1 = (ItemStack)this.inventory.get(0);
- ItemStack input2 = (ItemStack)this.inventory.get(1);
- ItemStack result = PestleRecipe.instance().getPestleResult(input1, input2);
- ItemStack output = (ItemStack)this.inventory.get(3);
- if(output.isEmpty())
- this.inventory.set(3, result.copy());
- else if(output.getItem() == result.getItem())
- output.grow(result.getCount());
- input1.shrink(1);
- input2.shrink(1);
- }
- }
- @Override
- public int getInventoryStackLimit() {
- return 64;
- }
- @Override
- public boolean isUsableByPlayer(EntityPlayer player) {
- return this.world.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) < 64.0D;
- }
- @Override
- public void openInventory(EntityPlayer player) {
- // TODO Auto-generated method stub
- }
- @Override
- public void closeInventory(EntityPlayer player) {
- // TODO Auto-generated method stub
- }
- @Override
- public boolean isItemValidForSlot(int index, ItemStack stack) {
- if(index == 3)
- return false;
- else {
- return true;
- }
- }
- public String getGuiID() {
- return "modmt:pestle";
- }
- @Override
- public int getField(int id) {
- return 0;
- }
- @Override
- public void setField(int id, int value) {
- }
- @Override
- public int getFieldCount() {
- return 3;
- }
- @Override
- public void clear() {
- this.inventory.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment