Highly experimental "NBT of file" expression.
I still need to do a lot more testing, my testserver's level.dat got to 0KB when using the "add blah to nbt of file" :o
This commit is contained in:
parent
34b05e4e2d
commit
91fdd40d19
1
bin/.gitignore
vendored
1
bin/.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
/me/
|
/me/
|
||||||
|
/org/
|
||||||
|
@ -21,6 +21,9 @@ import me.TheBukor.effects.EffUndoRedoSession;
|
|||||||
import me.TheBukor.expressions.ExprAreaOfSelection;
|
import me.TheBukor.expressions.ExprAreaOfSelection;
|
||||||
import me.TheBukor.expressions.ExprChangedBlocksSession;
|
import me.TheBukor.expressions.ExprChangedBlocksSession;
|
||||||
import me.TheBukor.expressions.ExprEditSessionLimit;
|
import me.TheBukor.expressions.ExprEditSessionLimit;
|
||||||
|
import me.TheBukor.expressions.ExprFileNBTv1_8_R1;
|
||||||
|
import me.TheBukor.expressions.ExprFileNBTv1_8_R2;
|
||||||
|
import me.TheBukor.expressions.ExprFileNBTv1_8_R3;
|
||||||
import me.TheBukor.expressions.ExprHeightOfSchematic;
|
import me.TheBukor.expressions.ExprHeightOfSchematic;
|
||||||
import me.TheBukor.expressions.ExprHeightOfSelection;
|
import me.TheBukor.expressions.ExprHeightOfSelection;
|
||||||
import me.TheBukor.expressions.ExprItemNBTv1_8_R1;
|
import me.TheBukor.expressions.ExprItemNBTv1_8_R1;
|
||||||
@ -62,6 +65,7 @@ public class SkStuff extends JavaPlugin {
|
|||||||
Skript.registerExpression(ExprNBTv1_8_R1.class, NBTTagCompound.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] of %entity/block/itemstack%", "%entity/block/itemstack%'s nbt[[ ]tag[s]]");
|
Skript.registerExpression(ExprNBTv1_8_R1.class, NBTTagCompound.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] of %entity/block/itemstack%", "%entity/block/itemstack%'s nbt[[ ]tag[s]]");
|
||||||
Skript.registerExpression(ExprItemNBTv1_8_R1.class, ItemStack.class, ExpressionType.SIMPLE, "%itemstack% with [custom] nbt[[ ]tag[s]] %string%");
|
Skript.registerExpression(ExprItemNBTv1_8_R1.class, ItemStack.class, ExpressionType.SIMPLE, "%itemstack% with [custom] nbt[[ ]tag[s]] %string%");
|
||||||
Skript.registerExpression(ExprTagOfv1_8_R1.class, Object.class, ExpressionType.SIMPLE, "[nbt[ ]]tag %string% of [nbt [compound]] %compound%");
|
Skript.registerExpression(ExprTagOfv1_8_R1.class, Object.class, ExpressionType.SIMPLE, "[nbt[ ]]tag %string% of [nbt [compound]] %compound%");
|
||||||
|
Skript.registerExpression(ExprFileNBTv1_8_R1.class, NBTTagCompound.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] of [file] %string%");
|
||||||
Classes.registerClass(new ClassInfo<NBTTagCompound>(NBTTagCompound.class, "compound").name("NBT Tag Compound").parser(new Parser<NBTTagCompound>() {
|
Classes.registerClass(new ClassInfo<NBTTagCompound>(NBTTagCompound.class, "compound").name("NBT Tag Compound").parser(new Parser<NBTTagCompound>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -99,6 +103,7 @@ public class SkStuff extends JavaPlugin {
|
|||||||
Skript.registerExpression(ExprNBTv1_8_R2.class, net.minecraft.server.v1_8_R2.NBTTagCompound.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] of %entity/block/itemstack%", "%entity/block/itemstack%'s nbt[[ ]tag[s]]");
|
Skript.registerExpression(ExprNBTv1_8_R2.class, net.minecraft.server.v1_8_R2.NBTTagCompound.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] of %entity/block/itemstack%", "%entity/block/itemstack%'s nbt[[ ]tag[s]]");
|
||||||
Skript.registerExpression(ExprItemNBTv1_8_R2.class, ItemStack.class, ExpressionType.SIMPLE, "%itemstack% with [custom] nbt[[ ]tag[s]] %string%");
|
Skript.registerExpression(ExprItemNBTv1_8_R2.class, ItemStack.class, ExpressionType.SIMPLE, "%itemstack% with [custom] nbt[[ ]tag[s]] %string%");
|
||||||
Skript.registerExpression(ExprTagOfv1_8_R2.class, Object.class, ExpressionType.SIMPLE, "[nbt[ ]]tag %string% of [nbt [compound]] %compound%");
|
Skript.registerExpression(ExprTagOfv1_8_R2.class, Object.class, ExpressionType.SIMPLE, "[nbt[ ]]tag %string% of [nbt [compound]] %compound%");
|
||||||
|
Skript.registerExpression(ExprFileNBTv1_8_R2.class, net.minecraft.server.v1_8_R2.NBTTagCompound.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] of [file] %string%");
|
||||||
Classes.registerClass(new ClassInfo<net.minecraft.server.v1_8_R2.NBTTagCompound>(net.minecraft.server.v1_8_R2.NBTTagCompound.class, "compound").name("NBT Tag Compound").parser(new Parser<net.minecraft.server.v1_8_R2.NBTTagCompound>() {
|
Classes.registerClass(new ClassInfo<net.minecraft.server.v1_8_R2.NBTTagCompound>(net.minecraft.server.v1_8_R2.NBTTagCompound.class, "compound").name("NBT Tag Compound").parser(new Parser<net.minecraft.server.v1_8_R2.NBTTagCompound>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -140,6 +145,7 @@ public class SkStuff extends JavaPlugin {
|
|||||||
Skript.registerExpression(ExprNBTv1_8_R3.class, net.minecraft.server.v1_8_R3.NBTTagCompound.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] of %entity/block/itemstack%", "%entity/block/itemstack%'s nbt[[ ]tag[s]]");
|
Skript.registerExpression(ExprNBTv1_8_R3.class, net.minecraft.server.v1_8_R3.NBTTagCompound.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] of %entity/block/itemstack%", "%entity/block/itemstack%'s nbt[[ ]tag[s]]");
|
||||||
Skript.registerExpression(ExprItemNBTv1_8_R3.class, ItemStack.class, ExpressionType.SIMPLE, "%itemstack% with [custom] nbt[[ ]tag[s]] %string%");
|
Skript.registerExpression(ExprItemNBTv1_8_R3.class, ItemStack.class, ExpressionType.SIMPLE, "%itemstack% with [custom] nbt[[ ]tag[s]] %string%");
|
||||||
Skript.registerExpression(ExprTagOfv1_8_R3.class, Object.class, ExpressionType.SIMPLE, "[nbt[ ]]tag %string% of [nbt [compound]] %compound%");
|
Skript.registerExpression(ExprTagOfv1_8_R3.class, Object.class, ExpressionType.SIMPLE, "[nbt[ ]]tag %string% of [nbt [compound]] %compound%");
|
||||||
|
Skript.registerExpression(ExprFileNBTv1_8_R3.class, net.minecraft.server.v1_8_R3.NBTTagCompound.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] of [file] %string%");
|
||||||
Classes.registerClass(new ClassInfo<net.minecraft.server.v1_8_R3.NBTTagCompound>(net.minecraft.server.v1_8_R3.NBTTagCompound.class, "compound").name("NBT Compound").parser(new Parser<net.minecraft.server.v1_8_R3.NBTTagCompound>() {
|
Classes.registerClass(new ClassInfo<net.minecraft.server.v1_8_R3.NBTTagCompound>(net.minecraft.server.v1_8_R3.NBTTagCompound.class, "compound").name("NBT Compound").parser(new Parser<net.minecraft.server.v1_8_R3.NBTTagCompound>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -196,10 +202,10 @@ public class SkStuff extends JavaPlugin {
|
|||||||
Skript.registerExpression(ExprLengthOfSelection.class, Integer.class, ExpressionType.SIMPLE, "(z( |-)size|length) of [(world[ ]edit|we)] selection of %player%", "%player%'s [(world[ ]edit|we) ]selection (z( |-)size|length)");
|
Skript.registerExpression(ExprLengthOfSelection.class, Integer.class, ExpressionType.SIMPLE, "(z( |-)size|length) of [(world[ ]edit|we)] selection of %player%", "%player%'s [(world[ ]edit|we) ]selection (z( |-)size|length)");
|
||||||
Skript.registerExpression(ExprHeightOfSelection.class, Integer.class, ExpressionType.SIMPLE, "(y( |-)size|height) of [(world[ ]edit|we)] selection of %player%", "%player%'s [(world[ ]edit|we) ]selection (y( |-)size|height)");
|
Skript.registerExpression(ExprHeightOfSelection.class, Integer.class, ExpressionType.SIMPLE, "(y( |-)size|height) of [(world[ ]edit|we)] selection of %player%", "%player%'s [(world[ ]edit|we) ]selection (y( |-)size|height)");
|
||||||
Skript.registerExpression(ExprAreaOfSelection.class, Integer.class, ExpressionType.SIMPLE, "area of [(world[ ]edit|we)] selection of %player%", "%player%'s [(world[ ]edit|we)] selection area");
|
Skript.registerExpression(ExprAreaOfSelection.class, Integer.class, ExpressionType.SIMPLE, "area of [(world[ ]edit|we)] selection of %player%", "%player%'s [(world[ ]edit|we)] selection area");
|
||||||
Skript.registerExpression(ExprVolumeOfSchematic.class, Integer.class, ExpressionType.SIMPLE, "volume of schem[atic] %string% [from [folder] %-string%]");
|
Skript.registerExpression(ExprVolumeOfSchematic.class, Integer.class, ExpressionType.SIMPLE, "volume of schem[atic] %string%");
|
||||||
Skript.registerExpression(ExprWidthOfSchematic.class, Integer.class, ExpressionType.SIMPLE, "(x( |-)size|width) of schem[atic] %string% [from [folder] %-string%]");
|
Skript.registerExpression(ExprWidthOfSchematic.class, Integer.class, ExpressionType.SIMPLE, "(x( |-)size|width) of schem[atic] %string%");
|
||||||
Skript.registerExpression(ExprHeightOfSchematic.class, Integer.class, ExpressionType.SIMPLE, "(y( |-)size|height) of schem[atic] %string% [from [folder] %-string%]");
|
Skript.registerExpression(ExprHeightOfSchematic.class, Integer.class, ExpressionType.SIMPLE, "(y( |-)size|height) of schem[atic] %string%");
|
||||||
Skript.registerExpression(ExprLengthOfSchematic.class, Integer.class, ExpressionType.SIMPLE, "(z( |-)size|length) of schem[atic] %string% [from [folder] %-string%]");
|
Skript.registerExpression(ExprLengthOfSchematic.class, Integer.class, ExpressionType.SIMPLE, "(z( |-)size|length) of schem[atic] %string%");
|
||||||
Classes.registerClass(new ClassInfo<EditSession>(EditSession.class, "editsession").name("Edit Session").parser(new Parser<EditSession>() {
|
Classes.registerClass(new ClassInfo<EditSession>(EditSession.class, "editsession").name("Edit Session").parser(new Parser<EditSession>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
123
src/me/TheBukor/expressions/ExprFileNBTv1_8_R1.java
Normal file
123
src/me/TheBukor/expressions/ExprFileNBTv1_8_R1.java
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
package me.TheBukor.expressions;
|
||||||
|
|
||||||
|
import java.io.EOFException;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.zip.ZipException;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import org.bukkit.event.Event;
|
||||||
|
|
||||||
|
import ch.njol.skript.classes.Changer.ChangeMode;
|
||||||
|
import ch.njol.skript.lang.Expression;
|
||||||
|
import ch.njol.skript.lang.SkriptParser.ParseResult;
|
||||||
|
import ch.njol.skript.lang.util.SimpleExpression;
|
||||||
|
import ch.njol.util.Kleenean;
|
||||||
|
import ch.njol.util.coll.CollectionUtils;
|
||||||
|
import net.minecraft.server.v1_8_R1.MojangsonParser;
|
||||||
|
import net.minecraft.server.v1_8_R1.NBTCompressedStreamTools;
|
||||||
|
import net.minecraft.server.v1_8_R1.NBTTagCompound;
|
||||||
|
|
||||||
|
public class ExprFileNBTv1_8_R1 extends SimpleExpression<NBTTagCompound> {
|
||||||
|
private Expression<String> input;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<? extends NBTTagCompound> getReturnType() {
|
||||||
|
return NBTTagCompound.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isSingle() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean arg2, ParseResult arg3) {
|
||||||
|
input = (Expression<String>) expr[0];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString(@Nullable Event e, boolean arg1) {
|
||||||
|
return "the NBT of file from file " + input.toString(e, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
protected NBTTagCompound[] get(Event e) {
|
||||||
|
NBTTagCompound NBT = null;
|
||||||
|
File file = new File(input.getSingle(e));
|
||||||
|
if (file.exists()) {
|
||||||
|
try {
|
||||||
|
InputStream fis = new FileInputStream(file);
|
||||||
|
NBT = NBTCompressedStreamTools.a(fis);
|
||||||
|
fis.close();
|
||||||
|
} catch (EOFException ex) {
|
||||||
|
//End of file, no error.
|
||||||
|
} catch (ZipException ex) {
|
||||||
|
return null; //Not a valid file (not compressed in GZIP format)
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return null; // Specified file doesn't exist
|
||||||
|
}
|
||||||
|
return new NBTTagCompound[] { NBT };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
|
||||||
|
File file = new File(input.getSingle(e));
|
||||||
|
String tags = (String) delta[0];
|
||||||
|
if (mode == ChangeMode.ADD) {
|
||||||
|
try {
|
||||||
|
InputStream fis = new FileInputStream(file);
|
||||||
|
NBTTagCompound NBT = NBTCompressedStreamTools.a(fis);
|
||||||
|
OutputStream os = new FileOutputStream(file);
|
||||||
|
NBTTagCompound NBT1 = MojangsonParser.parse(tags);
|
||||||
|
NBT.a(NBT1);
|
||||||
|
NBTCompressedStreamTools.a(NBT, os);
|
||||||
|
fis.close();
|
||||||
|
os.close();
|
||||||
|
} catch (EOFException ex) {
|
||||||
|
//End of file, no error.
|
||||||
|
} catch (ZipException ex) {
|
||||||
|
return; //Not a valid file (not compressed in GZIP format)
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
} else if (mode == ChangeMode.REMOVE) {
|
||||||
|
try {
|
||||||
|
InputStream fis = new FileInputStream(file);
|
||||||
|
NBTTagCompound NBT = NBTCompressedStreamTools.a(fis);
|
||||||
|
OutputStream os = new FileOutputStream(file);
|
||||||
|
NBT.remove(tags);
|
||||||
|
NBTCompressedStreamTools.a(NBT, os);
|
||||||
|
fis.close();
|
||||||
|
os.close();
|
||||||
|
} catch (EOFException ex) {
|
||||||
|
//End of file, no error.
|
||||||
|
} catch (ZipException ex) {
|
||||||
|
return; //Not a valid file (not compressed in GZIP format)
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public Class<?>[] acceptChange(ChangeMode mode) {
|
||||||
|
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE) {
|
||||||
|
return CollectionUtils.array(String.class);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
127
src/me/TheBukor/expressions/ExprFileNBTv1_8_R2.java
Normal file
127
src/me/TheBukor/expressions/ExprFileNBTv1_8_R2.java
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
package me.TheBukor.expressions;
|
||||||
|
|
||||||
|
import java.io.EOFException;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.zip.ZipException;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import org.bukkit.event.Event;
|
||||||
|
|
||||||
|
import ch.njol.skript.Skript;
|
||||||
|
import ch.njol.skript.classes.Changer.ChangeMode;
|
||||||
|
import ch.njol.skript.lang.Expression;
|
||||||
|
import ch.njol.skript.lang.SkriptParser.ParseResult;
|
||||||
|
import ch.njol.skript.lang.util.SimpleExpression;
|
||||||
|
import ch.njol.util.Kleenean;
|
||||||
|
import ch.njol.util.coll.CollectionUtils;
|
||||||
|
import net.minecraft.server.v1_8_R2.MojangsonParseException;
|
||||||
|
import net.minecraft.server.v1_8_R2.MojangsonParser;
|
||||||
|
import net.minecraft.server.v1_8_R2.NBTCompressedStreamTools;
|
||||||
|
import net.minecraft.server.v1_8_R2.NBTTagCompound;
|
||||||
|
|
||||||
|
public class ExprFileNBTv1_8_R2 extends SimpleExpression<NBTTagCompound> {
|
||||||
|
private Expression<String> input;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<? extends NBTTagCompound> getReturnType() {
|
||||||
|
return NBTTagCompound.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isSingle() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean arg2, ParseResult arg3) {
|
||||||
|
input = (Expression<String>) expr[0];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString(@Nullable Event e, boolean arg1) {
|
||||||
|
return "the NBT of file from file " + input.toString(e, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
protected NBTTagCompound[] get(Event e) {
|
||||||
|
NBTTagCompound NBT = null;
|
||||||
|
File file = new File(input.getSingle(e));
|
||||||
|
if (file.exists()) {
|
||||||
|
try {
|
||||||
|
InputStream fis = new FileInputStream(file);
|
||||||
|
NBT = NBTCompressedStreamTools.a(fis);
|
||||||
|
fis.close();
|
||||||
|
} catch (EOFException ex) {
|
||||||
|
//End of file, no error.
|
||||||
|
} catch (ZipException ex) {
|
||||||
|
return null; //Not a valid file (not compressed in GZIP format)
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return null; // Specified file doesn't exist
|
||||||
|
}
|
||||||
|
return new NBTTagCompound[] { NBT };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
|
||||||
|
File file = new File(input.getSingle(e));
|
||||||
|
String tags = (String) delta[0];
|
||||||
|
if (mode == ChangeMode.ADD) {
|
||||||
|
try {
|
||||||
|
InputStream fis = new FileInputStream(file);
|
||||||
|
NBTTagCompound NBT = NBTCompressedStreamTools.a(fis);
|
||||||
|
OutputStream os = new FileOutputStream(file);
|
||||||
|
NBTTagCompound NBT1 = MojangsonParser.parse(tags);
|
||||||
|
NBT.a(NBT1);
|
||||||
|
NBTCompressedStreamTools.a(NBT, os);
|
||||||
|
fis.close();
|
||||||
|
os.close();
|
||||||
|
} catch (EOFException ex) {
|
||||||
|
//End of file, no error.
|
||||||
|
} catch (ZipException ex) {
|
||||||
|
return; //Not a valid file (not compressed in GZIP format)
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
} catch (MojangsonParseException ex) {
|
||||||
|
Skript.warning("Error when parsing NBT - " + ex.getMessage());
|
||||||
|
}
|
||||||
|
} else if (mode == ChangeMode.REMOVE) {
|
||||||
|
try {
|
||||||
|
InputStream fis = new FileInputStream(file);
|
||||||
|
NBTTagCompound NBT = NBTCompressedStreamTools.a(fis);
|
||||||
|
OutputStream os = new FileOutputStream(file);
|
||||||
|
NBT.remove(tags);
|
||||||
|
NBTCompressedStreamTools.a(NBT, os);
|
||||||
|
fis.close();
|
||||||
|
os.close();
|
||||||
|
} catch (EOFException ex) {
|
||||||
|
//End of file, no error.
|
||||||
|
} catch (ZipException ex) {
|
||||||
|
return; //Not a valid file (not compressed in GZIP format)
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public Class<?>[] acceptChange(ChangeMode mode) {
|
||||||
|
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE) {
|
||||||
|
return CollectionUtils.array(String.class);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
127
src/me/TheBukor/expressions/ExprFileNBTv1_8_R3.java
Normal file
127
src/me/TheBukor/expressions/ExprFileNBTv1_8_R3.java
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
package me.TheBukor.expressions;
|
||||||
|
|
||||||
|
import java.io.EOFException;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.zip.ZipException;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import org.bukkit.event.Event;
|
||||||
|
|
||||||
|
import ch.njol.skript.Skript;
|
||||||
|
import ch.njol.skript.classes.Changer.ChangeMode;
|
||||||
|
import ch.njol.skript.lang.Expression;
|
||||||
|
import ch.njol.skript.lang.SkriptParser.ParseResult;
|
||||||
|
import ch.njol.skript.lang.util.SimpleExpression;
|
||||||
|
import ch.njol.util.Kleenean;
|
||||||
|
import ch.njol.util.coll.CollectionUtils;
|
||||||
|
import net.minecraft.server.v1_8_R3.MojangsonParseException;
|
||||||
|
import net.minecraft.server.v1_8_R3.MojangsonParser;
|
||||||
|
import net.minecraft.server.v1_8_R3.NBTCompressedStreamTools;
|
||||||
|
import net.minecraft.server.v1_8_R3.NBTTagCompound;
|
||||||
|
|
||||||
|
public class ExprFileNBTv1_8_R3 extends SimpleExpression<NBTTagCompound> {
|
||||||
|
private Expression<String> input;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<? extends NBTTagCompound> getReturnType() {
|
||||||
|
return NBTTagCompound.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isSingle() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean arg2, ParseResult arg3) {
|
||||||
|
input = (Expression<String>) expr[0];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString(@Nullable Event e, boolean arg1) {
|
||||||
|
return "the NBT of file from file " + input.toString(e, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
protected NBTTagCompound[] get(Event e) {
|
||||||
|
NBTTagCompound NBT = null;
|
||||||
|
File file = new File(input.getSingle(e));
|
||||||
|
if (file.exists()) {
|
||||||
|
try {
|
||||||
|
InputStream fis = new FileInputStream(file);
|
||||||
|
NBT = NBTCompressedStreamTools.a(fis);
|
||||||
|
fis.close();
|
||||||
|
} catch (EOFException ex) {
|
||||||
|
//End of file, no error.
|
||||||
|
} catch (ZipException ex) {
|
||||||
|
return null; //Not a valid file (not compressed in GZIP format)
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return null; // Specified file doesn't exist
|
||||||
|
}
|
||||||
|
return new NBTTagCompound[] { NBT };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
|
||||||
|
File file = new File(input.getSingle(e));
|
||||||
|
String tags = (String) delta[0];
|
||||||
|
if (mode == ChangeMode.ADD) {
|
||||||
|
try {
|
||||||
|
InputStream fis = new FileInputStream(file);
|
||||||
|
NBTTagCompound NBT = NBTCompressedStreamTools.a(fis);
|
||||||
|
OutputStream os = new FileOutputStream(file);
|
||||||
|
NBTTagCompound NBT1 = MojangsonParser.parse(tags);
|
||||||
|
NBT.a(NBT1);
|
||||||
|
NBTCompressedStreamTools.a(NBT, os);
|
||||||
|
fis.close();
|
||||||
|
os.close();
|
||||||
|
} catch (EOFException ex) {
|
||||||
|
//End of file, no error.
|
||||||
|
} catch (ZipException ex) {
|
||||||
|
return; //Not a valid file (not compressed in GZIP format)
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
} catch (MojangsonParseException ex) {
|
||||||
|
Skript.warning("Error when parsing NBT - " + ex.getMessage());
|
||||||
|
}
|
||||||
|
} else if (mode == ChangeMode.REMOVE) {
|
||||||
|
try {
|
||||||
|
InputStream fis = new FileInputStream(file);
|
||||||
|
NBTTagCompound NBT = NBTCompressedStreamTools.a(fis);
|
||||||
|
OutputStream os = new FileOutputStream(file);
|
||||||
|
NBT.remove(tags);
|
||||||
|
NBTCompressedStreamTools.a(NBT, os);
|
||||||
|
fis.close();
|
||||||
|
os.close();
|
||||||
|
} catch (EOFException ex) {
|
||||||
|
//End of file, no error.
|
||||||
|
} catch (ZipException ex) {
|
||||||
|
return; //Not a valid file (not compressed in GZIP format)
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public Class<?>[] acceptChange(ChangeMode mode) {
|
||||||
|
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE) {
|
||||||
|
return CollectionUtils.array(String.class);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user