lNockl

Java

Dec 7th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.76 KB | None | 0 0
  1. import java.lang.reflect.Field;
  2. import java.lang.reflect.Method;
  3.  
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.block.Block;
  6. import org.bukkit.block.BlockState;
  7. import org.bukkit.entity.LivingEntity;
  8. import org.bukkit.inventory.ItemStack;
  9. import org.bukkit.potion.PotionEffectType;
  10.  
  11. public class BeaconState {
  12.     public static class ReflectionUtil {
  13.         private static final String MC_VERSION = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
  14.  
  15.         public static Class<?> forClassName(String name) {
  16.             try {
  17.                 return Class.forName(name.replace("%MC_VERSION%", MC_VERSION));
  18.             } catch (Throwable error) {
  19.                 return null;
  20.             }
  21.         }
  22.  
  23.         public static Method getMethod(Class<?> clazz, String methodName, Class<?>... params) {
  24.             try {
  25.                 return clazz.getMethod(methodName, params);
  26.             } catch (Throwable error) {
  27.                 return null;
  28.             }
  29.         }
  30.  
  31.         public static Field getField(Class<?> clazz, String fieldName) {
  32.             try {
  33.                 return clazz.getDeclaredField(fieldName);
  34.             } catch (Throwable error) {
  35.                 return null;
  36.             }
  37.         }
  38.     }
  39.    
  40.     private static final Class<?> beaconState = ReflectionUtil.forClassName("net.minecraft.server.%MC_VERSION%.TileEntityBeacon"),
  41.                                   craftBeacon = ReflectionUtil.forClassName("org.bukkit.craftbukkit.%MC_VERSION%.block.CraftBeacon"),
  42.                                   entityHuman = ReflectionUtil.forClassName("net.minecraft.server.%MC_VERSION%.EntityHuman");
  43.    
  44.     private static final Field tileEntityBeacon = ReflectionUtil.getField(craftBeacon, "beacon"),
  45.                               primary = ReflectionUtil.getField(beaconState, "f"),
  46.                               secondary = ReflectionUtil.getField(beaconState, "g");
  47.    
  48.     private static final Method getName = ReflectionUtil.getMethod(beaconState, "getName"),
  49.                                 setName = ReflectionUtil.getMethod(beaconState, "a", String.class),
  50.                                 tier = ReflectionUtil.getMethod(beaconState, "l"),
  51.                                 getItem = ReflectionUtil.getMethod(beaconState, "getItem", int.class),
  52.                                 setItem = ReflectionUtil.getMethod(beaconState, "setItem", int.class, ItemStack.class);
  53.    
  54.     static {
  55.         tileEntityBeacon.setAccessible(true);
  56.         primary.setAccessible(true);
  57.         secondary.setAccessible(true);
  58.     }
  59.    
  60.     private final Block block;
  61.     private final Object blockState;
  62.    
  63.     public BeaconState(Block block) {
  64.         this(block.getState());
  65.     }
  66.    
  67.     public BeaconState(BlockState state) {
  68.         if (craftBeacon.isInstance(state)) {
  69.             try {
  70.                 this.block = state.getBlock();
  71.                 this.blockState = tileEntityBeacon.get(craftBeacon.cast(state));
  72.             } catch (Throwable error) {
  73.                 throw new IllegalArgumentException("BlockState must be a instance of org.bukkit.craftbukkit.block.CraftBeacon");              
  74.             }
  75.         } else {
  76.             throw new IllegalArgumentException("BlockState must be a instance of org.bukkit.craftbukkit.block.CraftBeacon");
  77.         }
  78.     }
  79.    
  80.     public static BeaconState asBeacon(Block block) {
  81.         return new BeaconState(block);
  82.     }
  83.    
  84.     public static BeaconState asBeacon(BlockState state) {
  85.         return new BeaconState(state);
  86.     }
  87.    
  88.     public Block getBlock() {
  89.         return block;
  90.     }
  91.    
  92.     public String getName() {
  93.         try {
  94.             return (String) getName.invoke(blockState);
  95.         } catch (Throwable error) {
  96.             return null;
  97.         }
  98.     }
  99.    
  100.     public void setName(String name) {
  101.         try {
  102.             setName.invoke(blockState, name);
  103.         } catch (Throwable error) {
  104.             error.printStackTrace();
  105.         }
  106.     }
  107.    
  108.     public PotionEffectType getPrimary() {
  109.         try {
  110.             return PotionEffectType.getById(primary.getInt(blockState));
  111.         } catch (Throwable error) {
  112.             return null;
  113.         }
  114.     }
  115.    
  116.     public PotionEffectType getSecondary() {
  117.         try {
  118.             return PotionEffectType.getById(secondary.getInt(blockState));
  119.         } catch (Throwable error) {
  120.             return null;
  121.         }
  122.     }
  123.    
  124.     public void setPrimary(PotionEffectType type) {
  125.         try {
  126.             primary.setInt(blockState, type.getId());
  127.         } catch (Throwable error) {
  128.             error.printStackTrace();
  129.         }
  130.     }
  131.    
  132.     public void setSecondary(PotionEffectType type) {
  133.         try {
  134.             secondary.setInt(blockState, type.getId());
  135.         } catch (Throwable error) {
  136.             error.printStackTrace();
  137.         }
  138.     }
  139.    
  140.     public void setBoth(PotionEffectType type) {
  141.         setPrimary(type);
  142.         setSecondary(type);
  143.     }
  144.    
  145.     public int getTier() {
  146.         try {
  147.             return ((Integer) tier.invoke(blockState)).intValue();
  148.         } catch (Throwable error) {
  149.             error.printStackTrace();
  150.             return 0;
  151.         }
  152.     }
  153.    
  154.     public ItemStack getItem() {
  155.         try {
  156.             return (ItemStack) getItem.invoke(blockState, 0);
  157.         } catch (Throwable error) {
  158.             return null;
  159.         }
  160.     }
  161.    
  162.     public void setItem(ItemStack stack) {
  163.         try {
  164.             setItem.invoke(blockState, 0, stack);
  165.         } catch (Throwable error) {
  166.        
  167.         }
  168.     }
  169.    
  170.     public boolean applicableFor(LivingEntity entity) {
  171.         if (entityHuman.isInstance(entity)) {
  172.             return entity.getLocation().distanceSquared(block.getLocation()) <= 4096;
  173.         }
  174.         return false;
  175.     }
Advertisement
Add Comment
Please, Sign In to add comment