/** * MrCrayfish's Furniture Mod * Copyright (C) 2016 MrCrayfish (http://www.mrcrayfish.com/) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.mrcrayfish.furniture.tileentity; import java.util.Random; import com.mrcrayfish.furniture.api.RecipeAPI; import com.mrcrayfish.furniture.api.RecipeData; import com.mrcrayfish.furniture.gui.containers.ContainerMicrowave; import com.mrcrayfish.furniture.util.ParticleSpawner; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.NetworkManager; import net.minecraft.network.Packet; import net.minecraft.network.play.server.S35PacketUpdateTileEntity; import net.minecraft.tileentity.TileEntityLockable; import net.minecraft.util.ChatComponentText; import net.minecraft.util.EnumFacing; import net.minecraft.util.IChatComponent; import net.minecraft.util.ITickable; public class TileEntityMicrowave extends TileEntityLockable implements ISidedInventory, ITickable { private static final int[] slot = new int[] { 0 }; private ItemStack item = null; private String customName; private boolean cooking = false; public int progress = 0; public ItemStack getItem() { return item; } public void startCooking() { if (item != null) { RecipeData data = RecipeAPI.getMicrowaveRecipeFromIngredients(item); if (data != null) { cooking = true; worldObj.updateComparatorOutputLevel(pos, blockType); } } } public void stopCooking() { this.cooking = false; this.progress = 0; worldObj.updateComparatorOutputLevel(pos, blockType); } public boolean isCooking() { return cooking; } private Random rand = new Random(); private int timer = 0; @Override public void update() { if (cooking) { if (this.worldObj.isRemote) { double posX = pos.getX() + 0.35D + (rand.nextDouble() / 3); double posZ = pos.getZ() + 0.35D + (rand.nextDouble() / 3); ParticleSpawner.spawnParticle("smoke", posX, pos.getY() + 0.065D, posZ); } progress++; if (progress >= 40) { if (item != null) { RecipeData data = RecipeAPI.getMicrowaveRecipeFromIngredients(item); if (data != null) { this.item = data.getOutput().copy(); } } if (!worldObj.isRemote) { worldObj.playSoundEffect(pos.getX(), pos.getY(), pos.getZ(), "cfm:microwave_finish", 0.75F, 1.0F); } timer = 0; progress = 0; cooking = false; worldObj.updateComparatorOutputLevel(pos, blockType); } else { if (timer == 20) { timer = 0; } if (timer == 0) { worldObj.playSoundEffect(pos.getX(), pos.getY(), pos.getZ(), "cfm:microwave_running", 0.75F, 1.0F); } timer++; } } } @Override public void readFromNBT(NBTTagCompound par1NBTTagCompound) { super.readFromNBT(par1NBTTagCompound); if (par1NBTTagCompound.hasKey("Item")) { NBTTagCompound nbt = par1NBTTagCompound.getCompoundTag("Item"); this.item = ItemStack.loadItemStackFromNBT(nbt); } this.cooking = par1NBTTagCompound.getBoolean("Coooking"); this.progress = par1NBTTagCompound.getInteger("Progress"); } @Override public void writeToNBT(NBTTagCompound par1NBTTagCompound) { super.writeToNBT(par1NBTTagCompound); NBTTagCompound nbt = new NBTTagCompound(); if (item != null) { item.writeToNBT(nbt); } par1NBTTagCompound.setTag("Item", nbt); par1NBTTagCompound.setBoolean("Coooking", cooking); par1NBTTagCompound.setInteger("Progress", progress); } @Override public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) { NBTTagCompound tagCom = pkt.getNbtCompound(); this.readFromNBT(tagCom); } @Override public Packet getDescriptionPacket() { NBTTagCompound tagCom = new NBTTagCompound(); this.writeToNBT(tagCom); return new S35PacketUpdateTileEntity(pos, getBlockMetadata(), tagCom); } @Override public int getSizeInventory() { return 1; } @Override public ItemStack getStackInSlot(int slot) { return this.item; } @Override public ItemStack decrStackSize(int slot, int number) { if (this.item != null) { ItemStack itemstack; if (this.item.stackSize <= number) { itemstack = this.item; this.item = null; return itemstack; } itemstack = this.item.splitStack(number); if (this.item.stackSize == 0) { this.item = null; } return itemstack; } return null; } @Override public ItemStack removeStackFromSlot(int slot) { return item; } @Override public void setInventorySlotContents(int par1, ItemStack par2ItemStack) { this.item = par2ItemStack; if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit()) { par2ItemStack.stackSize = this.getInventoryStackLimit(); } } @Override public int getInventoryStackLimit() { return 1; } @Override public boolean isUseableByPlayer(EntityPlayer entityplayer) { return this.worldObj.getTileEntity(pos) != this ? false : entityplayer.getDistanceSq(pos.getX() + 0.5D, pos.getY() + 0.5D, pos.getZ() + 0.5D) <= 64.0D; } @Override public void openInventory(EntityPlayer player) { } @Override public void closeInventory(EntityPlayer player) { } @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() { item = null; } @Override public String getName() { return hasCustomName() ? customName : "Microwave"; } @Override public boolean hasCustomName() { return customName != null; } @Override public IChatComponent getDisplayName() { return new ChatComponentText(getName()); } @Override public boolean isItemValidForSlot(int index, ItemStack stack) { return RecipeAPI.getMicrowaveRecipeFromIngredients(stack) != null; } @Override public int[] getSlotsForFace(EnumFacing side) { return slot; } @Override public boolean canInsertItem(int index, ItemStack stack, EnumFacing direction) { return RecipeAPI.getMicrowaveRecipeFromIngredients(stack) != null; } @Override public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction) { return RecipeAPI.getMicrowaveRecipeFromIngredients(stack) == null; } @Override public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) { return new ContainerMicrowave(playerInventory, this); } @Override public String getGuiID() { return "0"; } }