Little fixes, area/volume/width/height/length of schematic should work.

This commit is contained in:
Richard 2016-01-27 20:54:54 -02:00
parent 5665f39cb5
commit 2718e2f763
12 changed files with 103 additions and 99 deletions

View File

@ -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]

View File

@ -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();
}

View File

@ -35,7 +35,8 @@ public class EffDrawLineWE extends Effect {
editSession = (Expression<EditSession>) expr[2];
blockList = (Expression<ItemStack>) expr[3];
thickness = (Expression<Double>) expr[4];
if (result.mark == 1) filled = false;
if (result.mark == 1)
filled = false;
return true;
}

View File

@ -38,7 +38,8 @@ public class EffMakeCylinder extends Effect {
radius2 = (Expression<Double>) expr[3];
editSession = (Expression<EditSession>) expr[4];
blockList = (Expression<ItemStack>) expr[5];
if (result.mark == 1) filled = false;
if (result.mark == 1)
filled = false;
return true;
}
@Override

View File

@ -34,7 +34,8 @@ public class EffMakePyramid extends Effect {
radius = (Expression<Integer>) expr[1];
editSession = (Expression<EditSession>) expr[2];
blockList = (Expression<ItemStack>) expr[3];
if (result.mark == 1) filled = false;
if (result.mark == 1)
filled = false;
return true;
}
@Override

View File

@ -38,7 +38,8 @@ public class EffMakeSphere extends Effect {
radius3 = (Expression<Double>) expr[3];
editSession = (Expression<EditSession>) expr[4];
blockList = (Expression<ItemStack>) expr[5];
if (result.mark == 1) filled = false;
if (result.mark == 1)
filled = false;
return true;
}
@Override

View File

@ -19,7 +19,8 @@ public class EffUndoRedoSession extends Effect {
@Override
public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean arg2, ParseResult result) {
editSession = (Expression<EditSession>) expr[0];
if (result.mark == 1) redo = true;
if (result.mark == 1)
redo = true;
return true;
}

View File

@ -29,8 +29,8 @@ public class ExprFileNBT extends SimpleExpression<Object> {
private Expression<String> 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<? extends Object> getReturnType() {
@ -69,13 +69,6 @@ public class ExprFileNBT extends SimpleExpression<Object> {
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> {
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<Object> {
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();
}
}
}

View File

@ -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<ItemStack> {
}
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 {

View File

@ -115,6 +115,8 @@ public class ExprNBTOf extends SimpleExpression<Object> {
@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<Object> {
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<Object> {
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<Object> {
}
((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();
}

View File

@ -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;
@ -20,6 +19,9 @@ import ch.njol.util.Kleenean;
public class ExprSchematicArea extends SimpleExpression<Integer> {
private Expression<String> schematic;
private int parseMark;
private String toStringMark;
@Override
public Class<? extends Integer> getReturnType() {
return Integer.class;
@ -32,14 +34,26 @@ public class ExprSchematicArea extends SimpleExpression<Integer> {
@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<String>) 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<Integer> {
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() };
}
}

View File

@ -10,14 +10,16 @@ public class NBTUtil {
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<String, Object>) 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);