SHARE
TWEET

Untitled

a guest Jun 19th, 2017 52 in 25 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Copyright (C) 2015 Souleau Alexandre mrsheepsheepandcie@gmail.com
  3.  *
  4.  * Tint Health is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or at your option) any later version.
  5.  *
  6.  * Tint Health is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See http://www.gnu.org/licenses/ for more details.
  7.  */
  8.  
  9. package fr.mrsheepsheep.tinthealth;
  10.  
  11. import java.lang.reflect.Constructor;
  12. import java.lang.reflect.Field;
  13. import java.lang.reflect.InvocationTargetException;
  14. import java.lang.reflect.Method;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17.  
  18. import org.bukkit.Bukkit;
  19. import org.bukkit.entity.Damageable;
  20. import org.bukkit.entity.Player;
  21.  
  22. public class THFunctions {
  23.    
  24.     private static Method handle, sendPacket;
  25.     private static Method center, distance, time, movement;
  26.     private static Field player_connection;
  27.     private static Constructor<?> constructor, border_constructor;
  28.     private static Object constant;
  29.    
  30.     static {
  31.         try {
  32.             handle = getClass("org.bukkit.craftbukkit", "entity.CraftPlayer").getMethod("getHandle");
  33.             player_connection = getClass("net.minecraft.server", "EntityPlayer").getField("playerConnection");
  34.             for (Method m : getClass("net.minecraft.server", "PlayerConnection").getMethods()) {
  35.                 if (m.getName().equals("sendPacket")) {
  36.                     sendPacket = m;
  37.                     break;
  38.                 }
  39.             }
  40.             Class<?> enumclass;
  41.             try {
  42.                 enumclass = getClass("net.minecraft.server", "EnumWorldBorderAction");
  43.             } catch(ClassNotFoundException x) {
  44.                 enumclass = getClass("net.minecraft.server", "PacketPlayOutWorldBorder$EnumWorldBorderAction");
  45.             }
  46.             constructor = getClass("net.minecraft.server", "PacketPlayOutWorldBorder").getConstructor(getClass("net.minecraft.server", "WorldBorder"), enumclass);
  47.             border_constructor = getClass("net.minecraft.server", "WorldBorder").getConstructor();
  48.            
  49.             Method[] methods = getClass("net.minecraft.server", "WorldBorder").getMethods();
  50.  
  51.             String setCenter = "setCenter";
  52.             String setWarningDistance = "setWarningDistance";
  53.             String setWarningTime = "setWarningTime";
  54.             String transitionSizeBetween = "transitionSizeBetween";
  55.            
  56.             if (!inClass(methods, setCenter))
  57.                 setCenter = "c";
  58.             if (!inClass(methods, setWarningDistance))
  59.                 setWarningDistance = "c";
  60.             if (!inClass(methods, setWarningTime))
  61.                 setWarningTime = "b";
  62.             if (!inClass(methods, transitionSizeBetween))
  63.                 transitionSizeBetween = "a";
  64.            
  65.             center = getClass("net.minecraft.server", "WorldBorder").getMethod(setCenter, double.class, double.class);
  66.             distance = getClass("net.minecraft.server", "WorldBorder").getMethod(setWarningDistance, int.class);
  67.             time = getClass("net.minecraft.server", "WorldBorder").getMethod(setWarningTime, int.class);
  68.             movement = getClass("net.minecraft.server", "WorldBorder").getMethod(transitionSizeBetween, double.class, double.class, long.class);
  69.            
  70.             for (Object o: enumclass.getEnumConstants()) {
  71.                 if (o.toString().equals("INITIALIZE")) {
  72.                     constant = o;
  73.                     break;
  74.                 }
  75.             }
  76.         } catch (Exception e) {
  77.             e.printStackTrace();
  78.         }
  79.     }
  80.    
  81.     private static boolean inClass(Method[] methods, String methodName){
  82.         for (Method m : methods)
  83.             if (m.getName() == methodName)
  84.                 return true;
  85.         return false;
  86.     }
  87.    
  88.     private static Class<?> getClass(String prefix, String name) throws Exception {
  89.         return Class.forName(new StringBuilder().append(prefix + ".").append(Bukkit.getServer().getClass().getPackage().getName().substring(Bukkit.getServer().getClass().getPackage().getName().lastIndexOf(".") + 1)).append(".").append(name).toString());
  90.     }
  91.    
  92.     TintHealth plugin;
  93.     protected List<String> togglelist = new ArrayList<String>();
  94.  
  95.     protected THFunctions(TintHealth plugin) {
  96.         this.plugin = plugin;
  97.     }
  98.  
  99.     protected void sendBorder(Player p, int percentage){
  100.         percentage = Math.round(percentage / plugin.intensity);
  101.         setBorder(p, percentage);
  102.         if (plugin.fade && p.hasPermission("tinthealth.fade")) fadeBorder(p, percentage, plugin.fadetime);
  103.     }
  104.  
  105.     protected void fadeBorder(Player p, int percentage, long time){
  106.         int dist = -10000 * percentage + 1300000;
  107.         sendWorldBorderPacket(p, 0, 200000D, (double) dist, (long) 1000 * time + 4000); //Add 4000 to make sure the "security" zone does not count in the fade time
  108.         plugin.debug("Sent fade border for player " + p.getName());
  109.     }
  110.  
  111.     protected void removeBorder(Player p) {
  112.         sendWorldBorderPacket(p, 0, 200000D, 200000D, 0);
  113.         plugin.debug("Removed tint for player " + p.getName());
  114.     }
  115.  
  116.     protected void setBorder(Player p, int percentage){
  117.         int dist = -10000 * percentage + 1300000;
  118.         sendWorldBorderPacket(p, dist, 200000D, 200000D, 0);
  119.         plugin.debug("Set " + percentage + "% tint for player " + p.getName());
  120.     }
  121.  
  122.     protected void sendWorldBorderPacket(Player p, int dist, double oldradius, double newradius, long delay) {
  123. //     
  124. //          New Reflection Version
  125. //     
  126.         try {
  127.             Object worldborder = border_constructor.newInstance();
  128.             center.invoke(worldborder, p.getLocation().getX(), p.getLocation().getY());
  129.             distance.invoke(worldborder, dist);
  130.             time.invoke(worldborder, 15);
  131.             movement.invoke(worldborder, oldradius, newradius, delay);
  132.            
  133.             Object packet = constructor.newInstance(worldborder, constant);
  134.             //Object packet = new PacketPlayOutWorldParticles(EnumParticle.HEART, false, -4000, 100, -4000, 0.5F, 0.5F, 0.5F, 0F, 1, 0);
  135.             sendPacket.invoke(player_connection.get(handle.invoke(p)), packet);
  136.         } catch(IllegalAccessException e){
  137.             e.printStackTrace();
  138.         }
  139.         catch(InstantiationException e){
  140.             e.printStackTrace();
  141.         }
  142.         catch(InvocationTargetException e){
  143.             e.getCause().printStackTrace();
  144.         }
  145.         catch(NullPointerException e){
  146.             e.printStackTrace();
  147.         }
  148. //          Old ProtocollLib Version:
  149. //     
  150. //      border.getWorldBorderActions().write(0, WorldBorderAction.INITIALIZE);
  151. //      border.getIntegers()
  152. //      .write(0, 29999984)
  153. //      .write(1, 15)
  154. //      .write(2, dist);
  155. //      border.getLongs()
  156. //      .write(0, time);
  157. //      border.getDoubles()
  158. //      .write(0, p.getLocation().getX())
  159. //      .write(1, p.getLocation().getY())
  160. //      .write(2, newradius)
  161. //      .write(3, oldradius);
  162. //      try {
  163. //          protocolManager.sendServerPacket(p, border);
  164. //      } catch (InvocationTargetException e) {
  165. //          throw new RuntimeException("Cannot send packet " + border, e);
  166. //      }
  167.     }
  168.  
  169.     protected int getPlayerHealth(Player p){
  170.         return (int) ((Damageable) p).getHealth();
  171.     }
  172.  
  173.     @SuppressWarnings("deprecation")
  174.     protected int getMaxPlayerHealth(Player p){
  175.         return (int) ((Damageable) p).getMaxHealth();
  176.     }
  177.  
  178.     @SuppressWarnings("deprecation")
  179.     protected int getPlayerMissingHearts(Player p){
  180.         return (int) ((Damageable) p).getMaxHealth();
  181.     }
  182.  
  183.     protected int getPlayerHealthPercentage(Player p){
  184.         int health = getPlayerHealth(p);
  185.         int maxhealth = getMaxPlayerHealth(p);
  186.         return (Math.round((health * 100) / maxhealth));
  187.     }
  188.  
  189.     protected void disablePlayerTint(Player p){
  190.         togglelist.add(p.getName());
  191.     }
  192.  
  193.     protected void enablePlayerTint(Player p){
  194.         String pname = p.getName();
  195.         if (togglelist.contains(pname)) togglelist.remove(pname);
  196.     }
  197.  
  198.     protected void togglePlayerTint(Player p){
  199.         if (isTintEnabled(p)) disablePlayerTint(p);
  200.         else enablePlayerTint(p);
  201.     }
  202.  
  203.     protected boolean isTintEnabled(Player p) {
  204.         return !togglelist.contains(p.getName());
  205.     }
  206. }
RAW Paste Data
Want to get better at Java?
Learn to code Java in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top