因 Idea 内置 Github 客户端的问题导致部分代码出错

以下类中的代码回档至上次提交时间:
- ItemUtils
- TConfigInjector
- Langauge2Format
This commit is contained in:
坏黑 2018-05-23 21:32:48 +08:00
parent 0acf324b15
commit fb38ef8977
3 changed files with 595 additions and 633 deletions

View File

@ -1,61 +1,113 @@
package com.ilummc.tlib.inject; package com.ilummc.tlib.inject;
import com.google.common.collect.Lists;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName; import com.ilummc.tlib.annotations.TConfig;
import com.ilummc.tlib.TLib; import com.ilummc.tlib.resources.TLocale;
import com.ilummc.tlib.annotations.Config; import me.skymc.taboolib.fileutils.ConfigUtils;
import com.ilummc.tlib.bean.Property;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.*; import java.util.HashMap;
import java.util.stream.Collectors; import java.util.Map;
public class TConfigInjector { public class TConfigInjector {
public static void fixUnicode(YamlConfiguration configuration) {
try {
Field field = YamlConfiguration.class.getDeclaredField("yamlOptions");
field.setAccessible(true);
field.set(configuration, NoUnicodeDumperOption.INSTANCE);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
private static final class NoUnicodeDumperOption extends DumperOptions {
private static final NoUnicodeDumperOption INSTANCE = new NoUnicodeDumperOption();
@Override
public void setAllowUnicode(boolean allowUnicode) {
super.setAllowUnicode(false);
}
@Override
public boolean isAllowUnicode() {
return false;
}
@Override
public void setLineBreak(LineBreak lineBreak) {
super.setLineBreak(LineBreak.getPlatformLineBreak());
}
}
public static Object loadConfig(Plugin plugin, Class<?> clazz) { public static Object loadConfig(Plugin plugin, Class<?> clazz) {
try { try {
Config config = clazz.getAnnotation(Config.class); TConfig config = clazz.getAnnotation(TConfig.class);
Validate.notNull(config); Validate.notNull(config);
File file = new File(plugin.getDataFolder(), config.name()); File file = new File(plugin.getDataFolder(), config.name());
if (!file.exists()) if (config.fromJar()) plugin.saveResource(config.name(), true); if (!file.exists()) {
else saveConfig(plugin, clazz.newInstance()); if (config.fromJar()) {
return unserialize(plugin, clazz); plugin.saveResource(config.name(), true);
} else {
saveConfig(plugin, clazz.newInstance());
}
}
Object obj = unserialize(plugin, clazz);
if (config.readOnly()) {
saveConfig(plugin, obj);
}
return obj;
} catch (NullPointerException e) { } catch (NullPointerException e) {
TLib.getTLib().getLogger().warn("插件 " + plugin + " 的配置类 " + clazz.getSimpleName() + " 加载失败:没有 @Config 注解"); TLocale.Logger.warn("CONFIG.LOAD-FAIL-NO-ANNOTATION", plugin.toString(), clazz.getSimpleName());
} catch (Exception e) { } catch (Exception e) {
TLib.getTLib().getLogger().warn("插件 " + plugin + " 的配置类 " + clazz.getSimpleName() + " 加载失败"); TLocale.Logger.warn("CONFIG.LOAD-FAIL", plugin.toString(), clazz.getSimpleName());
} }
return null; return null;
} }
public static void reloadConfig(Plugin plugin, Object object) {
try {
TConfig config = object.getClass().getAnnotation(TConfig.class);
Validate.notNull(config);
File file = new File(plugin.getDataFolder(), config.name());
Map<String, Object> map = ConfigUtils.confToMap(ConfigUtils.loadYaml(plugin, file));
Object obj = ConfigUtils.mapToObj(map, object);
if (config.readOnly()) {
saveConfig(plugin, obj);
}
} catch (NullPointerException e) {
TLocale.Logger.warn("CONFIG.LOAD-FAIL-NO-ANNOTATION", plugin.toString(), object.getClass().getSimpleName());
} catch (Exception e) {
TLocale.Logger.warn("CONFIG.LOAD-FAIL", plugin.toString(), object.getClass().getSimpleName());
}
}
public static Object unserialize(Plugin plugin, Class<?> clazz) { public static Object unserialize(Plugin plugin, Class<?> clazz) {
try { try {
Config config = clazz.getAnnotation(Config.class); TConfig config = clazz.getAnnotation(TConfig.class);
Validate.notNull(config); Validate.notNull(config);
return new GsonBuilder().disableHtmlEscaping().excludeFieldsWithModifiers(config.excludeModifiers()) return ConfigUtils.confToObj(
.create().fromJson(new Gson().toJson(new Yaml() ConfigUtils.mapToConf(
.dump(Files.toString(new File(plugin.getDataFolder(), config.name()), Charset.forName(config.charset())))), clazz); ConfigUtils.yamlToMap(
Files.toString(new File(plugin.getDataFolder(), config.name()), Charset.forName(config.charset())))), clazz);
} catch (NullPointerException e) { } catch (NullPointerException e) {
TLib.getTLib().getLogger().warn("插件 " + plugin + " 的配置类 " + clazz.getSimpleName() + " 加载失败:没有 @Config 注解或文件不存在"); TLocale.Logger.warn("CONFIG.LOAD-FAIL-NO-FILE", plugin.toString(), clazz.getSimpleName());
return null; return null;
} catch (Exception e) { } catch (Exception e) {
try { try {
return clazz.newInstance(); return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e1) { } catch (InstantiationException | IllegalAccessException e1) {
TLib.getTLib().getLogger().warn("插件 " + plugin + " 的配置类 " + clazz.getSimpleName() + " 加载失败"); TLocale.Logger.warn("CONFIG.LOAD-FAIL", plugin.toString(), clazz.getSimpleName());
return null; return null;
} }
} }
@ -63,95 +115,29 @@ public class TConfigInjector {
public static Map<String, Object> serialize(Plugin plugin, Object object) { public static Map<String, Object> serialize(Plugin plugin, Object object) {
try { try {
Config config = object.getClass().getAnnotation(Config.class); TConfig config = object.getClass().getAnnotation(TConfig.class);
Validate.notNull(config); Validate.notNull(config);
return new Serializer(new LinkedHashMap<>(), object, config.excludeModifiers()).get(); return ConfigUtils.objToMap(ConfigUtils.objToConf(object).getValues(false), config.excludeModifiers());
} catch (NullPointerException e) { } catch (NullPointerException e) {
TLib.getTLib().getLogger().warn("插件 " + plugin + " 的配置类 " + object.getClass().getSimpleName() + " 序列化失败:没有 @Config 注解"); TLocale.Logger.warn("CONFIG.SAVE-FAIL-NO-ANNOTATION", plugin.toString(), object.getClass().getSimpleName());
} catch (Exception e) { } catch (Exception e) {
TLib.getTLib().getLogger().warn("插件 " + plugin + " 的配置类 " + object.getClass().getSimpleName() + " 序列化失败"); TLocale.Logger.warn("CONFIG.SAVE-FAIL", plugin.toString(), object.getClass().getSimpleName());
} }
return null; return null;
} }
public static void saveConfig(Plugin plugin, Object object) throws IOException, NullPointerException { public static void saveConfig(Plugin plugin, Object object) throws IOException, NullPointerException {
Config config = object.getClass().getAnnotation(Config.class); TConfig config = object.getClass().getAnnotation(TConfig.class);
Validate.notNull(config); Validate.notNull(config);
Object obj = serialize(plugin, object); Gson gson = new GsonBuilder().disableHtmlEscaping().create();
Validate.notNull(obj); Map map = gson.fromJson(gson.toJson(object), HashMap.class);
YamlConfiguration configuration = (YamlConfiguration) ConfigUtils.mapToConf(map);
File target = new File(plugin.getDataFolder(), config.name()); File target = new File(plugin.getDataFolder(), config.name());
if (!target.exists()) target.createNewFile(); if (!target.exists()) {
DumperOptions options = new DumperOptions(); target.createNewFile();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); }
Yaml yaml = new Yaml(options); byte[] arr = configuration.saveToString().getBytes(config.charset());
String str = yaml.dump(obj);
byte[] arr = str.getBytes(config.charset());
Files.write(arr, target); Files.write(arr, target);
} }
private static final List<Class<?>> primitiveType = Lists.newArrayList(Integer.class,
Double.class, Float.class, Boolean.class, Short.class, Byte.class, Character.class, Long.class, String.class);
private static class Serializer {
private HashMap<String, Object> map;
private Object o;
private int modifiers;
private Serializer(HashMap<String, Object> map, Object o, int modifiers) {
this.map = map;
this.o = o;
this.modifiers = modifiers;
}
private HashMap<String, Object> get() {
for (Field field : o.getClass().getDeclaredFields()) {
if ((field.getModifiers() & modifiers) == 0 && !field.isSynthetic())
try {
SerializedName node = field.getAnnotation(SerializedName.class);
if (!field.isAccessible()) field.setAccessible(true);
Object obj = field.get(o);
map.put(node == null ? field.getName() : node.value(), serialize(obj));
} catch (Exception ignored) {
}
}
return map;
}
@SuppressWarnings({"unchecked", "rawtypes"})
private Object serialize(Object o) {
try {
if (o.getClass().isPrimitive() || primitiveType.contains(o.getClass())) {
return o;
} else if (o.getClass().isArray()) {
List list = new ArrayList<>();
int len = (int) o.getClass().getField("length").get(o);
for (int i = 0; i < len; i++) {
list.add(serialize(Array.get(o, i)));
}
return list;
} else if (o instanceof Collection) {
return ((Collection) o).stream().map(this::serialize).collect(Collectors.toList());
} else if (o instanceof Map) {
Map map = new LinkedHashMap<>();
((Map) o).forEach((o1, o2) -> map.put((String) o1, serialize(o2)));
return map;
} else if (o instanceof ConfigurationSerializable) {
Map map = new LinkedHashMap<>();
map.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY,
ConfigurationSerialization.getAlias((Class<? extends ConfigurationSerializable>) o.getClass()));
map.putAll(((ConfigurationSerializable) o).serialize());
return map;
} else if (o instanceof Property) {
return serialize(((Property) o).get());
} else {
return new Serializer(new HashMap<>(), o, modifiers).get();
}
} catch (Exception ignored) {
return null;
}
}
}
} }

View File

@ -1,6 +1,6 @@
package me.skymc.taboolib.inventory; package me.skymc.taboolib.inventory;
import lombok.Getter; import com.ilummc.tlib.resources.TLocale;
import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPI;
import me.skymc.taboolib.Main; import me.skymc.taboolib.Main;
import me.skymc.taboolib.TabooLib; import me.skymc.taboolib.TabooLib;
@ -9,7 +9,6 @@ import me.skymc.taboolib.itemnbtapi.NBTItem;
import me.skymc.taboolib.itemnbtapi.NBTList; import me.skymc.taboolib.itemnbtapi.NBTList;
import me.skymc.taboolib.itemnbtapi.NBTListCompound; import me.skymc.taboolib.itemnbtapi.NBTListCompound;
import me.skymc.taboolib.itemnbtapi.NBTType; import me.skymc.taboolib.itemnbtapi.NBTType;
import me.skymc.taboolib.message.MsgUtils;
import me.skymc.taboolib.other.NumberUtils; import me.skymc.taboolib.other.NumberUtils;
import me.skymc.taboolib.string.Language; import me.skymc.taboolib.string.Language;
import org.bukkit.Color; import org.bukkit.Color;
@ -29,30 +28,50 @@ import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.stream.IntStream;
public class ItemUtils { public class ItemUtils {
@Getter
private static FileConfiguration itemdir = null; private static FileConfiguration itemdir = null;
@Getter
private static FileConfiguration itemCache = null; private static FileConfiguration itemCache = null;
@Getter
private static File finalItemsFolder; private static File finalItemsFolder;
@Getter
private static LinkedHashMap<String, String> itemlib = new LinkedHashMap<>(); private static LinkedHashMap<String, String> itemlib = new LinkedHashMap<>();
@Getter
private static LinkedHashMap<String, ItemStack> itemCaches = new LinkedHashMap<>(); private static LinkedHashMap<String, ItemStack> itemCaches = new LinkedHashMap<>();
@Getter
private static LinkedHashMap<String, ItemStack> itemCachesFinal = new LinkedHashMap<>(); private static LinkedHashMap<String, ItemStack> itemCachesFinal = new LinkedHashMap<>();
public static FileConfiguration getItemdir() {
return itemdir;
}
public static FileConfiguration getItemCache() {
return itemCache;
}
public static File getFinalItemsFolder() {
return finalItemsFolder;
}
public static LinkedHashMap<String, String> getItemlib() {
return itemlib;
}
public static LinkedHashMap<String, ItemStack> getItemCaches() {
return itemCaches;
}
public static LinkedHashMap<String, ItemStack> getItemCachesFinal() {
return itemCachesFinal;
}
/** /**
* 获取物品缓存 * 获取物品缓存
* 检测顺序 * 检测顺序
@ -81,7 +100,7 @@ public class ItemUtils {
reloadItemCache(); reloadItemCache();
itemdir = YamlConfiguration.loadConfiguration(new File(Main.getInst().getConfig().getString("DATAURL.ITEMDIR"))); itemdir = YamlConfiguration.loadConfiguration(new File(Main.getInst().getConfig().getString("DATAURL.ITEMDIR")));
} catch (Exception e) { } catch (Exception e) {
MsgUtils.warn("物品库载入失败: &4" + e.getMessage()); TLocale.Logger.error("ITEM-UTILS.FAIL-LOAD-ITEMS", e.toString());
} }
} }
@ -89,7 +108,7 @@ public class ItemUtils {
FileConfiguration conf = ConfigUtils.load(Main.getInst(), file); FileConfiguration conf = ConfigUtils.load(Main.getInst(), file);
for (String name : conf.getConfigurationSection("").getKeys(false)) { for (String name : conf.getConfigurationSection("").getKeys(false)) {
if (isExists(name)) { if (isExists(name)) {
MsgUtils.warn("无法载入载入物品 &4" + name + "&c, 因为它已经存在了"); TLocale.Logger.error("ITEM-UTILS.FAIL-LOAD-ITEMS", name);
} else if (finalFile) { } else if (finalFile) {
itemCachesFinal.put(name, loadItem(conf, name)); itemCachesFinal.put(name, loadItem(conf, name));
} else { } else {
@ -108,19 +127,15 @@ public class ItemUtils {
finalItemsFolder.mkdir(); finalItemsFolder.mkdir();
} }
// 检查固定物品库中的物品 // 检查固定物品库中的物品
for (File file : finalItemsFolder.listFiles()) { Arrays.stream(finalItemsFolder.listFiles()).forEach(file -> loadItemsFile(file, true));
loadItemsFile(file, true); TLocale.Logger.info("ITEM-UTILS.SUCCESS-LOAD-CACHES", String.valueOf(itemCaches.size() + itemCachesFinal.size()));
}
MsgUtils.send("载入 " + (itemCaches.size() + itemCachesFinal.size()) + " 项缓存物品");
} }
public static void reloadItemName() { public static void reloadItemName() {
FileConfiguration conf = new Language("ITEM_NAME", Main.getInst(), true).getConfiguration(); FileConfiguration conf = new Language("ITEM_NAME", Main.getInst(), true).getConfiguration();
itemlib.clear(); itemlib.clear();
for (String a : conf.getConfigurationSection("").getKeys(false)) { conf.getConfigurationSection("").getKeys(false).forEach(a -> itemlib.put(a, conf.getString(a)));
itemlib.put(a, conf.getString(a)); TLocale.Logger.info("ITEM-UTILS.SUCCESS-LOAD-NAMES", String.valueOf(itemlib.size()));
}
MsgUtils.send("载入 " + itemlib.size() + " 项物品名称");
} }
public static File getItemCacheFile() { public static File getItemCacheFile() {
@ -133,7 +148,7 @@ public class ItemUtils {
public static String getCustomName(ItemStack item) { public static String getCustomName(ItemStack item) {
if (item == null || item.getType().equals(Material.AIR)) { if (item == null || item.getType().equals(Material.AIR)) {
return ""; return TLocale.asString("ITEM-UTILS.EMPTY-ITEM");
} }
int data = item.getType().getMaxDurability() == 0 ? item.getDurability() : 0; 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); return item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : itemlib.get(item.getType() + ":" + data) == null ? item.getType().toString() : itemlib.get(item.getType() + ":" + data);
@ -192,17 +207,14 @@ public class ItemUtils {
} }
public static List<String> asString(List<String> args, Player placeholderPlayer) { public static List<String> asString(List<String> args, Player placeholderPlayer) {
for (int i = 0 ; i < args.size() ; i ++) { IntStream.range(0, args.size()).forEach(i -> args.set(i, asString(args.get(i), placeholderPlayer)));
args.set(i, asString(args.get(i), placeholderPlayer));
}
return args; return args;
} }
public static ItemFlag asItemFlag(String flag) { public static ItemFlag asItemFlag(String flag) {
try { try {
return ItemFlag.valueOf(flag); return ItemFlag.valueOf(flag);
} } catch (Exception e) {
catch (Exception e) {
return null; return null;
} }
} }
@ -212,8 +224,7 @@ public class ItemUtils {
try { try {
Material material = Material.getMaterial(args); Material material = Material.getMaterial(args);
return material != null ? material : Material.getMaterial(Integer.valueOf(args)); return material != null ? material : Material.getMaterial(Integer.valueOf(args));
} } catch (Exception e) {
catch (Exception e) {
return Material.STONE; return Material.STONE;
} }
} }
@ -223,8 +234,7 @@ public class ItemUtils {
try { try {
Enchantment enchantment = Enchantment.getByName(enchant); Enchantment enchantment = Enchantment.getByName(enchant);
return enchantment != null ? enchantment : Enchantment.getById(Integer.valueOf(enchant)); return enchantment != null ? enchantment : Enchantment.getById(Integer.valueOf(enchant));
} } catch (Exception e) {
catch (Exception e) {
return null; return null;
} }
} }
@ -234,8 +244,7 @@ public class ItemUtils {
try { try {
PotionEffectType type = PotionEffectType.getByName(potion); PotionEffectType type = PotionEffectType.getByName(potion);
return type != null ? type : PotionEffectType.getById(Integer.valueOf(potion)); return type != null ? type : PotionEffectType.getById(Integer.valueOf(potion));
} } catch (Exception e) {
catch (Exception e) {
return null; return null;
} }
} }
@ -243,8 +252,7 @@ public class ItemUtils {
public static Color asColor(String color) { public static Color asColor(String color) {
try { try {
return Color.fromBGR(Integer.valueOf(color.split("-")[0]), Integer.valueOf(color.split("-")[1]), Integer.valueOf(color.split("-")[2])); return Color.fromBGR(Integer.valueOf(color.split("-")[0]), Integer.valueOf(color.split("-")[1]), Integer.valueOf(color.split("-")[2]));
} } catch (Exception e) {
catch (Exception e) {
return Color.fromBGR(0, 0, 0); return Color.fromBGR(0, 0, 0);
} }
} }
@ -265,9 +273,10 @@ public class ItemUtils {
return "generic.armor"; return "generic.armor";
case "luck": case "luck":
return "generic.luck"; return "generic.luck";
} default:
return null; return null;
} }
}
/** /**
* 包含介绍 * 包含介绍
@ -308,7 +317,7 @@ public class ItemUtils {
public static ItemStack addLore(ItemStack is, String lore) { public static ItemStack addLore(ItemStack is, String lore) {
ItemMeta meta = is.getItemMeta(); ItemMeta meta = is.getItemMeta();
List<String> _lore = meta.hasLore() ? meta.getLore() : new ArrayList<>(); List<String> _lore = meta.hasLore() ? meta.getLore() : Collections.emptyList();
_lore.add(lore.replaceAll("&", "§")); _lore.add(lore.replaceAll("&", "§"));
is.setItemMeta(meta); is.setItemMeta(meta);
@ -341,14 +350,7 @@ public class ItemUtils {
* @param a 关键字 * @param a 关键字
*/ */
public static int getLore(ItemStack i, String a) { public static int getLore(ItemStack i, String a) {
if (isLored(i)) { return isLored(i) ? IntStream.range(0, i.getItemMeta().getLore().size()).filter(j -> i.getItemMeta().getLore().get(j).contains(a)).findFirst().orElse(0) : 0;
for (int j = 0; j < i.getItemMeta().getLore().size() ; j++) {
if (i.getItemMeta().getLore().get(j).contains(a)) {
return j;
}
}
}
return 0;
} }
/** /**
@ -377,13 +379,10 @@ public class ItemUtils {
public static ItemStack repalceLore(ItemStack i, String l1, String l2) { public static ItemStack repalceLore(ItemStack i, String l1, String l2) {
if (!isLored(i)) { if (!isLored(i)) {
return i; return i;
} } else {
else {
ItemMeta meta = i.getItemMeta(); ItemMeta meta = i.getItemMeta();
List<String> lore = meta.getLore(); List<String> lore = meta.getLore();
for (int j = 0 ; j < lore.size() ; j++) { IntStream.range(0, lore.size()).forEach(j -> lore.set(j, lore.get(j).replace(l1, l2)));
lore.set(j, lore.get(j).replace(l1, l2));
}
meta.setLore(lore); meta.setLore(lore);
i.setItemMeta(meta); i.setItemMeta(meta);
} }
@ -403,7 +402,7 @@ public class ItemUtils {
return section.getItemStack("bukkit"); return section.getItemStack("bukkit");
} }
// 材质 // 材质
ItemStack item = new ItemStack(asMaterial(section.get("material").toString())); ItemStack item = new ItemStack(asMaterial(section.getString("material")));
// 数量 // 数量
item.setAmount(section.contains("amount") ? section.getInt("amount") : 1); item.setAmount(section.contains("amount") ? section.getInt("amount") : 1);
// 耐久 // 耐久
@ -424,10 +423,8 @@ public class ItemUtils {
Enchantment enchant = asEnchantment(preEnchant); Enchantment enchant = asEnchantment(preEnchant);
if (enchant != null) { if (enchant != null) {
meta.addEnchant(enchant, section.getInt("enchants." + preEnchant), true); meta.addEnchant(enchant, section.getInt("enchants." + preEnchant), true);
} } else {
else { TLocale.Logger.error("ITEM-UTILS.FAIL-LOAD-ENCHANTS", preEnchant);
MsgUtils.warn("&8" + preEnchant + " &c不是一个有效的附魔名称");
MsgUtils.warn("&c输入 &4/taboolib enchants&c 查看所有附魔");
} }
} }
} }
@ -437,10 +434,8 @@ public class ItemUtils {
ItemFlag flag = asItemFlag(preFlag); ItemFlag flag = asItemFlag(preFlag);
if (flag != null) { if (flag != null) {
meta.addItemFlags(flag); meta.addItemFlags(flag);
} } else {
else { TLocale.Logger.error("ITEM-UTILS.FAIL-LOAD-FLAG", preFlag);
MsgUtils.warn("&8" + preFlag + " &c不是一个有效的标签名称");
MsgUtils.warn("&c输入 &4/taboolib flags&c 查看所有标签");
} }
} }
} }
@ -458,10 +453,8 @@ public class ItemUtils {
potionEffectType, potionEffectType,
NumberUtils.getInteger(section.getString("potions." + prePotionName).split("-")[0]), NumberUtils.getInteger(section.getString("potions." + prePotionName).split("-")[0]),
NumberUtils.getInteger(section.getString("potions." + prePotionName).split("-")[1]) - 1), true); NumberUtils.getInteger(section.getString("potions." + prePotionName).split("-")[1]) - 1), true);
} } else {
else { TLocale.Logger.error("ITEM-UTILS.FAIL-LOAD-POTION", prePotionName);
MsgUtils.warn("&8" + potionEffectType + " &c不是一个有效的药水名称");
MsgUtils.warn("&c输入 &4/taboolib potions&c 查看所有药水");
} }
} }
} }
@ -475,17 +468,13 @@ public class ItemUtils {
Object obj = section.get("nbt." + name); Object obj = section.get("nbt." + name);
if (obj instanceof String) { if (obj instanceof String) {
nbt.setString(name, obj.toString()); nbt.setString(name, obj.toString());
} } else if (obj instanceof Double) {
else if (obj instanceof Double) {
nbt.setDouble(name, Double.valueOf(obj.toString())); nbt.setDouble(name, Double.valueOf(obj.toString()));
} } else if (obj instanceof Integer) {
else if (obj instanceof Integer) {
nbt.setInteger(name, Integer.valueOf(obj.toString())); nbt.setInteger(name, Integer.valueOf(obj.toString()));
} } else if (obj instanceof Long) {
else if (obj instanceof Long) {
nbt.setLong(name, Long.valueOf(obj.toString())); nbt.setLong(name, Long.valueOf(obj.toString()));
} } else {
else {
nbt.setObject(name, obj); nbt.setObject(name, obj);
} }
} }
@ -502,26 +491,21 @@ public class ItemUtils {
if (num.toString().contains("%")) { if (num.toString().contains("%")) {
_attr.setDouble("Amount", Double.valueOf(num.toString().replace("%", "")) / 100D); _attr.setDouble("Amount", Double.valueOf(num.toString().replace("%", "")) / 100D);
_attr.setInteger("Operation", 1); _attr.setInteger("Operation", 1);
} } else {
else {
_attr.setDouble("Amount", Double.valueOf(num.toString())); _attr.setDouble("Amount", Double.valueOf(num.toString()));
_attr.setInteger("Operation", 0); _attr.setInteger("Operation", 0);
} }
_attr.setString("AttributeName", asAttribute(name)); _attr.setString("AttributeName", asAttribute(name));
_attr.setInteger("UUIDMost", NumberUtils.getRand().nextInt(Integer.MAX_VALUE)); _attr.setInteger("UUIDMost", NumberUtils.getRandom().nextInt(Integer.MAX_VALUE));
_attr.setInteger("UUIDLeast", NumberUtils.getRand().nextInt(Integer.MAX_VALUE)); _attr.setInteger("UUIDLeast", NumberUtils.getRandom().nextInt(Integer.MAX_VALUE));
_attr.setString("Name", asAttribute(name)); _attr.setString("Name", asAttribute(name));
if (!hand.equals("all")) { if (!"all".equals(hand)) {
_attr.setString("Slot", hand); _attr.setString("Slot", hand);
} }
} catch (Exception ignored) {
} }
catch (Exception e) { } else {
MsgUtils.warn("&8" + name + " &c属性载入失败: &8" + e.getMessage()); TLocale.Logger.error("ITEM-UTILS.FAIL-LOAD-POTION", name);
}
}
else {
MsgUtils.warn("&8" + name + " &c不是一个有效的属性名称");
MsgUtils.warn("&c输入 &4/taboolib attributes&c 查看所有属性");
} }
} }
} }
@ -546,26 +530,21 @@ public class ItemUtils {
if (num.toString().contains("%")) { if (num.toString().contains("%")) {
_attr.setDouble("Amount", Double.valueOf(num.toString().replace("%", "")) / 100D); _attr.setDouble("Amount", Double.valueOf(num.toString().replace("%", "")) / 100D);
_attr.setInteger("Operation", 1); _attr.setInteger("Operation", 1);
} } else {
else {
_attr.setDouble("Amount", Double.valueOf(num.toString())); _attr.setDouble("Amount", Double.valueOf(num.toString()));
_attr.setInteger("Operation", 0); _attr.setInteger("Operation", 0);
} }
_attr.setString("AttributeName", asAttribute(name)); _attr.setString("AttributeName", asAttribute(name));
_attr.setInteger("UUIDMost", NumberUtils.getRand().nextInt(Integer.MAX_VALUE)); _attr.setInteger("UUIDMost", NumberUtils.getRandom().nextInt(Integer.MAX_VALUE));
_attr.setInteger("UUIDLeast", NumberUtils.getRand().nextInt(Integer.MAX_VALUE)); _attr.setInteger("UUIDLeast", NumberUtils.getRandom().nextInt(Integer.MAX_VALUE));
_attr.setString("Name", asAttribute(name)); _attr.setString("Name", asAttribute(name));
if (!hand.equals("all")) { if (!"all".equals(hand)) {
_attr.setString("Slot", hand); _attr.setString("Slot", hand);
} }
} catch (NumberFormatException ignored) {
} }
catch (Exception e) { } else {
MsgUtils.warn("&8" + name + " &c属性载入失败: &8" + e.getMessage()); TLocale.Logger.error("ITEM-UTILS.FAIL-LOAD-POTION", name);
}
}
else {
MsgUtils.warn("&8" + name + " &c不是一个有效的属性名称");
MsgUtils.warn("&c输入 &4/taboolib attributes&c 查看所有属性");
} }
return nbt; return nbt;
} }

View File

@ -1,9 +1,6 @@
package me.skymc.taboolib.string.language2; package me.skymc.taboolib.string.language2;
import lombok.Getter;
import me.skymc.taboolib.string.language2.value.*; import me.skymc.taboolib.string.language2.value.*;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
@ -16,10 +13,7 @@ import java.util.List;
*/ */
public class Language2Format implements Language2Line { public class Language2Format implements Language2Line {
@Getter private Language2Value language2Value;
private Language2Value language2Value = null;
@Getter
private List<Language2Line> language2Lines = new ArrayList<>(); private List<Language2Line> language2Lines = new ArrayList<>();
public Language2Format(Player player, Language2Value value) { public Language2Format(Player player, Language2Value value) {
@ -79,8 +73,7 @@ public class Language2Format implements Language2Line {
parseValue(player, values, type); parseValue(player, values, type);
// 更改类型 // 更改类型
type = Language2Type.BOOK; type = Language2Type.BOOK;
} } else if (line.contains("[return]")) {
else if (line.contains("[return]")) {
// 递交数据 // 递交数据
parseValue(player, values, type); parseValue(player, values, type);
} }
@ -92,6 +85,14 @@ public class Language2Format implements Language2Line {
} }
} }
public Language2Value getLanguage2Value() {
return language2Value;
}
public List<Language2Line> getLanguage2Lines() {
return language2Lines;
}
/** /**
* 识别内容 * 识别内容
* *
@ -141,15 +142,11 @@ public class Language2Format implements Language2Line {
@Override @Override
public void send(Player player) { public void send(Player player) {
for (Language2Line line : language2Lines) { language2Lines.forEach(line -> line.send(player));
line.send(player);
}
} }
@Override @Override
public void console() { public void console() {
for (Language2Line line : language2Lines) { language2Lines.forEach(Language2Line::console);
line.console();
}
} }
} }