2018-02-28 21:03:26 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Industrial Wires.
|
2018-02-28 21:06:33 +01:00
|
|
|
* Copyright (C) 2016-2018 malte0811
|
2018-02-28 21:03:26 +01:00
|
|
|
* Industrial Wires is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
* Industrial Wires is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Industrial Wires. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-06-15 16:15:06 +02:00
|
|
|
package malte0811.industrialWires.mech_mb;
|
2018-02-28 21:03:26 +01:00
|
|
|
|
2018-06-16 19:15:15 +02:00
|
|
|
import blusunrize.immersiveengineering.api.IEEnums.SideConfig;
|
|
|
|
import blusunrize.immersiveengineering.common.util.Utils;
|
2018-03-24 17:45:25 +01:00
|
|
|
import malte0811.industrialWires.IWConfig;
|
2018-06-16 19:15:15 +02:00
|
|
|
import malte0811.industrialWires.blocks.IWProperties;
|
2018-06-15 16:15:06 +02:00
|
|
|
import malte0811.industrialWires.mech_mb.EUCapability.IC2EnergyHandler;
|
2018-02-28 21:03:26 +01:00
|
|
|
import malte0811.industrialWires.util.ConversionUtil;
|
2018-06-16 19:15:15 +02:00
|
|
|
import malte0811.industrialWires.util.MBSideConfig;
|
|
|
|
import malte0811.industrialWires.util.MBSideConfig.BlockFace;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2018-02-28 21:03:26 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
2018-06-16 19:15:15 +02:00
|
|
|
import net.minecraft.util.EnumHand;
|
2018-02-28 21:03:26 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2018-06-16 19:15:15 +02:00
|
|
|
import net.minecraft.util.math.Vec3i;
|
2018-02-28 21:03:26 +01:00
|
|
|
import net.minecraftforge.common.capabilities.Capability;
|
2018-06-16 19:15:15 +02:00
|
|
|
import net.minecraftforge.common.property.IExtendedBlockState;
|
|
|
|
import net.minecraftforge.common.util.Constants;
|
2018-06-15 16:15:06 +02:00
|
|
|
import net.minecraftforge.energy.CapabilityEnergy;
|
2018-02-28 21:03:26 +01:00
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
|
|
|
|
2018-06-16 19:15:15 +02:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import java.util.List;
|
2018-03-24 17:45:25 +01:00
|
|
|
|
2018-06-16 19:15:15 +02:00
|
|
|
import static blusunrize.immersiveengineering.api.IEEnums.SideConfig.INPUT;
|
|
|
|
import static blusunrize.immersiveengineering.api.IEEnums.SideConfig.OUTPUT;
|
2018-06-15 16:15:06 +02:00
|
|
|
import static malte0811.industrialWires.mech_mb.EUCapability.ENERGY_IC2;
|
|
|
|
import static malte0811.industrialWires.mech_mb.Waveform.Phases.get;
|
|
|
|
import static malte0811.industrialWires.mech_mb.Waveform.Speed.EXTERNAL;
|
|
|
|
import static malte0811.industrialWires.mech_mb.Waveform.Speed.ROTATION;
|
|
|
|
import static malte0811.industrialWires.mech_mb.Waveform.Type.DC;
|
|
|
|
import static malte0811.industrialWires.mech_mb.Waveform.Type.NONE;
|
2018-02-28 21:03:26 +01:00
|
|
|
import static malte0811.industrialWires.util.NBTKeys.*;
|
|
|
|
import static net.minecraftforge.energy.CapabilityEnergy.ENERGY;
|
|
|
|
|
2018-06-15 16:15:06 +02:00
|
|
|
public abstract class MechPartEnergyIO extends MechMBPart implements IMBPartElectric {
|
2018-02-28 21:03:26 +01:00
|
|
|
private double bufferToMB;
|
2018-03-24 17:45:25 +01:00
|
|
|
private Waveform wfToMB = Waveform.forParameters(NONE, get(has4Phases()), ROTATION);
|
2018-02-28 21:03:26 +01:00
|
|
|
private double bufferToWorld;
|
2018-03-24 17:45:25 +01:00
|
|
|
private Waveform wfToWorld = Waveform.forParameters(NONE, get(has4Phases()), ROTATION);
|
2018-06-16 19:15:15 +02:00
|
|
|
private final IEnergyStorage[] capForge = {new EnergyStorageMMB(INPUT), new EnergyStorageMMB(OUTPUT)};
|
|
|
|
private final IC2EnergyHandler[] capIc2 = {new IC2EHandlerMB(INPUT), new IC2EHandlerMB(OUTPUT)};
|
|
|
|
private MBSideConfig sides = new MBSideConfig(getEnergyConnections());
|
2018-06-15 09:35:14 +02:00
|
|
|
|
2018-02-28 21:03:26 +01:00
|
|
|
@Override
|
|
|
|
public Waveform getProduced(MechEnergy state) {
|
2018-06-15 16:15:06 +02:00
|
|
|
return transform(wfToMB, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Waveform transform(Waveform wf, MechEnergy e) {
|
|
|
|
return wf;
|
2018-02-28 21:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getAvailableEEnergy() {
|
|
|
|
return bufferToMB;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void extractEEnergy(double energy) {
|
|
|
|
bufferToMB -= energy;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double requestEEnergy(Waveform waveform, MechEnergy energy) {
|
2018-03-27 21:31:03 +02:00
|
|
|
if (!has4Phases()==waveform.isSinglePhase()) {
|
|
|
|
return getMaxBuffer() - bufferToWorld;
|
2018-02-28 21:03:26 +01:00
|
|
|
}
|
2018-03-23 16:27:32 +01:00
|
|
|
return 0;
|
2018-02-28 21:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-24 17:45:25 +01:00
|
|
|
public void insertEEnergy(double given, Waveform waveform, MechEnergy mechEnergy) {
|
2018-06-15 16:15:06 +02:00
|
|
|
waveform = transform(waveform, mechEnergy);
|
2018-03-27 21:31:03 +02:00
|
|
|
wfToWorld = waveform;
|
2018-02-28 21:03:26 +01:00
|
|
|
bufferToWorld += given;
|
|
|
|
}
|
|
|
|
|
2018-06-15 16:15:06 +02:00
|
|
|
@Override
|
|
|
|
public <T> T getCapability(Capability<T> cap, EnumFacing side, BlockPos pos) {
|
2018-06-16 19:15:15 +02:00
|
|
|
BlockFace s = new BlockFace(pos, side);
|
|
|
|
SideConfig conf = sides.getConfigForFace(s);
|
|
|
|
if (conf!=SideConfig.NONE) {
|
2018-06-15 16:15:06 +02:00
|
|
|
if (cap == ENERGY_IC2) {
|
2018-06-16 19:15:15 +02:00
|
|
|
return ENERGY_IC2.cast(capIc2[conf.ordinal()-1]);
|
2018-06-15 16:15:06 +02:00
|
|
|
}
|
|
|
|
if (cap == ENERGY) {
|
2018-06-16 19:15:15 +02:00
|
|
|
return ENERGY.cast(capForge[conf.ordinal()-1]);
|
2018-06-15 16:15:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.getCapability(cap, side, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <T> boolean hasCapability(Capability<T> cap, EnumFacing side, BlockPos pos) {
|
2018-06-16 19:15:15 +02:00
|
|
|
if (sides.getConfigForFace(new BlockFace(pos, side))!=SideConfig.NONE) {
|
2018-06-15 16:15:06 +02:00
|
|
|
if (cap == ENERGY_IC2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (cap == ENERGY) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.hasCapability(cap, side, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void createMEnergy(MechEnergy e) {}
|
2018-03-24 17:45:25 +01:00
|
|
|
|
2018-06-15 16:15:06 +02:00
|
|
|
@Override
|
|
|
|
public double requestMEnergy(MechEnergy e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void insertMEnergy(double added) {
|
|
|
|
int available = (int) (Math.min(ConversionUtil.ifPerJoule() * bufferToWorld,
|
|
|
|
getMaxBuffer()/getEnergyConnections().size()));
|
|
|
|
if (available > 0 && wfToWorld.isAC()) {//The IC2 net will deal with DC by itself
|
|
|
|
bufferToWorld -= outputFE(available);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getInertia() {
|
|
|
|
return 50;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getMaxSpeed() {
|
|
|
|
return IWConfig.MechConversion.allowMBEU()?100:-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToNBT(NBTTagCompound out) {
|
|
|
|
out.setDouble(BUFFER_IN, bufferToMB);
|
|
|
|
out.setDouble(BUFFER_OUT, bufferToWorld);
|
|
|
|
out.setString(BUFFER_IN+WAVEFORM, wfToMB.serializeToString());
|
|
|
|
out.setString(BUFFER_OUT+WAVEFORM, wfToWorld.serializeToString());
|
2018-06-16 19:15:15 +02:00
|
|
|
out.setTag(SIDE_CONFIG, sides.toNBT(getEnergyConnections()));
|
2018-06-15 16:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound in) {
|
|
|
|
bufferToMB = in.getDouble(BUFFER_IN);
|
|
|
|
bufferToWorld = in.getDouble(BUFFER_OUT);
|
|
|
|
wfToMB = Waveform.fromString(in.getString(BUFFER_IN+WAVEFORM));
|
|
|
|
wfToWorld = Waveform.fromString(in.getString(BUFFER_OUT+WAVEFORM));
|
2018-06-16 19:15:15 +02:00
|
|
|
sides = new MBSideConfig(getEnergyConnections(), in.getTagList(SIDE_CONFIG, Constants.NBT.TAG_INT));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-16 23:08:38 +02:00
|
|
|
public int interact(@Nonnull EnumFacing side, @Nonnull Vec3i offset, @Nonnull EntityPlayer player,
|
|
|
|
@Nonnull EnumHand hand, @Nonnull ItemStack heldItem) {
|
2018-06-16 19:15:15 +02:00
|
|
|
if (Utils.isHammer(heldItem)) {
|
|
|
|
BlockFace s = new BlockFace(new BlockPos(offset), side);
|
|
|
|
if (sides.isValid(s)) {
|
|
|
|
if (!world.isRemote) {
|
|
|
|
sides.cycleSide(s);
|
|
|
|
world.markForUpdate(BlockPos.ORIGIN);
|
|
|
|
}
|
2018-06-16 23:08:38 +02:00
|
|
|
return 0b11;
|
2018-06-16 19:15:15 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-16 23:08:38 +02:00
|
|
|
return -1;
|
2018-06-16 19:15:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IBlockState getExtState(IBlockState in) {
|
|
|
|
in = super.getExtState(in);
|
|
|
|
if (in instanceof IExtendedBlockState) {
|
|
|
|
in = ((IExtendedBlockState) in).withProperty(IWProperties.MB_SIDES, sides);
|
|
|
|
}
|
|
|
|
return in;
|
2018-06-15 16:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract double getMaxBuffer();
|
|
|
|
|
|
|
|
protected abstract boolean has4Phases();
|
|
|
|
|
2018-06-16 19:15:15 +02:00
|
|
|
public abstract List<BlockFace> getEnergyConnections();
|
2018-06-15 16:15:06 +02:00
|
|
|
|
|
|
|
private double outputFE(int available) {
|
|
|
|
double extracted = 0;
|
2018-06-16 19:15:15 +02:00
|
|
|
for (BlockFace output : getEnergyConnections()) {
|
|
|
|
if (output.face==null||sides.getConfigForFace(output)!=OUTPUT)
|
2018-06-15 16:15:06 +02:00
|
|
|
continue;
|
2018-06-16 19:15:15 +02:00
|
|
|
BlockPos outTE = output.offset.offset(output.face);
|
2018-06-15 16:15:06 +02:00
|
|
|
TileEntity te = world.getTileEntity(outTE);
|
2018-06-16 19:15:15 +02:00
|
|
|
EnumFacing sideReal = world.transformedToReal(output.face).getOpposite();
|
2018-06-15 16:15:06 +02:00
|
|
|
if (te != null && te.hasCapability(CapabilityEnergy.ENERGY, sideReal)) {
|
|
|
|
IEnergyStorage energy = te.getCapability(CapabilityEnergy.ENERGY, sideReal);
|
|
|
|
if (energy != null && energy.canReceive()) {
|
|
|
|
int received = energy.receiveEnergy(available, false);
|
|
|
|
available -= received;
|
|
|
|
extracted += ConversionUtil.joulesPerIf() * received;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return extracted;
|
|
|
|
}
|
|
|
|
|
|
|
|
class IC2EHandlerMB extends IC2EnergyHandler {
|
2018-06-16 19:15:15 +02:00
|
|
|
private SideConfig type;
|
|
|
|
|
2018-02-28 21:03:26 +01:00
|
|
|
{
|
|
|
|
tier = 3;//TODO does this mean everything blows up?
|
|
|
|
}
|
|
|
|
|
2018-06-16 19:15:15 +02:00
|
|
|
IC2EHandlerMB(SideConfig type) {
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
2018-02-28 21:03:26 +01:00
|
|
|
@Override
|
|
|
|
public double injectEnergy(EnumFacing side, double amount, double voltage) {
|
|
|
|
double buffer = bufferToMB;
|
|
|
|
double input = amount * ConversionUtil.joulesPerEu();
|
|
|
|
if (!wfToMB.isDC()) {
|
|
|
|
buffer = 0;
|
|
|
|
}
|
|
|
|
input = Math.min(input, getMaxBuffer()-buffer);
|
|
|
|
buffer += input;
|
|
|
|
bufferToMB = buffer;
|
2018-03-24 17:45:25 +01:00
|
|
|
wfToMB = Waveform.forParameters(DC, get(has4Phases()), EXTERNAL);
|
2018-02-28 21:03:26 +01:00
|
|
|
return amount-ConversionUtil.euPerJoule()*input;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getOfferedEnergy() {
|
2018-06-16 19:15:15 +02:00
|
|
|
if (wfToWorld.isDC() && type==OUTPUT) {
|
2018-03-27 21:31:03 +02:00
|
|
|
return Math.min(ConversionUtil.euPerJoule()*bufferToWorld,
|
2018-06-16 19:15:15 +02:00
|
|
|
ConversionUtil.euPerJoule()*getMaxBuffer())/getEnergyConnections().size();
|
2018-06-15 09:35:14 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getDemandedEnergy() {
|
2018-06-16 19:15:15 +02:00
|
|
|
if (type==INPUT) {
|
2018-06-15 09:35:14 +02:00
|
|
|
return Math.min(ConversionUtil.euPerJoule()*(getMaxBuffer()-bufferToMB),
|
2018-06-16 19:15:15 +02:00
|
|
|
ConversionUtil.euPerJoule()*getMaxBuffer())/getEnergyConnections().size();
|
2018-02-28 21:03:26 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void drawEnergy(double amount) {
|
|
|
|
bufferToWorld -= ConversionUtil.joulesPerEu()*amount;
|
|
|
|
}
|
2018-06-16 19:15:15 +02:00
|
|
|
}
|
2018-02-28 21:03:26 +01:00
|
|
|
|
2018-06-15 16:15:06 +02:00
|
|
|
class EnergyStorageMMB implements IEnergyStorage {
|
2018-06-16 19:15:15 +02:00
|
|
|
private SideConfig type;
|
|
|
|
|
|
|
|
EnergyStorageMMB(SideConfig type) {
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
2018-02-28 21:03:26 +01:00
|
|
|
@Override
|
|
|
|
public int receiveEnergy(int maxReceive, boolean simulate) {
|
2018-06-16 19:15:15 +02:00
|
|
|
if (type!=INPUT) {
|
2018-06-15 09:35:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2018-02-28 21:03:26 +01:00
|
|
|
double buffer = bufferToMB;
|
2018-06-15 16:15:06 +02:00
|
|
|
double input = maxReceive * ConversionUtil.joulesPerIf();
|
2018-02-28 21:03:26 +01:00
|
|
|
if (!wfToMB.isAC()) {
|
|
|
|
buffer = 0;
|
|
|
|
}
|
2018-06-15 16:15:06 +02:00
|
|
|
input = Math.min(input, getMaxBuffer() - buffer);
|
2018-02-28 21:03:26 +01:00
|
|
|
buffer += input;
|
|
|
|
if (!simulate) {
|
|
|
|
bufferToMB = buffer;
|
2018-03-24 17:45:25 +01:00
|
|
|
wfToMB = Waveform.forParameters(Waveform.Type.AC, get(has4Phases()), EXTERNAL);
|
2018-02-28 21:03:26 +01:00
|
|
|
}
|
2018-06-15 16:15:06 +02:00
|
|
|
return (int) (ConversionUtil.ifPerJoule() * input);
|
2018-02-28 21:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int extractEnergy(int maxExtract, boolean simulate) {
|
2018-06-16 19:15:15 +02:00
|
|
|
if (!wfToWorld.isAC() || type!=OUTPUT) {
|
2018-02-28 21:03:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
double buffer = bufferToWorld;
|
2018-06-15 16:15:06 +02:00
|
|
|
double output = maxExtract * ConversionUtil.joulesPerIf();
|
2018-06-15 09:35:14 +02:00
|
|
|
output = Math.min(output, buffer);
|
|
|
|
buffer -= output;
|
2018-02-28 21:03:26 +01:00
|
|
|
if (!simulate) {
|
|
|
|
bufferToWorld = buffer;
|
|
|
|
}
|
2018-06-15 16:15:06 +02:00
|
|
|
return (int) (ConversionUtil.ifPerJoule() * output);
|
2018-02-28 21:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergyStored() {
|
2018-06-15 16:15:06 +02:00
|
|
|
return (int) (ConversionUtil.ifPerJoule() * (bufferToWorld + bufferToMB));
|
2018-02-28 21:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getMaxEnergyStored() {
|
2018-06-15 16:15:06 +02:00
|
|
|
return (int) (2 * ConversionUtil.ifPerJoule() * getMaxBuffer());
|
2018-02-28 21:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExtract() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canReceive() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|