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

Some optimizations, also changed /sf timings to use floating point times

This commit is contained in:
TheBusyBiscuit 2019-10-27 20:07:12 +01:00
parent e036e0f756
commit 9f3c3a413b
6 changed files with 67 additions and 49 deletions

View File

@ -66,8 +66,9 @@ public final class OreGenSystem {
public static int getSupplies(OreGenResource resource, Chunk chunk, boolean generate) { public static int getSupplies(OreGenResource resource, Chunk chunk, boolean generate) {
if (resource == null) return 0; if (resource == null) return 0;
if (BlockStorage.hasChunkInfo(chunk, "resources_" + resource.getName().toUpperCase())) { String supply = BlockStorage.getChunkInfo(chunk, "resources_" + resource.getName().toUpperCase());
return Integer.parseInt(BlockStorage.getChunkInfo(chunk, "resources_" + resource.getName().toUpperCase())); if (supply != null) {
return Integer.parseInt(supply);
} }
else if (!generate) { else if (!generate) {
return 0; return 0;

View File

@ -98,11 +98,16 @@ public abstract class OilPump extends AContainer {
else { else {
OreGenResource oil = OreGenSystem.getResource("Oil"); OreGenResource oil = OreGenSystem.getResource("Oil");
int supplies = OreGenSystem.getSupplies(oil, b.getChunk(), false); int supplies = OreGenSystem.getSupplies(oil, b.getChunk(), false);
if (supplies > 0) { if (supplies > 0) {
for (int slot: getInputSlots()) { for (int slot: getInputSlots()) {
if (SlimefunManager.isItemSimiliar(inv.getItemInSlot(slot), new ItemStack(Material.BUCKET), true)) { if (SlimefunManager.isItemSimiliar(inv.getItemInSlot(slot), new ItemStack(Material.BUCKET), true)) {
MachineRecipe r = new MachineRecipe(26, new ItemStack[0], new ItemStack[] {SlimefunItems.BUCKET_OF_OIL}); MachineRecipe r = new MachineRecipe(26, new ItemStack[0], new ItemStack[] {SlimefunItems.BUCKET_OF_OIL});
if (!inv.fits(SlimefunItems.BUCKET_OF_OIL, getOutputSlots())) return;
if (!inv.fits(SlimefunItems.BUCKET_OF_OIL, getOutputSlots())) {
return;
}
inv.replaceExistingItem(slot, InvUtils.decreaseItem(inv.getItemInSlot(slot), 1)); inv.replaceExistingItem(slot, InvUtils.decreaseItem(inv.getItemInSlot(slot), 1));
processing.put(b, r); processing.put(b, r);
progress.put(b, r.getTicks()); progress.put(b, r.getTicks());

View File

@ -8,6 +8,8 @@ import java.util.Set;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import com.google.gson.GsonBuilder;
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
public class BlockInfoConfig extends Config { public class BlockInfoConfig extends Config {
@ -136,4 +138,8 @@ public class BlockInfoConfig extends Config {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public String toJSON() {
return new GsonBuilder().create().toJson(data);
}
} }

View File

@ -160,7 +160,9 @@ public class BlockStorage {
FileConfiguration cfg = YamlConfiguration.loadConfiguration(chunks); FileConfiguration cfg = YamlConfiguration.loadConfiguration(chunks);
for (String key: cfg.getKeys(false)) { for (String key: cfg.getKeys(false)) {
try { try {
if (world.getName().equals(key.split(";")[0])) SlimefunPlugin.getUtilities().mapChunks.put(key, cfg.getString(key)); if (world.getName().equals(key.split(";")[0])) {
SlimefunPlugin.getUtilities().mapChunks.put(key, new BlockInfoConfig(parseJSON(cfg.getString(key))));
}
} catch (Exception x) { } catch (Exception x) {
Slimefun.getLogger().log(Level.WARNING, "Failed to load " + chunks.getName() + " in World " + world.getName() + '(' + key + ") for Slimefun " + Slimefun.getVersion(), x); Slimefun.getLogger().log(Level.WARNING, "Failed to load " + chunks.getName() + " in World " + world.getName() + '(' + key + ") for Slimefun " + Slimefun.getVersion(), x);
} }
@ -238,6 +240,7 @@ public class BlockStorage {
for (Map.Entry<String, Config> entry: cache.entrySet()) { for (Map.Entry<String, Config> entry: cache.entrySet()) {
blocksCache.remove(entry.getKey()); blocksCache.remove(entry.getKey());
Config cfg = entry.getValue(); Config cfg = entry.getValue();
if (cfg.getKeys().isEmpty()) { if (cfg.getKeys().isEmpty()) {
if (!cfg.getFile().delete()) { if (!cfg.getFile().delete()) {
Slimefun.getLogger().log(Level.WARNING, "Could not delete File: " + cfg.getFile().getName()); Slimefun.getLogger().log(Level.WARNING, "Could not delete File: " + cfg.getFile().getName());
@ -269,10 +272,10 @@ public class BlockStorage {
if (chunkChanges > 0) { if (chunkChanges > 0) {
File chunks = new File(path_chunks + "chunks.sfc"); File chunks = new File(path_chunks + "chunks.sfc");
Config cfg = new Config("data-storage/Slimefun/temp.yml"); Config cfg = new Config(path_chunks + "chunks.temp");
for (Map.Entry<String, String> entry: SlimefunPlugin.getUtilities().mapChunks.entrySet()) { for (Map.Entry<String, BlockInfoConfig> entry: SlimefunPlugin.getUtilities().mapChunks.entrySet()) {
cfg.setValue(entry.getKey(), entry.getValue()); cfg.setValue(entry.getKey(), entry.getValue().toJSON());
} }
cfg.save(chunks); cfg.save(chunks);
@ -369,7 +372,7 @@ public class BlockStorage {
private static String getJSONData(Chunk chunk) { private static String getJSONData(Chunk chunk) {
if (chunk == null) return null; if (chunk == null) return null;
return SlimefunPlugin.getUtilities().mapChunks.get(serializeChunk(chunk)); return SlimefunPlugin.getUtilities().mapChunks.get(serializeChunk(chunk)).toJSON();
} }
@Deprecated @Deprecated
@ -422,7 +425,9 @@ public class BlockStorage {
storage.storage.put(l, cfg); storage.storage.put(l, cfg);
if (BlockMenuPreset.isInventory(cfg.getString("id"))) { if (BlockMenuPreset.isInventory(cfg.getString("id"))) {
if (BlockMenuPreset.isUniversalInventory(cfg.getString("id"))) { if (BlockMenuPreset.isUniversalInventory(cfg.getString("id"))) {
if (!SlimefunPlugin.getUtilities().universalInventories.containsKey(cfg.getString("id"))) storage.loadUniversalInventory(BlockMenuPreset.getPreset(cfg.getString("id"))); if (!SlimefunPlugin.getUtilities().universalInventories.containsKey(cfg.getString("id"))) {
storage.loadUniversalInventory(BlockMenuPreset.getPreset(cfg.getString("id")));
}
} }
else if (!storage.hasInventory(l)) { else if (!storage.hasInventory(l)) {
File file = new File("data-storage/Slimefun/stored-inventories/" + serializeLocation(l) + ".sfi"); File file = new File("data-storage/Slimefun/stored-inventories/" + serializeLocation(l) + ".sfi");
@ -433,9 +438,11 @@ public class BlockStorage {
} }
refreshCache(getStorage(l.getWorld()), l, cfg.getString("id"), serializeBlockInfo(cfg), updateTicker); refreshCache(getStorage(l.getWorld()), l, cfg.getString("id"), serializeBlockInfo(cfg), updateTicker);
} }
public static void setBlockInfo(Block b, String json, boolean updateTicker) { public static void setBlockInfo(Block b, String json, boolean updateTicker) {
setBlockInfo(b.getLocation(), json, updateTicker); setBlockInfo(b.getLocation(), json, updateTicker);
} }
public static void setBlockInfo(Location l, String json, boolean updateTicker) { public static void setBlockInfo(Location l, String json, boolean updateTicker) {
Config blockInfo = json == null ? new BlockInfoConfig() : parseBlockInfo(l, json); Config blockInfo = json == null ? new BlockInfoConfig() : parseBlockInfo(l, json);
if (blockInfo == null) return; if (blockInfo == null) return;
@ -666,17 +673,11 @@ public class BlockStorage {
public static Config getChunkInfo(Chunk chunk) { public static Config getChunkInfo(Chunk chunk) {
try { try {
Config cfg = new Config("data-storage/Slimefun/temp.yml"); BlockInfoConfig cfg = SlimefunPlugin.getUtilities().mapChunks.get(serializeChunk(chunk));
if (!SlimefunPlugin.getUtilities().mapChunks.containsKey(serializeChunk(chunk))) return cfg; return cfg == null ? new BlockInfoConfig() : cfg;
for (Map.Entry<String, String> entry: parseJSON(getJSONData(chunk)).entrySet()) {
cfg.setValue(entry.getKey(), entry.getValue());
}
return cfg;
} catch (Exception x) { } catch (Exception x) {
Slimefun.getLogger().log(Level.SEVERE, "Failed to parse ChunkInfo for Chunk: " + (chunk == null ? "?": chunk.getX()) + ", " + (chunk == null ? "?": chunk.getZ()) + " (" + getJSONData(chunk) + ") for Slimefun " + Slimefun.getVersion(), x); Slimefun.getLogger().log(Level.SEVERE, "Failed to parse ChunkInfo for Chunk: " + (chunk == null ? "?": chunk.getX()) + ", " + (chunk == null ? "?": chunk.getZ()) + " (" + getJSONData(chunk) + ") for Slimefun " + Slimefun.getVersion(), x);
return new Config("data-storage/Slimefun/temp.yml"); return new BlockInfoConfig();
} }
} }
@ -685,16 +686,14 @@ public class BlockStorage {
} }
public static void setChunkInfo(Chunk chunk, String key, String value) { public static void setChunkInfo(Chunk chunk, String key, String value) {
Config cfg = new Config("data-storage/Slimefun/temp.yml"); BlockInfoConfig cfg = SlimefunPlugin.getUtilities().mapChunks.get(serializeChunk(chunk));
if (hasChunkInfo(chunk)) cfg = getChunkInfo(chunk);
cfg.setValue(key, value);
JsonObject json = new JsonObject(); if (cfg == null) {
for (String path: cfg.getKeys()) { cfg = new BlockInfoConfig();
json.add(path, new JsonPrimitive(cfg.getString(path))); SlimefunPlugin.getUtilities().mapChunks.put(serializeChunk(chunk), cfg);
} }
SlimefunPlugin.getUtilities().mapChunks.put(serializeChunk(chunk), json.toString()); cfg.setValue(key, value);
chunkChanges++; chunkChanges++;
} }

View File

@ -1,5 +1,6 @@
package me.mrCookieSlime.Slimefun.api; package me.mrCookieSlime.Slimefun.api;
import java.text.DecimalFormat;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
@ -29,6 +30,8 @@ import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker;
public class TickerTask implements Runnable { public class TickerTask implements Runnable {
private static final DecimalFormat decimalFormat = new DecimalFormat("#.###");
private boolean halted = false; private boolean halted = false;
protected final Map<Location, Location> move = new HashMap<>(); protected final Map<Location, Location> move = new HashMap<>();
@ -60,7 +63,7 @@ public class TickerTask implements Runnable {
if (running) return; if (running) return;
running = true; running = true;
long timestamp = System.currentTimeMillis(); long timestamp = System.nanoTime();
skipped = 0; skipped = 0;
chunks = 0; chunks = 0;
@ -85,7 +88,7 @@ public class TickerTask implements Runnable {
if (!halted) { if (!halted) {
for (final String c: BlockStorage.getTickingChunks()) { for (final String c: BlockStorage.getTickingChunks()) {
long timestamp2 = System.currentTimeMillis(); long timestamp2 = System.nanoTime();
chunks++; chunks++;
for (final Location l: BlockStorage.getTickingLocations(c)) { for (final Location l: BlockStorage.getTickingLocations(c)) {
@ -102,17 +105,17 @@ public class TickerTask implements Runnable {
if (item.getBlockTicker().isSynchronized()) { if (item.getBlockTicker().isSynchronized()) {
Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> { Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> {
try { try {
long timestamp3 = System.currentTimeMillis(); long timestamp3 = System.nanoTime();
item.getBlockTicker().tick(b, item, BlockStorage.getLocationInfo(l)); item.getBlockTicker().tick(b, item, BlockStorage.getLocationInfo(l));
Long machinetime = machineTimings.get(item.getID()); Long machinetime = machineTimings.get(item.getID());
Integer chunk = chunkItemCount.get(c); Integer chunk = chunkItemCount.get(c);
Integer machine = machineCount.get(item.getID()); Integer machine = machineCount.get(item.getID());
machineTimings.put(item.getID(), (machinetime != null ? machinetime: 0) + (System.currentTimeMillis() - timestamp3)); machineTimings.put(item.getID(), (machinetime != null ? machinetime: 0) + (System.nanoTime() - timestamp3));
chunkItemCount.put(c, (chunk != null ? chunk: 0) + 1); chunkItemCount.put(c, (chunk != null ? chunk: 0) + 1);
machineCount.put(item.getID(), (machine != null ? machine: 0) + 1); machineCount.put(item.getID(), (machine != null ? machine: 0) + 1);
blockTimings.put(l, System.currentTimeMillis() - timestamp3); blockTimings.put(l, System.nanoTime() - timestamp3);
} catch (Exception x) { } catch (Exception x) {
int errors = bugged.getOrDefault(l, 0); int errors = bugged.getOrDefault(l, 0);
reportErrors(l, item, x, errors); reportErrors(l, item, x, errors);
@ -120,13 +123,13 @@ public class TickerTask implements Runnable {
}); });
} }
else { else {
long timestamp3 = System.currentTimeMillis(); long timestamp3 = System.nanoTime();
item.getBlockTicker().tick(b, item, BlockStorage.getLocationInfo(l)); item.getBlockTicker().tick(b, item, BlockStorage.getLocationInfo(l));
machineTimings.merge(item.getID(), (System.currentTimeMillis() - timestamp3), Long::sum); machineTimings.merge(item.getID(), (System.nanoTime() - timestamp3), Long::sum);
chunkItemCount.merge(c, 1, Integer::sum); chunkItemCount.merge(c, 1, Integer::sum);
machineCount.merge(item.getID(), 1, Integer::sum); machineCount.merge(item.getID(), 1, Integer::sum);
blockTimings.put(l, System.currentTimeMillis() - timestamp3); blockTimings.put(l, System.nanoTime() - timestamp3);
} }
tickers.add(item.getBlockTicker()); tickers.add(item.getBlockTicker());
} catch (Exception x) { } catch (Exception x) {
@ -144,7 +147,7 @@ public class TickerTask implements Runnable {
} }
} }
chunkTimings.put(c, System.currentTimeMillis() - timestamp2); chunkTimings.put(c, System.nanoTime() - timestamp2);
} }
} }
@ -159,7 +162,7 @@ public class TickerTask implements Runnable {
iterator.remove(); iterator.remove();
} }
time = System.currentTimeMillis() - timestamp; time = System.nanoTime() - timestamp;
running = false; running = false;
} }
@ -187,15 +190,15 @@ public class TickerTask implements Runnable {
} }
} }
public long getTime() { public String getTime() {
return time; return toMillis(time);
} }
public void info(CommandSender sender) { public void info(CommandSender sender) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&2== &aSlimefun Diagnostic Tool &2==")); sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&2== &aSlimefun Diagnostic Tool &2=="));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Halted: &e&l" + String.valueOf(halted).toUpperCase())); sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Halted: &e&l" + String.valueOf(halted).toUpperCase()));
sender.sendMessage(""); sender.sendMessage("");
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Impact: &e" + time + "ms / 50-750ms")); sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Impact: &e" + toMillis(time)));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Ticked Chunks: &e" + chunks)); sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Ticked Chunks: &e" + chunks));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Ticked Machines: &e" + machines)); sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Ticked Machines: &e" + machines));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Skipped Machines: &e" + skipped)); sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Skipped Machines: &e" + skipped));
@ -215,10 +218,10 @@ public class TickerTask implements Runnable {
for (Map.Entry<String, Long> entry : timings) { for (Map.Entry<String, Long> entry : timings) {
int count = machineCount.get(entry.getKey()); int count = machineCount.get(entry.getKey());
if (entry.getValue() > 0) if (entry.getValue() > 500_000)
hover.append("\n&c").append(entry.getKey()).append(" - ") hover.append("\n&c").append(entry.getKey()).append(" - ")
.append(count).append("x &7(").append(entry.getValue()).append("ms, ") .append(count).append("x &7(").append(toMillis(entry.getValue())).append(", ")
.append(entry.getValue() / count).append("ms avg/machine)"); .append(toMillis(entry.getValue() / count)).append(" avg/machine)");
else else
hidden++; hidden++;
} }
@ -237,11 +240,10 @@ public class TickerTask implements Runnable {
for (Map.Entry<String, Long> entry: timings) { for (Map.Entry<String, Long> entry: timings) {
int count = machineCount.get(entry.getKey()); int count = machineCount.get(entry.getKey());
if (entry.getValue() > 0) if (entry.getValue() > 500_000) {
sender.sendMessage(ChatColors.color(" &e" + entry.getKey() + " - " + count + "x &7(" + entry.getValue() + "ms" sender.sendMessage(ChatColors.color(" &e" + entry.getKey() + " - " + count + "x &7(" + toMillis(entry.getValue()) + ", " + toMillis(entry.getValue() / count) + " avg/machine)"));
+ ", " + (entry.getValue() / count) + "ms avg/machine)")); }
else else hidden++;
hidden++;
} }
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c+ &4" + hidden + " Hidden")); sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c+ &4" + hidden + " Hidden"));
@ -265,7 +267,7 @@ public class TickerTask implements Runnable {
if (entry.getValue() > 0) if (entry.getValue() > 0)
hover.append("\n&c").append(entry.getKey().replace("CraftChunk", "")).append(" - ") hover.append("\n&c").append(entry.getKey().replace("CraftChunk", "")).append(" - ")
.append(chunkItemCount.getOrDefault(entry.getKey(), 0)) .append(chunkItemCount.getOrDefault(entry.getKey(), 0))
.append("x &7(").append(entry.getValue()).append("ms)"); .append("x &7(").append(toMillis(entry.getValue())).append(")");
else else
hidden++; hidden++;
} }
@ -285,7 +287,7 @@ public class TickerTask implements Runnable {
for (Map.Entry<String, Long> entry: timings) { for (Map.Entry<String, Long> entry: timings) {
if (!chunksSkipped.contains(entry.getKey())) { if (!chunksSkipped.contains(entry.getKey())) {
if (entry.getValue() > 0) sender.sendMessage(" &c" + entry.getKey().replace("CraftChunk", "") + " - " if (entry.getValue() > 0) sender.sendMessage(" &c" + entry.getKey().replace("CraftChunk", "") + " - "
+ (chunkItemCount.getOrDefault(entry.getKey(), 0)) + "x &7(" + entry.getValue() + "ms)"); + (chunkItemCount.getOrDefault(entry.getKey(), 0)) + "x &7(" + toMillis(entry.getValue()) + ")");
else hidden++; else hidden++;
} }
} }
@ -317,6 +319,10 @@ public class TickerTask implements Runnable {
halted = true; halted = true;
} }
private String toMillis(long time) {
return decimalFormat.format(time / 1000000F) + "ms";
}
@Override @Override
public String toString() { public String toString() {
return "TickerTask {\n" return "TickerTask {\n"

View File

@ -27,6 +27,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.handlers.ItemHandler; import me.mrCookieSlime.Slimefun.Objects.handlers.ItemHandler;
import me.mrCookieSlime.Slimefun.Setup.PostSlimefunLoadingHandler; import me.mrCookieSlime.Slimefun.Setup.PostSlimefunLoadingHandler;
import me.mrCookieSlime.Slimefun.ancient_altar.AltarRecipe; import me.mrCookieSlime.Slimefun.ancient_altar.AltarRecipe;
import me.mrCookieSlime.Slimefun.api.BlockInfoConfig;
import me.mrCookieSlime.Slimefun.api.BlockStorage; import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.GuideHandler; import me.mrCookieSlime.Slimefun.api.GuideHandler;
import me.mrCookieSlime.Slimefun.api.PlayerProfile; import me.mrCookieSlime.Slimefun.api.PlayerProfile;
@ -92,7 +93,7 @@ public final class Utilities {
public final Map<String, BlockStorage> worlds = new HashMap<>(); public final Map<String, BlockStorage> worlds = new HashMap<>();
public final Set<String> loadedTickers = new HashSet<>(); public final Set<String> loadedTickers = new HashSet<>();
public final Map<String, String> mapChunks = new HashMap<>(); public final Map<String, BlockInfoConfig> mapChunks = new HashMap<>();
public final Map<String, Set<Location>> tickingChunks = new HashMap<>(); public final Map<String, Set<Location>> tickingChunks = new HashMap<>();
public final Map<String, UniversalBlockMenu> universalInventories = new HashMap<>(); public final Map<String, UniversalBlockMenu> universalInventories = new HashMap<>();