版本更新至:3.76
调整:开发框架改为 Gradle 新增:Language2 工具新增 [book] 类型
This commit is contained in:
51
src/main/java/me/skymc/taboolib/inventory/DropUtils.java
Normal file
51
src/main/java/me/skymc/taboolib/inventory/DropUtils.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package me.skymc.taboolib.inventory;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Item;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
|
||||
public class DropUtils {
|
||||
|
||||
public static Item drop(Player player, ItemStack itemStack, double bulletSpread, double radius) {
|
||||
Location location = player.getLocation();
|
||||
location.setY(location.getY() + 1.5);
|
||||
|
||||
Item item = player.getWorld().dropItem(location, itemStack);
|
||||
|
||||
double yaw = Math.toRadians(-player.getLocation().getYaw() - 90.0F);
|
||||
double pitch = Math.toRadians(-player.getLocation().getPitch());
|
||||
|
||||
double x = 0;
|
||||
double y = 0;
|
||||
double z = 0;
|
||||
|
||||
if (bulletSpread > 0) {
|
||||
double[] spread = { 1.0D, 1.0D, 1.0D };
|
||||
for (int t = 0; t < 3; t++) {
|
||||
spread[t] = ((NumberUtils.getRand().nextDouble() - NumberUtils.getRand().nextDouble()) * bulletSpread * 0.1D);
|
||||
}
|
||||
|
||||
x = Math.cos(pitch) * Math.cos(yaw) + spread[0];
|
||||
y = Math.sin(pitch) + spread[1];
|
||||
z = -Math.sin(yaw) * Math.cos(pitch) + spread[2];
|
||||
}
|
||||
else {
|
||||
x = Math.cos(pitch) * Math.cos(yaw);
|
||||
y = Math.sin(pitch);
|
||||
z = -Math.sin(yaw) * Math.cos(pitch);
|
||||
}
|
||||
Vector dirVel = new Vector(x, y, z);
|
||||
dirVel.normalize().multiply(radius);
|
||||
|
||||
item.setVelocity(dirVel);
|
||||
return item;
|
||||
}
|
||||
|
||||
}
|
||||
140
src/main/java/me/skymc/taboolib/inventory/InventoryUtil.java
Normal file
140
src/main/java/me/skymc/taboolib/inventory/InventoryUtil.java
Normal file
@@ -0,0 +1,140 @@
|
||||
package me.skymc.taboolib.inventory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
|
||||
public class InventoryUtil {
|
||||
|
||||
public final static LinkedList<Integer> SLOT_OF_CENTENTS = new LinkedList<>(Arrays.asList(
|
||||
10, 11, 12, 13, 14, 15, 16,
|
||||
19, 20, 21, 22, 23, 24, 25,
|
||||
28, 29, 30, 31, 32, 33, 34,
|
||||
37, 38, 39, 40, 41, 42, 43));
|
||||
|
||||
@Deprecated
|
||||
public static boolean isEmpey(Player p) {
|
||||
return isEmpty(p, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查背包是否有空位
|
||||
*
|
||||
* @param p 玩家
|
||||
* @param i 起始位置
|
||||
*/
|
||||
public static boolean isEmpty(Player p, int i) {
|
||||
while (i < 35) {
|
||||
if (p.getInventory().getItem(i) == null) {
|
||||
return true;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测玩家是否有指定物品
|
||||
*
|
||||
* @param player 玩家
|
||||
* @param item 物品
|
||||
* @param amount 数量
|
||||
* @param remove 是否删除
|
||||
*/
|
||||
public static boolean hasItem(Player player, ItemStack item, int amount, boolean remove) {
|
||||
int hasAmount = 0;
|
||||
for (ItemStack _item : player.getInventory()) {
|
||||
if (item.isSimilar(_item)) {
|
||||
hasAmount += _item.getAmount();
|
||||
}
|
||||
}
|
||||
if (hasAmount < amount) {
|
||||
return false;
|
||||
}
|
||||
int requireAmount = amount;
|
||||
for (int i = 0; i < player.getInventory().getSize() && remove; i++) {
|
||||
ItemStack _item = player.getInventory().getItem(i);
|
||||
if (_item != null && _item.isSimilar(item)) {
|
||||
/**
|
||||
* 如果循环到的物品数量 小于 需要的数量
|
||||
* 则 删除物品,减少需要的数量
|
||||
*/
|
||||
if (_item.getAmount() < requireAmount) {
|
||||
player.getInventory().setItem(i, null);
|
||||
requireAmount -= _item.getAmount();
|
||||
}
|
||||
/**
|
||||
* 如果循环到的物品数量 等于 需要的数量
|
||||
* 则 删除物品,直接结束
|
||||
*/
|
||||
else if (_item.getAmount() == requireAmount) {
|
||||
player.getInventory().setItem(i, null);
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 如果循环到的物品数量 大于 需要的数量
|
||||
* 则扣除 需要的数量
|
||||
*/
|
||||
else {
|
||||
_item.setAmount(_item.getAmount() - requireAmount);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean hasItem(Inventory targetInventory, ItemStack targetItem, Integer amount) {
|
||||
int inventoryAmount = 0;
|
||||
for (ItemStack item : targetInventory) {
|
||||
if (item != null) {
|
||||
if (item.isSimilar(targetItem)) {
|
||||
inventoryAmount += item.getAmount();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (inventoryAmount >= amount) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean takeItem2(Inventory inv, ItemStack takeitem, Integer amount) {
|
||||
for (int i = 0; i < inv.getSize(); ++i) {
|
||||
if (amount <= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
ItemStack item = inv.getItem(i);
|
||||
if (item == null) {
|
||||
continue;
|
||||
}
|
||||
if (!item.isSimilar(takeitem)) {
|
||||
continue;
|
||||
}
|
||||
if (item.getAmount() >= amount) {
|
||||
if (item.getAmount() - amount == 0) {
|
||||
inv.setItem(i, null);
|
||||
}
|
||||
else {
|
||||
item.setAmount(item.getAmount() - amount);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
amount -= item.getAmount();
|
||||
inv.setItem(i, null);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
608
src/main/java/me/skymc/taboolib/inventory/ItemUtils.java
Normal file
608
src/main/java/me/skymc/taboolib/inventory/ItemUtils.java
Normal file
@@ -0,0 +1,608 @@
|
||||
package me.skymc.taboolib.inventory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.inventory.meta.LeatherArmorMeta;
|
||||
import org.bukkit.inventory.meta.PotionMeta;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import me.skymc.taboolib.itemnbtapi.NBTItem;
|
||||
import me.skymc.taboolib.itemnbtapi.NBTList;
|
||||
import me.skymc.taboolib.itemnbtapi.NBTListCompound;
|
||||
import me.skymc.taboolib.itemnbtapi.NBTType;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
import me.skymc.taboolib.string.Language;
|
||||
|
||||
public class ItemUtils {
|
||||
|
||||
@Getter
|
||||
private static FileConfiguration itemdir = null;
|
||||
|
||||
@Getter
|
||||
private static FileConfiguration itemCache = null;
|
||||
|
||||
@Getter
|
||||
private static File finalItemsFolder;
|
||||
|
||||
@Getter
|
||||
private static LinkedHashMap<String, String> itemlib = new LinkedHashMap<>();
|
||||
|
||||
@Getter
|
||||
private static LinkedHashMap<String, ItemStack> itemCaches = new LinkedHashMap<>();
|
||||
|
||||
@Getter
|
||||
private static LinkedHashMap<String, ItemStack> itemCachesFinal = new LinkedHashMap<>();
|
||||
|
||||
/**
|
||||
* 获取物品缓存
|
||||
* 检测顺序:
|
||||
* 1. 固定物品库
|
||||
* 2. 动态物品库
|
||||
*
|
||||
* @param name 物品名称
|
||||
* @return
|
||||
*/
|
||||
public static ItemStack getCacheItem(String name) {
|
||||
// 检测固定物品库是否存在该物品
|
||||
if (itemCachesFinal.containsKey(name)) {
|
||||
return itemCachesFinal.get(name);
|
||||
}
|
||||
// 返回动态物品库
|
||||
return itemCaches.get(name);
|
||||
}
|
||||
|
||||
public static boolean isExists(String name) {
|
||||
return itemCachesFinal.containsKey(name) || itemCaches.containsKey(name);
|
||||
}
|
||||
|
||||
public static void LoadLib() {
|
||||
itemdir = YamlConfiguration.loadConfiguration(new File(Main.getInst().getConfig().getString("DATAURL.ITEMDIR")));
|
||||
reloadItemName();
|
||||
reloadItemCache();
|
||||
}
|
||||
|
||||
public static void loadItemsFile(File file, boolean finalFile) {
|
||||
FileConfiguration conf = ConfigUtils.load(Main.getInst(), file);
|
||||
for (String name : conf.getConfigurationSection("").getKeys(false)) {
|
||||
if (isExists(name)) {
|
||||
MsgUtils.warn("无法载入载入物品 &4" + name + "&c, 因为它已经存在了");
|
||||
} else if (finalFile) {
|
||||
itemCachesFinal.put(name, loadItem(conf, name));
|
||||
} else {
|
||||
itemCaches.put(name, loadItem(conf, name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void reloadItemCache() {
|
||||
itemCaches.clear();
|
||||
itemCachesFinal.clear();
|
||||
loadItemsFile(getItemCacheFile(), false);
|
||||
// 创建固定物品库
|
||||
finalItemsFolder = new File(Main.getInst().getDataFolder(), "FinalItems");
|
||||
if (!finalItemsFolder.exists()) {
|
||||
finalItemsFolder.mkdir();
|
||||
}
|
||||
// 检查固定物品库中的物品
|
||||
for (File file : finalItemsFolder.listFiles()) {
|
||||
loadItemsFile(file, true);
|
||||
}
|
||||
MsgUtils.send("载入 " + (itemCaches.size() + itemCachesFinal.size()) + " 项缓存物品");
|
||||
}
|
||||
|
||||
public static void reloadItemName() {
|
||||
FileConfiguration conf = new Language("ITEM_NAME", Main.getInst(), true).getConfiguration();
|
||||
itemlib.clear();
|
||||
for (String a : conf.getConfigurationSection("").getKeys(false)) {
|
||||
itemlib.put(a, conf.getString(a));
|
||||
}
|
||||
MsgUtils.send("载入 " + itemlib.size() + " 项物品名称");
|
||||
}
|
||||
|
||||
public static File getItemCacheFile() {
|
||||
File itemCacheFile = new File(Main.getInst().getDataFolder(), "items.yml");
|
||||
if (!itemCacheFile.exists()) {
|
||||
Main.getInst().saveResource("items.yml", true);
|
||||
}
|
||||
return itemCacheFile;
|
||||
}
|
||||
|
||||
public static String getCustomName(ItemStack item) {
|
||||
if (item == null || item.getType().equals(Material.AIR)) {
|
||||
return "空";
|
||||
}
|
||||
int data = item.getType().getMaxDurability() == 0 ? item.getDurability() : 0;
|
||||
return item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : itemlib.get(item.getType() + ":" + data) == null ? item.getType().toString() : itemlib.get(item.getType() + ":" + data);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static ItemStack getItemFromDir(String name) {
|
||||
if (itemdir != null) {
|
||||
return itemdir.getItemStack("item." + name);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static ItemStack item(int n, int a, int d) {
|
||||
ItemStack item = new ItemStack(n, a, (short)d);
|
||||
return item;
|
||||
}
|
||||
|
||||
public static ItemStack setName(ItemStack i, String n) {
|
||||
ItemMeta meta = i.getItemMeta();
|
||||
meta.setDisplayName(n);
|
||||
i.setItemMeta(meta);
|
||||
return i;
|
||||
}
|
||||
|
||||
public static ItemStack Enchant(ItemStack i, Enchantment e, int l) {
|
||||
ItemMeta meta = i.getItemMeta();
|
||||
meta.addEnchant(e, l, false);
|
||||
i.setItemMeta(meta);
|
||||
return i;
|
||||
}
|
||||
|
||||
public static ItemStack addFlag(ItemStack i, ItemFlag f) {
|
||||
ItemMeta meta = i.getItemMeta();
|
||||
meta.addItemFlags(f);
|
||||
i.setItemMeta(meta);
|
||||
return i;
|
||||
}
|
||||
|
||||
public static boolean isNull(ItemStack item) {
|
||||
return item == null || item.getType().equals(Material.AIR);
|
||||
}
|
||||
|
||||
public static boolean isName(ItemStack i, String a) {
|
||||
if (!isNamed(i) || i.getItemMeta() == null || i.getItemMeta().getDisplayName() == null || !i.getItemMeta().getDisplayName().equals(a)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isNameAs(ItemStack i, String a) {
|
||||
if (!isNamed(i) || !i.getItemMeta().getDisplayName().contains(a)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static String asString(String args, Player placeholderPlayer) {
|
||||
if (placeholderPlayer == null) {
|
||||
return args.replace("&", "§");
|
||||
}
|
||||
return PlaceholderAPI.setPlaceholders(placeholderPlayer, args.replace("&", "§"));
|
||||
}
|
||||
|
||||
public static List<String> asString(List<String> args, Player placeholderPlayer) {
|
||||
for (int i = 0 ; i < args.size() ; i ++) {
|
||||
args.set(i, asString(args.get(i), placeholderPlayer));
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
public static ItemFlag asItemFlag(String flag) {
|
||||
try {
|
||||
return ItemFlag.valueOf(flag);
|
||||
}
|
||||
catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static Material asMaterial(String args) {
|
||||
try {
|
||||
Material material = Material.getMaterial(args);
|
||||
return material != null ? material : Material.getMaterial(Integer.valueOf(args));
|
||||
}
|
||||
catch (Exception e) {
|
||||
return Material.STONE;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "deprecation" })
|
||||
public static Enchantment asEnchantment(String enchant) {
|
||||
try {
|
||||
Enchantment enchantment = Enchantment.getByName(enchant);
|
||||
return enchantment != null ? enchantment : Enchantment.getById(Integer.valueOf(enchant));
|
||||
}
|
||||
catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static PotionEffectType asPotionEffectType(String potion) {
|
||||
try {
|
||||
PotionEffectType type = PotionEffectType.getByName(potion);
|
||||
return type != null ? type : PotionEffectType.getById(Integer.valueOf(potion));
|
||||
}
|
||||
catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Color asColor(String color) {
|
||||
try {
|
||||
return Color.fromBGR(Integer.valueOf(color.split("-")[0]), Integer.valueOf(color.split("-")[1]), Integer.valueOf(color.split("-")[2]));
|
||||
}
|
||||
catch (Exception e) {
|
||||
return Color.fromBGR(0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public static String asAttribute(String name) {
|
||||
if (name.toLowerCase().equals("damage")) {
|
||||
return "generic.attackDamage";
|
||||
}
|
||||
else if (name.toLowerCase().equals("attackspeed")) {
|
||||
return "generic.attackSpeed";
|
||||
}
|
||||
else if (name.toLowerCase().equals("health")) {
|
||||
return "generic.maxHealth";
|
||||
}
|
||||
else if (name.toLowerCase().equals("speed")) {
|
||||
return "generic.movementSpeed";
|
||||
}
|
||||
else if (name.toLowerCase().equals("knockback")) {
|
||||
return "generic.knockbackResistance";
|
||||
}
|
||||
else if (name.toLowerCase().equals("armor")) {
|
||||
return "generic.armor";
|
||||
}
|
||||
else if (name.toLowerCase().equals("luck")) {
|
||||
return "generic.luck";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 包含介绍
|
||||
*
|
||||
* @param i 物品
|
||||
* @param a 关键字
|
||||
*/
|
||||
public static boolean hasLore(ItemStack i, String a) {
|
||||
if (!isLored(i) || !i.getItemMeta().getLore().toString().contains(a)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果已描述
|
||||
*
|
||||
* @param i
|
||||
* @return
|
||||
*/
|
||||
public static boolean isLored(ItemStack i) {
|
||||
if (i == null || i.getItemMeta() == null || i.getItemMeta().getLore() == null) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果已命名
|
||||
*
|
||||
* @param i
|
||||
* @return
|
||||
*/
|
||||
public static boolean isNamed(ItemStack i) {
|
||||
if (i == null || i.getItemMeta() == null || i.getItemMeta().getDisplayName() == null) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加描述
|
||||
*
|
||||
* @param is 物品
|
||||
* @param lore 描述
|
||||
*/
|
||||
public static ItemStack addLore(ItemStack is, String lore) {
|
||||
ItemMeta meta = is.getItemMeta();
|
||||
|
||||
List<String> _lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
|
||||
_lore.add(lore.replaceAll("&", "§"));
|
||||
|
||||
is.setItemMeta(meta);
|
||||
return is;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除描述
|
||||
*
|
||||
* @param is 物品
|
||||
* @param line 行数
|
||||
*/
|
||||
public static ItemStack delLore(ItemStack is, int line) {
|
||||
ItemMeta meta = is.getItemMeta();
|
||||
if (meta.hasLore()) {
|
||||
List<String> l = meta.getLore();
|
||||
if (l.size() >= line) {
|
||||
l.remove(line);
|
||||
meta.setLore(l);
|
||||
is.setItemMeta(meta);
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取介绍所在行数
|
||||
*
|
||||
* @param i 物品
|
||||
* @param a 关键字
|
||||
*/
|
||||
public static int getLore(ItemStack i, String a) {
|
||||
if (isLored(i)) {
|
||||
for (int j = 0; j < i.getItemMeta().getLore().size() ; j++) {
|
||||
if (i.getItemMeta().getLore().get(j).contains(a)) {
|
||||
return j;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加耐久
|
||||
*
|
||||
* @param i 物品
|
||||
* @param d 耐久
|
||||
*/
|
||||
public static ItemStack addDurability(ItemStack i, int d) {
|
||||
i.setDurability((short) (i.getDurability() + d));
|
||||
int min = i.getDurability();
|
||||
int max = i.getType().getMaxDurability();
|
||||
if (min >= max) {
|
||||
i.setType(Material.AIR);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换描述
|
||||
*
|
||||
* @param i 物品
|
||||
* @param l1 关键字1
|
||||
* @param l2 关键字2
|
||||
*/
|
||||
public static ItemStack repalceLore(ItemStack i, String l1, String l2) {
|
||||
if (!isLored(i)) {
|
||||
return i;
|
||||
}
|
||||
else {
|
||||
ItemMeta meta = i.getItemMeta();
|
||||
List<String> lore = meta.getLore();
|
||||
for (int j = 0 ; j < lore.size() ; j++) {
|
||||
lore.set(j, lore.get(j).replace(l1, l2));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
i.setItemMeta(meta);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
public static ItemStack loadItem(FileConfiguration f, String s) {
|
||||
return loadItem(f, s, null);
|
||||
}
|
||||
|
||||
public static ItemStack loadItem(FileConfiguration f, String s, Player papiPlayer) {
|
||||
return loadItem(f.getConfigurationSection(s), papiPlayer);
|
||||
}
|
||||
|
||||
public static ItemStack loadItem(ConfigurationSection section, Player papiPlayer) {
|
||||
if (section.get("bukkit") instanceof ItemStack) {
|
||||
return section.getItemStack("bukkit");
|
||||
}
|
||||
// 材质
|
||||
ItemStack item = new ItemStack(asMaterial(section.get("material").toString()));
|
||||
// 数量
|
||||
item.setAmount(section.contains("amount") ? section.getInt("amount") : 1);
|
||||
// 耐久
|
||||
item.setDurability((short) section.getInt("data"));
|
||||
// 元数据
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
// 展示名
|
||||
if (section.contains("name")) {
|
||||
meta.setDisplayName(asString(section.getString("name"), papiPlayer));
|
||||
}
|
||||
// 描述
|
||||
if (section.contains("lore")) {
|
||||
meta.setLore(asString(section.getStringList("lore"), papiPlayer));
|
||||
}
|
||||
// 附魔
|
||||
if (section.contains("enchants")) {
|
||||
for (String preEnchant : section.getConfigurationSection("enchants").getKeys(false)) {
|
||||
Enchantment enchant = asEnchantment(preEnchant);
|
||||
if (enchant != null) {
|
||||
meta.addEnchant(enchant, section.getInt("enchants." + preEnchant), true);
|
||||
}
|
||||
else {
|
||||
MsgUtils.warn("&8" + preEnchant + " &c不是一个有效的附魔名称");
|
||||
MsgUtils.warn("&c输入 &4/taboolib enchants&c 查看所有附魔");
|
||||
}
|
||||
}
|
||||
}
|
||||
// 标签
|
||||
if (section.contains("flags") && TabooLib.getVerint() > 10700) {
|
||||
for (String preFlag : section.getStringList("flags")) {
|
||||
ItemFlag flag = asItemFlag(preFlag);
|
||||
if (flag != null) {
|
||||
meta.addItemFlags(flag);
|
||||
}
|
||||
else {
|
||||
MsgUtils.warn("&8" + preFlag + " &c不是一个有效的标签名称");
|
||||
MsgUtils.warn("&c输入 &4/taboolib flags&c 查看所有标签");
|
||||
}
|
||||
}
|
||||
}
|
||||
// 皮革
|
||||
if (meta instanceof LeatherArmorMeta && section.contains("color")) {
|
||||
((LeatherArmorMeta) meta).setColor(asColor(section.getString("color")));
|
||||
}
|
||||
// 药水
|
||||
if (meta instanceof PotionMeta && section.contains("potions")) {
|
||||
PotionMeta potionMeta = (PotionMeta) meta;
|
||||
for (String prePotionName : section.getConfigurationSection("potions").getKeys(false)) {
|
||||
PotionEffectType potionEffectType = asPotionEffectType(prePotionName);
|
||||
if (potionEffectType != null) {
|
||||
potionMeta.addCustomEffect(new PotionEffect(
|
||||
potionEffectType,
|
||||
NumberUtils.getInteger(section.getString("potions." + prePotionName).split("-")[0]),
|
||||
NumberUtils.getInteger(section.getString("potions." + prePotionName).split("-")[1]) - 1), true);
|
||||
}
|
||||
else {
|
||||
MsgUtils.warn("&8" + potionEffectType + " &c不是一个有效的药水名称");
|
||||
MsgUtils.warn("&c输入 &4/taboolib potions&c 查看所有药水");
|
||||
}
|
||||
}
|
||||
}
|
||||
// 元数据
|
||||
item.setItemMeta(meta);
|
||||
// 数据
|
||||
NBTItem nbt = new NBTItem(item);
|
||||
// 物品标签
|
||||
if (section.contains("nbt")) {
|
||||
for (String name : section.getConfigurationSection("nbt").getKeys(false)) {
|
||||
Object obj = section.get("nbt." + name);
|
||||
if (obj instanceof String) {
|
||||
nbt.setString(name, obj.toString());
|
||||
}
|
||||
else if (obj instanceof Double) {
|
||||
nbt.setDouble(name, Double.valueOf(obj.toString()));
|
||||
}
|
||||
else if (obj instanceof Integer) {
|
||||
nbt.setInteger(name, Integer.valueOf(obj.toString()));
|
||||
}
|
||||
else if (obj instanceof Long) {
|
||||
nbt.setLong(name, Long.valueOf(obj.toString()));
|
||||
}
|
||||
else {
|
||||
nbt.setObject(name, obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 物品属性
|
||||
if (section.contains("attributes")) {
|
||||
NBTList attr = nbt.getList("AttributeModifiers", NBTType.NBTTagCompound);
|
||||
for (String hand : section.getConfigurationSection("attributes").getKeys(false)) {
|
||||
for (String name : section.getConfigurationSection("attributes." + hand).getKeys(false)) {
|
||||
if (asAttribute(name) != null) {
|
||||
try {
|
||||
NBTListCompound _attr = attr.addCompound();
|
||||
Object num = section.get("attributes." + hand + "." + name);
|
||||
if (num.toString().contains("%")) {
|
||||
_attr.setDouble("Amount", Double.valueOf(num.toString().replace("%", "")) / 100D);
|
||||
_attr.setInteger("Operation", 1);
|
||||
}
|
||||
else {
|
||||
_attr.setDouble("Amount", Double.valueOf(num.toString()));
|
||||
_attr.setInteger("Operation", 0);
|
||||
}
|
||||
_attr.setString("AttributeName", asAttribute(name));
|
||||
_attr.setInteger("UUIDMost", NumberUtils.getRand().nextInt(Integer.MAX_VALUE));
|
||||
_attr.setInteger("UUIDLeast", NumberUtils.getRand().nextInt(Integer.MAX_VALUE));
|
||||
_attr.setString("Name", asAttribute(name));
|
||||
if (!hand.equals("all")) {
|
||||
_attr.setString("Slot", hand);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
MsgUtils.warn("&8" + name + " &c属性载入失败: &8" + e.getMessage());
|
||||
}
|
||||
}
|
||||
else {
|
||||
MsgUtils.warn("&8" + name + " &c不是一个有效的属性名称");
|
||||
MsgUtils.warn("&c输入 &4/taboolib attributes&c 查看所有属性");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nbt.getItem();
|
||||
}
|
||||
|
||||
public static NBTItem setAttribute(NBTItem nbt, String name, Object num, String hand) {
|
||||
NBTList attr = nbt.getList("AttributeModifiers", NBTType.NBTTagCompound);
|
||||
if (asAttribute(name) != null) {
|
||||
try {
|
||||
NBTListCompound _attr = null;
|
||||
for (int i = 0 ; i < attr.size() ; i++) {
|
||||
NBTListCompound nlc = attr.getCompound(i);
|
||||
if (nlc.getString("AttributeName").equals(asAttribute(name))) {
|
||||
_attr = nlc;
|
||||
}
|
||||
}
|
||||
if (_attr == null) {
|
||||
_attr = attr.addCompound();
|
||||
}
|
||||
if (num.toString().contains("%")) {
|
||||
_attr.setDouble("Amount", Double.valueOf(num.toString().replace("%", "")) / 100D);
|
||||
_attr.setInteger("Operation", 1);
|
||||
}
|
||||
else {
|
||||
_attr.setDouble("Amount", Double.valueOf(num.toString()));
|
||||
_attr.setInteger("Operation", 0);
|
||||
}
|
||||
_attr.setString("AttributeName", asAttribute(name));
|
||||
_attr.setInteger("UUIDMost", NumberUtils.getRand().nextInt(Integer.MAX_VALUE));
|
||||
_attr.setInteger("UUIDLeast", NumberUtils.getRand().nextInt(Integer.MAX_VALUE));
|
||||
_attr.setString("Name", asAttribute(name));
|
||||
if (!hand.equals("all")) {
|
||||
_attr.setString("Slot", hand);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
MsgUtils.warn("&8" + name + " &c属性载入失败: &8" + e.getMessage());
|
||||
}
|
||||
}
|
||||
else {
|
||||
MsgUtils.warn("&8" + name + " &c不是一个有效的属性名称");
|
||||
MsgUtils.warn("&c输入 &4/taboolib attributes&c 查看所有属性");
|
||||
}
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void putO(ItemStack item, Inventory inv, int i) {
|
||||
inv.setItem(i, item);
|
||||
inv.setItem(i+1, item);
|
||||
inv.setItem(i+2, item);
|
||||
inv.setItem(i+9, item);
|
||||
inv.setItem(i+10, null);
|
||||
inv.setItem(i+11, item);
|
||||
inv.setItem(i+18, item);
|
||||
inv.setItem(i+19, item);
|
||||
inv.setItem(i+20, item);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package me.skymc.taboolib.inventory.speciaitem;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018年2月17日 下午8:35:42
|
||||
*/
|
||||
public abstract interface AbstractSpecialItem {
|
||||
|
||||
/**
|
||||
* 当接口被载入
|
||||
*/
|
||||
default void onEnable() {}
|
||||
|
||||
/**
|
||||
* 当接口被卸载
|
||||
*/
|
||||
default void onDisable() {}
|
||||
|
||||
/**
|
||||
* 获取识别名称
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
abstract String getName();
|
||||
|
||||
/**
|
||||
* 获取载入插件
|
||||
*
|
||||
* @return {@link Plugin}
|
||||
*/
|
||||
abstract Plugin getPlugin();
|
||||
|
||||
/**
|
||||
* 是否进行点击事件
|
||||
*
|
||||
* @param player 玩家
|
||||
* @param currentItem 点击物品
|
||||
* @param cursorItem 持有物品
|
||||
* @return {@link SpecialItemResult[]}
|
||||
*/
|
||||
abstract SpecialItemResult[] isCorrectClick(Player player, ItemStack currentItem, ItemStack cursorItem);
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
package me.skymc.taboolib.inventory.speciaitem;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018年2月17日 下午8:34:12
|
||||
*/
|
||||
public class SpecialItem implements Listener {
|
||||
|
||||
private static SpecialItem specialItem = null;
|
||||
|
||||
private final List<AbstractSpecialItem> ITEM_DATA = new CopyOnWriteArrayList<>();
|
||||
|
||||
@Getter
|
||||
private boolean isLoaded;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
private SpecialItem() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取工具对象
|
||||
*
|
||||
* @return {@link SpecialItem}
|
||||
*/
|
||||
public static SpecialItem getInst() {
|
||||
if (specialItem == null) {
|
||||
synchronized (SpecialItem.class) {
|
||||
if (specialItem == null) {
|
||||
specialItem = new SpecialItem();
|
||||
// 注册监听器
|
||||
Bukkit.getPluginManager().registerEvents(specialItem, Main.getInst());
|
||||
}
|
||||
}
|
||||
}
|
||||
return specialItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册接口
|
||||
*
|
||||
* @param item 接口对象
|
||||
*/
|
||||
public void register(AbstractSpecialItem item) {
|
||||
if (contains(item.getName())) {
|
||||
MsgUtils.warn("特殊物品接口已存在, 检查名称 &4" + item.getName() + " &c是否重复");
|
||||
}
|
||||
else {
|
||||
ITEM_DATA.add(item);
|
||||
if (isLoaded) {
|
||||
item.onEnable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销接口
|
||||
*
|
||||
* @param name 注册名称
|
||||
*/
|
||||
public void cancel(String name) {
|
||||
for (AbstractSpecialItem specialitem : ITEM_DATA) {
|
||||
if (specialitem.getName() != null && specialitem.getName().equals(specialitem)) {
|
||||
specialitem.onDisable();
|
||||
ITEM_DATA.remove(specialitem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销接口
|
||||
*
|
||||
* @param plugin 注册插件
|
||||
*/
|
||||
public void cancel(Plugin plugin) {
|
||||
for (AbstractSpecialItem specialitem : ITEM_DATA) {
|
||||
if (specialitem.getPlugin() != null && specialitem.getPlugin().equals(plugin)) {
|
||||
specialitem.onDisable();
|
||||
ITEM_DATA.remove(specialitem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断名称是否存在
|
||||
*
|
||||
* @param name 注册名称
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean contains(String name) {
|
||||
for (AbstractSpecialItem specialitem : ITEM_DATA) {
|
||||
if (specialitem.getName().equals(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入所有已注册接口
|
||||
*/
|
||||
public void loadItems() {
|
||||
ITEM_DATA.forEach(x -> x.onEnable());
|
||||
isLoaded = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销所有已注册接口
|
||||
*/
|
||||
public void unloadItems() {
|
||||
ITEM_DATA.forEach(x -> x.onDisable());
|
||||
ITEM_DATA.clear();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDisable(PluginDisableEvent e) {
|
||||
cancel(e.getPlugin());
|
||||
}
|
||||
|
||||
@EventHandler (priority = EventPriority.MONITOR)
|
||||
public void click(InventoryClickEvent e) {
|
||||
if (e.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
if (ItemUtils.isNull(e.getCurrentItem()) || ItemUtils.isNull(e.getCursor())) {
|
||||
return;
|
||||
}
|
||||
Player player = (Player) e.getWhoClicked();
|
||||
for (AbstractSpecialItem specialitem : ITEM_DATA) {
|
||||
for (SpecialItemResult result : specialitem.isCorrectClick(player, e.getCurrentItem(), e.getCursor())) {
|
||||
if (result == SpecialItemResult.CANCEL) {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
else if (result == SpecialItemResult.BREAK) {
|
||||
return;
|
||||
}
|
||||
else if (result == SpecialItemResult.REMOVE_ITEM_CURRENT) {
|
||||
e.setCurrentItem(null);
|
||||
}
|
||||
else if (result == SpecialItemResult.REMOVE_ITEM_CURSOR) {
|
||||
e.getWhoClicked().setItemOnCursor(null);
|
||||
}
|
||||
else if (result == SpecialItemResult.REMOVE_ITEM_CURRENT_AMOUNT_1) {
|
||||
if (e.getCurrentItem().getAmount() > 1) {
|
||||
e.getCurrentItem().setAmount(e.getCurrentItem().getAmount() - 1);
|
||||
}
|
||||
else {
|
||||
e.setCurrentItem(null);
|
||||
}
|
||||
}
|
||||
else if (result == SpecialItemResult.REMOVE_ITEM_CURSOR_AMOUNT_1) {
|
||||
if (e.getCursor().getAmount() > 1) {
|
||||
e.getCursor().setAmount(e.getCursor().getAmount() - 1);
|
||||
}
|
||||
else {
|
||||
e.getWhoClicked().setItemOnCursor(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package me.skymc.taboolib.inventory.speciaitem;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018年2月17日 下午8:55:36
|
||||
*/
|
||||
public enum SpecialItemResult {
|
||||
|
||||
/**
|
||||
* 停止接口检测
|
||||
*/
|
||||
BREAK,
|
||||
|
||||
/**
|
||||
* 取消点击事件
|
||||
*/
|
||||
CANCEL,
|
||||
|
||||
/**
|
||||
* 移除点击物品
|
||||
*/
|
||||
REMOVE_ITEM_CURRENT,
|
||||
|
||||
/**
|
||||
* 移除鼠标物品
|
||||
*/
|
||||
REMOVE_ITEM_CURSOR,
|
||||
|
||||
/**
|
||||
* 移除一个点击物品
|
||||
*/
|
||||
REMOVE_ITEM_CURRENT_AMOUNT_1,
|
||||
|
||||
/**
|
||||
* 移除一个鼠标物品
|
||||
*/
|
||||
REMOVE_ITEM_CURSOR_AMOUNT_1;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user