Solved Local Chat Problem.

Discussion in 'Spigot Plugin Development' started by Nosmakos, Jun 4, 2016.

Thread Status:
Not open for further replies.
  1. hey guys i made a post yesterday how to make a local chat but i made it and now i have other problem.
    (trying to figure out for hours what method i have to use for it to work)
    so the problem is that i have made a command that when they do /chat global it will add them to the list
    if they do /chat local it will remove them from the list.
    so the thing is i made it this to working with the local event:
    Code (Text):
        @EventHandler
        public void chat(AsyncPlayerChatEvent event) {
            if (this.getConfig().getBoolean("LocalChat.Enable")){
                Player sender = event.getPlayer();
                Iterator<Player> it = event.getRecipients().iterator();
                while (it.hasNext()){
                    Player player = it.next();
                    if (this.localChat.contains(player.getPlayer().getUniqueId())){
                        if ((!inRange(getDistance(sender.getLocation(), player.getLocation())))){
                            event.getRecipients().remove(player);
                            Bukkit.getConsoleSender().sendMessage("1");
                            return;
                        }
                        if (!this.localChat.contains(player.getPlayer().getUniqueId())){
                            if ((!inRange(getDistance(sender.getLocation(), player.getLocation())))){
                                it.remove();
                                Bukkit.getConsoleSender().sendMessage("2");
                            }
                        }
                    }
                }
            }
        }
    so the problem is that this area is not working - Local Chat
    Code (Text):
                        if (!this.localChat.contains(player.getPlayer().getUniqueId())){
                            if ((!inRange(getDistance(sender.getLocation(), player.getLocation())))){
                                it.remove();
                                Bukkit.getConsoleSender().sendMessage("2");
    the first one it does! what i want to do is when someone is in global chat it will add him in the list and he can be able to send in everyone in this server a message and everyone can see it. Also he won't be able to see others messages because they will be in the local chat but if he is inside of the distance like it should be he can be able to see others messages but they will be in the global chat. So this is working 100%
    Global chat is this
    Code (Text):
                    if (this.localChat.contains(player.getPlayer().getUniqueId())){
                        if ((!inRange(getDistance(sender.getLocation(), player.getLocation())))){
                            event.getRecipients().remove(player);
                            Bukkit.getConsoleSender().sendMessage("1");
                            return;
                        }
    so the problem is now that the local chat is not working but only global and the distance. But in the local it's like it canceling the whole event it doesnt event a message. In local i want to talk to each other only if they are inside of the distance. Anyone can help me? (Sorry if i wrote too many things i'm trying to explain what i'm trying to do as much is possible.)

    Also this is the global to see that is working:

    If someone is in global chat and away from the distance he can't see the local messages https://snag.gy/edL1iK.jpg
    but if he is in the same distance he can see other messages https://snag.gy/qEl5kt.jpg
     
    #1 Nosmakos, Jun 4, 2016
    Last edited: Jun 4, 2016
  2. Inkzzz

    Resource Staff

    You code formatting is wrong for what you are trying to do. You want to add another curly brace - [​IMG]
     
  3. like this?
    Code (Text):
                            Bukkit.getConsoleSender().sendMessage("1");
                            return;
                        }
                    }else if (!this.localChat.contains(player.getPlayer().getUniqueId())){
                        if ((!inRange(getDistance(sender.getLocation(), player.getLocation())))){
                            it.remove();
                            Bukkit.getConsoleSender().sendMessage("2");
                        }
                    }
                }
            }
        }
    EDIT: with this way it only enables the local now
     
    #3 Nosmakos, Jun 4, 2016
    Last edited: Jun 4, 2016
  4. Inkzzz

    Resource Staff

    You need to add a curly braced bracket before your else if statement.
     
  5. how i can do this because i might know what you mean but not right now because i don't know all the meanings yet can u show an example or something?
     
  6. Inkzzz

    Resource Staff

    By curly faced bracket I just mean '}'. Regardless, if this doesn't fix your issue I strongly suggest in you rewriting the listener as it won't take long and you'll most likely come across the issue.
     
  7. okay will do also if i make the event be like this
    Code (Text):
        @EventHandler
        public void chat(AsyncPlayerChatEvent event) {
            if (this.getConfig().getBoolean("LocalChat.Enable")){
                Player sender = event.getPlayer();
                Iterator<Player> it = event.getRecipients().iterator();
                while (it.hasNext()){
                    Player player = it.next();
                    if (this.localChat.contains(player.getPlayer().getUniqueId())){
                        return;
                    }
                    if ((!inRange(getDistance(sender.getLocation(), player.getLocation())))){
                        it.remove();
                        Bukkit.getConsoleSender().sendMessage("2");
                    }
                }
            }
        }
    everything is working with this code but by doing return; it's like canceling the event for the players who are in the list by doing /chat global
    is there any way to make if a player is in the global chat and no one is around him from the distance he won't see the player message if the player is local but if someone come to his distance he will see the local messages. Because right now i cancel the whole system if he is i the list.
    for example if the local(sender) is not in the list(like it supposed to be because he is local) and he is not in the distance make the global to not see any message from others.
     
  8. Inkzzz

    Resource Staff

    Use the continue keyword. This simply skips the current iteration.
     
  9. hmm what if
    for example if the local(sender) is not in the list(like it supposed to be because he is local) and he is not in the distance make the global to not see any message from others. is there any way to check this?
     
  10. Inkzzz

    Resource Staff

    Sorry, I am not understanding what you are trying to achieve.
     
  11. Im trying when someone is in the global chat he will be able to send message to everyone like a default chat but he won't be able to see others messages if they are not in the distance. Also Local chat is to send messages to each others only if they are in the distance .
     
  12. Inkzzz

    Resource Staff

    Before you check for local chat, check if the player is in global chat, if so, just return the method without doing anything, otherwise, execute your current code.
     
  13. this is what i did but if i return it he will be able to see others messages even from distance and not distance but i want the global to work like the local but with global he will be able to send message to everyone and also to see others messages he must be inside of this distance

    is there any way to send the message to everyone? this might work and also the distance will work for global if i do this i think
     
  14. Inkzzz

    Resource Staff

    You just need to format your code sufficiently, and use the keyword of 'continue' to skip iterations etc...
     
  15. how i can do that? never tried.
     
  16. Thank god i fixed it.

    this is the code now guys
    Code (Text):
        @EventHandler
        public void chat(AsyncPlayerChatEvent event) {
            if (this.getConfig().getBoolean("LocalChat.Enable")){
                Player sender = event.getPlayer();
                Iterator<Player> it = event.getRecipients().iterator();
                while (it.hasNext()){
                    Player player = it.next();
                    if (this.localChat.contains(sender.getPlayer().getUniqueId())){
                        if ((!inRange(getDistance(player.getLocation(), sender.getLocation())))){
                            event.getRecipients().remove(!this.localChat.contains(sender.getPlayer().getUniqueId()));
                            return;
                        }
                    }
                    if ((!inRange(getDistance(sender.getLocation(), player.getLocation())))){
                        it.remove();
                        Bukkit.getConsoleSender().sendMessage("2");
                    }
                }
            }
        }
    i had to check in the Global(in the list) if someone who was Local(not in the list) and wasn't in the distance to not show the messsage of the local players. Also added a return; so i can send the message to everyone if i am in the global!
    Code (Text):
                    if (this.localChat.contains(sender.getPlayer().getUniqueId())){
                        if ((!inRange(getDistance(player.getLocation(), sender.getLocation())))){
                            event.getRecipients().remove(!this.localChat.contains(sender.getPlayer().getUniqueId()));
                            return;
     
Thread Status:
Not open for further replies.