Advertisement
DPOH-VAR

NBTWrapper.groovy

Nov 23rd, 2013
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 15.99 KB | None | 0 0
  1. package ru.dpohvar.groovy.nbtwrap
  2.  
  3. /**
  4.  * NBTWrapper v1.0.0
  5.  * module for VarScript (Bukkit plugin)
  6.  * Author: DPOH-VAR
  7.  * (c) 2013
  8.  */
  9.  
  10. import org.bukkit.entity.Player
  11. import org.bukkit.entity.Entity
  12. import org.bukkit.inventory.ItemStack
  13. import org.bukkit.block.Block
  14. import static ru.dpohvar.groovy.nbtwrap.Utils.*
  15.  
  16. public class Utils {
  17.     static String nms = org.bukkit.Bukkit.getServer().handle.class.name.split("\\.")[3]
  18.     static Class classNBTBase = Class.forName "net.minecraft.server."+nms+".NBTBase"
  19.     static Class classNBTTagByte = Class.forName "net.minecraft.server."+nms+".NBTTagByte"
  20.     static Class classNBTTagShort = Class.forName "net.minecraft.server."+nms+".NBTTagShort"
  21.     static Class classNBTTagInt = Class.forName "net.minecraft.server."+nms+".NBTTagInt"
  22.     static Class classNBTTagLong = Class.forName "net.minecraft.server."+nms+".NBTTagLong"
  23.     static Class classNBTTagFloat = Class.forName "net.minecraft.server."+nms+".NBTTagFloat"
  24.     static Class classNBTTagDouble = Class.forName "net.minecraft.server."+nms+".NBTTagDouble"
  25.     static Class classNBTTagByteArray = Class.forName "net.minecraft.server."+nms+".NBTTagByteArray"
  26.     static Class classNBTTagIntArray = Class.forName "net.minecraft.server."+nms+".NBTTagIntArray"
  27.     static Class classNBTTagString = Class.forName "net.minecraft.server."+nms+".NBTTagString"
  28.     static Class classNBTTagCompound = Class.forName "net.minecraft.server."+nms+".NBTTagCompound"
  29.     static Class classNBTTagList = Class.forName "net.minecraft.server."+nms+".NBTTagList"
  30.  
  31.     public static def getNBT(def object) {
  32.         if (object == null) return null
  33.         if (classNBTTagCompound.isInstance(object)) return new NBTCompound(object)
  34.         if (classNBTTagList.isInstance(object)) return new NBTList(object)
  35.         return object.@data
  36.     }
  37.  
  38.     public static def wrapNBT(boolean object) {
  39.         byte val = object ? 1 as byte : 0 as byte
  40.         classNBTTagByte.newInstance "", val
  41.     }
  42.     public static def wrapNBT(byte object) {
  43.         classNBTTagByte.newInstance "", object as byte
  44.     }
  45.     public static def wrapNBT(short object) {
  46.         classNBTTagShort.newInstance "", object as short
  47.     }
  48.     public static def wrapNBT(int object) {
  49.         classNBTTagInt.newInstance "", object as int
  50.     }
  51.     public static def wrapNBT(long object) {
  52.         classNBTTagLong.newInstance "", object as long
  53.     }
  54.     public static def wrapNBT(float object) {
  55.         classNBTTagFloat.newInstance "", object as float
  56.     }
  57.     public static def wrapNBT(double object) {
  58.         classNBTTagDouble.newInstance "", object as double
  59.     }
  60.     public static def wrapNBT(String object) {
  61.         classNBTTagString.newInstance "", object as String
  62.     }
  63.     public static def wrapNBT(byte[] object) {
  64.         classNBTTagByteArray.newInstance "", object as byte[]
  65.     }
  66.     public static def wrapNBT(int[] object) {
  67.         classNBTTagIntArray.newInstance "", object as int[]
  68.     }
  69.     public static def wrapNBT(Collection list) {
  70.         def nbtList = classNBTTagList.newInstance("")
  71.         list.each {
  72.             if ( classNBTBase.isInstance(it) ) nbtList.add( it.clone() )
  73.             else nbtList.add( wrapNBT(it) )
  74.         }
  75.         return nbtList;
  76.     }
  77.     public static def wrapNBT(Map map) {
  78.         def nbtCompound = classNBTTagCompound.newInstance ("")
  79.         map.each {
  80.             if ( classNBTBase.isInstance(it.value) ) nbtCompound.set( it.key as String, it.value.clone() )
  81.             else nbtCompound.set( it.key as String, wrapNBT(it.value) )
  82.         }
  83.         return nbtCompound;
  84.     }
  85. }
  86.  
  87. public class NBTCompound implements Map {
  88.     def handle
  89.     Map handleMap
  90.     private NBTCompound(def handle){
  91.         this.handle = handle
  92.         this.handleMap = handle.map
  93.     }
  94.     public NBTCompound(){
  95.         this.handle = NBTTagCompound.newInstance ""
  96.         this.handleMap = this.handle.map
  97.     }
  98.     public boolean equals(NBTCompound other){
  99.         return handle.equals( other.@handle )
  100.     }
  101.     public NBTCompound clone() {
  102.         return new NBTCompound(handle.clone())
  103.     }
  104.     public def handle() {
  105.         return handle
  106.     }
  107.     public int size() {
  108.         return handleMap.size()
  109.     }
  110.     public boolean isEmpty() {
  111.         return handleMap.isEmpty()
  112.     }
  113.     public boolean containsKey(Object key) {
  114.         return handleMap.containsKey(key as String);
  115.     }
  116.     public boolean containsValue(Object value) {
  117.         if (classNBTBase.isInstance(value)) return handleMap.containsValue(value);
  118.         else return handleMap.containsValue(wrapNBT(value))
  119.     }
  120.     public Object get(Object key) {
  121.         return getNBT( handleMap.get(key as String) )
  122.     }
  123.     public Object put(Object key, Object value) {
  124.         def temp = get(key);
  125.         if (classNBTBase.isInstance(value)) handle.set(key as String, value.clone())
  126.         else handle.set(key as String, wrapNBT(value))
  127.         return temp;
  128.     }
  129.     public Object remove(Object key) {
  130.         def temp = get(key);
  131.         handle.remove(key as String)
  132.         return temp;
  133.     }
  134.     public void putAll(Map m) {
  135.         m.each {
  136.             if (classNBTBase.isInstance(it.value)) handle.set(it.key as String, it.value.clone())
  137.             else handle.set(it.key as String, wrapNBT(it.value))
  138.         }
  139.     }
  140.     public void clear() {
  141.         handleMap.clear();
  142.     }
  143.     public Set keySet() {
  144.         return handleMap.keySet()
  145.     }
  146.     public Collection values() {
  147.         return null;
  148.     }
  149.     public NBTEntrySet entrySet() {
  150.         return new NBTEntrySet(handleMap.entrySet());
  151.     }
  152.     public String toString() {
  153.         NBTEntryIterator i = entrySet().iterator();
  154.         if ( !i.hasNext()) return "{}";
  155.         StringBuilder sb = new StringBuilder().append('{');
  156.         for (;;) {
  157.             Map.Entry e = i.next();
  158.             sb.append(e.key).append('=');
  159.             if (e.value instanceof byte[]) sb.append("int[${e.value.length}]")
  160.             else if (e.value instanceof int[]) sb.append("byte[${e.value.length}]")
  161.             else sb.append(e.value);
  162.             if (! i.hasNext()) return sb.append('}').toString();
  163.             sb.append(", ");
  164.         }
  165.     }
  166.  
  167.     private class NBTEntrySet extends AbstractSet<Map.Entry> {
  168.         Set handleEntrySet;
  169.  
  170.         NBTEntrySet(Set entrySet){
  171.             this.handleEntrySet = entrySet;
  172.         }
  173.         public NBTEntryIterator iterator() {
  174.             return new NBTEntryIterator(handleEntrySet.iterator())
  175.         }
  176.         public boolean contains(Object value) {
  177.             if (classNBTBase.isInstance(value)) return handleEntrySet.contains(value)
  178.             else return handleEntrySet.contains( wrapNBT(value) )
  179.         }
  180.         public boolean remove(Object o) {
  181.             if (classNBTBase.isInstance(value)) return handleEntrySet.remove(value)
  182.             else return handleEntrySet.remove( wrapNBT(value) )
  183.         }
  184.         public int size() {
  185.             return NBTCompound.this.size();
  186.         }
  187.         public void clear() {
  188.             NBTCompound.this.clear();
  189.         }
  190.     }
  191.  
  192.     private class NBTEntryIterator extends Iterator<Map.Entry> {
  193.         Iterator handleEntryIterator
  194.  
  195.         NBTEntryIterator(Iterator entryIterator){
  196.             this.handleEntryIterator = entryIterator
  197.         }
  198.         public boolean hasNext() {
  199.             return handleEntryIterator.hasNext()
  200.         }
  201.         public NBTEntry next() {
  202.             return new NBTEntry(handleEntryIterator.next());
  203.         }
  204.         public void remove() {
  205.             handleEntryIterator.remove()
  206.         }
  207.     }
  208.  
  209.     private class NBTEntry extends Map.Entry<String,Object> {
  210.         private Map.Entry handleEntry
  211.    
  212.         public NBTEntry(Map.Entry entry){
  213.             this.handleEntry = entry
  214.         }
  215.    
  216.         public String getKey() {
  217.             return handleEntry.getKey()
  218.         }
  219.         public Object getValue() {
  220.             return getNBT(handleEntry.getValue())
  221.         }
  222.         public Object setValue(Object value) {
  223.             return NBTCompound.this.put(handleEntry.key,value)
  224.         }
  225.     }
  226. }
  227.  
  228. public class NBTList implements List {
  229.     def handle;
  230.     List handleList;
  231.    
  232.     private NBTList (def handle) {
  233.         this.handle = handle
  234.         this.handleList = handle.list
  235.     }
  236.     public NBTList () {
  237.         this.handle = NBTTagCompound.newInstance ""
  238.         this.handleList = this.handle.list
  239.     }
  240.     public NBTList clone(){
  241.         return new NBTList(handle.clone());
  242.     }
  243.     public boolean equals(NBTList other){
  244.         return handle.equals( other.@handle )
  245.     }
  246.     public def handle() {
  247.         return handle
  248.     }
  249.     public int size() {
  250.         return handleList.size()
  251.     }
  252.     public boolean isEmpty() {
  253.         return handleList.isEmpty();
  254.     }
  255.     public boolean contains(Object value) {
  256.         if (classNBTBase.isInstance(value)) return handleList.contains(value)
  257.         else return handleList.contains(wrapNBT(value))
  258.     }
  259.     public Iterator iterator() {
  260.         return new NBTListIterator( handleList.listIterator(0) )
  261.     }
  262.     public Object[] toArray() {
  263.         handleList.collect { getNBT(it) } as Object[]
  264.     }
  265.     public boolean add(Object value) {
  266.         if (classNBTBase.isInstance(value)) return handle.add(value.clone())
  267.         else return handleList.contains(wrapNBT(value))
  268.         return true;
  269.     }
  270.     public boolean remove(Object value) {
  271.         if (classNBTBase.isInstance(value)) return handleList.remove(value)
  272.         else return handleList.remove(wrapNBT(value))
  273.     }
  274.     public boolean containsAll(Collection<?> c) {
  275.         for (def value in c) {
  276.             if (classNBTBase.isInstance(value)) {
  277.                 if (!handleList.contains(value)) return false;
  278.             } else {
  279.                 if (!handleList.contains(wrapNBT(value))) return false;
  280.             }
  281.         }
  282.         return true;
  283.     }
  284.     public boolean addAll(Collection c) {
  285.         c.each {
  286.             if (classNBTBase.isInstance(it)) return handle.add(it.clone())
  287.             else return handleList.contains(wrapNBT(it))
  288.         }
  289.     }
  290.     public boolean addAll(int index, Collection c) {
  291.         c.collect {
  292.             if (classNBTBase.isInstance(it)) it.clone()
  293.             else wrapNBT(it)
  294.         }.each {
  295.             handle.add(index++,it)
  296.         }
  297.     }
  298.     public boolean removeAll(Collection<?> c) {
  299.         c.each {
  300.             if (classNBTBase.isInstance(it)) handleList.remove(it)
  301.             else handleList.remove(wrapNBT(it))
  302.         }
  303.         return true;
  304.     }
  305.     public boolean retainAll(Collection<?> c) {
  306.         handleList.clone().each {
  307.             if(!c.contains(it) && !c.contains(wrapNBT(it))) handleList.remove(it)
  308.         }
  309.     }
  310.     public void clear() {
  311.         handleList.clear();
  312.     }
  313.     public Object get(int index) {
  314.         return getNBT( handle.get(index) )
  315.     }
  316.     public Object set(int index, Object element) {
  317.         def result
  318.         if (classNBTBase.isInstance(element)) result = handleList.set(index,element.clone())
  319.         else result = handleList.set(wrapNBT(element))
  320.         if (result == null) return result
  321.         return wrapNBT(result)
  322.     }
  323.     public void add(int index, Object element) {
  324.         if (classNBTBase.isInstance(element)) handleList.add(index,element.clone())
  325.         else result = handleList.add(wrapNBT(element))
  326.     }
  327.     public Object remove(int index) {
  328.         result = handleList.remove(index);
  329.         return wrapNBT(result)
  330.     }
  331.     public int indexOf(Object element) {
  332.         if (classNBTBase.isInstance(element)) return handleList.indexOf(index,element)
  333.         else return handleList.indexOf(wrapNBT(element))
  334.     }
  335.     public int lastIndexOf(Object element) {
  336.         if (classNBTBase.isInstance(element)) return handleList.indexOf(index,element)
  337.         else return handleList.indexOf(wrapNBT(element))
  338.     }
  339.     public ListIterator listIterator() {
  340.         return new NBTListIterator( handleList.listIterator(0) )
  341.     }
  342.     public ListIterator listIterator(int index) {
  343.         return new NBTListIterator( handleList.listIterator(index) )
  344.     }
  345.     public List subList(int fromIndex, int toIndex) {
  346.         new ArrayList(this).subList(fromIndex, toIndex)
  347.     }
  348.     public Object[] toArray(Object[] a) {
  349.         NBTListIterator itr = iterator();
  350.         for (int i=0; i<a.length; i++) {
  351.             if (! itr.hasNext()) break;
  352.             a[i] = itr.next();
  353.         }
  354.         return a;
  355.     }
  356.     public String toString() {
  357.         NBTListIterator i = listIterator();
  358.         if (! i.hasNext()) return "[]";
  359.         StringBuilder sb = new StringBuilder().append('[');
  360.         for (;;) {
  361.             def e = i.next();
  362.             if(e instanceof int[]) sb.append("int[${e.length}]");
  363.             else if(e instanceof byte[]) sb.append("byte[${e.length}]");
  364.             else sb.append(e);
  365.             if (! i.hasNext()) return sb.append(']').toString();
  366.             sb.append(", ");
  367.         }
  368.     }    
  369.  
  370.     private class NBTListIterator implements ListIterator {
  371.         ListIterator handleIterator
  372.    
  373.         public NBTListIterator(iterator){
  374.             this.handleIterator = iterator;
  375.         }
  376.         public boolean hasNext() {
  377.             return handleIterator.hasNext()
  378.         }
  379.         public Object next() {
  380.             return getNBT( handleIterator.next() )
  381.         }
  382.         public boolean hasPrevious() {
  383.             return handleIterator.hasPrevious()
  384.         }
  385.         public Object previous() {
  386.             return getNBT( handleIterator.previous() )
  387.         }
  388.         public int nextIndex() {
  389.             handleIterator.nextIndex()
  390.         }
  391.         public int previousIndex() {
  392.             handleIterator.previousIndex()
  393.         }
  394.         public void remove() {
  395.             handleIterator.remove()
  396.         }
  397.         public void set(Object value) {
  398.             if(classNBTBase.isInstance(value)) handleIterator.set(value.clone())
  399.             else handleIterator.set( wrapNBT(value) )
  400.         }
  401.         public void add(Object value) {
  402.             if(classNBTBase.isInstance(value)) handleIterator.add(value.clone())
  403.             else handleIterator.add( wrapNBT(value) )
  404.         }
  405.     }
  406. }
  407.  
  408. Script.metaClass.nbt = { Collection t ->
  409.     return new NBTList( wrapNBT(t) );
  410. }
  411. Script.metaClass.nbt = { Object[] t ->
  412.     return new NBTList( wrapNBT(t as List) );
  413. }
  414. Script.metaClass.nbt = { Map t ->
  415.     return new NBTCompound( wrapNBT(t) );
  416. }
  417.  
  418. Script.metaClass.nbt = { def object, Closure handle ->
  419.     def main = delegate.nbt object
  420.     def temp = main.clone()
  421.     def result = handle temp
  422.     if (main != temp) delegate.nbt object, temp
  423.     return result
  424. }
  425.  
  426. Script.metaClass.nbt = { Entity e ->
  427.     def basic = classNBTTagCompound.newInstance("")
  428.     e.handle.e basic
  429.     new NBTCompound (basic)
  430. }
  431. Script.metaClass.nbt = { Entity e, NBTCompound vals ->
  432.     e.handle.f vals.@handle
  433.     return true
  434. }
  435.  
  436. int maxDist = Server.getViewDistance() * 32
  437. Script.metaClass.nbt = { Block b ->
  438.     def tile = b.world.getTileEntityAt b.x, b.y, b.z
  439.     if (!tile) return null
  440.     def basic = classNBTTagCompound.newInstance ""
  441.     tile.b basic
  442.     result = new NBTCompound (basic)
  443.     result.remove "x"
  444.     result.remove "y"
  445.     result.remove "z"
  446.     result
  447. }
  448. Script.metaClass.nbt = { Block b, NBTCompound vals ->
  449.     def tile = b.world.getTileEntityAt b.x, b.y, b.z
  450.     if (!tile) return false
  451.     vals = vals.clone()
  452.     if (!vals.x) vals.x = b.x
  453.     if (!vals.y) vals.y = b.y
  454.     if (!vals.z) vals.z = b.z
  455.     tile.a vals.handle()
  456.     def packet = tile.updatePacket;
  457.     b.world.players.findAll {
  458.         it.location.distance(b.location) < maxDist
  459.     }.each {
  460.         it.handle.playerConnection.sendPacket packet
  461.     }
  462.     return true
  463. }
  464.  
  465. Script.metaClass.nbt = { ItemStack item ->
  466.     def tag = item.handle.tag
  467.     if (tag == null) return null
  468.     else new NBTCompound( tag.clone() )
  469. }
  470. Script.metaClass.nbt = { ItemStack item, NBTCompound vals ->
  471.     item.handle.tag = vals.clone().handle();
  472. }
  473.  
  474. return [name:"NBTWrapper",version:[1,0,0]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement