Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.misterfrogger.quintuplinator;
- import ic2.api.energy.event.EnergyTileLoadEvent;
- import ic2.api.energy.event.EnergyTileUnloadEvent;
- import ic2.api.energy.tile.IEnergySink;
- import ic2.api.tile.IWrenchable;
- import java.util.HashMap;
- import net.minecraft.block.Block;
- import net.minecraft.client.Minecraft;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.inventory.ISidedInventory;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.network.INetworkManager;
- import net.minecraft.network.packet.Packet;
- import net.minecraft.network.packet.Packet132TileEntityData;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraftforge.common.ForgeDirection;
- import net.minecraftforge.common.MinecraftForge;
- import net.minecraftforge.fluids.Fluid;
- import net.minecraftforge.fluids.FluidStack;
- import net.minecraftforge.fluids.FluidTankInfo;
- import net.minecraftforge.fluids.IFluidHandler;
- import cpw.mods.fml.common.FMLCommonHandler;
- public class TileEntityPurificationChamber extends TileEntity implements ISidedInventory, IWrenchable, IFluidHandler, IEnergySink {
- public static final int ticksPerOp = 600;
- public short facingDir;
- private ItemStack[] inventory;
- public boolean working;
- public int progress;
- public int oxygenAmount;
- public double energyAmount;
- public boolean initialized;
- public static HashMap<Integer, ItemStack> recipes;
- public TileEntityPurificationChamber(){
- inventory = new ItemStack[2];
- if(recipes == null){
- recipes = new HashMap<Integer, ItemStack>();
- recipes.put(Block.oreIron.blockID, new ItemStack(Quintuplinator.itemClump, 3, 0));
- recipes.put(Block.oreGold.blockID, new ItemStack(Quintuplinator.itemClump, 3, 1));
- }
- progress = 0;
- }
- @Override
- public void onInventoryChanged(){
- if(worldObj != null) worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
- recalculateWorking();
- super.onInventoryChanged();
- }
- public void recalculateWorking(){
- if(worldObj == null || worldObj.isRemote) return;
- ItemStack stack = getStackInSlot(0);
- ItemStack result;
- if(stack != null && (result = recipes.get(stack.itemID)) != null && (getStackInSlot(1) == null || (64 - result.stackSize >= getStackInSlot(1).stackSize && result.itemID == getStackInSlot(1).itemID)) && oxygenAmount >= result.stackSize * 100){
- if(energyAmount >= 64){
- working = true;
- }else{
- working = false;
- }
- }else{
- working = false;
- progress = 0;
- }
- if(worldObj != null) worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
- }
- @Override
- public void updateEntity(){
- if(worldObj == null) return;
- if(!initialized && !worldObj.isRemote){
- initialized = true;
- MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
- }
- if(working){
- if(energyAmount < 64.0D){
- working = false;
- if(worldObj != null) worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
- }
- energyAmount -= 64.0D;
- if(progress < ticksPerOp){
- progress++;
- }else{
- if(FMLCommonHandler.instance().getEffectiveSide().isServer()){
- progress = 0;
- ItemStack result = recipes.get(getStackInSlot(0).itemID);
- if(getStackInSlot(1) == null){
- setInventorySlotContents(1, result.copy());
- }else{
- getStackInSlot(1).stackSize += result.stackSize;
- }
- oxygenAmount -= result.stackSize * 100;
- decrStackSize(0, 1);
- worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
- }
- }
- }
- }
- @Override
- public void invalidate(){
- if(!worldObj.isRemote){
- MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
- }
- }
- @Override
- public int getSizeInventory(){
- return inventory.length;
- }
- @Override
- public ItemStack getStackInSlot(int i){
- return inventory[i];
- }
- @Override
- public ItemStack decrStackSize(int slot, int count){
- ItemStack stack = getStackInSlot(slot);
- if(stack != null){
- if(stack.stackSize <= count){
- setInventorySlotContents(slot, null);
- }else{
- stack = stack.splitStack(count);
- onInventoryChanged();
- }
- }
- return stack;
- }
- @Override
- public ItemStack getStackInSlotOnClosing(int slot){
- ItemStack stack = getStackInSlot(slot);
- setInventorySlotContents(slot, null);
- return stack;
- }
- @Override
- public void setInventorySlotContents(int slot, ItemStack stack){
- inventory[slot] = stack;
- if(stack != null && stack.stackSize > getInventoryStackLimit()){
- stack.stackSize = getInventoryStackLimit();
- }
- onInventoryChanged();
- }
- @Override
- public String getInvName(){
- return "Purification Chamber";
- }
- @Override
- public boolean isInvNameLocalized(){
- return true;
- }
- @Override
- public int getInventoryStackLimit(){
- return 64;
- }
- @Override
- public boolean isUseableByPlayer(EntityPlayer player){
- return player.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D, zCoord + 0.5D) <= 64;
- }
- @Override
- public void openChest(){
- }
- @Override
- public void closeChest(){
- }
- @Override
- public boolean isItemValidForSlot(int slot, ItemStack item){
- switch(slot){
- case 0:
- return true;
- case 1:
- return false;
- default:
- return false;
- }
- }
- @Override
- public void writeToNBT(NBTTagCompound tag){
- tag.setShort("facing", facingDir);
- tag.setBoolean("working", working);
- tag.setInteger("progress", progress);
- tag.setInteger("oxygenAmount", oxygenAmount);
- tag.setDouble("energyAmount", energyAmount);
- NBTTagList list = new NBTTagList();
- for(int i = 0;i < getSizeInventory();i++){
- ItemStack stack = getStackInSlot(i);
- if(stack != null){
- NBTTagCompound item = new NBTTagCompound();
- item.setByte("slotNum", (byte) i);
- stack.writeToNBT(item);
- list.appendTag(item);
- }
- }
- tag.setTag("invContents", list);
- super.writeToNBT(tag);
- }
- @Override
- public void readFromNBT(NBTTagCompound tag){
- facingDir = tag.getShort("facing");
- working = tag.getBoolean("working");
- progress = tag.getInteger("progress");
- oxygenAmount = tag.getInteger("oxygenAmount");
- energyAmount = tag.getDouble("energyAmount");
- NBTTagList list = tag.getTagList("invContents");
- for(int i = 0;i < list.tagCount();i++){
- NBTTagCompound item = (NBTTagCompound) list.tagAt(i);
- int slot = item.getByte("slotNum");
- if(slot >= 0 && slot < getSizeInventory()){
- setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
- }
- }
- super.readFromNBT(tag);
- }
- @Override
- public Packet getDescriptionPacket(){
- NBTTagCompound tag = new NBTTagCompound();
- writeToNBT(tag);
- return new Packet132TileEntityData(xCoord, yCoord, zCoord, 1, tag);
- }
- @Override
- public void onDataPacket(INetworkManager networkManager, Packet132TileEntityData packet){
- readFromNBT(packet.data);
- Minecraft.getMinecraft().renderGlobal.markBlockForRenderUpdate(xCoord, yCoord, zCoord);
- }
- @Override
- public short getFacing(){
- return facingDir;
- }
- @Override
- public void setFacing(short facing){
- if(facing < 2) return;
- facingDir = facing;
- worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
- }
- @Override
- public boolean wrenchCanSetFacing(EntityPlayer player, int side){
- return side > 1 && side != facingDir;
- }
- @Override
- public float getWrenchDropRate(){
- return 0.8F;
- }
- @Override
- public ItemStack getWrenchDrop(EntityPlayer player){
- return new ItemStack(Quintuplinator.blockPurificationChamber);
- }
- @Override
- public boolean wrenchCanRemove(EntityPlayer player){
- return true;
- }
- @Override
- public int[] getAccessibleSlotsFromSide(int side){
- switch(side){
- case 0:
- return new int[0];
- case 1:
- return new int[]{0};
- default:
- return new int[]{1};
- }
- }
- @Override
- public boolean canExtractItem(int slot, ItemStack item, int side){
- return true;
- }
- @Override
- public boolean canInsertItem(int slot, ItemStack item, int side){
- return slot == 0;
- }
- @Override
- public boolean canDrain(ForgeDirection dir, Fluid fluid){
- return false;
- }
- @Override
- public boolean canFill(ForgeDirection dir, Fluid fluid){
- return fluid.getBlockID() == Quintuplinator.fluidOxygen.getBlockID();
- }
- @Override
- public FluidTankInfo[] getTankInfo(ForgeDirection dir){
- return new FluidTankInfo[] { new FluidTankInfo(new FluidStack(Quintuplinator.fluidOxygen, oxygenAmount), 6000) };
- }
- @Override
- public FluidStack drain(ForgeDirection dir, FluidStack max, boolean sim){
- return new FluidStack(Quintuplinator.fluidOxygen, 0);
- }
- @Override
- public FluidStack drain(ForgeDirection dir, int maxAmount, boolean sim){
- return new FluidStack(Quintuplinator.fluidOxygen, 0);
- }
- @Override
- public int fill(ForgeDirection dir, FluidStack max, boolean doFill){
- if(max.getFluid().getBlockID() != Quintuplinator.fluidOxygen.getBlockID()) return 0;
- int amount = Math.min(6000 - oxygenAmount, max.amount);
- if(doFill) oxygenAmount += amount;
- if(worldObj != null) worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
- recalculateWorking();
- return amount;
- }
- @Override
- public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction) {
- return true;
- }
- @Override
- public double demandedEnergyUnits() {
- return 25600.0D - energyAmount;
- }
- @Override
- public double injectEnergyUnits(ForgeDirection directionFrom, double amount) {
- if(energyAmount < 25600.0D){
- energyAmount += amount;
- if(!working) recalculateWorking();
- return 0;
- }else{
- return amount;
- }
- }
- @Override
- public int getMaxSafeInput() {
- return 128;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment