This commit is contained in:
sky 2020-01-20 16:20:30 +08:00
parent 098970b05d
commit 8f2b0f419e
5 changed files with 71 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package io.izzel.taboolib.module.lite; package io.izzel.taboolib.module.lite;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import io.izzel.taboolib.TabooLibAPI; import io.izzel.taboolib.TabooLibAPI;
import io.izzel.taboolib.module.locale.logger.TLogger; import io.izzel.taboolib.module.locale.logger.TLogger;
import io.izzel.taboolib.util.Ref; import io.izzel.taboolib.util.Ref;
@ -10,6 +11,7 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* @Author 坏黑 * @Author 坏黑
@ -18,6 +20,7 @@ import java.util.Map;
public class SimpleReflection { public class SimpleReflection {
private static Map<String, Map<String, Field>> fieldCached = Maps.newHashMap(); private static Map<String, Map<String, Field>> fieldCached = Maps.newHashMap();
private static Set<String> fieldLogger = Sets.newHashSet();
public static boolean isExists(Class<?> nmsClass) { public static boolean isExists(Class<?> nmsClass) {
return fieldCached.containsKey(nmsClass.getName()); return fieldCached.containsKey(nmsClass.getName());
@ -66,11 +69,15 @@ public class SimpleReflection {
public static void setFieldValue(Class<?> nmsClass, Object instance, String fieldName, Object value) { public static void setFieldValue(Class<?> nmsClass, Object instance, String fieldName, Object value) {
Map<String, Field> fields = fieldCached.get(nmsClass.getName()); Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) { if (fields == null) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName()); if (fieldLogger.add(nmsClass.getName() + "$" + fieldName)) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName() + "$" + fieldName);
}
} }
Field field = fields.get(fieldName); Field field = fields.get(fieldName);
if (value == null) { if (value == null) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName()); if (fieldLogger.add(nmsClass.getName() + "$" + fieldName)) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName() + "$" + fieldName);
}
} }
try { try {
field.set(instance, value); field.set(instance, value);
@ -82,11 +89,15 @@ public class SimpleReflection {
public static Object getFieldValue(Class<?> nmsClass, Object instance, String fieldName) { public static Object getFieldValue(Class<?> nmsClass, Object instance, String fieldName) {
Map<String, Field> fields = fieldCached.get(nmsClass.getName()); Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) { if (fields == null) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName()); if (fieldLogger.add(nmsClass.getName() + "$" + fieldName)) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName() + "$" + fieldName);
}
} }
Field field = fields.get(fieldName); Field field = fields.get(fieldName);
if (field == null) { if (field == null) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName()); if (fieldLogger.add(nmsClass.getName() + "$" + fieldName)) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName() + "$" + fieldName);
}
} }
try { try {
return field.get(instance); return field.get(instance);
@ -99,11 +110,15 @@ public class SimpleReflection {
public static <T> T getFieldValue(Class<?> nmsClass, Object instance, String fieldName, T def) { public static <T> T getFieldValue(Class<?> nmsClass, Object instance, String fieldName, T def) {
Map<String, Field> fields = fieldCached.get(nmsClass.getName()); Map<String, Field> fields = fieldCached.get(nmsClass.getName());
if (fields == null) { if (fields == null) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName()); if (fieldLogger.add(nmsClass.getName() + "$" + fieldName)) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName() + "$" + fieldName);
}
} }
Field field = fields.get(fieldName); Field field = fields.get(fieldName);
if (field == null) { if (field == null) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName()); if (fieldLogger.add(nmsClass.getName() + "$" + fieldName)) {
TLogger.getGlobalLogger().error("Field Not Found: " + nmsClass.getName() + "$" + fieldName);
}
} }
try { try {
return (T) field.get(instance); return (T) field.get(instance);

View File

@ -2,6 +2,7 @@ package io.izzel.taboolib.module.nms.nbt;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.util.NumberConversions;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -83,11 +84,11 @@ public class NBTBase {
} }
public String asString() { public String asString() {
return (String) data; return String.valueOf(data);
} }
public byte asByte() { public byte asByte() {
return (byte) data; return NumberConversions.toByte(data);
} }
public byte[] asByteArray() { public byte[] asByteArray() {
@ -95,7 +96,7 @@ public class NBTBase {
} }
public int asInt() { public int asInt() {
return (int) data; return NumberConversions.toInt(data);
} }
public int[] asIntArray() { public int[] asIntArray() {
@ -103,19 +104,19 @@ public class NBTBase {
} }
public double asDouble() { public double asDouble() {
return (double) data; return NumberConversions.toDouble(data);
} }
public float asFloat() { public float asFloat() {
return (float) data; return NumberConversions.toFloat(data);
} }
public short asShort() { public short asShort() {
return (short) data; return NumberConversions.toShort(data);
} }
public long asLong() { public long asLong() {
return (long) data; return NumberConversions.toLong(data);
} }
public NBTCompound asCompound() { public NBTCompound asCompound() {

View File

@ -49,7 +49,7 @@ public class TPacketHandler implements Listener {
} }
public static void addListener(Plugin plugin, TPacketListener listener) { public static void addListener(Plugin plugin, TPacketListener listener) {
packetListeners.computeIfAbsent(plugin.getName(), name -> Lists.newArrayList()).add(listener); packetListeners.computeIfAbsent(plugin.getName(), name -> Lists.newCopyOnWriteArrayList()).add(listener);
} }
public static void removeListener(Plugin plugin) { public static void removeListener(Plugin plugin) {

View File

@ -1,11 +1,11 @@
package io.izzel.taboolib.module.packet.channel; package io.izzel.taboolib.module.packet.channel;
import io.izzel.taboolib.module.packet.Packet; import io.izzel.taboolib.module.packet.Packet;
import io.izzel.taboolib.module.packet.TPacketHandler;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler; import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise; import io.netty.channel.ChannelPromise;
import io.izzel.taboolib.module.packet.TPacketHandler;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.Collection; import java.util.Collection;

View File

@ -1,6 +1,7 @@
package io.izzel.taboolib.util.item; package io.izzel.taboolib.util.item;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.gson.*;
import io.izzel.taboolib.Version; import io.izzel.taboolib.Version;
import io.izzel.taboolib.module.lite.SimpleEquip; import io.izzel.taboolib.module.lite.SimpleEquip;
import io.izzel.taboolib.module.lite.SimpleI18n; import io.izzel.taboolib.module.lite.SimpleI18n;
@ -305,6 +306,45 @@ public class Items {
return NMS.handle().saveNBT(item, nbt); return NMS.handle().saveNBT(item, nbt);
} }
public static ItemStack fromJson(String item) {
JsonElement json = new JsonParser().parse(item);
if (json instanceof JsonObject) {
ItemBuilder itemBuilder = new ItemBuilder(Material.STONE);
JsonElement type = ((JsonObject) json).get("type");
if (type != null) {
itemBuilder.material(Items.asMaterial(type.getAsString()));
}
JsonElement data = ((JsonObject) json).get("data");
if (data != null) {
itemBuilder.damage(data.getAsInt());
}
JsonElement amount = ((JsonObject) json).get("amount");
if (amount != null) {
itemBuilder.amount(amount.getAsInt());
}
ItemStack itemBuild = itemBuilder.build();
JsonElement meta = ((JsonObject) json).get("meta");
if (meta != null) {
return NMS.handle().saveNBT(itemBuild, NBTCompound.fromJson(meta.toString()));
}
return itemBuild;
}
return null;
}
public static String toJson(ItemStack item) {
JsonObject json = new JsonObject();
json.addProperty("type", item.getType().name());
json.addProperty("data", item.getData().getData());
json.addProperty("amount", item.getAmount());
json.add("meta", new JsonParser().parse(NMS.handle().loadNBT(item).toJson()));
return json.toString();
}
public static String toJsonFormatted(ItemStack item) {
return new GsonBuilder().setPrettyPrinting().create().toJson(new Gson().toJsonTree(toJson(item)));
}
public interface Matcher { public interface Matcher {
boolean match(ItemStack item); boolean match(ItemStack item);