Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package qwertyasdef.alchemtrans.Tile;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.inventory.ISidedInventory;
- import net.minecraft.inventory.ItemStackHelper;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.ITickable;
- 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 TileNetherrackFurnace extends TileEntity implements ITickable, ISidedInventory {
- private String customName;
- private int cookTime;
- private int totalCookTime;
- private NonNullList<ItemStack> inventory = NonNullList.<ItemStack>withSize(2, ItemStack.EMPTY);
- private int[] SLOTS_UP = {0};
- private int[] SLOTS_DOWN = {1};
- private int[] SLOTS_SIDE = {0, 1};
- public TileNetherrackFurnace() {
- }
- public String getCustomName() {
- return this.customName;
- }
- public void setCustomName(String customName) {
- this.customName = customName;
- }
- @Override
- public String getName() {
- return this.hasCustomName() ? this.customName : "container.netherrack_furnace";
- }
- @Override
- public boolean hasCustomName() {
- return this.customName != null && !this.customName.equals("");
- }
- @Override
- public ITextComponent getDisplayName() {
- return this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName());
- }
- // IInventory
- @Override
- public int getSizeInventory() {
- return this.inventory.size();
- }
- @Override
- public boolean isEmpty() {
- for (int i = 0; i < getSizeInventory(); i++) {
- if (this.getStackInSlot(i) != ItemStack.EMPTY) {
- return false;
- }
- }
- return true;
- }
- @Override
- public ItemStack getStackInSlot(int index) {
- if (index < 0 || index >= this.getSizeInventory())
- return null;
- return this.inventory.get(index);
- }
- @Override
- public ItemStack decrStackSize(int index, int count) {
- if (this.getStackInSlot(index) != ItemStack.EMPTY) {
- ItemStack itemstack;
- if (this.getStackInSlot(index).getCount() <= count) {
- itemstack = this.getStackInSlot(index);
- this.setInventorySlotContents(index, ItemStack.EMPTY);
- this.markDirty();
- return itemstack;
- } else {
- itemstack = this.getStackInSlot(index).splitStack(count);
- if (this.getStackInSlot(index).getCount() <= 0) {
- this.setInventorySlotContents(index, ItemStack.EMPTY);
- } else {
- //Just to show that changes happened
- this.setInventorySlotContents(index, this.getStackInSlot(index));
- }
- this.markDirty();
- return itemstack;
- }
- } else {
- return ItemStack.EMPTY;
- }
- }
- @Override
- public ItemStack removeStackFromSlot(int index) {
- ItemStack stack = this.getStackInSlot(index);
- this.setInventorySlotContents(index, ItemStack.EMPTY);
- return stack;
- }
- @Override
- public void setInventorySlotContents(int index, ItemStack stack) {
- if (index < 0 || index >= this.getSizeInventory()) {
- return;
- }
- if (stack != ItemStack.EMPTY && stack.getCount() > this.getInventoryStackLimit()) {
- stack.setCount(this.getInventoryStackLimit());
- }
- if (stack != ItemStack.EMPTY && stack.getCount() == 0) {
- stack = ItemStack.EMPTY;
- }
- this.inventory.set(index, stack);
- this.markDirty();
- }
- @Override
- public int getInventoryStackLimit() {
- return 64;
- }
- @Override
- public boolean isUsableByPlayer(EntityPlayer player) {
- return this.getWorld().getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
- }
- @Override
- public void openInventory(EntityPlayer player) {
- }
- @Override
- public void closeInventory(EntityPlayer player) {
- }
- @Override
- public boolean isItemValidForSlot(int index, ItemStack stack) {
- return index == 0;
- }
- @Override
- public int getField(int id) {
- switch (id) {
- case 0:
- return this.cookTime;
- case 1:
- return this.totalCookTime;
- default:
- return 0;
- }
- }
- @Override
- public void setField(int id, int value) {
- switch (id) {
- case 0:
- this.cookTime = value;
- break;
- case 1:
- this.totalCookTime = value;
- break;
- }
- }
- @Override
- public int getFieldCount() {
- return 2;
- }
- @Override
- public void clear() {
- for (int i = 0; i < this.getSizeInventory(); i++) {
- this.setInventorySlotContents(i, ItemStack.EMPTY);
- }
- }
- // ISidedInventory
- @Override
- public int[] getSlotsForFace(EnumFacing side) {
- switch (side) {
- case UP:
- return SLOTS_UP;
- case DOWN:
- return SLOTS_DOWN;
- default:
- return SLOTS_SIDE;
- }
- }
- @Override
- public boolean canInsertItem(int index, ItemStack itemIn, EnumFacing side) {
- return this.isItemValidForSlot(index, itemIn);
- }
- @Override
- public boolean canExtractItem(int index, ItemStack itemIn, EnumFacing side) {
- return true;
- }
- // ITickable
- @Override
- public void update() {
- ItemStack input = this.getStackInSlot(0);
- if (canSmelt()) {
- if (this.totalCookTime == 0) {
- this.totalCookTime = getCookTime(input);
- }
- ++this.cookTime;
- System.out.println("Cooking: cookTime = " + this.cookTime);
- if (this.cookTime >= this.totalCookTime) {
- smeltItem();
- input = this.getStackInSlot(0);
- System.out.println("Smelted item!");
- if (input != ItemStack.EMPTY) {
- this.totalCookTime = getCookTime(input);
- } else {
- this.totalCookTime = 0;
- }
- this.cookTime = 0;
- }
- } else {
- this.cookTime = 0;
- }
- }
- public int getCookTime(ItemStack stack) {
- return 160;
- }
- private boolean canSmelt() {
- ItemStack input = this.getStackInSlot(0);
- ItemStack result = input.copy();
- ItemStack output = this.getStackInSlot(1);
- if (input.isEmpty()) {
- return false;
- }
- if (result.isEmpty()) {
- return false;
- }
- if (result.getItem() == output.getItem() && result.getCount() + output.getCount() <= this.getInventoryStackLimit()) {
- return true;
- }
- return false;
- }
- public void smeltItem() {
- if (this.canSmelt()) {
- ItemStack input = this.getStackInSlot(0);
- ItemStack result = input.copy();
- ItemStack output = this.getStackInSlot(1);
- input.shrink(1);
- if (input.getCount() <= 0) {
- this.setInventorySlotContents(0, ItemStack.EMPTY);
- }
- if (output.isEmpty()) {
- this.setInventorySlotContents(1, result.copy());
- } else {
- output.grow(result.getCount());
- }
- }
- }
- // TileEntity
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound compound) {
- super.writeToNBT(compound);
- if (this.hasCustomName()) {
- compound.setString("CustomName", this.getCustomName());
- }
- compound.setInteger("CookTime", this.cookTime);
- compound.setInteger("TotalCookTime", this.totalCookTime);
- ItemStackHelper.saveAllItems(compound, this.inventory);
- return compound;
- }
- @Override
- public void readFromNBT(NBTTagCompound compound) {
- super.readFromNBT(compound);
- if (compound.hasKey("CustomName", 8)) {
- this.setCustomName(compound.getString("CustomName"));
- }
- this.cookTime = compound.getInteger("CookTime");
- this.totalCookTime = compound.getInteger("TotalCookTime");
- ItemStackHelper.loadAllItems(compound, this.inventory);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement