Solved Ping? Help!

Discussion in 'Spigot Plugin Development' started by Teh_Matt_GRyt, May 18, 2018.

Thread Status:
Not open for further replies.
  1. Hi I want to make a ping plugin but I searched on google (and yt) how to get the player's ping!
    I tried this:
    Code (Text):
    public class Ping implements CommandExecutor {
        private Plugin plugin = Main.getPlugin(Main.class);
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("ping")) {
                if (!p.hasPermission("")) {
                    p.sendMessage(plugin.getConfig().getString("Messages.No-Permissions").replaceAll("&", "ยง"));
                    return true;
                } else {
                    int ping = ((CraftPlayer) p).getHandle().ping;
                    p.sendMessage("Your ping: " + ping);
            return true;
    But when I type /ping it gets me an error and when I remove the "+ ping" from the message it works fine!
  2. Sorry our crystal sphere is not working today, would you be so nice and provide the error? :)
    • Funny Funny x 1
  3. would you be so nice and give me an example code or just don't speak :)
  4. [​IMG]

    So you are

    1) Not following the rules of posting here?
    2) Rude to other members trying to help you get better answers?
    3) Have a way too big mouth?
    4) No clue on how to ask precise questions?

    Get out of here, nobody will be helping you today!
    • Agree Agree x 2
    • Funny Funny x 1
  6. Please read. We're trying to help you. He's asking for the error you're getting so we know what's wrong. Though I gotta be honest, you really don't even need a stack trace to see what's wrong.

    Two things are wrong with your code:
    1. You're never checking if your command sender is an instance of a player. You're casting it to Player and assuming it is a Player. It may not be the case. If you type the command in the console, you'll get an error.
    2. You're trying to (when sending your ping message) add an integer to a string. Convert the int to a string if you want to display it in the message. Using String.format is probably the cleanest.

    I don't see anything funny here. Other than OP's somewhat random rude reply earlier, I really don't see much to laugh about. OP tried coding something, OP seems to be new to coding, he explains what is wrong (some people just didn't read the post properly) and he's asking for help. What's so funny? :confused:
  7. This may be right, but String + int results in String, so there is no issue there (apart from performance).

    Thats the funny thing. OP takes it as given that people will help him, even if he spits in their face.
    • Like Like x 1
  8. So.... I tried to change the int to string but it sais that:
  9. Oh, does Java automatically convert it to String? I didn't even know that detail. I suppose that only happens for the primitive type? I always convert it/use String.format... Guess you learn something new every day.

    I didn't say change the declaration to String, I said convert it to String. That's not the same :p
    Code (Java):
    in your sendMessage function. Keep the ping declaration as int/Integer. I assume you're a beginner, so you'll have to learn about objects in Java. Every object has a type. Java (and most other languages) are very strict when it comes to providing objects to a function. For example, if a function has a String parameter, you must provide a String. Anything else won't be accepted (which is what you're trying to do here, you're giving a combination of String and Integer to a function). Though there might be some automatic conversion as @Friwi said, you're better off understanding the object oriented stuff in Java.
    • Agree Agree x 1
    • Friendly Friendly x 1
  10. Same again.... an error occured while etc.
    here is the error:
    and here my code:
  11. You are coding with R1 which is not recommended to use but your server is R3. NMS is not multi version compatible, you would need reflections to have it work on all versions.
    • Agree Agree x 2
  12. You are compiling against a different version of spigot. Try building against your server's version.

    Server version: v1_8_R3
    Development version: v1_8_R1
    • Agree Agree x 2
  13. ok thanks! ill try it and if it wont work ill inform you :D
    Yay works! thank you!!
    #14 Teh_Matt_GRyt, May 18, 2018
    Last edited: May 18, 2018
    • Friendly Friendly x 1
  14. Dude, are you joking?
    • Funny Funny x 1
Thread Status:
Not open for further replies.

Share This Page