From 638d152120537ca5daa14997f832c701ad8951e4 Mon Sep 17 00:00:00 2001 From: u2g Date: Fri, 30 Dec 2016 18:44:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=B0=86=E7=89=A9=E5=93=81=E6=A0=88?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96=E4=B8=BA=E7=89=B9=E5=AE=9A=20YAML?= =?UTF-8?q?=20=E6=96=87=E4=BB=B6=E5=9F=BA=E7=A1=80=E4=B9=9F=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../moonlake/api/item/AttributeModify.java | 1 + .../moonlake/api/item/potion/PotionBase.java | 107 ++++++++- .../moonlake/manager/ItemManager.java | 206 ++++++++++++++++-- .../minecraft/moonlake/util/StringUtil.java | 26 +++ 4 files changed, 315 insertions(+), 25 deletions(-) diff --git a/src/com/minecraft/moonlake/api/item/AttributeModify.java b/src/com/minecraft/moonlake/api/item/AttributeModify.java index b78157c2..812bef99 100644 --- a/src/com/minecraft/moonlake/api/item/AttributeModify.java +++ b/src/com/minecraft/moonlake/api/item/AttributeModify.java @@ -244,6 +244,7 @@ public enum Type { for(final Type type : values()) { NAME_MAP.put(type.getType(), type); + NAME_MAP.put(type.getAttributeName(), type); // put attribute name } } diff --git a/src/com/minecraft/moonlake/api/item/potion/PotionBase.java b/src/com/minecraft/moonlake/api/item/potion/PotionBase.java index 96421a3d..d38db97a 100644 --- a/src/com/minecraft/moonlake/api/item/potion/PotionBase.java +++ b/src/com/minecraft/moonlake/api/item/potion/PotionBase.java @@ -18,6 +18,8 @@ package com.minecraft.moonlake.api.item.potion; +import com.minecraft.moonlake.validate.Validate; + /** *

PotionBase

* 基础药水效果 @@ -162,11 +164,11 @@ public abstract class PotionBase { /** * 基础药水效果类型: 虚弱药水 */ - public final static PotionBase LONG_WEAKNESS = new PotionBase("weakness") {}; + public final static PotionBase WEAKNESS = new PotionBase("weakness") {}; /** * 基础药水效果类型: 虚弱药水 延长版 */ - public final static PotionBase WEAKNESS = new PotionBase("long_weakness") {}; + public final static PotionBase LONG_WEAKNESS = new PotionBase("long_weakness") {}; private String value; @@ -189,4 +191,105 @@ public String getValue() { return value; } + + /** + * 将指定基础效果名转换为药水基础效果对象 + * + * @param type 名称 + * @return PotionBase + * @throws IllegalArgumentException 如果名称对象为 {@code null} 则抛出异常 + */ + public static PotionBase valueOf(String type) { + + return valueOf(type, null); + } + + /** + * 将指定基础效果名转换为药水基础效果对象 + * + * @param type 名称 + * @param def 默认值 + * @return PotionBase + * @throws IllegalArgumentException 如果名称对象为 {@code null} 则抛出异常 + */ + public static PotionBase valueOf(String type, PotionBase def) { + + Validate.notNull(type, "The potion base type object is null."); + + switch (type.toLowerCase()) { + + case "water": + return WATER; + case "mundane": + return MUNDANE_WATER; + case "thick": + return THICK_WATER; + case "awkward": + return AWKWARD_WATER; + case "night_vision": + return NIGHT_VISION; + case "long_night_vision": + return LONG_NIGHT_VISION; + case "invisibility": + return INVISIBILITY; + case "long_invisibility": + return LONG_INVISIBILITY; + case "leaping": + return LEAPING; + case "strong_leaping": + return STRONG_LEAPING; + case "long_leaping": + return LONG_LEAPING; + case "fire_resistance": + return FIRE_RESISTANCE; + case "long_fire_resistance": + return LONG_FIRE_RESISTANCE; + case "swiftness": + return SWIFTNESS; + case "strong_swiftness": + return STRONG_SWIFTNESS; + case "long_swiftness": + return LONG_SWIFTNESS; + case "slowness": + return SLOWNESS; + case "long_slowness": + return LONG_SLOWNESS; + case "water_breathing": + return WATER_BREATHING; + case "long_water_breathing": + return LONG_WATER_BREATHING; + case "healing": + return HEALING; + case "strong_healing": + return STRONG_HEALING; + case "harming": + return HARMING; + case "strong_harming": + return STRONG_HARMING; + case "poison": + return POISON; + case "strong_poison": + return STRONG_POISON; + case "long_poison": + return LONG_POISON; + case "regeneration": + return REGENERATION; + case "strong_regeneration": + return STRONG_REGENERATION; + case "long_regeneration": + return LONG_REGENERATION; + case "strength": + return STRENGTH; + case "strong_strength": + return STRONG_STRENGTH; + case "long_strength": + return LONG_STRENGTH; + case "weakness": + return WEAKNESS; + case "long_weakness": + return LONG_WEAKNESS; + default: + return def; + } + } } diff --git a/src/com/minecraft/moonlake/manager/ItemManager.java b/src/com/minecraft/moonlake/manager/ItemManager.java index 0eafe03d..f3d4748e 100644 --- a/src/com/minecraft/moonlake/manager/ItemManager.java +++ b/src/com/minecraft/moonlake/manager/ItemManager.java @@ -35,13 +35,12 @@ import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import java.io.*; import java.lang.reflect.Constructor; import java.lang.reflect.Method; -import java.util.Base64; -import java.util.List; -import java.util.Set; +import java.util.*; import static com.minecraft.moonlake.reflect.Reflect.*; @@ -463,19 +462,194 @@ public static ItemStack deserialize(String data) { return null; } - /** Serialize ItemStack To File */ - public static File serializeToFile(ItemBuilder builder) { + /** + * 将物品栈对象数据序列化为特定 YAML 文件数据 + * + * @param builder 物品栈构建器 + * @param out 输出的文件 + * @throws IllegalArgumentException 如果物品栈构建器对象为 {@code null} 则抛出异常 + * @throws IllegalArgumentException 如果输出文件对象为 {@code null} 则抛出异常 + * @throws MoonLakeException 如果保存到文件时错误则抛出异常 + */ + public static void serializeToFile(ItemBuilder builder, File out) { + + serializeToFile(builder, out, null); + } + + /** + * 将物品栈对象数据序列化为特定 YAML 文件数据 + * + * @param builder 物品栈构建器 + * @param out 输出的文件 + * @param charset 文件编码 + * @throws IllegalArgumentException 如果物品栈构建器对象为 {@code null} 则抛出异常 + * @throws IllegalArgumentException 如果输出文件对象为 {@code null} 则抛出异常 + * @throws MoonLakeException 如果保存到文件时错误则抛出异常 + */ + public static void serializeToFile(ItemBuilder builder, File out, String charset) { Validate.notNull(builder, "The item builder object is null."); - return serializeToFile(builder.build(true)); + serializeToFile(builder.build(true), out, charset); } - public static File serializeToFile(ItemStack itemStack) { + /** + * 将物品栈对象数据序列化为特定 YAML 文件数据 + * + * @param itemStack 物品栈 + * @param out 输出的文件 + * @throws IllegalArgumentException 如果物品栈对象为 {@code null} 则抛出异常 + * @throws IllegalArgumentException 如果输出文件对象为 {@code null} 则抛出异常 + * @throws MoonLakeException 如果保存到文件时错误则抛出异常 + */ + public static void serializeToFile(ItemStack itemStack, File out) { - throw new UnsupportedOperationException(); + serializeToFile(itemStack, out, null); + } + + /** + * 将物品栈对象数据序列化为特定 YAML 文件数据 + * + * @param itemStack 物品栈 + * @param out 输出的文件 + * @param charset 文件编码 + * @throws IllegalArgumentException 如果物品栈对象为 {@code null} 则抛出异常 + * @throws IllegalArgumentException 如果输出文件对象为 {@code null} 则抛出异常 + * @throws MoonLakeException 如果保存到文件时错误则抛出异常 + */ + public static void serializeToFile(ItemStack itemStack, File out, String charset) { + + Validate.notNull(itemStack, "The itemstack object is null."); + Validate.notNull(out, "The file out object is null."); + + YamlConfiguration yaml = new YamlConfiguration(); + yaml.set("Type", itemStack.getType().name()); + yaml.set("Data", (int) itemStack.getDurability()); + yaml.set("Amount", itemStack.getAmount()); + + ItemMeta itemMeta = itemStack.getItemMeta(); + + // display name + if(itemMeta.hasDisplayName()) + yaml.set("DisplayName", itemMeta.getDisplayName()); + + // lore + if(itemMeta.hasLore()) + yaml.set("Lore", itemMeta.getLore()); + + // enchantment + if(itemMeta.hasEnchants()) { + + Map enchantments = itemMeta.getEnchants(); + + for (final Map.Entry entry : enchantments.entrySet()) { + + yaml.set("Enchantment." + entry.getKey().getName(), entry.getValue()); + } + } + + // hide flag + Set hideFlags = itemMeta.getItemFlags(); + + if(hideFlags != null && !hideFlags.isEmpty()) { + + List result = new ArrayList<>(); + + for(final ItemFlag flag : hideFlags) + result.add(flag.name()); + + yaml.set("HideFlag", result); + } + + // unbreakable + if(MoonLakeAPI.getItemLibrary().isUnbreakable(itemStack)) + yaml.set("Unbreakable", true); + + // attribute modifiers + Set atts = MoonLakeAPI.getItemLibrary().getAttributes(itemStack); + + if(atts != null && !atts.isEmpty()) { + + for(final AttributeModify att : atts) { + + AttributeModify.Slot slot = att.getSlot().get(); + AttributeModify.Type type = att.getType().get(); + AttributeModify.Operation operation = att.getOperation().get(); + + yaml.set("AttributeModifiers." + type.getType() + ".Value", att.getAmount().getValue()); + + if(operation == AttributeModify.Operation.ADD_PERCENTAGE) + // percent + yaml.set("AttributeModifiers." + type.getType() + ".Percent", true); + + if(slot != null && slot != AttributeModify.Slot.MAIN_HAND && slot != AttributeModify.Slot.ALL) + // slot + yaml.set("AttributeModifiers." + type.getType() + ".Slot", slot.getType()); + } + } + + // nbt modifiers + NBTCompound tag = MoonLakeAPI.getNBTLibrary().readSafe(itemStack); + + // skull owner + if(itemStack.getType() == Material.SKULL_ITEM) { + + String skullOwner = tag.getString("SkullOwner", null); + + if(skullOwner != null) + yaml.set("SkullOwner", skullOwner); + } + + // age + int age = tag.getInt("Age", 6000); + + if(age != 6000) + yaml.set("Age", age); + + // pickup delay + int pickupDelay = tag.getInt("PickupDelay", -1); + + if(pickupDelay != -1) + yaml.set("PickupDelay", pickupDelay); + + // save file + OutputStreamWriter osw = null; + + try { + + if(charset == null) { + // use yaml default save + yaml.save(out); + } + else { + // use out stream save + String data = yaml.saveToString(); + + if(data.contains("\\u")) + // unicode + data = StringUtil.decodeUnicode(data); + + osw = new OutputStreamWriter(new FileOutputStream(out), charset); + osw.write(data); + } + } + catch (Exception e) { + + throw new MoonLakeException("The serialize to file save exception."); + } + finally { + + try { + + if(osw != null) { + osw.flush(); + osw.close(); + } + } + catch (Exception e) { + } + } } - /** Serialize ItemStack To File */ /** * 将特定 YAML 文件数据反序列化为物品栈对象 @@ -498,8 +672,6 @@ public static ItemStack deserializeFromFile(String path) { /** * 将特定 YAML 文件数据反序列化为物品栈对象 * - *

By Month_Light Ver: 1.0

- * * @param file 文件 * @return 物品栈对象 异常返回 null * @throws IllegalArgumentException 如果文件对象为 {@code null} 则抛出异常 @@ -674,18 +846,6 @@ public static ItemStack deserializeFromFile(File file) { tag.put("PickupDelay", pickupDelay); } - // generation - /*if(yml.isSet("Generation")) { - - if(material == Material.WRITTEN_BOOK) { - - int value = yml.getInt("Generation", -1); - - if(value != -1) - tag.put("generation", value); - } - }*/ - // write nbt MoonLakeAPI.getNBTLibrary().write(result, tag); diff --git a/src/com/minecraft/moonlake/util/StringUtil.java b/src/com/minecraft/moonlake/util/StringUtil.java index ed8034c1..82f35ca9 100644 --- a/src/com/minecraft/moonlake/util/StringUtil.java +++ b/src/com/minecraft/moonlake/util/StringUtil.java @@ -28,6 +28,8 @@ import java.util.Collection; import java.util.Date; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** *

StringUtil

@@ -39,6 +41,7 @@ public class StringUtil { public final static char COLOR_CHAR = '\u0026'; + public final static Pattern UNICODE_PATTERN = Pattern.compile("\\\\u[a-zA-Z0-9]{2,4}"); /** * 字符串实现类构造函数 @@ -279,4 +282,27 @@ public static double rounding(double value, int bit) { return new BigDecimal(value).setScale(bit, BigDecimal.ROUND_HALF_UP).doubleValue(); } + + /** + * 将指定包含 Unicode 码的字符串转换为中文 + * + * @param unicode 包含 Unicode 码的字符串 + * @return 转换中文后的字符串 + */ + public static String decodeUnicode(final String unicode) { + + if(unicode == null) + return null; + + Matcher matcher = UNICODE_PATTERN.matcher(unicode); + StringBuffer buffer = new StringBuffer(); + + while (matcher.find()) { + String group = matcher.group(); + group = group.substring(group.indexOf("\\u") + 2, group.length()); + matcher.appendReplacement(buffer, String.valueOf((char) Integer.parseInt(group, 16))); + } + matcher.appendTail(buffer); + return buffer.toString(); + } } From 8ada47c2ca9e670252ea84ecd45416ca5242656c Mon Sep 17 00:00:00 2001 From: u2g Date: Sat, 31 Dec 2016 22:02:41 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=88=B0=20v1.8-b4=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- config.yml | 4 +- plugin.yml | 2 +- src/com/minecraft/moonlake/MoonLakeAPI.java | 20 ++++ .../minecraft/moonlake/MoonLakePlugin.java | 4 +- .../event/player/MoonLakePlayerJoinEvent.java | 2 + .../moonlake/api/item/ItemBuilder.java | 22 ++++ .../moonlake/api/item/ItemBuilderWrapped.java | 16 +++ .../api/item/ItemExpressionWrapped.java | 12 +++ .../moonlake/api/item/MetaExpression.java | 10 +- .../moonlake/api/item/SkullExpression.java | 100 ++++++++++-------- .../moonlake/api/item/skull/SkullLibrary.java | 23 ++++ .../minecraft/moonlake/manager/IoManager.java | 2 +- 13 files changed, 162 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 28fdccd7..d15123b0 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# MoonLake [![GitHub version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=gh&type=6&v=1.8-a4&x2=0)](https://github.com/u2g/MoonLake) [![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=102)](https://github.com/u2g/MoonLake) [![Open Source Love](https://badges.frapsoft.com/os/gpl/gpl.svg?v=102)](https://github.com/u2g/MoonLake) +# MoonLake [![GitHub version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=gh&type=6&v=1.8-b4&x2=0)](https://github.com/u2g/MoonLake) [![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=102)](https://github.com/u2g/MoonLake) [![Open Source Love](https://badges.frapsoft.com/os/gpl/gpl.svg?v=102)](https://github.com/u2g/MoonLake) Minecraft MoonLake Core API Plugin -By Month_Light Ver: 1.8-a4 +By Month_Light Ver: 1.8-b4 ## 简介 这个插件提供了大量的 API 功能,实现了一些 Bukkit 无法做到的 NMS 功能
diff --git a/config.yml b/config.yml index f785cf5b..ca817c2f 100644 --- a/config.yml +++ b/config.yml @@ -1,10 +1,10 @@ ################################ # MoonLake Core API Plugin # -# By Month_Light Ver: 1.8-a4 # +# By Month_Light Ver: 1.8-b4 # ################################ # 插件版本号: 此项请勿修改! -version: '1.8-a4' +version: '1.8-b4' # 数据包通道监听器 # 此项可设置是否开启这个功能 diff --git a/plugin.yml b/plugin.yml index 90aedba1..0e584d40 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ name: MoonLake main: com.minecraft.moonlake.MoonLakePlugin -version: 1.8-a4 +version: 1.8-b4 prefix: MoonLake description: Minecraft MoonLake Core API Plugin website: http://www.mcyszh.com/ diff --git a/src/com/minecraft/moonlake/MoonLakeAPI.java b/src/com/minecraft/moonlake/MoonLakeAPI.java index 559ee8d0..019567c4 100644 --- a/src/com/minecraft/moonlake/MoonLakeAPI.java +++ b/src/com/minecraft/moonlake/MoonLakeAPI.java @@ -488,6 +488,26 @@ public CommandAnnotation getCommand() { }; } + /** + * 获取插件注解配置文件类接口 ConfigAnnotation 实例对象 + * + * @return ConfigAnnotation + */ + public static ConfigAnnotation getConfigAnnotation() { + + return getPluginAnnotation().getConfig(); + } + + /** + * 获取插件注解m了类接口 CommandAnnotation 实例对象 + * + * @return CommandAnnotation + */ + public static CommandAnnotation getCommandAnnotation() { + + return getPluginAnnotation().getCommand(); + } + /** * 获取 NBTLibrary 对象 * diff --git a/src/com/minecraft/moonlake/MoonLakePlugin.java b/src/com/minecraft/moonlake/MoonLakePlugin.java index bd93f508..dc5c7479 100644 --- a/src/com/minecraft/moonlake/MoonLakePlugin.java +++ b/src/com/minecraft/moonlake/MoonLakePlugin.java @@ -33,7 +33,7 @@ *
*
*

Minecraft MoonLake Core API Plugin

- *

By Month_Light Ver: 1.8-a4

+ *

By Month_Light Ver: 1.8-b4

*

Website: MoonLake Website

*

QQ Group: 377607025 -> Jump

*
@@ -68,7 +68,7 @@ *

修改操作请您遵守 GPLv3 协议,您必须公开修改过的所有代码!

*
* - * @version 1.8-a4 + * @version 1.8-b4 * @author Month_Light */ public class MoonLakePlugin extends JavaPlugin implements MoonLake { diff --git a/src/com/minecraft/moonlake/api/event/player/MoonLakePlayerJoinEvent.java b/src/com/minecraft/moonlake/api/event/player/MoonLakePlayerJoinEvent.java index 6d703d4f..60b5a57d 100644 --- a/src/com/minecraft/moonlake/api/event/player/MoonLakePlayerJoinEvent.java +++ b/src/com/minecraft/moonlake/api/event/player/MoonLakePlayerJoinEvent.java @@ -153,6 +153,8 @@ public enum ProtocolVersion { v1_10_1(210), v1_10_2(210), v1_11(315), + v1_11_1(316), + v1_11_2(316), ; private final int protocol; diff --git a/src/com/minecraft/moonlake/api/item/ItemBuilder.java b/src/com/minecraft/moonlake/api/item/ItemBuilder.java index bdf84a5b..65bff0e0 100644 --- a/src/com/minecraft/moonlake/api/item/ItemBuilder.java +++ b/src/com/minecraft/moonlake/api/item/ItemBuilder.java @@ -21,6 +21,7 @@ import com.minecraft.moonlake.api.item.potion.PotionEffectCustom; import com.minecraft.moonlake.api.item.potion.PotionEffectType; import com.minecraft.moonlake.builder.Builder; +import com.minecraft.moonlake.exception.MoonLakeException; import org.bukkit.Color; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemFlag; @@ -410,4 +411,25 @@ public interface ItemBuilder extends Builder { * @throws IllegalArgumentException 如果物品栈类型不为 {@code Material.*Potion} 则抛出异常 */ ItemBuilder setCustomPotion(com.minecraft.moonlake.enums.PotionEffectType effectType, int amplifier, int duration, boolean ambient, boolean showParticles); + + /** + * 设置头颅物品栈的头颅材质皮肤数据 + * + * @param data 皮肤数据 + * @throws IllegalArgumentException 如果头颅材质信息对象为 {@code null} 则抛出异常 + * @throws IllegalArgumentException 如果物品栈类型不为 {@code Material.SKULL_ITEM} 则抛出异常 + * @throws MoonLakeException 如果设置头颅材质时错误则抛出异常 + */ + ItemBuilder setSkullWithSkin(String data); + + /** + * 设置头颅物品栈的头颅材质皮肤数据 + * + * @param value 头颅材质值 + * @param signature 头颅材质签名 + * @throws IllegalArgumentException 如果头颅材质值对象为 {@code null} 则抛出异常 + * @throws IllegalArgumentException 如果物品栈类型不为 {@code Material.SKULL_ITEM} 则抛出异常 + * @throws MoonLakeException 如果设置头颅材质时错误则抛出异常 + */ + ItemBuilder setSkullWithSkin(String value, String signature); } diff --git a/src/com/minecraft/moonlake/api/item/ItemBuilderWrapped.java b/src/com/minecraft/moonlake/api/item/ItemBuilderWrapped.java index 81507d5d..3d2663cb 100644 --- a/src/com/minecraft/moonlake/api/item/ItemBuilderWrapped.java +++ b/src/com/minecraft/moonlake/api/item/ItemBuilderWrapped.java @@ -522,6 +522,22 @@ public ItemBuilder setCustomPotion(com.minecraft.moonlake.enums.PotionEffectType return setCustomPotion(effectType.createCustom(amplifier, duration, ambient, showParticles)); } + @Override + public ItemBuilder setSkullWithSkin(String data) { + + update(library().setSkullWithSkin(get(), data)); + + return this; + } + + @Override + public ItemBuilder setSkullWithSkin(String value, String signature) { + + update(library().setSkullWithSkin(get(), value, signature)); + + return this; + } + @Override public ItemStack build() { diff --git a/src/com/minecraft/moonlake/api/item/ItemExpressionWrapped.java b/src/com/minecraft/moonlake/api/item/ItemExpressionWrapped.java index c6c297e5..164617ff 100644 --- a/src/com/minecraft/moonlake/api/item/ItemExpressionWrapped.java +++ b/src/com/minecraft/moonlake/api/item/ItemExpressionWrapped.java @@ -125,6 +125,18 @@ public String getSkullSkinURL(ItemStack itemStack) { return base.getSkullSkinURL(itemStack); } + @Override + public ItemStack setSkullWithSkin(ItemStack itemStack, String data) throws MoonLakeException { + + return base.setSkullWithSkin(itemStack, data); + } + + @Override + public ItemStack setSkullWithSkin(ItemStack itemStack, String value, String signature) throws MoonLakeException { + + return base.setSkullWithSkin(itemStack, value, signature); + } + @Override public ItemStack setUnbreakable(ItemStack itemStack, boolean unbreakable) { diff --git a/src/com/minecraft/moonlake/api/item/MetaExpression.java b/src/com/minecraft/moonlake/api/item/MetaExpression.java index 10384415..60e40c96 100644 --- a/src/com/minecraft/moonlake/api/item/MetaExpression.java +++ b/src/com/minecraft/moonlake/api/item/MetaExpression.java @@ -102,9 +102,7 @@ public ItemStack addDurability(ItemStack itemStack, int durability) { Validate.notNull(itemStack, "The itemstack object is null."); - int nowDurability = itemStack.getDurability(); - - return setDurability(itemStack, nowDurability - durability); // set durability subtract be add durability + return setDurability(itemStack, itemStack.getDurability() - durability); // set durability subtract be add durability } @Override @@ -112,9 +110,7 @@ public ItemStack takeDurability(ItemStack itemStack, int durability) { Validate.notNull(itemStack, "The itemstack object is null."); - int nowDurability = itemStack.getDurability(); - - return setDurability(itemStack, nowDurability + durability); // set durability add be add durability + return setDurability(itemStack, itemStack.getDurability() + durability); // set durability add be add durability } @Override @@ -448,7 +444,7 @@ public Set getFlags(ItemStack itemStack) { if(itemMeta == null) { - return null; + return new HashSet<>(); } return itemMeta.getItemFlags(); } diff --git a/src/com/minecraft/moonlake/api/item/SkullExpression.java b/src/com/minecraft/moonlake/api/item/SkullExpression.java index 592010cc..bd860b3f 100644 --- a/src/com/minecraft/moonlake/api/item/SkullExpression.java +++ b/src/com/minecraft/moonlake/api/item/SkullExpression.java @@ -110,55 +110,13 @@ public ItemStack createSkullWithOwner(String skullOwner, String displayName) { @Override public ItemStack createSkullWithSkin(String data) throws MoonLakeException { - Validate.notNull(data, "The itemstack skull skin data object is null."); - - if(data.isEmpty()) { - - return createSkull(); - } - ItemStack itemStack = createSkull(); - SkullMeta skullMeta = (SkullMeta) itemStack.getItemMeta(); - - try { - - Reflect.getField("CraftMetaSkull", Reflect.PackageType.CRAFTBUKKIT_INVENTORY, true, "profile").set(skullMeta, createGameProfile(data)); - } - catch (Exception e) { - - throw new MoonLakeException("The create skull with skin meta exception.", e); - } - itemStack.setItemMeta(skullMeta); - - return itemStack; + return setSkullWithSkin(createSkull(), data); } @Override public ItemStack createSkullWithSkins(String value, String signature) throws MoonLakeException { - Validate.notNull(value, "The itemstack skull skin value object is null."); - - if(signature == null) { - - return createSkullWithSkin(value); - } - if(value.isEmpty()) { - - return createSkull(); - } - ItemStack itemStack = createSkull(); - SkullMeta skullMeta = (SkullMeta) itemStack.getItemMeta(); - - try { - - Reflect.getField("CraftMetaSkull", Reflect.PackageType.CRAFTBUKKIT_INVENTORY, true, "profile").set(skullMeta, createGameProfile(value, signature)); - } - catch (Exception e) { - - throw new MoonLakeException("The create skull with skin meta exception.", e); - } - itemStack.setItemMeta(skullMeta); - - return itemStack; + return setSkullWithSkin(createSkull(), value, signature); } @Override @@ -220,4 +178,58 @@ public String getSkullSkinURL(ItemStack itemStack) { throw new NotImplementedException(); } + + @Override + public ItemStack setSkullWithSkin(ItemStack itemStack, String data) throws MoonLakeException { + + Validate.notNull(data, "The itemstack skull skin data object is null."); + Validate.isTrue(itemStack.getType() == Material.SKULL_ITEM, "The itemstack type not is skull."); + + if(data.isEmpty()) { + + return createSkull(); + } + SkullMeta skullMeta = (SkullMeta) itemStack.getItemMeta(); + + try { + + Reflect.getField("CraftMetaSkull", Reflect.PackageType.CRAFTBUKKIT_INVENTORY, true, "profile").set(skullMeta, createGameProfile(data)); + } + catch (Exception e) { + + throw new MoonLakeException("The create skull with skin meta exception.", e); + } + itemStack.setItemMeta(skullMeta); + + return itemStack; + } + + @Override + public ItemStack setSkullWithSkin(ItemStack itemStack, String value, String signature) throws MoonLakeException { + + Validate.notNull(value, "The itemstack skull skin value object is null."); + Validate.isTrue(itemStack.getType() == Material.SKULL_ITEM, "The itemstack type not is skull."); + + if(signature == null) { + + return setSkullWithSkin(itemStack, value); + } + if(value.isEmpty()) { + + return createSkull(); + } + SkullMeta skullMeta = (SkullMeta) itemStack.getItemMeta(); + + try { + + Reflect.getField("CraftMetaSkull", Reflect.PackageType.CRAFTBUKKIT_INVENTORY, true, "profile").set(skullMeta, createGameProfile(value, signature)); + } + catch (Exception e) { + + throw new MoonLakeException("The create skull with skin meta exception.", e); + } + itemStack.setItemMeta(skullMeta); + + return itemStack; + } } diff --git a/src/com/minecraft/moonlake/api/item/skull/SkullLibrary.java b/src/com/minecraft/moonlake/api/item/skull/SkullLibrary.java index 3b9555f2..16fcbd4a 100644 --- a/src/com/minecraft/moonlake/api/item/skull/SkullLibrary.java +++ b/src/com/minecraft/moonlake/api/item/skull/SkullLibrary.java @@ -161,4 +161,27 @@ public interface SkullLibrary { */ @Deprecated String getSkullSkinURL(ItemStack itemStack); + + /** + * 设置头颅物品栈的皮肤材质数据 + * + * @param data 头颅材质信息 + * @return ItemStack + * @throws IllegalArgumentException 如果头颅材质信息对象为 {@code null} 则抛出异常 + * @throws IllegalArgumentException 如果物品栈类型不为 {@code Material.SKULL_ITEM} 则抛出异常 + * @throws MoonLakeException 如果设置头颅材质时错误则抛出异常 + */ + ItemStack setSkullWithSkin(ItemStack itemStack, String data) throws MoonLakeException; + + /** + * 设置头颅物品栈的皮肤材质数据 + * + * @param value 头颅材质值 + * @param signature 头颅材质签名 + * @return ItemStack + * @throws IllegalArgumentException 如果头颅材质值对象为 {@code null} 则抛出异常 + * @throws IllegalArgumentException 如果物品栈类型不为 {@code Material.SKULL_ITEM} 则抛出异常 + * @throws MoonLakeException 如果设置头颅材质时错误则抛出异常 + */ + ItemStack setSkullWithSkin(ItemStack itemStack, String value, String signature) throws MoonLakeException; } diff --git a/src/com/minecraft/moonlake/manager/IoManager.java b/src/com/minecraft/moonlake/manager/IoManager.java index 230e0ff1..043943ac 100644 --- a/src/com/minecraft/moonlake/manager/IoManager.java +++ b/src/com/minecraft/moonlake/manager/IoManager.java @@ -217,7 +217,7 @@ public static Map readLangFile(String prefix, File lang) { String key = line.substring(0, $); String value = line.substring($ + 1); - if (value.charAt(0) != '#') { + if (!value.isEmpty() && value.charAt(0) != '#') { temp.put(key, value.contains("" + StringUtil.COLOR_CHAR) ? StringUtil.toColor(prefix + value) : prefix + value); }