Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @SuppressWarnings("unchecked")
- public static void changeName(String name, Player player) {
- try {
- Method getHandle = player.getClass().getMethod("getHandle");
- Object entityPlayer = getHandle.invoke(player);
- boolean gameProfileExists = false;
- // Some 1.7 versions had the GameProfile class in a different package
- try {
- Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile");
- gameProfileExists = true;
- } catch (ClassNotFoundException ignored) {
- }
- try {
- Class.forName("com.mojang.authlib.GameProfile");
- gameProfileExists = true;
- } catch (ClassNotFoundException ignored) {
- }
- if (!gameProfileExists) {
- Field nameField = entityPlayer.getClass().getSuperclass().getDeclaredField("name");
- nameField.setAccessible(true);
- nameField.set(entityPlayer, name);
- } else {
- // Only 1.7+ servers will run this code
- Object profile = entityPlayer.getClass().getMethod("getProfile").invoke(entityPlayer);
- Field ff = profile.getClass().getDeclaredField("name");
- ff.setAccessible(true);
- ff.set(profile, name);
- }
- // In older versions, Bukkit.getOnlinePlayers() returned an Array instead of a Collection.
- if (Bukkit.class.getMethod("getOnlinePlayers", new Class<?>[0]).getReturnType() == Collection.class) {
- Collection<? extends Player> players = (Collection<? extends Player>) Bukkit.class.getMethod("getOnlinePlayers").invoke(null);
- for (Player p : players) {
- p.hidePlayer(player);
- p.showPlayer(player);
- }
- } else {
- Player[] players = ((Player[]) Bukkit.class.getMethod("getOnlinePlayers").invoke(null));
- for (Player p : players) {
- p.hidePlayer(player);
- p.showPlayer(player);
- }
- }
- } catch (Exception e) {
- /*
- * Merged all the exceptions. Less lines
- */
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement