Solved Tellraw + ChatRange?

Discussion in 'Spigot Plugin Development' started by WestZahid, May 14, 2016.

  1. Hello guys,

    I´ve made a code to send tellraw messages with hover and cmd; so far it works.
    Now I need a solution to send it to all player in X block range, instead of command-sender only.

    I just found this TUT, but it explain only how to make it with normal chat or will it work for me, too?
    The diffrent is, that he "limit" the "normal" chat range and I like to send the tellraw to all player in range.

    It would be awesome if someone can help me and post a example.
    (if its possible I like to add it into Chat class)

    This is how I send it atmo:
    Code (Text):
    Chat.send(p, Chat.NORMAL(Language.getString("prefix")+p.getName()+Language.getString("roll")+Language.getString("hide"))
                                +","+Chat.CMD(NoD+Language.getString("dice")+NoDP, Language.getString("repeat"), "/roll "+NoD+" "+NoDP+" hide")
                                +","+Chat.NORMAL(ChatColor.DARK_GRAY+" ["+diceResault+ChatColor.DARK_GRAY+"]"));

    I must replace the p at " send(p, ..)" and loop it to all in range,or?(idk-how)


    I see later that there is a API named Chat Component, and if I know it earlier I had use it, but now I like to use my own code; I think you understand why.
     
    #1 WestZahid, May 14, 2016
    Last edited: May 15, 2016
  2. I don't know if this is what you would be looking for, but here...
    Code (Java):
    EntityLocation entityloc = new EntityLocation(player);
    for (Player p : player.getWorld().getPlayers()) {
    Integer distance = entityloc.getDistance(p);
    if(distance < 40) {
    //Send message..
    }
     
    • Informative Informative x 1
  3. Choco

    Moderator

    - EntityLocation does not exist. I think you mean Location (player.getLocation();)
    - Use primitive data types rather than their wrapper classes (int vs Integer)

    Other than that, yea, I think that may be the best way to do things
     
    • Agree Agree x 1
    • Informative Informative x 1
  4. Sorry, I'm tired lol... Thanks for the correction.
     
    • Friendly Friendly x 1
  5. Dito, I´ll test it tomorrow. I hope it works with "Chat.send(p, ".
    so far, thank you :)
     
  6. Tip: Lambda.
    Code (Java):
    List<Player> radi = Bukkit.getOnlinePlayers()
        .stream()
        .filter(p->p.getWorld()==sender.getWorld()) // Filter (Playernamevariable->check)
        .filter(p->p.getDistance(sender)<=distanceMax) // Filter (Playervariable->Check)
        .forEach(p->p.sendMessage("Message here"));//For Each(playervariable-> check)
    Or just what the other dude said.
    Code (Java):
    for (Player p : Bukkit.getOnlinePlayers())
        if (p.getWorld() == sender.getWorld() && p.getDistance(sender) <= distanceMax)
            p.sendMessage(stuff);
    Now, if you'd want to send it to 1/7th or something like that of a 1000 player server, I'd rather make a new list to loop or just use lambda.
     
    • Informative Informative x 1
  7. I am atmo not at home, do you think that will work?

    In Command class
    Code (Text):

    In Command class
    String resault = "Chat.NORMAL("+Language.getString("prefix")+p.getName()+Language.getString("roll")+Language.getString("hide")+")"
                                +","+"Chat.CMD("+NoD+NoDP, Language.getString("repeat"), "/roll "+NoD+" "+NoDP+" hide"+")"
                                +","+"Chat.NORMAL("+ChatColor.DARK_GRAY+" ["+diceResault+ChatColor.DARK_GRAY+"]"+")";
    Chat.range(resault);
     
    I set Chat.NORMAL( in " " and the ) at the end

    In Chat class
    Code (Text):

    Chat.range(String resault){
        for (Player p : Bukkit.getOnlinePlayers())
        if (p.getWorld() == sender.getWorld() && p.getDistance(sender) <= distanceMax)
            Chat.send(p, resault);
     
     
    #7 WestZahid, May 14, 2016
    Last edited: May 14, 2016
  8. Your lambada is top notch but I'd do one thing different when filtering:
    Code (Java):
    .filter(p->p.getWorld()==sender.getWorld() && p.getDistance(sender)<=distanceMax).forEach(//BLAH);
    But that's just my opinion, it's like using nested if statements in my point of view xD, if you can do both checks at once it's easier on the machine to filer and it's a bit easier to read imo, but you can do it however you like :)
     
    • Like Like x 1
    • Winner Winner x 1
  9. Hey,
    thank you for your answers.

    I try both ways but get a underline at "getDistance".
    He want to change it to setFallDistance?
    Code (Text):

    public static void rangeCheck(Player sender,String resault){
            int maxDistance=25;
            for (Player p : Bukkit.getOnlinePlayers())
                if (p.getWorld() == sender.getWorld() && p.getDistance(sender) <= maxDistance){
                    send(p, resault);
                    }
        }
     
    I use this way because its more understandable for me.

    EDIT
    Is it possible to combine the code like this?
    I cant test it until i find out why getDistance dont work :(
    Code (Text):

    public static void send(Player sender,String msg){
            IChatBaseComponent chat=ChatSerializer.a("[\"\","+msg+"]");
            PacketPlayOutChat packet=new PacketPlayOutChat(chat);
            if(rangeCheck==true){
                int maxDistance=25;
                for (Player p : Bukkit.getOnlinePlayers())
                    if (p.getWorld() == sender.getWorld() && p.getDistance(sender) <= maxDistance)
                    ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);
                    }
                }
        }
     
     
    #9 WestZahid, May 14, 2016
    Last edited: May 14, 2016
  10. getDistance is a non existent method

    Edit:
    use Player#getLocation().distance(Player#getLocation());
     
    • Informative Informative x 1
  11. Yeah man you can only use get distance on a location.
     
  12. Thank you for your help via pm Übermensch :)
    ChatDistance for normal chat works fine.
    now I´ve got a new Problem (ofc) :D

    How can I send my message as console?
    I try to send the output to an extra method which should handle it, but I get always a error.
    I am sure its because of "PacketPlayOutChat" and so on, which is made for players not consoles.
    Is there any way to send my tellraws as console?

    My Code:
    Code (Text):
    public static void send(Player p, String resault, boolean use){
         
            File config=new File("plugins//RolePlayDice//config.yml");
            YamlConfiguration cfg= YamlConfiguration.loadConfiguration(config);
            boolean limitDistance=cfg.getBoolean("limitDistance");
            int maxDistance=cfg.getInt("distance");
         
            IChatBaseComponent chat=ChatSerializer.a("[\"\","+resault+"]");
            PacketPlayOutChat packet=new PacketPlayOutChat(chat);
            if(use==true && limitDistance==true){
                for (Player other : Bukkit.getOnlinePlayers())
                    if (other.getWorld() == p.getWorld() && other.getLocation().distance(p.getLocation()) <= maxDistance) {
                        ((CraftPlayer) other).getHandle().playerConnection.sendPacket(packet);
                    }
            }else if(use==true && limitDistance==false){
                for (Player other : Bukkit.getOnlinePlayers())
                    ((CraftPlayer) other).getHandle().playerConnection.sendPacket(packet);
            }else{
                ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);
            }
            return;
        }
     
        public static void consoleSend(String resault){
            IChatBaseComponent chat=ChatSerializer.a("[\"\","+resault+"]");
            PacketPlayOutChat packet=new PacketPlayOutChat(chat);
            for (Player other : Bukkit.getOnlinePlayers())
                ((CraftPlayer) other).getHandle().playerConnection.sendPacket(packet);
        }

    How I send my resault from Command class
    Code (Text):

    String resault=Chat.NORMAL(Language.getString("prefix")+p.getName()+Language.getString("roll"))
                 +","+Chat.CMD(NoD+Language.getString("dice")+NoDP, Language.getString("repeat"), "/roll "+NoD+" "+NoDP)
                 +","+Chat.NORMAL(ChatColor.DARK_GRAY+" ["+diceResault+ChatColor.DARK_GRAY+"]");
             Chat.send(p, resault, true);
     
     
  13. Bukkit#dispatchCommand(getConsoleSender, String command);