new energy API and power net

This commit is contained in:
Boblet 2021-06-04 15:21:33 +02:00
parent 6d211ee8db
commit bb8a42e0b7
8 changed files with 211 additions and 10 deletions

View File

@ -0,0 +1,12 @@
package api.hbm.energy;
/**
* For compatible cables with no buffer, using the IPowertNet. You can make your own cables with IEnergyConnector as well, but they won't join their power network.
* @author hbm
*/
public interface IEnergyConductor extends IEnergyConnector {
public IPowerNet getPowerNet();
public void setPowerNet(IPowerNet network);
}

View File

@ -0,0 +1,36 @@
package api.hbm.energy;
import net.minecraftforge.common.util.ForgeDirection;
/**
* For anything that connects to power and can be transferred power to, the bottom-level interface.
* @author hbm
*/
public interface IEnergyConnector {
/**
* Returns the amount of power that was added
* @param power
* @return
*/
public long transferPower(long power);
/**
* Whether the given side can be connected to
* @param dir
* @return
*/
public boolean canConnect(ForgeDirection dir);
/**
* The current power of either the machine or an entire network
* @return
*/
public long getPower();
/**
* The capacity of either the machine or an entire network
* @return
*/
public long getMaxPower();
}

View File

@ -1,5 +0,0 @@
package api.hbm.energy;
public interface IEnergyConsumer {
/// WIP ///
}

View File

@ -1,5 +0,0 @@
package api.hbm.energy;
public interface IEnergySource {
/// WIP ///
}

View File

@ -0,0 +1,13 @@
package api.hbm.energy;
/**
* For machines and things that have an energy buffer and are affected by EMPs
* @author hbm
*/
public interface IEnergyUser extends IEnergyConnector {
/**
* Not to be used for actual energy transfer, rather special external things like EMPs
*/
public void setPower();
}

View File

@ -0,0 +1,22 @@
package api.hbm.energy;
import java.util.List;
/**
* Not mandatory to use, but making your cables IPowerNet-compliant will allow them to connect to NTM cables.
* Cables will still work without it as long as they implement IEnergyConductor (or even IEnergyConnector) + self-built network code
* @author hbm
*/
public interface IPowerNet {
public void join(IPowerNet network);
public IPowerNet subscribe(IEnergyConductor conductor);
public void unsubscribe(IEnergyConductor conductor);
public void destroy();
public boolean isValid();
public List<IEnergyConductor> getSubscribers();
}

View File

@ -0,0 +1,50 @@
package api.hbm.energy;
import java.util.ArrayList;
import java.util.List;
/**
* Basic IPowerNet implementation. The behavior of this demo might change inbetween releases, but the API remains the same.
* For more consistency please implement your own IPowerNet.
* @author hbm
*/
public class PowerNet implements IPowerNet {
private boolean valid = true;
private List<IEnergyConductor> subscribers = new ArrayList();
@Override
public void join(IPowerNet network) { }
@Override
public IPowerNet subscribe(IEnergyConductor conductor) {
if(conductor.getPowerNet() != null)
conductor.getPowerNet().unsubscribe(conductor);
conductor.setPowerNet(this);
this.getSubscribers().add(conductor);
return this;
}
@Override
public void unsubscribe(IEnergyConductor conductor) {
conductor.setPowerNet(null);
this.getSubscribers().remove(conductor);
}
@Override
public List<IEnergyConductor> getSubscribers() {
return null;
}
@Override
public void destroy() {
this.valid = false;
}
@Override
public boolean isValid() {
return this.valid;
}
}

View File

@ -0,0 +1,78 @@
package com.hbm.tileentity.network;
import api.hbm.energy.IEnergyConductor;
import api.hbm.energy.IEnergyConnector;
import api.hbm.energy.IPowerNet;
import api.hbm.energy.PowerNet;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
public class TileEntityCableBaseTN extends TileEntity implements IEnergyConductor {
private IPowerNet network;
public void updateEntity() {
if(!worldObj.isRemote) {
for(ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
TileEntity te = worldObj.getTileEntity(xCoord, yCoord, zCoord);
if(te instanceof IEnergyConductor) {
IEnergyConductor conductor = (IEnergyConductor) te;
if(this.getPowerNet() == null) {
this.setPowerNet(conductor.getPowerNet());
} else if(conductor.getPowerNet() != null) {
conductor.getPowerNet().join(this.getPowerNet());
}
}
}
if(this.getPowerNet() == null) {
this.setPowerNet(new PowerNet().subscribe(this));
}
}
}
/**
* Only update until a power net is formed, in >99% of the cases it should be the first tick. Everything else is handled by neighbors and the net itself.
*/
@Override
public boolean canUpdate() {
return network == null;
}
@Override
public boolean canConnect(ForgeDirection dir) {
return true;
}
@Override
public long getPower() {
return 0;
}
@Override
public long getMaxPower() {
return 0;
}
@Override
public void setPowerNet(IPowerNet network) {
}
@Override
public long transferPower(long power) {
return 0;
}
@Override
public IPowerNet getPowerNet() {
return null;
}
}