diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/SlimefunAPI.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/SlimefunAPI.java new file mode 100644 index 000000000..9ba8b3b1d --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/SlimefunAPI.java @@ -0,0 +1,87 @@ +package io.github.thebusybiscuit.slimefun4.api; + +import java.util.Set; +import java.util.stream.Collectors; + +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import io.github.thebusybiscuit.slimefun4.api.items.ItemRestriction; +import me.mrCookieSlime.Slimefun.Objects.Category; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; +import me.mrCookieSlime.Slimefun.api.PlayerProfile; + +/** + * This class will hold the declarations of important API methods for Slimefun. + * Normally these methods will not be implemented in this interface, but some + * methods will have a default implementation for convenience. + * + * @author TheBusyBiscuit + * + */ +public interface SlimefunAPI { + + /** + * This method will register the given restriction. + * Calling this should directly influence the outcome of {@link SlimefunAPI#getItemRestrictions()}. + * + * @param restriction The restriction to register + */ + void addItemRestriction(ItemRestriction restriction); + + /** + * This method will return a {@link Set} of all instances of {@link ItemRestriction} that will directly + * affect the outcome of {@link SlimefunAPI#isAllowedToUse(Player, ItemStack, boolean)} + * + * @return The Set of all registered item restrictions + */ + Set getItemRestrictions(); + + /** + * This method will return whether a Player is allowed to use the given {@link ItemStack}. + * If warningMessages is set to true, the Player will be informed about the outcome of this method. + * + * Internally this method will loop through {@link SlimefunAPI#getItemRestrictions()} and perform + * checks using all available instances of {@link ItemRestriction}. + * + * Do NOT warn Players about a restriction if this method returns false. + * Warnings should be exclusively handled via {@link ItemRestriction#warnPlayer(PlayerProfile, Player, SlimefunItem, ItemStack)} + * + * @param p The Player to perform the check on + * @param item The Item to perform the check on + * @param sendWarnings Whether to warn the Player about not being able to use the given item + * @return Whether the Player is allowed to use the given item + */ + default boolean isAllowedToUse(Player p, ItemStack item, boolean sendWarnings) { + PlayerProfile profile = PlayerProfile.get(p); + SlimefunItem sfItem = SlimefunItem.getByItem(item); + + for (ItemRestriction restriction : getItemRestrictions()) { + if (!restriction.isAllowed(profile, p, sfItem, item)) { + if (sendWarnings) restriction.warnPlayer(profile, p, sfItem, item); + return false; + } + } + + return true; + } + + /** + * This method returns a {@link Set} of all registered instances of {@link Category}. + * + * @return A Set of all Categories + */ + Set getCategories(); + + /** + * This method returns A {@link Set} of all registered instances of {@link SlimefunItem}. + * Its default implementation generates a new Set based on all items found in the categories + * returned by {@link SlimefunAPI#getCategories()} + * + * @return A Set of all SlimefunItems + */ + default Set getItems() { + return getCategories().stream().flatMap(cat -> cat.getItems().stream()).collect(Collectors.toSet()); + } + +} diff --git a/src/main/java/me/mrCookieSlime/Slimefun/api/PlayerProfile.java b/src/main/java/me/mrCookieSlime/Slimefun/api/PlayerProfile.java index c6cbe11b7..50d38fc33 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/api/PlayerProfile.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/api/PlayerProfile.java @@ -264,9 +264,7 @@ public final class PlayerProfile { * * @param p The player's profile you wish to retrieve * @return The PlayerProfile of this player - * @deprecated Use {@link #get(OfflinePlayer, Consumer)} */ - @Deprecated public static PlayerProfile get(OfflinePlayer p) { PlayerProfile profile = SlimefunPlugin.getUtilities().profiles.get(p.getUniqueId());