mirror of
https://github.com/HbmMods/Hbm-s-Nuclear-Tech-GIT.git
synced 2026-01-25 10:32:49 +00:00
378 lines
8.6 KiB
Java
378 lines
8.6 KiB
Java
package com.hbm.tileentity.machine;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
import com.hbm.handler.FluidTypeHandler.FluidType;
|
|
import com.hbm.interfaces.IConsumer;
|
|
import com.hbm.interfaces.IFluidAcceptor;
|
|
import com.hbm.interfaces.IFluidContainer;
|
|
import com.hbm.inventory.FluidTank;
|
|
import com.hbm.inventory.MachineRecipes;
|
|
import com.hbm.inventory.MachineRecipes.GasCentOutput;
|
|
import com.hbm.lib.Library;
|
|
import com.hbm.packet.AuxElectricityPacket;
|
|
import com.hbm.packet.AuxGaugePacket;
|
|
import com.hbm.packet.LoopedSoundPacket;
|
|
import com.hbm.packet.PacketDispatcher;
|
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
import net.minecraft.init.Items;
|
|
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.minecraft.util.AxisAlignedBB;
|
|
|
|
public class TileEntityMachineGasCent extends TileEntity implements ISidedInventory, IConsumer, IFluidContainer, IFluidAcceptor {
|
|
|
|
private ItemStack slots[];
|
|
|
|
public long power;
|
|
public int progress;
|
|
public boolean isProgressing;
|
|
public static final int maxPower = 100000;
|
|
public static final int processingSpeed = 200;
|
|
|
|
public FluidTank tank;
|
|
|
|
private static final int[] slots_top = new int[] {3};
|
|
private static final int[] slots_bottom = new int[] {5, 6, 7, 8};
|
|
private static final int[] slots_side = new int[] {0, 3};
|
|
|
|
private String customName;
|
|
|
|
public TileEntityMachineGasCent() {
|
|
slots = new ItemStack[9];
|
|
tank = new FluidTank(FluidType.UF6, 8000, 0);
|
|
}
|
|
|
|
@Override
|
|
public int getSizeInventory() {
|
|
return slots.length;
|
|
}
|
|
|
|
@Override
|
|
public ItemStack getStackInSlot(int i) {
|
|
return slots[i];
|
|
}
|
|
|
|
@Override
|
|
public ItemStack getStackInSlotOnClosing(int i) {
|
|
if(slots[i] != null)
|
|
{
|
|
ItemStack itemStack = slots[i];
|
|
slots[i] = null;
|
|
return itemStack;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void setInventorySlotContents(int i, ItemStack itemStack) {
|
|
slots[i] = itemStack;
|
|
if(itemStack != null && itemStack.stackSize > getInventoryStackLimit())
|
|
{
|
|
itemStack.stackSize = getInventoryStackLimit();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String getInventoryName() {
|
|
return this.hasCustomInventoryName() ? this.customName : "container.gasCentrifuge";
|
|
}
|
|
|
|
@Override
|
|
public boolean hasCustomInventoryName() {
|
|
return this.customName != null && this.customName.length() > 0;
|
|
}
|
|
|
|
public void setCustomName(String name) {
|
|
this.customName = name;
|
|
}
|
|
|
|
@Override
|
|
public int getInventoryStackLimit() {
|
|
return 64;
|
|
}
|
|
|
|
@Override
|
|
public boolean isUseableByPlayer(EntityPlayer player) {
|
|
if(worldObj.getTileEntity(xCoord, yCoord, zCoord) != this)
|
|
{
|
|
return false;
|
|
}else{
|
|
return player.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D, zCoord + 0.5D) <=64;
|
|
}
|
|
}
|
|
|
|
//You scrubs aren't needed for anything (right now)
|
|
@Override
|
|
public void openInventory() {}
|
|
@Override
|
|
public void closeInventory() {}
|
|
|
|
@Override
|
|
public boolean isItemValidForSlot(int i, ItemStack itemStack) {
|
|
if(i == 2 || i == 3 || i == 4 || i == 5)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public ItemStack decrStackSize(int i, int j) {
|
|
if(slots[i] != null)
|
|
{
|
|
if(slots[i].stackSize <= j)
|
|
{
|
|
ItemStack itemStack = slots[i];
|
|
slots[i] = null;
|
|
return itemStack;
|
|
}
|
|
ItemStack itemStack1 = slots[i].splitStack(j);
|
|
if (slots[i].stackSize == 0)
|
|
{
|
|
slots[i] = null;
|
|
}
|
|
|
|
return itemStack1;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void readFromNBT(NBTTagCompound nbt) {
|
|
super.readFromNBT(nbt);
|
|
NBTTagList list = nbt.getTagList("items", 10);
|
|
|
|
power = nbt.getLong("powerTime");
|
|
progress = nbt.getShort("CookTime");
|
|
tank.readFromNBT(nbt, "tank");
|
|
slots = new ItemStack[getSizeInventory()];
|
|
|
|
for(int i = 0; i < list.tagCount(); i++)
|
|
{
|
|
NBTTagCompound nbt1 = list.getCompoundTagAt(i);
|
|
byte b0 = nbt1.getByte("slot");
|
|
if(b0 >= 0 && b0 < slots.length)
|
|
{
|
|
slots[b0] = ItemStack.loadItemStackFromNBT(nbt1);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void writeToNBT(NBTTagCompound nbt) {
|
|
super.writeToNBT(nbt);
|
|
nbt.setLong("powerTime", power);
|
|
nbt.setShort("cookTime", (short) progress);
|
|
tank.writeToNBT(nbt, "tank");
|
|
NBTTagList list = new NBTTagList();
|
|
|
|
for(int i = 0; i < slots.length; i++)
|
|
{
|
|
if(slots[i] != null)
|
|
{
|
|
NBTTagCompound nbt1 = new NBTTagCompound();
|
|
nbt1.setByte("slot", (byte)i);
|
|
slots[i].writeToNBT(nbt1);
|
|
list.appendTag(nbt1);
|
|
}
|
|
}
|
|
nbt.setTag("items", list);
|
|
}
|
|
|
|
@Override
|
|
public int[] getAccessibleSlotsFromSide(int p_94128_1_)
|
|
{
|
|
return p_94128_1_ == 0 ? slots_bottom : (p_94128_1_ == 1 ? slots_top : slots_side);
|
|
}
|
|
|
|
@Override
|
|
public boolean canInsertItem(int i, ItemStack itemStack, int j) {
|
|
return this.isItemValidForSlot(i, itemStack);
|
|
}
|
|
|
|
@Override
|
|
public boolean canExtractItem(int i, ItemStack itemStack, int j) {
|
|
return j != 0 || i != 1 || itemStack.getItem() == Items.bucket;
|
|
}
|
|
|
|
public int getCentrifugeProgressScaled(int i) {
|
|
return (progress * i) / processingSpeed;
|
|
}
|
|
|
|
public long getPowerRemainingScaled(int i) {
|
|
return (power * i) / maxPower;
|
|
}
|
|
|
|
private boolean canProcess() {
|
|
|
|
if(power > 0 && this.tank.getFill() >= MachineRecipes.getFluidConsumedGasCent(tank.getTankType())) {
|
|
|
|
List<GasCentOutput> list = MachineRecipes.getGasCentOutput(tank.getTankType());
|
|
|
|
if(list == null)
|
|
return false;
|
|
|
|
if(list.size() < 1 || list.size() > 4)
|
|
return false;
|
|
|
|
for(int i = 0; i < list.size(); i++) {
|
|
|
|
int slot = i + 5;
|
|
|
|
if(slots[slot] == null)
|
|
continue;
|
|
|
|
if(slots[slot].getItem() == list.get(i).output.getItem() &&
|
|
slots[slot].getItemDamage() == list.get(i).output.getItemDamage() &&
|
|
slots[slot].stackSize + list.get(i).output.stackSize <= slots[slot].getMaxStackSize())
|
|
continue;
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private void process() {
|
|
|
|
List<GasCentOutput> out = MachineRecipes.getGasCentOutput(tank.getTankType());
|
|
this.progress = 0;
|
|
tank.setFill(tank.getFill() - MachineRecipes.getFluidConsumedGasCent(tank.getTankType()));
|
|
|
|
List<GasCentOutput> random = new ArrayList();
|
|
|
|
for(int i = 0; i < out.size(); i++) {
|
|
for(int j = 0; j < out.get(i).weight; j++) {
|
|
random.add(out.get(i));
|
|
}
|
|
}
|
|
|
|
Collections.shuffle(random);
|
|
|
|
GasCentOutput result = random.get(worldObj.rand.nextInt(random.size()));
|
|
|
|
int slot = result.slot + 4;
|
|
|
|
if(slots[slot] == null) {
|
|
slots[slot] = result.output.copy();
|
|
} else {
|
|
slots[slot].stackSize += result.output.stackSize;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void updateEntity() {
|
|
|
|
if(!worldObj.isRemote) {
|
|
|
|
power = Library.chargeTEFromItems(slots, 0, power, maxPower);
|
|
tank.setType(1, 2, slots);
|
|
tank.loadTank(3, 4, slots);
|
|
tank.updateTank(xCoord, yCoord, zCoord, worldObj.provider.dimensionId);
|
|
|
|
if(canProcess()) {
|
|
|
|
isProgressing = true;
|
|
|
|
this.progress++;
|
|
|
|
this.power -= 200;
|
|
|
|
if(this.power < 0)
|
|
power = 0;
|
|
|
|
if(progress >= processingSpeed) {
|
|
process();
|
|
}
|
|
|
|
} else {
|
|
isProgressing = false;
|
|
this.progress = 0;
|
|
}
|
|
|
|
PacketDispatcher.wrapper.sendToAll(new AuxElectricityPacket(xCoord, yCoord, zCoord, power));
|
|
PacketDispatcher.wrapper.sendToAll(new AuxGaugePacket(xCoord, yCoord, zCoord, progress, 0));
|
|
PacketDispatcher.wrapper.sendToAll(new AuxGaugePacket(xCoord, yCoord, zCoord, isProgressing ? 1 : 0, 1));
|
|
PacketDispatcher.wrapper.sendToAll(new LoopedSoundPacket(xCoord, yCoord, zCoord));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public AxisAlignedBB getRenderBoundingBox() {
|
|
return TileEntity.INFINITE_EXTENT_AABB;
|
|
}
|
|
|
|
@Override
|
|
@SideOnly(Side.CLIENT)
|
|
public double getMaxRenderDistanceSquared()
|
|
{
|
|
return 65536.0D;
|
|
}
|
|
|
|
@Override
|
|
public void setPower(long i) {
|
|
power = i;
|
|
}
|
|
|
|
@Override
|
|
public long getPower() {
|
|
return power;
|
|
|
|
}
|
|
|
|
@Override
|
|
public long getMaxPower() {
|
|
return maxPower;
|
|
}
|
|
|
|
@Override
|
|
public void setFillstate(int fill, int index) {
|
|
tank.setFill(fill);
|
|
}
|
|
|
|
@Override
|
|
public void setType(FluidType type, int index) {
|
|
tank.setTankType(type);
|
|
}
|
|
|
|
@Override
|
|
public int getMaxFluidFill(FluidType type) {
|
|
return type.name().equals(this.tank.getTankType().name()) ? tank.getMaxFill() : 0;
|
|
}
|
|
|
|
@Override
|
|
public int getFluidFill(FluidType type) {
|
|
return type.name().equals(this.tank.getTankType().name()) ? tank.getFill() : 0;
|
|
}
|
|
|
|
@Override
|
|
public void setFluidFill(int i, FluidType type) {
|
|
if(type.name().equals(tank.getTankType().name()))
|
|
tank.setFill(i);
|
|
}
|
|
|
|
@Override
|
|
public List<FluidTank> getTanks() {
|
|
List<FluidTank> list = new ArrayList();
|
|
list.add(tank);
|
|
|
|
return list;
|
|
}
|
|
|
|
}
|