Solved Prefix color depending on who sees it.

Discussion in 'Plugin Development' started by phil14052, Apr 24, 2016.

Thread Status:
Not open for further replies.
  1. Hi.
    When a player writes a chat messages. In this case the player is called "Bob".
    Simon (Another player) does not like Bob. Simon has set Bob as his enemy.
    When Bob sends a chat message Bobs name is red for Simon. But for all others who have not set Bob as there enemy will see his name with another color.

    Example:
    What Simon sees:
    <Bob> HELLO GUYS!
    What other people who have not set Bob as there enemy see:
    <Bob> HELLO GUYS!
    Another example:
    Like factions when you set an enemy faction.

    I don't know if you will understand. But if you do I hope you can help.

    Extra notes:
    I have tried looking on google. Maybe I just look for the wrong things.
    And I do not mean something like this:
    Code:
    List<Player> hatesBOB = new ArrayList<Player>();
         hatesBOB.add(Bukkit.getPlayer("Simon"));
         for(Player p : Bukkit.getOnlinePlayers()){
             if(hatesBOB.contains(p)){
                 p.sendMessage("§c<Bob>§r HELLO GUYS!");
             }else{
                  p.sendMessage("§b<Bob>§r HELLO GUYS!");
                }
         }
    - Phil
     
  2. Online

    CraftCreeper6

    @phil14052
    I don't quite understand what you're asking.

    If not the code you posted, then what do you need?
     
  3. @CraftCreeper6
    I am not good at explaining.
    But I am trying to make a team plugin. Where you can set an other team as the teams enemy.
    So I want it so when a player from the enemy team writes in chat. The people from the first team will then see the enemy team with a red prefix. But all the other teams that have not set the enemy team to be there enemy will see it with another color.
    I hope you understand... Maybe I can find another example.
     
  4. Offline

    mine-care

    @phil14052
    Team? Why not using Scoreboards and let them do the job?

    If you want to handle it yourself you need to consider the following:
    the message is sent to each player individually. If that player is in the same team as the sender, the prefix is blue. Otherwise it is red.
    What is that you have trouble with?
     
  5. Offline

    Gonmarte

    I think you can do that with the event https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/player/AsyncPlayerChatEvent.html
    If Gonmarte set Bob as enemie, just create a hashmap, keys are peaple who set players as enemies, values are the enemies of that players. So you just need to get the value of Bob, and if it is Simon just change the color.
    Use uuids in the map.

    EDIT:
    Instead of storing both players, Store as key the team, and as values the enemies.
     
  6. @mine-care I do not use the scoreboard teams because I want it to be compatible with other scoreboard plugins. And I want to be a little creative with it.

    @mine-care & @Gonmarte
    I don't see how i can give them two outputs with the AsyncPlayerChatEvent.
    I want to use it. And with the event.setFormat.
    But I don't see how!
    I can only think of canceling the event. And send a new message to every player on the server with the prefix.

    My code now:
    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
        public void chatEvent(AsyncPlayerChatEvent e){
            Player p = e.getPlayer();
            ITeam team = tm.getPlayersTeam(p);
            if(e.isCancelled() == false){
                if(team != null){
                    PlayerRank prank = tm.getPlayerRank(p, team);
                    String rank = "";
                    if(prank.equals(PlayerRank.OWNER)) rank = "**";
                    if(prank.equals(PlayerRank.MEMBER)) rank = "*";
                    e.setFormat(e.getFormat().replace("{team_rank}", rank).replace("{team_name}", team.getName()));
                }else{
                    e.setFormat(e.getFormat().replace("{team_rank}", "").replace("{team_name}", ""));
                }
            }
        }
    
     
  7. Offline

    mine-care

  8. @mine-care
    I know about the #setformat method but it can not set a per player format. It sets the global format for when a player sends a message.
     
  9. Offline

    mine-care

    @phil14052 Indeed, thought you where trying to use it for some other part.
    Well then you need to cancel the chat event and send the message manually to all players by looping through all of them and sending them the message. this way you know which player receives what thus you can controll it ;)
     
  10. Offline

    mcdorli

    You can use == on enums.
     
  11. @mine-care Yeah.. I hoped there was a better way. But thats good enough.
    @mcdorli I know that. But sometimes I'm lazy (I know it's a bad thing). And then I think it's faster to do .equals(...) or == false (Could just do !(e.isCancelled())) and so on.

    But thanks for your help everyone.
    - Phil
     
  12. Offline

    mine-care

    @phil14052 for enums == is faster given that equals does quite a few more checks but even if it didn't it is still a method call so it might take slightly longer. Now ofcourse we are talking about very small ammounts of time but it is worth mentioning. The main advantage of == over .equals(Object) is that it is null safe. so if eitherof the two sides is null it will just be false unlike .equals which will error.

    If the thread is solved, please mark it as 'Solved'
     
  13. @mine-care Thank you. I did not know that about == and .equals(Object).
    The thread is half solved but i will mark it as 'Solved'.
     
    mine-care likes this.
Thread Status:
Not open for further replies.

Share This Page