版本更新至:3.76

调整:开发框架改为 Gradle
新增:Language2 工具新增 [book] 类型
This commit is contained in:
坏黑
2018-03-10 21:13:05 +08:00
parent 6439e4b780
commit ad1a21196f
238 changed files with 1686 additions and 1132 deletions

View File

@@ -0,0 +1,192 @@
package me.skymc.taboolib.itemnbtapi;
import java.util.Set;
import me.skymc.taboolib.TabooLib;
public class NBTCompound {
private String compundName;
private NBTCompound parent;
protected NBTCompound(NBTCompound owner, String name) {
this.compundName = name;
this.parent = owner;
}
public String getName() {
return compundName;
}
protected Object getCompound() {
return parent.getCompound();
}
protected void setCompound(Object compound) {
parent.setCompound(compound);
}
public NBTCompound getParent() {
return parent;
}
public void mergeCompound(NBTCompound comp){
NBTReflectionUtil.addOtherNBTCompound(this, comp);
}
public void setString(String key, String value) {
NBTReflectionUtil.setString(this, key, value);
}
public String getString(String key) {
return NBTReflectionUtil.getString(this, key);
}
protected String getContent(String key) {
return NBTReflectionUtil.getContent(this, key);
}
public void setInteger(String key, Integer value) {
NBTReflectionUtil.setInt(this, key, value);
}
public Integer getInteger(String key) {
return NBTReflectionUtil.getInt(this, key);
}
public void setDouble(String key, Double value) {
NBTReflectionUtil.setDouble(this, key, value);
}
public Double getDouble(String key) {
return NBTReflectionUtil.getDouble(this, key);
}
public void setByte(String key, Byte value) {
NBTReflectionUtil.setByte(this, key, value);
}
public Byte getByte(String key) {
return NBTReflectionUtil.getByte(this, key);
}
public void setShort(String key, Short value) {
NBTReflectionUtil.setShort(this, key, value);
}
public Short getShort(String key) {
return NBTReflectionUtil.getShort(this, key);
}
public void setLong(String key, Long value) {
NBTReflectionUtil.setLong(this, key, value);
}
public Long getLong(String key) {
return NBTReflectionUtil.getLong(this, key);
}
public void setFloat(String key, Float value) {
NBTReflectionUtil.setFloat(this, key, value);
}
public Float getFloat(String key) {
return NBTReflectionUtil.getFloat(this, key);
}
public void setByteArray(String key, byte[] value) {
NBTReflectionUtil.setByteArray(this, key, value);
}
public byte[] getByteArray(String key) {
return NBTReflectionUtil.getByteArray(this, key);
}
public void setIntArray(String key, int[] value) {
NBTReflectionUtil.setIntArray(this, key, value);
}
public int[] getIntArray(String key) {
return NBTReflectionUtil.getIntArray(this, key);
}
public void setBoolean(String key, Boolean value) {
NBTReflectionUtil.setBoolean(this, key, value);
}
protected void set(String key, Object val) {
NBTReflectionUtil.set(this, key, val);
}
public Boolean getBoolean(String key) {
return NBTReflectionUtil.getBoolean(this, key);
}
public void setObject(String key, Object value) {
NBTReflectionUtil.setObject(this, key, value);
}
public <T> T getObject(String key, Class<T> type) {
return NBTReflectionUtil.getObject(this, key, type);
}
public Boolean hasKey(String key) {
return NBTReflectionUtil.hasKey(this, key);
}
public void removeKey(String key) {
NBTReflectionUtil.remove(this, key);
}
public Set<String> getKeys() {
return NBTReflectionUtil.getKeys(this);
}
public NBTCompound addCompound(String name) {
NBTReflectionUtil.addNBTTagCompound(this, name);
return getCompound(name);
}
public NBTCompound getCompound(String name) {
NBTCompound next = new NBTCompound(this, name);
if (NBTReflectionUtil.valideCompound(next)) return next;
return null;
}
public NBTList getList(String name, NBTType type) {
return NBTReflectionUtil.getList(this, name, type);
}
public NBTType getType(String name) {
if (TabooLib.getVerint() == 10700) return NBTType.NBTTagEnd;
return NBTType.valueOf(NBTReflectionUtil.getType(this, name));
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
for (String key : getKeys()) {
result.append(toString(key));
}
return result.toString();
}
public String toString(String key) {
StringBuilder result = new StringBuilder();
NBTCompound compound = this;
while (compound.getParent() != null) {
result.append(" ");
compound = compound.getParent();
}
if (this.getType(key) == NBTType.NBTTagCompound) {
return this.getCompound(key).toString();
} else {
return result + "-" + key + ": " + getContent(key) + System.lineSeparator();
}
}
public String asNBTString(){
return getCompound().toString();
}
}

