2016-04-14 16:24:03 +00:00
|
|
|
package me.mrCookieSlime.Slimefun.api;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
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
|
|
|
|
2019-10-11 22:56:51 +00:00
|
|
|
import org.bukkit.Bukkit;
|
2020-02-02 15:11:18 +00:00
|
|
|
import org.bukkit.NamespacedKey;
|
2017-04-09 07:59:45 +00:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
2019-12-27 10:10:22 +00:00
|
|
|
import org.bukkit.scheduler.BukkitTask;
|
2017-04-09 07:59:45 +00:00
|
|
|
|
2019-12-27 01:16:01 +00:00
|
|
|
import io.github.thebusybiscuit.cscorelib2.config.Config;
|
2020-03-05 22:38:39 +00:00
|
|
|
import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem;
|
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.Objects.Research;
|
2019-09-06 08:34:17 +00:00
|
|
|
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.ItemState;
|
2016-04-14 16:24:03 +00:00
|
|
|
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
|
|
|
|
|
2017-08-15 14:37:25 +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
|
|
|
|
2020-03-05 00:01:38 +00:00
|
|
|
private Slimefun() {}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
public static void registerGuideHandler(GuideHandler handler) {
|
|
|
|
List<GuideHandler> handlers = SlimefunPlugin.getRegistry().getGuideHandlers().getOrDefault(handler.getTier(), new ArrayList<>());
|
|
|
|
handlers.add(handler);
|
|
|
|
SlimefunPlugin.getRegistry().getGuideHandlers().put(handler.getTier(), handlers);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Logger getLogger() {
|
|
|
|
return SlimefunPlugin.instance.getLogger();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public static Object getItemValue(String id, String key) {
|
|
|
|
return getItemConfig().getValue(id + '.' + key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public static void setItemVariable(String id, String key, Object value) {
|
|
|
|
getItemConfig().setDefaultValue(id + '.' + key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Config instance of Items.yml file.
|
|
|
|
* <p>
|
|
|
|
* It calls {@code SlimefunStartup#getItemCfg()}.
|
|
|
|
*
|
|
|
|
* @return the Items.yml Config instance.
|
|
|
|
*/
|
|
|
|
public static Config getItemConfig() {
|
|
|
|
return SlimefunPlugin.getItemCfg();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public static void registerResearch(Research research, ItemStack... items) {
|
|
|
|
for (ItemStack item : items) {
|
|
|
|
research.addItems(SlimefunItem.getByItem(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
research.register();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void registerResearch(NamespacedKey key, int id, String name, int cost, ItemStack... items) {
|
|
|
|
Research research = new Research(key, id, name, cost);
|
|
|
|
|
|
|
|
for (ItemStack item : items) {
|
|
|
|
research.addItems(SlimefunItem.getByItem(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
research.register();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public static boolean hasUnlocked(Player p, ItemStack item, boolean message) {
|
|
|
|
SlimefunItem sfItem = SlimefunItem.getByItem(item);
|
|
|
|
|
|
|
|
if (sfItem != null) {
|
|
|
|
if (sfItem.getState() == ItemState.DISABLED) {
|
|
|
|
if (message) SlimefunPlugin.getLocal().sendMessage(p, "messages.disabled-item", true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isEnabled(p, item, message) && hasPermission(p, sfItem, message)) {
|
|
|
|
if (sfItem.getResearch() == null) return true;
|
|
|
|
else if (PlayerProfile.get(p).hasUnlocked(sfItem.getResearch())) return true;
|
|
|
|
else {
|
|
|
|
if (message && !(sfItem instanceof VanillaItem)) {
|
|
|
|
SlimefunPlugin.getLocal().sendMessage(p, "messages.not-researched", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
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.get(p).hasUnlocked(sfItem.getResearch())) return true;
|
|
|
|
else {
|
|
|
|
if (message && !(sfItem instanceof VanillaItem)) SlimefunPlugin.getLocal().sendMessage(p, "messages.not-researched", true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public static boolean hasPermission(Player p, SlimefunItem item, boolean message) {
|
|
|
|
if (item == null) return true;
|
|
|
|
else if (item.getPermission().equalsIgnoreCase("")) return true;
|
|
|
|
else if (p.hasPermission(item.getPermission())) return true;
|
|
|
|
else {
|
|
|
|
if (message) SlimefunPlugin.getLocal().sendMessage(p, "messages.no-permission", true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public static boolean isEnabled(Player p, ItemStack item, boolean message) {
|
|
|
|
SlimefunItem sfItem = SlimefunItem.getByItem(item);
|
|
|
|
|
|
|
|
if (sfItem == null) return true;
|
|
|
|
else return isEnabled(p, sfItem, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public static boolean isEnabled(Player p, SlimefunItem sfItem, boolean message) {
|
|
|
|
if (sfItem.isDisabled()) {
|
|
|
|
if (message) SlimefunPlugin.getLocal().sendMessage(p, "messages.disabled-in-world", true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String world = p.getWorld().getName();
|
|
|
|
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;
|
|
|
|
else {
|
|
|
|
if (message) SlimefunPlugin.getLocal().sendMessage(p, "messages.disabled-in-world", true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (message) SlimefunPlugin.getLocal().sendMessage(p, "messages.disabled-in-world", true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
public static List<GuideHandler> getGuideHandlers(int tier) {
|
|
|
|
return SlimefunPlugin.getRegistry().getGuideHandlers().getOrDefault(tier, new ArrayList<>());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static BukkitTask runSync(Runnable r) {
|
|
|
|
return Bukkit.getScheduler().runTask(SlimefunPlugin.instance, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static BukkitTask runSync(Runnable r, long delay) {
|
|
|
|
return Bukkit.getScheduler().runTaskLater(SlimefunPlugin.instance, r, delay);
|
|
|
|
}
|
2017-06-13 17:59:39 +00:00
|
|
|
}
|