daily pastebin goal
15%
SHARE
TWEET

Untitled

a guest Jul 27th, 2016 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package de.creepit.creepcore.util;
  2.  
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.Field;
  5. import java.util.Collection;
  6. import java.util.HashMap;
  7. import java.util.HashSet;
  8. import java.util.Map.Entry;
  9. import java.util.Set;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.entity.Player;
  13.  
  14. import de.creepit.creepcore.CreepCore;
  15.  
  16. public class NameTagUtil{
  17.  
  18.     private static HashMap<String, Object> teams = new HashMap<>();
  19.  
  20.     @SuppressWarnings("rawtypes")
  21.     public static void updateTeams() {
  22.         for (Entry entry : teams.entrySet()) {
  23.             for (Player all : Bukkit.getOnlinePlayers()) {
  24.                 try {
  25.                     Constructor<?> scoreboardTeamConstructor = getNMSClass("PacketPlayOutScoreboardTeam")
  26.                             .getConstructor(getNMSClass("ScoreboardTeam"), int.class);
  27.  
  28.                     sendPacket(all, scoreboardTeamConstructor.newInstance(entry.getValue(), 1));
  29.  
  30.                     sendPacket(all, scoreboardTeamConstructor.newInstance(entry.getValue(), 0));
  31.                 } catch (Exception e) {
  32.                     e.printStackTrace();
  33.                 }
  34.             }
  35.  
  36.         }
  37.  
  38.     }
  39.     @SuppressWarnings("unchecked")
  40.     public static void removeFromTeam(Player player) {
  41.         teams.entrySet().forEach(entry -> {
  42.             Object packet = teams.get(entry.getKey());
  43.  
  44.             try {
  45.                 Field f = packet.getClass().getDeclaredField("c");
  46.  
  47.                 f.setAccessible(true);
  48.                 Set<String> list = new HashSet<>();
  49.                 list.addAll((Collection<? extends String>) f.get(packet));
  50.  
  51.                 if (list.contains(player.getName())) {
  52.                     list.remove(player.getName());
  53.                     setField(packet, "c", list);
  54.                     teams.put(entry.getKey(), packet);
  55.                 }
  56.  
  57.             } catch (Exception e) {
  58.                 e.printStackTrace();
  59.             }
  60.         });
  61.     }
  62.  
  63.     public static void addToTeam(Player player) {
  64.         removeFromTeam(player);
  65.         String suffix = String.valueOf(" §8●§7 ");
  66.         if (player.hasPermission("creepit.core.administrator")) {
  67.  
  68.             setPlayer(player, "administrator");
  69.  
  70.             player.setPlayerListName("§4Admin" + suffix + player.getName());
  71.  
  72.             return;
  73.         } else if (player.hasPermission("creepit.core.seniormoderator")) {
  74.             setPlayer(player, "srmoderator");
  75.             player.setPlayerListName("§cSr-Mod" + suffix + player.getName());
  76.             return;
  77.  
  78.         } else if (player.hasPermission("creepit.core.moderator")) {
  79.  
  80.             setPlayer(player, "moderator");
  81.             player.setPlayerListName("§cMod" + suffix + player.getName());
  82.             return;
  83.  
  84.         } else if (player.hasPermission("creepit.core.supporter")) {
  85.  
  86.             setPlayer(player, "supporter");
  87.             player.setPlayerListName("§9Sup" + suffix + player.getName());
  88.             return;
  89.  
  90.         } else if (player.hasPermission("creepit.core.headbuilder")) {
  91.  
  92.             setPlayer(player, "headbuilder");
  93.             player.setPlayerListName("§2Builder" + suffix + player.getName());
  94.             return;
  95.  
  96.         } else if (player.hasPermission("creepit.core.builder")) {
  97.  
  98.             setPlayer(player, "builder");
  99.             player.setPlayerListName("§eBuilder" + suffix + player.getName());
  100.             return;
  101.  
  102.         } else if (player.hasPermission("creepit.core.developer")) {
  103.  
  104.             setPlayer(player, "developer");
  105.             player.setPlayerListName("§dDev" + suffix + player.getName());
  106.             return;
  107.  
  108.         } else if (player.hasPermission("creepit.core.designer")) {
  109.  
  110.             setPlayer(player, "designer");
  111.             player.setPlayerListName("§3Design" + suffix + player.getName());
  112.             return;
  113.         } else if (player.hasPermission("creepit.core.youtuber")) {
  114.  
  115.             setPlayer(player, "youtuber");
  116.             player.setPlayerListName("§5" + player.getName());
  117.             return;
  118.         } else if (player.hasPermission("creepit.core.premiumplus")) {
  119.  
  120.             setPlayer(player, "premiumplus");
  121.             player.setPlayerListName("§6" + player.getName());
  122.             return;
  123.         } else if (player.hasPermission("creepit.core.premium")) {
  124.  
  125.             setPlayer(player, "premium");
  126.             player.setPlayerListName("§6" + player.getName());
  127.             return;
  128.         } else {
  129.             setPlayer(player, "spieler");
  130.             player.setPlayerListName("§a" + player.getName());
  131.             return;
  132.         }
  133.     }
  134.  
  135.     public static void initScoreboardTeams() {
  136.         String suffix = " §8●§7 ";
  137.  
  138.         try {
  139.             Constructor<?> boardConstructor = getNMSClass("Scoreboard").getConstructor();
  140.             Object board = boardConstructor.newInstance();
  141.             init("000Administrator", "administrator", "§4Admin" + suffix, board);
  142.             init("001SrModerator", "srmoderator", "§cSr-Mod" + suffix, board);
  143.             init("003Moderator", "moderator", "§cMod" + suffix, board);
  144.             init("004Supporter", "supporter", "§9Sup" + suffix, board);
  145.             init("005HeadBuilder", "headbuilder", "§2Builder" + suffix, board);
  146.             init("006Builder", "builder", "§eBuilder" + suffix, board);
  147.             init("007Developer", "developer", "§dDev" + suffix, board);
  148.             init("008Designer", "designer", "§3Design" + suffix, board);
  149.             init("009YouTuber", "youtuber", "§5", board);
  150.             init("010PremiumPlus", "premiumplus", "§6", board);
  151.             init("011Premium", "premium", "§6", board);
  152.             init("012Spieler", "spieler", "§a", board);
  153.         } catch (Exception e) {
  154.             e.printStackTrace();
  155.             Bukkit.getConsoleSender().sendMessage(CreepCore.getInstance().getConfiguration().getPrefix() + "§4Die Scoreboard Teams konnten nicht initialisiert werden!");
  156.         }
  157.  
  158.     }
  159.  
  160.     private static void init(String teamname, String HashMapName, String prefix, Object board) {
  161.  
  162.         try {
  163.  
  164.             Constructor<?> teamConstructor = getNMSClass("ScoreboardTeam").getConstructor(getNMSClass("Scoreboard"),
  165.                     String.class);
  166.  
  167.             Object packet = teamConstructor.newInstance(board, teamname);
  168.  
  169.             Object enumVisibility = getNMSClass("ScoreboardTeamBase").getDeclaredClasses()[0].getField("ALWAYS")
  170.                     .get(null);
  171.             setField(packet, "j", enumVisibility);
  172.  
  173.             setField(packet, "e", prefix);
  174.  
  175.             teams.put(HashMapName, packet);
  176.  
  177.         } catch (Exception e) {
  178.             e.printStackTrace();
  179.         }
  180.  
  181.     }
  182.  
  183.     @SuppressWarnings({ "rawtypes", "unchecked" })
  184.     private static void setPlayer(Player p, String HashMapName) {
  185.         Object packet = teams.get(HashMapName);
  186.         try {
  187.             Field f = packet.getClass().getDeclaredField("c");
  188.             f.setAccessible(true);
  189.             Set<String> list = new HashSet<>();
  190.             list.addAll((Collection<? extends String>) f.get(packet));
  191.             list.add(p.getName());
  192.             setField(packet, "c", list);
  193.  
  194.             teams.put(HashMapName, packet);
  195.  
  196.             for (Entry e : teams.entrySet()) {
  197.  
  198.                 Constructor<?> scoreboardTeamConstructor = getNMSClass("PacketPlayOutScoreboardTeam")
  199.                         .getConstructor(getNMSClass("ScoreboardTeam"), int.class);
  200.                 Object teampacket = scoreboardTeamConstructor.newInstance(e.getValue(), 0);
  201.  
  202.                 sendPacket(p, teampacket);
  203.  
  204.             }
  205.  
  206.         } catch (Exception e) {
  207.             e.printStackTrace();
  208.         }
  209.     }
  210.  
  211.     private static void setField(Object object, String fieldname, Object value) {
  212.         try {
  213.             Field field = object.getClass().getDeclaredField(fieldname);
  214.             field.setAccessible(true);
  215.             field.set(object, value);
  216.             field.setAccessible(false);
  217.         } catch (Exception ex) {
  218.             ex.printStackTrace();
  219.         }
  220.     }
  221.  
  222.     private static void sendPacket(Player player, Object packet) {
  223.         try {
  224.             Object handle = player.getClass().getMethod("getHandle").invoke(player);
  225.             Object playerConnection = handle.getClass().getField("playerConnection").get(handle);
  226.             playerConnection.getClass().getMethod("sendPacket", getNMSClass("Packet")).invoke(playerConnection, packet);
  227.         }
  228.  
  229.         catch (Exception e) {
  230.             e.printStackTrace();
  231.         }
  232.     }
  233.  
  234.     private static Class<?> getNMSClass(String name) {
  235.         String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
  236.         try {
  237.             return Class.forName("net.minecraft.server." + version + "." + name);
  238.         }
  239.  
  240.         catch (ClassNotFoundException e) {
  241.             e.printStackTrace();
  242.             return null;
  243.         }
  244.     }
  245.  
  246. }
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
 
Top