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

Another performance improvement to Energy networks

This commit is contained in:
TheBusyBiscuit 2020-07-15 18:23:21 +02:00
parent f14ec8dc99
commit eaaf0c5c01
7 changed files with 62 additions and 25 deletions

10
pom.xml
View File

@ -231,9 +231,17 @@
<packages>io.github.thebusybiscuit.slimefun4.utils*</packages>
</group>
<group>
<title>Slimefun4 - Item Implementations</title>
<title>Slimefun4 - Items</title>
<packages>io.github.thebusybiscuit.slimefun4.implementation.items*</packages>
</group>
<group>
<title>Slimefun4 - Multiblocks</title>
<packages>io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks*</packages>
</group>
<group>
<title>Slimefun4 - Electrical Machines</title>
<packages>io.github.thebusybiscuit.slimefun4.implementation.items.electric*</packages>
</group>
<group>
<title>Slimefun4 - Old packages</title>
<packages>me.mrCookieSlime.Slimefun*</packages>

View File

@ -30,7 +30,7 @@ public class WrongItemStackException extends RuntimeException {
* An error message to display
*/
public WrongItemStackException(String message) {
super("You probably wanted alter a different ItemStack: " + message);
super("You probably wanted to alter a different ItemStack: " + message);
}
}

View File

