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

Added new API class

This commit is contained in:
TheBusyBiscuit 2019-11-30 00:44:44 +01:00
parent d5482f5496
commit 41327d637c
2 changed files with 87 additions and 2 deletions

View File

@ -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<ItemRestriction> 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<Category> 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<SlimefunItem> getItems() {
return getCategories().stream().flatMap(cat -> cat.getItems().stream()).collect(Collectors.toSet());
}
}

View File

@ -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());