diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/PreCanUnlockResearchEvent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/PreCanUnlockResearchEvent.java new file mode 100644 index 000000000..8ad65c629 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/PreCanUnlockResearchEvent.java @@ -0,0 +1,95 @@ +package io.github.thebusybiscuit.slimefun4.api.events; + +import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; +import io.github.thebusybiscuit.slimefun4.core.researching.Research; +import io.github.thebusybiscuit.slimefun4.implementation.guide.BookSlimefunGuide; +import io.github.thebusybiscuit.slimefun4.implementation.guide.ChestSlimefunGuide; +import me.mrCookieSlime.Slimefun.Objects.Category; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; +import org.apache.commons.lang.Validate; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import javax.annotation.Nonnull; + +public class PreCanUnlockResearchEvent extends Event implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled = false; + + private final Player player; + private final Research research; + private final PlayerProfile profile; + private final SlimefunItem slimefunItem; + private final Category category; + + /** + * This {@link Event} is called whenever a {@link Player} click to unlock a research. + * This is called called before {@link Research#canUnlock(Player)} + * The event is not called for the cheat sheet + * + * @author uiytt + * + * @see ChestSlimefunGuide + * @see BookSlimefunGuide + * + */ + public PreCanUnlockResearchEvent(@Nonnull Player p, @Nonnull Research research, @Nonnull PlayerProfile profile, @Nonnull SlimefunItem slimefunItem, @Nonnull Category category) { + Validate.notNull(p, "The Player cannot be null"); + Validate.notNull(research, "Research cannot be null"); + Validate.notNull(profile, "PlayerProfile cannot be null"); + Validate.notNull(slimefunItem, "SlimefunItem cannot be null"); + Validate.notNull(category, "Category cannot be null"); + + this.player = p; + this.research = research; + this.profile = profile; + this.slimefunItem = slimefunItem; + this.category = category; + } + + @Nonnull + public Player getPlayer() { + return this.player; + } + @Nonnull + public Research getResearch() { + return this.research; + } + @Nonnull + public PlayerProfile getProfile() { + return this.profile; + } + @Nonnull + public SlimefunItem getSlimefunItem() { + return this.slimefunItem; + } + @Nonnull + public Category getCategory() { + return this.category; + } + + + @Nonnull + public static HandlerList getHandlerList() { + return handlers; + } + + @Nonnull + @Override + public HandlerList getHandlers() { + return getHandlerList(); + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } +} 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 0a26c0a7f..9b4aeed6a 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 @@ -5,6 +5,8 @@ import java.util.LinkedList; import java.util.List; import java.util.Locale; +import io.github.thebusybiscuit.slimefun4.api.events.PreCanUnlockResearchEvent; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; @@ -237,15 +239,20 @@ public class BookSlimefunGuide implements SlimefunGuideImplementation { private void research(Player p, PlayerProfile profile, SlimefunItem item, Research research, Category category, int page) { SlimefunPlugin.runSync(() -> { if (!SlimefunPlugin.getRegistry().getCurrentlyResearchingPlayers().contains(p.getUniqueId())) { - if (research.canUnlock(p)) { - if (profile.hasUnlocked(research)) { - openCategory(profile, category, page); - } else { - unlockItem(p, item, pl -> openCategory(profile, category, page)); - } + if (profile.hasUnlocked(research)) { + openCategory(profile, category, page); } else { - SlimefunPlugin.getLocalization().sendMessage(p, "messages.not-enough-xp", true); + PreCanUnlockResearchEvent event = new PreCanUnlockResearchEvent(p,research,profile,item,category); + Bukkit.getPluginManager().callEvent(event); + if(!event.isCancelled()) { + if (research.canUnlock(p)) { + unlockItem(p, item, pl -> openCategory(profile, category, page)); + } else { + SlimefunPlugin.getLocalization().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 4f346f291..6d6bdb936 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 @@ -10,6 +10,8 @@ import java.util.Locale; import java.util.Optional; import java.util.logging.Level; +import io.github.thebusybiscuit.slimefun4.api.events.PreCanUnlockResearchEvent; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.Sound; @@ -284,15 +286,20 @@ public class ChestSlimefunGuide implements SlimefunGuideImplementation { menu.addItem(index, new CustomItem(Material.BARRIER, ChatColor.WHITE + ItemUtils.getItemName(sfitem.getItem()), "&4&l" + SlimefunPlugin.getLocalization().getMessage(p, "guide.locked"), "", "&a> Click to unlock", "", "&7Cost: &b" + research.getCost() + " Level(s)")); menu.addMenuClickHandler(index, (pl, slot, item, action) -> { if (!SlimefunPlugin.getRegistry().getCurrentlyResearchingPlayers().contains(pl.getUniqueId())) { - if (research.canUnlock(pl)) { - if (profile.hasUnlocked(research)) { - openCategory(profile, category, page); - } else { - unlockItem(pl, sfitem, player -> openCategory(profile, category, page)); - } + if (profile.hasUnlocked(research)) { + openCategory(profile, category, page); } else { - SlimefunPlugin.getLocalization().sendMessage(pl, "messages.not-enough-xp", true); + PreCanUnlockResearchEvent event = new PreCanUnlockResearchEvent(p,research,profile,sfitem,category); + Bukkit.getPluginManager().callEvent(event); + if(!event.isCancelled()) { + if (research.canUnlock(pl)) { + unlockItem(pl, sfitem, player -> openCategory(profile, category, page)); + } else { + SlimefunPlugin.getLocalization().sendMessage(pl, "messages.not-enough-xp", true); + } + } } + } return false;