Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.earthcomputer.farmersheaven;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.entity.item.EntityItem;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.ISidedInventory;
- import net.minecraft.inventory.InventoryCrafting;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.crafting.CraftingManager;
- import net.minecraft.item.crafting.IRecipe;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.server.gui.IUpdatePlayerListBox;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.tileentity.TileEntityFurnace;
- import net.minecraft.util.BlockPos;
- import net.minecraft.util.ChatComponentText;
- import net.minecraft.util.ChatComponentTranslation;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.IChatComponent;
- import net.minecraft.world.World;
- import net.minecraftforge.common.util.Constants;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- public class TileEntityAutoCraftingTable extends TileEntity implements IUpdatePlayerListBox, ISidedInventory
- {
- private Container container = new Container() {
- @Override
- public boolean canInteractWith(EntityPlayer player)
- {
- return true;
- }
- @Override
- public void onCraftMatrixChanged(IInventory inventory)
- {
- changeOutput();
- }
- };
- private InventoryCrafting invCrafting = new InventoryCrafting(container, 3, 3);
- private ItemStack result;
- private ItemStack fuelStack;
- private IRecipe currentRecipe;
- private int craftTime = 0;
- public static final int MAX_CRAFT_TIME = 200;
- private int clientSyncTime = 0;
- private static final int MAX_CLIENT_SYNC_TIME = 10;
- private int burnTime = 0;
- private int maxBurnTime = 200;
- private int powered = -1;
- private String customName;
- /** Whether to use packets to update the output slot of the tile entity */
- private static final boolean USE_PACKETS = false;
- @Override
- public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState)
- {
- return oldState.getBlock() != newState.getBlock();
- }
- @Override
- public String getName()
- {
- return hasCustomName() ? customName : "container.autocrafting_table";
- }
- @Override
- public boolean hasCustomName()
- {
- return customName != null && customName.length() > 0;
- }
- @Override
- public IChatComponent getDisplayName()
- {
- return hasCustomName() ? new ChatComponentText(getName()) : new ChatComponentTranslation(getName());
- }
- @Override
- public int getSizeInventory()
- {
- return 11;
- }
- public boolean getPowered()
- {
- if(!hasWorldObj()) return true;
- if(powered == -1)
- {
- IBlockState state = worldObj.getBlockState(pos);
- powered = (Boolean) state.getValue(BlockAutoCraftingTable.POWERED) ? 1 : 0;
- }
- return powered > 0;
- }
- void setPowered(boolean powered)
- {
- this.powered = powered ? 1 : 0;
- }
- public InventoryCrafting getInventoryCrafting()
- {
- return invCrafting;
- }
- public int getCraftTime()
- {
- return craftTime;
- }
- public int getBurnTime()
- {
- return burnTime;
- }
- public int getMaxBurnTime()
- {
- return maxBurnTime;
- }
- public void setCraftTime(int craftTime)
- {
- this.craftTime = craftTime;
- }
- public void setBurnTime(int burnTime)
- {
- this.burnTime = burnTime;
- }
- public void setMaxBurnTime(int maxBurnTime)
- {
- this.maxBurnTime = maxBurnTime;
- }
- @Override
- public ItemStack getStackInSlot(int index)
- {
- return index == 9 ? result : (index == 10 ? fuelStack : invCrafting.getStackInSlot(index));
- }
- private void setStackInSlot(int index, ItemStack stack)
- {
- if(index == 9)
- {
- result = stack;
- }
- else if(index == 10)
- {
- fuelStack = stack;
- }
- else
- {
- invCrafting.setInventorySlotContents(index, stack);
- }
- }
- @Override
- public ItemStack decrStackSize(int index, int count)
- {
- if(getStackInSlot(index) != null)
- {
- ItemStack stack;
- if(getStackInSlot(index).stackSize <= count)
- {
- stack = getStackInSlot(index);
- setStackInSlot(index, null);
- return stack;
- }
- else
- {
- stack = getStackInSlot(index).splitStack(count);
- if(getStackInSlot(index).stackSize == 0)
- {
- setStackInSlot(index, null);
- }
- return stack;
- }
- }
- else
- {
- return null;
- }
- }
- @Override
- @SideOnly(Side.CLIENT)
- public ItemStack getStackInSlotOnClosing(int index)
- {
- if(getStackInSlot(index) != null)
- {
- ItemStack itemstack = getStackInSlot(index);
- setStackInSlot(index, null);
- return itemstack;
- }
- else
- {
- return null;
- }
- }
- @Override
- public void setInventorySlotContents(int index, ItemStack stack)
- {
- boolean sameStackInSlot = stack != null && stack.isItemEqual(getStackInSlot(index))
- && ItemStack.areItemStackTagsEqual(stack, getStackInSlot(index));
- setStackInSlot(index, stack);
- if(stack != null && stack.stackSize > getInventoryStackLimit())
- {
- stack.stackSize = getInventoryStackLimit();
- }
- if(index < 9 && !sameStackInSlot)
- {
- changeOutput();
- markDirty();
- }
- }
- @Override
- public int getInventoryStackLimit()
- {
- return 64;
- }
- @Override
- public boolean isUseableByPlayer(EntityPlayer player)
- {
- return worldObj.getTileEntity(pos) != this ? false : player.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 boolean isItemValidForSlot(int index, ItemStack stack)
- {
- // true && (false || false)
- return index != 9 && (index != 10 || TileEntityFurnace.isItemFuel(stack));
- }
- @Override
- public int getField(int id)
- {
- switch(id)
- {
- case 0:
- return craftTime;
- case 1:
- return burnTime;
- case 2:
- return maxBurnTime;
- default:
- return 0;
- }
- }
- @Override
- public void setField(int id, int value)
- {
- switch(id)
- {
- case 0:
- craftTime = value;
- break;
- case 1:
- burnTime = value;
- break;
- case 2:
- maxBurnTime = value;
- break;
- }
- }
- @Override
- public int getFieldCount()
- {
- return 3;
- }
- @Override
- public void clear()
- {
- for(int i = 0; i < getSizeInventory(); i++)
- {
- setStackInSlot(i, null);
- }
- }
- @Override
- public void readFromNBT(NBTTagCompound compound)
- {
- super.readFromNBT(compound);
- NBTTagList craftMatrix = compound.getTagList("CraftMatrix", Constants.NBT.TAG_COMPOUND);
- for(int i = 0; i < craftMatrix.tagCount(); i++)
- {
- NBTTagCompound stack = craftMatrix.getCompoundTagAt(i);
- int slot = stack.getByte("Slot") & 255;
- if(slot >= 0 && slot < 9) setStackInSlot(slot, ItemStack.loadItemStackFromNBT(stack));
- }
- NBTTagCompound result = compound.getCompoundTag("Result");
- if(result.hasNoTags())
- this.result = null;
- else this.result = ItemStack.loadItemStackFromNBT(result);
- NBTTagCompound fuel = compound.getCompoundTag("Fuel");
- if(fuel.hasNoTags())
- this.fuelStack = null;
- else this.fuelStack = ItemStack.loadItemStackFromNBT(fuel);
- changeOutput();
- craftTime = compound.getInteger("CraftTime");
- burnTime = compound.getInteger("BurnTime");
- maxBurnTime = compound.getInteger("MaxBurnTime");
- if(maxBurnTime <= 0) maxBurnTime = 200;
- if(compound.hasKey("CustomName")) customName = compound.getString("CustomName");
- }
- @Override
- public void writeToNBT(NBTTagCompound compound)
- {
- super.writeToNBT(compound);
- NBTTagList craftMatrix = new NBTTagList();
- for(int i = 0; i < 9; i++)
- {
- if(getStackInSlot(i) != null)
- {
- NBTTagCompound stack = new NBTTagCompound();
- getStackInSlot(i).writeToNBT(stack);
- stack.setByte("Slot", (byte) i);
- craftMatrix.appendTag(stack);
- }
- }
- compound.setTag("CraftMatrix", craftMatrix);
- if(result != null)
- {
- NBTTagCompound result = new NBTTagCompound();
- this.result.writeToNBT(result);
- compound.setTag("Result", result);
- }
- if(fuelStack != null)
- {
- NBTTagCompound fuel = new NBTTagCompound();
- fuelStack.writeToNBT(fuel);
- compound.setTag("Fuel", fuel);
- }
- compound.setInteger("CraftTime", craftTime);
- compound.setInteger("BurnTime", burnTime);
- compound.setInteger("MaxBurnTime", maxBurnTime);
- if(hasCustomName()) compound.setString("CustomName", customName);
- }
- @Override
- public void update()
- {
- if(!worldObj.isRemote) updateOutput();
- updateComparators();
- }
- private void changeOutput()
- {
- if(currentRecipe == null || !currentRecipe.matches(invCrafting, worldObj)) craftTime = 0;
- currentRecipe = null;
- for(Object object : CraftingManager.getInstance().getRecipeList())
- {
- IRecipe recipe = (IRecipe) object;
- if(recipe.matches(invCrafting, worldObj))
- {
- currentRecipe = recipe;
- }
- }
- }
- private void updateOutput()
- {
- burnTime = Math.max(burnTime - 1, 0);
- boolean flag = currentRecipe == null;
- flag = flag || currentRecipe.getRecipeOutput() == null;
- if(result != null)
- {
- flag = flag || !ItemStack.areItemsEqual(currentRecipe.getRecipeOutput(), result);
- flag = flag || !ItemStack.areItemStackTagsEqual(currentRecipe.getRecipeOutput(), result);
- flag = flag || result.stackSize > result.getMaxStackSize() - currentRecipe.getRecipeOutput().stackSize;
- }
- if(flag)
- {
- craftTime = 0;
- }
- else
- {
- if(!isBurning())
- craftTime = Math.max(craftTime - 2, 0);
- else
- {
- if(burnTime <= 0 && !getPowered())
- {
- burnTime = TileEntityFurnace.getItemBurnTime(fuelStack);
- maxBurnTime = burnTime;
- ItemStack stack = fuelStack.copy();
- decrStackSize(10, 1);
- if(stack.getItem().hasContainerItem(fuelStack))
- {
- ItemStack containerItem = stack.getItem().getContainerItem(fuelStack).copy();
- if(fuelStack == null)
- setInventorySlotContents(10, containerItem);
- else
- {
- EntityItem entity = new EntityItem(worldObj);
- entity.setLocationAndAngles(pos.getX() + 0.5, pos.getY() + 0.2, pos.getZ() + 0.5, 0, 0);
- entity.setEntityItemStack(containerItem);
- worldObj.spawnEntityInWorld(entity);
- }
- }
- }
- if(!getPowered()) craftTime++;
- if(craftTime >= MAX_CRAFT_TIME)
- {
- craftTime = 0;
- ItemStack recipeOutput = currentRecipe.getRecipeOutput();
- if(result == null)
- result = recipeOutput.copy();
- else result.stackSize += recipeOutput.stackSize;
- ItemStack[] additionalOutputs = CraftingManager.getInstance().func_180303_b(invCrafting, worldObj);
- for(int i = 0; i < 9; i++)
- {
- decrStackSize(i, 1);
- if(additionalOutputs[i] != null)
- {
- if(getStackInSlot(i) == null)
- {
- setInventorySlotContents(i, additionalOutputs[i]);
- }
- else
- {
- EntityItem entity = new EntityItem(worldObj);
- entity.setLocationAndAngles(pos.getX() + 0.5, pos.getY() + 0.2, pos.getZ() + 0.5, 0, 0);
- entity.setEntityItemStack(additionalOutputs[i]);
- worldObj.spawnEntityInWorld(entity);
- }
- }
- }
- }
- }
- }
- if(USE_PACKETS)
- {
- clientSyncTime++;
- if(clientSyncTime >= MAX_CLIENT_SYNC_TIME)
- {
- FarmersHeaven.NETWORK.sendToAll(new AutoCraftingTableUpdateMessage(this));
- clientSyncTime = 0;
- }
- }
- }
- void updateComparators()
- {
- worldObj.updateComparatorOutputLevel(pos, worldObj.getBlockState(pos).getBlock());
- for(int x = -1; x <= 1; x++)
- for(int z = -1; z <= 1; z++)
- {
- BlockPos pos = new BlockPos(x, 1, z).add(this.pos);
- worldObj.updateComparatorOutputLevel(pos, worldObj.getBlockState(pos).getBlock());
- }
- }
- @Override
- public int[] getSlotsForFace(EnumFacing side)
- {
- return side == EnumFacing.DOWN ? new int[] { 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10 }
- : (side == EnumFacing.UP ? new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 } : new int[] { 10 });
- }
- @Override
- public boolean canInsertItem(int index, ItemStack itemStack, EnumFacing direction)
- {
- return isItemValidForSlot(index, itemStack);
- }
- @Override
- public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction)
- {
- if(index == 9)
- return true;
- else if(index == 10)
- return !TileEntityFurnace.isItemFuel(getStackInSlot(10));
- else return currentRecipe == null;
- }
- public void setCustomName(String customName)
- {
- this.customName = customName;
- }
- public boolean isCrafting()
- {
- return craftTime > 0;
- }
- public int getScaleCraftingWidth(int maxWidth)
- {
- return craftTime * maxWidth / MAX_CRAFT_TIME;
- }
- public boolean isBurning()
- {
- return fuelStack != null || burnTime > 0;
- }
- public int getBurnTimeHeight(int maxHeight)
- {
- if(maxBurnTime == 0) maxBurnTime = 200;
- return burnTime * maxHeight / maxBurnTime;
- }
- public boolean shouldRenderFlame()
- {
- return isBurning() && burnTime > 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment