Arraylist messages only for players which are in the arraylist

Discussion in 'Plugin Development' started by Minesuchtiiii, Mar 30, 2014.

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

    Minesuchtiiii

    HI, I want that only players that are in the array list can see messages from the players that are in the array list.
    so if:
    player A is in the list, player B too.
    if A types something B can see it.

    Player A is in the list, Player b not.
    if a types something player b cant see it.
    Player A isnt in the list, player B isnt too.
    if A types something B can see it.
    Player A isnt in the list, player B is in the list and player C is too in the list.
    if a types something B and C cant see the message.
    if B or see types something only b or C can see it.

    How would this work??

    I have:

    for(Player list : Bukkit.getOnlinePlayer() {
    if(waiting.contains(list.getName() {
    list.sendMessage....
    e.setFormat....

    and so on...

    }

    Thanks at all
     
  2. Offline

    SuperOmegaCow

    Minesuchtiiii
    if the player who typed is in the array then for through the array and send it to people in the array. If the player who typed is not in the array then for through online players and if the player is not inside the array, send it to them.
     
  3. Offline

    Minesuchtiiii

    bump & SuperOmegaCow dont really helps me... some code?!?! PlayerChatEvent or AsyncPlayerChatEvent?!
     
  4. Offline

    SuperOmegaCow

    Minesuchtiiii
    I refuse to spoon feed. I will tell you to use Async if you know what you are doing.
     
    xMrPoi likes this.
  5. Offline

    Minesuchtiiii

    Tryied everything... still not working!!-.-
     
  6. Offline

    SuperOmegaCow

    Minesuchtiiii
    I don't really think you know what you are doing. Go learn java. If you still do not want to listen here is some code:


    public void onPlayerChat(PlayerChatEvent event) {

    if(this.arraylist.conatins(event.getPlayer().getName()) {
    event.setCanceled();
    for(String name : this.arraylist) {
    Player p = Bukkit.getPlayer(name);
    p.sendMessage(event.getMessage);
    }
    } else {
    event.setCanceled();
    for(Player p : Bukkit.getOnlinePlayers()) {
    if(this.arraylist.contains(p.getName())
    p.sendMessage(event.getMessage());
    }
    }

    }
     
  7. Offline

    Minesuchtiiii

    SuperOmegaCow
    First thanks that you replied to my thread!
    And 2nd: I know what I do.. all other events are working, so it`s the first time I code with the AsyncChatEvent..
    This is for a minigame plugin which will work on a server...
    But still thanks, I`m trying it out now!
    You're welcome.
     
  8. Offline

    Minesuchtiiii

  9. Offline

    xTrollxDudex

  10. Offline

    Minesuchtiiii

  11. Offline

    xTrollxDudex

    Minesuchtiiii
    SynchronizedList list = Collections.synchronizedList(new ArrayList<>());
     
  12. Offline

    SuperOmegaCow

    xTrollxDudex
    I didn't provide code that is async. It is sync hence the PlayerChatEvent not AsyncPlayerChatEvent.
     
  13. Offline

    Minesuchtiiii

    SuperOmegaCow
    so whats the new code?? sry but I really need help, it's really important!
     
  14. Offline

    SuperOmegaCow

    Minesuchtiiii
    Please just learn java. If you can't figure it out with what I provided then something is seriously wrong.
     
  15. Offline

    xMrPoi

    Make an arraylist. Cancel the event. Check if sender is in array. If so, loop through people in array and send to them. If not, loop through all players online and if they're in the list, don't send it to them.
     
  16. Offline

    xTrollxDudex

    SuperOmegaCow
    Oopsies... You would be better off using async pce because the sync one is deprecated, and I believe it is for internal CB use only.
     
    skyrimfan1 likes this.
  17. Offline

    skyrimfan1

    You also might want to not store the player instances in an arraylist because it can cause memory leaks. Try instead:

    Code:
    ArrayList<String> playerNames = new ArrayList<String>();
    for (String name : playerNames) {
        Player player = Bukkit.getPlayerExact(name)
        // Send message here
    }
    You could also try enclosing it in a sync runnable if you're using the APCE?
     
  18. Offline

    Minesuchtiiii

    SuperOmegaCow
    At first I want say that I'm not from england, America, or whatever! I'm 14 and now you can think what you want!
    I should learn java? Dude, I said you that everything is working perfect.. I'm just working the first time at the PlayerAsyncChatEvent!
    You understand? I know java, I'm just trying the first time this event, and I'm 14 and not english, I learn english in school and I'm coding inly since 4 months!!!
    So.. any problem?

    xMrPoi
    xTrollxDudex
    skyrimfan1

    What I've done until now is this, It isn't working as I want...If only 1 player is in the list and chats, no one can see the message, if 2 players are in the list, the other dude can see the message, but not the one who typed the message...
    Rest is working.. but if you have any idea feel free to tell me.

    Code:java
    1. public class ChatListener implements Listener {
    2.  
    3. private Main plugin;
    4.  
    5. public ChatListener(Main plugin) {
    6.  
    7. this.plugin = plugin;
    8.  
    9. }
    10.  
    11. @EventHandler
    12. public void onChat(AsyncPlayerChatEvent e) {
    13. Player p = e.getPlayer();
    14.  
    15. int i = this.plugin.cfg.getInt("punkte.spieler." + p.getName());
    16.  
    17. if(this.plugin.waiting.contains(p.getName())) {
    18. for(Player all : Bukkit.getOnlinePlayers()) {
    19. if(this.plugin.waiting.contains(all.getName())) {
    20. if(this.plugin.waiting.size() < 1) { //
    21. e.setCancelled(true);
    22. e.setFormat("§7(§e" + i + "§7)[§aLobby§7]§9" + p.getName() + " §f> §a" + e.getMessage());
    23. all.sendMessage("§7(§e" + i + "§7)[§aLobby§7]§9" + p.getName() + " §f> §a" + e.getMessage());
    24. return;
    25.  
    26. } else { //
    27. e.setCancelled(true);
    28. e.setFormat("§7(§e" + i + "§7)[§aLobby§7]§9" + p.getName() + " §f> §a" + e.getMessage());
    29. all.sendMessage("§7(§e" + i + "§7)[§aLobby§7]§9" + p.getName() + " §f> §a" + e.getMessage());
    30. p.sendMessage("§7(§e" + i + "§7)[§aLobby§7]§9" + p.getName() + " §f> §a" + e.getMessage());
    31. return;
    32. } //
    33. } else {
    34. e.getRecipients().remove(all);
    35. e.setCancelled(false); //
    36. return;
    37. }
    38.  
    39. }
    40. for(Player notall : Bukkit.getOnlinePlayers()) {
    41. if(!(this.plugin.waiting.contains(notall.getName()))) {
    42. e.getRecipients().remove(notall);
    43. return;
    44. }
    45. }
    46.  
    47.  
    48. }
    49. if(!(this.plugin.waiting.contains(p.getName()))) {
    50. for(Player alle : Bukkit.getOnlinePlayers()) {
    51. if(this.plugin.waiting.contains(alle.getName())) {
    52. e.getRecipients().remove(alle);
    53. e.setFormat(e.getFormat());
    54. e.setCancelled(false);
    55. return;
    56. }
    57. }
    58. }
    59.  
    60.  
    61. }
    62. }


    Thanks for reading this.

    bump

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

    skyrimfan1

    It appears to me that you're cancelling the chat event and then setting its format right afterwards (lolwut) which is not good ... You also might want to reverse the operator at this line:
    Code:
      if(this.plugin.waiting.size() < 1) { //
     
  20. Offline

    Minesuchtiiii

    skyrimfan1 i cancell it so the normal chat doesnt appear. Then i set the format and send the message to all players which are in the list..
    Still not working perfect!
     
  21. Offline

    malandrix_bunny

    Prepare for the upcoming update. Instead of saving a player's name I recommend that you use the Player.getUniqueId() to store players instead now.
     
  22. Offline

    skyrimfan1

    Only if the data stored is permanent. If not, it is purrfectly okay to store names.
     
Thread Status:
Not open for further replies.

Share This Page