Advertisement
orblazer

[Citizens] sendTeam (fix collision and nameplate)

Sep 6th, 2019
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. /**
  2.  * Send team of npc
  3.  *
  4.  * @param action the action (create, update, delete)
  5.  * @param player the player want receive the team (pass null for broadcast)
  6.  */
  7. private void sendTeam(@Nonnull String action, Player player) {
  8.     try {
  9.         final String id = uuid.toString().replaceAll("-", "").substring(0, 16);
  10.  
  11.         // Get scoreboard team instance
  12.         final Class<?> scoreboardTeamClass = ReflectionUtils.PacketType.PLAY_OUT_SCOREBOARD_TEAM.getPacket();
  13.         final Object scoreboardTeam = Reflection.getConstructor(scoreboardTeamClass).invoke();
  14.         final Class<?> iChatBaseComponentClass = Reflection.getMinecraftClass("IChatBaseComponent");
  15.         final Class<?> chatComponentClass = Reflection.getMinecraftClass("ChatComponentText");
  16.         final Reflection.ConstructorInvoker chatComponentConstructor = Reflection.getConstructor(chatComponentClass, String.class);
  17.  
  18.         Reflection.getField(scoreboardTeamClass, "a", String.class).set(scoreboardTeam, id);
  19.         Reflection.getField(scoreboardTeamClass, "b", iChatBaseComponentClass)
  20.                 .set(scoreboardTeam, chatComponentConstructor.invoke(id));
  21.  
  22.         // Set prefix and suffix
  23.         if (!Strings.isNullOrEmpty(prefix)) {
  24.             Reflection.getField(scoreboardTeamClass, "c", iChatBaseComponentClass)
  25.                     .set(scoreboardTeam, chatComponentConstructor.invoke(prefix));
  26.         }
  27.         if (!Strings.isNullOrEmpty(suffix)) {
  28.             Reflection.getField(scoreboardTeamClass, "d", iChatBaseComponentClass)
  29.                     .set(scoreboardTeam, chatComponentConstructor.invoke(suffix));
  30.         }
  31.  
  32.         // Disable name tag and collision
  33.         Reflection.getField(scoreboardTeamClass, "e", String.class).set(scoreboardTeam, isNameVisible ? "always" : "never");
  34.         Reflection.getField(scoreboardTeamClass, "f", String.class).set(scoreboardTeam, "never");
  35.  
  36.         // Set action of packet
  37.         switch (action.toLowerCase()) {
  38.             case "create":
  39.                 Reflection.getField(scoreboardTeamClass, "i", int.class).set(scoreboardTeam, 0);
  40.  
  41.                 // Add npc to team
  42.                 Reflection.getField(scoreboardTeamClass, "h", Collection.class)
  43.                         .set(scoreboardTeam, Collections.singletonList(name));
  44.                 break;
  45.             case "delete":
  46.                 Reflection.getField(scoreboardTeamClass, "i", int.class).set(scoreboardTeam, 1);
  47.                 break;
  48.             case "update":
  49.                 Reflection.getField(scoreboardTeamClass, "i", int.class).set(scoreboardTeam, 2);
  50.                 break;
  51.         }
  52.  
  53.         // Send packet
  54.         if (player != null) {
  55.             ReflectionUtils.sendPacket(player, scoreboardTeam);
  56.         } else {
  57.             broadcastPacket(scoreboardTeam);
  58.         }
  59.     } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException e) {
  60.         e.printStackTrace();
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement