TagAPI - Change/color the name over people's heads!

Discussion in 'Resources' started by mbaxter, Sep 6, 2012.

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

    KeybordPiano459

    Ohh, like this?
    Code:java
    1. Player player = event.getNamedPlayer();
    2. event.setTag(ChatColor.RED + player.getDisplayName());
     
  2. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    There ya go.
     
    chasechocolate, madmac and hawkfalcon like this.
  3. Offline

    KeybordPiano459

  4. Offline

    Comphenix

    This is a very nice standardization for plugins to modify the in-game display name, but unfortunately it's pretty much guaranteed to conflict with any other packet modification systems.

    While working on ProtocolLib, I tried really hard to work around your "hook" method, but it's pretty difficult. TagAPI applies its hack with a MONITOR player "logged in" listener, so I can't just increase the priority. I might be able to use a sync task to apply the hack a tick after the player has logged in, but then I would potentially miss packets sent in the meantime.

    I also tried to override the "queue" method in net.minecraft.server.NetworkManager with java.reflect.Proxy, but then TagAPI would complain about a CastClassException. I can't subclass it, as that would break compatibility in the future, and while I can dynamically extend classes with Cglib, it doesn't support classes without a default constructor. I would have to include jMock or similar, which adds too much overhead. The library is big enough as is.

    I think this really illustrates the need for something like ProtocolLib. While it doesn't have to be my library, there should be some way for plugins to avoid stepping on each others toes. TagAPI is indeed a nice step in that direction, but I don't think it goes far enough.

    I did make my own version of TagAPI as an example of how to use ProtocolLib, but of course, it's not compatible with plugins that are currently using TagAPI. It's trivial to make it compatible, however. I just have to move the classes into your namespace. But I'd rather not do that.
     
  5. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Comphenix next release will work fine with ProtocolLib.
     
    Comphenix and iKeirNez like this.
  6. Offline

    hawkfalcon

    :x It would be nice if you didn't have to have them download this separately :x
     
  7. Offline

    Chipmunk9998

    Very nice, but I was just wondering if it was possible to get rid of that space at the end of a name?
     
  8. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    You can have your plugin grab the files page on bukkitdev and then download latest.
    That's a client issue.
     
    hawkfalcon likes this.
  9. Offline

    hawkfalcon

    Yabutstill.
     
  10. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    There is no other way to resolve this that doesn't involve breaking plugins.
     
  11. Offline

    hawkfalcon

    I guess:3
     
  12. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Version 1.7 released, comes with ProtocolLib support (you don't need it to run this, but if you are running ProtocolLib things will work fine now) and handling for plugins that try calling its non-threadsafe methods out of the main thread.

    http://dev.bukkit.org/server-mods/tag/files/11-v1-7/
     
  13. Offline

    MattexFilms

    I'm having some trouble, I am trying to set the colour of a player's tag depending on their health. Here is what I have so far:

    PHP:
    @EventHandler
        
    public void onNameTagGreen(PlayerReceiveNameTagEvent event) {
            
    Player player = (Playerevent;
            if (
    event.getPlayer().getHealth() < 20 || event.getPlayer().getHealth() > 16) {
                
    event.setTag(ChatColor.GREEN player.getName());
            }
        }
    When ever a SECOND player joins the server I get an lovely console error saying:

    [SEVERE] Could not pass event PlayerReceiveNameTagEvent
    org.bukkit.event.EventException

    Any ideas?
     
  14. Offline

    Sabersamus

    Well for one, You are saying
    Code:java
    1. Player player = (Player) event;


    Im pretty sure you cant cast player on an event :p
     
  15. Offline

    MattexFilms

    Would it be more along the lines of:

    Code:
    Player player = event.getPlayer();
    And how do you get java code on forum posts, all I can get is general code, php and html.

    Okay I got it working, but I am having trouble changing the colour of a players tag depending on their health. This is the code I have:

    PHP:
    @EventHandler
        
    public void onNameTagGreen(PlayerReceiveNameTagEvent event) {
            
    Player player event.getPlayer();
            if (
    event.getPlayer().getHealth() < 20 || event.getPlayer().getHealth() > 16) {
                
    event.setTag(ChatColor.GREEN player.getName());
            }else if(
    event.getPlayer().getHealth() < 15 && event.getPlayer().getHealth() > 11){
                
    event.setTag(ChatColor.YELLOW player.getName());
            }else if(
    event.getPlayer().getHealth() < 10 && event.getPlayer().getHealth() > 6){
                
    event.setTag(ChatColor.GOLD player.getName());
            }else if(
    event.getPlayer().getHealth() < && event.getPlayer().getHealth() > 0){
                
    event.setTag(ChatColor.RED player.getName());
            }
        }
    But a players name stays green even at 1 heart, when they die I get a console error same as before.

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

    Sabersamus

    Try event.getNamedPlayer()

    Also:
    Code:java
    1. if (event.getPlayer().getHealth() < 20 || event.getPlayer().getHealth() > 16) {


    Would be true no matter what, because max health is 20, so it will stop there, try && instead of ||

    to get java syntax

    Code:
    [syntax=java] public static void main(String[] args){} [/syntax]
     
  17. Offline

    MattexFilms

    Thanks for the java syntax.

    It is still not working. Here is the code:

    Code:java
    1. @EventHandler (priority = EventPriority.HIGHEST)
    2. public void onNameTagGreen(PlayerReceiveNameTagEvent event) {
    3. Player player = event.getNamedPlayer();
    4. if (event.getNamedPlayer().getHealth() < 20 && event.getNamedPlayer().getHealth() > 16) {
    5. event.setTag(ChatColor.GREEN + player.getName());
    6. }else if(event.getNamedPlayer().getHealth() < 15 && event.getNamedPlayer().getHealth() > 11){
    7. event.setTag(ChatColor.YELLOW + player.getName());
    8. }else if(event.getNamedPlayer().getHealth() < 10 && event.getNamedPlayer().getHealth() > 6){
    9. event.setTag(ChatColor.GOLD + player.getName());
    10. }else if(event.getNamedPlayer().getHealth() < 5 && event.getNamedPlayer().getHealth() > 0){
    11. event.setTag(ChatColor.RED + player.getName());
    12. }
    13. }

    Now nothing happens, the name tags don't change colour at all.
     
  18. Offline

    MrFigg

    You probably have to put TagAPI.refreshPlayer(Player player) in an EntityDamageEvent listener.
     
  19. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    MattexFilms

    Please read the documentation I laid out in the first post. It clearly documents when the event fires and when you need to force it with a refresh call.
     
  20. Offline

    MattexFilms

    Ahh yes, forgot about the refresh. I'll see if that works.

    Ok I'm not sure if I am doing this right or not. I made a EntityDamageListener class and put this in it

    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageEvent event){
    3. Player player = (Player) event;
    4. TagAPI.refreshPlayer(player);
    5. }


    And in the other listener class I have this:

    Code:java
    1. @EventHandler (priority = EventPriority.HIGHEST)
    2. public void onNameTagGreen(PlayerReceiveNameTagEvent event) {
    3. Player player = event.getNamedPlayer();
    4. if (event.getNamedPlayer().getHealth() < 20 && event.getNamedPlayer().getHealth() > 16) {
    5. event.setTag(ChatColor.GREEN + player.getName());
    6. }else if(event.getNamedPlayer().getHealth() < 15 && event.getNamedPlayer().getHealth() > 11){
    7. event.setTag(ChatColor.YELLOW + player.getName());
    8. }else if(event.getNamedPlayer().getHealth() < 10 && event.getNamedPlayer().getHealth() > 6){
    9. event.setTag(ChatColor.GOLD + player.getName());
    10. }else if(event.getNamedPlayer().getHealth() < 5 && event.getNamedPlayer().getHealth() > 0){
    11. event.setTag(ChatColor.RED + player.getName());
    12. }
    13. }

    I am not sure if I am making some rookie mistake.

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

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    You need to go learn how to code in Java first, then come back to this.
     
    jimuskin likes this.
  22. Offline

    MrFigg

    Code:JAVA
    1. HashMap<String, ChatColor> damageColor = new HashMap<String, ChatColor>();
    2.  
    3. @EventHandler(priority = EventPriority.MONITOR)
    4. public void onPlayerJoin(PlayerJoinEvent event) {
    5. setDamageColor(event.getPlayer(), 0);
    6. }
    7.  
    8. @EventHandler(priority = EventPriority.MONITOR)
    9. public void onPlayerQuit(PlayerQuitEvent event) {
    10. if(damageColor.containsKey(event.getPlayer().getName())) {
    11. damageColor.remove(event.getPlayer().getName());
    12. }
    13. }
    14.  
    15. @EventHandler(priority = EventPriority.MONITOR)
    16. public void onEntityDamage(EntityDamageEvent event) {
    17. if(event.getEntity() instanceof Player) {
    18. setDamageColor((Player) event.getEntity(), event.getDamage());
    19. }
    20. }
    21.  
    22. public void setDamageColor(Player player, int damage) {
    23. int health = player.getHealth() - damage;
    24. if(health<=5) damageColor.put(player.getName(), ChatColor.RED);
    25. else if(health<=10) damageColor.put(player.getName(), ChatColor.GOLD);
    26. else if(health<=15) damageColor.put(player.getName(), ChatColor.YELLOW);
    27. else damageColor.put(player.getName(), ChatColor.GREEN);
    28. TagAPI.refreshPlayer(player);
    29. }
    30.  
    31. @EventHandler(priority = EventPriority.HIGHEST)
    32. public void onNameTag(PlayerReceiveNameTagEvent event) {
    33. Player player = event.getNamedPlayer();
    34. if(damageColor.containsKey(player.getName()) {
    35. event.setTag(damageColor.get(player.getName())+player.getName());
    36. }
    37. }
    38.  


    This is a crude example that sends packets way to often. (Every time the player takes any damage, regardless of whether or not their damage color has changed.)
     
  23. Offline

    Lolmewn

    Why have I never seen this before?!
     
    chasechocolate likes this.
  24. Offline

    gamemakertim

    Yes, but not if u only change the collor

    How do i fix this?
    08:55:13 [INFO] [Metrics] Software caused connection abort: recv failed

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

    blablubbabc

    Can I hide the player tags with this?
     
  26. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Can't hide them. Can make them "" which is a small box without text in it.
     
  27. Offline

    hawkfalcon

    Why not? Can this be added in the future? Or is it client side?
     
  28. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Clientside. Disappears only when crouching
     
  29. Offline

    hawkfalcon

    Hmm, interesting. Maybe you could somehow trick it into thinking your crouching ;p
    Its interesting that you can change them, but not remove them. Odd.
     
    blablubbabc likes this.
  30. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    That's easy, but the client will show you as crouching.
     
    hawkfalcon likes this.
Thread Status:
Not open for further replies.

Share This Page