Easily change the name of a player! (event the name above the head :P)

Discussion in 'Resources' started by p000ison, Jun 24, 2012.

?

Rate my code from 1-10; 1 is bad :P

  1. 1

    5.0%
  2. 2

    0 vote(s)
    0.0%
  3. 3

    0 vote(s)
    0.0%
  4. 4

    5.0%
  5. 5

    20.0%
  6. 6

    0 vote(s)
    0.0%
  7. 7

    0 vote(s)
    0.0%
  8. 8

    25.0%
  9. 9

    20.0%
  10. 10

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

    p000ison

    Heyho Community,

    I made this, because I think a simple and good lib/class would be cool.
    So @FTWin01Gurl wanted to make a library, so you can take this and make one :p
    It was fun to create it and only took 5 mins so yeah do with it what you want :p
    Never tested it, so simply contact me if something isnt working.

    Main Thread:
    http://forums.bukkit.org/threads/tutorial-teams-name-color-and-wool-hats-asigned-to-player.80835/

    Code:
    /*
     * Copyright (C) 2012 p000ison
     * 
     * This work is licensed under the Creative Commons
     * Attribution-NonCommercial-NoDerivs 3.0 Unported License. To view a copy of
     * this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ or send
     * a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco,
     * California, 94105, USA.
     * 
     */
     
    import java.util.HashMap;
    import java.util.Map;
    import net.minecraft.server.EntityPlayer;
    import net.minecraft.server.Packet20NamedEntitySpawn;
    import org.bukkit.Bukkit;
    import org.bukkit.Server;
    import org.bukkit.craftbukkit.entity.CraftPlayer;
    import org.bukkit.entity.Player;
     
    /**
     *
     * @author Max
     */
    public class NameChanger
    {
     
        private Map<String, Title> titles = new HashMap<String, Title>();
        private Server server;
     
        /**
         * Title Changer
         *
         * @param server
         */
        public NameChanger(Server server)
        {
            this.server = server;
        }
     
        /**
         * Changes/Adds a Title of a player. Put "null" where you dont want to
         * change it.
         *
         * @param player
         * @param displayName
         * @param listName
         * @param title
         */
        public void addTitle(Player player, String displayName, String listName, String title, boolean autoFixToLong)
        {
            if (autoFixToLong) {
                if (displayName != null && displayName.length() > 16) {
                    displayName = displayName.substring(0, 16);
                }
                if (listName != null && listName.length() > 16) {
                    listName = listName.substring(0, 16);
                }
                if (title != null && title.length() > 16) {
                    title = title.substring(0, 16);
                }
            }
     
            titles.put(player.getName(), new Title(title, listName, displayName));
            init(player);
        }
     
        /**
         * Gets called only once
         *
         * @param player
         */
        private void init(Player player)
        {
            Title title = titles.get(player.getName());
            String displayName = title.getListName();
            String listName = title.getListName();
            String titleS = title.getTitle();
     
            if (displayName != null) {
                player.setDisplayName(displayName);
            }
     
            if (listName != null) {
                player.setPlayerListName(listName);
            }
     
            if (titleS != null) {
                String oldName = player.getName();
                EntityPlayer changingName = ((CraftPlayer) player).getHandle();
     
                changingName.name = titleS;
     
                for (Player players : Bukkit.getOnlinePlayers()) {
                    if (!players.equals(player)) {
                        sendNameChangePacket(players, changingName);
                    }
                }
                changingName.name = oldName;
            }
        }
     
        /**
         * This must be called when a player joins. The player is that player that
         * joins.
         *
         * @param player
         */
        public void updateJoin(Player player)
        {
     
            for (Map.Entry<String, Title> entry : titles.entrySet()) {
                String p = entry.getKey();
                Player rp = server.getPlayerExact(p);
     
                if (rp == null) {
                    continue;
                }
     
                String playerTitle = entry.getValue().getTitle();
     
                if (playerTitle == null) {
                    continue;
                }
     
                String oldName = p;
                EntityPlayer changingName = ((CraftPlayer) rp).getHandle();
                changingName.name = playerTitle;
     
                if (!player.equals(rp)) {
                    sendNameChangePacket(player, changingName);
                }
     
                changingName.name = oldName;
            }
        }
     
        /**
         * Sends a fake packet to change the title above the players head
         *
         * @param player
         * @param playerToSend
         */
        private static void sendNameChangePacket(Player player, EntityPlayer playerToSend)
        {
            ((CraftPlayer) player).getHandle().netServerHandler.sendPacket(new Packet20NamedEntitySpawn(playerToSend));
        }
     
        /**
         * Represents a Title
         *
         */
        private class Title
        {
     
            private String displayName;
            private String listName;
            private String title;
     
            public Title(String title, String listName, String displayName)
            {
                this.title = title;
                this.listName = listName;
                this.displayName = displayName;
            }
     
            public String getDisplayName()
            {
                return displayName;
            }
     
            public String getListName()
            {
                return listName;
            }
     
            public String getTitle()
            {
                return title;
            }
        }
    }
    
    p000ison
     
    Taco, JxAxVxAx and josephgold5 like this.
  2. I am confused, do I start with this then do the tutorial...
    OR
    Do I copy this then add to the code so I is how I want it?

    Also there do I put in what is in the example?
     
  3. Offline

    JxAxVxAx

    Pls Change mad into made !
    And nice tutorial :p
     
  4. Offline

    p000ison

    Its a whole class. So copy it into your project and do for example: DisplayNameChanger.changeDisplayName(player, name);

    k, done :p

    If you like the class please give me an internet :p
     
  5. Where do I put the DisplayNameChanger...?
    Do you mean I put that anywhere I want there?

    Also where do I put for example:
    Code:
    public void onEnable(){
                this.getCommand("blue").setExecutor(new TeamExecutor(this));
                this.getCommand("red").setExecutor(new TeamExecutor(this));
                this.getCommand("leave").setExecutor(new TeamExecutor(this));
    }
    
    Or is this totally seperate?

    Sorry for begin difficult :)
     
  6. Offline

    JxAxVxAx

    Another mistake :p

    Changes the titel of a player above his head

    Tiel , Change To Title

    And I wanna ask you if you can add This Method To The Code

    changing the players 's name back after he has executed a command .
     
  7. Offline

    p000ison

    hm can not find "Tiel " lol

    So you mean a reset method?
    If yes give me 2 mins xD

    fixed a typo and added reset methods

    You can put it where you want to change the name :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  8. ??
     
  9. I think he meant that you spelled "title" wrong :p
     
  10. Offline

    JxAxVxAx

    I said it wrong previously :p
    It Should Be This.

    titel => Change To Title


    EDIT : Thanks For Reset Methods :p
    READ THIS !!!
    EDIT EDIT : I notice All Your Title Has Been Spelled to titel :p
     
  11. Offline

    p000ison

    Ye i will change it tomorrow xD In Germany its spelled like this so yeah xDD Almost the same :p
     
  12. Offline

    jamietech

    You need to substring the username if it's 15 or 16 characters long as 16 is the maximum amount of characters for a players 'title' (name above head) and tab list name which means when you add the color to their name and it's that long you get super fun exceptions in console.
     
  13. Offline

    Delocaz

    You do know that replacing & with the unicode thing is bad? If i were to nick myself "Delocaz&", it will crash all clients, according to the wiki.
     
  14. Offline

    md_5

    Still wrong :p
    But nice tutorial, you can also do a similar (more hacky thing) and change the actual player, so they get a different inventory etc etc.
     
  15. Offline

    p000ison

    Ah ye, Ill update it soon :p Have a better idea than this :p

    Found some minutes to write a new and better one. Simply call updateJoin when a player joins.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  16. Offline

    jamietech

    p000ison
     
  17. Offline

    p000ison

    Added as optional
     
  18. Offline

    Gawdzahh

    p000ison EntityPlayer? I guess I am meant to be adding something that I haven't?
     
  19. Offline

    p000ison

    You need craftbukkit as a library so you include the net.minecraft stuff
     
  20. Offline

    Gawdzahh

    I added the craftbukkit jar, Still error and I cant import EntityPlayer and CraftPlayer doesn't exist.
     
  21. Offline

    p000ison

    remove the 2 imports from above and import them again
     
  22. Offline

    Gawdzahh

    Now I only get error on
    ((CraftPlayer)playersonline).getHandle().netServerHandler.sendPacket(new Packet20NamedEntitySpawn(changingName));
     
  23. Offline

    Comphenix

    It's playerConnection now (after 1.4.6).

    In any case, why not use TagAPI instead?
     
  24. Offline

    Gawdzahh

    I heard that your players have to download a file or something like that, And I would much rather do it myself and learn. :)

    Oh and Thanks for that I got it working! Only in events for some reason :p
     
  25. Offline

    Comphenix

    They will have to download and install TagAPI, sure, but a lot of servers are already running it. It's not that big of a hurdle.

    But it's not just a matter of convenience. TagAPI ensures that your plugin is compatible with every other plugin that also modifies the displayed name of a player (list here). p000ison's library is not compatible in these cases.
     
  26. Offline

    p000ison

    I would recommend the TagAPI, too. I think the time I wrote this there was no tagapi :p
     
  27. Offline

    Gawdzahh

    I see what you guys are saying but my server will only really include a max of 5 plugins, my custom coded one, bPermissions, Votifier, BuyCraft and maybe worldedit/voxelsniper when needed.
     
Thread Status:
Not open for further replies.

Share This Page