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 NBTCompound getCompound(String name) { NBTCompound next = new NBTCompound(this, name); if (NBTReflectionUtil.valideCompound(next)) return next; return null; }
public NBTCompound addCompound(String name) { if(getType(name) == NBTType.NBTTagCompound)return getCompound(name); NBTReflectionUtil.addNBTTagCompound(this, name); return getCompound(name); }
nbtItem.addCompound(COMP_TEST_KEY); NBTCompound comp = nbtItem.getCompound(COMP_TEST_KEY); comp.setString(STRING_TEST_KEY, STRING_TEST_VALUE + "2"); comp.setInteger(INT_TEST_KEY, INT_TEST_VALUE * 2); comp.setDouble(DOUBLE_TEST_KEY, DOUBLE_TEST_VALUE * 2); NBTList list = comp.getList("testlist", NBTType.NBTTagString); if (MinecraftVersion.getVersion() == MinecraftVersion.MC1_7_R4) { getLogger().warning( NBTList taglist = comp.getList("complist", NBTType.NBTTagCompound); NBTListCompound lcomp = taglist.addCompound(); lcomp.setDouble("double1", 0.3333); if (!comp.hasKey(STRING_TEST_KEY)) { getLogger().warning("Wasn't able to check a compound key! The Item-NBT-API may not work!"); compatible = false; if (!(STRING_TEST_VALUE + "2").equals(comp.getString(STRING_TEST_KEY)) || comp.getInteger(INT_TEST_KEY) != INT_TEST_VALUE * 2 || comp.getDouble(DOUBLE_TEST_KEY) != DOUBLE_TEST_VALUE * 2 || comp.getBoolean(BOOLEAN_TEST_KEY) == BOOLEAN_TEST_VALUE) { getLogger() .warning("One key does not equal the original compound value! The Item-NBT-API may not work!"); list = comp.getList("testlist", NBTType.NBTTagString); if (comp.getType("testlist") != NBTType.NBTTagList) { getLogger().warning("Wasn't able to get the correct Tag type! The Item-NBT-API may not work!"); compatible = false;
public static void addOtherNBTCompound(NBTCompound comp, NBTCompound nbtcompound) { Object rootnbttag = comp.getCompound(); if (rootnbttag == null) { rootnbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); } if (!valideCompound(comp)) return; Object workingtag = gettoCompount(rootnbttag, comp); try { ReflectionMethod.COMPOUND_ADD.run(workingtag, nbtcompound.getCompound()); comp.setCompound(rootnbttag); } catch (Exception ex) { ex.printStackTrace(); } }
protected Object getCompound() { return parent.getCompound(); }
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 remove(NBTCompound comp, String key) { Object rootnbttag = comp.getCompound(); if (rootnbttag == null) { rootnbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); } if (!valideCompound(comp)) return; Object workingtag = gettoCompount(rootnbttag, comp); ReflectionMethod.COMPOUND_REMOVE_KEY.run(workingtag, key); comp.setCompound(rootnbttag); }
public String asNBTString(){ Object comp = NBTReflectionUtil.gettoCompount(getCompound(), this); if(comp == null)return "{}"; return comp.toString(); }
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 = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); } if (!valideCompound(comp)) { new Throwable("InvalideCompound").printStackTrace(); return; } Object workingtag = gettoCompount(rootnbttag, comp); Method method; try { method = workingtag.getClass().getMethod("set", String.class, ClassWrapper.NMS_NBTBASE.getClazz()); method.invoke(workingtag, key, val); comp.setCompound(rootnbttag); } catch (Exception ex) { ex.printStackTrace(); } }
public static Boolean valideCompound(NBTCompound comp) { Object root = comp.getCompound(); if (root == null) { root = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); } return (gettoCompount(root, comp)) != null; }
public static void setData(NBTCompound comp, ReflectionMethod type, String key, Object data) { if (data == null) { remove(comp, key); return; } Object rootnbttag = comp.getCompound(); if (rootnbttag == null) { rootnbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); } if (!valideCompound(comp)) return; Object workingtag = gettoCompount(rootnbttag, comp); type.run(workingtag, key, data); comp.setCompound(rootnbttag); }
public static String getContent(NBTCompound comp, String key) { Object rootnbttag = comp.getCompound(); if (rootnbttag == null) { rootnbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); } 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) { ex.printStackTrace(); } 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 = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); } if (!valideCompound(comp)) return; Object workingtag = gettoCompount(nbttag, comp); Method method; try { method = workingtag.getClass().getMethod("set", String.class, ClassWrapper.NMS_NBTBASE.getClazz()); method.invoke(workingtag, name, ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz().newInstance()); comp.setCompound(nbttag); return; } catch (Exception ex) { ex.printStackTrace(); } return; }
@SuppressWarnings({"unchecked"}) public static Object convertNBTCompoundtoNMSItem(NBTCompound nbtcompound) { @SuppressWarnings("rawtypes") Class clazz = ClassWrapper.NMS_ITEMSTACK.getClazz(); try { if(MinecraftVersion.getVersion().getVersionId() >= MinecraftVersion.MC1_12_R1.getVersionId()){ Constructor<?> constructor = clazz.getConstructor(ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()); constructor.setAccessible(true); return constructor.newInstance(nbtcompound.getCompound()); }else{ Method method = clazz.getMethod("createStack", ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()); method.setAccessible(true); return method.invoke(null, nbtcompound.getCompound()); } } catch (Exception e) { e.printStackTrace(); } return null; }
public static NBTList getList(NBTCompound comp, String key, NBTType type) { Object rootnbttag = comp.getCompound(); if (rootnbttag == null) { rootnbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); } 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) { ex.printStackTrace(); } return null; }
public static Object getData(NBTCompound comp, ReflectionMethod type, String key) { Object rootnbttag = comp.getCompound(); if (rootnbttag == null) { return null; } if (!valideCompound(comp)) return null; Object workingtag = gettoCompount(rootnbttag, comp); return type.run(workingtag, key); }
@SuppressWarnings("unchecked") public static Set<String> getKeys(NBTCompound comp) { Object rootnbttag = comp.getCompound(); if (rootnbttag == null) { rootnbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(); } if (!valideCompound(comp)) return null; Object workingtag = gettoCompount(rootnbttag, comp); return (Set<String>) ReflectionMethod.COMPOUND_GET_KEYS.run(workingtag); }