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

Added a callback to Research#unlock(...)

This commit is contained in:
TheBusyBiscuit 2020-05-17 14:13:46 +02:00
parent de8fb5ccef
commit 65228fbeb1
5 changed files with 33 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package io.github.thebusybiscuit.slimefun4.core.commands.subcommands; package io.github.thebusybiscuit.slimefun4.core.commands.subcommands;
import java.util.Optional; import java.util.Optional;
import java.util.function.UnaryOperator;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -73,8 +74,10 @@ class ResearchCommand extends SubCommand {
Optional<Research> research = getResearchFromString(input); Optional<Research> research = getResearchFromString(input);
if (research.isPresent()) { if (research.isPresent()) {
research.get().unlock(p, true); research.get().unlock(p, true, player -> {
SlimefunPlugin.getLocal().sendMessage(sender, "messages.give-research", true, msg -> msg.replace(PLACEHOLDER_PLAYER, p.getName()).replace(PLACEHOLDER_RESEARCH, research.get().getName(p))); UnaryOperator<String> 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 { else {
SlimefunPlugin.getLocal().sendMessage(sender, "messages.not-valid-research", true, msg -> msg.replace(PLACEHOLDER_RESEARCH, input)); SlimefunPlugin.getLocal().sendMessage(sender, "messages.not-valid-research", true, msg -> msg.replace(PLACEHOLDER_RESEARCH, input));

View File

@ -1,5 +1,7 @@
package io.github.thebusybiscuit.slimefun4.core.guide; package io.github.thebusybiscuit.slimefun4.core.guide;
import java.util.function.Consumer;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; 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.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.Slimefun;
/** /**
* This interface is used for the different implementations that add behaviour * 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); 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<Player> callback) {
Research research = sfitem.getResearch(); Research research = sfitem.getResearch();
if (p.getGameMode() == GameMode.CREATIVE && SlimefunPlugin.getRegistry().isFreeCreativeResearchingEnabled()) { if (p.getGameMode() == GameMode.CREATIVE && SlimefunPlugin.getRegistry().isFreeCreativeResearchingEnabled()) {
research.unlock(p, true); research.unlock(p, true, callback);
Slimefun.runSync(callback, 5L);
} }
else { else {
research.unlock(p, false);
p.setLevel(p.getLevel() - research.getCost()); p.setLevel(p.getLevel() - research.getCost());
Slimefun.runSync(callback, 103L); research.unlock(p, false, callback);
} }
} }

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.function.Consumer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
@ -195,15 +196,29 @@ public class Research implements Keyed {
return creativeResearch || p.getLevel() >= cost; 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}. * Unlocks this {@link Research} for the specified {@link Player}.
* *
* @param p * @param p
* The {@link Player} for which to unlock this {@link Research} * The {@link Player} for which to unlock this {@link Research}
* @param instant * @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<Player> callback) {
if (!instant) { if (!instant) {
Slimefun.runSync(() -> { Slimefun.runSync(() -> {
p.playSound(p.getLocation(), Sound.ENTITY_BAT_TAKEOFF, 0.7F, 1F); p.playSound(p.getLocation(), Sound.ENTITY_BAT_TAKEOFF, 0.7F, 1F);
@ -219,14 +234,14 @@ public class Research implements Keyed {
if (!event.isCancelled()) { if (!event.isCancelled()) {
if (instant) { if (instant) {
finishResearch(p, profile); finishResearch(p, profile, callback);
} }
else if (SlimefunPlugin.getRegistry().getCurrentlyResearchingPlayers().add(p.getUniqueId())) { else if (SlimefunPlugin.getRegistry().getCurrentlyResearchingPlayers().add(p.getUniqueId())) {
SlimefunPlugin.getLocal().sendMessage(p, "messages.research.start", true, msg -> msg.replace(PLACEHOLDER_RESEARCH, getName(p))); SlimefunPlugin.getLocal().sendMessage(p, "messages.research.start", true, msg -> msg.replace(PLACEHOLDER_RESEARCH, getName(p)));
playResearchAnimation(p); playResearchAnimation(p);
Slimefun.runSync(() -> { Slimefun.runSync(() -> {
finishResearch(p, profile); finishResearch(p, profile, callback);
SlimefunPlugin.getRegistry().getCurrentlyResearchingPlayers().remove(p.getUniqueId()); SlimefunPlugin.getRegistry().getCurrentlyResearchingPlayers().remove(p.getUniqueId());
}, (RESEARCH_PROGRESS.length + 1) * 20L); }, (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<Player> callback) {
profile.setResearched(this, true); profile.setResearched(this, true);
SlimefunPlugin.getLocal().sendMessage(p, "messages.unlocked", true, msg -> msg.replace(PLACEHOLDER_RESEARCH, getName(p))); SlimefunPlugin.getLocal().sendMessage(p, "messages.unlocked", true, msg -> msg.replace(PLACEHOLDER_RESEARCH, getName(p)));
callback.accept(p);
if (SlimefunPlugin.getRegistry().isResearchFireworkEnabled() && SlimefunGuideSettings.hasFireworksEnabled(p)) { if (SlimefunPlugin.getRegistry().isResearchFireworkEnabled() && SlimefunGuideSettings.hasFireworksEnabled(p)) {
FireworkUtils.launchRandom(p, 1); FireworkUtils.launchRandom(p, 1);

View File

@ -165,7 +165,7 @@ public class BookSlimefunGuide implements SlimefunGuideImplementation {
openCategory(profile, category, page); openCategory(profile, category, page);
} }
else { 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); else SlimefunPlugin.getLocal().sendMessage(p, "messages.not-enough-xp", true);

View File

@ -229,7 +229,7 @@ public class ChestSlimefunGuide implements SlimefunGuideImplementation {
openCategory(profile, category, page); openCategory(profile, category, page);
} }
else { else {
unlockItem(pl, sfitem, () -> openCategory(profile, category, page)); unlockItem(pl, sfitem, player -> openCategory(profile, category, page));
} }
} }
else { else {