From 399359ae253806b435aed212ac41b9b1dfacd9e2 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Mon, 3 May 2021 15:19:33 +0200 Subject: [PATCH 01/64] Reduced memory usage for networks and enforce world limitation --- CHANGELOG.md | 1 + pom.xml | 6 ++-- .../slimefun4/api/network/Network.java | 33 ++++++++++++++++--- .../items/blocks/OutputChest.java | 19 +++++++++++ 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff1986c1c..758afc04e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ * Magical Glass can no longer be placed * (API) Removed deprecated "SlimefunBlockHandler" * Removed Automated Crafting Chamber +* Memory and performance improvements for Cargo and Energy networks #### Fixes * Fixed #2987 diff --git a/pom.xml b/pom.xml index 38bcaa301..f46296bf9 100644 --- a/pom.xml +++ b/pom.xml @@ -368,9 +368,9 @@ - com.github.TheBusyBiscuit + com.github.thebusybiscuit CS-CoreLib2 - 0.30.4 + 0.31.0 compile @@ -424,7 +424,7 @@ - com.gmail.nossr50.mcMMO + com.gmail.nossr50.mcmmo mcMMO 2.1.196 provided diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/Network.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/Network.java index 015279689..fa53e78ca 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/Network.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/Network.java @@ -4,6 +4,7 @@ import java.util.ArrayDeque; import java.util.HashSet; import java.util.Queue; import java.util.Set; +import java.util.UUID; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -11,7 +12,9 @@ import javax.annotation.Nullable; import org.apache.commons.lang.Validate; import org.bukkit.Location; import org.bukkit.Particle; +import org.bukkit.World; +import io.github.thebusybiscuit.cscorelib2.blocks.BlockPosition; import io.github.thebusybiscuit.slimefun4.core.networks.NetworkManager; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.listeners.NetworkListener; @@ -20,6 +23,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.listeners.NetworkListen * An abstract Network class to manage networks in a stateful way * * @author meiamsome + * @author TheBusyBiscuit * * @see NetworkListener * @see NetworkManager @@ -37,8 +41,19 @@ public abstract class Network { */ protected Location regulator; + /** + * The {@link UUID} of the {@link World} this {@link Network} exists within. + */ + private final UUID worldId; + + /** + * This {@link Set} holds all {@link Network} positions that are part of this {@link Network}. + * The {@link World} should be equal for all positions, therefore we can save memory by simply + * storing {@link BlockPosition#getAsLong(int, int, int)}. + */ + private final Set positions = new HashSet<>(); + private final Queue nodeQueue = new ArrayDeque<>(); - protected final Set connectedLocations = new HashSet<>(); protected final Set regulatorNodes = new HashSet<>(); protected final Set connectorNodes = new HashSet<>(); protected final Set terminusNodes = new HashSet<>(); @@ -57,8 +72,9 @@ public abstract class Network { this.manager = manager; this.regulator = regulator; + this.worldId = regulator.getWorld().getUID(); - connectedLocations.add(regulator); + positions.add(BlockPosition.getAsLong(regulator)); nodeQueue.add(regulator.clone()); } @@ -115,7 +131,10 @@ public abstract class Network { * The {@link Location} to add */ protected void addLocationToNetwork(@Nonnull Location l) { - if (connectedLocations.add(l.clone())) { + Validate.notNull(l, "You cannot add a Location to a Network which is null!"); + Validate.isTrue(l.getWorld().getUID().equals(worldId), "Networks cannot exist in multiple worlds!"); + + if (positions.add(BlockPosition.getAsLong(l))) { markDirty(l); } } @@ -144,10 +163,14 @@ public abstract class Network { * @return Whether the given {@link Location} is part of this {@link Network} */ public boolean connectsTo(@Nonnull Location l) { - if (regulator.equals(l)) { + Validate.notNull(l, "The Location cannot be null."); + + if (this.regulator.equals(l)) { return true; + } else if (!l.getWorld().getUID().equals(this.worldId)) { + return false; } else { - return connectedLocations.contains(l); + return positions.contains(BlockPosition.getAsLong(l)); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/OutputChest.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/OutputChest.java index fb4a5d380..26eefc52f 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/OutputChest.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/OutputChest.java @@ -5,6 +5,7 @@ import java.util.Optional; import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import org.apache.commons.lang.Validate; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -50,8 +51,25 @@ public class OutputChest extends SlimefunItem { addItemHandler(new VanillaInventoryDropHandler<>(Chest.class)); } + /** + * This method checks if an {@link OutputChest} is placed next to the given {@link Block}. + * The returned object is an {@link Optional}. + * This {@link Optional} will be empty if no {@link OutputChest} was found or if no {@link OutputChest} + * could fit the {@link ItemStack} inside. + * Otherwise the {@link Inventory} of the {@link OutputChest} will be returned (as an {@link Optional}). + * + * @param b + * The {@link Block} from which to look for an adjacent {@link OutputChest} + * @param item + * The {@link ItemStack} to insert + * + * @return An {@link Optional} describing the result + */ @Nonnull public static Optional findOutputChestFor(@Nonnull Block b, @Nonnull ItemStack item) { + Validate.notNull(b, "The target block must not be null!"); + Validate.notNull(item, "The ItemStack should not be null!"); + for (BlockFace face : possibleFaces) { Block potentialOutput = b.getRelative(face); @@ -76,6 +94,7 @@ public class OutputChest extends SlimefunItem { } } + // No Inventory was found return Optional.empty(); } From 7b615a66f277875ff344f7edf69f7c4fad8a7fa1 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Mon, 3 May 2021 15:22:13 +0200 Subject: [PATCH 02/64] Fixes #3013 --- CHANGELOG.md | 1 + .../implementation/listeners/NetworkListener.java | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 758afc04e..8b4535d87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ * Fixed #2937 * Fixed #3007 * Fixed #3012 +* Fixed #3013 ## Release Candidate 22 (18 Apr 2021) https://thebusybiscuit.github.io/builds/TheBusyBiscuit/Slimefun4/stable/#22 diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/NetworkListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/NetworkListener.java index 98b55adfb..404f803ec 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/NetworkListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/NetworkListener.java @@ -2,12 +2,14 @@ package io.github.thebusybiscuit.slimefun4.implementation.listeners; import javax.annotation.Nonnull; +import org.bukkit.block.Block; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; +import io.github.thebusybiscuit.slimefun4.api.events.ExplosiveToolBreakBlocksEvent; import io.github.thebusybiscuit.slimefun4.api.network.Network; import io.github.thebusybiscuit.slimefun4.core.networks.NetworkManager; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; @@ -39,4 +41,12 @@ public class NetworkListener implements Listener { public void onBlockPlace(BlockPlaceEvent e) { manager.updateAllNetworks(e.getBlock().getLocation()); } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onExplosiveToolUse(ExplosiveToolBreakBlocksEvent e) { + // Fixes #3013 - Also update networks when using an explosive tool + for (Block b : e.getAdditionalBlocks()) { + manager.updateAllNetworks(b.getLocation()); + } + } } From 0b55c9e8a0ac393034847f436e6dcc59bd39aace Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Mon, 3 May 2021 15:24:19 +0200 Subject: [PATCH 03/64] Updated author tag --- .../slimefun4/implementation/listeners/NetworkListener.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/NetworkListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/NetworkListener.java index 404f803ec..605fc2022 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/NetworkListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/NetworkListener.java @@ -18,6 +18,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; * This {@link Listener} is responsible for all updates to a {@link Network}. * * @author meiamsome + * @author TheBusyBiscuit * * @see Network * @see NetworkManager @@ -25,6 +26,9 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; */ public class NetworkListener implements Listener { + /** + * Our {@link NetworkManager} instance. + */ private final NetworkManager manager; public NetworkListener(@Nonnull SlimefunPlugin plugin, @Nonnull NetworkManager manager) { From ff8e630dec1edb11e9d3b92fdf0c5cfa75fc3fb9 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Mon, 3 May 2021 15:29:47 +0200 Subject: [PATCH 04/64] Fixed group id for mcmmo --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f46296bf9..2ccd3a410 100644 --- a/pom.xml +++ b/pom.xml @@ -424,7 +424,7 @@ - com.gmail.nossr50.mcmmo + com.gmail.nossr50.mcMMO mcMMO 2.1.196 provided From 227621906c86787a0e4fe8b8eca6f419bcfd9821 Mon Sep 17 00:00:00 2001 From: Martin Brom Date: Fri, 23 Apr 2021 19:33:46 +0200 Subject: [PATCH 05/64] Add the option to disable learning animations server & client side --- .../slimefun4/core/SlimefunRegistry.java | 6 ++ .../guide/SlimefunGuideImplementation.java | 11 ++-- .../options/LearningAnimationOption.java | 55 +++++++++++++++++++ .../guide/options/SlimefunGuideSettings.java | 22 ++++++++ src/main/resources/config.yml | 1 + .../researches/TestResearchUnlocking.java | 2 +- 6 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/LearningAnimationOption.java 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 e6c146590..a647390dc 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java @@ -69,6 +69,7 @@ public final class SlimefunRegistry { private boolean enableResearches; private boolean freeCreativeResearches; private boolean researchFireworks; + private boolean disableLearningAnimation; private boolean logDuplicateBlockEntries; private boolean talismanActionBarMessages; @@ -109,6 +110,7 @@ public final class SlimefunRegistry { backwardsCompatibility = cfg.getBoolean("options.backwards-compatibility"); freeCreativeResearches = cfg.getBoolean("researches.free-in-creative-mode"); researchFireworks = cfg.getBoolean("researches.enable-fireworks"); + disableLearningAnimation = cfg.getBoolean("researches.disable-learning-animation"); logDuplicateBlockEntries = cfg.getBoolean("options.log-duplicate-block-entries"); talismanActionBarMessages = cfg.getBoolean("talismans.use-actionbar"); } @@ -238,6 +240,10 @@ public final class SlimefunRegistry { return researchFireworks; } + public boolean isLearningAnimationDisabled() { + return disableLearningAnimation; + } + /** * This method returns a {@link List} of every enabled {@link MultiBlock}. * diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/SlimefunGuideImplementation.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/SlimefunGuideImplementation.java index 873ce5105..dac529e77 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/SlimefunGuideImplementation.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/SlimefunGuideImplementation.java @@ -9,6 +9,7 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; +import io.github.thebusybiscuit.slimefun4.core.guide.options.SlimefunGuideSettings; import io.github.thebusybiscuit.slimefun4.core.researching.Research; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.guide.SurvivalSlimefunGuide; @@ -18,7 +19,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; /** * This interface is used for the different implementations that add behaviour * to the {@link SlimefunGuide}. - * + * * @author TheBusyBiscuit * * @see SlimefunGuideMode @@ -30,7 +31,7 @@ public interface SlimefunGuideImplementation { /** * Every {@link SlimefunGuideImplementation} can be associated with a * {@link SlimefunGuideMode}. - * + * * @return The mode this {@link SlimefunGuideImplementation} represents */ @Nonnull @@ -40,7 +41,7 @@ public interface SlimefunGuideImplementation { * Returns the {@link ItemStack} representation for this {@link SlimefunGuideImplementation}. * In other words: The {@link ItemStack} you hold in your hand and that you use to * open your {@link SlimefunGuide} - * + * * @return The {@link ItemStack} representation for this {@link SlimefunGuideImplementation} */ @Nonnull @@ -63,7 +64,9 @@ public interface SlimefunGuideImplementation { research.unlock(p, true, callback); } else { p.setLevel(p.getLevel() - research.getCost()); - research.unlock(p, false, callback); + boolean skipLearningAnimation = SlimefunPlugin.getRegistry().isLearningAnimationDisabled() + || !SlimefunGuideSettings.hasLearningAnimationEnabled(p); + research.unlock(p, skipLearningAnimation, callback); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/LearningAnimationOption.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/LearningAnimationOption.java new file mode 100644 index 000000000..1ffd31d63 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/LearningAnimationOption.java @@ -0,0 +1,55 @@ +package io.github.thebusybiscuit.slimefun4.core.guide.options; + +import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI; +import io.github.thebusybiscuit.cscorelib2.item.CustomItem; +import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon; +import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import java.util.Optional; + +public class LearningAnimationOption implements SlimefunGuideOption { + + @Override + public SlimefunAddon getAddon() { + return SlimefunPlugin.instance(); + } + + @Override + public NamespacedKey getKey() { + return new NamespacedKey(SlimefunPlugin.instance(), "research_learning_animation"); + } + + @Override + public Optional getDisplayItem(Player p, ItemStack guide) { + if (SlimefunPlugin.getRegistry().isLearningAnimationDisabled()) { + return Optional.empty(); + } else { + boolean enabled = getSelectedOption(p, guide).orElse(true); + ItemStack item = new CustomItem(Material.PAPER, "&bLearning Animation: &" + (enabled ? "aYes" : "4No"), "", "&7You can now toggle whether you", "&7will see information about your pondering in chat", "&7upon researching an item.", "", "&7\u21E8 &eClick to " + (enabled ? "disable" : "enable") + " your learning animation"); + return Optional.of(item); + } + } + + @Override + public void onClick(Player p, ItemStack guide) { + setSelectedOption(p, guide, !getSelectedOption(p, guide).orElse(true)); + SlimefunGuideSettings.openSettings(p, guide); + } + + @Override + public Optional getSelectedOption(Player p, ItemStack guide) { + NamespacedKey key = getKey(); + boolean value = !PersistentDataAPI.hasByte(p, key) || PersistentDataAPI.getByte(p, key) == (byte) 1; + return Optional.of(value); + } + + @Override + public void setSelectedOption(Player p, ItemStack guide, Boolean value) { + PersistentDataAPI.setByte(p, getKey(), (byte) (value ? 1 : 0)); + } + +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/SlimefunGuideSettings.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/SlimefunGuideSettings.java index 6662fcb22..c893af96e 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/SlimefunGuideSettings.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/SlimefunGuideSettings.java @@ -47,6 +47,7 @@ public final class SlimefunGuideSettings { static { options.add(new GuideModeOption()); options.add(new FireworksOption()); + options.add(new LearningAnimationOption()); options.add(new PlayerLanguageOption()); } @@ -249,4 +250,25 @@ public final class SlimefunGuideSettings { return true; } + /** + * This method checks if the given {@link Player} has enabled the {@link LearningAnimationOption} + * in their {@link SlimefunGuide}. + * If they enabled this setting, they will see messages in chat about the progress of their {@link Research}. + * + * @param p The {@link Player} + * + * @return Whether this {@link Player} wants to info messages in chat when unlocking a {@link Research} + */ + public static boolean hasLearningAnimationEnabled(@Nonnull Player p) { + for (SlimefunGuideOption option : options) { + if (option instanceof LearningAnimationOption) { + LearningAnimationOption learningAnimation = (LearningAnimationOption) option; + ItemStack guide = SlimefunGuide.getItem(SlimefunGuideMode.SURVIVAL_MODE); + return learningAnimation.getSelectedOption(p, guide).orElse(true); + } + } + + return true; + } + } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index a0f9cd53a..a0240c6d4 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -26,6 +26,7 @@ guide: researches: free-in-creative-mode: true enable-fireworks: true + disable-learning-animation: false URID: info-delay: 3000 diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/researches/TestResearchUnlocking.java b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/researches/TestResearchUnlocking.java index 9f4f88bac..dd017af4f 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/researches/TestResearchUnlocking.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/researches/TestResearchUnlocking.java @@ -47,7 +47,7 @@ class TestResearchUnlocking { latch.countDown(); }); - latch.await(10, TimeUnit.SECONDS); + latch.await(instant ? 1 : 10, TimeUnit.SECONDS); return ref.get(); } From fd12521a0a48961d47f02f18937800a7bde2e45c Mon Sep 17 00:00:00 2001 From: Martin Brom Date: Fri, 23 Apr 2021 20:55:36 +0200 Subject: [PATCH 06/64] Add requested changes Revert change in research test --- .../slimefun4/core/SlimefunRegistry.java | 5 +++++ .../options/LearningAnimationOption.java | 22 ++++++++++++------- .../researches/TestResearchUnlocking.java | 2 +- 3 files changed, 20 insertions(+), 9 deletions(-) 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 a647390dc..6658c3b69 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java @@ -240,6 +240,11 @@ public final class SlimefunRegistry { return researchFireworks; } + /** + * Returns whether the research learning animations is disabled + * + * @return Whether the research learning animations is disabled + */ public boolean isLearningAnimationDisabled() { return disableLearningAnimation; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/LearningAnimationOption.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/LearningAnimationOption.java index 1ffd31d63..fd4a02260 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/LearningAnimationOption.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/LearningAnimationOption.java @@ -1,28 +1,34 @@ package io.github.thebusybiscuit.slimefun4.core.guide.options; -import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI; -import io.github.thebusybiscuit.cscorelib2.item.CustomItem; -import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon; -import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; +import java.util.Optional; + +import javax.annotation.Nonnull; + import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import java.util.Optional; +import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI; +import io.github.thebusybiscuit.cscorelib2.item.CustomItem; +import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon; +import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; public class LearningAnimationOption implements SlimefunGuideOption { + @Nonnull @Override public SlimefunAddon getAddon() { return SlimefunPlugin.instance(); } + @Nonnull @Override public NamespacedKey getKey() { return new NamespacedKey(SlimefunPlugin.instance(), "research_learning_animation"); } + @Nonnull @Override public Optional getDisplayItem(Player p, ItemStack guide) { if (SlimefunPlugin.getRegistry().isLearningAnimationDisabled()) { @@ -35,20 +41,20 @@ public class LearningAnimationOption implements SlimefunGuideOption { } @Override - public void onClick(Player p, ItemStack guide) { + public void onClick(@Nonnull Player p, @Nonnull ItemStack guide) { setSelectedOption(p, guide, !getSelectedOption(p, guide).orElse(true)); SlimefunGuideSettings.openSettings(p, guide); } @Override - public Optional getSelectedOption(Player p, ItemStack guide) { + public Optional getSelectedOption(@Nonnull Player p, @Nonnull ItemStack guide) { NamespacedKey key = getKey(); boolean value = !PersistentDataAPI.hasByte(p, key) || PersistentDataAPI.getByte(p, key) == (byte) 1; return Optional.of(value); } @Override - public void setSelectedOption(Player p, ItemStack guide, Boolean value) { + public void setSelectedOption(@Nonnull Player p, @Nonnull ItemStack guide, @Nonnull Boolean value) { PersistentDataAPI.setByte(p, getKey(), (byte) (value ? 1 : 0)); } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/researches/TestResearchUnlocking.java b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/researches/TestResearchUnlocking.java index dd017af4f..9f4f88bac 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/researches/TestResearchUnlocking.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/researches/TestResearchUnlocking.java @@ -47,7 +47,7 @@ class TestResearchUnlocking { latch.countDown(); }); - latch.await(instant ? 1 : 10, TimeUnit.SECONDS); + latch.await(10, TimeUnit.SECONDS); return ref.get(); } From e7ae14beaf06f2f79bdf7f6431a7bcb0d2977361 Mon Sep 17 00:00:00 2001 From: Martin Brom Date: Sat, 24 Apr 2021 22:05:57 +0200 Subject: [PATCH 07/64] Add locale message for learning animation option Add helper function to get value of selected option in the survival guide --- .../options/LearningAnimationOption.java | 5 +-- .../guide/options/SlimefunGuideSettings.java | 36 +++++++++++-------- src/main/resources/languages/en/messages.yml | 19 ++++++++++ 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/LearningAnimationOption.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/LearningAnimationOption.java index fd4a02260..6ef24e6f7 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/LearningAnimationOption.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/LearningAnimationOption.java @@ -34,8 +34,9 @@ public class LearningAnimationOption implements SlimefunGuideOption { if (SlimefunPlugin.getRegistry().isLearningAnimationDisabled()) { return Optional.empty(); } else { - boolean enabled = getSelectedOption(p, guide).orElse(true); - ItemStack item = new CustomItem(Material.PAPER, "&bLearning Animation: &" + (enabled ? "aYes" : "4No"), "", "&7You can now toggle whether you", "&7will see information about your pondering in chat", "&7upon researching an item.", "", "&7\u21E8 &eClick to " + (enabled ? "disable" : "enable") + " your learning animation"); + String enabled = getSelectedOption(p, guide).orElse(true) ? "enabled" : "disabled"; + ItemStack item = new CustomItem(Material.PAPER, SlimefunPlugin.getLocalization().getMessages( + p, "guide.options.learning-animation." + enabled)); return Optional.of(item); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/SlimefunGuideSettings.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/SlimefunGuideSettings.java index c893af96e..1c82c952b 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/SlimefunGuideSettings.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/options/SlimefunGuideSettings.java @@ -233,21 +233,12 @@ public final class SlimefunGuideSettings { * in their {@link SlimefunGuide}. * If they enabled this setting, they will see fireworks when they unlock a {@link Research}. * - * @param p - * The {@link Player} + * @param p The {@link Player} * * @return Whether this {@link Player} wants to see fireworks when unlocking a {@link Research} */ public static boolean hasFireworksEnabled(@Nonnull Player p) { - for (SlimefunGuideOption option : options) { - if (option instanceof FireworksOption) { - FireworksOption fireworks = (FireworksOption) option; - ItemStack guide = SlimefunGuide.getItem(SlimefunGuideMode.SURVIVAL_MODE); - return fireworks.getSelectedOption(p, guide).orElse(true); - } - } - - return true; + return getOptionValue(p, FireworksOption.class, true); } /** @@ -260,15 +251,30 @@ public final class SlimefunGuideSettings { * @return Whether this {@link Player} wants to info messages in chat when unlocking a {@link Research} */ public static boolean hasLearningAnimationEnabled(@Nonnull Player p) { + return getOptionValue(p, LearningAnimationOption.class, true); + } + + /** + * Helper method to get the value of a {@link SlimefunGuideOption} that the {@link Player} + * has set in their {@link SlimefunGuide} + * + * @param p The {@link Player} + * @param cls Class of the {@link SlimefunGuideOption} to get the value of + * @param defaultValue Default value to return in case the option is not found at all or has no value set + * @param Type of the {@link SlimefunGuideOption} value + * @param