From 227621906c86787a0e4fe8b8eca6f419bcfd9821 Mon Sep 17 00:00:00 2001 From: Martin Brom Date: Fri, 23 Apr 2021 19:33:46 +0200 Subject: [PATCH] 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(); }