View File

@@ -0,0 +1,35 @@
package me.skymc.taboolib.itemnbtapi;
public class NBTContainer extends NBTCompound{
private Object nbt;
public NBTContainer() {
super(null, null);
nbt = NBTReflectionUtil.getNewNBTTag();
}
protected NBTContainer(Object nbt){
super(null, null);
this.nbt = nbt;
}
public NBTContainer(String nbtString) throws IllegalArgumentException {
super(null, null);
try{
nbt = NBTReflectionUtil.parseNBT(nbtString);
}catch(Exception ex){
ex.printStackTrace();
throw new IllegalArgumentException("Malformed Json: " + ex.getMessage());
}
}
protected Object getCompound() {
return nbt;
}
protected void setCompound(Object tag) {
nbt = tag;
}
}

View File

@@ -0,0 +1,22 @@
package me.skymc.taboolib.itemnbtapi;
import org.bukkit.entity.Entity;
public class NBTEntity extends NBTCompound {
private final Entity ent;
public NBTEntity(Entity entity) {
super(null, null);
ent = entity;
}
protected Object getCompound() {
return NBTReflectionUtil.getEntityNBTTagCompound(NBTReflectionUtil.getNMSEntity(ent));
}
protected void setCompound(Object compound) {
NBTReflectionUtil.setEntityNBTTag(compound, NBTReflectionUtil.getNMSEntity(ent));
}
}

View File

@@ -0,0 +1,46 @@
package me.skymc.taboolib.itemnbtapi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class NBTFile extends NBTCompound {
private final File file;
private Object nbt;
public NBTFile(File file) throws IOException {
super(null, null);
this.file = file;
if (file.exists()) {
FileInputStream inputsteam = new FileInputStream(file);
nbt = NBTReflectionUtil.readNBTFile(inputsteam);
} else {
nbt = NBTReflectionUtil.getNewNBTTag();
save();
}
}
public void save() throws IOException {
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
FileOutputStream outStream = new FileOutputStream(file);
NBTReflectionUtil.saveNBTFile(nbt, outStream);
}
public File getFile() {
return file;
}
protected Object getCompound() {
return nbt;
}
protected void setCompound(Object compound) {
nbt = compound;
}
}

View File

@@ -0,0 +1,38 @@
package me.skymc.taboolib.itemnbtapi;
import org.bukkit.inventory.ItemStack;
public class NBTItem extends NBTCompound {
private ItemStack bukkitItem;
public NBTItem(ItemStack item) {
super(null, null);
bukkitItem = item.clone();
}
protected Object getCompound() {
return NBTReflectionUtil.getItemRootNBTTagCompound(NBTReflectionUtil.getNMSItemStack(bukkitItem));
}
protected void setCompound(Object compound) {
bukkitItem = NBTReflectionUtil.getBukkitItemStack(NBTReflectionUtil.setNBTTag(compound, NBTReflectionUtil.getNMSItemStack(bukkitItem)));
}
public ItemStack getItem() {
return bukkitItem;
}
protected void setItem(ItemStack item) {
bukkitItem = item;
}
public static NBTContainer convertItemtoNBT(ItemStack item){
return NBTReflectionUtil.convertNMSItemtoNBTCompound(NBTReflectionUtil.getNMSItemStack(item));
}
public static ItemStack convertNBTtoItem(NBTCompound comp){
return NBTReflectionUtil.getBukkitItemStack(NBTReflectionUtil.convertNBTCompoundtoNMSItem(comp));
}
}

