Hopefuly should work fine.

Updated to 1.9, moving from Reflection to a new method using and
Interface with useful NMS methods (some expressions are still using
Reflection).
This commit is contained in:
Richard
2016-03-03 22:01:19 -03:00
parent f3fcb59fa1
commit b0aa11475e
23 changed files with 4900 additions and 4238 deletions

View File

@@ -1,179 +0,0 @@
package me.TheBukor.SkStuff.util;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.fusesource.jansi.Ansi;
import ch.njol.skript.Skript;
public class NBTUtil {
private static Class<?> nbtBaseClass = ReflectionUtils.getNMSClass("NBTBase", false);
private static Class<?> nbtClass = ReflectionUtils.getNMSClass("NBTTagCompound", false);
private static Class<?> nbtListClass = ReflectionUtils.getNMSClass("NBTTagList", false);
/**
* 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 NBT, Object toAdd) {
if (NBT.getClass() == nbtClass && toAdd.getClass() == nbtClass) {
try {
HashMap<String, Object> map = (HashMap<String, Object>) ReflectionUtils.getField("map", nbtClass, toAdd);
Set<String> keySet = (Set<String>) nbtClass.getMethod("c").invoke(toAdd);
Iterator<String> iterator = keySet.iterator();
while(iterator.hasNext()) {
String string = (String) iterator.next();
Object base = nbtBaseClass.cast(map.get(string));
if((byte) nbtBaseClass.getMethod("getTypeId").invoke(base) == 10) {
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, nbtBaseClass.cast(nbtClass));
} else {
nbtClass.getMethod("set", String.class, nbtBaseClass).invoke(NBT, string, base.getClass().getMethod("clone").invoke(base));
}
} else {
nbtClass.getMethod("set", String.class, nbtBaseClass).invoke(NBT, string, base.getClass().getMethod("clone").invoke(base));
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
/**
* Gets the ID of the contents inside a NBT List.
* I needed to add this because the 1.7 and before versions of the NBTTagList
* class had a different name for the method that got this value.
* 1.8 used "f()", while 1.7 used "d()".
*/
public static int getContentsId(Object list) {
if (list.getClass() == nbtListClass) {
Field type = null;
int result = 0;
try {
type = nbtListClass.getDeclaredField("type");
type.setAccessible(true);
result = type.getInt(list);
type.setAccessible(false);
return result;
} catch (Exception ex) {
type.setAccessible(false);
ex.printStackTrace();
}
return result;
}
return 0;
}
/**
* Used for the "addToList()" and "setIndex()" methods if the typeId of the contents is still not defined.
*/
public static void setContentsId(Object list, int newId) {
if (list.getClass() == nbtListClass) {
Field type = null;
try {
type = nbtListClass.getDeclaredField("type");
type.setAccessible(true);
type.set(list, newId);
type.setAccessible(false);
} catch (Exception ex) {
type.setAccessible(false);
ex.printStackTrace();
}
}
}
@SuppressWarnings("unchecked")
public static List<Object> getContents(Object list) {
if (list.getClass() == nbtListClass) {
List<Object> result = null;
result = (List<Object>) ReflectionUtils.getField("list", nbtListClass, list);
return result;
}
return null;
}
/**
* Kind of a copy of the "add()" method from the NBTTagList class.
*/
public static void addToList(Object list, Object[] toAdd) {
if (list.getClass() == nbtListClass && toAdd[0].getClass() == nbtBaseClass) {
int listTypeId = NBTUtil.getContentsId(list);
int toAddId = 0;
try {
toAddId = (int) toAdd.getClass().getMethod("getTypeId").invoke(toAdd);
} catch (Exception ex) {
ex.printStackTrace();
}
if (listTypeId == 0) {
try {
NBTUtil.setContentsId(list, toAddId);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (listTypeId != toAddId) {
Skript.warning(Ansi.ansi().fgBright(Ansi.Color.RED) + "Adding mismatching tag types to NBT list" + Ansi.ansi().fgBright(Ansi.Color.DEFAULT));
return;
}
for (Object tag : toAdd) {
NBTUtil.getContents(list).add(tag);
}
}
}
public static void removefromList(Object list, int index) {
if (list.getClass() == nbtListClass) {
if (index >= 0 && index < NBTUtil.getContents(list).size()) {
NBTUtil.getContents(list).remove(index);
}
}
}
public static void setIndex(Object list, int index, Object toAdd) {
if (list.getClass() == nbtListClass && toAdd.getClass() == nbtBaseClass) {
if (index >= 0 && index < NBTUtil.getContents(list).size()) {
int listTypeId = NBTUtil.getContentsId(list);
int toAddId = 0;
try {
toAddId = (int) toAdd.getClass().getMethod("getTypeId").invoke(toAdd);
} catch (Exception ex) {
ex.printStackTrace();
}
if (listTypeId == 0) {
try {
NBTUtil.setContentsId(list, toAddId);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (listTypeId != toAddId) {
Skript.warning("Adding mismatching tag types to NBT list");
return;
}
NBTUtil.getContents(list).set(index, toAdd);
}
}
}
public static Object getIndex(Object list, int index) {
if (list.getClass() == nbtListClass) {
if (index >= 0 && index < NBTUtil.getContents(list).size()) {
NBTUtil.getContents(list).get(index);
}
}
return null;
}
}

View File

@@ -1,52 +1,56 @@
package me.TheBukor.SkStuff.util;
import java.io.File;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
public interface NMSInterface {
public void addToCompound(Object compound, Object toAdd);
public void removeFromCompound(Object compound, String ... toRemove);
public Object parseRawNBT(String rawNBT);
public int getContentsId(Object nbtList);
public Object[] getContents(Object nbtList);
public void addToList(Object nbtList, Object toAdd);
public void removeFromList(Object nbtList, int index);
public void setIndex(Object nbtList, int index, Object toSet);
public Object getIndex(Object nbtList, int index);
public void removePathfinderGoal(Object entity, Class<?> goalClass, boolean isTargetSelector);
public void addPathfinderGoal(Object entity, int priority, Object goal, boolean isTargetSelector);
public void registerCompoundClassInfo();
public void registerNBTListClassInfo();
public Object getEntityNBT(Entity entity);
public Object getTileNBT(Block block);
public Object getItemNBT(ItemStack itemStack);
public void setEntityNBT(Entity entity, Object newCompound);
public void setTileNBT(Block block, Object newCompound);
public ItemStack getItemWithNBT(ItemStack itemStack, Object compound);
public Object getFileNBT(File file);
public void setFileNBT(File file, Object newCompound);
}
package me.TheBukor.SkStuff.util;
import java.io.File;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
public interface NMSInterface {
public void addToCompound(Object compound, Object toAdd);
public void removeFromCompound(Object compound, String ... toRemove);
public Object parseRawNBT(String rawNBT);
public int getContentsId(Object nbtList);
public Object[] getContents(Object nbtList);
public void addToList(Object nbtList, Object toAdd);
public void removeFromList(Object nbtList, int index);
public void setIndex(Object nbtList, int index, Object toSet);
public Object getIndex(Object nbtList, int index);
public void removePathfinderGoal(Object entity, Class<?> goalClass, boolean isTargetSelector);
public void addPathfinderGoal(Object entity, int priority, Object goal, boolean isTargetSelector);
public void registerCompoundClassInfo();
public void registerNBTListClassInfo();
public Object getEntityNBT(Entity entity);
public Object getTileNBT(Block block);
public Object getItemNBT(ItemStack itemStack);
public void setEntityNBT(Entity entity, Object newCompound);
public void setTileNBT(Block block, Object newCompound);
public ItemStack getItemWithNBT(ItemStack itemStack, Object compound);
public Object getFileNBT(File file);
public void setFileNBT(File file, Object newCompound);
public Object convertToNBT(Number number);
public Object convertToNBT(String string);
}

View File

@@ -1,444 +1,498 @@
package me.TheBukor.SkStuff.util;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_7_R4.inventory.CraftItemStack;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.coll.CollectionUtils;
import net.minecraft.server.v1_7_R4.MojangsonParser;
import net.minecraft.server.v1_7_R4.NBTBase;
import net.minecraft.server.v1_7_R4.NBTCompressedStreamTools;
import net.minecraft.server.v1_7_R4.NBTTagCompound;
import net.minecraft.server.v1_7_R4.NBTTagDouble;
import net.minecraft.server.v1_7_R4.NBTTagEnd;
import net.minecraft.server.v1_7_R4.NBTTagFloat;
import net.minecraft.server.v1_7_R4.NBTTagInt;
import net.minecraft.server.v1_7_R4.NBTTagList;
import net.minecraft.server.v1_7_R4.NBTTagString;
import net.minecraft.server.v1_7_R4.TileEntity;
import net.minecraft.server.v1_7_R4.World;
public class NMS_v1_7_R4 implements NMSInterface {
@SuppressWarnings("unchecked")
@Override
public void addToCompound(Object compound, Object toAdd) {
if (compound instanceof NBTTagCompound && toAdd instanceof NBTTagCompound) {
HashMap<String, Object> map = (HashMap<String, Object>) ReflectionUtils.getField("map", NBTTagCompound.class, toAdd);
Set<String> keySet = ((NBTTagCompound) toAdd).c();
Iterator<String> iterator = keySet.iterator();
while(iterator.hasNext()) {
String string = (String) iterator.next();
NBTBase base = (NBTBase) map.get(string);
if(base.getTypeId() == 10) {
if(((NBTTagCompound) compound).hasKeyOfType(string, 10)) {
NBTTagCompound localNBT = ((NBTTagCompound) compound).getCompound(string);
addToCompound(localNBT, (NBTTagCompound) base);
} else {
((NBTTagCompound) compound).set(string, base.clone());
}
} else {
((NBTTagCompound) compound).set(string, base.clone());
}
}
}
}
@Override
public void removeFromCompound(Object compound, String ... toRemove) {
if (compound instanceof NBTTagCompound) {
((NBTTagCompound) compound).remove(toRemove.toString()); //FIXME
}
}
@Override
public Object parseRawNBT(String rawNBT) {
NBTTagCompound parsedNBT = null;
parsedNBT = (NBTTagCompound) MojangsonParser.parse(rawNBT);
return parsedNBT;
}
@Override
public int getContentsId(Object nbtList) {
if (nbtList instanceof NBTTagList) {
return ((NBTTagList) nbtList).d();
}
return 0;
}
@Override
public Object[] getContents(Object nbtList) {
if (nbtList instanceof NBTTagList) {
List<Object> contents = new ArrayList<Object>();
for (int i = 0; i < ((NBTTagList) nbtList).size(); i++) {
if (getIndex(nbtList, i) != null) {
contents.add(getIndex(nbtList, i));
}
}
return contents.toArray();
}
return null;
}
@Override
public void addToList(Object nbtList, Object toAdd) {
if (nbtList instanceof NBTTagList && toAdd instanceof NBTBase) {
((NBTTagList) nbtList).add((NBTBase) toAdd);
}
}
@SuppressWarnings("unchecked")
@Override
public void removeFromList(Object nbtList, int index) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
List<Object> actualList = null;
actualList = (List<Object>) ReflectionUtils.getField("list", NBTTagList.class, nbtList);
actualList.remove(index);
}
}
@SuppressWarnings("unchecked")
@Override
public void setIndex(Object nbtList, int index, Object toSet) {
if (nbtList instanceof NBTTagList && toSet instanceof NBTBase && index >= 0 && index < ((NBTTagList) nbtList).size()) {
int typeId = getContentsId(nbtList);
int toSetId = ((NBTBase) toSet).getTypeId();
if (typeId == 0) {
ReflectionUtils.setField("type", NBTTagList.class, nbtList, toSetId);
} else if (typeId != toSetId) {
Skript.warning("Adding mismatching tag types to NBT list");
return;
}
List<Object> actualList = null;
actualList = (List<Object>) ReflectionUtils.getField("list", NBTTagList.class, nbtList);
actualList.set(index, toSet);
}
}
@SuppressWarnings("unchecked")
@Override
public Object getIndex(Object nbtList, int index) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
List<NBTBase> actualList = null;
actualList = (List<NBTBase>) ReflectionUtils.getField("list", NBTTagList.class, nbtList);
NBTBase value = (NBTBase) actualList.get(index);
if (value instanceof NBTTagEnd)
return null;
else
return value;
}
return null;
}
@Override
public void removePathfinderGoal(Object entity, Class<?> goalClass, boolean isTargetSelector) {
Bukkit.getLogger().warning("Sorry, Pathfinder Goals are only supported in 1.8 and above");
}
@Override
public void addPathfinderGoal(Object entity, int priority, Object goal, boolean isTargetSelector) {
Bukkit.getLogger().warning("Sorry, Pathfinder Goals are only supported in 1.8 and above");
}
@Override
public void registerCompoundClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagCompound>(NBTTagCompound.class, "compound").user("((nbt)?( ?tag)?) ?compounds?").name("NBT Compound").changer(new Changer<NBTTagCompound>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE || mode == ChangeMode.SET) {
return CollectionUtils.array(String[].class, NBTTagCompound[].class);
}
return null;
}
@Override
public void change(NBTTagCompound[] NBT, @Nullable Object[] delta, ChangeMode mode) {
if (mode == ChangeMode.SET) {
if (delta[0] instanceof NBTTagCompound) {
NBT[0] = (NBTTagCompound) delta[0];
} else {
NBTTagCompound parsedNBT = (NBTTagCompound) parseRawNBT((String) delta[0]);
NBT[0] = parsedNBT;
}
} else if (mode == ChangeMode.ADD) {
if (delta[0] instanceof String) {
NBTTagCompound parsedNBT = (NBTTagCompound) parseRawNBT((String) delta[0]);
addToCompound(NBT[0], parsedNBT);
} else {
addToCompound(NBT[0], delta[0]);
}
} else if (mode == ChangeMode.REMOVE) {
if (delta[0] instanceof NBTTagCompound)
return;
for (Object s : delta) {
NBT[0].remove((String) s);
}
}
}
}).parser(new Parser<NBTTagCompound>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagCompound parse(String rawNBT, ParseContext context) {
if (rawNBT.startsWith("nbt:{") && rawNBT.endsWith("}")) {
rawNBT.substring(4);
NBTTagCompound NBT = (NBTTagCompound) parseRawNBT(rawNBT);
if (NBT.toString().equals("{}")) {
return null;
}
return NBT;
}
return null;
}
@Override
public String toString(NBTTagCompound compound, int arg1) {
return compound.toString();
}
@Override
public String toVariableNameString(NBTTagCompound compound) {
return "nbt:" + compound.toString();
}
}));
}
@Override
public void registerNBTListClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagList>(NBTTagList.class, "nbtlist").user("nbt ?list ?(tag)?").name("NBT List").changer(new Changer<NBTTagList>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.SET || mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
return CollectionUtils.array(Float[].class, Double[].class, String[].class, NBTTagCompound[].class, Integer[].class, NBTTagList[].class);
}
return null;
}
@Override
public void change(NBTTagList[] nbtList, @Nullable Object[] delta, ChangeMode mode) {
int typeId = 0;
if (delta instanceof Float[]) {
typeId = 5;
} else if (delta instanceof Double[]) {
typeId = 6;
} else if (delta instanceof String[]) {
typeId = 8;
} else if (delta instanceof NBTTagList[]) {
typeId = 9;
} else if (delta instanceof NBTTagCompound[]) {
typeId = 10;
} else if (delta instanceof Integer[]) {
typeId = 11;
} else {
return;
}
if (mode == ChangeMode.SET) {
if (typeId == 9)
nbtList[0] = (NBTTagList) delta[0];
} else if (mode == ChangeMode.ADD) {
if (getContentsId(nbtList[0]) == typeId) {
if (typeId == 5) {
NBTTagFloat floatTag = new NBTTagFloat((float) delta[0]);
addToList(nbtList[0], floatTag);
} else if (typeId == 6) {
NBTTagDouble doubleTag = new NBTTagDouble((double) delta[0]);
addToList(nbtList[0], doubleTag);
} else if (typeId == 8) {
NBTTagString stringTag = new NBTTagString((String) delta [0]);
addToList(nbtList[0], stringTag);
} else if (typeId == 10) {
addToList(nbtList[0], delta[0]);
} else if (typeId == 11) {
NBTTagInt intTag = new NBTTagInt((int) delta[0]);
addToList(nbtList[0], intTag);
}
}
} else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
nbtList[0] = new NBTTagList();
}
}
}).parser(new Parser<NBTTagList>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagList parse(String ignored, ParseContext context) {
return null;
}
@Override
public String toString(NBTTagList nbtList, int arg1) {
return nbtList.toString();
}
@Override
public String toVariableNameString(NBTTagList nbtList) {
return nbtList.toString();
}
}));
}
@Override
public Object getEntityNBT(Entity entity) {
net.minecraft.server.v1_7_R4.Entity nmsEntity = ((CraftEntity) entity).getHandle();
NBTTagCompound NBT = new NBTTagCompound();
nmsEntity.e(NBT);
return NBT;
}
@Override
public Object getTileNBT(Block block) {
NBTTagCompound NBT = new NBTTagCompound();
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(block.getX(), block.getY(), block.getZ());
if (tileEntity == null)
return null;
tileEntity.b(NBT);
return NBT;
}
@Override
public Object getItemNBT(ItemStack itemStack) {
if (itemStack.getType() == Material.AIR)
return null;
NBTTagCompound NBT = new NBTTagCompound();
net.minecraft.server.v1_7_R4.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
NBT = nmsItem.getTag();
if (NBT == null || NBT.toString().equals("{}")) //Null or empty.
return null;
return new Object[] { NBT };
}
@Override
public void setEntityNBT(Entity entity, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
net.minecraft.server.v1_7_R4.Entity nmsEntity = ((CraftEntity) entity).getHandle();
nmsEntity.f((NBTTagCompound) newCompound);
}
}
@Override
public void setTileNBT(Block block, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(block.getX(), block.getY(), block.getZ());
if (tileEntity == null)
return;
tileEntity.a((NBTTagCompound) newCompound);
tileEntity.update();
nmsWorld.notify(tileEntity.x, tileEntity.y, tileEntity.z);
}
}
@Override
public ItemStack getItemWithNBT(ItemStack itemStack, Object compound) {
if (compound instanceof NBTTagCompound) {
if (itemStack.getType() == Material.AIR || itemStack == null)
return null;
if (compound == null || compound.toString().equals("{}"))
return itemStack;
net.minecraft.server.v1_7_R4.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
nmsItem.setTag((NBTTagCompound) compound);
ItemStack newItem = CraftItemStack.asCraftMirror(nmsItem);
return newItem;
}
return null;
}
@Override
public Object getFileNBT(File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException ex) {
return null; //File doesn't exist.
}
NBTTagCompound fileNBT = null;
try {
fileNBT = NBTCompressedStreamTools.a(fis);
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return fileNBT;
}
@Override
public void setFileNBT(File file, Object newCompound) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
try {
NBTCompressedStreamTools.a((NBTTagCompound) newCompound, os);
os.close();
} catch (Exception ex) {
if (ex instanceof EOFException) {
; //Ignore, just end of the file
} else {
ex.printStackTrace();
}
} finally {
try {
os.close();
} catch (Exception ex) {
if (ex instanceof EOFException) {
; //Ignore.
} else {
ex.printStackTrace();
}
}
}
}
package me.TheBukor.SkStuff.util;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_7_R4.inventory.CraftItemStack;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.coll.CollectionUtils;
import net.minecraft.server.v1_7_R4.MojangsonParser;
import net.minecraft.server.v1_7_R4.NBTBase;
import net.minecraft.server.v1_7_R4.NBTCompressedStreamTools;
import net.minecraft.server.v1_7_R4.NBTTagCompound;
import net.minecraft.server.v1_7_R4.NBTTagDouble;
import net.minecraft.server.v1_7_R4.NBTTagFloat;
import net.minecraft.server.v1_7_R4.NBTTagInt;
import net.minecraft.server.v1_7_R4.NBTTagList;
import net.minecraft.server.v1_7_R4.NBTTagString;
import net.minecraft.server.v1_7_R4.TileEntity;
import net.minecraft.server.v1_7_R4.World;
import net.minecraft.server.v1_8_R3.NBTTagByte;
import net.minecraft.server.v1_8_R3.NBTTagLong;
import net.minecraft.server.v1_8_R3.NBTTagShort;
public class NMS_v1_7_R4 implements NMSInterface {
@SuppressWarnings("unchecked")
@Override
public void addToCompound(Object compound, Object toAdd) {
if (compound instanceof NBTTagCompound && toAdd instanceof NBTTagCompound) {
HashMap<String, Object> map = (HashMap<String, Object>) ReflectionUtils.getField("map", NBTTagCompound.class, toAdd);
Set<String> keySet = ((NBTTagCompound) toAdd).c();
Iterator<String> iterator = keySet.iterator();
while(iterator.hasNext()) {
String string = (String) iterator.next();
NBTBase base = (NBTBase) map.get(string);
if(base.getTypeId() == 10) {
if(((NBTTagCompound) compound).hasKeyOfType(string, 10)) {
NBTTagCompound localNBT = ((NBTTagCompound) compound).getCompound(string);
addToCompound(localNBT, (NBTTagCompound) base);
} else {
((NBTTagCompound) compound).set(string, base.clone());
}
} else {
((NBTTagCompound) compound).set(string, base.clone());
}
}
}
}
@Override
public void removeFromCompound(Object compound, String ... toRemove) {
if (compound instanceof NBTTagCompound) {
for (String s : toRemove) {
((NBTTagCompound) compound).remove(s);
}
}
}
@Override
public NBTTagCompound parseRawNBT(String rawNBT) {
NBTTagCompound parsedNBT = null;
parsedNBT = (NBTTagCompound) MojangsonParser.parse(rawNBT);
return parsedNBT;
}
@Override
public int getContentsId(Object nbtList) {
if (nbtList instanceof NBTTagList) {
return ((NBTTagList) nbtList).d();
}
return 0;
}
@Override
public Object[] getContents(Object nbtList) {
if (nbtList instanceof NBTTagList) {
Object[] contents = new Object[((NBTTagList) nbtList).size()];
for (int i = 0; i < ((NBTTagList) nbtList).size(); i++) {
if (getIndex(nbtList, i) != null) {
contents[i] = getIndex(nbtList, i);
}
}
return contents;
}
return null;
}
@Override
public void addToList(Object nbtList, Object toAdd) {
if (nbtList instanceof NBTTagList && toAdd instanceof NBTBase) {
((NBTTagList) nbtList).add((NBTBase) toAdd);
}
}
@SuppressWarnings("unchecked")
@Override
public void removeFromList(Object nbtList, int index) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
List<Object> actualList = null;
actualList = (List<Object>) ReflectionUtils.getField("list", NBTTagList.class, nbtList);
actualList.remove(index);
}
}
@SuppressWarnings("unchecked")
@Override
public void setIndex(Object nbtList, int index, Object toSet) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
int typeId = getContentsId(nbtList);
NBTBase toSetNBT = null;
if (toSet instanceof NBTBase)
toSetNBT = (NBTBase) toSet;
else if (toSet instanceof Number)
toSetNBT = (NBTBase) convertToNBT((Number) toSet);
else if (toSet instanceof String)
toSetNBT = convertToNBT((String) toSet);
else
return;
int toSetId = (toSetNBT).getTypeId();
if (typeId == 0) {
ReflectionUtils.setField("type", NBTTagList.class, nbtList, toSetId);
} else if (typeId != toSetId) {
Skript.warning("Adding mismatching tag types to NBT list");
return;
}
List<Object> actualList = null;
actualList = (List<Object>) ReflectionUtils.getField("list", NBTTagList.class, nbtList);
actualList.set(index, toSetNBT);
}
}
@SuppressWarnings("unchecked")
@Override
public Object getIndex(Object nbtList, int index) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
List<NBTBase> actualList = null;
actualList = (List<NBTBase>) ReflectionUtils.getField("list", NBTTagList.class, nbtList);
NBTBase value = (NBTBase) actualList.get(index);
return value;
}
return null;
}
@Override
public void removePathfinderGoal(Object entity, Class<?> goalClass, boolean isTargetSelector) {
Bukkit.getLogger().warning("Sorry, Pathfinder Goals are only supported in 1.8 and above");
}
@Override
public void addPathfinderGoal(Object entity, int priority, Object goal, boolean isTargetSelector) {
Bukkit.getLogger().warning("Sorry, Pathfinder Goals are only supported in 1.8 and above");
}
@Override
public void registerCompoundClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagCompound>(NBTTagCompound.class, "compound").user("((nbt)?( ?tag)?) ?compounds?").name("NBT Compound").changer(new Changer<NBTTagCompound>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE || mode == ChangeMode.SET) {
return CollectionUtils.array(String[].class, NBTTagCompound[].class);
}
return null;
}
@Override
public void change(NBTTagCompound[] NBT, @Nullable Object[] delta, ChangeMode mode) {
if (mode == ChangeMode.SET) {
if (delta[0] instanceof NBTTagCompound) {
NBT[0] = (NBTTagCompound) delta[0];
} else {
NBTTagCompound parsedNBT = parseRawNBT((String) delta[0]);
NBT[0] = parsedNBT;
}
} else if (mode == ChangeMode.ADD) {
if (delta[0] instanceof String) {
NBTTagCompound parsedNBT = parseRawNBT((String) delta[0]);
addToCompound(NBT[0], parsedNBT);
} else {
addToCompound(NBT[0], delta[0]);
}
} else if (mode == ChangeMode.REMOVE) {
if (delta[0] instanceof NBTTagCompound)
return;
for (Object s : delta) {
NBT[0].remove((String) s);
}
}
}
}).parser(new Parser<NBTTagCompound>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagCompound parse(String rawNBT, ParseContext context) {
if (rawNBT.startsWith("nbt:{") && rawNBT.endsWith("}")) {
rawNBT.substring(4);
NBTTagCompound NBT = parseRawNBT(rawNBT);
if (NBT.toString().equals("{}")) {
return null;
}
return NBT;
}
return null;
}
@Override
public String toString(NBTTagCompound compound, int arg1) {
return compound.toString();
}
@Override
public String toVariableNameString(NBTTagCompound compound) {
return "nbt:" + compound.toString();
}
}));
}
@Override
public void registerNBTListClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagList>(NBTTagList.class, "nbtlist").user("nbt ?list ?(tag)?").name("NBT List").changer(new Changer<NBTTagList>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.SET || mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
return CollectionUtils.array(Float[].class, Double[].class, String[].class, NBTTagCompound[].class, Integer[].class, NBTTagList[].class);
}
return null;
}
@Override
public void change(NBTTagList[] nbtList, @Nullable Object[] delta, ChangeMode mode) {
int typeId = 0;
if (delta instanceof Float[]) {
typeId = 5;
} else if (delta instanceof Double[]) {
typeId = 6;
} else if (delta instanceof String[]) {
typeId = 8;
} else if (delta instanceof NBTTagList[]) {
typeId = 9;
} else if (delta instanceof NBTTagCompound[]) {
typeId = 10;
} else if (delta instanceof Integer[]) {
typeId = 11;
} else {
return;
}
if (mode == ChangeMode.SET) {
if (typeId == 9)
nbtList[0] = (NBTTagList) delta[0];
} else if (mode == ChangeMode.ADD) {
if (getContentsId(nbtList[0]) == typeId) {
if (typeId == 5) {
NBTTagFloat floatTag = new NBTTagFloat((float) delta[0]);
addToList(nbtList[0], floatTag);
} else if (typeId == 6) {
NBTTagDouble doubleTag = new NBTTagDouble((double) delta[0]);
addToList(nbtList[0], doubleTag);
} else if (typeId == 8) {
NBTTagString stringTag = new NBTTagString((String) delta [0]);
addToList(nbtList[0], stringTag);
} else if (typeId == 10) {
addToList(nbtList[0], delta[0]);
} else if (typeId == 11) {
NBTTagInt intTag = new NBTTagInt((int) delta[0]);
addToList(nbtList[0], intTag);
}
}
} else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
nbtList[0] = new NBTTagList();
}
}
}).parser(new Parser<NBTTagList>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagList parse(String ignored, ParseContext context) {
return null;
}
@Override
public String toString(NBTTagList nbtList, int arg1) {
return nbtList.toString();
}
@Override
public String toVariableNameString(NBTTagList nbtList) {
return nbtList.toString();
}
}));
}
@Override
public NBTTagCompound getEntityNBT(Entity entity) {
net.minecraft.server.v1_7_R4.Entity nmsEntity = ((CraftEntity) entity).getHandle();
NBTTagCompound NBT = new NBTTagCompound();
nmsEntity.e(NBT);
return NBT;
}
@Override
public NBTTagCompound getTileNBT(Block block) {
NBTTagCompound NBT = new NBTTagCompound();
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(block.getX(), block.getY(), block.getZ());
if (tileEntity == null)
return null;
tileEntity.b(NBT);
return NBT;
}
@Override
public NBTTagCompound getItemNBT(ItemStack itemStack) {
if (itemStack.getType() == Material.AIR)
return null;
NBTTagCompound NBT = new NBTTagCompound();
net.minecraft.server.v1_7_R4.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
NBT = nmsItem.getTag();
if (NBT == null || NBT.toString().equals("{}")) //Null or empty.
return null;
return NBT;
}
@Override
public void setEntityNBT(Entity entity, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
net.minecraft.server.v1_7_R4.Entity nmsEntity = ((CraftEntity) entity).getHandle();
nmsEntity.f((NBTTagCompound) newCompound);
}
}
@Override
public void setTileNBT(Block block, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(block.getX(), block.getY(), block.getZ());
if (tileEntity == null)
return;
tileEntity.a((NBTTagCompound) newCompound);
tileEntity.update();
nmsWorld.notify(tileEntity.x, tileEntity.y, tileEntity.z);
}
}
@Override
public ItemStack getItemWithNBT(ItemStack itemStack, Object compound) {
if (compound instanceof NBTTagCompound) {
if (itemStack.getType() == Material.AIR || itemStack == null)
return null;
if (compound == null || compound.toString().equals("{}"))
return itemStack;
net.minecraft.server.v1_7_R4.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
nmsItem.setTag((NBTTagCompound) compound);
ItemStack newItem = CraftItemStack.asCraftMirror(nmsItem);
return newItem;
}
return null;
}
@Override
public NBTTagCompound getFileNBT(File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException ex) {
return null; //File doesn't exist.
}
NBTTagCompound fileNBT = null;
try {
fileNBT = NBTCompressedStreamTools.a(fis);
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return fileNBT;
}
@Override
public void setFileNBT(File file, Object newCompound) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
try {
NBTCompressedStreamTools.a((NBTTagCompound) newCompound, os);
os.close();
} catch (Exception ex) {
if (ex instanceof EOFException) {
; //Ignore, just end of the file
} else {
ex.printStackTrace();
}
} finally {
try {
os.close();
} catch (Exception ex) {
if (ex instanceof EOFException) {
; //Ignore.
} else {
ex.printStackTrace();
}
}
}
}
public NBTTagByte convertToNBT(byte b) {
return new NBTTagByte(b);
}
public NBTTagShort convertToNBT(short s) {
return new NBTTagShort(s);
}
public NBTTagInt convertToNBT(int i) {
return new NBTTagInt(i);
}
public NBTTagLong convertToNBT(long l) {
return new NBTTagLong(l);
}
public NBTTagFloat convertToNBT(float f) {
return new NBTTagFloat(f);
}
public NBTTagDouble convertToNBT(double d) {
return new NBTTagDouble(d);
}
public NBTTagString convertToNBT(String string) {
return new NBTTagString(string);
}
@Override
public Object convertToNBT(Number number) {
if (number instanceof Byte)
return convertToNBT((byte) number);
else if (number instanceof Short)
return convertToNBT((short) number);
else if (number instanceof Integer)
return convertToNBT((int) number);
else if (number instanceof Long)
return convertToNBT((long) number);
else if (number instanceof Float)
return convertToNBT((float) number);
else if (number instanceof Double)
return convertToNBT((double) number);
return number;
}
}

