Only show chat if conditions are met

Discussion in 'Plugin Development' started by ItsLeoFTW, Aug 27, 2014.

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

    ItsLeoFTW

    So, for a new (unique) minigame I'm making (building off of a tutorial), I want to make something like this:

    • Player A is in arena C.
    • Player B is in arena D.
    • Player B chats, but only the people in the same arena as player B see the message.
    • Any other chat messages (someone chats who's not in the game) will only be shown to people who are not in a game.
    This is the listener that handles chat... can anyone help?
    Code:java
    1. @EventHandler
    2. public void onChat(AsyncPlayerChatEvent e) {
    3. Set<Player> recipients = e.getRecipients();
    4.  
    5. Player p = e.getPlayer();
    6. String msg = e.getMessage();
    7. recipients.clear();
    8. recipients.add(p);
    9. if (!ArenaManager.getManager().isInGame(p)) return;
    10. Arena ar = getArenaWithPlayer(p.getName());
    11.  
    12. e.setCancelled(false);
    13. e.setFormat(e.getFormat());
    14. e.getRecipients().clear();
    15. p.sendMessage("in arena");
    16. for (String s : ar.getPlayers()) {
    17. if (Bukkit.getPlayer(s) != null) {
    18. recipients.add(Bukkit.getPlayer(s));
    19.  
    20. }
    21. }
    22. }
    23. public Arena getArenaWithPlayer(String p) {
    24. Arena a = null;
    25. for (Arena ar : ArenaManager.getManager().arenas) {
    26. if (ar.getPlayers().contains(p)){ a = ar; return a; }
    27. }
    28. return null;
    29. }


    I simply can not figure this out.
     
  2. Offline

    Necrodoom

    But what is the issue?
     
  3. Offline

    ItsLeoFTW

    Oh. The issue is that players not in an arena can still see messages from a player in an arena.
     
  4. Offline

    Necrodoom

    ItsLeoFTW I can spot quite a few errors there.
    Multiple useless and unused variables, ignoring case where player is not in arena, to not actually setting the recipient list.

    This is assuming you actually registered the event.
     
  5. Offline

    ItsLeoFTW

    Necrodoom

    I guess I'll just rewrite it, it kind of does seem bad.
     
Thread Status:
Not open for further replies.

Share This Page