View File

@@ -0,0 +1,128 @@
package me.skymc.taboolib.itemnbtapi;
import java.lang.reflect.Method;
import me.skymc.taboolib.itemnbtapi.utils.MethodNames;
import me.skymc.taboolib.message.MsgUtils;
public class NBTList {
private String listName;
private NBTCompound parent;
private NBTType type;
private Object listObject;
protected NBTList(NBTCompound owner, String name, NBTType type, Object list) {
parent = owner;
listName = name;
this.type = type;
this.listObject = list;
if (!(type == NBTType.NBTTagString || type == NBTType.NBTTagCompound)) {
System.err.println("List types != String/Compound are currently not implemented!");
}
}
protected void save() {
parent.set(listName, listObject);
}
public NBTListCompound addCompound() {
if (type != NBTType.NBTTagCompound) {
new Throwable("Using Compound method on a non Compound list!").printStackTrace();
return null;
}
try {
Method method = listObject.getClass().getMethod("add", NBTReflectionUtil.getNBTBase());
Object compound = NBTReflectionUtil.getNBTTagCompound().newInstance();
method.invoke(listObject, compound);
return new NBTListCompound(this, compound);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public NBTListCompound getCompound(int id) {
if (type != NBTType.NBTTagCompound) {
new Throwable("Using Compound method on a non Compound list!").printStackTrace();
return null;
}
try {
Method method = listObject.getClass().getMethod("get", int.class);
Object compound = method.invoke(listObject, id);
return new NBTListCompound(this, compound);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public String getString(int i) {
if (type != NBTType.NBTTagString) {
new Throwable("Using String method on a non String list!").printStackTrace();
return null;
}
try {
Method method = listObject.getClass().getMethod("getString", int.class);
return (String) method.invoke(listObject, i);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
@SuppressWarnings("unchecked")
public void addString(String s) {
if (type != NBTType.NBTTagString) {
new Throwable("Using String method on a non String list!").printStackTrace();
return;
}
try {
Method method = listObject.getClass().getMethod("add", NBTReflectionUtil.getNBTBase());
method.invoke(listObject, NBTReflectionUtil.getNBTTagString().getConstructor(String.class).newInstance(s));
save();
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
@SuppressWarnings("unchecked")
public void setString(int i, String s) {
if (type != NBTType.NBTTagString) {
new Throwable("Using String method on a non String list!").printStackTrace();
return;
}
try {
Method method = listObject.getClass().getMethod("a", int.class, NBTReflectionUtil.getNBTBase());
method.invoke(listObject, i, NBTReflectionUtil.getNBTTagString().getConstructor(String.class).newInstance(s));
save();
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public void remove(int i) {
try {
Method method = listObject.getClass().getMethod(MethodNames.getRemoveMethodName(), int.class);
method.invoke(listObject, i);
save();
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public int size() {
try {
Method method = listObject.getClass().getMethod("size");
return (int) method.invoke(listObject);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return -1;
}
public NBTType getType() {
return type;
}
}

View File

@@ -0,0 +1,104 @@
package me.skymc.taboolib.itemnbtapi;
import java.util.HashSet;
import java.util.Set;
import me.skymc.taboolib.message.MsgUtils;
public class NBTListCompound {
private NBTList owner;
private Object compound;
protected NBTListCompound(NBTList parent, Object obj) {
owner = parent;
compound = obj;
}
public void setString(String key, String value) {
if (value == null) {
remove(key);
return;
}
try {
compound.getClass().getMethod("setString", String.class, String.class).invoke(compound, key, value);
owner.save();
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public void setInteger(String key, int value) {
try {
compound.getClass().getMethod("setInt", String.class, int.class).invoke(compound, key, value);
owner.save();
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public int getInteger(String value) {
try {
return (int) compound.getClass().getMethod("getInt", String.class).invoke(compound, value);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return 0;
}
public void setDouble(String key, double value) {
try {
compound.getClass().getMethod("setDouble", String.class, double.class).invoke(compound, key, value);
owner.save();
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public double getDouble(String key) {
try {
return (double) compound.getClass().getMethod("getDouble", String.class).invoke(compound, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return 0;
}
public String getString(String key) {
try {
return (String) compound.getClass().getMethod("getString", String.class).invoke(compound, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return "";
}
public boolean hasKey(String key) {
try {
return (boolean) compound.getClass().getMethod("hasKey", String.class).invoke(compound, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return false;
}
@SuppressWarnings("unchecked")
public Set<String> getKeys() {
try {
return (Set<String>) compound.getClass().getMethod("c").invoke(compound);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return new HashSet<>();
}
public void remove(String key) {
try {
compound.getClass().getMethod("remove", String.class).invoke(compound, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
}

View File

@@ -0,0 +1,978 @@
package me.skymc.taboolib.itemnbtapi;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.Set;
import java.util.Stack;
import org.bukkit.Bukkit;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import me.skymc.taboolib.TabooLib;
import me.skymc.taboolib.itemnbtapi.utils.GsonWrapper;
import me.skymc.taboolib.itemnbtapi.utils.MethodNames;
import me.skymc.taboolib.message.MsgUtils;
// TODO: finish codestyle cleanup -sgdc3
public class NBTReflectionUtil {
private static final String version = TabooLib.getVersion();
@SuppressWarnings("rawtypes")
private static Class getCraftItemStack() {
try {
Class clazz = Class.forName("org.bukkit.craftbukkit." + version + ".inventory.CraftItemStack");
return clazz;
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
return null;
}
}
@SuppressWarnings("rawtypes")
private static Class getCraftEntity() {
try {
Class clazz = Class.forName("org.bukkit.craftbukkit." + version + ".entity.CraftEntity");
return clazz;
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
return null;
}
}
@SuppressWarnings("rawtypes")
protected static Class getNBTBase() {
try {
Class clazz = Class.forName("net.minecraft.server." + version + ".NBTBase");
return clazz;
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
return null;
}
}
@SuppressWarnings("rawtypes")
protected static Class getNBTTagString() {
try {
Class clazz = Class.forName("net.minecraft.server." + version + ".NBTTagString");
return clazz;
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
return null;
}
}
@SuppressWarnings("rawtypes")
protected static Class getNMSItemStack() {
try {
Class clazz = Class.forName("net.minecraft.server." + version + ".ItemStack");
return clazz;
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
return null;
}
}
@SuppressWarnings("rawtypes")
protected static Class getNBTTagCompound() {
try {
Class clazz = Class.forName("net.minecraft.server." + version + ".NBTTagCompound");
return clazz;
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
return null;
}
}
@SuppressWarnings("rawtypes")
protected static Class getNBTCompressedStreamTools() {
try {
Class clazz = Class.forName("net.minecraft.server." + version + ".NBTCompressedStreamTools");
return clazz;
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
return null;
}
}
@SuppressWarnings("rawtypes")
protected static Class getMojangsonParser() {
try {
Class c = Class.forName("net.minecraft.server." + version + ".MojangsonParser");
return c;
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
return null;
}
}
@SuppressWarnings("rawtypes")
protected static Class getTileEntity() {
try {
Class clazz = Class.forName("net.minecraft.server." + version + ".TileEntity");
return clazz;
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
return null;
}
}
@SuppressWarnings("rawtypes")
protected static Class getCraftWorld() {
try {
Class clazz = Class.forName("org.bukkit.craftbukkit." + version + ".CraftWorld");
return clazz;
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
return null;
}
}
public static Object getNewNBTTag() {
String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
try {
@SuppressWarnings("rawtypes")
Class c = Class.forName("net.minecraft.server." + version + ".NBTTagCompound");
return c.newInstance();
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
return null;
}
}
private static Object getNewBlockPosition(int x, int y, int z) {
String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
try {
@SuppressWarnings("rawtypes")
Class clazz = Class.forName("net.minecraft.server." + version + ".BlockPosition");
return clazz.getConstructor(int.class, int.class, int.class).newInstance(x, y, z);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
return null;
}
}
public static Object setNBTTag(Object NBTTag, Object NMSItem) {
try {
Method method;
method = NMSItem.getClass().getMethod("setTag", NBTTag.getClass());
method.invoke(NMSItem, NBTTag);
return NMSItem;
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
@SuppressWarnings("unchecked")
public static Object getNMSItemStack(ItemStack item) {
@SuppressWarnings("rawtypes")
Class clazz = getCraftItemStack();
Method method;
try {
method = clazz.getMethod("asNMSCopy", ItemStack.class);
Object answer = method.invoke(clazz, item);
return answer;
} catch (Exception e) {
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
}
return null;
}
@SuppressWarnings("unchecked")
public static Object getNMSEntity(Entity entity) {
@SuppressWarnings("rawtypes")
Class clazz = getCraftEntity();
Method method;
try {
method = clazz.getMethod("getHandle");
return method.invoke(getCraftEntity().cast(entity));
} catch (Exception e) {
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
}
return null;
}
@SuppressWarnings({"unchecked"})
public static Object parseNBT(String json) {
@SuppressWarnings("rawtypes")
Class cis = getMojangsonParser();
java.lang.reflect.Method method;
try {
method = cis.getMethod("parse", String.class);
return method.invoke(null, json);
} catch (Exception e) {
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
}
return null;
}
@SuppressWarnings({"unchecked"})
public static Object readNBTFile(FileInputStream stream) {
@SuppressWarnings("rawtypes")
Class clazz = getNBTCompressedStreamTools();
Method method;
try {
method = clazz.getMethod("a", InputStream.class);
return method.invoke(clazz, stream);
} catch (Exception e) {
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
}
return null;
}
@SuppressWarnings({"unchecked"})
public static Object saveNBTFile(Object nbt, FileOutputStream stream) {
@SuppressWarnings("rawtypes")
Class clazz = getNBTCompressedStreamTools();
Method method;
try {
method = clazz.getMethod("a", getNBTTagCompound(), OutputStream.class);
return method.invoke(clazz, nbt, stream);
} catch (Exception e) {
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
}
return null;
}
@SuppressWarnings({"unchecked"})
public static ItemStack getBukkitItemStack(Object item) {
@SuppressWarnings("rawtypes")
Class clazz = getCraftItemStack();
Method method;
try {
method = clazz.getMethod("asCraftMirror", item.getClass());
Object answer = method.invoke(clazz, item);
return (ItemStack) answer;
} catch (Exception e) {
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
}
return null;
}
@SuppressWarnings({"unchecked"})
public static Object getItemRootNBTTagCompound(Object nmsitem) {
@SuppressWarnings("rawtypes")
Class clazz = nmsitem.getClass();
Method method;
try {
method = clazz.getMethod("getTag");
Object answer = method.invoke(nmsitem);
return answer;
} catch (Exception e) {
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
}
return null;
}
@SuppressWarnings({"unchecked"})
public static Object convertNBTCompoundtoNMSItem(NBTCompound nbtcompound) {
@SuppressWarnings("rawtypes")
Class clazz = getNMSItemStack();
try {
Object nmsstack = clazz.getConstructor(getNBTTagCompound()).newInstance(nbtcompound.getCompound());
return nmsstack;
} catch (Exception e) {
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
}
return null;
}
@SuppressWarnings({"unchecked"})
public static NBTContainer convertNMSItemtoNBTCompound(Object nmsitem) {
@SuppressWarnings("rawtypes")
Class clazz = nmsitem.getClass();
Method method;
try {
method = clazz.getMethod("save", getNBTTagCompound());
Object answer = method.invoke(nmsitem, getNewNBTTag());
return new NBTContainer(answer);
} catch (Exception e) {
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
}
return null;
}
@SuppressWarnings({"unchecked"})
public static Object getEntityNBTTagCompound(Object nmsitem) {
@SuppressWarnings("rawtypes")
Class c = nmsitem.getClass();
Method method;
try {
method = c.getMethod(MethodNames.getEntityNbtGetterMethodName(), getNBTTagCompound());
Object nbt = getNBTTagCompound().newInstance();
Object answer = method.invoke(nmsitem, nbt);
if (answer == null)
answer = nbt;
return answer;
} catch (Exception e) {
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
}
return null;
}
public static Object setEntityNBTTag(Object NBTTag, Object NMSItem) {
try {
Method method;
method = NMSItem.getClass().getMethod(MethodNames.getEntityNbtSetterMethodName(), getNBTTagCompound());
method.invoke(NMSItem, NBTTag);
return NMSItem;
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public static Object getTileEntityNBTTagCompound(BlockState tile) {
Method method;
try {
Object pos = getNewBlockPosition(tile.getX(), tile.getY(), tile.getZ());
Object cworld = getCraftWorld().cast(tile.getWorld());
Object nmsworld = cworld.getClass().getMethod("getHandle").invoke(cworld);
Object o = nmsworld.getClass().getMethod("getTileEntity", pos.getClass()).invoke(nmsworld, pos);
method = getTileEntity().getMethod(MethodNames.getTileDataMethodName(), getNBTTagCompound());
Object tag = getNBTTagCompound().newInstance();
Object answer = method.invoke(o, tag);
if (answer == null)
answer = tag;
return answer;
} catch (Exception e) {
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
}
return null;
}
public static void setTileEntityNBTTagCompound(BlockState tile, Object comp) {
Method method;
try {
Object pos = getNewBlockPosition(tile.getX(), tile.getY(), tile.getZ());
Object cworld = getCraftWorld().cast(tile.getWorld());
Object nmsworld = cworld.getClass().getMethod("getHandle").invoke(cworld);
Object o = nmsworld.getClass().getMethod("getTileEntity", pos.getClass()).invoke(nmsworld, pos);
method = getTileEntity().getMethod("a", getNBTTagCompound());
method.invoke(o, comp);
} catch (Exception e) {
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
}
}
@SuppressWarnings("unchecked")
public static Object getSubNBTTagCompound(Object compound, String name) {
@SuppressWarnings("rawtypes")
Class c = compound.getClass();
Method method;
try {
method = c.getMethod("getCompound", String.class);
Object answer = method.invoke(compound, name);
return answer;
} catch (Exception e) {
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
}
return null;
}
public static void addNBTTagCompound(NBTCompound comp, String name) {
if (name == null) {
remove(comp, name);
return;
}
Object nbttag = comp.getCompound();
if (nbttag == null) {
nbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return;
Object workingtag = gettoCompount(nbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("set", String.class, getNBTBase());
method.invoke(workingtag, name, getNBTTagCompound().newInstance());
comp.setCompound(nbttag);
return;
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return;
}
public static Boolean valideCompound(NBTCompound comp) {
Object root = comp.getCompound();
if (root == null) {
root = getNewNBTTag();
}
return (gettoCompount(root, comp)) != null;
}
private static Object gettoCompount(Object nbttag, NBTCompound comp) {
Stack<String> structure = new Stack<>();
while (comp.getParent() != null) {
structure.add(comp.getName());
comp = comp.getParent();
}
while (!structure.isEmpty()) {
nbttag = getSubNBTTagCompound(nbttag, structure.pop());
if (nbttag == null) {
return null;
}
}
return nbttag;
}
public static void addOtherNBTCompound(NBTCompound comp, NBTCompound nbtcompound) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("a", getNBTTagCompound());
method.invoke(workingtag, nbtcompound.getCompound());
comp.setCompound(rootnbttag);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public static void setString(NBTCompound comp, String key, String text) {
if (text == null) {
remove(comp, key);
return;
}
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("setString", String.class, String.class);
method.invoke(workingtag, key, text);
comp.setCompound(rootnbttag);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public static String getString(NBTCompound comp, String key) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return null;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("getString", String.class);
return (String) method.invoke(workingtag, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public static String getContent(NBTCompound comp, String key) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return null;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("get", String.class);
return method.invoke(workingtag, key).toString();
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public static void setInt(NBTCompound comp, String key, Integer i) {
if (i == null) {
remove(comp, key);
return;
}
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("setInt", String.class, int.class);
method.invoke(workingtag, key, i);
comp.setCompound(rootnbttag);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public static Integer getInt(NBTCompound comp, String key) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return null;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("getInt", String.class);
return (Integer) method.invoke(workingtag, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public static void setByteArray(NBTCompound comp, String key, byte[] b) {
if (b == null) {
remove(comp, key);
return;
}
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("setByteArray", String.class, byte[].class);
method.invoke(workingtag, key, b);
comp.setCompound(rootnbttag);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return;
}
public static byte[] getByteArray(NBTCompound comp, String key) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return null;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("getByteArray", String.class);
return (byte[]) method.invoke(workingtag, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public static void setIntArray(NBTCompound comp, String key, int[] i) {
if (i == null) {
remove(comp, key);
return;
}
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("setIntArray", String.class, int[].class);
method.invoke(workingtag, key, i);
comp.setCompound(rootnbttag);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public static int[] getIntArray(NBTCompound comp, String key) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return null;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("getIntArray", String.class);
return (int[]) method.invoke(workingtag, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public static void setFloat(NBTCompound comp, String key, Float f) {
if (f == null) {
remove(comp, key);
return;
}
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("setFloat", String.class, float.class);
method.invoke(workingtag, key, (float) f);
comp.setCompound(rootnbttag);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public static Float getFloat(NBTCompound comp, String key) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return null;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("getFloat", String.class);
return (Float) method.invoke(workingtag, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public static void setLong(NBTCompound comp, String key, Long f) {
if (f == null) {
remove(comp, key);
return;
}
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("setLong", String.class, long.class);
method.invoke(workingtag, key, (long) f);
comp.setCompound(rootnbttag);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public static Long getLong(NBTCompound comp, String key) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return null;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("getLong", String.class);
return (Long) method.invoke(workingtag, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public static void setShort(NBTCompound comp, String key, Short f) {
if (f == null) {
remove(comp, key);
return;
}
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("setShort", String.class, short.class);
method.invoke(workingtag, key, (short) f);
comp.setCompound(rootnbttag);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public static Short getShort(NBTCompound comp, String key) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return null;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("getShort", String.class);
return (Short) method.invoke(workingtag, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public static void setByte(NBTCompound comp, String key, Byte f) {
if (f == null) {
remove(comp, key);
return;
}
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("setByte", String.class, byte.class);
method.invoke(workingtag, key, (byte) f);
comp.setCompound(rootnbttag);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public static Byte getByte(NBTCompound comp, String key) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return null;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("getByte", String.class);
return (Byte) method.invoke(workingtag, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public static void setDouble(NBTCompound comp, String key, Double d) {
if (d == null) {
remove(comp, key);
return;
}
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("setDouble", String.class, double.class);
method.invoke(workingtag, key, d);
comp.setCompound(rootnbttag);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public static Double getDouble(NBTCompound comp, String key) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return null;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("getDouble", String.class);
return (Double) method.invoke(workingtag, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public static byte getType(NBTCompound comp, String key) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return 0;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod(MethodNames.getTypeMethodName(), String.class);
return (byte) method.invoke(workingtag, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return 0;
}
public static void setBoolean(NBTCompound comp, String key, Boolean d) {
if (d == null) {
remove(comp, key);
return;
}
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("setBoolean", String.class, boolean.class);
method.invoke(workingtag, key, d);
comp.setCompound(rootnbttag);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public static Boolean getBoolean(NBTCompound comp, String key) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return null;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("getBoolean", String.class);
return (Boolean) method.invoke(workingtag, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public static void set(NBTCompound comp, String key, Object val) {
if (val == null) {
remove(comp, key);
return;
}
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) {
new Throwable("InvalideCompound").printStackTrace();
return;
}
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("set", String.class, getNBTBase());
method.invoke(workingtag, key, val);
comp.setCompound(rootnbttag);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public static NBTList getList(NBTCompound comp, String key, NBTType type) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return null;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("getList", String.class, int.class);
return new NBTList(comp, key, type, method.invoke(workingtag, key, type.getId()));
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
public static void setObject(NBTCompound comp, String key, Object value) {
try {
String json = GsonWrapper.getString(value);
setString(comp, key, json);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public static <T> T getObject(NBTCompound comp, String key, Class<T> type) {
String json = getString(comp, key);
if (json == null) {
return null;
}
return GsonWrapper.deserializeJson(json, type);
}
public static void remove(NBTCompound comp, String key) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("remove", String.class);
method.invoke(workingtag, key);
comp.setCompound(rootnbttag);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
}
public static Boolean hasKey(NBTCompound comp, String key) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return null;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("hasKey", String.class);
return (Boolean) method.invoke(workingtag, key);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
@SuppressWarnings("unchecked")
public static Set<String> getKeys(NBTCompound comp) {
Object rootnbttag = comp.getCompound();
if (rootnbttag == null) {
rootnbttag = getNewNBTTag();
}
if (!valideCompound(comp)) return null;
Object workingtag = gettoCompount(rootnbttag, comp);
Method method;
try {
method = workingtag.getClass().getMethod("c");
return (Set<String>) method.invoke(workingtag);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
}
return null;
}
}

View File

@@ -0,0 +1,22 @@
package me.skymc.taboolib.itemnbtapi;
import org.bukkit.block.BlockState;
public class NBTTileEntity extends NBTCompound {
private final BlockState tile;
public NBTTileEntity(BlockState tile) {
super(null, null);
this.tile = tile;
}
protected Object getCompound() {
return NBTReflectionUtil.getTileEntityNBTTagCompound(tile);
}
protected void setCompound(Object compound) {
NBTReflectionUtil.setTileEntityNBTTagCompound(tile, compound);
}
}

View File

@@ -0,0 +1,34 @@
package me.skymc.taboolib.itemnbtapi;
public enum NBTType {
NBTTagEnd(0),
NBTTagByte(1),
NBTTagShort(2),
NBTTagInt(3),
NBTTagLong(4),
NBTTagFloat(5),
NBTTagDouble(6),
NBTTagByteArray(7),
NBTTagIntArray(11),
NBTTagString(8),
NBTTagList(9),
NBTTagCompound(10);
NBTType(int i) {
id = i;
}
private final int id;
public int getId() {
return id;
}
public static NBTType valueOf(int id) {
for (NBTType t : values())
if (t.getId() == id)
return t;
return NBTType.NBTTagEnd;
}
}

View File

@@ -0,0 +1,29 @@
package me.skymc.taboolib.itemnbtapi.utils;
import com.google.gson.Gson;
import me.skymc.taboolib.message.MsgUtils;
public class GsonWrapper {
private static final Gson gson = new Gson();
public static String getString(Object obj) {
return gson.toJson(obj);
}
public static <T> T deserializeJson(String json, Class<T> type) {
try {
if (json == null) {
return null;
}
T obj = gson.fromJson(json, type);
return type.cast(obj);
} catch (Exception ex) {
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
return null;
}
}
}

View File

@@ -0,0 +1,35 @@
package me.skymc.taboolib.itemnbtapi.utils;
import me.skymc.taboolib.TabooLib;
public class MethodNames {
public static String getEntityNbtGetterMethodName() {
return "b";
}
public static String getEntityNbtSetterMethodName() {
return "a";
}
public static String getTileDataMethodName() {
if (TabooLib.getVerint() <= 10800) {
return "b";
}
return "save";
}
public static String getTypeMethodName() {
if (TabooLib.getVerint() <= 10800) {
return "b";
}
return "d";
}
public static String getRemoveMethodName() {
if (TabooLib.getVerint() <= 10800) {
return "a";
}
return "remove";
}
}