mirror of
https://github.com/HbmMods/Hbm-s-Nuclear-Tech-GIT.git
synced 2026-01-25 10:32:49 +00:00
functional power net NT
This commit is contained in:
parent
acc48bf52c
commit
e4b9285eac
@ -11,12 +11,18 @@ public interface IPowerNet {
|
||||
|
||||
public void join(IPowerNet network);
|
||||
|
||||
public IPowerNet subscribe(IEnergyConductor conductor);
|
||||
public void unsubscribe(IEnergyConductor conductor);
|
||||
public IPowerNet joinLink(IEnergyConductor conductor);
|
||||
public void leaveLink(IEnergyConductor conductor);
|
||||
|
||||
public void subscribe(IEnergyConnector connector);
|
||||
public void unsubscribe(IEnergyConnector connector);
|
||||
public boolean isSubscribed(IEnergyConnector connector);
|
||||
|
||||
public void destroy();
|
||||
|
||||
public boolean isValid();
|
||||
|
||||
public List<IEnergyConductor> getSubscribers();
|
||||
public List<IEnergyConductor> getLinks();
|
||||
|
||||
public long transferPower(long power);
|
||||
}
|
||||
|
||||
@ -3,6 +3,8 @@ package api.hbm.energy;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@ -11,40 +13,97 @@ import java.util.List;
|
||||
public class PowerNet implements IPowerNet {
|
||||
|
||||
private boolean valid = true;
|
||||
private List<IEnergyConductor> subscribers = new ArrayList();
|
||||
private List<IEnergyConductor> links = new ArrayList();
|
||||
private List<IEnergyConnector> subscribers = new ArrayList();
|
||||
|
||||
@Override
|
||||
public void join(IPowerNet network) { }
|
||||
|
||||
@Override
|
||||
public IPowerNet subscribe(IEnergyConductor conductor) {
|
||||
public IPowerNet joinLink(IEnergyConductor conductor) {
|
||||
|
||||
if(conductor.getPowerNet() != null)
|
||||
conductor.getPowerNet().unsubscribe(conductor);
|
||||
conductor.getPowerNet().leaveLink(conductor);
|
||||
|
||||
conductor.setPowerNet(this);
|
||||
this.getSubscribers().add(conductor);
|
||||
this.getLinks().add(conductor);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unsubscribe(IEnergyConductor conductor) {
|
||||
public void leaveLink(IEnergyConductor conductor) {
|
||||
conductor.setPowerNet(null);
|
||||
this.getSubscribers().remove(conductor);
|
||||
this.getLinks().remove(conductor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IEnergyConductor> getSubscribers() {
|
||||
return null;
|
||||
public void subscribe(IEnergyConnector connector) {
|
||||
this.subscribers.add(connector);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unsubscribe(IEnergyConnector connector) {
|
||||
this.subscribers.remove(connector);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSubscribed(IEnergyConnector connector) {
|
||||
return this.subscribers.contains(connector);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IEnergyConductor> getLinks() {
|
||||
return this.links;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
this.valid = false;
|
||||
|
||||
for(IEnergyConductor link : this.links) {
|
||||
link.setPowerNet(null);
|
||||
}
|
||||
|
||||
this.links.clear();
|
||||
this.subscribers.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return this.valid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long transferPower(long power) {
|
||||
|
||||
this.subscribers.removeIf(x ->
|
||||
x == null || !(x instanceof TileEntity) || ((TileEntity)x).isInvalid()
|
||||
);
|
||||
|
||||
if(this.subscribers.isEmpty())
|
||||
return power;
|
||||
|
||||
List<Long> weight = new ArrayList();
|
||||
long totalReq = 0;
|
||||
|
||||
for(IEnergyConnector con : this.subscribers) {
|
||||
long req = Math.max(con.getMaxPower() - con.getPower(), 0);
|
||||
weight.add(req);
|
||||
totalReq += req;
|
||||
}
|
||||
|
||||
long totalGiven = 0;
|
||||
|
||||
for(int i = 0; i < this.subscribers.size(); i++) {
|
||||
IEnergyConnector con = this.subscribers.get(i);
|
||||
long req = weight.get(i);
|
||||
double fraction = (double)req / (double)totalReq;
|
||||
|
||||
long given = (int) Math.floor(fraction * power);
|
||||
|
||||
totalGiven += (given - con.transferPower(given));
|
||||
}
|
||||
|
||||
return power - totalGiven;
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,6 +47,7 @@ public class ModBlocks {
|
||||
public static Block test_missile;
|
||||
public static Block test_core;
|
||||
public static Block test_charge;
|
||||
public static Block test_conductor;
|
||||
|
||||
public static Block ore_uranium;
|
||||
public static Block ore_uranium_scorched;
|
||||
@ -1148,6 +1149,7 @@ public class ModBlocks {
|
||||
test_missile = new TestMissile(Material.iron).setBlockName("test_missile").setCreativeTab(null).setHardness(2.5F).setResistance(10.0F).setBlockTextureName(RefStrings.MODID + ":test_missile");
|
||||
test_core = new TestCore(Material.iron).setBlockName("test_core").setCreativeTab(null).setHardness(2.5F).setResistance(10.0F).setBlockTextureName(RefStrings.MODID + ":test_core");
|
||||
test_charge = new TestCharge(Material.iron).setBlockName("test_charge").setCreativeTab(null).setHardness(2.5F).setResistance(10.0F);
|
||||
test_conductor = new TestConductor(Material.iron).setBlockName("test_conductor").setCreativeTab(null).setHardness(2.5F).setResistance(10.0F).setBlockTextureName(RefStrings.MODID + ":test_conductor");
|
||||
|
||||
ore_uranium = new BlockOutgas(Material.rock, true, 5, false).setBlockName("ore_uranium").setCreativeTab(MainRegistry.blockTab).setHardness(5.0F).setResistance(10.0F).setBlockTextureName(RefStrings.MODID + ":ore_uranium");
|
||||
ore_uranium_scorched = new BlockOutgas(Material.rock, true, 5, false).setBlockName("ore_uranium_scorched").setCreativeTab(MainRegistry.blockTab).setHardness(5.0F).setResistance(10.0F).setBlockTextureName(RefStrings.MODID + ":ore_uranium_scorched");
|
||||
@ -2060,6 +2062,7 @@ public class ModBlocks {
|
||||
GameRegistry.registerBlock(test_missile, test_missile.getUnlocalizedName());
|
||||
GameRegistry.registerBlock(test_core, test_core.getUnlocalizedName());
|
||||
GameRegistry.registerBlock(test_charge, test_charge.getUnlocalizedName());
|
||||
GameRegistry.registerBlock(test_conductor, test_conductor.getUnlocalizedName());
|
||||
|
||||
//Ores
|
||||
GameRegistry.registerBlock(ore_uranium, ore_uranium.getUnlocalizedName());
|
||||
|
||||
26
src/main/java/com/hbm/blocks/test/TestConductor.java
Normal file
26
src/main/java/com/hbm/blocks/test/TestConductor.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.hbm.blocks.test;
|
||||
|
||||
import com.hbm.interfaces.Untested;
|
||||
import com.hbm.tileentity.network.TileEntityCableBaseNT;
|
||||
|
||||
import net.minecraft.block.BlockContainer;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
/**
|
||||
* Powered by satan energy!
|
||||
* @author hbm
|
||||
*/
|
||||
@Untested
|
||||
public class TestConductor extends BlockContainer {
|
||||
|
||||
public TestConductor(Material p_i45386_1_) {
|
||||
super(p_i45386_1_);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
|
||||
return new TileEntityCableBaseNT();
|
||||
}
|
||||
}
|
||||
@ -74,6 +74,7 @@ import com.hbm.tileentity.deco.*;
|
||||
import com.hbm.tileentity.machine.*;
|
||||
import com.hbm.tileentity.machine.TileEntityMachineReactorLarge.ReactorFuelType;
|
||||
import com.hbm.tileentity.machine.rbmk.*;
|
||||
import com.hbm.tileentity.network.TileEntityCableBaseNT;
|
||||
import com.hbm.tileentity.turret.*;
|
||||
import com.hbm.world.feature.SchistStratum;
|
||||
import com.hbm.world.generator.CellularDungeonFactory;
|
||||
@ -480,6 +481,7 @@ public class MainRegistry {
|
||||
GameRegistry.registerTileEntity(TileEntityStorageDrum.class, "tileentity_waste_storage_drum");
|
||||
GameRegistry.registerTileEntity(TileEntityDeaerator.class, "tileentity_deaerator");
|
||||
GameRegistry.registerTileEntity(TileEntityChungus.class, "tileentity_chungus");
|
||||
GameRegistry.registerTileEntity(TileEntityCableBaseNT.class, "tileentity_ohgod");
|
||||
|
||||
GameRegistry.registerTileEntity(TileEntityRBMKRod.class, "tileentity_rbmk_rod");
|
||||
GameRegistry.registerTileEntity(TileEntityRBMKRodReaSim.class, "tileentity_rbmk_rod_reasim");
|
||||
|
||||
@ -10,11 +10,15 @@ import com.hbm.lib.Library;
|
||||
import com.hbm.tileentity.TileEntityMachineBase;
|
||||
|
||||
import api.hbm.energy.IBatteryItem;
|
||||
import api.hbm.energy.IEnergyConductor;
|
||||
import api.hbm.energy.IEnergyConnector;
|
||||
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;
|
||||
|
||||
public class TileEntityMachineBattery extends TileEntityMachineBase implements IConsumer, ISource {
|
||||
public class TileEntityMachineBattery extends TileEntityMachineBase implements IConsumer, ISource, IEnergyConnector {
|
||||
|
||||
public long power = 0;
|
||||
public long maxPower = 1000000;
|
||||
@ -162,11 +166,32 @@ public class TileEntityMachineBattery extends TileEntityMachineBase implements I
|
||||
public void updateEntity() {
|
||||
|
||||
if(worldObj.getBlock(xCoord, yCoord, zCoord) instanceof MachineBattery && !worldObj.isRemote) {
|
||||
|
||||
this.maxPower = ((MachineBattery)worldObj.getBlock(xCoord, yCoord, zCoord)).maxPower;
|
||||
|
||||
short mode = (short) this.getRelevantMode();
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
for(ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
|
||||
TileEntity te = worldObj.getTileEntity(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);
|
||||
|
||||
if(te instanceof IEnergyConductor) {
|
||||
IEnergyConductor con = (IEnergyConductor) te;
|
||||
|
||||
if(con.getPowerNet() != null && !con.getPowerNet().isSubscribed(this))
|
||||
con.getPowerNet().subscribe(this);
|
||||
}
|
||||
|
||||
if(mode == 1 || mode == 2) {
|
||||
if(te instanceof IEnergyConnector) {
|
||||
IEnergyConnector con = (IEnergyConnector) te;
|
||||
this.power = con.transferPower(this.power);
|
||||
}
|
||||
}
|
||||
}
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
this.maxPower = ((MachineBattery)worldObj.getBlock(xCoord, yCoord, zCoord)).maxPower;
|
||||
|
||||
if(mode == 1 || mode == 2)
|
||||
{
|
||||
age++;
|
||||
@ -273,5 +298,28 @@ public class TileEntityMachineBattery extends TileEntityMachineBase implements I
|
||||
public void clearList() {
|
||||
this.list.clear();
|
||||
}
|
||||
|
||||
/*
|
||||
* SATAN - TECH
|
||||
*/
|
||||
@Override
|
||||
public long transferPower(long power) {
|
||||
|
||||
this.power += power;
|
||||
|
||||
if(this.power > this.maxPower) {
|
||||
|
||||
long overshoot = this.power - this.maxPower;
|
||||
this.power = this.maxPower;
|
||||
return overshoot;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canConnect(ForgeDirection dir) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,23 +1,26 @@
|
||||
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 {
|
||||
public class TileEntityCableBaseNT extends TileEntity implements IEnergyConductor {
|
||||
|
||||
private IPowerNet network;
|
||||
|
||||
@Override
|
||||
public void updateEntity() {
|
||||
|
||||
if(!worldObj.isRemote) {
|
||||
|
||||
//we got here either because the net doesn't exist or because it's not valid, so that's safe to assume
|
||||
this.setPowerNet(null);
|
||||
|
||||
for(ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
|
||||
TileEntity te = worldObj.getTileEntity(xCoord, yCoord, zCoord);
|
||||
TileEntity te = worldObj.getTileEntity(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);
|
||||
|
||||
if(te instanceof IEnergyConductor) {
|
||||
|
||||
@ -25,24 +28,36 @@ public class TileEntityCableBaseTN extends TileEntity implements IEnergyConducto
|
||||
|
||||
if(this.getPowerNet() == null) {
|
||||
this.setPowerNet(conductor.getPowerNet());
|
||||
} else if(conductor.getPowerNet() != null) {
|
||||
}
|
||||
|
||||
if(conductor.getPowerNet() != null) {
|
||||
conductor.getPowerNet().join(this.getPowerNet());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(this.getPowerNet() == null) {
|
||||
this.setPowerNet(new PowerNet().subscribe(this));
|
||||
this.setPowerNet(new PowerNet().joinLink(this));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate() {
|
||||
super.invalidate();
|
||||
|
||||
//TODO: find out why sometimes the power net doesn't dissolve when it definitely should
|
||||
if(this.network != null) {
|
||||
this.network.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
return (this.network == null || !this.network.isValid()) && !this.isInvalid();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -62,17 +77,20 @@ public class TileEntityCableBaseTN extends TileEntity implements IEnergyConducto
|
||||
|
||||
@Override
|
||||
public void setPowerNet(IPowerNet network) {
|
||||
|
||||
this.network = network;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long transferPower(long power) {
|
||||
return 0;
|
||||
|
||||
if(this.network == null)
|
||||
return power;
|
||||
|
||||
return this.network.transferPower(power);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPowerNet getPowerNet() {
|
||||
return null;
|
||||
return this.network;
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
BIN
src/main/resources/assets/hbm/textures/blocks/test_conductor.png
Normal file
BIN
src/main/resources/assets/hbm/textures/blocks/test_conductor.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 193 B |
Loading…
x
Reference in New Issue
Block a user