From 5a19b186330c22156c4b49091db5ee3217f28495 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Sat, 4 Jul 2020 19:04:29 +0200 Subject: [PATCH] Thunderstorms now count as night for Solar Generators (+ other stuff) --- CHANGELOG.md | 5 ++ .../slimefun4/core/SlimefunRegistry.java | 4 +- .../core/networks/cargo/CargoNet.java | 5 ++ .../networks/cargo/ChestTerminalNetwork.java | 16 +++++ .../services/profiler/PerformanceSummary.java | 25 ++++--- .../services/profiler/SlimefunProfiler.java | 28 ++++++++ .../items/androids/Instruction.java | 4 +- .../items/androids/ProgrammableAndroid.java | 69 ++++++++----------- .../implementation/items/androids/Script.java | 28 ++++++++ .../electric/generators/SolarGenerator.java | 22 +++++- .../items/electric/machines/AutoAnvil.java | 10 +-- .../items/electric/machines/AutoBrewer.java | 15 ++-- .../electric/machines/AutoDisenchanter.java | 13 ++-- .../items/electric/machines/AutoDrier.java | 10 +-- .../electric/machines/AutoEnchanter.java | 15 ++-- .../electric/machines/ElectricDustWasher.java | 16 ++--- .../electric/machines/ElectricGoldPan.java | 17 ++--- .../machines/ElectricIngotPulverizer.java | 7 +- .../machines/HeatedPressureChamber.java | 15 ++-- .../listeners/BlockPhysicsListener.java | 1 + .../listeners/CargoNodeListener.java | 23 +++++-- .../listeners/DebugFishListener.java | 20 +++--- .../slimefun4/utils/ChestMenuUtils.java | 8 +++ .../slimefun4/utils/NumberUtils.java | 4 +- .../Objects/SlimefunItem/SlimefunItem.java | 4 +- .../abstractItems/AGenerator.java | 14 +++- .../Slimefun/api/BlockStorage.java | 58 +++++++++++----- .../listeners/TestCargoNodeListener.java | 4 ++ 28 files changed, 298 insertions(+), 162 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc1826bed..9b0b2f270 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,10 @@ * Performance improvements to the Cargo Net * performance improvements to the Energy Net * Performance improvements to Rainbow Blocks +* Performance improvements to Androids +* performance improvements to Generators and Electric Machines +* Cargo timings will now be attributed to the corresponding node and not the Cargo manager +* Thunderstorms now count as night time for Solar Generators #### Fixes * Fixed #2005 @@ -81,6 +85,7 @@ * Fixed concurrency-related issues with the profiling * Fixed #2066 * Fixed Rainbow Glass Panes not properly connecting to blocks +* Fixed Androids turning in the wrong direction ## Release Candidate 13 (16 Jun 2020) https://thebusybiscuit.github.io/builds/TheBusyBiscuit/Slimefun4/stable/#13 diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java index 62aebdfae..3093112fb 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java @@ -18,6 +18,7 @@ import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.cscorelib2.collections.KeyMap; import io.github.thebusybiscuit.cscorelib2.config.Config; +import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion; import io.github.thebusybiscuit.slimefun4.api.geo.GEOResource; import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; import io.github.thebusybiscuit.slimefun4.core.attributes.WitherProof; @@ -25,6 +26,7 @@ import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideImplementation import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideLayout; import io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlock; import io.github.thebusybiscuit.slimefun4.core.researching.Research; +import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.guide.BookSlimefunGuide; import io.github.thebusybiscuit.slimefun4.implementation.guide.CheatSheetSlimefunGuide; import io.github.thebusybiscuit.slimefun4.implementation.guide.ChestSlimefunGuide; @@ -99,7 +101,7 @@ public class SlimefunRegistry { researchRanks.addAll(cfg.getStringList("research-ranks")); - backwardsCompatibility = cfg.getBoolean("options.backwards-compatibility"); + backwardsCompatibility = cfg.getBoolean("options.backwards-compatibility") || SlimefunPlugin.getMinecraftVersion().isBefore(MinecraftVersion.MINECRAFT_1_14); freeCreativeResearches = cfg.getBoolean("researches.free-in-creative-mode"); researchFireworks = cfg.getBoolean("researches.enable-fireworks"); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNet.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNet.java index 4dd87917a..334e913f7 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNet.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNet.java @@ -183,6 +183,7 @@ public class CargoNet extends ChestTerminalNetwork { } SlimefunPlugin.getProfiler().newEntry(); + SlimefunPlugin.getProfiler().scheduleEntries(inputNodes.size()); Slimefun.runSync(() -> run(inputs, outputs, chestTerminalInputs, chestTerminalOutputs)); } } @@ -252,12 +253,16 @@ public class CargoNet extends ChestTerminalNetwork { // All operations happen here: Everything gets iterated from the Input Nodes. // (Apart from ChestTerminal Buses) for (Map.Entry entry : inputs.entrySet()) { + long nodeTimestamp = System.nanoTime(); Location input = entry.getKey(); Optional attachedBlock = getAttachedBlock(input.getBlock()); if (attachedBlock.isPresent()) { routeItems(input, attachedBlock.get(), entry.getValue(), outputs); } + + // This will prevent this timings from showing up for the Cargo Manager + timestamp += SlimefunPlugin.getProfiler().closeEntry(entry.getKey(), SlimefunItems.CARGO_INPUT_NODE.getItem(), nodeTimestamp); } // Chest Terminal Code diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/ChestTerminalNetwork.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/ChestTerminalNetwork.java index 744a06b6a..7ccfa5606 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/ChestTerminalNetwork.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/ChestTerminalNetwork.java @@ -32,6 +32,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils; import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.api.BlockStorage; import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; import me.mrCookieSlime.Slimefun.api.inventory.DirtyChestMenu; @@ -183,7 +184,10 @@ abstract class ChestTerminalNetwork extends Network { } private void collectImportRequests() { + SlimefunItem item = SlimefunItem.getByID("CT_IMPORT_BUS"); + for (Location bus : imports) { + long timestamp = SlimefunPlugin.getProfiler().newEntry(); BlockMenu menu = BlockStorage.getInventory(bus); if (menu.getItemInSlot(17) == null) { @@ -201,11 +205,16 @@ abstract class ChestTerminalNetwork extends Network { if (menu.getItemInSlot(17) != null) { itemRequests.add(new ItemRequest(bus, 17, menu.getItemInSlot(17), ItemTransportFlow.INSERT)); } + + SlimefunPlugin.getProfiler().closeEntry(bus, item, timestamp); } } private void collectExportRequests() { + SlimefunItem item = SlimefunItem.getByID("CT_EXPORT_BUS"); + for (Location bus : exports) { + long timestamp = SlimefunPlugin.getProfiler().newEntry(); BlockMenu menu = BlockStorage.getInventory(bus); if (menu.getItemInSlot(17) != null) { @@ -236,17 +245,24 @@ abstract class ChestTerminalNetwork extends Network { itemRequests.add(new ItemRequest(bus, 17, items.get(index), ItemTransportFlow.WITHDRAW)); } } + + SlimefunPlugin.getProfiler().closeEntry(bus, item, timestamp); } } private void collectTerminalRequests() { + SlimefunItem item = SlimefunItem.getByID("CHEST_TERMINAL"); + for (Location terminal : terminals) { + long timestamp = SlimefunPlugin.getProfiler().newEntry(); BlockMenu menu = BlockStorage.getInventory(terminal); ItemStack sendingItem = menu.getItemInSlot(TERMINAL_OUT_SLOT); if (sendingItem != null) { itemRequests.add(new ItemRequest(terminal, TERMINAL_OUT_SLOT, sendingItem, ItemTransportFlow.INSERT)); } + + SlimefunPlugin.getProfiler().closeEntry(terminal, item, timestamp); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceSummary.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceSummary.java index 788b4a9f4..37c24bd57 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceSummary.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceSummary.java @@ -22,6 +22,8 @@ class PerformanceSummary { // The threshold at which a Block or Chunk is significant enough to appear in /sf timings private static final int VISIBILITY_THRESHOLD = 280_000; + private static final int MIN_ITEMS = 3; + private static final int MAX_ITEMS = 10; // A minecraft server tick is 50ms and Slimefun ticks are stretched across // two ticks (sync and async blocks), so we use 100ms as a reference here @@ -56,7 +58,7 @@ class PerformanceSummary { sender.sendMessage(ChatColor.GOLD + "Performance: " + getPerformanceRating()); sender.sendMessage(""); - summarizeTimings(totalTickedBlocks + " Blocks", sender, items, entry -> { + summarizeTimings(totalTickedBlocks, "block", sender, items, entry -> { int count = profiler.getBlocksOfId(entry.getKey()); String time = NumberUtils.getAsMillis(entry.getValue()); @@ -70,24 +72,25 @@ class PerformanceSummary { } }); - summarizeTimings(chunks.size() + " Chunks", sender, chunks, entry -> { + summarizeTimings(chunks.size(), "chunk", sender, chunks, entry -> { int count = profiler.getBlocksInChunk(entry.getKey()); String time = NumberUtils.getAsMillis(entry.getValue()); - return entry.getKey() + " - " + count + "x Blocks (" + time + ")"; + return entry.getKey() + " - " + count + " block" + (count != 1 ? 's' : "") + " (" + time + ")"; }); - summarizeTimings(plugins.size() + " Plugins", sender, plugins, entry -> { + summarizeTimings(plugins.size(), "plugin", sender, plugins, entry -> { int count = profiler.getBlocksFromPlugin(entry.getKey()); String time = NumberUtils.getAsMillis(entry.getValue()); - return entry.getKey() + " - " + count + "x Blocks (" + time + ")"; + return entry.getKey() + " - " + count + " block" + (count != 1 ? 's' : "") + " (" + time + ")"; }); } - private void summarizeTimings(String prefix, CommandSender sender, Map map, Function, String> formatter) { + private void summarizeTimings(int count, String name, CommandSender sender, Map map, Function, String> formatter) { Stream> stream = map.entrySet().stream(); List> results = stream.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).collect(Collectors.toList()); + String prefix = count + " " + name + (count != 1 ? 's' : ""); if (sender instanceof Player) { TextComponent component = new TextComponent(prefix); @@ -96,11 +99,14 @@ class PerformanceSummary { TextComponent hoverComponent = new TextComponent(" (Hover for details)"); hoverComponent.setColor(ChatColor.GRAY); StringBuilder builder = new StringBuilder(); + + int displayed = 0; int hidden = 0; for (Map.Entry entry : results) { - if (entry.getValue() > VISIBILITY_THRESHOLD) { + if (displayed < MAX_ITEMS && (displayed < MIN_ITEMS || entry.getValue() > VISIBILITY_THRESHOLD)) { builder.append("\n").append(ChatColor.YELLOW).append(formatter.apply(entry)); + displayed++; } else { hidden++; @@ -114,16 +120,19 @@ class PerformanceSummary { sender.spigot().sendMessage(component); } else { + int displayed = 0; int hidden = 0; + StringBuilder builder = new StringBuilder(); builder.append(ChatColor.GOLD); builder.append(prefix); builder.append(ChatColor.YELLOW); for (Map.Entry entry : results) { - if (entry.getValue() > VISIBILITY_THRESHOLD) { + if (displayed < MAX_ITEMS && (displayed < MIN_ITEMS || entry.getValue() > VISIBILITY_THRESHOLD)) { builder.append("\n "); builder.append(ChatColor.stripColor(formatter.apply(entry))); + displayed++; } else { hidden++; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/SlimefunProfiler.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/SlimefunProfiler.java index 7bfa9c733..8c73f0444 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/SlimefunProfiler.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/SlimefunProfiler.java @@ -73,6 +73,18 @@ public class SlimefunProfiler { return System.nanoTime(); } + /** + * This method schedules a given amount of entries for the future. + * Be careful to {@link #closeEntry(Location, SlimefunItem, long)} all of them again! + * No {@link PerformanceSummary} will be sent until all entires were closed. + * + * @param amount + * The amount of entries that should be scheduled. + */ + public void scheduleEntries(int amount) { + queued.getAndAdd(amount); + } + /** * This method closes a previously started entry. * Make sure to call {@link #newEntry()} to get the timestamp in advance. @@ -87,6 +99,9 @@ public class SlimefunProfiler { * @return The total timings of this entry */ public long closeEntry(Location l, SlimefunItem item, long timestamp) { + Validate.notNull(l, "Location must not be null!"); + Validate.notNull(item, "You need to specify a SlimefunItem!"); + if (timestamp == 0) { return 0; } @@ -261,6 +276,19 @@ public class SlimefunProfiler { return NumberUtils.getAsMillis(totalElapsedTime); } + /** + * This method checks whether the {@link SlimefunProfiler} has collected timings on + * the given {@link Block} + * + * @param b The {@link Block} + * + * @return Whether timings of this {@link Block} have been collected + */ + public boolean hasTimings(Block b) { + Validate.notNull("Cannot get timings for a null Block"); + return timings.containsKey(new ProfiledBlock(b)); + } + public String getTime(Block b) { Validate.notNull("Cannot get timings for a null Block"); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Instruction.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Instruction.java index 8cb11286c..a2c70889b 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Instruction.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Instruction.java @@ -41,12 +41,12 @@ enum Instruction { // Directions TURN_LEFT(AndroidType.NONE, HeadTexture.SCRIPT_LEFT, (android, b, inv, face) -> { int mod = -1; - android.rotate(b, mod); + android.rotate(b, face, mod); }), TURN_RIGHT(AndroidType.NONE, HeadTexture.SCRIPT_RIGHT, (android, b, inv, face) -> { int mod = 1; - android.rotate(b, mod); + android.rotate(b, face, mod); }), // Action - Pickaxe diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java index d963bf588..a43094b52 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java @@ -2,7 +2,6 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.androids; import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.function.Predicate; @@ -16,6 +15,7 @@ import org.bukkit.Tag; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.Dispenser; +import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Rotatable; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -49,6 +49,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineFuel; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.interfaces.InventoryBlock; import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker; import me.mrCookieSlime.Slimefun.api.BlockStorage; +import me.mrCookieSlime.Slimefun.api.Slimefun; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset; @@ -198,8 +199,8 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent @Override public void tick(Block b, SlimefunItem item, Config data) { - if (b != null) { - ProgrammableAndroid.this.tick(b); + if (b != null && data != null) { + ProgrammableAndroid.this.tick(b, data); } } @@ -387,23 +388,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent } else { Script script = scripts.get(target); - List lore = new LinkedList<>(); - lore.add("&7by &r" + script.getAuthor()); - lore.add(""); - lore.add("&7Downloads: &r" + script.getDownloads()); - lore.add("&7Rating: " + getScriptRatingPercentage(script)); - lore.add("&a" + script.getUpvotes() + " \u263A &7| &4\u2639 " + script.getDownvotes()); - lore.add(""); - lore.add("&eLeft Click &rto download this Script"); - lore.add("&4(This will override your current Script)"); - - if (script.canRate(p)) { - lore.add("&eShift + Left Click &rto leave a positive Rating"); - lore.add("&eShift + Right Click &rto leave a negative Rating"); - } - - ItemStack item = new CustomItem(getItem(), "&b" + script.getName(), lore.toArray(new String[0])); - menu.addItem(index, item, (player, slot, stack, action) -> { + menu.addItem(index, script.getAsItemStack(this, p), (player, slot, stack, action) -> { if (action.isShiftClicked()) { if (script.isAuthor(player)) { SlimefunPlugin.getLocalization().sendMessage(player, "android.scripts.rating.own", true); @@ -509,11 +494,6 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent return list; } - protected String getScriptRatingPercentage(Script script) { - float percentage = script.getRating(); - return NumberUtils.getColorFromPercentage(percentage) + String.valueOf(percentage) + ChatColor.RESET + "% "; - } - protected void editInstruction(Player p, Block b, String[] script, int index) { ChestMenu menu = new ChestMenu(ChatColor.DARK_AQUA + SlimefunPlugin.getLocalization().getMessage(p, "android.scripts.editor")); ChestMenuUtils.drawBackground(menu, 0, 1, 2, 3, 4, 5, 6, 7, 8); @@ -627,23 +607,26 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent public abstract int getTier(); - protected void tick(Block b) { + protected void tick(Block b, Config data) { if (b.getType() != Material.PLAYER_HEAD) { // The Android was destroyed or moved. return; } - if ("false".equals(BlockStorage.getLocationInfo(b.getLocation(), "paused"))) { + if ("false".equals(data.getString("paused"))) { BlockMenu menu = BlockStorage.getInventory(b); - float fuel = Float.parseFloat(BlockStorage.getLocationInfo(b.getLocation(), "fuel")); + String fuelData = data.getString("fuel"); + float fuel = fuelData == null ? 0 : Float.parseFloat(fuelData); if (fuel < 0.001) { consumeFuel(b, menu); } else { - String[] script = PatternUtils.DASH.split(BlockStorage.getLocationInfo(b.getLocation(), "script")); + String code = data.getString("script"); + String[] script = PatternUtils.DASH.split(code == null ? DEFAULT_SCRIPT : code); - int index = Integer.parseInt(BlockStorage.getLocationInfo(b.getLocation(), "index")) + 1; + String indexData = data.getString("index"); + int index = (indexData == null ? 0 : Integer.parseInt(indexData)) + 1; if (index >= script.length) { index = 0; } @@ -653,7 +636,8 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent Instruction instruction = Instruction.valueOf(script[index]); if (getAndroidType().isType(instruction.getRequiredType())) { - BlockFace face = BlockFace.valueOf(BlockStorage.getLocationInfo(b.getLocation(), "rotation")); + String rotationData = data.getString("rotation"); + BlockFace face = rotationData == null ? BlockFace.NORTH : BlockFace.valueOf(rotationData); switch (instruction) { case START: @@ -679,8 +663,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent } } - protected void rotate(Block b, int mod) { - BlockFace current = BlockFace.valueOf(BlockStorage.getLocationInfo(b.getLocation(), "rotation")); + protected void rotate(Block b, BlockFace current, int mod) { int index = POSSIBLE_ROTATIONS.indexOf(current) + mod; if (index == POSSIBLE_ROTATIONS.size()) { @@ -693,7 +676,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent BlockFace rotation = POSSIBLE_ROTATIONS.get(index); Rotatable rotatatable = (Rotatable) b.getBlockData(); - rotatatable.setRotation(rotation); + rotatatable.setRotation(rotation.getOppositeFace()); b.setBlockData(rotatatable); BlockStorage.addBlockInfo(b, "rotation", rotation.name()); } @@ -739,7 +722,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent dispenser.setItem(slot, null); return true; } - else if (SlimefunUtils.isItemSimilar(newFuel, currentFuel, true)) { + else if (SlimefunUtils.isItemSimilar(newFuel, currentFuel, true, false)) { int rest = newFuel.getType().getMaxStackSize() - currentFuel.getAmount(); if (rest > 0) { @@ -766,7 +749,8 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent menu.pushItem(new ItemStack(Material.BUCKET), getOutputSlots()); } - BlockStorage.addBlockInfo(b, "fuel", String.valueOf((int) (fuel.getTicks() * this.getFuelEfficiency()))); + int fuelLevel = (int) (fuel.getTicks() * getFuelEfficiency()); + BlockStorage.addBlockInfo(b, "fuel", String.valueOf(fuelLevel)); break; } } @@ -809,12 +793,15 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent protected void move(Block b, BlockFace face, Block block) { if (block.getY() > 0 && block.getY() < block.getWorld().getMaxHeight() && (block.getType() == Material.AIR || block.getType() == Material.CAVE_AIR)) { - block.setType(Material.PLAYER_HEAD); - Rotatable blockData = (Rotatable) block.getBlockData(); - blockData.setRotation(face.getOppositeFace()); - block.setBlockData(blockData); + BlockData blockData = Material.PLAYER_HEAD.createBlockData(data -> { + if (data instanceof Rotatable) { + Rotatable rotatable = ((Rotatable) data); + rotatable.setRotation(face.getOppositeFace()); + } + }); - SkullBlock.setFromBase64(block, texture); + block.setBlockData(blockData); + Slimefun.runSync(() -> SkullBlock.setFromBase64(block, texture)); b.setType(Material.AIR); BlockStorage.moveBlockInfo(b.getLocation(), block.getLocation()); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Script.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Script.java index f075fcd86..783cff824 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Script.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Script.java @@ -11,11 +11,15 @@ import java.util.logging.Level; import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.cscorelib2.config.Config; +import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.slimefun4.utils.ChatUtils; +import io.github.thebusybiscuit.slimefun4.utils.NumberUtils; import me.mrCookieSlime.Slimefun.api.Slimefun; public final class Script { @@ -105,6 +109,30 @@ public final class Script { return !upvoters.contains(p.getUniqueId().toString()) && !downvoters.contains(p.getUniqueId().toString()); } + ItemStack getAsItemStack(ProgrammableAndroid android, Player p) { + List lore = new LinkedList<>(); + lore.add("&7by &r" + getAuthor()); + lore.add(""); + lore.add("&7Downloads: &r" + getDownloads()); + lore.add("&7Rating: " + getScriptRatingPercentage()); + lore.add("&a" + getUpvotes() + " \u263A &7| &4\u2639 " + getDownvotes()); + lore.add(""); + lore.add("&eLeft Click &rto download this Script"); + lore.add("&4(This will override your current Script)"); + + if (canRate(p)) { + lore.add("&eShift + Left Click &rto leave a positive Rating"); + lore.add("&eShift + Right Click &rto leave a negative Rating"); + } + + return new CustomItem(android.getItem(), "&b" + getName(), lore.toArray(new String[0])); + } + + private String getScriptRatingPercentage() { + float percentage = getRating(); + return NumberUtils.getColorFromPercentage(percentage) + String.valueOf(percentage) + ChatColor.RESET + "% "; + } + /** * This method returns the amount of upvotes this {@link Script} has received. * diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/SolarGenerator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/SolarGenerator.java index f66ce9036..aea9f3d5e 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/SolarGenerator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/SolarGenerator.java @@ -68,11 +68,18 @@ public abstract class SolarGenerator extends SimpleSlimefunItem return 0; } + boolean isDaytime = isDaytime(world); + + // Performance optimization for daytime-only solar generators + if (!isDaytime && getNightEnergy() == 0) { + return 0; + } + if (!world.isChunkLoaded(l.getBlockX() >> 4, l.getBlockZ() >> 4) || l.getBlock().getLightFromSky() != 15) { return 0; } - if (world.getTime() < 12300 || world.getTime() > 23850) { + if (isDaytime) { return getDayEnergy(); } @@ -86,6 +93,19 @@ public abstract class SolarGenerator extends SimpleSlimefunItem }; } + /** + * This method returns whether a given {@link World} has daytime. + * It will also return false if a thunderstorm is active in this world. + * + * @param world + * The {@link World} to check + * + * @return Whether the given {@link World} has daytime and no active thunderstorm + */ + private boolean isDaytime(World world) { + return !world.hasStorm() && !world.isThundering() && (world.getTime() < 12300 || world.getTime() > 23850); + } + @Override public void preRegister() { super.preRegister(); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoAnvil.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoAnvil.java index 14e2a570e..831583010 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoAnvil.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoAnvil.java @@ -57,12 +57,12 @@ public abstract class AutoAnvil extends AContainer { if (timeleft > 0) { ChestMenuUtils.updateProgressbar(menu, 22, timeleft, processing.get(b).getTicks(), getProgressBar()); - if (ChargableBlock.isChargable(b)) { - if (ChargableBlock.getCharge(b) < getEnergyConsumption()) return; - ChargableBlock.addCharge(b, -getEnergyConsumption()); - progress.put(b, timeleft - 1); + if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { + return; } - else progress.put(b, timeleft - 1); + + ChargableBlock.addCharge(b, -getEnergyConsumption()); + progress.put(b, timeleft - 1); } else { menu.replaceExistingItem(22, new CustomItem(new ItemStack(Material.BLACK_STAINED_GLASS_PANE), " ")); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoBrewer.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoBrewer.java index c25218602..85cb3d60e 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoBrewer.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoBrewer.java @@ -68,17 +68,12 @@ public abstract class AutoBrewer extends AContainer { if (timeleft > 0) { ChestMenuUtils.updateProgressbar(menu, 22, timeleft, processing.get(b).getTicks(), getProgressBar()); - if (ChargableBlock.isChargable(b)) { - if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { - return; - } + if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { + return; + } - ChargableBlock.addCharge(b, -getEnergyConsumption()); - progress.put(b, timeleft - 1); - } - else { - progress.put(b, timeleft - 1); - } + ChargableBlock.addCharge(b, -getEnergyConsumption()); + progress.put(b, timeleft - 1); } else { menu.replaceExistingItem(22, new CustomItem(new ItemStack(Material.BLACK_STAINED_GLASS_PANE), " ")); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoDisenchanter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoDisenchanter.java index 92621dafb..ff9b03605 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoDisenchanter.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoDisenchanter.java @@ -78,15 +78,12 @@ public class AutoDisenchanter extends AContainer { if (timeleft > 0) { ChestMenuUtils.updateProgressbar(menu, 22, timeleft, processing.get(b).getTicks(), getProgressBar()); - if (ChargableBlock.isChargable(b)) { - if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { - return; - } - - ChargableBlock.addCharge(b, -getEnergyConsumption()); - progress.put(b, timeleft - 1); + if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { + return; } - else progress.put(b, timeleft - 1); + + ChargableBlock.addCharge(b, -getEnergyConsumption()); + progress.put(b, timeleft - 1); } else { menu.replaceExistingItem(22, new CustomItem(Material.BLACK_STAINED_GLASS_PANE, " ")); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoDrier.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoDrier.java index ffdcb4c6c..c25892873 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoDrier.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoDrier.java @@ -104,12 +104,12 @@ public class AutoDrier extends AContainer implements RecipeDisplayItem { if (timeleft > 0) { ChestMenuUtils.updateProgressbar(menu, 22, timeleft, processing.get(b).getTicks(), getProgressBar()); - if (ChargableBlock.isChargable(b)) { - if (ChargableBlock.getCharge(b) < getEnergyConsumption()) return; - ChargableBlock.addCharge(b, -getEnergyConsumption()); - progress.put(b, timeleft - 1); + if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { + return; } - else progress.put(b, timeleft - 1); + + ChargableBlock.addCharge(b, -getEnergyConsumption()); + progress.put(b, timeleft - 1); } else { menu.replaceExistingItem(22, new CustomItem(new ItemStack(Material.BLACK_STAINED_GLASS_PANE), " ")); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoEnchanter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoEnchanter.java index c0db898c7..afcb3f41f 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoEnchanter.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoEnchanter.java @@ -67,17 +67,12 @@ public class AutoEnchanter extends AContainer { if (timeleft > 0) { ChestMenuUtils.updateProgressbar(menu, 22, timeleft, processing.get(b).getTicks(), getProgressBar()); - if (ChargableBlock.isChargable(b)) { - if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { - return; - } + if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { + return; + } - ChargableBlock.addCharge(b, -getEnergyConsumption()); - progress.put(b, timeleft - 1); - } - else { - progress.put(b, timeleft - 1); - } + ChargableBlock.addCharge(b, -getEnergyConsumption()); + progress.put(b, timeleft - 1); } else { menu.replaceExistingItem(22, new CustomItem(new ItemStack(Material.BLACK_STAINED_GLASS_PANE), " ")); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricDustWasher.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricDustWasher.java index 011d8fef8..4f037164e 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricDustWasher.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricDustWasher.java @@ -59,18 +59,14 @@ public abstract class ElectricDustWasher extends AContainer { if (timeleft > 0 && getSpeed() < 10) { ChestMenuUtils.updateProgressbar(menu, 22, timeleft, processing.get(b).getTicks(), getProgressBar()); - if (ChargableBlock.isChargable(b)) { - if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { - return; - } - ChargableBlock.addCharge(b, -getEnergyConsumption()); - progress.put(b, timeleft - 1); - } - else { - progress.put(b, timeleft - 1); + if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { + return; } + + ChargableBlock.addCharge(b, -getEnergyConsumption()); + progress.put(b, timeleft - 1); } - else if (ChargableBlock.isChargable(b)) { + else { if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { return; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricGoldPan.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricGoldPan.java index 1564d400e..f8e75b2e4 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricGoldPan.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricGoldPan.java @@ -63,19 +63,14 @@ public abstract class ElectricGoldPan extends AContainer implements RecipeDispla if (timeleft > 0 && getSpeed() < 10) { ChestMenuUtils.updateProgressbar(menu, 22, timeleft, processing.get(b).getTicks(), getProgressBar()); - if (ChargableBlock.isChargable(b)) { - if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { - return; - } + if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { + return; + } - ChargableBlock.addCharge(b, -getEnergyConsumption()); - progress.put(b, timeleft - 1); - } - else { - progress.put(b, timeleft - 1); - } + ChargableBlock.addCharge(b, -getEnergyConsumption()); + progress.put(b, timeleft - 1); } - else if (ChargableBlock.isChargable(b)) { + else { if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { return; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricIngotPulverizer.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricIngotPulverizer.java index 14993cfac..3fb9aec88 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricIngotPulverizer.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/ElectricIngotPulverizer.java @@ -53,10 +53,9 @@ public class ElectricIngotPulverizer extends AContainer implements RecipeDisplay @Override protected void registerDefaultRecipes() { - // this is an extra recipe on top of PostSetup.loadSmelteryRecipes() for converting Vanilla Gold Ingot to Slimefun gold dust - registerRecipe(3, - new ItemStack(Material.GOLD_INGOT), - SlimefunItems.GOLD_DUST); + // this is an extra recipe on top of PostSetup.loadSmelteryRecipes() for converting + // Vanilla Gold Ingot to Slimefun gold dust + registerRecipe(3, new ItemStack(Material.GOLD_INGOT), SlimefunItems.GOLD_DUST); } @Override diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/HeatedPressureChamber.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/HeatedPressureChamber.java index 2fbea6f3b..e13a6b319 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/HeatedPressureChamber.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/HeatedPressureChamber.java @@ -148,17 +148,12 @@ public abstract class HeatedPressureChamber extends AContainer { if (timeleft > 0) { ChestMenuUtils.updateProgressbar(menu, 22, timeleft, processing.get(b).getTicks(), getProgressBar()); - if (ChargableBlock.isChargable(b)) { - if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { - return; - } + if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { + return; + } - ChargableBlock.addCharge(b, -getEnergyConsumption()); - progress.put(b, timeleft - 1); - } - else { - progress.put(b, timeleft - 1); - } + ChargableBlock.addCharge(b, -getEnergyConsumption()); + progress.put(b, timeleft - 1); } else { menu.replaceExistingItem(22, new CustomItem(new ItemStack(Material.BLACK_STAINED_GLASS_PANE), " ")); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockPhysicsListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockPhysicsListener.java index 0b69c00a6..51a0018ec 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockPhysicsListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockPhysicsListener.java @@ -89,6 +89,7 @@ public class BlockPhysicsListener implements Listener { public void onBucketUse(PlayerBucketEmptyEvent e) { // Fix for placing water on player heads Location l = e.getBlockClicked().getRelative(e.getBlockFace()).getLocation(); + if (BlockStorage.hasBlockInfo(l)) { e.setCancelled(true); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/CargoNodeListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/CargoNodeListener.java index 57e19ff1c..ae00e218a 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/CargoNodeListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/CargoNodeListener.java @@ -9,6 +9,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils; import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; /** * This {@link Listener} is solely responsible for preventing Cargo Nodes from being placed @@ -25,15 +26,29 @@ public class CargoNodeListener implements Listener { @EventHandler(ignoreCancelled = true) public void onCargoNodePlace(BlockPlaceEvent e) { - if (e.getBlock().getY() != e.getBlockAgainst().getY() && isCargoNode(new ItemStackWrapper(e.getItemInHand()))) { + if (e.getBlock().getY() != e.getBlockAgainst().getY() && isCargoNode(e.getItemInHand())) { SlimefunPlugin.getLocalization().sendMessage(e.getPlayer(), "machines.CARGO_NODES.must-be-placed", true); e.setCancelled(true); } } private boolean isCargoNode(ItemStack item) { - return SlimefunUtils.isItemSimilar(item, SlimefunItems.CARGO_INPUT_NODE, false) - || SlimefunUtils.isItemSimilar(item, SlimefunItems.CARGO_OUTPUT_NODE, false) - || SlimefunUtils.isItemSimilar(item, SlimefunItems.CARGO_OUTPUT_NODE_2, false); + if (SlimefunPlugin.getRegistry().isBackwardsCompatible()) { + ItemStackWrapper wrapper = new ItemStackWrapper(item); + + return SlimefunUtils.isItemSimilar(wrapper, SlimefunItems.CARGO_INPUT_NODE, false) + || SlimefunUtils.isItemSimilar(wrapper, SlimefunItems.CARGO_OUTPUT_NODE, false) + || SlimefunUtils.isItemSimilar(wrapper, SlimefunItems.CARGO_OUTPUT_NODE_2, false); + } + + SlimefunItem sfItem = SlimefunItem.getByItem(item); + + if (sfItem == null) { + return false; + } + + return sfItem.getID().equals(SlimefunItems.CARGO_INPUT_NODE.getItemId()) + || sfItem.getID().equals(SlimefunItems.CARGO_OUTPUT_NODE.getItemId()) + || sfItem.getID().equals(SlimefunItems.CARGO_OUTPUT_NODE_2.getItemId()); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/DebugFishListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/DebugFishListener.java index d2ff3e424..1a3a886e9 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/DebugFishListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/DebugFishListener.java @@ -98,21 +98,21 @@ public class DebugFishListener implements Listener { } if (item.isTicking()) { - p.sendMessage(ChatColors.color("&dTicker: " + greenCheckmark)); + p.sendMessage(ChatColors.color("&dTicking: " + greenCheckmark)); p.sendMessage(ChatColors.color(" &dAsync: &e" + (item.getBlockTicker().isSynchronized() ? redCross : greenCheckmark))); + } + else if (item.getEnergyTicker() != null) { + p.sendMessage(ChatColors.color("&dTicking: &3Indirect")); + } + else { + p.sendMessage(ChatColors.color("&dTicking: " + redCross)); + } + + if (SlimefunPlugin.getProfiler().hasTimings(b)) { p.sendMessage(ChatColors.color(" &dTimings: &e" + SlimefunPlugin.getProfiler().getTime(b))); p.sendMessage(ChatColors.color(" &dTotal Timings: &e" + SlimefunPlugin.getProfiler().getTime(item))); p.sendMessage(ChatColors.color(" &dChunk Timings: &e" + SlimefunPlugin.getProfiler().getTime(b.getChunk()))); } - else if (item.getEnergyTicker() != null) { - p.sendMessage(ChatColors.color("&dTicking: " + "&3Indirect")); - p.sendMessage(ChatColors.color(" &dTimings: &e" + SlimefunPlugin.getProfiler().getTime(b))); - p.sendMessage(ChatColors.color(" &dChunk Timings: &e" + SlimefunPlugin.getProfiler().getTime(b.getChunk()))); - } - else { - p.sendMessage(ChatColors.color("&dTicker: " + redCross)); - p.sendMessage(ChatColors.color("&dTicking: " + redCross)); - } if (ChargableBlock.isChargable(b)) { p.sendMessage(ChatColors.color("&dChargeable: " + greenCheckmark)); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChestMenuUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChestMenuUtils.java index bebdaf7ab..22fb6b61b 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChestMenuUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/ChestMenuUtils.java @@ -6,6 +6,7 @@ import java.util.List; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; @@ -100,6 +101,13 @@ public final class ChestMenuUtils { } public static void updateProgressbar(ChestMenu menu, int slot, int timeLeft, int time, ItemStack indicator) { + Inventory inv = menu.toInventory(); + + // We don't need to update the progress bar if noone is watching :o + if (inv == null || inv.getViewers().isEmpty()) { + return; + } + ItemStack item = indicator.clone(); ItemMeta im = item.getItemMeta(); im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/NumberUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/NumberUtils.java index 15e9f15fc..dbf6a0c74 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/NumberUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/NumberUtils.java @@ -77,10 +77,10 @@ public final class NumberUtils { String[] parts = PatternUtils.NUMBER_SEPERATOR.split(number); if (parts.length == 1) { - return parts[0]; + return parts[0] + "ms"; } else { - return parts[0] + ',' + parts[1] + "ms"; + return parts[0] + '.' + parts[1] + "ms"; } } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java index 492aa39c1..32f22f887 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java @@ -589,7 +589,7 @@ public class SlimefunItem implements Placeable { } // Backwards compatibility - if (SlimefunPlugin.getMinecraftVersion().isBefore(MinecraftVersion.MINECRAFT_1_14) || SlimefunPlugin.getRegistry().isBackwardsCompatible()) { + if (SlimefunPlugin.getRegistry().isBackwardsCompatible()) { boolean loreInsensitive = this instanceof Rechargeable || this instanceof SlimefunBackpack || id.equals("BROKEN_SPAWNER") || id.equals("REINFORCED_SPAWNER"); return SlimefunUtils.isItemSimilar(item, this.item, !loreInsensitive); } @@ -863,7 +863,7 @@ public class SlimefunItem implements Placeable { } // Backwards compatibility - if (SlimefunPlugin.getMinecraftVersion().isBefore(MinecraftVersion.MINECRAFT_1_14) || SlimefunPlugin.getRegistry().isBackwardsCompatible()) { + if (SlimefunPlugin.getRegistry().isBackwardsCompatible()) { // This wrapper improves the heavy ItemStack#getItemMeta() call by caching it. ItemStackWrapper wrapper = new ItemStackWrapper(item); diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java index ad84a1ac9..89ff25612 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java @@ -17,6 +17,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.AbstractEnergyProvider; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils; +import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper; import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.AdvancedMenuClickHandler; import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ClickAction; @@ -143,7 +144,7 @@ public abstract class AGenerator extends AbstractEnergyProvider { @Override public double generateEnergy(Location l, SlimefunItem sf, Config data) { BlockMenu inv = BlockStorage.getInventory(l); - boolean chargeable = ChargableBlock.isChargable(l); + boolean chargeable = getCapacity() > 0; int charge = chargeable ? ChargableBlock.getCharge(l) : 0; if (isProcessing(l)) { @@ -169,7 +170,7 @@ public abstract class AGenerator extends AbstractEnergyProvider { else { ItemStack fuel = processing.get(l).getInput(); - if (SlimefunUtils.isItemSimilar(fuel, new ItemStack(Material.LAVA_BUCKET), true) || SlimefunUtils.isItemSimilar(fuel, SlimefunItems.FUEL_BUCKET, true) || SlimefunUtils.isItemSimilar(fuel, SlimefunItems.OIL_BUCKET, true)) { + if (isBucket(fuel)) { inv.pushItem(new ItemStack(Material.BUCKET), getOutputSlots()); } @@ -204,6 +205,15 @@ public abstract class AGenerator extends AbstractEnergyProvider { }; } + private boolean isBucket(ItemStack item) { + if (item == null) { + return false; + } + + ItemStackWrapper wrapper = new ItemStackWrapper(item); + return SlimefunUtils.isItemSimilar(wrapper, new ItemStack(Material.LAVA_BUCKET), true) || SlimefunUtils.isItemSimilar(wrapper, SlimefunItems.FUEL_BUCKET, true) || SlimefunUtils.isItemSimilar(wrapper, SlimefunItems.OIL_BUCKET, true); + } + private MachineFuel findRecipe(BlockMenu menu, Map found) { for (MachineFuel fuel : fuelTypes) { for (int slot : getInputSlots()) { diff --git a/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java b/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java index 8f1929daa..70ebe6b12 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java @@ -386,17 +386,29 @@ public class BlockStorage { * @since 4.0 */ public static ItemStack retrieve(Block block) { - if (!hasBlockInfo(block)) return null; + if (!hasBlockInfo(block)) { + return null; + } else { - final SlimefunItem item = SlimefunItem.getByID(getLocationInfo(block.getLocation(), "id")); + SlimefunItem item = SlimefunItem.getByID(getLocationInfo(block.getLocation(), "id")); clearBlockInfo(block); - if (item == null) return null; - else return item.getItem(); + + if (item == null) { + return null; + } + else { + return item.getItem(); + } } } public static Config getLocationInfo(Location l) { BlockStorage storage = getStorage(l.getWorld()); + + if (storage == null) { + return emptyBlockData; + } + Config cfg = storage.storage.get(l); return cfg == null ? emptyBlockData : cfg; } @@ -480,7 +492,14 @@ public class BlockStorage { public static boolean hasBlockInfo(Location l) { BlockStorage storage = getStorage(l.getWorld()); - return storage != null && storage.storage.containsKey(l) && getLocationInfo(l, "id") != null; + + if (storage != null) { + Config cfg = storage.storage.get(l); + return cfg != null && cfg.getString("id") != null; + } + else { + return false; + } } public static void setBlockInfo(Block block, Config cfg, boolean updateTicker) { @@ -489,12 +508,13 @@ public class BlockStorage { public static void setBlockInfo(Location l, Config cfg, boolean updateTicker) { BlockStorage storage = getStorage(l.getWorld()); + if (storage == null) { Slimefun.getLogger().warning("Could not set Block info for non-registered World '" + l.getWorld().getName() + "'. Is some plugin trying to store data in a fake world?"); return; } - storage.storage.put(l, cfg); + storage.storage.put(l, cfg); String id = cfg.getString("id"); if (BlockMenuPreset.isInventory(id)) { @@ -505,8 +525,13 @@ public class BlockStorage { } else if (!storage.hasInventory(l)) { File file = new File(PATH_INVENTORIES + serializeLocation(l) + ".sfi"); - if (file.exists()) storage.inventories.put(l, new BlockMenu(BlockMenuPreset.getPreset(id), l, new io.github.thebusybiscuit.cscorelib2.config.Config(file))); - else storage.loadInventory(l, BlockMenuPreset.getPreset(id)); + + if (file.exists()) { + storage.inventories.put(l, new BlockMenu(BlockMenuPreset.getPreset(id), l, new io.github.thebusybiscuit.cscorelib2.config.Config(file))); + } + else { + storage.loadInventory(l, BlockMenuPreset.getPreset(id)); + } } } @@ -590,8 +615,8 @@ public class BlockStorage { } BlockStorage storage = getStorage(from.getWorld()); - - setBlockInfo(to, getLocationInfo(from), true); + Config previousData = getLocationInfo(from); + setBlockInfo(to, previousData, true); if (storage.inventories.containsKey(from)) { BlockMenu menu = storage.inventories.get(from); @@ -600,7 +625,7 @@ public class BlockStorage { menu.move(to); } - refreshCache(storage, from, getLocationInfo(from).getString("id"), null, true); + refreshCache(storage, from, previousData.getString("id"), null, true); storage.storage.remove(from); String chunkString = locationToChunkString(from); @@ -636,11 +661,9 @@ public class BlockStorage { String chunkString = locationToChunkString(l); if (value != null) { - Set locations = SlimefunPlugin.getRegistry().getActiveTickers().get(chunkString); - if (locations == null) locations = new HashSet<>(); - + Set locations = SlimefunPlugin.getRegistry().getActiveTickers().computeIfAbsent(chunkString, c -> new HashSet<>()); locations.add(l); - SlimefunPlugin.getRegistry().getActiveTickers().put(chunkString, locations); + SlimefunPlugin.getRegistry().getActiveChunks().add(chunkString); } } @@ -797,7 +820,10 @@ public class BlockStorage { public static BlockMenu getInventory(Location l) { BlockStorage storage = getStorage(l.getWorld()); - if (storage == null) return null; + + if (storage == null) { + return null; + } BlockMenu menu = storage.inventories.get(l); diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/listeners/TestCargoNodeListener.java b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/listeners/TestCargoNodeListener.java index f878de7aa..bdf2e6d9b 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/listeners/TestCargoNodeListener.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/listeners/TestCargoNodeListener.java @@ -55,6 +55,7 @@ public class TestCargoNodeListener { @ParameterizedTest @SlimefunItemsSource(items = { "CARGO_INPUT_NODE", "CARGO_OUTPUT_NODE", "CARGO_OUTPUT_NODE_2" }) public void testInvalidPlacement(ItemStack item) { + SlimefunPlugin.getRegistry().setBackwardsCompatible(true); Player player = server.addPlayer(); Location l = new Location(player.getWorld(), 190, 50, 400); Block b = l.getBlock(); @@ -63,10 +64,12 @@ public class TestCargoNodeListener { BlockPlaceEvent event = new BlockPlaceEvent(b, new BlockStateMock(), against, item, player, true, EquipmentSlot.HAND); listener.onCargoNodePlace(event); Assertions.assertTrue(event.isCancelled()); + SlimefunPlugin.getRegistry().setBackwardsCompatible(false); } @Test public void testNonCargoNode() { + SlimefunPlugin.getRegistry().setBackwardsCompatible(true); Player player = server.addPlayer(); Location l = new Location(player.getWorld(), 190, 50, 400); Block b = l.getBlock(); @@ -77,6 +80,7 @@ public class TestCargoNodeListener { BlockPlaceEvent event = new BlockPlaceEvent(b, new BlockStateMock(), against, item, player, true, EquipmentSlot.HAND); listener.onCargoNodePlace(event); Assertions.assertFalse(event.isCancelled()); + SlimefunPlugin.getRegistry().setBackwardsCompatible(false); } }