View File

@@ -1,434 +1,554 @@
package me.TheBukor.SkStuff.util;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_8_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.coll.CollectionUtils;
import net.minecraft.server.v1_8_R1.BlockPosition;
import net.minecraft.server.v1_8_R1.EntityInsentient;
import net.minecraft.server.v1_8_R1.MojangsonParser;
import net.minecraft.server.v1_8_R1.NBTBase;
import net.minecraft.server.v1_8_R1.NBTCompressedStreamTools;
import net.minecraft.server.v1_8_R1.NBTTagCompound;
import net.minecraft.server.v1_8_R1.NBTTagDouble;
import net.minecraft.server.v1_8_R1.NBTTagEnd;
import net.minecraft.server.v1_8_R1.NBTTagFloat;
import net.minecraft.server.v1_8_R1.NBTTagInt;
import net.minecraft.server.v1_8_R1.NBTTagList;
import net.minecraft.server.v1_8_R1.NBTTagString;
import net.minecraft.server.v1_8_R1.PathfinderGoal;
import net.minecraft.server.v1_8_R1.PathfinderGoalSelector;
import net.minecraft.server.v1_8_R1.TileEntity;
import net.minecraft.server.v1_8_R1.World;
public class NMS_v1_8_R1 implements NMSInterface {
@Override
public void addToCompound(Object compound, Object toAdd) {
if (compound instanceof NBTTagCompound && toAdd instanceof NBTTagCompound) {
((NBTTagCompound) compound).a((NBTTagCompound) toAdd);
}
}
@Override
public void removeFromCompound(Object compound, String ... toRemove) {
if (compound instanceof NBTTagCompound) {
((NBTTagCompound) compound).remove(toRemove.toString()); //FIXME
}
}
@Override
public Object parseRawNBT(String rawNBT) {
NBTTagCompound parsedNBT = null;
parsedNBT = MojangsonParser.parse(rawNBT);
return parsedNBT;
}
@Override
public int getContentsId(Object nbtList) {
if (nbtList instanceof NBTTagList) {
return ((NBTTagList) nbtList).f();
}
return 0;
}
@Override
public Object[] getContents(Object nbtList) {
if (nbtList instanceof NBTTagList) {
List<Object> contents = new ArrayList<Object>();
for (int i = 0; i < ((NBTTagList) nbtList).size(); i++) {
if (getIndex(nbtList, i) != null) {
contents.add(getIndex(nbtList, i));
}
}
return contents.toArray();
}
return null;
}
@Override
public void addToList(Object nbtList, Object toAdd) {
if (nbtList instanceof NBTTagList && toAdd instanceof NBTBase) {
((NBTTagList) nbtList).add((NBTBase) toAdd);
}
}
@Override
public void removeFromList(Object nbtList, int index) {
if (nbtList instanceof NBTTagList) {
((NBTTagList) nbtList).a(index);
}
}
@Override
public void setIndex(Object nbtList, int index, Object toSet) {
if (nbtList instanceof NBTTagList && toSet instanceof NBTBase) {
((NBTTagList) nbtList).a(index, (NBTBase) toSet);
}
}
@Override
public Object getIndex(Object nbtList, int index) {
if (nbtList instanceof NBTTagList) {
NBTBase value = ((NBTTagList) nbtList).g(index);
if (value instanceof NBTTagEnd)
return null;
else
return value;
}
return null;
}
@Override
public void removePathfinderGoal(Object entity, Class<?> goalClass, boolean isTargetSelector) {
if (entity instanceof EntityInsentient) {
((EntityInsentient) entity).setGoalTarget(null);
if (isTargetSelector) {
Iterator<?> goals = ((List<?>) ReflectionUtils.getField("b", PathfinderGoalSelector.class, ((EntityInsentient) entity).targetSelector)).iterator();
while (goals.hasNext()) {
Object goal = goals.next();
if (ReflectionUtils.getField("a", goal.getClass(), goal).getClass() == goalClass) {
goals.remove();
}
}
} else {
Iterator<?> goals = ((List<?>) ReflectionUtils.getField("b", PathfinderGoalSelector.class, ((EntityInsentient) entity).goalSelector)).iterator();
while (goals.hasNext()) {
Object goal = goals.next();
if (ReflectionUtils.getField("a", goal.getClass(), goal).getClass() == goalClass) {
goals.remove();
}
}
}
}
}
@Override
public void addPathfinderGoal(Object entity, int priority, Object goal, boolean isTargetSelector) {
if (entity instanceof EntityInsentient && goal instanceof PathfinderGoal) {
if (isTargetSelector)
((EntityInsentient) entity).targetSelector.a(priority, (PathfinderGoal) goal);
else
((EntityInsentient) entity).goalSelector.a(priority, (PathfinderGoal) goal);
}
}
@Override
public void registerCompoundClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagCompound>(NBTTagCompound.class, "compound").user("((nbt)?( ?tag)?) ?compounds?").name("NBT Compound").changer(new Changer<NBTTagCompound>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE || mode == ChangeMode.SET) {
return CollectionUtils.array(String[].class, NBTTagCompound[].class);
}
return null;
}
@Override
public void change(NBTTagCompound[] NBT, @Nullable Object[] delta, ChangeMode mode) {
if (mode == ChangeMode.SET) {
if (delta[0] instanceof NBTTagCompound) {
NBT[0] = (NBTTagCompound) delta[0];
} else {
NBTTagCompound parsedNBT = (NBTTagCompound) parseRawNBT((String) delta[0]);
NBT[0] = parsedNBT;
}
} else if (mode == ChangeMode.ADD) {
if (delta[0] instanceof String) {
NBTTagCompound parsedNBT = (NBTTagCompound) parseRawNBT((String) delta[0]);
addToCompound(NBT[0], parsedNBT);
} else {
addToCompound(NBT[0], delta[0]);
}
} else if (mode == ChangeMode.REMOVE) {
if (delta[0] instanceof NBTTagCompound)
return;
for (Object s : delta) {
NBT[0].remove((String) s);
}
}
}
}).parser(new Parser<NBTTagCompound>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagCompound parse(String rawNBT, ParseContext context) {
if (rawNBT.startsWith("nbt:{") && rawNBT.endsWith("}")) {
rawNBT.substring(4);
rawNBT.substring(4);
NBTTagCompound NBT = (NBTTagCompound) parseRawNBT(rawNBT);
if (NBT.toString().equals("{}")) {
return null;
}
return NBT;
}
return null;
}
@Override
public String toString(NBTTagCompound compound, int arg1) {
return compound.toString();
}
@Override
public String toVariableNameString(NBTTagCompound compound) {
return "nbt:" + compound.toString();
}
}));
}
@Override
public void registerNBTListClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagList>(NBTTagList.class, "nbtlist").user("nbt ?list ?(tag)?").name("NBT List").changer(new Changer<NBTTagList>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.SET || mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
return CollectionUtils.array(Float[].class, Double[].class, String[].class, NBTTagCompound[].class, Integer[].class, NBTTagList[].class);
}
return null;
}
@Override
public void change(NBTTagList[] nbtList, @Nullable Object[] delta, ChangeMode mode) {
int typeId = 0;
if (delta instanceof Float[]) {
typeId = 5;
} else if (delta instanceof Double[]) {
typeId = 6;
} else if (delta instanceof String[]) {
typeId = 8;
} else if (delta instanceof NBTTagList[]) {
typeId = 9;
} else if (delta instanceof NBTTagCompound[]) {
typeId = 10;
} else if (delta instanceof Integer[]) {
typeId = 11;
} else {
return;
}
if (mode == ChangeMode.SET) {
if (typeId == 9)
nbtList[0] = (NBTTagList) delta[0];
} else if (mode == ChangeMode.ADD) {
if (getContentsId(nbtList[0]) == typeId) {
if (typeId == 5) {
NBTTagFloat floatTag = new NBTTagFloat((float) delta[0]);
addToList(nbtList[0], floatTag);
} else if (typeId == 6) {
NBTTagDouble doubleTag = new NBTTagDouble((double) delta[0]);
addToList(nbtList[0], doubleTag);
} else if (typeId == 8) {
NBTTagString stringTag = new NBTTagString((String) delta [0]);
addToList(nbtList[0], stringTag);
} else if (typeId == 10) {
addToList(nbtList[0], delta[0]);
} else if (typeId == 11) {
NBTTagInt intTag = new NBTTagInt((int) delta[0]);
addToList(nbtList[0], intTag);
}
}
} else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
nbtList[0] = new NBTTagList();
}
}
}).parser(new Parser<NBTTagList>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagList parse(String ignored, ParseContext context) {
return null;
}
@Override
public String toString(NBTTagList nbtList, int arg1) {
return nbtList.toString();
}
@Override
public String toVariableNameString(NBTTagList nbtList) {
return nbtList.toString();
}
}));
}
@Override
public Object getEntityNBT(Entity entity) {
net.minecraft.server.v1_8_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle();
NBTTagCompound NBT = new NBTTagCompound();
nmsEntity.e(NBT);
return NBT;
}
@Override
public Object getTileNBT(Block block) {
NBTTagCompound NBT = new NBTTagCompound();
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
if (tileEntity == null)
return null;
tileEntity.b(NBT);
return NBT;
}
@Override
public Object getItemNBT(ItemStack itemStack) {
if (itemStack.getType() == Material.AIR)
return null;
NBTTagCompound NBT = new NBTTagCompound();
net.minecraft.server.v1_8_R1.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
NBT = nmsItem.getTag();
if (NBT == null || NBT.toString().equals("{}")) //Null or empty.
return null;
return new Object[] { NBT };
}
@Override
public void setEntityNBT(Entity entity, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
net.minecraft.server.v1_8_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle();
nmsEntity.f((NBTTagCompound) newCompound);
}
}
@Override
public void setTileNBT(Block block, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
if (tileEntity == null)
return;
tileEntity.a((NBTTagCompound) newCompound);
tileEntity.update();
nmsWorld.notify(tileEntity.getPosition());
}
}
@Override
public ItemStack getItemWithNBT(ItemStack itemStack, Object compound) {
if (compound instanceof NBTTagCompound) {
if (itemStack.getType() == Material.AIR || itemStack == null)
return null;
if (compound == null || compound.toString().equals("{}"))
return itemStack;
net.minecraft.server.v1_8_R1.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
nmsItem.setTag((NBTTagCompound) compound);
ItemStack newItem = CraftItemStack.asCraftMirror(nmsItem);
return newItem;
}
return null;
}
@Override
public Object getFileNBT(File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException ex) {
return null; //File doesn't exist.
}
NBTTagCompound fileNBT = null;
try {
fileNBT = NBTCompressedStreamTools.a(fis);
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return fileNBT;
}
@Override
public void setFileNBT(File file, Object newCompound) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
try {
NBTCompressedStreamTools.a((NBTTagCompound) newCompound, os);
os.close();
} catch (Exception ex) {
if (ex instanceof EOFException) {
; //Ignore, just end of the file
} else {
ex.printStackTrace();
}
} finally {
try {
os.close();
} catch (Exception ex) {
if (ex instanceof EOFException) {
; //Ignore.
} else {
ex.printStackTrace();
}
}
}
}
package me.TheBukor.SkStuff.util;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.OutputStream;
import java.io.StreamCorruptedException;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_8_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.coll.CollectionUtils;
import ch.njol.yggdrasil.Fields;
import net.minecraft.server.v1_8_R1.BlockPosition;
import net.minecraft.server.v1_8_R1.EntityInsentient;
import net.minecraft.server.v1_8_R1.MojangsonParser;
import net.minecraft.server.v1_8_R1.NBTBase;
import net.minecraft.server.v1_8_R1.NBTCompressedStreamTools;
import net.minecraft.server.v1_8_R1.NBTTagByte;
import net.minecraft.server.v1_8_R1.NBTTagCompound;
import net.minecraft.server.v1_8_R1.NBTTagDouble;
import net.minecraft.server.v1_8_R1.NBTTagFloat;
import net.minecraft.server.v1_8_R1.NBTTagInt;
import net.minecraft.server.v1_8_R1.NBTTagList;
import net.minecraft.server.v1_8_R1.NBTTagLong;
import net.minecraft.server.v1_8_R1.NBTTagShort;
import net.minecraft.server.v1_8_R1.NBTTagString;
import net.minecraft.server.v1_8_R1.PathfinderGoal;
import net.minecraft.server.v1_8_R1.PathfinderGoalSelector;
import net.minecraft.server.v1_8_R1.TileEntity;
import net.minecraft.server.v1_8_R1.World;
public class NMS_v1_8_R1 implements NMSInterface {
@Override
public void addToCompound(Object compound, Object toAdd) {
if (compound instanceof NBTTagCompound && toAdd instanceof NBTTagCompound) {
((NBTTagCompound) compound).a((NBTTagCompound) toAdd);
}
}
@Override
public void removeFromCompound(Object compound, String ... toRemove) {
if (compound instanceof NBTTagCompound) {
for (String s : toRemove) {
((NBTTagCompound) compound).remove(s);
}
}
}
@Override
public NBTTagCompound parseRawNBT(String rawNBT) {
NBTTagCompound parsedNBT = null;
parsedNBT = MojangsonParser.parse(rawNBT);
return parsedNBT;
}
@Override
public int getContentsId(Object nbtList) {
if (nbtList instanceof NBTTagList) {
return ((NBTTagList) nbtList).f();
}
return 0;
}
@Override
public Object[] getContents(Object nbtList) {
if (nbtList instanceof NBTTagList) {
Object[] contents = new Object[((NBTTagList) nbtList).size()];
for (int i = 0; i < ((NBTTagList) nbtList).size(); i++) {
if (getIndex(nbtList, i) != null) {
contents[i] = getIndex(nbtList, i);
}
}
return contents;
}
return null;
}
@Override
public void addToList(Object nbtList, Object toAdd) {
if (nbtList instanceof NBTTagList && toAdd instanceof NBTBase) {
((NBTTagList) nbtList).add((NBTBase) toAdd);
}
}
@Override
public void removeFromList(Object nbtList, int index) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
((NBTTagList) nbtList).a(index);
}
}
@Override
public void setIndex(Object nbtList, int index, Object toSet) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
if (toSet instanceof NBTBase) {
((NBTTagList) nbtList).a(index, (NBTBase) toSet);
} else if (toSet instanceof Number) {
((NBTTagList) nbtList).a(index, convertToNBT((Number) toSet));
} else if (toSet instanceof String) {
((NBTTagList) nbtList).a(index, convertToNBT((String) toSet));
}
}
}
@Override
public Object getIndex(Object nbtList, int index) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
NBTBase value = ((NBTTagList) nbtList).g(index);
if (value instanceof NBTTagByte) {
return ((NBTTagByte) value).f(); //Byte stored inside a NBTNumber
} else if (value instanceof NBTTagShort) {
return ((NBTTagShort) value).e(); //Short inside a NBTNumber
} else if (value instanceof NBTTagInt) {
return ((NBTTagInt) value).d(); //Integer inside a NBTNumber
} else if (value instanceof NBTTagLong) {
return ((NBTTagLong) value).c(); //Long inside a NBTNumber
} else if (value instanceof NBTTagFloat) {
return ((NBTTagFloat) value).h(); //Float inside a NBTNumber
} else if (value instanceof NBTTagDouble) {
return ((NBTTagDouble) value).g(); //Double inside a NBTNumber
} else if (value instanceof NBTTagString) {
return ((NBTTagString) value).a_(); //String inside the NBTTagString
} else if (value instanceof NBTTagList || value instanceof NBTTagCompound) {
return value; //No need to convert anything, these are registered by the addon.
}
}
return null;
}
@Override
public void removePathfinderGoal(Object entity, Class<?> goalClass, boolean isTargetSelector) {
if (entity instanceof EntityInsentient) {
((EntityInsentient) entity).setGoalTarget(null);
if (isTargetSelector) {
Iterator<?> goals = ((List<?>) ReflectionUtils.getField("b", PathfinderGoalSelector.class, ((EntityInsentient) entity).targetSelector)).iterator();
while (goals.hasNext()) {
Object goal = goals.next();
if (ReflectionUtils.getField("a", goal.getClass(), goal).getClass() == goalClass) {
goals.remove();
}
}
} else {
Iterator<?> goals = ((List<?>) ReflectionUtils.getField("b", PathfinderGoalSelector.class, ((EntityInsentient) entity).goalSelector)).iterator();
while (goals.hasNext()) {
Object goal = goals.next();
if (ReflectionUtils.getField("a", goal.getClass(), goal).getClass() == goalClass) {
goals.remove();
}
}
}
}
}
@Override
public void addPathfinderGoal(Object entity, int priority, Object goal, boolean isTargetSelector) {
if (entity instanceof EntityInsentient && goal instanceof PathfinderGoal) {
if (isTargetSelector)
((EntityInsentient) entity).targetSelector.a(priority, (PathfinderGoal) goal);
else
((EntityInsentient) entity).goalSelector.a(priority, (PathfinderGoal) goal);
}
}
@Override
public void registerCompoundClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagCompound>(NBTTagCompound.class, "compound").user("((nbt)?( ?tag)?) ?compounds?").name("NBT Compound").changer(new Changer<NBTTagCompound>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE || mode == ChangeMode.SET) {
return CollectionUtils.array(String[].class, NBTTagCompound[].class);
}
return null;
}
@Override
public void change(NBTTagCompound[] NBT, @Nullable Object[] delta, ChangeMode mode) {
if (mode == ChangeMode.SET) {
if (delta[0] instanceof NBTTagCompound) {
NBT[0] = (NBTTagCompound) delta[0];
} else {
NBTTagCompound parsedNBT = null;
parsedNBT = parseRawNBT((String) delta[0]);
NBT[0] = parsedNBT;
}
} else if (mode == ChangeMode.ADD) {
if (delta[0] instanceof String) {
NBTTagCompound parsedNBT = null;
parsedNBT = parseRawNBT((String) delta[0]);
addToCompound(NBT[0], parsedNBT);
} else {
addToCompound(NBT[0], delta[0]);
}
} else if (mode == ChangeMode.REMOVE) {
if (delta[0] instanceof NBTTagCompound)
return;
for (Object s : delta) {
NBT[0].remove((String) s);
}
}
}
}).parser(new Parser<NBTTagCompound>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagCompound parse(String rawNBT, ParseContext context) {
if (rawNBT.startsWith("nbt:{") && rawNBT.endsWith("}")) {
NBTTagCompound NBT = null;
NBT = parseRawNBT(rawNBT.substring(4));
return NBT;
}
return null;
}
@Override
public String toString(NBTTagCompound compound, int arg1) {
return compound.toString();
}
@Override
public String toVariableNameString(NBTTagCompound compound) {
return "nbt:" + compound.toString();
}
}).serializer(new Serializer<NBTTagCompound>() {
@Override
public Fields serialize(NBTTagCompound compound) throws NotSerializableException {
Fields f = new Fields();
f.putObject("asString", compound.toString());
return f;
}
@Override
public void deserialize(NBTTagCompound compound, Fields f) throws StreamCorruptedException, NotSerializableException {
assert false;
}
@Override
protected boolean canBeInstantiated() {
return false;
}
@Override
protected NBTTagCompound deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
String s = fields.getObject("asString", String.class);
NBTTagCompound compound = null;
compound = parseRawNBT(s);
return compound;
}
@Override
@Nullable
public NBTTagCompound deserialize(String s) {
NBTTagCompound compound = null;
compound = parseRawNBT(s);
return compound;
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
}));
}
@Override
public void registerNBTListClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagList>(NBTTagList.class, "nbtlist").user("nbt ?list ?(tag)?").name("NBT List").changer(new Changer<NBTTagList>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.SET || mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
return CollectionUtils.array(Float[].class, Double[].class, String[].class, NBTTagCompound[].class, Integer[].class, NBTTagList[].class);
}
return null;
}
@Override
public void change(NBTTagList[] nbtList, @Nullable Object[] delta, ChangeMode mode) {
int typeId = 0;
if (delta instanceof Byte[]) {
typeId = 1;
} else if (delta instanceof Short[]) {
typeId = 2;
} else if (delta instanceof Integer[]) {
typeId = 3;
} else if (delta instanceof Long[]) {
typeId = 4;
} else if (delta instanceof Float[]) {
typeId = 5;
} else if (delta instanceof Double[]) {
typeId = 6;
} else if (delta instanceof String[]) {
typeId = 8;
} else if (delta instanceof NBTTagList[]) {
typeId = 9;
} else if (delta instanceof NBTTagCompound[]) {
typeId = 10;
} else {
return;
}
if (mode == ChangeMode.SET) {
if (typeId == 9)
nbtList[0] = (NBTTagList) delta[0];
} else if (mode == ChangeMode.ADD) {
if (getContentsId(nbtList[0]) == typeId) {
if (delta[0] instanceof Number)
addToList(nbtList, convertToNBT((Number) delta[0]));
else if (delta[0] instanceof String)
addToList(nbtList, convertToNBT((String) delta[0]));
else if (delta[0] instanceof NBTTagCompound || delta[0] instanceof NBTTagList)
addToList(nbtList, delta[0]);
}
} else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
nbtList[0] = new NBTTagList();
}
}
}).parser(new Parser<NBTTagList>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagList parse(String listString, ParseContext context) {
if (listString.startsWith("[") && listString.endsWith("]")) {
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:[0:" + listString.substring(1) + "}");
NBTTagList parsedList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return parsedList;
}
return null;
}
@Override
public String toString(NBTTagList nbtList, int arg1) {
return nbtList.toString();
}
@Override
public String toVariableNameString(NBTTagList nbtList) {
return nbtList.toString();
}
}).serializer(new Serializer<NBTTagList>() {
@Override
public Fields serialize(NBTTagList nbtList) throws NotSerializableException {
Fields f = new Fields();
f.putObject("asString", nbtList.toString());
return f;
}
@Override
public void deserialize(NBTTagList nbtList, Fields f) throws StreamCorruptedException, NotSerializableException {
assert false;
}
@Override
protected boolean canBeInstantiated() {
return false;
}
@Override
protected NBTTagList deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
String s = fields.getObject("asString", String.class);
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagList nbtList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return nbtList;
}
@Override
@Nullable
public NBTTagList deserialize(String s) {
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagList nbtList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return nbtList;
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
}));
}
@Override
public NBTTagCompound getEntityNBT(Entity entity) {
net.minecraft.server.v1_8_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle();
NBTTagCompound NBT = new NBTTagCompound();
nmsEntity.e(NBT);
return NBT;
}
@Override
public NBTTagCompound getTileNBT(Block block) {
NBTTagCompound NBT = new NBTTagCompound();
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
if (tileEntity == null)
return null;
tileEntity.b(NBT);
return NBT;
}
@Override
public NBTTagCompound getItemNBT(ItemStack itemStack) {
if (itemStack.getType() == Material.AIR)
return null;
NBTTagCompound itemNBT = CraftItemStack.asNMSCopy(itemStack).getTag();
if (String.valueOf(itemNBT).equals("{}"))
itemNBT = null;
return itemNBT;
}
@Override
public void setEntityNBT(Entity entity, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
net.minecraft.server.v1_8_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle();
nmsEntity.f((NBTTagCompound) newCompound);
}
}
@Override
public void setTileNBT(Block block, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
if (tileEntity == null)
return;
tileEntity.a((NBTTagCompound) newCompound);
tileEntity.update();
nmsWorld.notify(tileEntity.getPosition());
}
}
@Override
public ItemStack getItemWithNBT(ItemStack itemStack, Object compound) {
net.minecraft.server.v1_8_R1.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
if (compound instanceof NBTTagCompound && itemStack != null) {
if (itemStack.getType() == Material.AIR)
return null;
if (((NBTTagCompound) compound).isEmpty())
return itemStack;
nmsItem.setTag((NBTTagCompound) compound);
ItemStack newItem = CraftItemStack.asBukkitCopy(nmsItem);
return newItem;
} else if (compound == null) {
nmsItem.setTag(null);
ItemStack newItem = CraftItemStack.asBukkitCopy(nmsItem);
return newItem;
}
return itemStack;
}
@Override
public NBTTagCompound getFileNBT(File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException ex) {
return null; //File doesn't exist.
}
NBTTagCompound fileNBT = null;
try {
fileNBT = NBTCompressedStreamTools.a(fis);
fis.close();
} catch (IOException ex) {
if (ex instanceof EOFException) {
; //Nothing.
} else {
ex.printStackTrace();
}
}
return fileNBT;
}
@Override
public void setFileNBT(File file, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
try {
NBTCompressedStreamTools.a((NBTTagCompound) newCompound, os);
os.close();
} catch (IOException ex) {
if (ex instanceof EOFException) {
; //Ignore, just end of the file
} else {
ex.printStackTrace();
}
}
}
}
public NBTBase convertToNBT(Number number) {
if (number instanceof Byte) {
return new NBTTagByte((byte) number);
} else if (number instanceof Short) {
return new NBTTagShort((short) number);
} else if (number instanceof Integer) {
return new NBTTagInt((int) number);
} else if (number instanceof Long) {
return new NBTTagLong((long) number);
} else if (number instanceof Float) {
return new NBTTagFloat((float) number);
} else if (number instanceof Double) {
return new NBTTagDouble((double) number);
}
return null;
}
public NBTTagString convertToNBT(String string) {
return new NBTTagString(string);
}
}

View File

@@ -1,439 +1,560 @@
package me.TheBukor.SkStuff.util;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_8_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R2.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.coll.CollectionUtils;
import net.minecraft.server.v1_8_R2.BlockPosition;
import net.minecraft.server.v1_8_R2.EntityInsentient;
import net.minecraft.server.v1_8_R2.MojangsonParseException;
import net.minecraft.server.v1_8_R2.MojangsonParser;
import net.minecraft.server.v1_8_R2.NBTBase;
import net.minecraft.server.v1_8_R2.NBTCompressedStreamTools;
import net.minecraft.server.v1_8_R2.NBTTagCompound;
import net.minecraft.server.v1_8_R2.NBTTagDouble;
import net.minecraft.server.v1_8_R2.NBTTagEnd;
import net.minecraft.server.v1_8_R2.NBTTagFloat;
import net.minecraft.server.v1_8_R2.NBTTagInt;
import net.minecraft.server.v1_8_R2.NBTTagList;
import net.minecraft.server.v1_8_R2.NBTTagString;
import net.minecraft.server.v1_8_R2.PathfinderGoal;
import net.minecraft.server.v1_8_R2.PathfinderGoalSelector;
import net.minecraft.server.v1_8_R2.TileEntity;
import net.minecraft.server.v1_8_R2.World;
public class NMS_v1_8_R2 implements NMSInterface {
@Override
public void addToCompound(Object compound, Object toAdd) {
if (compound instanceof NBTTagCompound && toAdd instanceof NBTTagCompound) {
((NBTTagCompound) compound).a((NBTTagCompound) toAdd);
}
}
@Override
public void removeFromCompound(Object compound, String ... toRemove) {
if (compound instanceof NBTTagCompound) {
((NBTTagCompound) compound).remove(toRemove.toString()); //FIXME
}
}
@Override
public Object parseRawNBT(String rawNBT) {
NBTTagCompound parsedNBT = null;
try {
parsedNBT = MojangsonParser.parse(rawNBT);
} catch (MojangsonParseException ex) {
Skript.warning("Error when parsing NBT - " + ex.getMessage());
}
return parsedNBT;
}
@Override
public int getContentsId(Object nbtList) {
if (nbtList instanceof NBTTagList) {
return ((NBTTagList) nbtList).f();
}
return 0;
}
@Override
public Object[] getContents(Object nbtList) {
if (nbtList instanceof NBTTagList) {
List<Object> contents = new ArrayList<Object>();
for (int i = 0; i < ((NBTTagList) nbtList).size(); i++) {
if (getIndex(nbtList, i) != null) {
contents.add(getIndex(nbtList, i));
}
}
return contents.toArray();
}
return null;
}
@Override
public void addToList(Object nbtList, Object toAdd) {
if (nbtList instanceof NBTTagList && toAdd instanceof NBTBase) {
((NBTTagList) nbtList).add((NBTBase) toAdd);
}
}
@Override
public void removeFromList(Object nbtList, int index) {
if (nbtList instanceof NBTTagList) {
((NBTTagList) nbtList).a(index);
}
}
@Override
public void setIndex(Object nbtList, int index, Object toSet) {
if (nbtList instanceof NBTTagList && toSet instanceof NBTBase) {
((NBTTagList) nbtList).a(index, (NBTBase) toSet);
}
}
@Override
public Object getIndex(Object nbtList, int index) {
if (nbtList instanceof NBTTagList) {
NBTBase value = ((NBTTagList) nbtList).g(index);
if (value instanceof NBTTagEnd)
return null;
else
return value;
}
return null;
}
@Override
public void removePathfinderGoal(Object entity, Class<?> goalClass, boolean isTargetSelector) {
if (entity instanceof EntityInsentient) {
((EntityInsentient) entity).setGoalTarget(null);
if (isTargetSelector) {
Iterator<?> goals = ((List<?>) ReflectionUtils.getField("b", PathfinderGoalSelector.class, ((EntityInsentient) entity).targetSelector)).iterator();
while (goals.hasNext()) {
Object goal = goals.next();
if (ReflectionUtils.getField("a", goal.getClass(), goal).getClass() == goalClass) {
goals.remove();
}
}
} else {
Iterator<?> goals = ((List<?>) ReflectionUtils.getField("b", PathfinderGoalSelector.class, ((EntityInsentient) entity).goalSelector)).iterator();
while (goals.hasNext()) {
Object goal = goals.next();
if (ReflectionUtils.getField("a", goal.getClass(), goal).getClass() == goalClass) {
goals.remove();
}
}
}
}
}
@Override
public void addPathfinderGoal(Object entity, int priority, Object goal, boolean isTargetSelector) {
if (entity instanceof EntityInsentient && goal instanceof PathfinderGoal) {
if (isTargetSelector)
((EntityInsentient) entity).targetSelector.a(priority, (PathfinderGoal) goal);
else
((EntityInsentient) entity).goalSelector.a(priority, (PathfinderGoal) goal);
}
}
@Override
public void registerCompoundClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagCompound>(NBTTagCompound.class, "compound").user("((nbt)?( ?tag)?) ?compounds?").name("NBT Compound").changer(new Changer<NBTTagCompound>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE || mode == ChangeMode.SET) {
return CollectionUtils.array(String[].class, NBTTagCompound[].class);
}
return null;
}
@Override
public void change(NBTTagCompound[] NBT, @Nullable Object[] delta, ChangeMode mode) {
if (mode == ChangeMode.SET) {
if (delta[0] instanceof NBTTagCompound) {
NBT[0] = (NBTTagCompound) delta[0];
} else {
NBTTagCompound parsedNBT = (NBTTagCompound) parseRawNBT((String) delta[0]);
NBT[0] = parsedNBT;
}
} else if (mode == ChangeMode.ADD) {
if (delta[0] instanceof String) {
NBTTagCompound parsedNBT = (NBTTagCompound) parseRawNBT((String) delta[0]);
addToCompound(NBT[0], parsedNBT);
} else {
addToCompound(NBT[0], delta[0]);
}
} else if (mode == ChangeMode.REMOVE) {
if (delta[0] instanceof NBTTagCompound)
return;
for (Object s : delta) {
NBT[0].remove((String) s);
}
}
}
}).parser(new Parser<NBTTagCompound>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagCompound parse(String rawNBT, ParseContext context) {
if (rawNBT.startsWith("nbt:{") && rawNBT.endsWith("}")) {
rawNBT.substring(4);
NBTTagCompound NBT = (NBTTagCompound) parseRawNBT(rawNBT);
if (NBT.toString().equals("{}")) {
return null;
}
return NBT;
}
return null;
}
@Override
public String toString(NBTTagCompound compound, int arg1) {
return compound.toString();
}
@Override
public String toVariableNameString(NBTTagCompound compound) {
return "nbt:" + compound.toString();
}
}));
}
@Override
public void registerNBTListClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagList>(NBTTagList.class, "nbtlist").user("nbt ?list ?(tag)?").name("NBT List").changer(new Changer<NBTTagList>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.SET || mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
return CollectionUtils.array(Float[].class, Double[].class, String[].class, NBTTagCompound[].class, Integer[].class, NBTTagList[].class);
}
return null;
}
@Override
public void change(NBTTagList[] nbtList, @Nullable Object[] delta, ChangeMode mode) {
int typeId = 0;
if (delta instanceof Float[]) {
typeId = 5;
} else if (delta instanceof Double[]) {
typeId = 6;
} else if (delta instanceof String[]) {
typeId = 8;
} else if (delta instanceof NBTTagList[]) {
typeId = 9;
} else if (delta instanceof NBTTagCompound[]) {
typeId = 10;
} else if (delta instanceof Integer[]) {
typeId = 11;
} else {
return;
}
if (mode == ChangeMode.SET) {
if (typeId == 9)
nbtList[0] = (NBTTagList) delta[0];
} else if (mode == ChangeMode.ADD) {
if (getContentsId(nbtList[0]) == typeId) {
if (typeId == 5) {
NBTTagFloat floatTag = new NBTTagFloat((float) delta[0]);
addToList(nbtList[0], floatTag);
} else if (typeId == 6) {
NBTTagDouble doubleTag = new NBTTagDouble((double) delta[0]);
addToList(nbtList[0], doubleTag);
} else if (typeId == 8) {
NBTTagString stringTag = new NBTTagString((String) delta [0]);
addToList(nbtList[0], stringTag);
} else if (typeId == 10) {
addToList(nbtList[0], delta[0]);
} else if (typeId == 11) {
NBTTagInt intTag = new NBTTagInt((int) delta[0]);
addToList(nbtList[0], intTag);
}
}
} else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
nbtList[0] = new NBTTagList();
}
}
}).parser(new Parser<NBTTagList>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagList parse(String ignored, ParseContext context) {
return null;
}
@Override
public String toString(NBTTagList nbtList, int arg1) {
return nbtList.toString();
}
@Override
public String toVariableNameString(NBTTagList nbtList) {
return nbtList.toString();
}
}));
}
@Override
public Object getEntityNBT(Entity entity) {
net.minecraft.server.v1_8_R2.Entity nmsEntity = ((CraftEntity) entity).getHandle();
NBTTagCompound NBT = new NBTTagCompound();
nmsEntity.e(NBT);
return NBT;
}
@Override
public Object getTileNBT(Block block) {
NBTTagCompound NBT = new NBTTagCompound();
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
if (tileEntity == null)
return null;
tileEntity.b(NBT);
return NBT;
}
@Override
public Object getItemNBT(ItemStack itemStack) {
if (itemStack.getType() == Material.AIR)
return null;
NBTTagCompound NBT = new NBTTagCompound();
net.minecraft.server.v1_8_R2.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
NBT = nmsItem.getTag();
if (NBT == null || NBT.toString().equals("{}")) //Null or empty.
return null;
return new Object[] { NBT };
}
@Override
public void setEntityNBT(Entity entity, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
net.minecraft.server.v1_8_R2.Entity nmsEntity = ((CraftEntity) entity).getHandle();
nmsEntity.f((NBTTagCompound) newCompound);
}
}
@Override
public void setTileNBT(Block block, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
if (tileEntity == null)
return;
tileEntity.a((NBTTagCompound) newCompound);
tileEntity.update();
nmsWorld.notify(tileEntity.getPosition());
}
}
@Override
public ItemStack getItemWithNBT(ItemStack itemStack, Object compound) {
if (compound instanceof NBTTagCompound) {
if (itemStack.getType() == Material.AIR || itemStack == null)
return null;
if (compound == null || compound.toString().equals("{}"))
return itemStack;
net.minecraft.server.v1_8_R2.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
nmsItem.setTag((NBTTagCompound) compound);
ItemStack newItem = CraftItemStack.asCraftMirror(nmsItem);
return newItem;
}
return null;
}
@Override
public Object getFileNBT(File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException ex) {
return null; //File doesn't exist.
}
NBTTagCompound fileNBT = null;
try {
fileNBT = NBTCompressedStreamTools.a(fis);
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return fileNBT;
}
@Override
public void setFileNBT(File file, Object newCompound) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
try {
NBTCompressedStreamTools.a((NBTTagCompound) newCompound, os);
os.close();
} catch (Exception ex) {
if (ex instanceof EOFException) {
; //Ignore, just end of the file
} else {
ex.printStackTrace();
}
} finally {
try {
os.close();
} catch (Exception ex) {
if (ex instanceof EOFException) {
; //Ignore.
} else {
ex.printStackTrace();
}
}
}
}
package me.TheBukor.SkStuff.util;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.OutputStream;
import java.io.StreamCorruptedException;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_8_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R2.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.coll.CollectionUtils;
import ch.njol.yggdrasil.Fields;
import net.minecraft.server.v1_8_R2.BlockPosition;
import net.minecraft.server.v1_8_R2.EntityInsentient;
import net.minecraft.server.v1_8_R2.MojangsonParseException;
import net.minecraft.server.v1_8_R2.MojangsonParser;
import net.minecraft.server.v1_8_R2.NBTBase;
import net.minecraft.server.v1_8_R2.NBTCompressedStreamTools;
import net.minecraft.server.v1_8_R2.NBTTagByte;
import net.minecraft.server.v1_8_R2.NBTTagCompound;
import net.minecraft.server.v1_8_R2.NBTTagDouble;
import net.minecraft.server.v1_8_R2.NBTTagFloat;
import net.minecraft.server.v1_8_R2.NBTTagInt;
import net.minecraft.server.v1_8_R2.NBTTagList;
import net.minecraft.server.v1_8_R2.NBTTagLong;
import net.minecraft.server.v1_8_R2.NBTTagShort;
import net.minecraft.server.v1_8_R2.NBTTagString;
import net.minecraft.server.v1_8_R2.PathfinderGoal;
import net.minecraft.server.v1_8_R2.PathfinderGoalSelector;
import net.minecraft.server.v1_8_R2.TileEntity;
import net.minecraft.server.v1_8_R2.World;
public class NMS_v1_8_R2 implements NMSInterface {
@Override
public void addToCompound(Object compound, Object toAdd) {
if (compound instanceof NBTTagCompound && toAdd instanceof NBTTagCompound) {
((NBTTagCompound) compound).a((NBTTagCompound) toAdd);
}
}
@Override
public void removeFromCompound(Object compound, String ... toRemove) {
if (compound instanceof NBTTagCompound) {
for (String s : toRemove) {
((NBTTagCompound) compound).remove(s);
}
}
}
@Override
public NBTTagCompound parseRawNBT(String rawNBT) {
NBTTagCompound parsedNBT = null;
try {
parsedNBT = MojangsonParser.parse(rawNBT);
} catch (MojangsonParseException ex) {
Skript.warning("Error when parsing NBT - " + ex.getMessage());
}
return parsedNBT;
}
@Override
public int getContentsId(Object nbtList) {
if (nbtList instanceof NBTTagList) {
return ((NBTTagList) nbtList).f();
}
return 0;
}
@Override
public Object[] getContents(Object nbtList) {
if (nbtList instanceof NBTTagList) {
Object[] contents = new Object[((NBTTagList) nbtList).size()];
for (int i = 0; i < ((NBTTagList) nbtList).size(); i++) {
if (getIndex(nbtList, i) != null) {
contents[i] = getIndex(nbtList, i);
}
}
return contents;
}
return null;
}
@Override
public void addToList(Object nbtList, Object toAdd) {
if (nbtList instanceof NBTTagList && toAdd instanceof NBTBase) {
((NBTTagList) nbtList).add((NBTBase) toAdd);
}
}
@Override
public void removeFromList(Object nbtList, int index) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
((NBTTagList) nbtList).a(index);
}
}
@Override
public void setIndex(Object nbtList, int index, Object toSet) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
if (toSet instanceof NBTBase) {
((NBTTagList) nbtList).a(index, (NBTBase) toSet);
} else if (toSet instanceof Number) {
((NBTTagList) nbtList).a(index, convertToNBT((Number) toSet));
} else if (toSet instanceof String) {
((NBTTagList) nbtList).a(index, convertToNBT((String) toSet));
}
}
}
@Override
public Object getIndex(Object nbtList, int index) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
NBTBase value = ((NBTTagList) nbtList).g(index);
if (value instanceof NBTTagByte) {
return ((NBTTagByte) value).f(); //Byte stored inside a NBTNumber
} else if (value instanceof NBTTagShort) {
return ((NBTTagShort) value).e(); //Short inside a NBTNumber
} else if (value instanceof NBTTagInt) {
return ((NBTTagInt) value).d(); //Integer inside a NBTNumber
} else if (value instanceof NBTTagLong) {
return ((NBTTagLong) value).c(); //Long inside a NBTNumber
} else if (value instanceof NBTTagFloat) {
return ((NBTTagFloat) value).h(); //Float inside a NBTNumber
} else if (value instanceof NBTTagDouble) {
return ((NBTTagDouble) value).g(); //Double inside a NBTNumber
} else if (value instanceof NBTTagString) {
return ((NBTTagString) value).a_(); //String inside the NBTTagString
} else if (value instanceof NBTTagList || value instanceof NBTTagCompound) {
return value; //No need to convert anything, these are registered by the addon.
}
}
return null;
}
@Override
public void removePathfinderGoal(Object entity, Class<?> goalClass, boolean isTargetSelector) {
if (entity instanceof EntityInsentient) {
((EntityInsentient) entity).setGoalTarget(null);
if (isTargetSelector) {
Iterator<?> goals = ((List<?>) ReflectionUtils.getField("b", PathfinderGoalSelector.class, ((EntityInsentient) entity).targetSelector)).iterator();
while (goals.hasNext()) {
Object goal = goals.next();
if (ReflectionUtils.getField("a", goal.getClass(), goal).getClass() == goalClass) {
goals.remove();
}
}
} else {
Iterator<?> goals = ((List<?>) ReflectionUtils.getField("b", PathfinderGoalSelector.class, ((EntityInsentient) entity).goalSelector)).iterator();
while (goals.hasNext()) {
Object goal = goals.next();
if (ReflectionUtils.getField("a", goal.getClass(), goal).getClass() == goalClass) {
goals.remove();
}
}
}
}
}
@Override
public void addPathfinderGoal(Object entity, int priority, Object goal, boolean isTargetSelector) {
if (entity instanceof EntityInsentient && goal instanceof PathfinderGoal) {
if (isTargetSelector)
((EntityInsentient) entity).targetSelector.a(priority, (PathfinderGoal) goal);
else
((EntityInsentient) entity).goalSelector.a(priority, (PathfinderGoal) goal);
}
}
@Override
public void registerCompoundClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagCompound>(NBTTagCompound.class, "compound").user("((nbt)?( ?tag)?) ?compounds?").name("NBT Compound").changer(new Changer<NBTTagCompound>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE || mode == ChangeMode.SET) {
return CollectionUtils.array(String[].class, NBTTagCompound[].class);
}
return null;
}
@Override
public void change(NBTTagCompound[] NBT, @Nullable Object[] delta, ChangeMode mode) {
if (mode == ChangeMode.SET) {
if (delta[0] instanceof NBTTagCompound) {
NBT[0] = (NBTTagCompound) delta[0];
} else {
NBTTagCompound parsedNBT = null;
parsedNBT = parseRawNBT((String) delta[0]);
NBT[0] = parsedNBT;
}
} else if (mode == ChangeMode.ADD) {
if (delta[0] instanceof String) {
NBTTagCompound parsedNBT = null;
parsedNBT = parseRawNBT((String) delta[0]);
addToCompound(NBT[0], parsedNBT);
} else {
addToCompound(NBT[0], delta[0]);
}
} else if (mode == ChangeMode.REMOVE) {
if (delta[0] instanceof NBTTagCompound)
return;
for (Object s : delta) {
NBT[0].remove((String) s);
}
}
}
}).parser(new Parser<NBTTagCompound>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagCompound parse(String rawNBT, ParseContext context) {
if (rawNBT.startsWith("nbt:{") && rawNBT.endsWith("}")) {
NBTTagCompound NBT = null;
NBT = parseRawNBT(rawNBT.substring(4));
return NBT;
}
return null;
}
@Override
public String toString(NBTTagCompound compound, int arg1) {
return compound.toString();
}
@Override
public String toVariableNameString(NBTTagCompound compound) {
return "nbt:" + compound.toString();
}
}).serializer(new Serializer<NBTTagCompound>() {
@Override
public Fields serialize(NBTTagCompound compound) throws NotSerializableException {
Fields f = new Fields();
f.putObject("asString", compound.toString());
return f;
}
@Override
public void deserialize(NBTTagCompound compound, Fields f) throws StreamCorruptedException, NotSerializableException {
assert false;
}
@Override
protected boolean canBeInstantiated() {
return false;
}
@Override
protected NBTTagCompound deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
String s = fields.getObject("asString", String.class);
NBTTagCompound compound = null;
compound = parseRawNBT(s);
return compound;
}
@Override
@Nullable
public NBTTagCompound deserialize(String s) {
NBTTagCompound compound = null;
compound = parseRawNBT(s);
return compound;
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
}));
}
@Override
public void registerNBTListClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagList>(NBTTagList.class, "nbtlist").user("nbt ?list ?(tag)?").name("NBT List").changer(new Changer<NBTTagList>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.SET || mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
return CollectionUtils.array(Float[].class, Double[].class, String[].class, NBTTagCompound[].class, Integer[].class, NBTTagList[].class);
}
return null;
}
@Override
public void change(NBTTagList[] nbtList, @Nullable Object[] delta, ChangeMode mode) {
int typeId = 0;
if (delta instanceof Byte[]) {
typeId = 1;
} else if (delta instanceof Short[]) {
typeId = 2;
} else if (delta instanceof Integer[]) {
typeId = 3;
} else if (delta instanceof Long[]) {
typeId = 4;
} else if (delta instanceof Float[]) {
typeId = 5;
} else if (delta instanceof Double[]) {
typeId = 6;
} else if (delta instanceof String[]) {
typeId = 8;
} else if (delta instanceof NBTTagList[]) {
typeId = 9;
} else if (delta instanceof NBTTagCompound[]) {
typeId = 10;
} else {
return;
}
if (mode == ChangeMode.SET) {
if (typeId == 9)
nbtList[0] = (NBTTagList) delta[0];
} else if (mode == ChangeMode.ADD) {
if (getContentsId(nbtList[0]) == typeId) {
if (delta[0] instanceof Number)
addToList(nbtList, convertToNBT((Number) delta[0]));
else if (delta[0] instanceof String)
addToList(nbtList, convertToNBT((String) delta[0]));
else if (delta[0] instanceof NBTTagCompound || delta[0] instanceof NBTTagList)
addToList(nbtList, delta[0]);
}
} else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
nbtList[0] = new NBTTagList();
}
}
}).parser(new Parser<NBTTagList>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagList parse(String listString, ParseContext context) {
if (listString.startsWith("[") && listString.endsWith("]")) {
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:[0:" + listString.substring(1) + "}");
NBTTagList parsedList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return parsedList;
}
return null;
}
@Override
public String toString(NBTTagList nbtList, int arg1) {
return nbtList.toString();
}
@Override
public String toVariableNameString(NBTTagList nbtList) {
return nbtList.toString();
}
}).serializer(new Serializer<NBTTagList>() {
@Override
public Fields serialize(NBTTagList nbtList) throws NotSerializableException {
Fields f = new Fields();
f.putObject("asString", nbtList.toString());
return f;
}
@Override
public void deserialize(NBTTagList nbtList, Fields f) throws StreamCorruptedException, NotSerializableException {
assert false;
}
@Override
protected boolean canBeInstantiated() {
return false;
}
@Override
protected NBTTagList deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
String s = fields.getObject("asString", String.class);
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagList nbtList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return nbtList;
}
@Override
@Nullable
public NBTTagList deserialize(String s) {
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagList nbtList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return nbtList;
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
}));
}
@Override
public NBTTagCompound getEntityNBT(Entity entity) {
net.minecraft.server.v1_8_R2.Entity nmsEntity = ((CraftEntity) entity).getHandle();
NBTTagCompound NBT = new NBTTagCompound();
nmsEntity.e(NBT);
return NBT;
}
@Override
public NBTTagCompound getTileNBT(Block block) {
NBTTagCompound NBT = new NBTTagCompound();
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
if (tileEntity == null)
return null;
tileEntity.b(NBT);
return NBT;
}
@Override
public NBTTagCompound getItemNBT(ItemStack itemStack) {
if (itemStack.getType() == Material.AIR)
return null;
NBTTagCompound itemNBT = CraftItemStack.asNMSCopy(itemStack).getTag();
if (String.valueOf(itemNBT).equals("{}"))
itemNBT = null;
return itemNBT;
}
@Override
public void setEntityNBT(Entity entity, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
net.minecraft.server.v1_8_R2.Entity nmsEntity = ((CraftEntity) entity).getHandle();
nmsEntity.f((NBTTagCompound) newCompound);
}
}
@Override
public void setTileNBT(Block block, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
if (tileEntity == null)
return;
tileEntity.a((NBTTagCompound) newCompound);
tileEntity.update();
nmsWorld.notify(tileEntity.getPosition());
}
}
@Override
public ItemStack getItemWithNBT(ItemStack itemStack, Object compound) {
net.minecraft.server.v1_8_R2.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
if (compound instanceof NBTTagCompound && itemStack != null) {
if (itemStack.getType() == Material.AIR)
return null;
if (((NBTTagCompound) compound).isEmpty())
return itemStack;
nmsItem.setTag((NBTTagCompound) compound);
ItemStack newItem = CraftItemStack.asBukkitCopy(nmsItem);
return newItem;
} else if (compound == null) {
nmsItem.setTag(null);
ItemStack newItem = CraftItemStack.asBukkitCopy(nmsItem);
return newItem;
}
return itemStack;
}
@Override
public NBTTagCompound getFileNBT(File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException ex) {
return null; //File doesn't exist.
}
NBTTagCompound fileNBT = null;
try {
fileNBT = NBTCompressedStreamTools.a(fis);
fis.close();
} catch (IOException ex) {
if (ex instanceof EOFException) {
; //Nothing.
} else {
ex.printStackTrace();
}
}
return fileNBT;
}
@Override
public void setFileNBT(File file, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
try {
NBTCompressedStreamTools.a((NBTTagCompound) newCompound, os);
os.close();
} catch (IOException ex) {
if (ex instanceof EOFException) {
; //Ignore, just end of the file
} else {
ex.printStackTrace();
}
}
}
}
public NBTBase convertToNBT(Number number) {
if (number instanceof Byte) {
return new NBTTagByte((byte) number);
} else if (number instanceof Short) {
return new NBTTagShort((short) number);
} else if (number instanceof Integer) {
return new NBTTagInt((int) number);
} else if (number instanceof Long) {
return new NBTTagLong((long) number);
} else if (number instanceof Float) {
return new NBTTagFloat((float) number);
} else if (number instanceof Double) {
return new NBTTagDouble((double) number);
}
return null;
}
public NBTTagString convertToNBT(String string) {
return new NBTTagString(string);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,562 @@
package me.TheBukor.SkStuff.util;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.OutputStream;
import java.io.StreamCorruptedException;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_9_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftItemStack;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.coll.CollectionUtils;
import ch.njol.yggdrasil.Fields;
import net.minecraft.server.v1_9_R1.BlockPosition;
import net.minecraft.server.v1_9_R1.EntityInsentient;
import net.minecraft.server.v1_9_R1.IBlockData;
import net.minecraft.server.v1_9_R1.MojangsonParseException;
import net.minecraft.server.v1_9_R1.MojangsonParser;
import net.minecraft.server.v1_9_R1.NBTBase;
import net.minecraft.server.v1_9_R1.NBTCompressedStreamTools;
import net.minecraft.server.v1_9_R1.NBTTagByte;
import net.minecraft.server.v1_9_R1.NBTTagCompound;
import net.minecraft.server.v1_9_R1.NBTTagDouble;
import net.minecraft.server.v1_9_R1.NBTTagFloat;
import net.minecraft.server.v1_9_R1.NBTTagInt;
import net.minecraft.server.v1_9_R1.NBTTagList;
import net.minecraft.server.v1_9_R1.NBTTagLong;
import net.minecraft.server.v1_9_R1.NBTTagShort;
import net.minecraft.server.v1_9_R1.NBTTagString;
import net.minecraft.server.v1_9_R1.PathfinderGoal;
import net.minecraft.server.v1_9_R1.PathfinderGoalSelector;
import net.minecraft.server.v1_9_R1.TileEntity;
import net.minecraft.server.v1_9_R1.World;
public class NMS_v1_9_R1 implements NMSInterface {
@Override
public void addToCompound(Object compound, Object toAdd) {
if (compound instanceof NBTTagCompound && toAdd instanceof NBTTagCompound) {
((NBTTagCompound) compound).a((NBTTagCompound) toAdd);
}
}
@Override
public void removeFromCompound(Object compound, String ... toRemove) {
if (compound instanceof NBTTagCompound) {
for (String s : toRemove) {
((NBTTagCompound) compound).remove(s);
}
}
}
@Override
public NBTTagCompound parseRawNBT(String rawNBT) {
NBTTagCompound parsedNBT = null;
try {
parsedNBT = MojangsonParser.parse(rawNBT);
} catch (MojangsonParseException ex) {
Skript.warning("Error when parsing NBT - " + ex.getMessage());
}
return parsedNBT;
}
@Override
public int getContentsId(Object nbtList) {
if (nbtList instanceof NBTTagList) {
return ((NBTTagList) nbtList).d();
}
return 0;
}
@Override
public Object[] getContents(Object nbtList) {
if (nbtList instanceof NBTTagList) {
Object[] contents = new Object[((NBTTagList) nbtList).size()];
for (int i = 0; i < ((NBTTagList) nbtList).size(); i++) {
if (getIndex(nbtList, i) != null) {
contents[i] = getIndex(nbtList, i);
}
}
return contents;
}
return null;
}
@Override
public void addToList(Object nbtList, Object toAdd) {
if (nbtList instanceof NBTTagList && toAdd instanceof NBTBase) {
((NBTTagList) nbtList).add((NBTBase) toAdd);
}
}
@Override
public void removeFromList(Object nbtList, int index) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
((NBTTagList) nbtList).remove(index);
}
}
@Override
public void setIndex(Object nbtList, int index, Object toSet) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
if (toSet instanceof NBTBase) {
((NBTTagList) nbtList).a(index, (NBTBase) toSet);
} else if (toSet instanceof Number) {
((NBTTagList) nbtList).a(index, convertToNBT((Number) toSet));
} else if (toSet instanceof String) {
((NBTTagList) nbtList).a(index, convertToNBT((String) toSet));
}
}
}
@Override
public Object getIndex(Object nbtList, int index) {
if (nbtList instanceof NBTTagList && index >= 0 && index < ((NBTTagList) nbtList).size()) {
NBTBase value = ((NBTTagList) nbtList).h(index);
if (value instanceof NBTTagByte) {
return ((NBTTagByte) value).f(); //Byte stored inside a NBTNumber
} else if (value instanceof NBTTagShort) {
return ((NBTTagShort) value).e(); //Short inside a NBTNumber
} else if (value instanceof NBTTagInt) {
return ((NBTTagInt) value).d(); //Integer inside a NBTNumber
} else if (value instanceof NBTTagLong) {
return ((NBTTagLong) value).c(); //Long inside a NBTNumber
} else if (value instanceof NBTTagFloat) {
return ((NBTTagFloat) value).h(); //Float inside a NBTNumber
} else if (value instanceof NBTTagDouble) {
return ((NBTTagDouble) value).g(); //Double inside a NBTNumber
} else if (value instanceof NBTTagString) {
return ((NBTTagString) value).a_(); //String inside the NBTTagString
} else if (value instanceof NBTTagList || value instanceof NBTTagCompound) {
return value; //No need to convert anything, these are registered by the addon.
}
}
return null;
}
@Override
public void removePathfinderGoal(Object entity, Class<?> goalClass, boolean isTargetSelector) {
if (entity instanceof EntityInsentient) {
((EntityInsentient) entity).setGoalTarget(null);
if (isTargetSelector) {
Iterator<?> goals = ((List<?>) ReflectionUtils.getField("b", PathfinderGoalSelector.class, ((EntityInsentient) entity).targetSelector)).iterator();
while (goals.hasNext()) {
Object goal = goals.next();
if (ReflectionUtils.getField("a", goal.getClass(), goal).getClass() == goalClass) {
goals.remove();
}
}
} else {
Iterator<?> goals = ((List<?>) ReflectionUtils.getField("b", PathfinderGoalSelector.class, ((EntityInsentient) entity).goalSelector)).iterator();
while (goals.hasNext()) {
Object goal = goals.next();
if (ReflectionUtils.getField("a", goal.getClass(), goal).getClass() == goalClass) {
goals.remove();
}
}
}
}
}
@Override
public void addPathfinderGoal(Object entity, int priority, Object goal, boolean isTargetSelector) {
if (entity instanceof EntityInsentient && goal instanceof PathfinderGoal) {
if (isTargetSelector)
((EntityInsentient) entity).targetSelector.a(priority, (PathfinderGoal) goal);
else
((EntityInsentient) entity).goalSelector.a(priority, (PathfinderGoal) goal);
}
}
@Override
public void registerCompoundClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagCompound>(NBTTagCompound.class, "compound").user("((nbt)?( ?tag)?) ?compounds?").name("NBT Compound").changer(new Changer<NBTTagCompound>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE || mode == ChangeMode.SET) {
return CollectionUtils.array(String[].class, NBTTagCompound[].class);
}
return null;
}
@Override
public void change(NBTTagCompound[] NBT, @Nullable Object[] delta, ChangeMode mode) {
if (mode == ChangeMode.SET) {
if (delta[0] instanceof NBTTagCompound) {
NBT[0] = (NBTTagCompound) delta[0];
} else {
NBTTagCompound parsedNBT = null;
parsedNBT = parseRawNBT((String) delta[0]);
NBT[0] = parsedNBT;
}
} else if (mode == ChangeMode.ADD) {
if (delta[0] instanceof String) {
NBTTagCompound parsedNBT = null;
parsedNBT = parseRawNBT((String) delta[0]);
addToCompound(NBT[0], parsedNBT);
} else {
addToCompound(NBT[0], delta[0]);
}
} else if (mode == ChangeMode.REMOVE) {
if (delta[0] instanceof NBTTagCompound)
return;
for (Object s : delta) {
NBT[0].remove((String) s);
}
}
}
}).parser(new Parser<NBTTagCompound>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagCompound parse(String rawNBT, ParseContext context) {
if (rawNBT.startsWith("nbt:{") && rawNBT.endsWith("}")) {
NBTTagCompound NBT = null;
NBT = parseRawNBT(rawNBT.substring(4));
return NBT;
}
return null;
}
@Override
public String toString(NBTTagCompound compound, int arg1) {
return compound.toString();
}
@Override
public String toVariableNameString(NBTTagCompound compound) {
return "nbt:" + compound.toString();
}
}).serializer(new Serializer<NBTTagCompound>() {
@Override
public Fields serialize(NBTTagCompound compound) throws NotSerializableException {
Fields f = new Fields();
f.putObject("asString", compound.toString());
return f;
}
@Override
public void deserialize(NBTTagCompound compound, Fields f) throws StreamCorruptedException, NotSerializableException {
assert false;
}
@Override
protected boolean canBeInstantiated() {
return false;
}
@Override
protected NBTTagCompound deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
String s = fields.getObject("asString", String.class);
NBTTagCompound compound = null;
compound = parseRawNBT(s);
return compound;
}
@Override
@Nullable
public NBTTagCompound deserialize(String s) {
NBTTagCompound compound = null;
compound = parseRawNBT(s);
return compound;
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
}));
}
@Override
public void registerNBTListClassInfo() {
Classes.registerClass(new ClassInfo<NBTTagList>(NBTTagList.class, "nbtlist").user("nbt ?list ?(tag)?").name("NBT List").changer(new Changer<NBTTagList>() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.SET || mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
return CollectionUtils.array(Float[].class, Double[].class, String[].class, NBTTagCompound[].class, Integer[].class, NBTTagList[].class);
}
return null;
}
@Override
public void change(NBTTagList[] nbtList, @Nullable Object[] delta, ChangeMode mode) {
int typeId = 0;
if (delta instanceof Byte[]) {
typeId = 1;
} else if (delta instanceof Short[]) {
typeId = 2;
} else if (delta instanceof Integer[]) {
typeId = 3;
} else if (delta instanceof Long[]) {
typeId = 4;
} else if (delta instanceof Float[]) {
typeId = 5;
} else if (delta instanceof Double[]) {
typeId = 6;
} else if (delta instanceof String[]) {
typeId = 8;
} else if (delta instanceof NBTTagList[]) {
typeId = 9;
} else if (delta instanceof NBTTagCompound[]) {
typeId = 10;
} else {
return;
}
if (mode == ChangeMode.SET) {
if (typeId == 9)
nbtList[0] = (NBTTagList) delta[0];
} else if (mode == ChangeMode.ADD) {
if (getContentsId(nbtList[0]) == typeId) {
if (delta[0] instanceof Number)
addToList(nbtList, convertToNBT((Number) delta[0]));
else if (delta[0] instanceof String)
addToList(nbtList, convertToNBT((String) delta[0]));
else if (delta[0] instanceof NBTTagCompound || delta[0] instanceof NBTTagList)
addToList(nbtList, delta[0]);
}
} else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
nbtList[0] = new NBTTagList();
}
}
}).parser(new Parser<NBTTagList>() {
@Override
public String getVariableNamePattern() {
return ".+";
}
@Override
@Nullable
public NBTTagList parse(String listString, ParseContext context) {
if (listString.startsWith("[") && listString.endsWith("]")) {
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:[0:" + listString.substring(1) + "}");
NBTTagList parsedList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return parsedList;
}
return null;
}
@Override
public String toString(NBTTagList nbtList, int arg1) {
return nbtList.toString();
}
@Override
public String toVariableNameString(NBTTagList nbtList) {
return nbtList.toString();
}
}).serializer(new Serializer<NBTTagList>() {
@Override
public Fields serialize(NBTTagList nbtList) throws NotSerializableException {
Fields f = new Fields();
f.putObject("asString", nbtList.toString());
return f;
}
@Override
public void deserialize(NBTTagList nbtList, Fields f) throws StreamCorruptedException, NotSerializableException {
assert false;
}
@Override
protected boolean canBeInstantiated() {
return false;
}
@Override
protected NBTTagList deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
String s = fields.getObject("asString", String.class);
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagList nbtList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return nbtList;
}
@Override
@Nullable
public NBTTagList deserialize(String s) {
NBTTagCompound tempNBT = null;
tempNBT = parseRawNBT("{SkStuffIsCool:" + s + "}");
NBTTagList nbtList = (NBTTagList) tempNBT.get("SkStuffIsCool");
return nbtList;
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
}));
}
@Override
public NBTTagCompound getEntityNBT(Entity entity) {
net.minecraft.server.v1_9_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle();
NBTTagCompound NBT = new NBTTagCompound();
nmsEntity.e(NBT);
return NBT;
}
@Override
public NBTTagCompound getTileNBT(Block block) {
NBTTagCompound NBT = new NBTTagCompound();
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
if (tileEntity == null)
return null;
tileEntity.save(NBT);
return NBT;
}
@Override
public NBTTagCompound getItemNBT(ItemStack itemStack) {
if (itemStack.getType() == Material.AIR)
return null;
NBTTagCompound itemNBT = CraftItemStack.asNMSCopy(itemStack).getTag();
if (String.valueOf(itemNBT).equals("{}"))
itemNBT = null;
return itemNBT;
}
@Override
public void setEntityNBT(Entity entity, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
net.minecraft.server.v1_9_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle();
nmsEntity.f((NBTTagCompound) newCompound);
}
}
@Override
public void setTileNBT(Block block, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
World nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
TileEntity tileEntity = nmsWorld.getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
if (tileEntity == null)
return;
tileEntity.a((NBTTagCompound) newCompound);
tileEntity.update();
IBlockData tileEntType = nmsWorld.getType(new BlockPosition(block.getX(), block.getY(), block.getZ()));
nmsWorld.notify(tileEntity.getPosition(), tileEntType, tileEntType, 3);
}
}
@Override
public ItemStack getItemWithNBT(ItemStack itemStack, Object compound) {
net.minecraft.server.v1_9_R1.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
if (compound instanceof NBTTagCompound && itemStack != null) {
if (itemStack.getType() == Material.AIR)
return null;
if (((NBTTagCompound) compound).isEmpty())
return itemStack;
nmsItem.setTag((NBTTagCompound) compound);
ItemStack newItem = CraftItemStack.asBukkitCopy(nmsItem);
return newItem;
} else if (compound == null) {
nmsItem.setTag(null);
ItemStack newItem = CraftItemStack.asBukkitCopy(nmsItem);
return newItem;
}
return itemStack;
}
@Override
public NBTTagCompound getFileNBT(File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException ex) {
return null; //File doesn't exist.
}
NBTTagCompound fileNBT = null;
try {
fileNBT = NBTCompressedStreamTools.a(fis);
fis.close();
} catch (IOException ex) {
if (ex instanceof EOFException) {
; //Nothing.
} else {
ex.printStackTrace();
}
}
return fileNBT;
}
@Override
public void setFileNBT(File file, Object newCompound) {
if (newCompound instanceof NBTTagCompound) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
try {
NBTCompressedStreamTools.a((NBTTagCompound) newCompound, os);
os.close();
} catch (IOException ex) {
if (ex instanceof EOFException) {
; //Ignore, just end of the file
} else {
ex.printStackTrace();
}
}
}
}
public NBTBase convertToNBT(Number number) {
if (number instanceof Byte) {
return new NBTTagByte((byte) number);
} else if (number instanceof Short) {
return new NBTTagShort((short) number);
} else if (number instanceof Integer) {
return new NBTTagInt((int) number);
} else if (number instanceof Long) {
return new NBTTagLong((long) number);
} else if (number instanceof Float) {
return new NBTTagFloat((float) number);
} else if (number instanceof Double) {
return new NBTTagDouble((double) number);
}
return null;
}
public NBTTagString convertToNBT(String string) {
return new NBTTagString(string);
}
}

View File

@@ -1,64 +1,74 @@
package me.TheBukor.SkStuff.util;
import java.lang.reflect.Field;
import org.bukkit.Bukkit;
public class ReflectionUtils {
public static Class<?> getNMSClass(String classString, boolean isArray) {
String version = getVersion();
String name = "net.minecraft.server." + version + classString;
if (isArray)
name = "[L" + name + ";";
Class<?> nmsClass = null;
try {
nmsClass = Class.forName(name);
} catch (ClassNotFoundException ex) {
Bukkit.getLogger().warning("Unable to get NMS class \'" + name + "\'! You are probably running an unsupported version!");
return null;
}
return nmsClass;
}
public static Class<?> getOBCClass(String classString) {
String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3] + ".";
String name = "org.bukkit.craftbukkit." + version + classString;
Class<?> obcClass = null;
try {
obcClass = Class.forName(name);
} catch (ClassNotFoundException ex) {
Bukkit.getLogger().warning("Unable to get OBC class \'" + name + "\'! You are probably running an unsupported version!");
return null;
}
return obcClass;
}
public static Object getField(String field, Class<?> clazz, Object object) {
Field f = null;
Object obj = null;
try {
f = clazz.getDeclaredField(field);
f.setAccessible(true);
obj = f.get(object);
} catch (Exception ex) {
ex.printStackTrace();
}
return obj;
}
public static void setField(String field, Class<?> clazz, Object object, Object toSet) {
Field f = null;
try {
f = clazz.getDeclaredField(field);
f.setAccessible(true);
f.set(object, toSet);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static String getVersion() {
return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3] + ".";
}
package me.TheBukor.SkStuff.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import org.bukkit.Bukkit;
public class ReflectionUtils {
public static Class<?> getNMSClass(String classString) {
String version = getVersion();
String name = "net.minecraft.server." + version + classString;
Class<?> nmsClass = null;
try {
nmsClass = Class.forName(name);
} catch (ClassNotFoundException ex) {
Bukkit.getLogger().warning("Unable to get NMS class \'" + name + "\'! You are probably running an unsupported version!");
return null;
}
return nmsClass;
}
public static Class<?> getOBCClass(String classString) {
String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3] + ".";
String name = "org.bukkit.craftbukkit." + version + classString;
Class<?> obcClass = null;
try {
obcClass = Class.forName(name);
} catch (ClassNotFoundException ex) {
Bukkit.getLogger().warning("Unable to get OBC class \'" + name + "\'! You are probably running an unsupported version!");
return null;
}
return obcClass;
}
public static Object getField(String field, Class<?> clazz, Object object) {
Field f = null;
Object obj = null;
try {
f = clazz.getDeclaredField(field);
f.setAccessible(true);
obj = f.get(object);
} catch (Exception ex) {
ex.printStackTrace();
}
return obj;
}
public static void setField(String field, Class<?> clazz, Object object, Object toSet) {
Field f = null;
try {
f = clazz.getDeclaredField(field);
f.setAccessible(true);
f.set(object, toSet);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static Constructor<?> getConstructor(String constructor, Class<?> clazz, Object object, Class<?> ... params) {
Constructor<?> constr = null;
try {
constr = clazz.getDeclaredConstructor(params);
constr.setAccessible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
return constr;
}
public static String getVersion() {
return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3] + ".";
}
}