1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Small performance improvement

This commit is contained in:
TheBusyBiscuit 2020-07-02 20:41:40 +02:00
parent b2f594ed88
commit 4ddea8f7ae
3 changed files with 87 additions and 76 deletions

View File

@ -56,6 +56,7 @@
* Item Energy is now also stored persistently via NBT
* General performance improvements for ticking blocks
* Performance improvements to the Cargo Net
* Some performance improvements to the Energy Net
#### Fixes
* Fixed #2005

View File

@ -141,68 +141,71 @@ public class EnergyNet extends Network {
double supply = DoubleHandler.fixDouble(tickAllGenerators() + tickAllCapacitors());
double demand = 0;
int available = (int) supply;
int availableEnergy = (int) supply;
for (Location destination : consumers) {
int capacity = ChargableBlock.getMaxCharge(destination);
int charge = ChargableBlock.getCharge(destination);
for (Location machine : consumers) {
int capacity = ChargableBlock.getMaxCharge(machine);
int charge = ChargableBlock.getCharge(machine);
if (charge < capacity) {
int rest = capacity - charge;
demand += rest;
int availableSpace = capacity - charge;
demand += availableSpace;
if (available > 0) {
if (available > rest) {
ChargableBlock.setUnsafeCharge(destination, capacity, false);
available = available - rest;
if (availableEnergy > 0) {
if (availableEnergy > availableSpace) {
ChargableBlock.setUnsafeCharge(machine, capacity, false);
availableEnergy -= availableSpace;
}
else {
ChargableBlock.setUnsafeCharge(destination, charge + available, false);
available = 0;
ChargableBlock.setUnsafeCharge(machine, charge + availableEnergy, false);
availableEnergy = 0;
}
}
}
}
for (Location battery : storage) {
if (available > 0) {
int capacity = ChargableBlock.getMaxCharge(battery);
storeExcessEnergy(availableEnergy);
updateHologram(b, supply, demand);
}
}
private void storeExcessEnergy(int available) {
for (Location capacitor : storage) {
if (available > 0) {
int capacity = ChargableBlock.getMaxCharge(capacitor);
if (available > capacity) {
ChargableBlock.setUnsafeCharge(capacitor, capacity, true);
available -= capacity;
}
else {
ChargableBlock.setUnsafeCharge(capacitor, available, true);
available = 0;
}
}
else {
ChargableBlock.setUnsafeCharge(capacitor, 0, true);
}
}
for (Location generator : generators) {
int capacity = ChargableBlock.getMaxCharge(generator);
if (capacity > 0) {
if (available > 0) {
if (available > capacity) {
ChargableBlock.setUnsafeCharge(battery, capacity, true);
ChargableBlock.setUnsafeCharge(generator, capacity, false);
available = available - capacity;
}
else {
ChargableBlock.setUnsafeCharge(battery, available, true);
ChargableBlock.setUnsafeCharge(generator, available, false);
available = 0;
}
}
else {
ChargableBlock.setUnsafeCharge(battery, 0, true);
ChargableBlock.setUnsafeCharge(generator, 0, false);
}
}
for (Location source : generators) {
if (ChargableBlock.isChargable(source)) {
if (available > 0) {
int capacity = ChargableBlock.getMaxCharge(source);
if (available > capacity) {
ChargableBlock.setUnsafeCharge(source, capacity, false);
available = available - capacity;
}
else {
ChargableBlock.setUnsafeCharge(source, available, false);
available = 0;
}
}
else {
ChargableBlock.setUnsafeCharge(source, 0, false);
}
}
}
updateHologram(b, supply, demand);
}
}
@ -212,11 +215,10 @@ public class EnergyNet extends Network {
for (Location source : generators) {
long timestamp = SlimefunPlugin.getProfiler().newEntry();
SlimefunItem item = BlockStorage.check(source);
Config config = BlockStorage.getLocationInfo(source);
SlimefunItem item = SlimefunItem.getByID(config.getString("id"));
if (item != null) {
Config config = BlockStorage.getLocationInfo(source);
try {
GeneratorTicker generator = item.getEnergyTicker();
@ -263,8 +265,8 @@ public class EnergyNet extends Network {
private double tickAllCapacitors() {
double supply = 0;
for (Location battery : storage) {
supply += ChargableBlock.getCharge(battery);
for (Location capacitor : storage) {
supply += ChargableBlock.getCharge(capacitor);
}
return supply;

View File

@ -7,12 +7,13 @@ import org.bukkit.block.Block;
import io.github.thebusybiscuit.cscorelib2.skull.SkullBlock;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.utils.HeadTexture;
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.Slimefun;
public final class ChargableBlock {
private static final String KEY = "energy-charge";
private ChargableBlock() {}
public static boolean isChargable(Block b) {
@ -20,11 +21,12 @@ public final class ChargableBlock {
}
public static boolean isChargable(Location l) {
if (!BlockStorage.hasBlockInfo(l)) {
String id = BlockStorage.checkID(l);
if (id == null) {
return false;
}
String id = BlockStorage.checkID(l);
return SlimefunPlugin.getRegistry().getEnergyCapacities().containsKey(id);
}
@ -33,13 +35,13 @@ public final class ChargableBlock {
}
public static int getCharge(Location l) {
String charge = BlockStorage.getLocationInfo(l, "energy-charge");
String charge = BlockStorage.getLocationInfo(l, KEY);
if (charge != null) {
return Integer.parseInt(charge);
}
else {
BlockStorage.addBlockInfo(l, "energy-charge", "0", false);
BlockStorage.addBlockInfo(l, KEY, "0", false);
return 0;
}
}
@ -60,17 +62,15 @@ public final class ChargableBlock {
}
}
if (charge != getCharge(l)) {
BlockStorage.addBlockInfo(l, "energy-charge", String.valueOf(charge), false);
}
BlockStorage.addBlockInfo(l, KEY, String.valueOf(charge), false);
}
public static void setUnsafeCharge(Location l, int charge, boolean updateTexture) {
if (charge != getCharge(l)) {
BlockStorage.addBlockInfo(l, "energy-charge", String.valueOf(charge), false);
BlockStorage.addBlockInfo(l, KEY, String.valueOf(charge), false);
if (updateTexture) {
updateCapacitor(l);
updateCapacitor(l, charge, getMaxCharge(l));
}
}
}
@ -79,42 +79,51 @@ public final class ChargableBlock {
return addCharge(b.getLocation(), charge);
}
public static int addCharge(Location l, int charge) {
int capacity = getMaxCharge(l);
int energy = getCharge(l);
int space = capacity - energy;
int rest = charge;
public static int addCharge(Location l, int addedCharge) {
String id = BlockStorage.checkID(l);
if (space > 0 && charge > 0) {
if (space > charge) {
setCharge(l, energy + charge);
if (id == null) {
BlockStorage.clearBlockInfo(l);
return 0;
}
int capacity = SlimefunPlugin.getRegistry().getEnergyCapacities().getOrDefault(id, 0);
int charge = getCharge(l);
int availableSpace = capacity - charge;
int rest = addedCharge;
if (availableSpace > 0 && addedCharge > 0) {
if (availableSpace > addedCharge) {
charge += addedCharge;
setCharge(l, charge);
rest = 0;
}
else {
rest = charge - space;
setCharge(l, capacity);
rest = addedCharge - availableSpace;
charge = capacity;
setCharge(l, charge);
}
if (SlimefunPlugin.getRegistry().getEnergyCapacitors().contains(BlockStorage.checkID(l))) {
updateCapacitor(l);
if (SlimefunPlugin.getRegistry().getEnergyCapacitors().contains(id)) {
updateCapacitor(l, charge, capacity);
}
}
else if (charge < 0 && energy >= -charge) {
setCharge(l, energy + charge);
else if (addedCharge < 0 && charge >= -addedCharge) {
charge += addedCharge;
setCharge(l, charge);
if (SlimefunPlugin.getRegistry().getEnergyCapacitors().contains(BlockStorage.checkID(l))) {
updateCapacitor(l);
if (SlimefunPlugin.getRegistry().getEnergyCapacitors().contains(id)) {
updateCapacitor(l, charge, capacity);
}
}
return rest;
}
private static void updateCapacitor(Location l) {
private static void updateCapacitor(Location l, int charge, int capacity) {
Slimefun.runSync(() -> {
Block b = l.getBlock();
int charge = getCharge(b);
int capacity = getMaxCharge(b);
if (b.getType() == Material.PLAYER_HEAD || b.getType() == Material.PLAYER_WALL_HEAD) {
if (charge < (int) (capacity * 0.25)) {
@ -138,8 +147,7 @@ public final class ChargableBlock {
}
public static int getMaxCharge(Location l) {
Config cfg = BlockStorage.getLocationInfo(l);
String id = cfg.getString("id");
String id = BlockStorage.checkID(l);
if (id == null) {
BlockStorage.clearBlockInfo(l);