1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35: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.energy.ChargableBlock;
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.inventory.BlockMenu;
@ -405,7 +405,7 @@ public class SlimefunItem {
}
else if (h instanceof EnergyTicker) {
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) {
this.register(slimefun);
ChargableBlock.registerChargableBlock(id, capacity, true);
EnergyNet.registerComponent(id, NetworkComponent.CONSUMER);
EnergyNet.registerComponent(id, EnergyNetComponent.CONSUMER);
}
public void registerUnrechargeableBlock(boolean slimefun, int capacity) {
@ -495,12 +495,12 @@ public class SlimefunItem {
public void registerEnergyDistributor(boolean slimefun) {
this.register(slimefun);
EnergyNet.registerComponent(id, NetworkComponent.DISTRIBUTOR);
EnergyNet.registerComponent(id, EnergyNetComponent.DISTRIBUTOR);
}
public void registerDistibutingCapacitor(boolean slimefun, final int capacity) {
this.register(slimefun);
EnergyNet.registerComponent(id, NetworkComponent.DISTRIBUTOR);
EnergyNet.registerComponent(id, EnergyNetComponent.DISTRIBUTOR);
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.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.network.Network;
import me.mrCookieSlime.Slimefun.api.network.NetworkComponent;
import me.mrCookieSlime.Slimefun.holograms.EnergyHologram;
public class EnergyNet extends Network {
public static enum NetworkComponent {
SOURCE,
DISTRIBUTOR,
CONSUMER,
NONE;
}
private static final int RANGE = 6;
public static Set<String> machinesInput = new HashSet<>();
public static Set<String> machinesStorage = 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());
}
public static NetworkComponent getComponent(String id) {
if (machinesInput.contains(id)) return NetworkComponent.SOURCE;
if (machinesStorage.contains(id)) return NetworkComponent.DISTRIBUTOR;
if (machinesOutput.contains(id)) return NetworkComponent.CONSUMER;
return NetworkComponent.NONE;
public static EnergyNetComponent getComponent(String id) {
if (machinesInput.contains(id)) return EnergyNetComponent.SOURCE;
if (machinesStorage.contains(id)) return EnergyNetComponent.DISTRIBUTOR;
if (machinesOutput.contains(id)) return EnergyNetComponent.CONSUMER;
return EnergyNetComponent.NONE;
}
public static NetworkComponent getComponent(Location l) {
if (!BlockStorage.hasBlockInfo(l)) return NetworkComponent.NONE;
public static EnergyNetComponent getComponent(Location l) {
if (!BlockStorage.hasBlockInfo(l)) return EnergyNetComponent.NONE;
String id = BlockStorage.checkID(l);
if (machinesInput.contains(id)) return NetworkComponent.SOURCE;
if (machinesStorage.contains(id)) return NetworkComponent.DISTRIBUTOR;
if (machinesOutput.contains(id)) return NetworkComponent.CONSUMER;
return NetworkComponent.NONE;
if (machinesInput.contains(id)) return EnergyNetComponent.SOURCE;
if (machinesStorage.contains(id)) return EnergyNetComponent.DISTRIBUTOR;
if (machinesOutput.contains(id)) return EnergyNetComponent.CONSUMER;
return EnergyNetComponent.NONE;
}
public static void registerComponent(String id, NetworkComponent component) {
public static void registerComponent(String id, EnergyNetComponent component) {
switch (component) {
case CONSUMER:
machinesOutput.add(id);
@ -91,24 +85,25 @@ public class EnergyNet extends Network {
return RANGE;
}
public Network.Component classifyLocation(Location l) {
if (regulator.equals(l)) return Network.Component.REGULATOR;
public NetworkComponent classifyLocation(Location l) {
if (regulator.equals(l)) return NetworkComponent.REGULATOR;
switch (getComponent(l)) {
case DISTRIBUTOR:
return Network.Component.CONNECTOR;
return NetworkComponent.CONNECTOR;
case CONSUMER:
case SOURCE:
return Network.Component.TERMINUS;
return NetworkComponent.TERMINUS;
default:
return null;
}
}
public void locationClassificationChange(Location l, Network.Component from, Network.Component to) {
if (from == Network.Component.TERMINUS) {
public void locationClassificationChange(Location l, NetworkComponent from, NetworkComponent to) {
if (from == NetworkComponent.TERMINUS) {
input.remove(l);
output.remove(l);
}
switch (getComponent(l)) {
case DISTRIBUTOR:
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.UniversalBlockMenu;
import me.mrCookieSlime.Slimefun.api.network.Network;
import me.mrCookieSlime.Slimefun.api.network.NetworkComponent;
import me.mrCookieSlime.Slimefun.holograms.CargoHologram;
public class CargoNet extends Network {
@ -86,28 +87,28 @@ public class CargoNet extends Network {
return RANGE;
}
public Network.Component classifyLocation(Location l) {
public NetworkComponent classifyLocation(Location l) {
String id = BlockStorage.checkID(l);
if (id == null) return null;
switch(id) {
case "CARGO_MANAGER":
return Component.REGULATOR;
return NetworkComponent.REGULATOR;
case "CARGO_NODE":
return Component.CONNECTOR;
return NetworkComponent.CONNECTOR;
case "CARGO_NODE_INPUT":
case "CARGO_NODE_OUTPUT":
case "CARGO_NODE_OUTPUT_ADVANCED":
case "CT_IMPORT_BUS":
case "CT_EXPORT_BUS":
case "CHEST_TERMINAL":
return Component.TERMINUS;
return NetworkComponent.TERMINUS;
default:
return null;
}
}
public void locationClassificationChange(Location l, Component from, Component to) {
if (from == Component.TERMINUS) {
public void locationClassificationChange(Location l, NetworkComponent from, NetworkComponent to) {
if (from == NetworkComponent.TERMINUS) {
inputNodes.remove(l);
outputNodes.remove(l);
advancedOutputNodes.remove(l);
@ -115,7 +116,7 @@ public class CargoNet extends Network {
imports.remove(l);
exports.remove(l);
}
if (to == Component.TERMINUS) {
if (to == NetworkComponent.TERMINUS) {
switch(BlockStorage.checkID(l)) {
case "CARGO_NODE_INPUT":
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 Component classifyLocation(Location l);
public abstract void locationClassificationChange(Location l, Component from, Component to);
public abstract NetworkComponent classifyLocation(Location l);
public abstract void locationClassificationChange(Location l, NetworkComponent from, NetworkComponent to);
protected Location regulator;
private Queue<Location> nodeQueue = new ArrayDeque<>();
@ -94,15 +87,15 @@ public abstract class Network {
return connectedLocations.contains(l);
}
private Component getCurrentClassification(Location l) {
private NetworkComponent getCurrentClassification(Location l) {
if(regulatorNodes.contains(l)) {
return Component.REGULATOR;
return NetworkComponent.REGULATOR;
}
else if(connectorNodes.contains(l)) {
return Component.CONNECTOR;
return NetworkComponent.CONNECTOR;
}
else if(terminusNodes.contains(l)) {
return Component.TERMINUS;
return NetworkComponent.TERMINUS;
}
return null;
}
@ -111,28 +104,28 @@ public abstract class Network {
int steps = 0;
while (nodeQueue.peek() != null) {
Location l = nodeQueue.poll();
Component currentAssignment = getCurrentClassification(l);
Component classification = classifyLocation(l);
NetworkComponent currentAssignment = getCurrentClassification(l);
NetworkComponent classification = classifyLocation(l);
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.
unregisterNetwork(this);
return;
}
else if (currentAssignment == Component.TERMINUS) {
else if (currentAssignment == NetworkComponent.TERMINUS) {
terminusNodes.remove(l);
}
if (classification == Component.REGULATOR) {
if (classification == NetworkComponent.REGULATOR) {
regulatorNodes.add(l);
discoverNeighbors(l);
}
else if(classification == Component.CONNECTOR) {
else if(classification == NetworkComponent.CONNECTOR) {
connectorNodes.add(l);
discoverNeighbors(l);
}
else if(classification == Component.TERMINUS) {
else if(classification == NetworkComponent.TERMINUS) {
terminusNodes.add(l);
}

View File

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