Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nathan72419.SingularityUtil.TileEntity;
- import com.nathan72419.SingularityUtil.misc.recipe.MixerRecipesManager;
- import cofh.api.energy.EnergyStorage;
- import cofh.api.energy.IEnergyReceiver;
- import cofh.core.util.fluid.FluidTankAdv;
- import cpw.mods.fml.relauncher.Side;
- import cpw.mods.fml.relauncher.SideOnly;
- 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.tileentity.TileEntity;
- import net.minecraftforge.common.util.ForgeDirection;
- import net.minecraftforge.fluids.Fluid;
- import net.minecraftforge.fluids.FluidContainerRegistry;
- import net.minecraftforge.fluids.FluidStack;
- import net.minecraftforge.fluids.FluidTankInfo;
- import net.minecraftforge.fluids.IFluidHandler;
- import tconstruct.TConstruct;
- public class TileEntityMixer extends TileEntity implements ISidedInventory, IFluidHandler, IEnergyReceiver{
- private ItemStack[] MixerItemStacks = new ItemStack [1];
- private static final int[] slotsTop = new int[] {0};
- private static final int[] slotsBottom = new int[] {2, 1};
- private static final int[] slotsSides = new int[] {1};
- public int mixingTime;
- public int currentMixingTime;
- private String name;
- public FluidStack inputFluid;
- public FluidStack outputFluid;
- FluidTankAdv inputTank = new FluidTankAdv(FluidContainerRegistry.BUCKET_VOLUME * 4);
- FluidTankAdv outputTank = new FluidTankAdv(FluidContainerRegistry.BUCKET_VOLUME * 4);
- EnergyStorage Battery = new EnergyStorage(10000);
- @Override
- public int getSizeInventory() {
- return this.MixerItemStacks.length;
- }
- @Override
- public ItemStack getStackInSlot(int var) {
- return this.MixerItemStacks[var];
- }
- @Override
- public ItemStack decrStackSize(int var1, int var2) {
- if (this.MixerItemStacks[var1] != null){
- ItemStack stack;
- if (this.MixerItemStacks[var1].stackSize <= var2){
- stack = this.MixerItemStacks[var1];
- this.MixerItemStacks[var1] = null;
- return stack;
- }
- else{
- stack = this.MixerItemStacks[var1].splitStack(var2);
- if(this.MixerItemStacks[var1].stackSize == 0){
- this.MixerItemStacks[var1] = null;
- }
- return stack;
- }
- }
- else {
- return null;
- }
- }
- public void naming(String name)
- {
- this.name = name;
- }
- @Override
- public ItemStack getStackInSlotOnClosing(int var1) {
- if (this.MixerItemStacks[var1] != null)
- {
- ItemStack stack = this.MixerItemStacks[var1];
- this.MixerItemStacks[var1] = null;
- return stack;
- }
- else {
- return null;
- }
- }
- @Override
- public void setInventorySlotContents(int p_70299_1_, ItemStack p_70299_2_) {
- }
- @Override
- public String getInventoryName() {
- return this.hasCustomInventoryName() ? this.name : "container.mixer";
- }
- @Override
- public boolean hasCustomInventoryName() {
- return this.name != null && this.name.length() > 0;
- }
- @Override
- public int getInventoryStackLimit() {
- return 64;
- }
- @Override
- public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {
- return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : p_70300_1_.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
- }
- public boolean IsInputFluid(Fluid fluid){
- return MixerRecipesManager.isFluidValid(fluid);
- }
- public boolean isMixing() {
- return inputTank.getFluidAmount() > TConstruct.oreLiquidValue;
- }
- public void readfromNBT(NBTTagCompound nbt){
- super.readFromNBT(nbt);
- this.MixerItemStacks = new ItemStack[this.getSizeInventory()];
- NBTTagList nbttaglist = nbt.getTagList("Items", 10);
- for (int i = 0; i < nbttaglist.tagCount(); ++i)
- {
- NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
- byte b0 = nbttagcompound1.getByte("Slot");
- if (b0 >= 0 && b0 < this.MixerItemStacks.length)
- {
- this.MixerItemStacks[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
- }
- }
- if (nbt.hasKey("CustomName", 8)){
- this.name = nbt.getString("CustomName");
- }
- this.mixingTime = nbt.getShort("MixingTime");
- inputTank.readFromNBT(nbt.getCompoundTag("InputTank"));
- outputTank.readFromNBT(nbt.getCompoundTag("OutputTank"));
- }
- public void writeToNBT(NBTTagCompound nbt){
- super.writeToNBT(nbt);
- nbt.setShort("MixingTime", (short)this.mixingTime);
- NBTTagList nbttaglist = new NBTTagList();
- for (int i = 0; i < this.MixerItemStacks.length; ++i)
- {
- if (this.MixerItemStacks[i] != null)
- {
- NBTTagCompound nbttagcompound1 = new NBTTagCompound();
- nbttagcompound1.setByte("Slot", (byte)i);
- this.MixerItemStacks[i].writeToNBT(nbttagcompound1);
- nbttaglist.appendTag(nbttagcompound1);
- }
- }
- nbt.setTag("Items", nbttaglist);
- if (this.hasCustomInventoryName())
- {
- nbt.setString("CustomName", this.name);
- }
- nbt.setTag("InputTank", inputTank.writeToNBT(new NBTTagCompound()));
- nbt.setTag("OutputTank", outputTank.writeToNBT(new NBTTagCompound()));
- }
- public void updateEntity(){
- boolean isMixing = inputTank.getFluidAmount() > TConstruct.oreLiquidValue;
- boolean flag = false;
- if(!this.worldObj.isRemote)
- {
- if (this.MixerItemStacks[0] != null && this.inputTank.getFluid() != null && this.Battery.getEnergyStored() >= 15)
- {
- if(this.canMix())
- {
- if (inputTank.getFluidAmount() >= TConstruct.oreLiquidValue)
- {
- flag = true;
- }
- }
- if (this.isMixing() && this.canMix())
- {
- ++this.mixingTime;
- if (this.mixingTime == 200){
- this.mixingTime = 0;
- this.Mix();
- flag = true;
- }
- }
- else
- {
- this.mixingTime = 0;
- }
- }
- if (isMixing != (inputTank.getFluidAmount() >= TConstruct.oreLiquidValue) && this.Battery.getEnergyStored() >= 15){
- flag = true;
- }
- }
- if(flag)
- {
- this.markDirty();
- }
- }
- public void Mix() {
- if (this.canMix())
- {
- FluidStack output = MixerRecipesManager.mixing().getOutput(this.MixerItemStacks[0], this.inputTank.getFluid());
- int OutputAmount = 0;
- int InputAmount = this.inputTank.getFluidAmount();
- if (this.outputTank.getFluidAmount() ==0)
- {
- this.outputTank.setFluid(output);
- if (this.outputTank.getFluidAmount() < output.amount)
- {
- ++ OutputAmount;
- this.outputTank.fill(new FluidStack(output, OutputAmount), true);
- }
- }
- else if (this.outputTank.getFluid().getFluid() == output.getFluid()){
- OutputAmount += output.amount;
- this.outputTank.fill(new FluidStack(output, OutputAmount), true);
- }
- --InputAmount;
- this.Battery.extractEnergy(15, false);
- if (InputAmount <= 0)
- {
- this.inputTank.setFluid(null);
- }
- this.inputTank.drain(InputAmount, true);
- }
- }
- private boolean canMix()
- {
- if(this.MixerItemStacks[0] == null || this.inputFluid == null)
- {
- return false;
- }
- else
- {
- FluidStack fluid = MixerRecipesManager.mixing().getOutput(this.MixerItemStacks[0], this.inputTank.getFluid());
- if (fluid == null){ return false; }
- if (this.outputTank.getFluid() == null) {return true;}
- if (!this.outputTank.getFluid().isFluidEqual(fluid)) {return false;}
- else{return false;}
- }
- }
- @Override
- public void openInventory() {}
- @Override
- public void closeInventory() {}
- @Override
- public boolean isItemValidForSlot(int slot, ItemStack stack) {
- return MixerRecipesManager.isItemValid(stack);
- }
- // SidedInventory Method
- @Override
- public int[] getAccessibleSlotsFromSide(int something) {
- return something == 0 ? slotsBottom : (something == 1 ? slotsTop : slotsSides);
- }
- @Override
- public boolean canInsertItem(int p_102007_1_, ItemStack stack, int direction) {
- return this.isItemValidForSlot(p_102007_1_, stack);
- }
- @Override
- public boolean canExtractItem(int p_102008_1_, ItemStack p_102008_2_, int p_102008_3_) {
- return false;
- }
- //Client Side
- @SideOnly(Side.CLIENT)
- public int getMixingProgress(int part)
- {
- return this.mixingTime*part/200;
- }
- //FluidHandler
- @Override
- public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
- if (IsInputFluid(resource.getFluid()) && (from != ForgeDirection.DOWN || from != ForgeDirection.DOWN)) {
- return inputTank.fill(resource, doFill);
- }
- return 0;
- }
- @Override
- public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
- return null;
- }
- @Override
- public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
- return from == ForgeDirection.DOWN ? new FluidStack(this.inputFluid.getFluid(), maxDrain) : null;
- }
- @Override
- public boolean canFill(ForgeDirection from, Fluid fluid) {
- return true;
- }
- @Override
- public boolean canDrain(ForgeDirection from, Fluid fluid) {
- return true;
- }
- @Override
- public FluidTankInfo[] getTankInfo(ForgeDirection from) {
- return new FluidTankInfo[] { inputTank.getInfo(), outputTank.getInfo() };
- }
- @Override
- public boolean canConnectEnergy(ForgeDirection from) {
- return true;
- }
- @Override
- public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) {
- return this.Battery.receiveEnergy(maxReceive, false);
- }
- @Override
- public int getEnergyStored(ForgeDirection from) {
- return this.Battery.getEnergyStored();
- }
- @Override
- public int getMaxEnergyStored(ForgeDirection from) {
- return 120000;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement