Changing player-name?

Discussion in 'Plugin Development' started by TheDDestroyer12, Jul 31, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    TheDDestroyer12

    Hello!
    I'm working on a little plugin (just for practice), that will make you able to give you a title to your name-tag!
    For example; "Steve [Survivor]"

    I (just) can't figure out how to change the name :)
    Here is my code;
    Code:java
    1. package com.tdd12.bukkit.titles;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.PluginDescriptionFile;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Titles extends JavaPlugin {
    11.  
    12. // plugin.yml
    13. PluginDescriptionFile description = this.getDescription();
    14.  
    15. @Override
    16. public void onEnable() {
    17. this.getLogger().info("[" + description.getName() + ", v" + description.getVersion() + "] has been ENABLED!");
    18. }
    19.  
    20. @Override
    21. public void onDisable() {
    22. this.getLogger().info("[" + description.getName() + ", v" + description.getVersion() + "] has been DISABLED!");
    23. }
    24.  
    25. // The "starter" to all messages!
    26. public String starter() {
    27. String msg = ChatColor.GRAY + "[" + ChatColor.YELLOW + "Titles" + ChatColor.GRAY + "] " + ChatColor.WHITE;
    28. return msg;
    29. }
    30.  
    31. public void setPlayerTitle(Player player, String title) {
    32. //TODO CHANGE PLAYER NAME
    33. }
    34.  
    35. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    36. Player player = (Player) sender;
    37. if (commandLabel.equalsIgnoreCase("title")) {
    38. if (args.length == 1) {
    39. setPlayerTitle(player, args[0]);
    40. }
    41. }
    42. return true;
    43. }
    44. }

    Is there any easy way of doing it for Bukkit 1.7.9?
    I've followed some tutorials on youtube and read some threads in the bukkit forum, but they seems to be outdated!

    In all the tutorials and threads, this line is shown:
    Code:java
    1. ((CraftPlayer)playersinworld).getHandle().netServerHandler.sendPacket(new Packet20NamedEntitySpawn(changingName));

    And that's the problem! I get a red underline under the word "netServerHandler".
    Is it outdated, or have I missed something?

    Thanks in advance!
     
  2. Offline

    TeeePeee

    The netServerHandler is (if I remember correctly) 1.6.4 code.

    The easiest thing to do is just hook into TagAPI and change it that way. Otherwise, you'll have to change Packet20NamedEntitySpawn to PacketPlayOutNamedEntitySpawn and construct a new GameProfile with the right name. It sounds more complicated because it is.

    Also note you have to send the packet to every player on the server, and send it to new players that log-in after the packet has been constructed initially and sent.

    Moral of the story, use TagAPI. It's handy.
     
  3. Offline

    TheDDestroyer12

    TeeePeee
    Okay, thanks for the advice!

    The problem is that I want to do everything myself because I want to learn! ;)

    Should I begin with learning about "packets"? (I don't even know what that is :confused:)
    Is there any tutorials that aren't outdated?
    How do I create a new "game profile"?

    Thanks again! ;)
     
  4. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Do it yourself, and you potentially break all plugins using TagAPI. TagAPI's entire purpose is to avoid conflict between plugins on setting a name tag.

    That said, if you're adding a title to a user a better way would be using the Scoreboard API (built into Bukkit).

    Either way, you should not be playing with packets.
     
    TeeePeee likes this.
  5. Offline

    FabeGabeMC

    mbaxter
    TagAPI uses the scoreboard api doesn't it?
     
  6. Offline

    travja

    First off, as others have said, your code is a little outdated. You can find all the updated Packet names here: https://docs.google.com/spreadsheet...aU1RZUswZ2dqUFRpTTkyUEk1dXc&usp=sharing#gid=0
    And also, it's getHandle().playerConnection instead of netServerHandler. And lastly, even with packets I believe the name limit is still 16 characters. You can add a prefix using scoreboards and that is probably the best way.
     
    TheDDestroyer12 likes this.
  7. Offline

    TheDDestroyer12

    Your "TagAPI" is awesome ;) , but I really want to do it myself, without any libraries, except the bukkit ones!
    How do I do it without packets then? :confused:
    All tutorials I can find is on how to do it with packets.
    Do you know any that is without it?

    Thanks!
     
  8. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    No sir. Totally separate concepts.

    Use the Scoreboard API to append a title to your name (like [Survivor]). Anybody telling you to use packets for this, or TagAPI, is completely off the mark. Create a Scoreboard Team with a Suffix of "[Survivor]" and put players on that team. BAM! Instant title to their nickname over their head! :)
     
    TheDDestroyer12 likes this.
  9. Offline

    TheDDestroyer12


    Thanks! :) I'll look after some tutorials on that! ;)

    EDIT: The tutorials/forum threads I watched and read, was mainly about changing color, but I'm pretty sure you could have changed the name with that too. I might be completely wrong :)
    One of them was this thread: https://forums.bukkit.org/threads/changing-the-a-players-name-tag-similar-to-tagapi.106825/
     
  10. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    TheDDestroyer12 I suggest just reading the JavaDocs for the Scoreboard stuff. There isn't much there, and a lot of tutorials I've seen around here don't adequately explain how scoreboards work.
     
    TheDDestroyer12 likes this.
  11. Offline

    TheDDestroyer12


    Okay, I will do so! Might upload it later on and link to it from here :) Thanks ;)
     
    mbaxter likes this.
  12. Offline

    fireblast709

    http://xkcd.com/927/
     
  13. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    There were no standards. TagAPI created a single place for multiple plugins to function together via events, as Bukkit will not add such an event to its API. ;)
     
  14. Offline

    fireblast709

    mbaxter your post just reminded me of that xkcd :p
     
  15. Offline

    TheDDestroyer12

    Hi again! Now I've extended my code a little bit, but I can't get it working...
    Here is the new code:
    Code:java
    1. package com.tdd12.bukkit.ttitles;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.PluginDescriptionFile;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10. import org.bukkit.scoreboard.Scoreboard;
    11. import org.bukkit.scoreboard.ScoreboardManager;
    12. import org.bukkit.scoreboard.Team;
    13.  
    14. public class TTitles extends JavaPlugin {
    15.  
    16. // plugin.yml
    17. PluginDescriptionFile description = this.getDescription();
    18.  
    19. // Scoreboard stuff
    20. static ScoreboardManager sm;
    21. static Scoreboard scoreboard;
    22.  
    23. // On plugin enable!
    24. @Override
    25. public void onEnable() {
    26. sm = Bukkit.getServer().getScoreboardManager();
    27. scoreboard = sm.getNewScoreboard();
    28. this.getLogger().info("[" + description.getName() + ", v" + description.getVersion() + "] has been ENABLED!");
    29. }
    30.  
    31. // On plugin disable!
    32. @Override
    33. public void onDisable() {
    34. this.getLogger().info("[" + description.getName() + ", v" + description.getVersion() + "] has been DISABLED!");
    35. }
    36.  
    37. // The "starter" to all messages!
    38. public String starter() {
    39. String msg = ChatColor.GRAY + "[" + ChatColor.YELLOW + "Titles" + ChatColor.GRAY + "] " + ChatColor.WHITE;
    40. return msg;
    41. }
    42.  
    43. // Create a new "title group" (*team)
    44. public static void newTitleGroup(String teamName, Player player) {
    45. player.sendMessage("New Team 1");
    46. Team team = scoreboard.registerNewTeam(teamName);
    47. player.sendMessage("New Team 2");
    48. team.addPlayer(player);
    49. player.sendMessage("New Team 3");
    50. team.setSuffix(ChatColor.GREEN + " [" + teamName + "]" + ChatColor.WHITE);
    51. player.sendMessage("New Team 4");
    52. }
    53.  
    54. // When the player enters a command:
    55. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    56. Player player = (Player) sender;
    57. player.sendMessage("Step 1");
    58. if (commandLabel.equalsIgnoreCase("title")) {
    59. player.sendMessage("Step 2");
    60. if (args.length == 1) {
    61. player.sendMessage("Step 3"); //<------------- Works to here!
    62. TTitles.newTitleGroup(args[1], player);
    63. player.sendMessage("Step 4");
    64. player.sendMessage(starter() + ChatColor.AQUA + "You now have the title" + scoreboard.getPlayerTeam(player).getSuffix() + ChatColor.AQUA + "!");
    65. player.sendMessage("Step 5");
    66. }
    67. }
    68. return true;
    69. }
    70. }


    Server log:
    Code:
    C:\Users\TheDDestroyer12\Desktop\Bukkit plugins\Server>java -Xmx1024M -jar craft
    bukkit.jar -o true
    Loading libraries, please wait...
    [10:38:05 INFO]: Starting minecraft server version 1.7.9
    [10:38:05 INFO]: Loading properties
    [10:38:05 INFO]: Default game type: SURVIVAL
    [10:38:05 INFO]: Generating keypair
    [10:38:05 INFO]: Starting Minecraft server on *:12345
    [10:38:05 INFO]: This server is running CraftBukkit version git-Bukkit-1.7.9-R0.
    1-10-g8688bd4-b3092jnks (MC: 1.7.9) (Implementing API version 1.7.9-R0.2)
    [10:38:05 INFO]: [TTitles] Loading TTitles v1.0
    [10:38:05 INFO]: Preparing level "world"
    [10:38:05 INFO]: Preparing start region for level 0 (Seed: -2098223078032070244)
     
    [10:38:06 INFO]: ----- Bukkit Auto Updater -----
    [10:38:06 INFO]: It appears that you're running a Beta Build, when you've specif
    ied in bukkit.yml that you prefer to run Recommended Builds.
    [10:38:06 INFO]: If you would like to be kept informed about new Beta Build rele
    ases, it is recommended that you change 'preferred-channel' in your bukkit.yml t
    o 'beta'.
    [10:38:06 INFO]: With that set, you will be told whenever a new version is avail
    able for download, so that you can always keep up to date and secure with the la
    test fixes.
    [10:38:06 INFO]: If you would like to disable this warning, simply set 'suggest-
    channels' to false in bukkit.yml.
    [10:38:06 INFO]: ----- ------------------- -----
    [10:38:06 INFO]: Preparing start region for level 1 (Seed: -2098223078032070244)
     
    [10:38:07 INFO]: Preparing start region for level 2 (Seed: -2098223078032070244)
     
    [10:38:07 INFO]: [TTitles] Enabling TTitles v1.0
    [10:38:07 INFO]: [TTitles] [TTitles, v1.0] has been ENABLED!
    [10:38:07 INFO]: Server permissions file permissions.yml is empty, ignoring it
    [10:38:07 INFO]: Done (1,921s)! For help, type "help" or "?"
    [10:38:36 INFO]: UUID of player DDestroyer12 is dd07c042-1198-4af1-b8a2-6c7433b8
    6b8d
    [10:38:36 INFO]: DDestroyer12[/127.0.0.1:53788] logged in with entity id 135 at
    ([world] 50.060495364011196, 63.0, 200.76450825912684)
    [10:38:43 INFO]: DDestroyer12 issued server command: /title builder
    [10:38:43 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'titl
    e' in plugin TTitles v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServe
    r.java:701) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:956) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :817) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at com.tdd12.bukkit.ttitles.TTitles.onCommand(TTitles.java:62) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            ... 13 more
    >
    I have a little knowledge on how to read error logs, but this one, I don't understand :)

    Can you see the problem(s) in this code? I might have done something wrong with the Scoreboard & ScoreboardManager constructors :) As I said, I get the message that says "Step 3!", but then it says "An internal error occoured..."
    The messages are for debugging only :)
     
  16. Offline

    Necrodoom

    TheDDestroyer12 you check if there is one arg, but then try to get the second arg, which returns array out of bounds exception.
    Arrays start from index 0, as in, args[0].
     
  17. Offline

    TheDDestroyer12


    I tried that, but it didn't work... Maybe I did something wrong. I'll try again :)
    EDIT: When I do that, I only get to step 2... Can't figure out why :confused:

    I whish you could debug bukkit plugins in Eclipse... :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  18. Offline

    Necrodoom

    TheDDestroyer12 debug with printing then. Print every variable you check and use.
    Also, paste edited code.
     
Thread Status:
Not open for further replies.

Share This Page