287 lines
10 KiB
Java

package com.hbm.tileentity.machine;
import java.util.HashMap;
import java.util.List;
import com.hbm.blocks.ModBlocks;
import com.hbm.interfaces.IControlReceiver;
import com.hbm.inventory.UpgradeManagerNT;
import com.hbm.inventory.container.ContainerMachinePUREX;
import com.hbm.inventory.fluid.Fluids;
import com.hbm.inventory.fluid.tank.FluidTank;
import com.hbm.inventory.gui.GUIMachinePUREX;
import com.hbm.inventory.recipes.PUREXRecipes;
import com.hbm.inventory.recipes.loader.GenericRecipe;
import com.hbm.items.ModItems;
import com.hbm.items.machine.ItemMachineUpgrade;
import com.hbm.items.machine.ItemMachineUpgrade.UpgradeType;
import com.hbm.lib.Library;
import com.hbm.module.machine.ModuleMachinePUREX;
import com.hbm.tileentity.IGUIProvider;
import com.hbm.tileentity.IUpgradeInfoProvider;
import com.hbm.tileentity.TileEntityMachineBase;
import com.hbm.util.BobMathUtil;
import com.hbm.util.fauxpointtwelve.DirPos;
import com.hbm.util.i18n.I18nUtil;
import api.hbm.energymk2.IEnergyReceiverMK2;
import api.hbm.fluidmk2.IFluidStandardTransceiverMK2;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
public class TileEntityMachinePUREX extends TileEntityMachineBase implements IEnergyReceiverMK2, IFluidStandardTransceiverMK2, IUpgradeInfoProvider, IControlReceiver, IGUIProvider {
public FluidTank[] inputTanks;
public FluidTank[] outputTanks;
public long power;
public long maxPower = 1_000_000;
public boolean didProcess = false;
public boolean frame = false;
public int anim;
public int prevAnim;
public ModuleMachinePUREX purexModule;
public UpgradeManagerNT upgradeManager = new UpgradeManagerNT(this);
public TileEntityMachinePUREX() {
super(13);
this.inputTanks = new FluidTank[3];
this.outputTanks = new FluidTank[1];
for(int i = 0; i < 3; i++) {
this.inputTanks[i] = new FluidTank(Fluids.NONE, 24_000);
}
this.outputTanks[0] = new FluidTank(Fluids.NONE, 24_000);
this.purexModule = new ModuleMachinePUREX(0, this, slots)
.itemInput(4).itemOutput(7)
.fluidInput(inputTanks[0], inputTanks[1], inputTanks[2]).fluidOutput(outputTanks[0]);
}
@Override
public String getName() {
return "container.machinePUREX";
}
@Override
public void updateEntity() {
if(maxPower <= 0) this.maxPower = 1_000_000;
if(!worldObj.isRemote) {
GenericRecipe recipe = PUREXRecipes.INSTANCE.recipeNameMap.get(purexModule.recipe);
if(recipe != null) {
this.maxPower = recipe.power * 100;
}
this.maxPower = BobMathUtil.max(this.power, this.maxPower, 1_000_000);
this.power = Library.chargeTEFromItems(slots, 0, power, maxPower);
upgradeManager.checkSlots(slots, 2, 3);
for(DirPos pos : getConPos()) {
this.trySubscribe(worldObj, pos);
for(FluidTank tank : inputTanks) if(tank.getTankType() != Fluids.NONE) this.trySubscribe(tank.getTankType(), worldObj, pos);
for(FluidTank tank : outputTanks) if(tank.getFill() > 0) this.tryProvide(tank, worldObj, pos);
}
double speed = 1D;
double pow = 1D;
speed += Math.min(upgradeManager.getLevel(UpgradeType.SPEED), 3) / 3D;
speed += Math.min(upgradeManager.getLevel(UpgradeType.OVERDRIVE), 3);
pow -= Math.min(upgradeManager.getLevel(UpgradeType.POWER), 3) * 0.25D;
pow += Math.min(upgradeManager.getLevel(UpgradeType.SPEED), 3) * 1D;
pow += Math.min(upgradeManager.getLevel(UpgradeType.OVERDRIVE), 3) * 10D / 3D;
this.purexModule.update(speed, pow, true, slots[1]);
this.didProcess = this.purexModule.didProcess;
if(this.purexModule.markDirty) this.markDirty();
this.networkPackNT(100);
} else {
this.prevAnim = this.anim;
if(this.didProcess) this.anim++;
if(worldObj.getTotalWorldTime() % 20 == 0) {
frame = !worldObj.getBlock(xCoord, yCoord + 5, zCoord).isAir(worldObj, xCoord, yCoord + 5, zCoord);
}
}
}
public DirPos[] getConPos() {
return new DirPos[] {
new DirPos(xCoord + 3, yCoord, zCoord - 2, Library.POS_X),
new DirPos(xCoord + 3, yCoord, zCoord - 1, Library.POS_X),
new DirPos(xCoord + 3, yCoord, zCoord + 0, Library.POS_X),
new DirPos(xCoord + 3, yCoord, zCoord + 1, Library.POS_X),
new DirPos(xCoord + 3, yCoord, zCoord + 2, Library.POS_X),
new DirPos(xCoord - 3, yCoord, zCoord - 1, Library.NEG_X),
new DirPos(xCoord - 3, yCoord, zCoord - 2, Library.NEG_X),
new DirPos(xCoord - 3, yCoord, zCoord + 0, Library.NEG_X),
new DirPos(xCoord - 3, yCoord, zCoord + 1, Library.NEG_X),
new DirPos(xCoord - 3, yCoord, zCoord + 2, Library.NEG_X),
new DirPos(xCoord - 2, yCoord, zCoord + 3, Library.POS_Z),
new DirPos(xCoord - 1, yCoord, zCoord + 3, Library.POS_Z),
new DirPos(xCoord + 0, yCoord, zCoord + 3, Library.POS_Z),
new DirPos(xCoord + 1, yCoord, zCoord + 3, Library.POS_Z),
new DirPos(xCoord + 2, yCoord, zCoord + 3, Library.POS_Z),
new DirPos(xCoord - 2, yCoord, zCoord - 3, Library.NEG_Z),
new DirPos(xCoord - 1, yCoord, zCoord - 3, Library.NEG_Z),
new DirPos(xCoord + 0, yCoord, zCoord - 3, Library.NEG_Z),
new DirPos(xCoord + 1, yCoord, zCoord - 3, Library.NEG_Z),
new DirPos(xCoord + 2, yCoord, zCoord - 3, Library.NEG_Z),
};
}
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
for(FluidTank tank : inputTanks) tank.serialize(buf);
for(FluidTank tank : outputTanks) tank.serialize(buf);
buf.writeLong(power);
buf.writeLong(maxPower);
buf.writeBoolean(didProcess);
this.purexModule.serialize(buf);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
for(FluidTank tank : inputTanks) tank.deserialize(buf);
for(FluidTank tank : outputTanks) tank.deserialize(buf);
this.power = buf.readLong();
this.maxPower = buf.readLong();
this.didProcess = buf.readBoolean();
this.purexModule.deserialize(buf);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
for(int i = 0; i < 3; i++) {
this.inputTanks[i].readFromNBT(nbt, "i" + i);
}
this.outputTanks[0].readFromNBT(nbt, "o" + 0);
this.power = nbt.getLong("power");
this.maxPower = nbt.getLong("maxPower");
this.purexModule.readFromNBT(nbt);
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
for(int i = 0; i < 3; i++) {
this.inputTanks[i].writeToNBT(nbt, "i" + i);
}
this.outputTanks[0].writeToNBT(nbt, "o" + 0);
nbt.setLong("power", power);
nbt.setLong("maxPower", maxPower);
this.purexModule.writeToNBT(nbt);
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
if(slot == 0) return true; // battery
if(slot == 1 && stack.getItem() == ModItems.blueprints) return true;
if(slot >= 2 && slot <= 3 && stack.getItem() instanceof ItemMachineUpgrade) return true; // upgrades
if(this.purexModule.isItemValid(slot, stack)) return true; // recipe input crap
return false;
}
@Override
public boolean canExtractItem(int i, ItemStack itemStack, int j) {
return (i >= 7 && i <= 12) || this.purexModule.isSlotClogged(i);
}
@Override
public int[] getAccessibleSlotsFromSide(int side) {
return new int[] {4, 5, 6, 7, 8, 9, 10, 11, 12};
}
@Override public long getPower() { return power; }
@Override public void setPower(long power) { this.power = power; }
@Override public long getMaxPower() { return maxPower; }
@Override public FluidTank[] getReceivingTanks() { return inputTanks; }
@Override public FluidTank[] getSendingTanks() { return outputTanks; }
@Override public FluidTank[] getAllTanks() { return new FluidTank[] {inputTanks[0], inputTanks[1], inputTanks[2], outputTanks[0]}; }
@Override public Container provideContainer(int ID, EntityPlayer player, World world, int x, int y, int z) { return new ContainerMachinePUREX(player.inventory, this); }
@Override @SideOnly(Side.CLIENT) public Object provideGUI(int ID, EntityPlayer player, World world, int x, int y, int z) { return new GUIMachinePUREX(player.inventory, this); }
@Override public boolean hasPermission(EntityPlayer player) { return this.isUseableByPlayer(player); }
@Override
public void receiveControl(NBTTagCompound data) {
if(data.hasKey("index") && data.hasKey("selection")) {
int index = data.getInteger("index");
String selection = data.getString("selection");
if(index == 0) {
this.purexModule.recipe = selection;
this.markChanged();
}
}
}
AxisAlignedBB bb = null;
@Override
public AxisAlignedBB getRenderBoundingBox() {
if(bb == null) bb = AxisAlignedBB.getBoundingBox(xCoord - 2, yCoord, zCoord - 2, xCoord + 3, yCoord + 5, zCoord + 3);
return bb;
}
@Override
@SideOnly(Side.CLIENT)
public double getMaxRenderDistanceSquared() {
return 65536.0D;
}
@Override
public boolean canProvideInfo(UpgradeType type, int level, boolean extendedInfo) {
return type == UpgradeType.SPEED || type == UpgradeType.POWER || type == UpgradeType.OVERDRIVE;
}
@Override
public void provideInfo(UpgradeType type, int level, List<String> info, boolean extendedInfo) {
info.add(IUpgradeInfoProvider.getStandardLabel(ModBlocks.machine_purex));
if(type == UpgradeType.SPEED) {
info.add(EnumChatFormatting.GREEN + I18nUtil.resolveKey(KEY_SPEED, "+" + (level * 100 / 3) + "%"));
info.add(EnumChatFormatting.RED + I18nUtil.resolveKey(KEY_CONSUMPTION, "+" + (level * 50) + "%"));
}
if(type == UpgradeType.POWER) {
info.add(EnumChatFormatting.GREEN + I18nUtil.resolveKey(KEY_CONSUMPTION, "-" + (level * 25) + "%"));
}
if(type == UpgradeType.OVERDRIVE) {
info.add((BobMathUtil.getBlink() ? EnumChatFormatting.RED : EnumChatFormatting.DARK_GRAY) + "YES");
}
}
@Override
public HashMap<UpgradeType, Integer> getValidUpgrades() {
HashMap<UpgradeType, Integer> upgrades = new HashMap<>();
upgrades.put(UpgradeType.SPEED, 3);
upgrades.put(UpgradeType.POWER, 3);
upgrades.put(UpgradeType.OVERDRIVE, 3);
return upgrades;
}
}