2013-07-13 17:14:39 +02:00
|
|
|
package com.earth2me.essentials.api;
|
2011-12-06 16:32:06 +01:00
|
|
|
|
2013-07-13 19:52:53 +02:00
|
|
|
import com.earth2me.essentials.User;
|
2018-12-15 19:54:01 +01:00
|
|
|
import com.earth2me.essentials.utils.MaterialUtil;
|
|
|
|
import com.earth2me.essentials.utils.NumberUtil;
|
|
|
|
import com.earth2me.essentials.utils.StringUtil;
|
2017-12-13 07:06:25 +01:00
|
|
|
import org.bukkit.Material;
|
2013-07-13 19:40:46 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2011-12-06 16:32:06 +01:00
|
|
|
|
2017-06-11 02:17:43 +02:00
|
|
|
import java.util.Collection;
|
2015-04-15 06:06:16 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2020-10-04 18:03:52 +02:00
|
|
|
/**
|
|
|
|
* Provides access to the current item alias registry.
|
|
|
|
*
|
|
|
|
* @deprecated External plugins should use {@link net.ess3.api.IItemDb} instead, which includes access to {@link net.ess3.api.IItemDb.ItemResolver}
|
|
|
|
* APIs.
|
|
|
|
*/
|
|
|
|
@Deprecated
|
2015-04-15 06:06:16 +02:00
|
|
|
public interface IItemDb {
|
|
|
|
|
2018-12-15 19:54:01 +01:00
|
|
|
/**
|
|
|
|
* Create a stack from the given name with the given quantity.
|
|
|
|
*
|
2020-10-03 19:46:05 +02:00
|
|
|
* @param name Item name to look up in the database
|
2018-12-15 19:54:01 +01:00
|
|
|
* @param quantity Quantity of the item stack
|
|
|
|
* @return The requested item stack
|
|
|
|
* @throws Exception if the item stack cannot be created
|
|
|
|
*/
|
|
|
|
default ItemStack get(final String name, final int quantity) throws Exception {
|
2021-01-02 05:16:24 +01:00
|
|
|
final ItemStack stack = get(name);
|
2018-12-15 19:54:01 +01:00
|
|
|
stack.setAmount(quantity);
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a stack from the given name with the maximum stack size for that material.
|
2020-10-03 19:46:05 +02:00
|
|
|
* <p>
|
2019-04-15 11:09:16 +02:00
|
|
|
* Note that this will always check against resolver functions from other plugins as well.
|
|
|
|
* To avoid this behaviour, use net.ess3.api.IItemDb#get(String name, boolean useResolvers).
|
|
|
|
*
|
2018-12-15 19:54:01 +01:00
|
|
|
* @param name Item name to look up in the database
|
|
|
|
* @return The requested item stack with the maximum stack size
|
|
|
|
* @throws Exception if the item stack cannot be created
|
|
|
|
*/
|
2015-04-15 06:06:16 +02:00
|
|
|
ItemStack get(final String name) throws Exception;
|
|
|
|
|
2018-12-15 19:54:01 +01:00
|
|
|
/**
|
|
|
|
* Get a comma-separated string list of up to 15 aliases for the given stack.
|
|
|
|
*
|
|
|
|
* @param item Item stack whose names to find
|
|
|
|
* @return Comma-separated list of up to 15 item names
|
|
|
|
*/
|
2020-10-03 19:46:05 +02:00
|
|
|
default String names(final ItemStack item) {
|
2018-12-15 19:54:01 +01:00
|
|
|
List<String> nameList = nameList(item);
|
|
|
|
|
|
|
|
if (nameList.size() > 15) {
|
|
|
|
nameList = nameList.subList(0, 14);
|
|
|
|
}
|
|
|
|
return StringUtil.joinList(", ", nameList);
|
2020-04-25 14:08:57 +02:00
|
|
|
}
|
2013-07-13 19:40:46 +02:00
|
|
|
|
2018-12-15 19:54:01 +01:00
|
|
|
/**
|
|
|
|
* Get a List of all aliases for the given item stack.
|
|
|
|
*
|
|
|
|
* @param item Item stack whose names to find
|
|
|
|
* @return List of all names
|
|
|
|
*/
|
2018-10-12 18:26:22 +02:00
|
|
|
List<String> nameList(ItemStack item);
|
|
|
|
|
2018-12-15 19:54:01 +01:00
|
|
|
/**
|
|
|
|
* Get the primary name for the given item stack.
|
|
|
|
*
|
|
|
|
* @param item Item stack whose name to find
|
|
|
|
* @return Primary name of the item
|
|
|
|
*/
|
2015-06-03 22:11:56 +02:00
|
|
|
String name(ItemStack item);
|
2013-04-30 18:25:25 +02:00
|
|
|
|
2018-12-15 19:54:01 +01:00
|
|
|
/**
|
|
|
|
* Get all stacks in a given User's inventory that matches the given arguments.
|
|
|
|
*
|
|
|
|
* @param user The user with the player inventory to search
|
|
|
|
* @param args Either an item name, or one of the following:
|
|
|
|
* hand (default), inventory/invent/all, blocks
|
|
|
|
* @return A List of all matching ItemStacks
|
|
|
|
* @throws Exception if the given args are invalid or no blocks are found
|
|
|
|
*/
|
2015-04-15 06:06:16 +02:00
|
|
|
List<ItemStack> getMatching(User user, String[] args) throws Exception;
|
2015-04-20 17:56:45 +02:00
|
|
|
|
2018-12-15 19:54:01 +01:00
|
|
|
/**
|
2020-06-15 14:46:44 +02:00
|
|
|
* Converts the given {@link ItemStack} to a string representation that can be saved.
|
|
|
|
* This is typically used for /createkit but can be used by other plugins for various purposes too.
|
|
|
|
* Note that this will try registered resolvers first - to avoid this, use {@link net.ess3.api.IItemDb#serialize(ItemStack, boolean)} instead.
|
2018-12-15 19:54:01 +01:00
|
|
|
*
|
2020-06-15 14:46:44 +02:00
|
|
|
* @param is The stack to serialize
|
|
|
|
* @return A string representing the given stack
|
2018-12-15 19:54:01 +01:00
|
|
|
*/
|
2015-06-03 22:11:56 +02:00
|
|
|
String serialize(ItemStack is);
|
2017-06-11 02:17:43 +02:00
|
|
|
|
2018-12-15 19:54:01 +01:00
|
|
|
/**
|
|
|
|
* Return names recognised by the database, intended for tab-completion.
|
|
|
|
*
|
|
|
|
* @return Collection of all item names
|
|
|
|
*/
|
2017-06-11 02:17:43 +02:00
|
|
|
Collection<String> listNames();
|
2017-12-13 07:06:25 +01:00
|
|
|
|
2018-12-15 19:54:01 +01:00
|
|
|
/**
|
|
|
|
* Get the material matching the given legacy ID. Used for conversion from item IDs to
|
|
|
|
* modern names.
|
|
|
|
*
|
|
|
|
* @param id Legacy ID of material to find
|
|
|
|
* @return Updated material
|
|
|
|
*/
|
2018-10-12 18:26:22 +02:00
|
|
|
@Deprecated
|
2020-10-03 19:46:05 +02:00
|
|
|
default Material getFromLegacyId(final int id) {
|
2018-12-15 19:54:01 +01:00
|
|
|
return getFromLegacy(id, (byte) 0);
|
|
|
|
}
|
2017-12-13 07:06:25 +01:00
|
|
|
|
2018-12-15 19:54:01 +01:00
|
|
|
/**
|
|
|
|
* Get the legacy ID for the given material.
|
|
|
|
*
|
|
|
|
* @param material Material to look up
|
|
|
|
* @return Legacy ID of given material
|
|
|
|
* @throws Exception if the ID cannot be looked up
|
|
|
|
* @deprecated Item IDs are no longer supported.
|
|
|
|
*/
|
2018-10-12 18:26:22 +02:00
|
|
|
@Deprecated
|
2017-12-13 07:06:25 +01:00
|
|
|
int getLegacyId(Material material) throws Exception;
|
2018-12-15 19:54:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert colon syntax (eg. "13", "1:5") legacy IDs to Material. Used for conversion from
|
|
|
|
* item IDs to modern names.
|
|
|
|
*
|
|
|
|
* @param item Legacy ID in colon syntax.
|
2019-08-13 16:11:43 +02:00
|
|
|
* @return Material if an appropriate material exists, else null.
|
2018-12-15 19:54:01 +01:00
|
|
|
*/
|
2020-10-03 19:46:05 +02:00
|
|
|
default Material getFromLegacy(final String item) {
|
2018-12-15 19:54:01 +01:00
|
|
|
final String[] split = item.split(":");
|
|
|
|
|
2019-08-13 16:11:43 +02:00
|
|
|
if (!NumberUtil.isInt(split[0])) return null;
|
|
|
|
|
2018-12-15 19:54:01 +01:00
|
|
|
final int id = Integer.parseInt(split[0]);
|
|
|
|
byte damage = 0;
|
|
|
|
|
|
|
|
if (split.length > 1 && NumberUtil.isInt(split[1])) {
|
|
|
|
damage = Byte.parseByte(split[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return getFromLegacy(id, damage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert legacy ID and damage value to Material. Used for conversion from item IDs to
|
|
|
|
* modern names.
|
|
|
|
*
|
2020-10-03 19:46:05 +02:00
|
|
|
* @param id Legacy ID
|
2018-12-15 19:54:01 +01:00
|
|
|
* @param damage Damage value
|
|
|
|
* @return Material
|
|
|
|
*/
|
|
|
|
default Material getFromLegacy(final int id, final byte damage) {
|
|
|
|
return MaterialUtil.convertFromLegacy(id, damage);
|
|
|
|
}
|
2019-01-12 21:44:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the item database is loaded and ready for use.
|
|
|
|
*
|
|
|
|
* @return Whether items have finished loading
|
|
|
|
*/
|
|
|
|
boolean isReady();
|
2011-12-06 16:32:06 +01:00
|
|
|
}
|