From 2718e2f763c3575ecb1898aaebeaac4fdd13f79b Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 27 Jan 2016 20:54:54 -0200 Subject: [PATCH] Little fixes, area/volume/width/height/length of schematic should work. --- plugin.yml | 2 +- src/me/TheBukor/SkStuff/SkStuff.java | 57 ++++++++----------- .../SkStuff/effects/EffDrawLineWE.java | 3 +- .../SkStuff/effects/EffMakeCylinder.java | 3 +- .../SkStuff/effects/EffMakePyramid.java | 3 +- .../SkStuff/effects/EffMakeSphere.java | 3 +- .../SkStuff/effects/EffUndoRedoSession.java | 3 +- .../SkStuff/expressions/ExprFileNBT.java | 31 +++------- .../SkStuff/expressions/ExprItemNBT.java | 5 +- .../SkStuff/expressions/ExprNBTOf.java | 29 ++++------ .../expressions/ExprSchematicArea.java | 39 ++++++++++--- src/me/TheBukor/SkStuff/util/NBTUtil.java | 24 ++++---- 12 files changed, 103 insertions(+), 99 deletions(-) diff --git a/plugin.yml b/plugin.yml index 9cc7c29..d33fd55 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ name: SkStuff author: TheBukor description: A Skript addon which adds extra functionalities such as NBT and extended WorldEdit support. -version: 1.4.2 +version: 1.4.2.1 main: me.TheBukor.SkStuff.SkStuff softdepend: [Skript, WorldEdit] \ No newline at end of file diff --git a/src/me/TheBukor/SkStuff/SkStuff.java b/src/me/TheBukor/SkStuff/SkStuff.java index 861f4a3..43e5b3e 100644 --- a/src/me/TheBukor/SkStuff/SkStuff.java +++ b/src/me/TheBukor/SkStuff/SkStuff.java @@ -92,7 +92,7 @@ public class SkStuff extends JavaPlugin { Skript.registerExpression(ExprFileNBT.class, Object.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] from [file] %string%"); Skript.registerExpression(ExprNoClip.class, Boolean.class, ExpressionType.PROPERTY, "no[( |-)]clip (state|mode) of %entity%", "%entity%'s no[( |-)]clip (state|mode)"); Skript.registerExpression(ExprFireProof.class, Boolean.class, ExpressionType.PROPERTY, "fire[ ]proof (state|mode) of %entity%", "%entity%'s fire[ ]proof (state|mode)"); - + Classes.registerClass(new ClassInfo((Class) nbtClass, "compound").user("((nbt)?( ?tag)?) ?compounds?").name("NBT Compound").changer(new Changer() { @Override @@ -106,28 +106,29 @@ public class SkStuff extends JavaPlugin { @Override public void change(Object[] NBT, @Nullable Object[] delta, ChangeMode mode) { - String newTags = (String) delta[0]; - if (mode == ChangeMode.ADD) { - Object NBT1 = null; - try { - NBT1 = nbtParserClass.getMethod("parse", String.class).invoke(NBT1, newTags); - NBTUtil.addCompound(NBT[0], NBT1); - } catch (Exception ex) { - if (ex instanceof InvocationTargetException) { - if (ex.getCause().getClass().getName().contains("MojangsonParseException") ) { + if (NBT[0].getClass().getName().contains("NBTTagCompound")) { + if (!(delta[0] instanceof String)) + return; + String newTags = (String) delta[0]; + if (mode == ChangeMode.ADD) { + Object NBT1 = null; + try { + NBT1 = nbtParserClass.getMethod("parse", String.class).invoke(NBT1, newTags); + } catch (Exception ex) { + if (ex instanceof InvocationTargetException && ex.getCause().getClass().getName().contains("MojangsonParseException")) { Bukkit.getConsoleSender().sendMessage("[SkStuff] " + ChatColor.RED + "Error when parsing NBT - " + ex.getCause().getMessage()); return; } ex.printStackTrace(); } - ex.printStackTrace(); - } - } else if (mode == ChangeMode.REMOVE) { - for (Object s : delta) { - try { - NBT.getClass().getMethod("remove", String.class).invoke(NBT, (String) s); - } catch (Exception ex) { - ex.printStackTrace(); + NBTUtil.addCompound(NBT[0], NBT1); + } else if (mode == ChangeMode.REMOVE) { + for (Object s : delta) { + try { + nbtClass.getMethod("remove", String.class).invoke(NBT[0], (String) s); + } catch (Exception ex) { + ex.printStackTrace(); + } } } } @@ -141,24 +142,14 @@ public class SkStuff extends JavaPlugin { @Override @Nullable - public Object parse(String s, ParseContext context) { - if (s.startsWith("{") && s.endsWith("}")) { + public Object parse(String rawNBT, ParseContext context) { + if (rawNBT.startsWith("{") && rawNBT.contains(":") && rawNBT.endsWith("}")) { Object NBT = null; try { - NBT = nbtClass.newInstance(); - } catch (InstantiationException | IllegalAccessException ex) { - ex.printStackTrace(); - } - try { - Object NBT1 = null; - NBT1 = nbtParserClass.getMethod("parse", String.class).invoke(NBT1, s); - nbtClass.getMethod("a", nbtClass).invoke(NBT, NBT1); + NBT = nbtParserClass.getMethod("parse", String.class).invoke(NBT, rawNBT); } catch (Exception ex) { - if (ex instanceof InvocationTargetException) { - if (ex.getCause().getClass().getName().contains("MojangsonParseException") ) { - return null; - } - ex.printStackTrace(); + if (ex instanceof InvocationTargetException && ex.getCause().getClass().getName().contains("MojangsonParseException")) { + return null; } ex.printStackTrace(); } diff --git a/src/me/TheBukor/SkStuff/effects/EffDrawLineWE.java b/src/me/TheBukor/SkStuff/effects/EffDrawLineWE.java index 9b81681..dd6eba0 100644 --- a/src/me/TheBukor/SkStuff/effects/EffDrawLineWE.java +++ b/src/me/TheBukor/SkStuff/effects/EffDrawLineWE.java @@ -35,7 +35,8 @@ public class EffDrawLineWE extends Effect { editSession = (Expression) expr[2]; blockList = (Expression) expr[3]; thickness = (Expression) expr[4]; - if (result.mark == 1) filled = false; + if (result.mark == 1) + filled = false; return true; } diff --git a/src/me/TheBukor/SkStuff/effects/EffMakeCylinder.java b/src/me/TheBukor/SkStuff/effects/EffMakeCylinder.java index 6a75ee7..56c5140 100644 --- a/src/me/TheBukor/SkStuff/effects/EffMakeCylinder.java +++ b/src/me/TheBukor/SkStuff/effects/EffMakeCylinder.java @@ -38,7 +38,8 @@ public class EffMakeCylinder extends Effect { radius2 = (Expression) expr[3]; editSession = (Expression) expr[4]; blockList = (Expression) expr[5]; - if (result.mark == 1) filled = false; + if (result.mark == 1) + filled = false; return true; } @Override diff --git a/src/me/TheBukor/SkStuff/effects/EffMakePyramid.java b/src/me/TheBukor/SkStuff/effects/EffMakePyramid.java index 822baca..38c5abf 100644 --- a/src/me/TheBukor/SkStuff/effects/EffMakePyramid.java +++ b/src/me/TheBukor/SkStuff/effects/EffMakePyramid.java @@ -34,7 +34,8 @@ public class EffMakePyramid extends Effect { radius = (Expression) expr[1]; editSession = (Expression) expr[2]; blockList = (Expression) expr[3]; - if (result.mark == 1) filled = false; + if (result.mark == 1) + filled = false; return true; } @Override diff --git a/src/me/TheBukor/SkStuff/effects/EffMakeSphere.java b/src/me/TheBukor/SkStuff/effects/EffMakeSphere.java index a1e1698..af66ef6 100644 --- a/src/me/TheBukor/SkStuff/effects/EffMakeSphere.java +++ b/src/me/TheBukor/SkStuff/effects/EffMakeSphere.java @@ -38,7 +38,8 @@ public class EffMakeSphere extends Effect { radius3 = (Expression) expr[3]; editSession = (Expression) expr[4]; blockList = (Expression) expr[5]; - if (result.mark == 1) filled = false; + if (result.mark == 1) + filled = false; return true; } @Override diff --git a/src/me/TheBukor/SkStuff/effects/EffUndoRedoSession.java b/src/me/TheBukor/SkStuff/effects/EffUndoRedoSession.java index 2ceff99..c97e6a3 100644 --- a/src/me/TheBukor/SkStuff/effects/EffUndoRedoSession.java +++ b/src/me/TheBukor/SkStuff/effects/EffUndoRedoSession.java @@ -19,7 +19,8 @@ public class EffUndoRedoSession extends Effect { @Override public boolean init(Expression[] expr, int matchedPattern, Kleenean arg2, ParseResult result) { editSession = (Expression) expr[0]; - if (result.mark == 1) redo = true; + if (result.mark == 1) + redo = true; return true; } diff --git a/src/me/TheBukor/SkStuff/expressions/ExprFileNBT.java b/src/me/TheBukor/SkStuff/expressions/ExprFileNBT.java index e0fb37f..cb3d3dc 100644 --- a/src/me/TheBukor/SkStuff/expressions/ExprFileNBT.java +++ b/src/me/TheBukor/SkStuff/expressions/ExprFileNBT.java @@ -29,8 +29,8 @@ public class ExprFileNBT extends SimpleExpression { private Expression input; private Class nbtClass = ReflectionUtils.getNMSClass("NBTTagCompound"); - private Class nbtCompressedClass = ReflectionUtils.getNMSClass("NBTCompressedStreamTools"); private Class nbtParserClass = ReflectionUtils.getNMSClass("MojangsonParser"); + private Class nbtCompressedClass = ReflectionUtils.getNMSClass("NBTCompressedStreamTools"); @Override public Class getReturnType() { @@ -69,13 +69,6 @@ public class ExprFileNBT extends SimpleExpression { NBT = nbtCompressedClass.getMethod("a", FileInputStream.class).invoke(NBT, fis); fis.close(); } catch (Exception ex) { - if (ex instanceof InvocationTargetException) { - if (ex.getCause().getClass().getName().contains("MojangsonParseException") ) { - Bukkit.getConsoleSender().sendMessage("[SkStuff] " + ChatColor.RED + "Error when parsing NBT - " + ex.getCause().getMessage()); - return null; - } - ex.printStackTrace(); - } ex.printStackTrace(); } finally { try { @@ -104,18 +97,15 @@ public class ExprFileNBT extends SimpleExpression { Object NBT = null; NBT = nbtCompressedClass.getMethod("a", FileInputStream.class).invoke(NBT, fis); Object NBT1 = null; - NBT1 = nbtParserClass.getMethod("parse", nbtClass).invoke(NBT1, tags); + NBT1 = nbtParserClass.getMethod("parse", String.class).invoke(NBT1, tags); NBTUtil.addCompound(NBT, NBT1); nbtCompressedClass.getMethod("a", nbtClass, FileOutputStream.class).invoke(nbtCompressedClass.newInstance(), NBT, os); fis.close(); os.close(); } catch (Exception ex) { - if (ex instanceof InvocationTargetException) { - if (ex.getCause().getClass().getName().contains("MojangsonParseException") ) { - Bukkit.getConsoleSender().sendMessage("[SkStuff] " + ChatColor.RED + "Error when parsing NBT - " + ex.getCause().getMessage()); - return; - } - ex.printStackTrace(); + if (ex instanceof InvocationTargetException && ex.getCause().getClass().getName().contains("MojangsonParseException")) { + Bukkit.getConsoleSender().sendMessage("[SkStuff] " + ChatColor.RED + "Error when parsing NBT - " + ex.getCause().getMessage()); + return; } ex.printStackTrace(); } finally { @@ -137,16 +127,11 @@ public class ExprFileNBT extends SimpleExpression { fis.close(); os.close(); } catch (Exception ex) { - if (ex instanceof InvocationTargetException) { - if (ex.getCause().getClass().getName().contains("MojangsonParseException") ) { - Bukkit.getConsoleSender().sendMessage("[SkStuff] " + ChatColor.RED + "Error when parsing NBT - " + ex.getCause().getMessage()); - return; - } + if (ex instanceof EOFException) { + // No actual error, just end of the file. Ignore it. + } else { ex.printStackTrace(); - } else if (ex instanceof EOFException) { - //No actual error, just end of the file. Ignore it. } - ex.printStackTrace(); } } } diff --git a/src/me/TheBukor/SkStuff/expressions/ExprItemNBT.java b/src/me/TheBukor/SkStuff/expressions/ExprItemNBT.java index e813112..583ab1e 100644 --- a/src/me/TheBukor/SkStuff/expressions/ExprItemNBT.java +++ b/src/me/TheBukor/SkStuff/expressions/ExprItemNBT.java @@ -1,5 +1,7 @@ package me.TheBukor.SkStuff.expressions; +import java.lang.reflect.InvocationTargetException; + import javax.annotation.Nullable; import org.bukkit.Bukkit; @@ -69,10 +71,11 @@ public class ExprItemNBT extends SimpleExpression { } nmsItem.getClass().getMethod("setTag", nbtClass).invoke(nmsItem, NBT); } catch (Exception ex) { - if (ex.getCause().getClass().getName().contains("MojangsonParseException") ) { + if (ex instanceof InvocationTargetException && ex.getCause().getClass().getName().contains("MojangsonParseException")) { Bukkit.getConsoleSender().sendMessage("[SkStuff] " + ChatColor.RED + "Error when parsing NBT - " + ex.getCause().getMessage()); return null; } + ex.printStackTrace(); } Object newItem = null; try { diff --git a/src/me/TheBukor/SkStuff/expressions/ExprNBTOf.java b/src/me/TheBukor/SkStuff/expressions/ExprNBTOf.java index cf34bdc..4c026f9 100644 --- a/src/me/TheBukor/SkStuff/expressions/ExprNBTOf.java +++ b/src/me/TheBukor/SkStuff/expressions/ExprNBTOf.java @@ -115,6 +115,8 @@ public class ExprNBTOf extends SimpleExpression { @Override public void change(Event e, Object[] delta, ChangeMode mode) { Object tar = target.getSingle(e); + if (!(delta[0] instanceof String)) + return; if (tar instanceof Entity) { Object NBT = null; Object nmsEnt = null; @@ -138,12 +140,9 @@ public class ExprNBTOf extends SimpleExpression { NBTUtil.addCompound(NBT, NBT1); nmsEnt.getClass().getMethod("f", nbtClass).invoke(nmsEnt, NBT); } catch (Exception ex) { - if (ex instanceof InvocationTargetException) { - if (ex.getCause().getClass().getName().contains("MojangsonParseException")) { - Bukkit.getConsoleSender().sendMessage("[SkStuff] " + ChatColor.RED + "Error when parsing NBT - " + ex.getCause().getMessage()); - return; - } - ex.printStackTrace(); + if (ex instanceof InvocationTargetException && ex.getCause().getClass().getName().contains("MojangsonParseException")) { + Bukkit.getConsoleSender().sendMessage("[SkStuff] " + ChatColor.RED + "Error when parsing NBT - " + ex.getCause().getMessage()); + return; } ex.printStackTrace(); } @@ -193,12 +192,9 @@ public class ExprNBTOf extends SimpleExpression { tileEntity.getClass().getMethod("update").invoke(tileEntity); nmsWorld.getClass().getMethod("notify", nmsPosClass).invoke(nmsWorld, tileEntity.getClass().getMethod("getPosition").invoke(tileEntity)); } catch (Exception ex) { - if (ex instanceof InvocationTargetException) { - if (ex.getCause().getClass().getName().contains("MojangsonParseException")) { - Bukkit.getConsoleSender().sendMessage("[SkStuff] " + ChatColor.RED + "Error when parsing NBT - " + ex.getCause().getMessage()); - return; - } - ex.printStackTrace(); + if (ex instanceof InvocationTargetException && ex.getCause().getClass().getName().contains("MojangsonParseException")) { + Bukkit.getConsoleSender().sendMessage("[SkStuff] " + ChatColor.RED + "Error when parsing NBT - " + ex.getCause().getMessage()); + return; } ex.printStackTrace(); } @@ -256,12 +252,9 @@ public class ExprNBTOf extends SimpleExpression { } ((Slot) slot[0]).setItem((ItemStack) newItem); } catch (Exception ex) { - if (ex instanceof InvocationTargetException) { - if (ex.getCause().getClass().getName().contains("MojangsonParseException") ) { - Bukkit.getConsoleSender().sendMessage("[SkStuff] " + ChatColor.RED + "Error when parsing NBT - " + ex.getCause().getMessage()); - return; - } - ex.printStackTrace(); + if (ex instanceof InvocationTargetException && ex.getCause().getClass().getName().contains("MojangsonParseException")) { + Bukkit.getConsoleSender().sendMessage("[SkStuff] " + ChatColor.RED + "Error when parsing NBT - " + ex.getCause().getMessage()); + return; } ex.printStackTrace(); } diff --git a/src/me/TheBukor/SkStuff/expressions/ExprSchematicArea.java b/src/me/TheBukor/SkStuff/expressions/ExprSchematicArea.java index 7acf229..dcace01 100644 --- a/src/me/TheBukor/SkStuff/expressions/ExprSchematicArea.java +++ b/src/me/TheBukor/SkStuff/expressions/ExprSchematicArea.java @@ -5,7 +5,6 @@ import java.io.IOException; import javax.annotation.Nullable; -import org.bukkit.Bukkit; import org.bukkit.event.Event; import com.sk89q.worldedit.Vector; @@ -19,6 +18,9 @@ import ch.njol.util.Kleenean; public class ExprSchematicArea extends SimpleExpression { private Expression schematic; + + private int parseMark; + private String toStringMark; @Override public Class getReturnType() { @@ -32,14 +34,26 @@ public class ExprSchematicArea extends SimpleExpression { @SuppressWarnings("unchecked") @Override - public boolean init(Expression[] expr, int matchedPattern, Kleenean arg2, ParseResult arg3) { + public boolean init(Expression[] expr, int matchedPattern, Kleenean arg2, ParseResult result) { schematic = (Expression) expr[0]; + parseMark = result.mark; + if (parseMark == 0) { + toStringMark = "volume"; + } else if (parseMark == 1) { + toStringMark = "width (x-size)"; + } else if (parseMark == 2) { + toStringMark = "height (y-size)"; + } else if (parseMark == 3) { + toStringMark = "length (z-size)"; + } else if (parseMark == 4) { + toStringMark = "area"; + } return true; } @Override public String toString(@Nullable Event e, boolean arg1) { - return "the volume of the schematic from " + schematic.toString(e, false); + return "the " + toStringMark + " of the schematic from " + schematic.toString(e, false); } @SuppressWarnings("deprecation") @@ -48,13 +62,24 @@ public class ExprSchematicArea extends SimpleExpression { protected Integer[] get(Event e) { String schem = schematic.getSingle(e); File schemFile = new File((schem.endsWith(".schematic") ? schem : (schem + ".schematic"))); - Vector v = null; + Vector size = null; try { - v = MCEditSchematicFormat.getFormat(schemFile).load(schemFile).getSize(); + size = MCEditSchematicFormat.getFormat(schemFile).load(schemFile).getSize(); } catch (DataException | IOException ex) { return null; } - Bukkit.broadcastMessage("vector: " + v.getX() + v.getY() + v.getZ()); - return new Integer[] { 1 }; + Number result = null; + if (parseMark == 0) { + result = (size.getX() * size.getY() * size.getZ()); + } else if (parseMark == 1) { + result = size.getX(); + } else if (parseMark == 2) { + result = size.getY(); + } else if (parseMark == 3) { + result = size.getZ(); + } else if (parseMark == 4) { + result = (size.getX() * size.getZ()); + } + return new Integer[] { result.intValue() }; } } \ No newline at end of file diff --git a/src/me/TheBukor/SkStuff/util/NBTUtil.java b/src/me/TheBukor/SkStuff/util/NBTUtil.java index 8bec5dc..74080a9 100644 --- a/src/me/TheBukor/SkStuff/util/NBTUtil.java +++ b/src/me/TheBukor/SkStuff/util/NBTUtil.java @@ -9,15 +9,17 @@ public class NBTUtil { private static Class nbtBaseClass = ReflectionUtils.getNMSClass("NBTBase"); private static Class nbtClass = ReflectionUtils.getNMSClass("NBTTagCompound"); - /** - * This is actually a copy of the "a(NBTTagCompound)" method in the NBTTagCompound class using reflection + /** + * This is actually a copy of the "a(NBTTagCompound)" method in the NBTTagCompound class. * I needed to add this because the 1.7 and before versions of the NBTTagCompound class didn't have this method, * so there wasn't actually a reliable way to multiple tags at once into a compound. * For the original code for the method, check https://github.com/linouxis9/mc-dev-1.8.7/blob/master/net/minecraft/server/NBTTagCompound.java#L348 + * + * Please note that I adapted it to work using reflection. */ @SuppressWarnings("unchecked") - public static void addCompound(Object source, Object toAdd) { - if (source.getClass().getName().contains("NBTTagCompound") && toAdd.getClass().getName().contains("NBTTagCompound")) { + public static void addCompound(Object NBT, Object toAdd) { + if (NBT.getClass().getName().contains("NBTTagCompound") && toAdd.getClass().getName().contains("NBTTagCompound")) { try { Field map = nbtClass.getDeclaredField("map"); map.setAccessible(true); @@ -28,15 +30,15 @@ public class NBTUtil { String string = (String) iterator.next(); Object base = nbtBaseClass.cast((((HashMap) map.get(toAdd)).get(string))); if((byte) nbtBaseClass.getMethod("getTypeId").invoke(base) == 10) { - if((boolean) nbtClass.getMethod("hasKeyOfType", String.class, int.class).invoke(source, string, 10)) { - Object NBT = null; - NBT = nbtClass.getMethod("getCompound", String.class).invoke(NBT, string); - NBTUtil.addCompound(NBT, base.getClass().cast(nbtClass)); + if((boolean) nbtClass.getMethod("hasKeyOfType", String.class, int.class).invoke(NBT, string, 10)) { + Object localNBT = null; + localNBT = nbtClass.getMethod("getCompound", String.class).invoke(localNBT, string); + NBTUtil.addCompound(localNBT.toString(), base.getClass().cast(nbtClass)); } else { - nbtClass.getMethod("set", String.class, nbtBaseClass).invoke(source, string, base.getClass().getMethod("clone").invoke(base)); + nbtClass.getMethod("set", String.class, nbtBaseClass).invoke(NBT, string, base.getClass().getMethod("clone").invoke(base)); } } else { - nbtClass.getMethod("set", String.class, nbtBaseClass).invoke(source, string, base.getClass().getMethod("clone").invoke(base)); + nbtClass.getMethod("set", String.class, nbtBaseClass).invoke(NBT, string, base.getClass().getMethod("clone").invoke(base)); } } map.setAccessible(false); @@ -45,4 +47,4 @@ public class NBTUtil { } } } -} +} \ No newline at end of file