Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.chef.mod.tileentity;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.InventoryPlayer;
- import net.minecraft.init.Items;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.ISidedInventory;
- import net.minecraft.inventory.SlotFurnaceFuel;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.server.gui.IUpdatePlayerListBox;
- import net.minecraft.tileentity.TileEntityLockable;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.ResourceLocation;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- import com.chef.mod.Debugger;
- import com.chef.mod.Reference;
- import com.chef.mod.blocks.IceCreamMaker;
- import com.chef.mod.blocks.IceCreamMaker.EnumIce;
- import com.chef.mod.container.ContainerIceCreamMaker;
- import com.chef.mod.crafting.IceCreamMakerRecipes;
- import com.chef.mod.init.MyItems;
- public class TileEntityIceCreamMaker extends TileEntityLockable implements IUpdatePlayerListBox, ISidedInventory {
- private static final int[] slotsTop = new int[] { 0, 1 };
- private static final int[] slotsBottom = new int[] { 3 };
- private static final int[] slotsSides = new int[] { 2 };
- private ItemStack slots[];
- public TileEntityIceCreamMaker() {
- slots = new ItemStack[4];
- }
- public int ice;
- public int cookTime;
- public static final int maxIce = 10000;
- public static final int iceCreamMakerSpeed = 100;
- private String iceMakerCustomName;
- public int getSizeInventory() {
- return this.slots.length;
- }
- public ItemStack getStackInSlot(int index) {
- return this.slots[index];
- }
- public ItemStack decrStackSize(int index, int count) {
- if (this.slots[index] != null) {
- ItemStack itemstack;
- if (this.slots[index].stackSize <= count) {
- itemstack = this.slots[index];
- this.slots[index] = null;
- return itemstack;
- } else {
- itemstack = this.slots[index].splitStack(count);
- if (this.slots[index].stackSize == 0) {
- this.slots[index] = null;
- }
- return itemstack;
- }
- } else {
- return null;
- }
- }
- public ItemStack getStackInSlotOnClosing(int index) {
- if (this.slots[index] != null) {
- ItemStack itemstack = this.slots[index];
- this.slots[index] = null;
- return itemstack;
- } else {
- return null;
- }
- }
- @Override
- public void setInventorySlotContents(int i, ItemStack itemstack) {
- slots[i] = itemstack;
- if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
- itemstack.stackSize = getInventoryStackLimit();
- }
- }
- public String getName() {
- return this.hasCustomName() ? this.iceMakerCustomName : "container.iceCreamMaker";
- }
- public boolean hasCustomName() {
- return this.iceMakerCustomName != null && this.iceMakerCustomName.length() > 0;
- }
- public void setCustomInventoryName(String string) {
- this.iceMakerCustomName = string;
- }
- public void readFromNBT(NBTTagCompound compound) {
- super.readFromNBT(compound);
- NBTTagList nbttaglist = compound.getTagList("Items", 10);
- this.slots = new ItemStack[this.getSizeInventory()];
- for (int i = 0; i < nbttaglist.tagCount(); ++i) {
- NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
- byte b0 = nbttagcompound1.getByte("Slot");
- if (b0 >= 0 && b0 < this.slots.length) {
- this.slots[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
- }
- }
- ice = compound.getShort("PowerTime");
- cookTime = compound.getShort("CookTime");
- if (compound.hasKey("CustomName", 8)) {
- this.iceMakerCustomName = compound.getString("CustomName");
- }
- }
- public void writeToNBT(NBTTagCompound compound) {
- super.writeToNBT(compound);
- compound.setShort("PowerTime", (short) ice);
- compound.setShort("CookTime", (short) cookTime);
- NBTTagList nbttaglist = new NBTTagList();
- for (int i = 0; i < this.slots.length; ++i) {
- if (this.slots[i] != null) {
- NBTTagCompound nbttagcompound1 = new NBTTagCompound();
- nbttagcompound1.setByte("Slot", (byte) i);
- this.slots[i].writeToNBT(nbttagcompound1);
- nbttaglist.appendTag(nbttagcompound1);
- }
- }
- compound.setTag("Items", nbttaglist);
- if (this.hasCustomName()) {
- compound.setString("CustomName", this.iceMakerCustomName);
- }
- }
- public int getInventoryStackLimit() {
- return 64;
- }
- public boolean hasIce() {
- return ice > 0;
- }
- public boolean isIcing() {
- return this.cookTime > 0;
- }
- @SideOnly(Side.CLIENT)
- public static boolean isActive(IInventory iinventory) {
- return iinventory.getField(0) > 0;
- }
- public void update() {
- boolean flag = this.hasIce();
- boolean flag1 = false;
- if (hasIce() && this.isIcing()) {
- this.ice--;
- }
- if (!worldObj.isRemote) {
- if (this.hasItemPower(this.slots[2]) && this.ice <= (this.maxIce - this.getItemPower(this.slots[2]))) {
- this.ice += getItemPower(this.slots[2]);
- if (this.slots[2] != null) {
- flag1 = true;
- this.slots[2].stackSize--;
- if (this.slots[2].stackSize == 0) {
- this.slots[2] = this.slots[2].getItem().getContainerItem(this.slots[2]);
- }
- }
- }
- if (this.isIcing()) {
- flag1 = true;
- IceCreamMaker.setState(this.isIcing(), this.worldObj, this.pos);
- } else {
- IceCreamMaker.setState(this.isIcing(), this.worldObj, this.pos);
- }
- if (hasIce() && canIce()) {
- cookTime++;
- if (this.cookTime == this.iceCreamMakerSpeed) {
- this.cookTime = 0;
- this.iceItem();
- flag1 = true;
- }
- } else {
- cookTime = 0;
- }
- }
- if (flag1) {
- this.markDirty();
- }
- IceCreamMaker.setIceAmount(this.ice);
- Debugger.log("Ice amount stored in IceCreamMaker: " + this.ice);
- }
- private boolean canIce() {
- if (slots[0] == null || slots[1] == null) {
- return false;
- } else {
- ItemStack itemstack = IceCreamMakerRecipes.getIceCreamResult(slots[0].getItem(), slots[1].getItem());
- if (itemstack == null)
- return false;
- if (this.slots[3] == null)
- return true;
- if (!this.slots[3].isItemEqual(itemstack))
- return false;
- int result = this.slots[3].stackSize + itemstack.stackSize;
- return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());
- }
- }
- public void iceItem() {
- if (this.canIce()) {
- ItemStack itemstack = IceCreamMakerRecipes.getIceCreamResult(this.slots[0].getItem(), slots[1].getItem());
- if (this.slots[3] == null) {
- this.slots[3] = itemstack.copy();
- } else if (this.slots[3].getItem() == itemstack.getItem()) {
- this.slots[3].stackSize += itemstack.stackSize; // Forge BugFix:
- // Results may
- // have multiple
- // items
- }
- for (int i = 0; i < 2; i++) {
- if (slots[i].stackSize <= 0) {
- slots[i] = new ItemStack(slots[i].getItem().setFull3D());
- } else {
- slots[i].stackSize--;
- }
- if (slots[i].stackSize <= 0) {
- slots[i] = null;
- }
- }
- }
- }
- public static int getItemPower(ItemStack itemstack) {
- if (itemstack == null) {
- return 0;
- } else {
- Item item = itemstack.getItem();
- if (item == MyItems.ice_shard)
- return 50;
- return 0;
- }
- }
- public static boolean hasItemPower(ItemStack itemstack) {
- return getItemPower(itemstack) > 0;
- }
- public boolean isUseableByPlayer(EntityPlayer player) {
- return this.worldObj.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;
- }
- public void openInventory(EntityPlayer player) {
- }
- public void closeInventory(EntityPlayer player) {
- }
- public boolean isItemValidForSlot(int index, ItemStack stack) {
- return index == 2 ? false : (index != 1 ? true : hasItemPower(stack) || SlotFurnaceFuel.isBucket(stack));
- }
- public int[] getSlotsForFace(EnumFacing side) {
- return side == EnumFacing.DOWN ? slotsBottom : (side == EnumFacing.UP ? slotsTop : slotsSides);
- }
- public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) {
- return this.isItemValidForSlot(index, itemStackIn);
- }
- public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction) {
- if (direction == EnumFacing.DOWN && index == 1) {
- Item item = stack.getItem();
- if (item != Items.water_bucket && item != Items.bucket) {
- return false;
- }
- }
- return true;
- }
- public int getIceMakerProgressScaled(int i) {
- return (cookTime * i) / iceCreamMakerSpeed;
- }
- public int getIceRemainingScaled(int i) {
- return (ice * i) / maxIce;
- }
- public String getGuiID() {
- return "chef:iceCreamMaker";
- }
- public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
- return new ContainerIceCreamMaker(playerInventory, this);
- }
- public int getField(int id) {
- switch (id) {
- case 0:
- // Burn time
- return this.ice;
- case 1:
- // Cook time
- return this.cookTime;
- default:
- return 0;
- }
- }
- public void setField(int id, int value) {
- switch (id) {
- case 0:
- this.ice = value;
- break;
- case 1:
- this.cookTime = value;
- break;
- }
- }
- public int getFieldCount() {
- return 3;
- }
- public void clear() {
- for (int i = 0; i < this.slots.length; ++i) {
- this.slots[i] = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment