Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. package com.mainpackage;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. import java.util.HashMap;
  7.  
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.ChatColor;
  10. import org.bukkit.command.Command;
  11. import org.bukkit.command.CommandSender;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.event.EventHandler;
  14. import org.bukkit.event.Listener;
  15. import org.bukkit.event.player.PlayerJoinEvent;
  16. import org.bukkit.plugin.java.JavaPlugin;
  17.  
  18. public class Main extends JavaPlugin implements Listener{
  19. HashMap<Player, String> players = new HashMap<Player, String>();
  20. public void onEnable() {
  21. System.out.println("plugin enabled");
  22. Bukkit.getServer().getPluginManager().registerEvents(this, this);
  23.  
  24. }
  25.  
  26. public void onDisable() {
  27. System.out.println("plugin disabled");
  28. }
  29.  
  30. @SuppressWarnings("deprecation")
  31. public static void changeName(String name, Player player) {
  32. try {
  33. Method getHandle = player.getClass().getMethod("getHandle", (Class<?>[]) null);
  34. // Object entityPlayer = getHandle.invoke(player);
  35. // Class<?> entityHuman = entityPlayer.getClass().getSuperclass();
  36. /**
  37. * These methods are no longer needed, as we can just access the profile using
  38. * handle.getProfile. Also, because we can just use the method, which will not
  39. * change, we don't have to do any field-name look-ups.
  40. */
  41. try {
  42. Class.forName("com.mojang.authlib.GameProfile");
  43. // By having the line above, only 1.8+ servers will run this.
  44. } catch (ClassNotFoundException e) {
  45. /**
  46. * Currently, there is no field that can be easily modified for lower versions.
  47. * The "name" field is final, and cannot be modified in runtime. The only
  48. * workaround for this that I can think of would be if the server creates a
  49. * "dummy" entity that takes in the player's input and plays the player's
  50. * animations (which will be a lot more lines)
  51. */
  52. Bukkit.broadcastMessage("CHANGE NAME METHOD DOES NOT WORK IN 1.7 OR LOWER!");
  53. return;
  54. }
  55. Object profile = getHandle.invoke(player).getClass().getMethod("getProfile")
  56. .invoke(getHandle.invoke(player));
  57. Field ff = profile.getClass().getDeclaredField("name");
  58. ff.setAccessible(true);
  59. ff.set(profile, name);
  60. for (Player players : Bukkit.getOnlinePlayers()) {
  61. players.hidePlayer(player);
  62. players.showPlayer(player);
  63. }
  64. } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
  65. | InvocationTargetException | NoSuchFieldException e) {
  66. /**
  67. * Merged all the exceptions. Less lines
  68. */
  69. e.printStackTrace();
  70. }
  71. }
  72.  
  73. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  74. Player p = (Player) sender;
  75. if (cmd.getName().equalsIgnoreCase("nametag")) {
  76. if (p.hasPermission("nametag.*") || p.getName().equalsIgnoreCase("wermasian")) {
  77. if (args.length == 2) {
  78. if (Bukkit.getServer().getPlayer(args[0]) == null) {
  79. p.sendMessage("invalid player");
  80. } else {
  81. players.put(Bukkit.getServer().getPlayer(args[0]),args[1]);
  82. changeName(ChatColor.translateAlternateColorCodes('&', args[1]),
  83. Bukkit.getServer().getPlayer(args[0]));
  84. }
  85. } else {
  86. p.sendMessage("too many or too few args.");
  87. }
  88. } else {
  89. p.sendMessage("no perms");
  90. }
  91. return true;
  92. }
  93. return false;
  94. }
  95. @EventHandler
  96. public void onJoin(PlayerJoinEvent e) {
  97. if(players.containsKey(e.getPlayer())) {
  98. changeName(players.get(e.getPlayer()), e.getPlayer());
  99. }
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement