Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.dpohvar.groovy.nbtwrap
- /**
- * NBTWrapper v1.0.0
- * module for VarScript (Bukkit plugin)
- * Author: DPOH-VAR
- * (c) 2013
- */
- import org.bukkit.entity.Player
- import org.bukkit.entity.Entity
- import org.bukkit.inventory.ItemStack
- import org.bukkit.block.Block
- import static ru.dpohvar.groovy.nbtwrap.Utils.*
- public class Utils {
- static String nms = org.bukkit.Bukkit.getServer().handle.class.name.split("\\.")[3]
- static Class classNBTBase = Class.forName "net.minecraft.server."+nms+".NBTBase"
- static Class classNBTTagByte = Class.forName "net.minecraft.server."+nms+".NBTTagByte"
- static Class classNBTTagShort = Class.forName "net.minecraft.server."+nms+".NBTTagShort"
- static Class classNBTTagInt = Class.forName "net.minecraft.server."+nms+".NBTTagInt"
- static Class classNBTTagLong = Class.forName "net.minecraft.server."+nms+".NBTTagLong"
- static Class classNBTTagFloat = Class.forName "net.minecraft.server."+nms+".NBTTagFloat"
- static Class classNBTTagDouble = Class.forName "net.minecraft.server."+nms+".NBTTagDouble"
- static Class classNBTTagByteArray = Class.forName "net.minecraft.server."+nms+".NBTTagByteArray"
- static Class classNBTTagIntArray = Class.forName "net.minecraft.server."+nms+".NBTTagIntArray"
- static Class classNBTTagString = Class.forName "net.minecraft.server."+nms+".NBTTagString"
- static Class classNBTTagCompound = Class.forName "net.minecraft.server."+nms+".NBTTagCompound"
- static Class classNBTTagList = Class.forName "net.minecraft.server."+nms+".NBTTagList"
- public static def getNBT(def object) {
- if (object == null) return null
- if (classNBTTagCompound.isInstance(object)) return new NBTCompound(object)
- if (classNBTTagList.isInstance(object)) return new NBTList(object)
- return object.@data
- }
- public static def wrapNBT(boolean object) {
- byte val = object ? 1 as byte : 0 as byte
- classNBTTagByte.newInstance "", val
- }
- public static def wrapNBT(byte object) {
- classNBTTagByte.newInstance "", object as byte
- }
- public static def wrapNBT(short object) {
- classNBTTagShort.newInstance "", object as short
- }
- public static def wrapNBT(int object) {
- classNBTTagInt.newInstance "", object as int
- }
- public static def wrapNBT(long object) {
- classNBTTagLong.newInstance "", object as long
- }
- public static def wrapNBT(float object) {
- classNBTTagFloat.newInstance "", object as float
- }
- public static def wrapNBT(double object) {
- classNBTTagDouble.newInstance "", object as double
- }
- public static def wrapNBT(String object) {
- classNBTTagString.newInstance "", object as String
- }
- public static def wrapNBT(byte[] object) {
- classNBTTagByteArray.newInstance "", object as byte[]
- }
- public static def wrapNBT(int[] object) {
- classNBTTagIntArray.newInstance "", object as int[]
- }
- public static def wrapNBT(Collection list) {
- def nbtList = classNBTTagList.newInstance("")
- list.each {
- if ( classNBTBase.isInstance(it) ) nbtList.add( it.clone() )
- else nbtList.add( wrapNBT(it) )
- }
- return nbtList;
- }
- public static def wrapNBT(Map map) {
- def nbtCompound = classNBTTagCompound.newInstance ("")
- map.each {
- if ( classNBTBase.isInstance(it.value) ) nbtCompound.set( it.key as String, it.value.clone() )
- else nbtCompound.set( it.key as String, wrapNBT(it.value) )
- }
- return nbtCompound;
- }
- }
- public class NBTCompound implements Map {
- def handle
- Map handleMap
- private NBTCompound(def handle){
- this.handle = handle
- this.handleMap = handle.map
- }
- public NBTCompound(){
- this.handle = NBTTagCompound.newInstance ""
- this.handleMap = this.handle.map
- }
- public boolean equals(NBTCompound other){
- return handle.equals( other.@handle )
- }
- public NBTCompound clone() {
- return new NBTCompound(handle.clone())
- }
- public def handle() {
- return handle
- }
- public int size() {
- return handleMap.size()
- }
- public boolean isEmpty() {
- return handleMap.isEmpty()
- }
- public boolean containsKey(Object key) {
- return handleMap.containsKey(key as String);
- }
- public boolean containsValue(Object value) {
- if (classNBTBase.isInstance(value)) return handleMap.containsValue(value);
- else return handleMap.containsValue(wrapNBT(value))
- }
- public Object get(Object key) {
- return getNBT( handleMap.get(key as String) )
- }
- public Object put(Object key, Object value) {
- def temp = get(key);
- if (classNBTBase.isInstance(value)) handle.set(key as String, value.clone())
- else handle.set(key as String, wrapNBT(value))
- return temp;
- }
- public Object remove(Object key) {
- def temp = get(key);
- handle.remove(key as String)
- return temp;
- }
- public void putAll(Map m) {
- m.each {
- if (classNBTBase.isInstance(it.value)) handle.set(it.key as String, it.value.clone())
- else handle.set(it.key as String, wrapNBT(it.value))
- }
- }
- public void clear() {
- handleMap.clear();
- }
- public Set keySet() {
- return handleMap.keySet()
- }
- public Collection values() {
- return null;
- }
- public NBTEntrySet entrySet() {
- return new NBTEntrySet(handleMap.entrySet());
- }
- public String toString() {
- NBTEntryIterator i = entrySet().iterator();
- if ( !i.hasNext()) return "{}";
- StringBuilder sb = new StringBuilder().append('{');
- for (;;) {
- Map.Entry e = i.next();
- sb.append(e.key).append('=');
- if (e.value instanceof byte[]) sb.append("int[${e.value.length}]")
- else if (e.value instanceof int[]) sb.append("byte[${e.value.length}]")
- else sb.append(e.value);
- if (! i.hasNext()) return sb.append('}').toString();
- sb.append(", ");
- }
- }
- private class NBTEntrySet extends AbstractSet<Map.Entry> {
- Set handleEntrySet;
- NBTEntrySet(Set entrySet){
- this.handleEntrySet = entrySet;
- }
- public NBTEntryIterator iterator() {
- return new NBTEntryIterator(handleEntrySet.iterator())
- }
- public boolean contains(Object value) {
- if (classNBTBase.isInstance(value)) return handleEntrySet.contains(value)
- else return handleEntrySet.contains( wrapNBT(value) )
- }
- public boolean remove(Object o) {
- if (classNBTBase.isInstance(value)) return handleEntrySet.remove(value)
- else return handleEntrySet.remove( wrapNBT(value) )
- }
- public int size() {
- return NBTCompound.this.size();
- }
- public void clear() {
- NBTCompound.this.clear();
- }
- }
- private class NBTEntryIterator extends Iterator<Map.Entry> {
- Iterator handleEntryIterator
- NBTEntryIterator(Iterator entryIterator){
- this.handleEntryIterator = entryIterator
- }
- public boolean hasNext() {
- return handleEntryIterator.hasNext()
- }
- public NBTEntry next() {
- return new NBTEntry(handleEntryIterator.next());
- }
- public void remove() {
- handleEntryIterator.remove()
- }
- }
- private class NBTEntry extends Map.Entry<String,Object> {
- private Map.Entry handleEntry
- public NBTEntry(Map.Entry entry){
- this.handleEntry = entry
- }
- public String getKey() {
- return handleEntry.getKey()
- }
- public Object getValue() {
- return getNBT(handleEntry.getValue())
- }
- public Object setValue(Object value) {
- return NBTCompound.this.put(handleEntry.key,value)
- }
- }
- }
- public class NBTList implements List {
- def handle;
- List handleList;
- private NBTList (def handle) {
- this.handle = handle
- this.handleList = handle.list
- }
- public NBTList () {
- this.handle = NBTTagCompound.newInstance ""
- this.handleList = this.handle.list
- }
- public NBTList clone(){
- return new NBTList(handle.clone());
- }
- public boolean equals(NBTList other){
- return handle.equals( other.@handle )
- }
- public def handle() {
- return handle
- }
- public int size() {
- return handleList.size()
- }
- public boolean isEmpty() {
- return handleList.isEmpty();
- }
- public boolean contains(Object value) {
- if (classNBTBase.isInstance(value)) return handleList.contains(value)
- else return handleList.contains(wrapNBT(value))
- }
- public Iterator iterator() {
- return new NBTListIterator( handleList.listIterator(0) )
- }
- public Object[] toArray() {
- handleList.collect { getNBT(it) } as Object[]
- }
- public boolean add(Object value) {
- if (classNBTBase.isInstance(value)) return handle.add(value.clone())
- else return handleList.contains(wrapNBT(value))
- return true;
- }
- public boolean remove(Object value) {
- if (classNBTBase.isInstance(value)) return handleList.remove(value)
- else return handleList.remove(wrapNBT(value))
- }
- public boolean containsAll(Collection<?> c) {
- for (def value in c) {
- if (classNBTBase.isInstance(value)) {
- if (!handleList.contains(value)) return false;
- } else {
- if (!handleList.contains(wrapNBT(value))) return false;
- }
- }
- return true;
- }
- public boolean addAll(Collection c) {
- c.each {
- if (classNBTBase.isInstance(it)) return handle.add(it.clone())
- else return handleList.contains(wrapNBT(it))
- }
- }
- public boolean addAll(int index, Collection c) {
- c.collect {
- if (classNBTBase.isInstance(it)) it.clone()
- else wrapNBT(it)
- }.each {
- handle.add(index++,it)
- }
- }
- public boolean removeAll(Collection<?> c) {
- c.each {
- if (classNBTBase.isInstance(it)) handleList.remove(it)
- else handleList.remove(wrapNBT(it))
- }
- return true;
- }
- public boolean retainAll(Collection<?> c) {
- handleList.clone().each {
- if(!c.contains(it) && !c.contains(wrapNBT(it))) handleList.remove(it)
- }
- }
- public void clear() {
- handleList.clear();
- }
- public Object get(int index) {
- return getNBT( handle.get(index) )
- }
- public Object set(int index, Object element) {
- def result
- if (classNBTBase.isInstance(element)) result = handleList.set(index,element.clone())
- else result = handleList.set(wrapNBT(element))
- if (result == null) return result
- return wrapNBT(result)
- }
- public void add(int index, Object element) {
- if (classNBTBase.isInstance(element)) handleList.add(index,element.clone())
- else result = handleList.add(wrapNBT(element))
- }
- public Object remove(int index) {
- result = handleList.remove(index);
- return wrapNBT(result)
- }
- public int indexOf(Object element) {
- if (classNBTBase.isInstance(element)) return handleList.indexOf(index,element)
- else return handleList.indexOf(wrapNBT(element))
- }
- public int lastIndexOf(Object element) {
- if (classNBTBase.isInstance(element)) return handleList.indexOf(index,element)
- else return handleList.indexOf(wrapNBT(element))
- }
- public ListIterator listIterator() {
- return new NBTListIterator( handleList.listIterator(0) )
- }
- public ListIterator listIterator(int index) {
- return new NBTListIterator( handleList.listIterator(index) )
- }
- public List subList(int fromIndex, int toIndex) {
- new ArrayList(this).subList(fromIndex, toIndex)
- }
- public Object[] toArray(Object[] a) {
- NBTListIterator itr = iterator();
- for (int i=0; i<a.length; i++) {
- if (! itr.hasNext()) break;
- a[i] = itr.next();
- }
- return a;
- }
- public String toString() {
- NBTListIterator i = listIterator();
- if (! i.hasNext()) return "[]";
- StringBuilder sb = new StringBuilder().append('[');
- for (;;) {
- def e = i.next();
- if(e instanceof int[]) sb.append("int[${e.length}]");
- else if(e instanceof byte[]) sb.append("byte[${e.length}]");
- else sb.append(e);
- if (! i.hasNext()) return sb.append(']').toString();
- sb.append(", ");
- }
- }
- private class NBTListIterator implements ListIterator {
- ListIterator handleIterator
- public NBTListIterator(iterator){
- this.handleIterator = iterator;
- }
- public boolean hasNext() {
- return handleIterator.hasNext()
- }
- public Object next() {
- return getNBT( handleIterator.next() )
- }
- public boolean hasPrevious() {
- return handleIterator.hasPrevious()
- }
- public Object previous() {
- return getNBT( handleIterator.previous() )
- }
- public int nextIndex() {
- handleIterator.nextIndex()
- }
- public int previousIndex() {
- handleIterator.previousIndex()
- }
- public void remove() {
- handleIterator.remove()
- }
- public void set(Object value) {
- if(classNBTBase.isInstance(value)) handleIterator.set(value.clone())
- else handleIterator.set( wrapNBT(value) )
- }
- public void add(Object value) {
- if(classNBTBase.isInstance(value)) handleIterator.add(value.clone())
- else handleIterator.add( wrapNBT(value) )
- }
- }
- }
- Script.metaClass.nbt = { Collection t ->
- return new NBTList( wrapNBT(t) );
- }
- Script.metaClass.nbt = { Object[] t ->
- return new NBTList( wrapNBT(t as List) );
- }
- Script.metaClass.nbt = { Map t ->
- return new NBTCompound( wrapNBT(t) );
- }
- Script.metaClass.nbt = { def object, Closure handle ->
- def main = delegate.nbt object
- def temp = main.clone()
- def result = handle temp
- if (main != temp) delegate.nbt object, temp
- return result
- }
- Script.metaClass.nbt = { Entity e ->
- def basic = classNBTTagCompound.newInstance("")
- e.handle.e basic
- new NBTCompound (basic)
- }
- Script.metaClass.nbt = { Entity e, NBTCompound vals ->
- e.handle.f vals.@handle
- return true
- }
- int maxDist = Server.getViewDistance() * 32
- Script.metaClass.nbt = { Block b ->
- def tile = b.world.getTileEntityAt b.x, b.y, b.z
- if (!tile) return null
- def basic = classNBTTagCompound.newInstance ""
- tile.b basic
- result = new NBTCompound (basic)
- result.remove "x"
- result.remove "y"
- result.remove "z"
- result
- }
- Script.metaClass.nbt = { Block b, NBTCompound vals ->
- def tile = b.world.getTileEntityAt b.x, b.y, b.z
- if (!tile) return false
- vals = vals.clone()
- if (!vals.x) vals.x = b.x
- if (!vals.y) vals.y = b.y
- if (!vals.z) vals.z = b.z
- tile.a vals.handle()
- def packet = tile.updatePacket;
- b.world.players.findAll {
- it.location.distance(b.location) < maxDist
- }.each {
- it.handle.playerConnection.sendPacket packet
- }
- return true
- }
- Script.metaClass.nbt = { ItemStack item ->
- def tag = item.handle.tag
- if (tag == null) return null
- else new NBTCompound( tag.clone() )
- }
- Script.metaClass.nbt = { ItemStack item, NBTCompound vals ->
- item.handle.tag = vals.clone().handle();
- }
- return [name:"NBTWrapper",version:[1,0,0]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement