lNockl

ParticleEffect

Feb 16th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 26.60 KB | None | 0 0
  1. package me.filipenock.mg.Utils;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Method;
  5. import java.util.Collection;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.Color;
  9. import org.bukkit.Location;
  10. import org.bukkit.entity.Player;
  11.  
  12.  
  13. @SuppressWarnings("all")
  14. public enum ParticleEffect
  15. {
  16.  
  17.    //EXAMPLE:
  18.    // ParticleEffect.ANGRY_VILLAGER.sendToPlayer(player, location, offsetX, offsetY, offsetZ, speed, count);
  19.  
  20.  
  21.     HUGE_EXPLOSION("hugeexplosion", "EXPLOSION_HUGE"),
  22.     LARGE_EXPLODE("largeexplode", "EXPLOSION_LARGE"),
  23.     BUBBLE("bubble", "WATER_BUBBLE"),
  24.     SUSPEND("suspended", "SUSPENDED"),
  25.     DEPTH_SUSPEND("depthsuspend", "SUSPENDED_DEPTH"),
  26.     MAGIC_CRIT("magicCrit", "CRIT_MAGIC"),
  27.     MOB_SPELL("mobSpell", "SPELL_MOB", true),
  28.     MOB_SPELL_AMBIENT("mobSpellAmbient", "SPELL_MOB_AMBIENT"),
  29.     INSTANT_SPELL("instantSpell", "SPELL_INSTANT"),
  30.     WITCH_MAGIC("witchMagic", "SPELL_WITCH"),
  31.     EXPLODE("explode", "EXPLOSION_NORMAL"),
  32.     SPLASH("splash", "WATER_SPLASH"),
  33.     LARGE_SMOKE("largesmoke", "SMOKE_LARGE"),
  34.     RED_DUST("reddust", "REDSTONE", true),
  35.     SNOWBALL_POOF("snowballpoof", "SNOWBALL"),
  36.     ANGRY_VILLAGER("angryVillager", "VILLAGER_ANGRY"),
  37.     HAPPY_VILLAGER("happyVillager", "VILLAGER_HAPPY"),
  38.     EXPLOSION_NORMAL(ParticleEffect.EXPLODE.getName()),
  39.     EXPLOSION_LARGE(ParticleEffect.LARGE_EXPLODE.getName()),
  40.     EXPLOSION_HUGE(ParticleEffect.HUGE_EXPLOSION.getName()),
  41.     FIREWORKS_SPARK("fireworksSpark"),
  42.     WATER_BUBBLE(ParticleEffect.BUBBLE.getName()),
  43.     WATER_SPLASH(ParticleEffect.SPLASH.getName()),
  44.     WATER_WAKE("wake"),
  45.     SUSPENDED(ParticleEffect.SUSPEND.getName()),
  46.     SUSPENDED_DEPTH(ParticleEffect.DEPTH_SUSPEND.getName()),
  47.     CRIT("crit"),
  48.     CRIT_MAGIC(ParticleEffect.MAGIC_CRIT.getName()),
  49.     SMOKE_NORMAL("smoke"),
  50.     SMOKE_LARGE(ParticleEffect.LARGE_SMOKE.getName()),
  51.     SPELL("spell"),
  52.     SPELL_INSTANT(ParticleEffect.INSTANT_SPELL.getName()),
  53.     SPELL_MOB(ParticleEffect.MOB_SPELL.getName(), true),
  54.     SPELL_MOB_AMBIENT(ParticleEffect.MOB_SPELL_AMBIENT.getName()),
  55.     SPELL_WITCH(ParticleEffect.WITCH_MAGIC.getName()),
  56.     DRIP_WATER("dripWater"),
  57.     DRIP_LAVA("dripLava"),
  58.     VILLAGER_ANGRY(ParticleEffect.ANGRY_VILLAGER.getName()),
  59.     VILLAGER_HAPPY(ParticleEffect.HAPPY_VILLAGER.getName()),
  60.     TOWN_AURA("townaura"),
  61.     NOTE("note", true),
  62.     PORTAL("portal"),
  63.     ENCHANTMENT_TABLE("enchantmenttable"),
  64.     FLAME("flame"),
  65.     LAVA("lava"),
  66.     FOOTSTEP("footstep"),
  67.     CLOUD("cloud"),
  68.     REDSTONE("reddust", true),
  69.     SNOWBALL("snowballpoof"),
  70.     SNOW_SHOVEL("snowshovel"),
  71.     SLIME("slime"),
  72.     HEART("heart"),
  73.     BARRIER("barrier"),
  74.     ITEM_CRACK("iconcrack_"),
  75.     BLOCK_CRACK("blockcrack_"),
  76.     BLOCK_DUST("blockdust_"),
  77.     WATER_DROP("droplet"),
  78.     ITEM_TAKE("take"),
  79.     MOB_APPEARANCE("mobappearance");
  80.    
  81.     private String particleName;
  82.     private String enumValue;
  83.     private boolean hasColor;
  84.     private static Class<?> nmsPacketPlayOutParticle;
  85.     private static Class<?> nmsEnumParticle;
  86.     private static int particleRange;
  87.     private static Class<?> nmsPlayerConnection;
  88.     private static Class<?> nmsEntityPlayer;
  89.     private static Class<?> ioNettyChannel;
  90.     private static Method nmsNetworkGetVersion;
  91.     private static Field nmsFieldPlayerConnection;
  92.     private static Field nmsFieldNetworkManager;
  93.     private static Field nmsFieldNetworkManagerI;
  94.     private static Field nmsFieldNetworkManagerM;
  95.    
  96.     private ParticleEffect(String particleName, String enumValue, boolean hasColor) {
  97.         this.particleName = particleName;
  98.         this.enumValue = enumValue;
  99.         this.hasColor = hasColor;
  100.     }
  101.    
  102.     private ParticleEffect(String particleName, String enumValue) {
  103.         this(particleName, enumValue, false);
  104.     }
  105.    
  106.     private ParticleEffect(String particleName) {
  107.         this(particleName, null);
  108.     }
  109.    
  110.     private ParticleEffect(String particleName, boolean hasColor) {
  111.         this(particleName, null, hasColor);
  112.     }
  113.    
  114.     public String getName() {
  115.         return this.particleName;
  116.     }
  117.    
  118.     public boolean hasColor() {
  119.         return this.hasColor;
  120.     }
  121.    
  122.     @Deprecated
  123.     public static void setRange(int range) {
  124.         if (range < 0) {
  125.             throw new IllegalArgumentException("Range must be positive!");
  126.         }
  127.         ParticleEffect.particleRange = range;
  128.     }
  129.    
  130.     @Deprecated
  131.     public static int getRange() {
  132.         return ParticleEffect.particleRange;
  133.     }
  134.    
  135.     @Deprecated
  136.     private void sendToPlayer(Player player, Location location, float offsetX, float offsetY, float offsetZ, float speed, int count, int... extra) throws Exception {
  137.         this.sendToPlayer(player, location, offsetX, offsetY, offsetZ, speed, count, false, extra);
  138.     }
  139.    
  140.     private void sendToPlayer(Player player, Location location, float offsetX, float offsetY, float offsetZ, float speed, int count, boolean force, int... extra) throws Exception {
  141.         if (!force && !isPlayerInRange(player, location)) {
  142.             return;
  143.         }
  144.         if (ReflectionUtilities.getVersion().contains("v1_8")) {
  145.             try {
  146.                 if (ParticleEffect.nmsEnumParticle == null) {
  147.                     ParticleEffect.nmsEnumParticle = ReflectionUtilities.getNMSClass("EnumParticle");
  148.                 }
  149.                 if (this == ParticleEffect.BLOCK_CRACK) {
  150.                     int id = 0;
  151.                     int data = 0;
  152.                     if (extra.length > 0) {
  153.                         id = extra[0];
  154.                     }
  155.                     if (extra.length > 1) {
  156.                         data = extra[1];
  157.                     }
  158.                     extra = new int[] { id, id | data << 12 };
  159.                 }
  160.                 Object packet = ParticleEffect.nmsPacketPlayOutParticle.getConstructor(ParticleEffect.nmsEnumParticle, Boolean.TYPE, Float.TYPE, Float.TYPE, Float.TYPE, Float.TYPE, Float.TYPE, Float.TYPE, Float.TYPE, Integer.TYPE, int[].class).newInstance(getEnum(ParticleEffect.nmsEnumParticle.getName() + "." + ((this.enumValue != null) ? this.enumValue : this.name().toUpperCase())), true, (float)location.getX(), (float)location.getY(), (float)location.getZ(), offsetX, offsetY, offsetZ, speed, count, extra);
  161.                 Object handle = ReflectionUtilities.getHandle(player);
  162.                 Object connection = ReflectionUtilities.getField(handle.getClass(), "playerConnection").get(handle);
  163.                 ReflectionUtilities.getMethod(connection.getClass(), "sendPacket", (Class<?>[])new Class[0]).invoke(connection, packet);
  164.                 return;
  165.             }
  166.             catch (Exception e) {
  167.                 throw e;
  168.             }
  169.         }
  170.         try {
  171.             if (this.particleName == null) {
  172.                 this.particleName = this.name().toLowerCase();
  173.             }
  174.             String name = this.particleName;
  175.             if (this == ParticleEffect.BLOCK_CRACK || this == ParticleEffect.ITEM_CRACK || this == ParticleEffect.BLOCK_DUST) {
  176.                 int id2 = 0;
  177.                 int data2 = 0;
  178.                 if (extra.length > 0) {
  179.                     id2 = extra[0];
  180.                 }
  181.                 if (extra.length > 1) {
  182.                     data2 = extra[1];
  183.                 }
  184.                 name = name + id2 + "_" + data2;
  185.             }
  186.             Object packet2 = ParticleEffect.nmsPacketPlayOutParticle.getConstructor(String.class, Float.TYPE, Float.TYPE, Float.TYPE, Float.TYPE, Float.TYPE, Float.TYPE, Float.TYPE, Integer.TYPE).newInstance(name, (float)location.getX(), (float)location.getY(), (float)location.getZ(), offsetX, offsetY, offsetZ, speed, count);
  187.             Object handle2 = ReflectionUtilities.getHandle(player);
  188.             Object connection2 = ReflectionUtilities.getField(handle2.getClass(), "playerConnection").get(handle2);
  189.             ReflectionUtilities.getMethod(connection2.getClass(), "sendPacket", (Class<?>[])new Class[0]).invoke(connection2, packet2);
  190.         }
  191.         catch (Exception e) {
  192.             throw e;
  193.         }
  194.     }
  195.    
  196.     public void sendToPlayer(Player player, Location location, float offsetX, float offsetY, float offsetZ, float speed, int count, boolean force) throws Exception {
  197.         this.sendToPlayer(player, location, offsetX, offsetY, offsetZ, speed, count, force, new int[0]);
  198.     }
  199.    
  200.     @Deprecated
  201.     public void sendToPlayer(Player player, Location location, float offsetX, float offsetY, float offsetZ, float speed, int count) throws Exception {
  202.         this.sendToPlayer(player, location, offsetX, offsetY, offsetZ, speed, count, false);
  203.     }
  204.    
  205.     @Deprecated
  206.     public void sendToPlayers(Collection<? extends Player> players, Location location, float offsetX, float offsetY, float offsetZ, float speed, int count) throws Exception {
  207.         for (Player p : players) {
  208.             this.sendToPlayer(p, location, offsetX, offsetY, offsetZ, speed, count);
  209.         }
  210.     }
  211.    
  212.     public void sendToPlayers(Collection<? extends Player> players, Location location, float offsetX, float offsetY, float offsetZ, float speed, int count, boolean force) throws Exception {
  213.         for (Player p : players) {
  214.             this.sendToPlayer(p, location, offsetX, offsetY, offsetZ, speed, count, force);
  215.         }
  216.     }
  217.    
  218.     @Deprecated
  219.     public void sendToPlayers(Player[] players, Location location, float offsetX, float offsetY, float offsetZ, float speed, int count) throws Exception {
  220.         for (Player p : players) {
  221.             this.sendToPlayer(p, location, offsetX, offsetY, offsetZ, speed, count);
  222.         }
  223.     }
  224.    
  225.     @Deprecated
  226.     public void sendColor(Player p, Location location, Color color) throws Exception {
  227.         if (!this.hasColor) {
  228.             return;
  229.         }
  230.         this.sendToPlayer(p, location, this.getColor(color.getRed()), this.getColor(color.getGreen()), this.getColor(color.getBlue()), 1.0f, 0);
  231.     }
  232.    
  233.     public void sendColor(Player p, Location location, Color color, boolean force) throws Exception {
  234.         if (!this.hasColor) {
  235.             return;
  236.         }
  237.         this.sendToPlayer(p, location, this.getColor(color.getRed()), this.getColor(color.getGreen()), this.getColor(color.getBlue()), 1.0f, 0, force);
  238.     }
  239.    
  240.     @Deprecated
  241.     public void sendColor(Player p, Location location, java.awt.Color color) throws Exception {
  242.         if (!this.hasColor) {
  243.             return;
  244.         }
  245.         this.sendToPlayer(p, location, this.getColor(color.getRed()), this.getColor(color.getGreen()), this.getColor(color.getBlue()), 1.0f, 0);
  246.     }
  247.    
  248.     public void sendColor(Player p, Location location, java.awt.Color color, boolean force) throws Exception {
  249.         if (!this.hasColor) {
  250.             return;
  251.         }
  252.         this.sendToPlayer(p, location, this.getColor(color.getRed()), this.getColor(color.getGreen()), this.getColor(color.getBlue()), 1.0f, 0, force);
  253.     }
  254.    
  255.     @Deprecated
  256.     public void sendColor(Collection<? extends Player> players, Location location, java.awt.Color color) throws Exception {
  257.         if (!this.hasColor) {
  258.             return;
  259.         }
  260.         for (Player p : players) {
  261.             this.sendColor(p, location, color);
  262.         }
  263.     }
  264.    
  265.     public void sendColor(Collection<? extends Player> players, Location location, java.awt.Color color, boolean force) throws Exception {
  266.         if (!this.hasColor) {
  267.             return;
  268.         }
  269.         for (Player p : players) {
  270.             this.sendColor(p, location, color, force);
  271.         }
  272.     }
  273.    
  274.     public void sendColor(Collection<? extends Player> players, Location location, Color color) throws Exception {
  275.         if (!this.hasColor) {
  276.             return;
  277.         }
  278.         for (Player p : players) {
  279.             this.sendColor(p, location, color);
  280.         }
  281.     }
  282.    
  283.     @Deprecated
  284.     public void sendColor(Collection<? extends Player> players, Location location, Color color, boolean force) throws Exception {
  285.         if (!this.hasColor) {
  286.             return;
  287.         }
  288.         for (Player p : players) {
  289.             this.sendColor(p, location, color, force);
  290.         }
  291.     }
  292.    
  293.     @Deprecated
  294.     public void sendColor(Player[] players, Location location, Color color) throws Exception {
  295.         if (!this.hasColor) {
  296.             return;
  297.         }
  298.         for (Player p : players) {
  299.             this.sendColor(p, location, color);
  300.         }
  301.     }
  302.    
  303.     @Deprecated
  304.     public void sendColor(Player[] players, Location location, java.awt.Color color) throws Exception {
  305.         if (!this.hasColor) {
  306.             return;
  307.         }
  308.         for (Player p : players) {
  309.             this.sendColor(p, location, color);
  310.         }
  311.     }
  312.    
  313.     protected float getColor(float value) {
  314.         if (value <= 0.0f) {
  315.             value = -1.0f;
  316.         }
  317.         return value / 255.0f;
  318.     }
  319.    
  320.     @Deprecated
  321.     public void sendBlockCrack(Player player, Location location, int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int count) throws Exception {
  322.         if (this != ParticleEffect.BLOCK_CRACK) {
  323.             throw new IllegalArgumentException("This method is only available for BLOCK_CRACK!");
  324.         }
  325.         this.sendToPlayer(player, location, offsetX, offsetY, offsetZ, speed, count, id, data);
  326.     }
  327.    
  328.     public void sendBlockCrack(Player player, Location location, int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int count, boolean force) throws Exception {
  329.         if (this != ParticleEffect.BLOCK_CRACK) {
  330.             throw new IllegalArgumentException("This method is only available for BLOCK_CRACK!");
  331.         }
  332.         this.sendToPlayer(player, location, offsetX, offsetY, offsetZ, speed, count, force, id, data);
  333.     }
  334.    
  335.     @Deprecated
  336.     public void sendBlockCrack(Collection<? extends Player> players, Location location, int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int count) throws Exception {
  337.         if (this != ParticleEffect.BLOCK_CRACK) {
  338.             throw new IllegalArgumentException("This method is only available for BLOCK_CRACK!");
  339.         }
  340.         for (Player p : players) {
  341.             this.sendBlockCrack(p, location, id, data, offsetX, offsetY, offsetZ, speed, count);
  342.         }
  343.     }
  344.    
  345.     public void sendBlockCrack(Collection<? extends Player> players, Location location, int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int count, boolean force) throws Exception {
  346.         if (this != ParticleEffect.BLOCK_CRACK) {
  347.             throw new IllegalArgumentException("This method is only available for BLOCK_CRACK!");
  348.         }
  349.         for (Player p : players) {
  350.             this.sendBlockCrack(p, location, id, data, offsetX, offsetY, offsetZ, speed, count, force);
  351.         }
  352.     }
  353.    
  354.     @Deprecated
  355.     public void sendBlockCrack(Player[] players, Location location, int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int count) throws Exception {
  356.         if (this != ParticleEffect.BLOCK_CRACK) {
  357.             throw new IllegalArgumentException("This method is only available for BLOCK_CRACK!");
  358.         }
  359.         for (Player p : players) {
  360.             this.sendBlockCrack(p, location, id, data, offsetX, offsetY, offsetZ, speed, count);
  361.         }
  362.     }
  363.    
  364.     @Deprecated
  365.     public void sendItemCrack(Player player, Location location, int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int count) throws Exception {
  366.         if (this != ParticleEffect.ITEM_CRACK) {
  367.             throw new IllegalArgumentException("This method is only available for ITEM_CRACK!");
  368.         }
  369.         this.sendToPlayer(player, location, offsetX, offsetY, offsetZ, speed, count, id, data);
  370.     }
  371.    
  372.     public void sendItemCrack(Player player, Location location, int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int count, boolean force) throws Exception {
  373.         if (this != ParticleEffect.ITEM_CRACK) {
  374.             throw new IllegalArgumentException("This method is only available for ITEM_CRACK!");
  375.         }
  376.         this.sendToPlayer(player, location, offsetX, offsetY, offsetZ, speed, count, force, id, data);
  377.     }
  378.    
  379.     @Deprecated
  380.     public void sendItemCrack(Collection<? extends Player> players, Location location, int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int count) throws Exception {
  381.         if (this != ParticleEffect.ITEM_CRACK) {
  382.             throw new IllegalArgumentException("This method is only available for ITEM_CRACK!");
  383.         }
  384.         for (Player p : players) {
  385.             this.sendItemCrack(p, location, id, data, offsetX, offsetY, offsetZ, speed, count);
  386.         }
  387.     }
  388.    
  389.     public void sendItemCrack(Collection<? extends Player> players, Location location, int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int count, boolean force) throws Exception {
  390.         if (this != ParticleEffect.ITEM_CRACK) {
  391.             throw new IllegalArgumentException("This method is only available for ITEM_CRACK!");
  392.         }
  393.         for (Player p : players) {
  394.             this.sendItemCrack(p, location, id, data, offsetX, offsetY, offsetZ, speed, count, force);
  395.         }
  396.     }
  397.    
  398.     @Deprecated
  399.     public void sendItemCrack(Player[] players, Location location, int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int count) throws Exception {
  400.         if (this != ParticleEffect.ITEM_CRACK) {
  401.             throw new IllegalArgumentException("This method is only available for ITEM_CRACK!");
  402.         }
  403.         for (Player p : players) {
  404.             this.sendItemCrack(p, location, id, data, offsetX, offsetY, offsetZ, speed, count);
  405.         }
  406.     }
  407.    
  408.     @Deprecated
  409.     public void sendBlockDust(Player p, Location location, int id, float offsetX, float offsetY, float offsetZ, float speed, int count) throws Exception {
  410.         if (this != ParticleEffect.BLOCK_DUST) {
  411.             throw new IllegalArgumentException("This method is only available for BLOCK_DUST!");
  412.         }
  413.         this.sendToPlayer(p, location, offsetX, offsetY, offsetZ, speed, count, id);
  414.     }
  415.    
  416.     public void sendBlockDust(Player p, Location location, int id, float offsetX, float offsetY, float offsetZ, float speed, int count, boolean force) throws Exception {
  417.         if (this != ParticleEffect.BLOCK_DUST) {
  418.             throw new IllegalArgumentException("This method is only available for BLOCK_DUST!");
  419.         }
  420.         this.sendToPlayer(p, location, offsetX, offsetY, offsetZ, speed, count, force, id);
  421.     }
  422.    
  423.     @Deprecated
  424.     public void sendBlockDust(Collection<? extends Player> players, Location location, int id, float offsetX, float offsetY, float offsetZ, float speed, int count) throws Exception {
  425.         if (this != ParticleEffect.BLOCK_DUST) {
  426.             throw new IllegalArgumentException("This method is only available for BLOCK_DUST!");
  427.         }
  428.         for (Player p : players) {
  429.             this.sendBlockDust(p, location, id, offsetX, offsetY, offsetZ, speed, count);
  430.         }
  431.     }
  432.    
  433.     public void sendBlockDust(Collection<? extends Player> players, Location location, int id, float offsetX, float offsetY, float offsetZ, float speed, int count, boolean force) throws Exception {
  434.         if (this != ParticleEffect.BLOCK_DUST) {
  435.             throw new IllegalArgumentException("This method is only available for BLOCK_DUST!");
  436.         }
  437.         for (Player p : players) {
  438.             this.sendBlockDust(p, location, id, offsetX, offsetY, offsetZ, speed, count, force);
  439.         }
  440.     }
  441.    
  442.     @Deprecated
  443.     public void sendBlockDust(Player[] players, Location location, int id, float offsetX, float offsetY, float offsetZ, float speed, int count) throws Exception {
  444.         if (this != ParticleEffect.BLOCK_DUST) {
  445.             throw new IllegalArgumentException("This method is only available for BLOCK_DUST!");
  446.         }
  447.         for (Player p : players) {
  448.             this.sendBlockDust(p, location, id, offsetX, offsetY, offsetZ, speed, count);
  449.         }
  450.     }
  451.    
  452.     protected static int getVersion(Player p) {
  453.         try {
  454.             Object handle = ReflectionUtilities.getHandle(p);
  455.             Object connection = ParticleEffect.nmsFieldPlayerConnection.get(handle);
  456.             Object network = ParticleEffect.nmsFieldNetworkManager.get(connection);
  457.             Object channel;
  458.             if (ReflectionUtilities.getVersion().contains("1_7")) {
  459.                 channel = ParticleEffect.nmsFieldNetworkManagerM.get(network);
  460.             }
  461.             else {
  462.                 channel = ParticleEffect.nmsFieldNetworkManagerI.get(network);
  463.             }
  464.             Object version = ReflectionUtilities.getVersion().contains("1_7") ? ParticleEffect.nmsNetworkGetVersion.invoke(network, channel) : 47;
  465.             return (int)version;
  466.         }
  467.         catch (Exception e) {
  468.             e.printStackTrace();
  469.             return 0;
  470.         }
  471.     }
  472.    
  473.     private static Enum<?> getEnum(String enumFullName) {
  474.         String[] x = enumFullName.split("\\.(?=[^\\.]+$)");
  475.         if (x.length == 2) {
  476.             String enumClassName = x[0];
  477.             String enumName = x[1];
  478.             try {
  479.                 Class<Enum> cl = (Class<Enum>)Class.forName(enumClassName);
  480.                 return Enum.valueOf(cl, enumName);
  481.             }
  482.             catch (ClassNotFoundException e) {
  483.                 e.printStackTrace();
  484.             }
  485.         }
  486.         return null;
  487.     }
  488.    
  489.     public static boolean isPlayerInRange(Player p, Location center) {
  490.         double distance = 0.0;
  491.         return p.getLocation().getWorld().equals(center.getWorld()) && (distance = center.distanceSquared(p.getLocation())) <= Double.MAX_VALUE && distance < ParticleEffect.particleRange * ParticleEffect.particleRange;
  492.     }
  493.    
  494.     static {
  495.         ParticleEffect.nmsPacketPlayOutParticle = ReflectionUtilities.getNMSClass("PacketPlayOutWorldParticles");
  496.         ParticleEffect.particleRange = 25;
  497.         String ver = ReflectionUtilities.getVersion();
  498.         try {
  499.             ParticleEffect.nmsPlayerConnection = ReflectionUtilities.getNMSClass("PlayerConnection");
  500.             ParticleEffect.nmsEntityPlayer = ReflectionUtilities.getNMSClass("EntityPlayer");
  501.             ParticleEffect.ioNettyChannel = (ver.contains("1_7") ? Class.forName("net.minecraft.util.io.netty.channel.Channel") : Class.forName("io.netty.channel.Channel"));
  502.             ParticleEffect.nmsFieldPlayerConnection = ReflectionUtilities.getField(ParticleEffect.nmsEntityPlayer, "playerConnection");
  503.             ParticleEffect.nmsFieldNetworkManager = ReflectionUtilities.getField(ParticleEffect.nmsPlayerConnection, "networkManager");
  504.             ParticleEffect.nmsFieldNetworkManagerI = ReflectionUtilities.getField(ParticleEffect.nmsFieldNetworkManager.getType(), "i");
  505.             ParticleEffect.nmsFieldNetworkManagerM = ReflectionUtilities.getField(ParticleEffect.nmsFieldNetworkManager.getType(), "m");
  506.             ParticleEffect.nmsNetworkGetVersion = ReflectionUtilities.getMethod(ParticleEffect.nmsFieldNetworkManager.getType(), "getVersion", ParticleEffect.ioNettyChannel);
  507.         }
  508.         catch (Exception e) {
  509.             System.err.println("[ParticleLIB] Error while loading: " + e.getMessage());
  510.             e.printStackTrace(System.err);
  511.             Bukkit.getPluginManager().disablePlugin(Bukkit.getPluginManager().getPlugin("ParticleLIB"));
  512.         }
  513.     }
  514.    
  515.     public static class ReflectionUtilities
  516.     {
  517.         public static void setValue(Object instance, String fieldName, Object value) throws Exception {
  518.             Field field = instance.getClass().getDeclaredField(fieldName);
  519.             field.setAccessible(true);
  520.             field.set(instance, value);
  521.         }
  522.        
  523.         public static Object getValue(Object instance, String fieldName) throws Exception {
  524.             Field field = instance.getClass().getDeclaredField(fieldName);
  525.             field.setAccessible(true);
  526.             return field.get(instance);
  527.         }
  528.        
  529.         public static String getVersion() {
  530.             String name = Bukkit.getServer().getClass().getPackage().getName();
  531.             String version = name.substring(name.lastIndexOf(46) + 1) + ".";
  532.             return version;
  533.         }
  534.        
  535.         public static Class<?> getNMSClass(String className) {
  536.             String fullName = "net.minecraft.server." + getVersion() + className;
  537.             Class<?> clazz = null;
  538.             try {
  539.                 clazz = Class.forName(fullName);
  540.             }
  541.             catch (Exception e) {
  542.                 e.printStackTrace();
  543.             }
  544.             return clazz;
  545.         }
  546.        
  547.         public static Class<?> getOBCClass(String className) {
  548.             String fullName = "org.bukkit.craftbukkit." + getVersion() + className;
  549.             Class<?> clazz = null;
  550.             try {
  551.                 clazz = Class.forName(fullName);
  552.             }
  553.             catch (Exception e) {
  554.                 e.printStackTrace();
  555.             }
  556.             return clazz;
  557.         }
  558.        
  559.         public static Object getHandle(Object obj) {
  560.             try {
  561.                 return getMethod(obj.getClass(), "getHandle", (Class<?>[])new Class[0]).invoke(obj, new Object[0]);
  562.             }
  563.             catch (Exception e) {
  564.                 e.printStackTrace();
  565.                 return null;
  566.             }
  567.         }
  568.        
  569.         public static Field getField(Class<?> clazz, String name) {
  570.             try {
  571.                 Field field = clazz.getDeclaredField(name);
  572.                 field.setAccessible(true);
  573.                 return field;
  574.             }
  575.             catch (Exception e) {
  576.                 e.printStackTrace();
  577.                 return null;
  578.             }
  579.         }
  580.        
  581.         public static Method getMethod(Class<?> clazz, String name, Class<?>... args) {
  582.             for (Method m : clazz.getMethods()) {
  583.                 if (m.getName().equals(name) && (args.length == 0 || ClassListEqual(args, m.getParameterTypes()))) {
  584.                     m.setAccessible(true);
  585.                     return m;
  586.                 }
  587.             }
  588.             return null;
  589.         }
  590.        
  591.         public static boolean ClassListEqual(Class<?>[] l1, Class<?>[] l2) {
  592.             boolean equal = true;
  593.             if (l1.length != l2.length) {
  594.                 return false;
  595.             }
  596.             for (int i = 0; i < l1.length; ++i) {
  597.                 if (l1[i] != l2[i]) {
  598.                     equal = false;
  599.                     break;
  600.                 }
  601.             }
  602.             return equal;
  603.         }
  604.     }
  605. }
Advertisement
Add Comment
Please, Sign In to add comment