1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 11:45:51 +00:00

Refactored Network classes

This commit is contained in:
TheBusyBiscuit 2019-08-30 23:55:40 +02:00
parent 3400746c34
commit fb0cd04b81
6 changed files with 64 additions and 58 deletions

View File

@ -29,7 +29,7 @@ import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.Slimefun; import me.mrCookieSlime.Slimefun.api.Slimefun;
import me.mrCookieSlime.Slimefun.api.energy.ChargableBlock; import me.mrCookieSlime.Slimefun.api.energy.ChargableBlock;
import me.mrCookieSlime.Slimefun.api.energy.EnergyNet; import me.mrCookieSlime.Slimefun.api.energy.EnergyNet;
import me.mrCookieSlime.Slimefun.api.energy.EnergyNet.NetworkComponent; import me.mrCookieSlime.Slimefun.api.energy.EnergyNetComponent;
import me.mrCookieSlime.Slimefun.api.energy.EnergyTicker; import me.mrCookieSlime.Slimefun.api.energy.EnergyTicker;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
@ -405,7 +405,7 @@ public class SlimefunItem {
} }
else if (h instanceof EnergyTicker) { else if (h instanceof EnergyTicker) {
this.energyTicker = (EnergyTicker) h; this.energyTicker = (EnergyTicker) h;
EnergyNet.registerComponent(getID(), NetworkComponent.SOURCE); EnergyNet.registerComponent(getID(), EnergyNetComponent.SOURCE);
} }
} }
} }
@ -480,7 +480,7 @@ public class SlimefunItem {
public void registerChargeableBlock(boolean slimefun, int capacity) { public void registerChargeableBlock(boolean slimefun, int capacity) {
this.register(slimefun); this.register(slimefun);
ChargableBlock.registerChargableBlock(id, capacity, true); ChargableBlock.registerChargableBlock(id, capacity, true);
EnergyNet.registerComponent(id, NetworkComponent.CONSUMER); EnergyNet.registerComponent(id, EnergyNetComponent.CONSUMER);
} }
public void registerUnrechargeableBlock(boolean slimefun, int capacity) { public void registerUnrechargeableBlock(boolean slimefun, int capacity) {
@ -495,12 +495,12 @@ public class SlimefunItem {
public void registerEnergyDistributor(boolean slimefun) { public void registerEnergyDistributor(boolean slimefun) {
this.register(slimefun); this.register(slimefun);
EnergyNet.registerComponent(id, NetworkComponent.DISTRIBUTOR); EnergyNet.registerComponent(id, EnergyNetComponent.DISTRIBUTOR);
} }
public void registerDistibutingCapacitor(boolean slimefun, final int capacity) { public void registerDistibutingCapacitor(boolean slimefun, final int capacity) {
this.register(slimefun); this.register(slimefun);
EnergyNet.registerComponent(id, NetworkComponent.DISTRIBUTOR); EnergyNet.registerComponent(id, EnergyNetComponent.DISTRIBUTOR);
ChargableBlock.registerCapacitor(id, capacity); ChargableBlock.registerCapacitor(id, capacity);
} }

View File

@ -13,44 +13,38 @@ import me.mrCookieSlime.Slimefun.SlimefunStartup;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.BlockStorage; import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.network.Network; import me.mrCookieSlime.Slimefun.api.network.Network;
import me.mrCookieSlime.Slimefun.api.network.NetworkComponent;
import me.mrCookieSlime.Slimefun.holograms.EnergyHologram; import me.mrCookieSlime.Slimefun.holograms.EnergyHologram;
public class EnergyNet extends Network { public class EnergyNet extends Network {
public static enum NetworkComponent {
SOURCE,
DISTRIBUTOR,
CONSUMER,
NONE;
}
private static final int RANGE = 6; private static final int RANGE = 6;
public static Set<String> machinesInput = new HashSet<>(); public static Set<String> machinesInput = new HashSet<>();
public static Set<String> machinesStorage = new HashSet<>(); public static Set<String> machinesStorage = new HashSet<>();
public static Set<String> machinesOutput = new HashSet<>(); public static Set<String> machinesOutput = new HashSet<>();
public static NetworkComponent getComponent(Block b) { public static EnergyNetComponent getComponent(Block b) {
return getComponent(b.getLocation()); return getComponent(b.getLocation());
} }
public static NetworkComponent getComponent(String id) { public static EnergyNetComponent getComponent(String id) {
if (machinesInput.contains(id)) return NetworkComponent.SOURCE; if (machinesInput.contains(id)) return EnergyNetComponent.SOURCE;
if (machinesStorage.contains(id)) return NetworkComponent.DISTRIBUTOR; if (machinesStorage.contains(id)) return EnergyNetComponent.DISTRIBUTOR;
if (machinesOutput.contains(id)) return NetworkComponent.CONSUMER; if (machinesOutput.contains(id)) return EnergyNetComponent.CONSUMER;
return NetworkComponent.NONE; return EnergyNetComponent.NONE;
} }
public static NetworkComponent getComponent(Location l) { public static EnergyNetComponent getComponent(Location l) {
if (!BlockStorage.hasBlockInfo(l)) return NetworkComponent.NONE; if (!BlockStorage.hasBlockInfo(l)) return EnergyNetComponent.NONE;
String id = BlockStorage.checkID(l); String id = BlockStorage.checkID(l);
if (machinesInput.contains(id)) return NetworkComponent.SOURCE; if (machinesInput.contains(id)) return EnergyNetComponent.SOURCE;
if (machinesStorage.contains(id)) return NetworkComponent.DISTRIBUTOR; if (machinesStorage.contains(id)) return EnergyNetComponent.DISTRIBUTOR;
if (machinesOutput.contains(id)) return NetworkComponent.CONSUMER; if (machinesOutput.contains(id)) return EnergyNetComponent.CONSUMER;
return NetworkComponent.NONE; return EnergyNetComponent.NONE;
} }
public static void registerComponent(String id, NetworkComponent component) { public static void registerComponent(String id, EnergyNetComponent component) {
switch (component) { switch (component) {
case CONSUMER: case CONSUMER:
machinesOutput.add(id); machinesOutput.add(id);
@ -91,24 +85,25 @@ public class EnergyNet extends Network {
return RANGE; return RANGE;
} }
public Network.Component classifyLocation(Location l) { public NetworkComponent classifyLocation(Location l) {
if (regulator.equals(l)) return Network.Component.REGULATOR; if (regulator.equals(l)) return NetworkComponent.REGULATOR;
switch (getComponent(l)) { switch (getComponent(l)) {
case DISTRIBUTOR: case DISTRIBUTOR:
return Network.Component.CONNECTOR; return NetworkComponent.CONNECTOR;
case CONSUMER: case CONSUMER:
case SOURCE: case SOURCE:
return Network.Component.TERMINUS; return NetworkComponent.TERMINUS;
default: default:
return null; return null;
} }
} }
public void locationClassificationChange(Location l, Network.Component from, Network.Component to) { public void locationClassificationChange(Location l, NetworkComponent from, NetworkComponent to) {
if (from == Network.Component.TERMINUS) { if (from == NetworkComponent.TERMINUS) {
input.remove(l); input.remove(l);
output.remove(l); output.remove(l);
} }
switch (getComponent(l)) { switch (getComponent(l)) {
case DISTRIBUTOR: case DISTRIBUTOR:
if (ChargableBlock.isCapacitor(l)) storage.add(l); if (ChargableBlock.isCapacitor(l)) storage.add(l);

View File

@ -0,0 +1,10 @@
package me.mrCookieSlime.Slimefun.api.energy;
public enum EnergyNetComponent {
SOURCE,
DISTRIBUTOR,
CONSUMER,
NONE;
}

View File

@ -30,6 +30,7 @@ import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import me.mrCookieSlime.Slimefun.api.inventory.UniversalBlockMenu; import me.mrCookieSlime.Slimefun.api.inventory.UniversalBlockMenu;
import me.mrCookieSlime.Slimefun.api.network.Network; import me.mrCookieSlime.Slimefun.api.network.Network;
import me.mrCookieSlime.Slimefun.api.network.NetworkComponent;
import me.mrCookieSlime.Slimefun.holograms.CargoHologram; import me.mrCookieSlime.Slimefun.holograms.CargoHologram;
public class CargoNet extends Network { public class CargoNet extends Network {
@ -86,28 +87,28 @@ public class CargoNet extends Network {
return RANGE; return RANGE;
} }
public Network.Component classifyLocation(Location l) { public NetworkComponent classifyLocation(Location l) {
String id = BlockStorage.checkID(l); String id = BlockStorage.checkID(l);
if (id == null) return null; if (id == null) return null;
switch(id) { switch(id) {
case "CARGO_MANAGER": case "CARGO_MANAGER":
return Component.REGULATOR; return NetworkComponent.REGULATOR;
case "CARGO_NODE": case "CARGO_NODE":
return Component.CONNECTOR; return NetworkComponent.CONNECTOR;
case "CARGO_NODE_INPUT": case "CARGO_NODE_INPUT":
case "CARGO_NODE_OUTPUT": case "CARGO_NODE_OUTPUT":
case "CARGO_NODE_OUTPUT_ADVANCED": case "CARGO_NODE_OUTPUT_ADVANCED":
case "CT_IMPORT_BUS": case "CT_IMPORT_BUS":
case "CT_EXPORT_BUS": case "CT_EXPORT_BUS":
case "CHEST_TERMINAL": case "CHEST_TERMINAL":
return Component.TERMINUS; return NetworkComponent.TERMINUS;
default: default:
return null; return null;
} }
} }
public void locationClassificationChange(Location l, Component from, Component to) { public void locationClassificationChange(Location l, NetworkComponent from, NetworkComponent to) {
if (from == Component.TERMINUS) { if (from == NetworkComponent.TERMINUS) {
inputNodes.remove(l); inputNodes.remove(l);
outputNodes.remove(l); outputNodes.remove(l);
advancedOutputNodes.remove(l); advancedOutputNodes.remove(l);
@ -115,7 +116,7 @@ public class CargoNet extends Network {
imports.remove(l); imports.remove(l);
exports.remove(l); exports.remove(l);
} }
if (to == Component.TERMINUS) { if (to == NetworkComponent.TERMINUS) {
switch(BlockStorage.checkID(l)) { switch(BlockStorage.checkID(l)) {
case "CARGO_NODE_INPUT": case "CARGO_NODE_INPUT":
inputNodes.add(l); inputNodes.add(l);

View File

@ -49,16 +49,9 @@ public abstract class Network {
} }
} }
public static enum Component {
CONNECTOR,
REGULATOR,
TERMINUS;
}
public abstract int getRange(); public abstract int getRange();
public abstract Component classifyLocation(Location l); public abstract NetworkComponent classifyLocation(Location l);
public abstract void locationClassificationChange(Location l, Component from, Component to); public abstract void locationClassificationChange(Location l, NetworkComponent from, NetworkComponent to);
protected Location regulator; protected Location regulator;
private Queue<Location> nodeQueue = new ArrayDeque<>(); private Queue<Location> nodeQueue = new ArrayDeque<>();
@ -94,15 +87,15 @@ public abstract class Network {
return connectedLocations.contains(l); return connectedLocations.contains(l);
} }
private Component getCurrentClassification(Location l) { private NetworkComponent getCurrentClassification(Location l) {
if(regulatorNodes.contains(l)) { if(regulatorNodes.contains(l)) {
return Component.REGULATOR; return NetworkComponent.REGULATOR;
} }
else if(connectorNodes.contains(l)) { else if(connectorNodes.contains(l)) {
return Component.CONNECTOR; return NetworkComponent.CONNECTOR;
} }
else if(terminusNodes.contains(l)) { else if(terminusNodes.contains(l)) {
return Component.TERMINUS; return NetworkComponent.TERMINUS;
} }
return null; return null;
} }
@ -111,28 +104,28 @@ public abstract class Network {
int steps = 0; int steps = 0;
while (nodeQueue.peek() != null) { while (nodeQueue.peek() != null) {
Location l = nodeQueue.poll(); Location l = nodeQueue.poll();
Component currentAssignment = getCurrentClassification(l); NetworkComponent currentAssignment = getCurrentClassification(l);
Component classification = classifyLocation(l); NetworkComponent classification = classifyLocation(l);
if (classification != currentAssignment) { if (classification != currentAssignment) {
if (currentAssignment == Component.REGULATOR || currentAssignment == Component.CONNECTOR) { if (currentAssignment == NetworkComponent.REGULATOR || currentAssignment == NetworkComponent.CONNECTOR) {
// Requires a complete rebuild of the network, so we just throw the current one away. // Requires a complete rebuild of the network, so we just throw the current one away.
unregisterNetwork(this); unregisterNetwork(this);
return; return;
} }
else if (currentAssignment == Component.TERMINUS) { else if (currentAssignment == NetworkComponent.TERMINUS) {
terminusNodes.remove(l); terminusNodes.remove(l);
} }
if (classification == Component.REGULATOR) { if (classification == NetworkComponent.REGULATOR) {
regulatorNodes.add(l); regulatorNodes.add(l);
discoverNeighbors(l); discoverNeighbors(l);
} }
else if(classification == Component.CONNECTOR) { else if(classification == NetworkComponent.CONNECTOR) {
connectorNodes.add(l); connectorNodes.add(l);
discoverNeighbors(l); discoverNeighbors(l);
} }
else if(classification == Component.TERMINUS) { else if(classification == NetworkComponent.TERMINUS) {
terminusNodes.add(l); terminusNodes.add(l);
} }

View File

@ -0,0 +1,7 @@
package me.mrCookieSlime.Slimefun.api.network;
public enum NetworkComponent {
CONNECTOR,
REGULATOR,
TERMINUS;
}