@ -6,6 +6,7 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang.Validate;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@ -14,7 +15,7 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import io.github.thebusybiscuit.cscorelib2.chat.ChatColors;
import io.github.thebusybiscuit.slimefun4.core.commands.subcommands.Commands;
import io.github.thebusybiscuit.slimefun4.core.commands.subcommands.SlimefunSubCommands;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
/**
@ -25,6 +26,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
*/
public class SlimefunCommand implements CommandExecutor, Listener {
private boolean registered = false;
private final SlimefunPlugin plugin;
private final List<SubCommand> commands = new LinkedList<>();
private final Map<SubCommand, Integer> commandUsage = new HashMap<>();
@ -40,11 +42,14 @@ public class SlimefunCommand implements CommandExecutor, Listener {
}
public void register() {
Validate.isTrue(!registered, "Slimefun's subcommands have already been registered!");
registered = true;
plugin.getServer().getPluginManager().registerEvents(this, plugin);
plugin.getCommand("slimefun").setExecutor(this);
plugin.getCommand("slimefun").setTabCompleter(new SlimefunTabCompleter(this));
Commands.addCommands(this, commands);
commands.addAll(SlimefunSubCommands.getAllCommands(this));
}
public SlimefunPlugin getPlugin() {

View File

@ -1,18 +1,28 @@
package io.github.thebusybiscuit.slimefun4.core.commands.subcommands;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import io.github.thebusybiscuit.slimefun4.core.commands.SlimefunCommand;
import io.github.thebusybiscuit.slimefun4.core.commands.SubCommand;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
public final class Commands {
/**
* This class holds the implementations of every {@link SubCommand}.
* The implementations themselves are package-private, this class only provides
* a static setup method
*
* @author TheBusyBiscuit
*
*/
public final class SlimefunSubCommands {
private Commands() {
}
private SlimefunSubCommands() {}
public static void addCommands(SlimefunCommand cmd, Collection<SubCommand> commands) {
public static Collection<SubCommand> getAllCommands(SlimefunCommand cmd) {
SlimefunPlugin plugin = cmd.getPlugin();
List<SubCommand> commands = new LinkedList<>();
commands.add(new HelpCommand(plugin, cmd));
commands.add(new VersionsCommand(plugin, cmd));
@ -27,5 +37,7 @@ public final class Commands {
commands.add(new SearchCommand(plugin, cmd));
commands.add(new DebugFishCommand(plugin, cmd));
commands.add(new BackpackCommand(plugin, cmd));
return commands;
}
}

View File

@ -1,6 +1,8 @@
package io.github.thebusybiscuit.slimefun4.core.networks.energy;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
@ -144,7 +146,8 @@ public class EnergyNet extends Network {
SimpleHologram.update(b, "&4No Energy Network found");
}
else {
int supply = tickAllGenerators(timestamp::getAndAdd) + tickAllCapacitors();
Map<Location, Integer> generatorsWithCapacity = new HashMap<>();
int supply = tickAllGenerators(generatorsWithCapacity, timestamp::getAndAdd) + tickAllCapacitors();
int remainingEnergy = supply;
int demand = 0;
@ -169,7 +172,7 @@ public class EnergyNet extends Network {
}
}
storeExcessEnergy(remainingEnergy);
storeExcessEnergy(generatorsWithCapacity, remainingEnergy);
updateHologram(b, supply, demand);
}
@ -177,7 +180,7 @@ public class EnergyNet extends Network {
SlimefunPlugin.getProfiler().closeEntry(b.getLocation(), SlimefunItems.ENERGY_REGULATOR.getItem(), timestamp.get());
}
private void storeExcessEnergy(int available) {
private void storeExcessEnergy(Map<Location, Integer> generators, int available) {
for (Location capacitor : storage) {
if (available > 0) {
int capacity = ChargableBlock.getMaxCharge(capacitor);
@ -196,14 +199,15 @@ public class EnergyNet extends Network {
}
}
for (Location generator : generators) {
int capacity = ChargableBlock.getMaxCharge(generator);
for (Map.Entry<Location, Integer> entry : generators.entrySet()) {
Location generator = entry.getKey();
int capacity = entry.getValue();
if (capacity > 0) {
if (available > 0) {
if (available > capacity) {
ChargableBlock.setUnsafeCharge(generator, capacity, false);
available = available - capacity;
available -= capacity;
}
else {
ChargableBlock.setUnsafeCharge(generator, available, false);
@ -217,7 +221,7 @@ public class EnergyNet extends Network {
}
}
private int tickAllGenerators(LongConsumer timeCallback) {
private int tickAllGenerators(Map<Location, Integer> generatorsWithCapacity, LongConsumer timeCallback) {
int supply = 0;
Set<Location> exploded = new HashSet<>();
@ -228,10 +232,11 @@ public class EnergyNet extends Network {
if (item instanceof EnergyNetProvider) {
try {
EnergyNetProvider generator = (EnergyNetProvider) item;
int energy = generator.getGeneratedOutput(source, config);
EnergyNetProvider provider = (EnergyNetProvider) item;
int energy = provider.getGeneratedOutput(source, config);
if (generator.getCapacity() > 0) {
if (provider.getCapacity() > 0) {
generatorsWithCapacity.put(source, provider.getCapacity());
String charge = config.getString("energy-charge");
if (charge != null) {
@ -239,7 +244,7 @@ public class EnergyNet extends Network {
}
}
if (generator.willExplode(source, config)) {
if (provider.willExplode(source, config)) {
exploded.add(source);
BlockStorage.clearBlockInfo(source);
Reactor.processing.remove(source);
@ -265,9 +270,15 @@ public class EnergyNet extends Network {
else if (item != null) {
try {
// This will be removed very very soon
// It is only here for backwards compatibility
// It is only here for temporary backwards compatibility
GeneratorTicker generator = item.getEnergyTicker();
Integer capacity = SlimefunPlugin.getRegistry().getEnergyCapacities().get(item.getID());
if (capacity != null && capacity.intValue() > 0) {
generatorsWithCapacity.put(source, capacity.intValue());
}
if (generator != null) {
double energy = generator.generateEnergy(source, item, config);

View File

@ -119,12 +119,12 @@ public class ElevatorPlate extends SimpleSlimefunItem<BlockUseHandler> {
ChatComponent line;
if (block.getY() == b.getY()) {
line = new ChatComponent("\n" + ChatColor.GRAY + "> " + (floors.size() - i) + ". " + ChatColor.RESET + floor);
line.setHoverEvent(new HoverEvent(ChatColors.color(SlimefunPlugin.getLocalization().getMessage(p, "machines.ELEVATOR.current-floor")), "", ChatColor.RESET + floor, ""));
line = new ChatComponent("\n" + ChatColor.GRAY + "> " + (floors.size() - i) + ". " + ChatColor.WHITE + floor);
line.setHoverEvent(new HoverEvent(ChatColors.color(SlimefunPlugin.getLocalization().getMessage(p, "machines.ELEVATOR.current-floor")), "", ChatColor.WHITE + floor, ""));
}
else {
line = new ChatComponent("\n" + ChatColor.GRAY.toString() + (floors.size() - i) + ". " + ChatColor.RESET + floor);
line.setHoverEvent(new HoverEvent(ChatColors.color(SlimefunPlugin.getLocalization().getMessage(p, "machines.ELEVATOR.click-to-teleport")), "", ChatColor.RESET + floor, ""));
line = new ChatComponent("\n" + ChatColor.GRAY + (floors.size() - i) + ". " + ChatColor.WHITE + floor);
line.setHoverEvent(new HoverEvent(ChatColors.color(SlimefunPlugin.getLocalization().getMessage(p, "machines.ELEVATOR.click-to-teleport")), "", ChatColor.WHITE + floor, ""));
line.setClickEvent(new ClickEvent(new NamespacedKey(SlimefunPlugin.instance(), DATA_KEY + i), player -> Slimefun.runSync(() -> {
users.add(player.getUniqueId());
@ -135,7 +135,7 @@ public class ElevatorPlate extends SimpleSlimefunItem<BlockUseHandler> {
}
player.teleport(new Location(player.getWorld(), block.getX() + 0.5, block.getY() + 0.4, block.getZ() + 0.5, yaw, player.getEyeLocation().getPitch()));
player.sendTitle(ChatColor.RESET + ChatColors.color(floor), " ", 20, 60, 20);
player.sendTitle(ChatColor.WHITE + ChatColors.color(floor), " ", 20, 60, 20);
})));
}

View File

@ -82,6 +82,7 @@ public abstract class AGenerator extends AbstractEnergyProvider {
}
}
}
progress.remove(b.getLocation());
processing.remove(b.getLocation());
return true;