Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. @SuppressWarnings("unchecked")
  2. public static void changeName(String name, Player player) {
  3. try {
  4. Method getHandle = player.getClass().getMethod("getHandle");
  5. Object entityPlayer = getHandle.invoke(player);
  6. boolean gameProfileExists = false;
  7. // Some 1.7 versions had the GameProfile class in a different package
  8. try {
  9. Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile");
  10. gameProfileExists = true;
  11. } catch (ClassNotFoundException ignored) {
  12.  
  13. }
  14. try {
  15. Class.forName("com.mojang.authlib.GameProfile");
  16. gameProfileExists = true;
  17. } catch (ClassNotFoundException ignored) {
  18.  
  19. }
  20. if (!gameProfileExists) {
  21. Field nameField = entityPlayer.getClass().getSuperclass().getDeclaredField("name");
  22. nameField.setAccessible(true);
  23. nameField.set(entityPlayer, name);
  24. } else {
  25. // Only 1.7+ servers will run this code
  26. Object profile = entityPlayer.getClass().getMethod("getProfile").invoke(entityPlayer);
  27. Field ff = profile.getClass().getDeclaredField("name");
  28. ff.setAccessible(true);
  29. ff.set(profile, name);
  30. }
  31. // In older versions, Bukkit.getOnlinePlayers() returned an Array instead of a Collection.
  32. if (Bukkit.class.getMethod("getOnlinePlayers", new Class<?>[0]).getReturnType() == Collection.class) {
  33. Collection<? extends Player> players = (Collection<? extends Player>) Bukkit.class.getMethod("getOnlinePlayers").invoke(null);
  34. for (Player p : players) {
  35. p.hidePlayer(player);
  36. p.showPlayer(player);
  37. }
  38. } else {
  39. Player[] players = ((Player[]) Bukkit.class.getMethod("getOnlinePlayers").invoke(null));
  40. for (Player p : players) {
  41. p.hidePlayer(player);
  42. p.showPlayer(player);
  43. }
  44. }
  45. } catch (Exception e) {
  46. /*
  47. * Merged all the exceptions. Less lines
  48. */
  49. e.printStackTrace();
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement