Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.chef.mod.tileentity;
- 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.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.EnumFacing;
- import com.chef.mod.Debugger;
- import com.chef.mod.blocks.IceCreamMaker;
- import com.chef.mod.container.ContainerIceCreamMaker;
- import com.chef.mod.crafting.IceCreamMakerRecipes;
- import com.chef.mod.init.MyItems;
- import cpw.mods.fml.relauncher.Side;
- import cpw.mods.fml.relauncher.SideOnly;
- public class TileEntityIceCreamMaker extends TileEntity implements 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 = 125;
- private String customName;
- public int scaledIceLevel;
- 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 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.customName = 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.hasCustomInventoryName()) {
- compound.setString("CustomName", this.customName);
- }
- }
- public int getInventoryStackLimit() {
- return 64;
- }
- public boolean hasIce() {
- return ice > 0;
- }
- public boolean isIcing() {
- return this.cookTime > 0;
- }
- public void updateEntity() {
- boolean flag = this.hasIce();
- boolean flag1 = false;
- if (hasIce() && this.isIcing()) {
- this.ice--;
- }
- if (!worldObj.isRemote) {
- if (this.hasItemIceTime(this.slots[2]) && this.ice <= (this.maxIce - this.getItemIceTime(this.slots[2]))) {
- this.ice += getItemIceTime(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]);
- }
- }
- }
- flag1 = true;
- scaledIceLevel = Math.round(this.ice / 1000);
- IceCreamMaker.updateBlockState(this.isIcing(), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
- if (hasIce() && canIce()) {
- cookTime++;
- if (this.cookTime == this.iceCreamMakerSpeed) {
- this.cookTime = 1;
- this.iceItem();
- flag1 = true;
- }
- } else {
- cookTime = 0;
- }
- }
- if (flag1) {
- this.markDirty();
- }
- }
- 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 getItemIceTime(ItemStack itemstack) {
- if (itemstack == null) {
- return 0;
- } else {
- Item item = itemstack.getItem();
- if (item == MyItems.ice_shard)
- return 50;
- return 0;
- }
- }
- public static boolean hasItemIceTime(ItemStack itemstack) {
- return getItemIceTime(itemstack) > 0;
- }
- public boolean isUseableByPlayer(EntityPlayer player) {
- return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double) this.xCoord + 0.5D,
- (double) this.yCoord + 0.5D, (double) this.zCoord + 0.5D) <= 64.0D;
- }
- @Override
- public boolean isItemValidForSlot(int i, ItemStack itemstack) {
- return i == 2 ? false : (i == 1 ? hasItemIceTime(itemstack) : true);
- }
- public int[] getSlotsForFace(EnumFacing side) {
- return side == EnumFacing.DOWN ? slotsBottom : (side == EnumFacing.UP ? slotsTop : slotsSides);
- }
- 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;
- }
- }
- @Override
- public String getInventoryName() {
- return this.hasCustomInventoryName() ? this.customName : "container.iceCreamMaker";
- }
- @Override
- public boolean hasCustomInventoryName() {
- return this.customName != null && this.customName.length() > 0;
- }
- public void openInventory() {}
- public void closeInventory() {}
- @Override
- public int[] getAccessibleSlotsFromSide(int i) {
- if (i == 0) {
- return slotsBottom;
- } else if (i == 1) {
- return slotsTop;
- } else {
- return slotsSides;
- }
- }
- @Override
- public boolean canInsertItem(int i, ItemStack itemstack, int j) {
- return this.isItemValidForSlot(i, itemstack);
- }
- @Override
- public boolean canExtractItem(int i, ItemStack itemstack, int j) {
- return j != 0 || i != 1 || itemstack.getItem() == Items.bucket;
- }
- }
Add Comment
Please, Sign In to add comment