SHARE
TWEET

Untitled

a guest May 21st, 2016 20 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package de.klein5.TowerDefense.datawatcher;
  2.  
  3. import java.lang.reflect.Field;
  4.  
  5. import org.bukkit.Bukkit;
  6.  
  7. import de.klein5.TowerDefense.datawatcher.Reflection.FieldAccessor;
  8.  
  9. public enum EnumDataWatcherObjects {
  10.  
  11.     ENTITY_BITMASK_00("Entity","ay"),
  12.     ENTITY_AIR_01("Entity","az"),
  13.     ENTITY_NAME_02("Entity","aA"),
  14.     ENTITY_NAME_VISABLE_03("Entity","aB"),
  15.     ENTITY_SILENT_04("Entity","aC"),
  16.     LIVING_HAND_05("EntityLiving","at"),
  17.     LIVING_HEALTH_06("EntityLiving","HEALTH"),
  18.     LIVING_POITION_COLOR_07("EntityLiving","f"),
  19.     LIVING_POITION_AMBIENT_08("EntityLiving","g"),
  20.     LIVING_ARROWS_09("EntityLiving","h"),
  21.     HUMAN_ABSORPTION_10("EntityHuman","a"),
  22.     HUMAN_SCORE_11("EntityHuman","b"),
  23.     HUMAN_SKIN_12("EntityHuman","bq"),
  24.     HUMAN_UNKNOWN_13("EntityHuman","br");
  25.    
  26.     private EnumDataWatcherObjects(String owner, String field) {
  27.  
  28.         Class<?> ownerClass = getClass(ServerPackets.MINECRAFT_PACKAGE, owner);
  29.         this.object = getField(ownerClass, field).get(null);
  30.        
  31.     }
  32.    
  33.     private Object object;
  34.    
  35.     public Object getObject(){
  36.         return object;
  37.     }
  38.     public static Class<?> getClass(ServerPackets packet, String name) {
  39.         try {
  40.             return Class.forName(packet + name);
  41.         } catch (ClassNotFoundException e) {
  42.             e.printStackTrace();
  43.         }
  44.         return null;
  45.     }
  46.     public static <T> FieldAccessor<T> getField(Class<?> target, String fieldName) {
  47.         return getField(target, fieldName, null, 0);
  48.     }
  49.  
  50.     public static <T> FieldAccessor<T> getField(Class<?> target, Class<T> fieldType, int index) {
  51.         return getField(target, null, fieldType, index);
  52.     }
  53.  
  54.     private static <T> FieldAccessor<T> getField(Class<?> target, String fieldName, Class<T> fieldType, int index) {
  55.         for (final Field field : target.getDeclaredFields())
  56.             if ((fieldName == null || fieldName.equals(field.getName())) && (fieldType == null || (fieldType.isAssignableFrom(field.getType()) && index-- <= 0))) {
  57.                 field.setAccessible(true);
  58.                 return new FieldAccessor<T>() {
  59.  
  60.                     @Override
  61.                     @SuppressWarnings("unchecked")
  62.                     public T get(Object target) {
  63.                         try {
  64.                             return (T) field.get(target);
  65.                         } catch (IllegalAccessException e) {
  66.                             throw new IllegalStateException("Cannot use reflection.", e);
  67.                         } catch (IllegalArgumentException e) {
  68.                             throw e;
  69.                         }
  70.                     }
  71.  
  72.                     @Override
  73.                     public void set(Object target, Object value) {
  74.                         try {
  75.                             field.set(target, value);
  76.                         } catch (IllegalAccessException e) {
  77.                             throw new IllegalStateException("Cannot use reflection.", e);
  78.                         } catch (IllegalArgumentException e) {
  79.                             throw e;
  80.                         }
  81.                     }
  82.  
  83.                     public Field getField() {
  84.                         return field;
  85.                     }
  86.  
  87.                     @Override
  88.                     public boolean hasField(Object target) {
  89.                         // TODO Auto-generated method stub
  90.                         return false;
  91.                     }
  92.                 };
  93.             }
  94.  
  95.         if (target.getSuperclass() != null)
  96.             return getField(target.getSuperclass(), fieldName, fieldType, index);
  97.  
  98.         throw new IllegalStateException(String.format("Unable to find field %s (%s).", fieldName, fieldType));
  99.     }
  100.     public enum ServerPackets {
  101.          
  102.         MINECRAFT_PACKAGE("net.minecraft.server." + Bukkit.getServer().getClass().getPackage().getName().substring(23));
  103.  
  104.         ServerPackets(String source) {
  105.             this.name = source;
  106.         }
  107.  
  108.         private final String name;
  109.  
  110.         @Override
  111.         public String toString() {
  112.             return this.name + ".";
  113.         }
  114.     }
  115. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top