diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/ResearchCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/ResearchCommand.java index eeca270f7..a4ab4cac1 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/ResearchCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/ResearchCommand.java @@ -1,6 +1,7 @@ package io.github.thebusybiscuit.slimefun4.core.commands.subcommands; import java.util.Optional; +import java.util.function.UnaryOperator; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -73,8 +74,10 @@ class ResearchCommand extends SubCommand { Optional research = getResearchFromString(input); if (research.isPresent()) { - research.get().unlock(p, true); - SlimefunPlugin.getLocal().sendMessage(sender, "messages.give-research", true, msg -> msg.replace(PLACEHOLDER_PLAYER, p.getName()).replace(PLACEHOLDER_RESEARCH, research.get().getName(p))); + research.get().unlock(p, true, player -> { + UnaryOperator variables = msg -> msg.replace(PLACEHOLDER_PLAYER, player.getName()).replace(PLACEHOLDER_RESEARCH, research.get().getName(player)); + SlimefunPlugin.getLocal().sendMessage(player, "messages.give-research", true, variables); + }); } else { SlimefunPlugin.getLocal().sendMessage(sender, "messages.not-valid-research", true, msg -> msg.replace(PLACEHOLDER_RESEARCH, input)); 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 71d4a6729..a956157ad 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 @@ -1,5 +1,7 @@ package io.github.thebusybiscuit.slimefun4.core.guide; +import java.util.function.Consumer; + import org.bukkit.GameMode; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -11,7 +13,6 @@ import io.github.thebusybiscuit.slimefun4.implementation.guide.ChestSlimefunGuid import me.mrCookieSlime.Slimefun.SlimefunPlugin; import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; -import me.mrCookieSlime.Slimefun.api.Slimefun; /** * This interface is used for the different implementations that add behaviour @@ -53,17 +54,15 @@ public interface SlimefunGuideImplementation { void displayItem(PlayerProfile profile, SlimefunItem item, boolean addToHistory); - default void unlockItem(Player p, SlimefunItem sfitem, Runnable callback) { + default void unlockItem(Player p, SlimefunItem sfitem, Consumer callback) { Research research = sfitem.getResearch(); if (p.getGameMode() == GameMode.CREATIVE && SlimefunPlugin.getRegistry().isFreeCreativeResearchingEnabled()) { - research.unlock(p, true); - Slimefun.runSync(callback, 5L); + research.unlock(p, true, callback); } else { - research.unlock(p, false); p.setLevel(p.getLevel() - research.getCost()); - Slimefun.runSync(callback, 103L); + research.unlock(p, false, callback); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/researching/Research.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/researching/Research.java index 34db96b0f..b881da69d 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/researching/Research.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/researching/Research.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Optional; +import java.util.function.Consumer; import org.bukkit.Bukkit; import org.bukkit.GameMode; @@ -195,15 +196,29 @@ public class Research implements Keyed { return creativeResearch || p.getLevel() >= cost; } + /** + * This unlocks this {@link Research} for the given {@link Player} without any form of callback. + * + * @param p + * The {@link Player} who should unlock this {@link Research} + * @param instant + * Whether to unlock it instantly + */ + public void unlock(Player p, boolean instant) { + unlock(p, instant, pl -> {}); + } + /** * Unlocks this {@link Research} for the specified {@link Player}. * * @param p * The {@link Player} for which to unlock this {@link Research} * @param instant - * Whether to unlock the research instantly + * Whether to unlock this {@link Research} instantly + * @param callback + * A callback which will be run when the {@link Research} animation completed */ - public void unlock(Player p, boolean instant) { + public void unlock(Player p, boolean instant, Consumer callback) { if (!instant) { Slimefun.runSync(() -> { p.playSound(p.getLocation(), Sound.ENTITY_BAT_TAKEOFF, 0.7F, 1F); @@ -219,14 +234,14 @@ public class Research implements Keyed { if (!event.isCancelled()) { if (instant) { - finishResearch(p, profile); + finishResearch(p, profile, callback); } else if (SlimefunPlugin.getRegistry().getCurrentlyResearchingPlayers().add(p.getUniqueId())) { SlimefunPlugin.getLocal().sendMessage(p, "messages.research.start", true, msg -> msg.replace(PLACEHOLDER_RESEARCH, getName(p))); playResearchAnimation(p); Slimefun.runSync(() -> { - finishResearch(p, profile); + finishResearch(p, profile, callback); SlimefunPlugin.getRegistry().getCurrentlyResearchingPlayers().remove(p.getUniqueId()); }, (RESEARCH_PROGRESS.length + 1) * 20L); } @@ -236,9 +251,10 @@ public class Research implements Keyed { }); } - private void finishResearch(Player p, PlayerProfile profile) { + private void finishResearch(Player p, PlayerProfile profile, Consumer callback) { profile.setResearched(this, true); SlimefunPlugin.getLocal().sendMessage(p, "messages.unlocked", true, msg -> msg.replace(PLACEHOLDER_RESEARCH, getName(p))); + callback.accept(p); if (SlimefunPlugin.getRegistry().isResearchFireworkEnabled() && SlimefunGuideSettings.hasFireworksEnabled(p)) { FireworkUtils.launchRandom(p, 1); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/BookSlimefunGuide.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/BookSlimefunGuide.java index 66788027e..9b8a9607a 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/BookSlimefunGuide.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/BookSlimefunGuide.java @@ -165,7 +165,7 @@ public class BookSlimefunGuide implements SlimefunGuideImplementation { openCategory(profile, category, page); } else { - unlockItem(p, item, () -> openCategory(profile, category, page)); + unlockItem(p, item, pl -> openCategory(profile, category, page)); } } else SlimefunPlugin.getLocal().sendMessage(p, "messages.not-enough-xp", true); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/ChestSlimefunGuide.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/ChestSlimefunGuide.java index 6e6ca9518..634670478 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/ChestSlimefunGuide.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/ChestSlimefunGuide.java @@ -229,7 +229,7 @@ public class ChestSlimefunGuide implements SlimefunGuideImplementation { openCategory(profile, category, page); } else { - unlockItem(pl, sfitem, () -> openCategory(profile, category, page)); + unlockItem(pl, sfitem, player -> openCategory(profile, category, page)); } } else {