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

Added PreCanUnlockResearchEvent

This event is called when a player click to unlock a research
This commit is contained in:
uiytt 2020-10-12 19:19:31 +02:00
parent 15884c65aa
commit 9ded6d1ac4
3 changed files with 123 additions and 14 deletions

View File

@ -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;
}
}

View File

@ -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,16 +239,21 @@ 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 {
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);
}
}
}
}
});
}

View File

@ -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,16 +286,21 @@ 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 {
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;
});