SHARE
TWEET

ScoreboardSign

a guest Jan 11th, 2017 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package mc.uhost.fr.api;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.util.Collection;
  5.  
  6. import org.apache.commons.lang.Validate;
  7. import org.bukkit.ChatColor;
  8. import org.bukkit.craftbukkit.v1_8_R2.entity.CraftPlayer;
  9. import org.bukkit.entity.Player;
  10.  
  11. import net.minecraft.server.v1_8_R2.IScoreboardCriteria;
  12. import net.minecraft.server.v1_8_R2.Packet;
  13. import net.minecraft.server.v1_8_R2.PacketPlayOutScoreboardDisplayObjective;
  14. import net.minecraft.server.v1_8_R2.PacketPlayOutScoreboardObjective;
  15. import net.minecraft.server.v1_8_R2.PacketPlayOutScoreboardScore;
  16. import net.minecraft.server.v1_8_R2.PacketPlayOutScoreboardTeam;
  17. import net.minecraft.server.v1_8_R2.PlayerConnection;
  18.  
  19.  
  20. /**
  21.  *
  22.  * @author zyuiop
  23.  * @author Seveningful
  24.  *
  25.  */
  26.  
  27. public class ScoreboardSign {
  28.     private boolean created = false;
  29.     private  String[] lines = new String[16];
  30.     private PacketPlayOutScoreboardTeam[] teams = new PacketPlayOutScoreboardTeam[16];
  31.     private final Player player;
  32.     private String objectiveName;
  33.  
  34.     public ScoreboardSign(Player player, String objectiveName) {
  35.         this.player = player;
  36.         this.objectiveName = objectiveName;
  37.     }
  38.  
  39.     public ScoreboardSign(Player player, String objectiveName, String[] lines) {
  40.         this.player = player;
  41.         this.objectiveName = objectiveName;
  42.         this.lines = lines;
  43.     }
  44.  
  45.     public void create() {
  46.         if (created) return;
  47.  
  48.  
  49.         PlayerConnection player = getPlayer();
  50.         player.sendPacket(createObjectivePacket(0, objectiveName));
  51.         player.sendPacket(setObjectiveSlot());
  52.         int i = 0;
  53.         while (i < lines.length)
  54.         {
  55.             sendLine(i++);
  56.         }
  57.         created = true;
  58.  
  59.  
  60.     }
  61.  
  62.     public void destroy() {
  63.         if (!created)
  64.             return;
  65.  
  66.         getPlayer().sendPacket(createObjectivePacket(1, null));
  67.  
  68.         created = false;
  69.     }
  70.  
  71.     private PlayerConnection getPlayer() {
  72.         return ((CraftPlayer) player).getHandle().playerConnection;
  73.     }
  74.  
  75.     @SuppressWarnings("rawtypes")
  76.     private Packet sendLine(int line) {
  77.         if (line > 15)
  78.             return null;
  79.         if (line < 0)
  80.             return null;
  81.         if (!created)
  82.             return null;
  83.         PacketPlayOutScoreboardTeam teamPacket = apply(line);
  84.         int score = (line * 1);
  85.         getPlayer().sendPacket(teamPacket);
  86.         return sendScore(ChatColor.values()[line].toString() + ChatColor.RESET , score);
  87.     }
  88.  
  89.     public void setObjectiveName(String name) {
  90.         this.objectiveName = name;
  91.         if (created)
  92.             getPlayer().sendPacket(createObjectivePacket(2, name));
  93.     }
  94.  
  95.     public void setLine(int line, String value) {
  96.         lines[line] = value;
  97.         getPlayer().sendPacket(sendLine(line));
  98.     }
  99.  
  100.     public void removeLine(int line) {
  101.         String oldLine = ChatColor.values()[line].toString() + ChatColor.RESET;
  102.         if (oldLine != null && created)
  103.             getPlayer().sendPacket(removeLine(oldLine));
  104.  
  105.         lines[line] = null;
  106.     }
  107.  
  108.     public String getLine(int line) {
  109.         if (line > 15)
  110.             return null;
  111.         if (line < 0)
  112.             return null;
  113.         return lines[line];
  114.     }
  115.  
  116.     /*
  117.     Factories
  118.      */
  119.     private PacketPlayOutScoreboardObjective createObjectivePacket(int mode, String displayName) {
  120.         PacketPlayOutScoreboardObjective packet = new PacketPlayOutScoreboardObjective();
  121.         try {
  122.             // Nom de l'objectif
  123.             Field name = packet.getClass().getDeclaredField("a");
  124.             name.setAccessible(true);
  125.             name.set(packet, player.getName());
  126.  
  127.             // Mode
  128.             // 0 : créer
  129.             // 1 : Supprimer
  130.             // 2 : Mettre à jour
  131.             Field modeField = packet.getClass().getDeclaredField("d");
  132.             modeField.setAccessible(true);
  133.             modeField.set(packet, mode);
  134.  
  135.             if (mode == 0 || mode == 2) {
  136.                 Field displayNameField = packet.getClass().getDeclaredField("b");
  137.                 displayNameField.setAccessible(true);
  138.                 displayNameField.set(packet, displayName);
  139.  
  140.                 Field display = packet.getClass().getDeclaredField("c");
  141.                 display.setAccessible(true);
  142.                 display.set(packet, IScoreboardCriteria.EnumScoreboardHealthDisplay.INTEGER);
  143.             }
  144.         } catch (NoSuchFieldException | IllegalAccessException e) {
  145.             e.printStackTrace();
  146.         }
  147.  
  148.         return packet;
  149.     }
  150.  
  151.     private PacketPlayOutScoreboardDisplayObjective setObjectiveSlot() {
  152.         PacketPlayOutScoreboardDisplayObjective packet = new PacketPlayOutScoreboardDisplayObjective();
  153.         try {
  154.             // Slot de l'objectif
  155.             Field position = packet.getClass().getDeclaredField("a");
  156.             position.setAccessible(true);
  157.             position.set(packet, 1); // SideBar
  158.  
  159.             Field name = packet.getClass().getDeclaredField("b");
  160.             name.setAccessible(true);
  161.             name.set(packet, player.getName());
  162.         } catch (NoSuchFieldException | IllegalAccessException e) {
  163.             e.printStackTrace();
  164.         }
  165.  
  166.         return packet;
  167.     }
  168.  
  169.     private PacketPlayOutScoreboardScore sendScore(String line, int score) {
  170.         PacketPlayOutScoreboardScore packet = new PacketPlayOutScoreboardScore(line);
  171.         try {
  172.             Field name = packet.getClass().getDeclaredField("b");
  173.             name.setAccessible(true);
  174.             name.set(packet, player.getName());
  175.  
  176.             Field scoreField = packet.getClass().getDeclaredField("c");
  177.             scoreField.setAccessible(true);
  178.             scoreField.set(packet, score); // SideBar
  179.  
  180.             Field action = packet.getClass().getDeclaredField("d");
  181.             action.setAccessible(true);
  182.             action.set(packet, PacketPlayOutScoreboardScore.EnumScoreboardAction.CHANGE);
  183.         } catch (NoSuchFieldException | IllegalAccessException e) {
  184.             e.printStackTrace();
  185.         }
  186.  
  187.         return packet;
  188.     }
  189.  
  190.     private PacketPlayOutScoreboardScore removeLine(String line) {
  191.         return new PacketPlayOutScoreboardScore(line);
  192.     }
  193.  
  194.     public Player getBukkitPlayer() {
  195.         return player;
  196.     }
  197.  
  198.     public String getDisplayName() {
  199.         return objectiveName;
  200.     }
  201.  
  202.     public String[] getLines() {
  203.         return lines;
  204.     }
  205.  
  206.  
  207.     private String[] splitString(String string)
  208.     {
  209.         Validate.isTrue(string.length() <= 32, "String can't have more than 32 characters " );
  210.         if (string.isEmpty()) {
  211.             return new String[] { " ", "" };
  212.         }
  213.         StringBuilder prefix = new StringBuilder(string.substring(0, string.length() >= 16 ? 16 : string.length()));
  214.         StringBuilder suffix = new StringBuilder(string.length() > 16 ? string.substring(16) : "");
  215.         if (prefix.charAt(prefix.length() - 1) == '§')
  216.         {
  217.             prefix.deleteCharAt(prefix.length() - 1);
  218.             suffix.insert(0, '§');
  219.         }
  220.         if(!suffix.toString().isEmpty())
  221.             suffix.insert(0, ChatColor.getLastColors(prefix.toString()));
  222.  
  223.         return new String[] { prefix.toString().length() > 16 ? prefix.toString().substring(0, 16) : prefix.toString(), suffix.toString().length() > 16 ? suffix.toString().substring(0, 16) : suffix.toString() };
  224.     }
  225.  
  226.  
  227.     private PacketPlayOutScoreboardTeam apply(int line){
  228.         if(teams[line] != null)
  229.         {
  230.             setField(getTeamLine(line),"h", 2);
  231.             setField(getTeamLine(line), "c", splitString(getLine(line))[0]);
  232.             setField(getTeamLine(line), "d", splitString(getLine(line))[1]);
  233.         }
  234.  
  235.  
  236.         return getTeamLine(line);
  237.     }
  238.  
  239.     private PacketPlayOutScoreboardTeam getTeamLine(int line) {
  240.         if(teams[line] == null)
  241.         {
  242.             PacketPlayOutScoreboardTeam team = new PacketPlayOutScoreboardTeam();
  243.             setField(team, "a", line + "");
  244.             setField(team, "b", line + "");
  245.             setField(team, "c", splitString(getLine(line))[0]);
  246.             setField(team, "d", splitString(getLine(line))[1]);
  247.             addEntry(team, ChatColor.values()[line].toString() + ChatColor.RESET);
  248.             teams[line] = team;
  249.         }
  250.         return teams[line];
  251.     }
  252.  
  253.  
  254.     private static void setField(PacketPlayOutScoreboardTeam packet, String field, Object value) {
  255.         try {
  256.             Field f = packet.getClass().getDeclaredField(field);
  257.             f.setAccessible(true);
  258.             f.set(packet, value);
  259.             f.setAccessible(false);
  260.         } catch (Exception ex) {ex.printStackTrace();}
  261.     }
  262.  
  263.     @SuppressWarnings({ "rawtypes", "unchecked" })
  264.     private void addEntry(PacketPlayOutScoreboardTeam packet, String entry) {
  265.         Field f = null;
  266.         try {
  267.             f = packet.getClass().getDeclaredField("g");
  268.             f.setAccessible(true);
  269.             ((Collection) f.get(packet)).add(entry);
  270.         } catch (NoSuchFieldException e) {
  271.             e.printStackTrace();
  272.         } catch (IllegalAccessException e) {
  273.             e.printStackTrace();
  274.         }
  275.  
  276.     }
  277.  
  278.     /**
  279.      * Reset all & leave bar & scrolling text
  280.      */
  281.     public void reset() {
  282.         for(int i = 1; i <14; i++)
  283.             removeLine(i);
  284.     }
  285. }
RAW Paste Data
Top