From 5a9a297d005ccd9b12dca7684f404ec28679eead Mon Sep 17 00:00:00 2001 From: Nathan Curran <30569566+Focusvity@users.noreply.github.com> Date: Sat, 25 Apr 2020 15:33:42 +1000 Subject: [PATCH] v0.9.1 --- pom.xml | 2 +- .../me/dpohvar/powernbt/api/NBTManager.java | 1 - .../powernbt/completer/CompleterNBT.java | 24 +- .../powernbt/completer/TypeCompleter.java | 1 - .../powernbt/listener/SelectListener.java | 1 - .../java/me/dpohvar/powernbt/nbt/NBTBase.java | 5 + .../dpohvar/powernbt/nbt/NBTTagLongArray.java | 233 ++++++++++++++++++ .../java/me/dpohvar/powernbt/nbt/NBTType.java | 45 +++- 8 files changed, 301 insertions(+), 11 deletions(-) create mode 100644 src/main/java/me/dpohvar/powernbt/nbt/NBTTagLongArray.java diff --git a/pom.xml b/pom.xml index 9bb8784..6f0cc9b 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,7 @@ me.dpohvar.powernbt PowerNBT - 0.9 + 0.9.1 jar Powerful NBT editor for CraftBukkit 1.5 and later diff --git a/src/main/java/me/dpohvar/powernbt/api/NBTManager.java b/src/main/java/me/dpohvar/powernbt/api/NBTManager.java index 96108a7..5b12ab4 100644 --- a/src/main/java/me/dpohvar/powernbt/api/NBTManager.java +++ b/src/main/java/me/dpohvar/powernbt/api/NBTManager.java @@ -656,5 +656,4 @@ public ItemStack createCraftItemStack(NBTCompound compound) if (compound == null) return new ItemStack(Material.AIR); return itemStackUtils.createCraftItemStackFromNBT(compound.getHandle()); } - } diff --git a/src/main/java/me/dpohvar/powernbt/completer/CompleterNBT.java b/src/main/java/me/dpohvar/powernbt/completer/CompleterNBT.java index 16742a6..4c703cd 100644 --- a/src/main/java/me/dpohvar/powernbt/completer/CompleterNBT.java +++ b/src/main/java/me/dpohvar/powernbt/completer/CompleterNBT.java @@ -35,7 +35,7 @@ public void fillTabs(Caller caller, TabFormer former) throws Exception String word = former.poll(); // object if (word.isEmpty()) { - former.addIfStarts("buffer", "list", "compound", "byte[]", "int[]", "debug", "file:", "gz:", "sch:"); + former.addIfStarts("buffer", "list", "compound", "byte[]", "int[]", "long[]", "debug", "file:", "gz:", "sch:"); if (caller.getOwner() instanceof Entity) former.addIfStarts("block", "inventory", "hand", "hand:"); if (caller.getOwner() instanceof Entity && former.getQuery().startsWith("id")) { @@ -145,7 +145,7 @@ public void fillTabs(Caller caller, TabFormer former) throws Exception former.addIfStarts("float", "double"); } else if (val1.matches("\\[((-?[0-9]+|#-?[0-9a-fA-F]+)(,(?!\\])|(?=\\])))*\\]")) { - former.addIfStarts("byte[]", "int[]"); + former.addIfStarts("byte[]", "int[]", "long[]"); } } else { @@ -240,6 +240,7 @@ public void fillTabs(Caller caller, TabFormer former) throws Exception case DOUBLE: case BYTEARRAY: case INTARRAY: + case LONGARRAY: String s = NBTViewer.getShortValue(base, false); former.add(s); return; @@ -271,6 +272,7 @@ public void fillTabs(Caller caller, TabFormer former) throws Exception case DOUBLE: case BYTEARRAY: case INTARRAY: + case LONGARRAY: String s = NBTViewer.getShortValue(b, false); former.add(s); return; @@ -288,7 +290,7 @@ public void fillTabs(Caller caller, TabFormer former) throws Exception } } } - former.addIfStarts("me", "item", "buffer", "list", "compound", "byte[]", "int[]"); + former.addIfStarts("me", "item", "buffer", "list", "compound", "byte[]", "int[]", "long[]"); if (caller.getOwner() instanceof Entity) former.addIfStarts("block", "inventory", "hand", "hand:"); if (caller.getOwner() instanceof Entity && former.getQuery().startsWith("id")) { @@ -394,7 +396,7 @@ public void fillTabs(Caller caller, TabFormer former) throws Exception former.addIfStarts("float", "double"); } else if (val2.matches("\\[((-?[0-9]+|#-?[0-9a-fA-F]+)(,(?!\\])|(?=\\])))*\\]")) { - former.addIfStarts("byte[]", "int[]"); + former.addIfStarts("byte[]", "int[]", "long[]"); } } else { @@ -447,6 +449,12 @@ private void completeTag(NBTContainer container, TabFormer former) throws Exc { former.addIfStarts("[" + i + "]"); } + } else if (base instanceof NBTTagLongArray) + { + for (int i = 0; i < ((NBTTagLongArray) base).size(); i++) + { + former.addIfStarts("[" + i + "]"); + } } } for (String type : container.getTypes()) @@ -503,6 +511,13 @@ private void completeTag(NBTContainer container, TabFormer former) throws Exc String s = "[" + i + "]"; if (s.toUpperCase().startsWith(qu.toUpperCase())) former.add(option + s); } + } else if (base instanceof NBTTagLongArray) + { + for (int i = 0; i < ((NBTTagLongArray) base).size(); i++) + { + String s = "[" + i + "]"; + if (s.toUpperCase().startsWith(qu.toUpperCase())) former.add(option + s); + } } } for (String type : container.getTypes()) @@ -519,7 +534,6 @@ private void completeTag(NBTContainer container, TabFormer former) throws Exc } } } - } diff --git a/src/main/java/me/dpohvar/powernbt/completer/TypeCompleter.java b/src/main/java/me/dpohvar/powernbt/completer/TypeCompleter.java index a9f8d05..4c9b152 100644 --- a/src/main/java/me/dpohvar/powernbt/completer/TypeCompleter.java +++ b/src/main/java/me/dpohvar/powernbt/completer/TypeCompleter.java @@ -120,5 +120,4 @@ private Object getObjectByQueue(String key, Queue queue) return r; } } - } diff --git a/src/main/java/me/dpohvar/powernbt/listener/SelectListener.java b/src/main/java/me/dpohvar/powernbt/listener/SelectListener.java index 0cccc0e..3abcd05 100644 --- a/src/main/java/me/dpohvar/powernbt/listener/SelectListener.java +++ b/src/main/java/me/dpohvar/powernbt/listener/SelectListener.java @@ -101,5 +101,4 @@ public void inventory(InventoryClickEvent event) caller.handleException(t); } } - } diff --git a/src/main/java/me/dpohvar/powernbt/nbt/NBTBase.java b/src/main/java/me/dpohvar/powernbt/nbt/NBTBase.java index 8e309c9..487587c 100644 --- a/src/main/java/me/dpohvar/powernbt/nbt/NBTBase.java +++ b/src/main/java/me/dpohvar/powernbt/nbt/NBTBase.java @@ -54,6 +54,8 @@ public static NBTBase wrap(Object handle) return new NBTTagCompound(true, handle); case 11: return new NBTTagIntArray(true, handle); + case 12: + return new NBTTagLongArray(true, handle); default: return null; } @@ -85,6 +87,8 @@ public static NBTBase getDefault(byte type) return new NBTTagCompound(); case 11: return new NBTTagIntArray(); + case 12: + return new NBTTagLongArray(); default: return null; } @@ -109,6 +113,7 @@ public static NBTBase getByValue(Object o) if (o instanceof byte[]) return new NBTTagByteArray((byte[]) o); if (o instanceof CharSequence) return new NBTTagString(o.toString()); if (o instanceof int[]) return new NBTTagIntArray((int[]) o); + if (o instanceof long[]) return new NBTTagLongArray((long[]) o); if (o instanceof Map) { NBTTagCompound tag = new NBTTagCompound(); diff --git a/src/main/java/me/dpohvar/powernbt/nbt/NBTTagLongArray.java b/src/main/java/me/dpohvar/powernbt/nbt/NBTTagLongArray.java new file mode 100644 index 0000000..764a0be --- /dev/null +++ b/src/main/java/me/dpohvar/powernbt/nbt/NBTTagLongArray.java @@ -0,0 +1,233 @@ +package me.dpohvar.powernbt.nbt; + +import java.util.*; + +import static me.dpohvar.powernbt.utils.NBTUtils.nbtUtils; + +public class NBTTagLongArray extends NBTTagNumericArray +{ + + public static final byte typeId = 12; + + public NBTTagLongArray() + { + super(new long[0]); + } + + public NBTTagLongArray(String ignored) + { + super(new long[0]); + } + + public NBTTagLongArray(long[] l) + { + super(nbtUtils.createTag(l, typeId)); + } + + public NBTTagLongArray(String ignored, long[] l) + { + this(l); + } + + public NBTTagLongArray(boolean ignored, Object tag) + { + super(tag); + if (nbtUtils.getTagType(handle) != typeId) + { + throw new IllegalArgumentException(); + } + } + + public static ArrayList longArrToList(long[] ln) + { + ArrayList temp = new ArrayList(ln.length); + for (long anLn : ln) + { + temp.add(anLn); + } + return temp; + } + + public static long[] listToLongArr(Collection ln) + { + long[] temp = new long[ln.size()]; + int i = 0; + for (Long anLn : ln) + { + temp[i++] = anLn; + } + return temp; + } + + public boolean equals(Object o) + { + if (o instanceof NBTBase) + { + ((NBTBase) o).getHandle(); + } + return handle.equals(o); + } + + public int hashCode() + { + return handle.hashCode(); + } + + public long[] get() + { + return (long[]) super.get(); + } + + public void set(long[] value) + { + super.set(value); + } + + public int size() + { + return get().length; + } + + @Override + public boolean contains(Object o) + { + return longArrToList(get()).contains(o); + } + + public Long get(int i) + { + long[] array = get(); + if (i >= array.length) return null; + return array[i]; + } + + public Long set(int i, Number value) + { + Long res = get(i); + long[] array = get(); + List list = new LinkedList(); + for (long b : array) list.add(b); + while (list.size() <= i) + { + list.add(0L); + } + list.set(i, value.longValue()); + long[] result = new long[list.size()]; + int t = 0; + for (long b : list) result[t++] = b; + set(result); + return res; + } + + public Long remove(int i) + { + Long res = get(i); + long[] array = get(); + if (i < 0 || i >= array.length) return res; + List list = new LinkedList(); + for (long b : array) list.add(b); + while (list.size() <= i) + { + list.add((long) 0); + } + list.remove(i); + long[] result = new long[list.size()]; + int t = 0; + for (long b : list) result[t++] = b; + set(result); + return res; + } + + @Override + public int indexOf(Object o) + { + return longArrToList(get()).indexOf(o); + } + + @Override + public int lastIndexOf(Object o) + { + return longArrToList(get()).lastIndexOf(o); + } + + @Override + public List subList(int fromIndex, int toIndex) + { + long[] r = new long[toIndex - fromIndex]; + int t = 0; + long[] s = get(); + for (int i = fromIndex; i < toIndex; i++) + { + r[t++] = s[i]; + } + return new NBTTagLongArray(r); + } + + @Override + public boolean add(Number value) + { + long[] array = get(); + List list = new LinkedList(); + for (long b : array) list.add(b); + list.add(value.longValue()); + long[] result = new long[list.size()]; + int t = 0; + for (long b : list) result[t++] = b; + set(result); + return false; + } + + @Override + public boolean remove(Object o) + { + List longs = longArrToList(get()); + boolean result = longs.remove(o); + if (result) + { + set(listToLongArr(longs)); + } + return result; + } + + @Override + public boolean removeAll(Collection c) + { + List longs = longArrToList(get()); + boolean result = longs.removeAll(c); + if (result) + { + set(listToLongArr(longs)); + } + return result; + } + + @Override + public boolean retainAll(Collection c) + { + List longs = longArrToList(get()); + boolean result = longs.retainAll(c); + if (result) + { + set(listToLongArr(longs)); + } + return result; + } + + @Override + public void clear() + { + set(new long[0]); + } + + @Override + public String toString() + { + return Arrays.toString(get()); + } + + @Override + public byte getTypeId() + { + return 12; + } +} diff --git a/src/main/java/me/dpohvar/powernbt/nbt/NBTType.java b/src/main/java/me/dpohvar/powernbt/nbt/NBTType.java index 596802c..333e6e4 100644 --- a/src/main/java/me/dpohvar/powernbt/nbt/NBTType.java +++ b/src/main/java/me/dpohvar/powernbt/nbt/NBTType.java @@ -19,6 +19,7 @@ public enum NBTType LIST((byte) 9, "list", "\u2630", DARK_GRAY), // ☰ COMPOUND((byte) 10, "compound", "\u27B2", GRAY), // ➲ INTARRAY((byte) 11, "int[] ", ChatColor.BOLD + "\u24D8", DARK_BLUE), // ⓘ + LONGARRAY((byte) 12, "long[]", ChatColor.BOLD + "\u24DB", DARK_GREEN), // ⓛ ; public final String name; public final String prefix; @@ -233,7 +234,7 @@ public NBTBase parse(String s) } if (t == null) try { - t = (byte) (long) Long.parseLong(x); + t = (byte) Long.parseLong(x); } catch (Throwable ignored) { @@ -273,7 +274,7 @@ public NBTBase parse(String s) } if (t == null) try { - t = (int) (long) Long.parseLong(x); + t = (int) Long.parseLong(x); } catch (Throwable ignored) { @@ -283,6 +284,46 @@ public NBTBase parse(String s) } return new NBTTagIntArray(v); } + case LONGARRAY: + { + if (!s.matches("\\[((-?[0-9]+|#-?[0-9a-fA-F]+)(,(?!\\])|(?=\\])))*\\]")) + { + throw new RuntimeException(plugin.translate("error_parse", s, LONGARRAY.name)); + } + String sp = s.substring(1, s.length() - 1); + if (sp.isEmpty()) return new NBTTagLongArray(); + String[] ss = sp.split(","); + long[] v = new long[ss.length]; + for (int i = 0; i < v.length; i++) + { + Long t = null; + String x = ss[i]; + if (x.startsWith("#")) try + { + t = Long.parseLong(x.substring(1), 16); + } + catch (Throwable ignored) + { + } + try + { + t = Long.parseLong(x); + } + catch (Throwable ignored) + { + } + if (t == null) try + { + t = Long.parseLong(x); + } + catch (Throwable ignored) + { + } + if (t == null) throw new RuntimeException(plugin.translate("error_parse", x, LONG.name)); + v[i] = t; + } + return new NBTTagLongArray(v); + } default: { throw new RuntimeException(plugin.translate("error_parse", s, this.name));