1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-21 12:15:50 +00:00
Slimefun4/src/me/mrCookieSlime/Slimefun/api/Slimefun.java

332 lines
12 KiB
Java
Raw Normal View History

2016-04-14 16:24:03 +00:00
package me.mrCookieSlime.Slimefun.api;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
2019-08-31 09:36:45 +00:00
import java.util.logging.Logger;
2016-04-14 16:24:03 +00:00
2017-04-09 07:59:45 +00:00
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
2016-04-14 16:24:03 +00:00
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
2019-08-31 09:36:45 +00:00
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
2016-04-14 16:24:03 +00:00
import me.mrCookieSlime.Slimefun.GPS.GPSNetwork;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.Research;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem.State;
2019-08-31 09:36:45 +00:00
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.VanillaItem;
2019-08-31 10:54:54 +00:00
import me.mrCookieSlime.Slimefun.Setup.Messages;
2016-04-14 16:24:03 +00:00
/**
* Provides a few convenience methods.
*
* @since 4.0
*/
2019-08-31 13:52:52 +00:00
public final class Slimefun {
2017-06-13 17:59:39 +00:00
2019-08-31 13:52:52 +00:00
private Slimefun() {}
2016-04-14 16:24:03 +00:00
public static void registerGuideHandler(GuideHandler handler) {
2019-08-31 10:54:54 +00:00
List<GuideHandler> handlers = SlimefunPlugin.getUtilities().guideHandlers.getOrDefault(handler.getTier(), new ArrayList<>());
2016-04-14 16:24:03 +00:00
handlers.add(handler);
2019-08-31 10:54:54 +00:00
SlimefunPlugin.getUtilities().guideHandlers.put(handler.getTier(), handlers);
2016-04-14 16:24:03 +00:00
}
2017-06-13 17:59:39 +00:00
/**
* Returns the GPSNetwork instance.
*
* @return the GPSNetwork instance.
*/
2016-04-14 16:24:03 +00:00
public static GPSNetwork getGPSNetwork() {
2019-09-02 21:14:48 +00:00
return SlimefunPlugin.instance.getGPS();
2019-08-31 09:36:45 +00:00
}
public static Logger getLogger() {
return SlimefunPlugin.instance.getLogger();
2016-04-14 16:24:03 +00:00
}
2017-06-13 17:59:39 +00:00
/**
* Returns the value associated to this key for the SlimefunItem corresponding to this id.
*
* @param id the id of the SlimefunItem, not null
* @param key the key of the value to get, not null
*
* @return the value associated to the key for the SlimefunItem corresponding to the id,
* or null if it doesn't exist.
*/
2016-04-14 16:24:03 +00:00
public static Object getItemValue(String id, String key) {
return getItemConfig().getValue(id + "." + key);
2016-04-14 16:24:03 +00:00
}
2017-06-13 17:59:39 +00:00
/**
* Sets a default value associated to this key for the SlimefunItem corresponding to this id.
*
* @param id the id of the SlimefunItem, not null
* @param key the key of the value to set, not null
* @param value the value to set, can be null
*/
2016-04-14 16:24:03 +00:00
public static void setItemVariable(String id, String key, Object value) {
getItemConfig().setDefaultValue(id + "." + key, value);
2016-04-14 16:24:03 +00:00
}
2017-06-13 17:59:39 +00:00
/**
* Returns the Config instance of Items.yml file.
* <p>
* It calls {@code SlimefunStartup#getItemCfg()}.
*
* @return the Items.yml Config instance.
*/
2016-04-14 16:24:03 +00:00
public static Config getItemConfig() {
2019-08-31 09:36:45 +00:00
return SlimefunPlugin.getItemCfg();
2016-04-14 16:24:03 +00:00
}
/**
* Registers this Research and automatically binds these ItemStacks to it.
* <p>
* This convenience method spares from doing the code below:
* <pre>
* {@code
* Research r = new Research(7, "Glowstone Armor", 3);
* r.addItems(SlimefunItem.getByItem(SlimefunItems.GLOWSTONE_HELMET),
* SlimefunItem.getByItem(SlimefunItems.GLOWSTONE_CHESTPLATE),
* SlimefunItem.getByItem(SlimefunItems.GLOWSTONE_LEGGINGS),
* SlimefunItem.getByItem(SlimefunItems.GLOWSTONE_BOOTS));
* r.register();
* }*
* </pre>
* @param research the research to register, not null
* @param items the items to bind, not null
*/
2016-04-14 16:24:03 +00:00
public static void registerResearch(Research research, ItemStack... items) {
for (ItemStack item: items) {
research.addItems(SlimefunItem.getByItem(item));
}
research.register();
}
2017-06-13 17:59:39 +00:00
/**
* Checks if this player can use this item.
*
* @param p the player to check, not null
* @param item the item to check, not null
* @param message whether a message should be sent to the player or not
*
* @return <code>true</code> if the item is a SlimefunItem, enabled, researched and if the player has the permission to use it,
* <code>false</code> otherwise.
*/
2016-04-14 16:24:03 +00:00
public static boolean hasUnlocked(Player p, ItemStack item, boolean message) {
SlimefunItem sfItem = SlimefunItem.getByItem(item);
State state = SlimefunItem.getState(item);
2016-04-14 16:24:03 +00:00
if (sfItem == null) {
2017-06-22 20:39:19 +00:00
if (state != State.ENABLED) {
if (message && state != State.VANILLA) Messages.local.sendTranslation(p, "messages.disabled-item", true);
2016-04-14 16:24:03 +00:00
return false;
}
else return true;
}
else if (isEnabled(p, item, message) && hasPermission(p, sfItem, message)) {
if (sfItem.getResearch() == null) return true;
else if (PlayerProfile.fromUUID(p.getUniqueId()).hasUnlocked(sfItem.getResearch())) return true;
2016-04-14 16:24:03 +00:00
else {
if (message && !(sfItem instanceof VanillaItem)) Messages.local.sendTranslation(p, "messages.not-researched", true);
2016-04-14 16:24:03 +00:00
return false;
}
}
else return false;
}
2017-06-13 17:59:39 +00:00
/**
* Checks if this player can use this item.
*
* @param p the player to check, not null
* @param sfItem the item to check, not null
* @param message whether a message should be sent to the player or not
*
* @return <code>true</code> if the item is enabled, researched and the player has the permission to use it,
* <code>false</code> otherwise.
*/
2016-04-14 16:24:03 +00:00
public static boolean hasUnlocked(Player p, SlimefunItem sfItem, boolean message) {
if (isEnabled(p, sfItem, message) && hasPermission(p, sfItem, message)) {
if (sfItem.getResearch() == null) return true;
else if (PlayerProfile.fromUUID(p.getUniqueId()).hasUnlocked(sfItem.getResearch())) return true;
2016-04-14 16:24:03 +00:00
else {
if (message && !(sfItem instanceof VanillaItem)) Messages.local.sendTranslation(p, "messages.not-researched", true);
2016-04-14 16:24:03 +00:00
return false;
}
}
return false;
}
2017-06-13 17:59:39 +00:00
/**
* Checks if this player has the permission to use this item.
*
* @param p the player to check, not null
* @param item the item to check, null returns <code>true</code>
* @param message whether a message should be sent to the player or not
*
* @return <code>true</code> if the item is not null and if the player has the permission to use it,
* <code>false</code> otherwise.
*/
2016-04-14 16:24:03 +00:00
public static boolean hasPermission(Player p, SlimefunItem item, boolean message) {
if (item == null) return true;
2017-10-13 19:12:33 +00:00
else if (item.getPermission().equalsIgnoreCase("")) return true;
else if (p.hasPermission(item.getPermission())) return true;
2016-04-14 16:24:03 +00:00
else {
if (message) Messages.local.sendTranslation(p, "messages.no-permission", true);
return false;
}
}
2017-06-13 17:59:39 +00:00
/**
* Checks if this item is enabled in the world this player is in.
*
* @param p the player to get the world he is in, not null
* @param item the item to check, not null
* @param message whether a message should be sent to the player or not
*
* @return <code>true</code> if the item is a SlimefunItem and is enabled in the world the player is in,
* <code>false</code> otherwise.
*/
2016-04-14 16:24:03 +00:00
public static boolean isEnabled(Player p, ItemStack item, boolean message) {
String world = p.getWorld().getName();
SlimefunItem sfItem = SlimefunItem.getByItem(item);
if (sfItem == null) return !SlimefunItem.isDisabled(item);
2019-08-31 09:36:45 +00:00
if (SlimefunPlugin.getWhitelist().contains(world + ".enabled")) {
if (SlimefunPlugin.getWhitelist().getBoolean(world + ".enabled")) {
if (!SlimefunPlugin.getWhitelist().contains(world + ".enabled-items." + sfItem.getID())) SlimefunPlugin.getWhitelist().setDefaultValue(world + ".enabled-items." + sfItem.getID(), true);
if (SlimefunPlugin.getWhitelist().getBoolean(world + ".enabled-items." + sfItem.getID())) return true;
2016-04-14 16:24:03 +00:00
else {
if (message) Messages.local.sendTranslation(p, "messages.disabled-in-world", true);
return false;
}
}
else {
if (message) Messages.local.sendTranslation(p, "messages.disabled-in-world", true);
return false;
}
}
else return true;
}
2017-06-13 17:59:39 +00:00
/**
* Checks if this item is enabled in the world this player is in.
*
* @param p the player to get the world he is in, not null
* @param sfItem the item to check, not null
* @param message whether a message should be sent to the player or not
*
* @return <code>true</code> if the item is enabled in the world the player is in,
* <code>false</code> otherwise.
*/
2016-04-14 16:24:03 +00:00
public static boolean isEnabled(Player p, SlimefunItem sfItem, boolean message) {
String world = p.getWorld().getName();
2019-08-31 09:36:45 +00:00
if (SlimefunPlugin.getWhitelist().contains(world + ".enabled")) {
if (SlimefunPlugin.getWhitelist().getBoolean(world + ".enabled")) {
if (!SlimefunPlugin.getWhitelist().contains(world + ".enabled-items." + sfItem.getID())) SlimefunPlugin.getWhitelist().setDefaultValue(world + ".enabled-items." + sfItem.getID(), true);
if (SlimefunPlugin.getWhitelist().getBoolean(world + ".enabled-items." + sfItem.getID())) return true;
2016-04-14 16:24:03 +00:00
else {
if (message) Messages.local.sendTranslation(p, "messages.disabled-in-world", true);
return false;
}
}
else {
if (message) Messages.local.sendTranslation(p, "messages.disabled-in-world", true);
return false;
}
}
else return true;
}
/**
* Lists all the IDs of the enabled items.
*
* @return the list of all the IDs of the enabled items.
*/
2016-04-14 16:24:03 +00:00
public static List<String> listIDs() {
2019-08-28 08:59:20 +00:00
List<String> ids = new ArrayList<>();
2016-04-14 16:24:03 +00:00
for (SlimefunItem item: SlimefunItem.list()) {
2017-10-13 14:34:38 +00:00
ids.add(item.getID());
2016-04-14 16:24:03 +00:00
}
return ids;
}
/**
* Returns a list of all the ItemStacks representing the registered categories.
*
* @return the list of the display items of all the registered categories.
* @see #currentCategories
*/
2016-04-14 16:24:03 +00:00
public static List<ItemStack> listCategories() {
2019-08-28 08:59:20 +00:00
List<ItemStack> items = new ArrayList<>();
2016-04-14 16:24:03 +00:00
for (Category c: Category.list()) {
items.add(c.getItem());
}
return items;
}
2017-06-13 17:59:39 +00:00
/**
* Binds this description to the SlimefunItem corresponding to this id.
*
* @param id the id of the SlimefunItem, not null
* @param description the description, not null
*
* @deprecated As of 4.1.10, renamed to {@link #addHint(String, String...)} for better name convenience.
*/
@Deprecated
2016-04-14 16:24:03 +00:00
public static void addDescription(String id, String... description) {
getItemConfig().setDefaultValue(id + ".description", Arrays.asList(description));
}
2017-06-13 17:59:39 +00:00
/**
* Binds this hint to the SlimefunItem corresponding to this id.
*
* @param id the id of the SlimefunItem, not null
* @param hint the hint, not null
*
* @since 4.1.10, rename of {@link #addDescription(String, String...)}.
*/
public static void addHint(String id, String... hint) {
getItemConfig().setDefaultValue(id + ".hint", Arrays.asList(hint));
}
/**
* Binds this YouTube link to the SlimefunItem corresponding to this id.
*
* @param id the id of the SlimefunItem, not null
* @param link the link of the YouTube video, not null
*/
2016-04-14 16:24:03 +00:00
public static void addYoutubeVideo(String id, String link) {
getItemConfig().setDefaultValue(id + ".youtube", link);
}
2017-06-13 17:59:39 +00:00
/**
* Binds this link as a Wiki page to the SlimefunItem corresponding to this id.
*
* @param id the id of the SlimefunItem, not null
* @param link the link of the Wiki page, not null
*/
2016-04-14 16:24:03 +00:00
public static void addWikiPage(String id, String link) {
getItemConfig().setDefaultValue(id + ".wiki", link);
}
2017-06-13 17:59:39 +00:00
/**
* Convenience method to simplify binding an official Wiki page to the SlimefunItem corresponding to this id.
*
* @param id the id of the SlimefunItem, not null
* @param page the ending of the link corresponding to the page, not null
*/
2016-04-14 16:24:03 +00:00
public static void addOfficialWikiPage(String id, String page) {
2017-01-19 17:26:56 +00:00
addWikiPage(id, "https://github.com/TheBusyBiscuit/Slimefun4/wiki/" + page);
2016-04-14 16:24:03 +00:00
}
2017-06-13 17:59:39 +00:00
2016-04-14 16:24:03 +00:00
public static List<GuideHandler> getGuideHandlers(int tier) {
2019-08-31 10:54:54 +00:00
return SlimefunPlugin.getUtilities().guideHandlers.getOrDefault(tier, new ArrayList<>());
2016-04-14 16:24:03 +00:00
}
2019-08-31 10:12:38 +00:00
public static String getVersion() {
return SlimefunPlugin.instance.getDescription().getVersion();
}
2017-06-13 17:59:39 +00:00
}