Define a String as a Player

Discussion in 'Plugin Development' started by KaiBB, May 14, 2012.

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

    KaiBB

    I think the title is self explanatory.
    My code so far isn't working.
    Code:
        @EventHandler
        public void onPlayerChat(PlayerChatEvent event) {
            String msg = event.getMessage();
            for (Player player : Bukkit.getServer().getOnlinePlayers()) {
                if (msg.contains(player.getName())) {
                    if (Bukkit.getServer().getPlayerExact(msg).isOnline()
                            && Bukkit.getServer().getPlayerExact(msg).isOp()) {
                        Player op = Bukkit.getServer().getPlayerExact(msg);
                        Location loc = op.getLocation();
                        Block b = loc.getBlock();
                        Material mat = b.getType();
                        DO STUFF
                    }
                }
            }
        }
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    Code:
    Bukkit.getServer().getPlayerExact(msg)
    if your msg is anything but a name this will fail with a null iirc
     
  3. Offline

    KaiBB

    Even if it is a name, it won't work. It returns a large error.
     
  4. Offline

    r0306

    KaiBB
    Try this:
    Code:
        @EventHandler
        public void onPlayerChat(PlayerChatEvent event) {
            String[] msg = event.getMessage().split(" ");
            for (Player player : Bukkit.getServer().getOnlinePlayers()) {
            for (int i = 0; i < msg.length; i ++ ) {
                if (msg[i].contains(player.getName())) {
                    if (Bukkit.getServer().getPlayerExact(msg).isOnline()
                            && Bukkit.getServer().getPlayerExact(msg).isOp()) {
                        Player op = Bukkit.getServer().getPlayerExact(msg);
                        Location loc = op.getLocation();
                        Block b = loc.getBlock();
                        Material mat = b.getType();
                        DO STUFF
                        }
                    }
                }
            }
        }
    What are you trying to do anyways?
     
  5. Offline

    Sagacious_Zed Bukkit Docs

    And the error?
     
  6. Offline

    KaiBB

    r0306
    I'll try this, thanks. And, I guess it doesn't matter if I tell ya, making a plugin for the admins that keep Minecraft in the background. When a player chats a message containing an op's name, for ex, "Help me KaiBB!", it plays a note on the op's client.

    Sagacious_Zed
    If r0306's method doesn't work, I'll post it.
     
  7. Offline

    r0306

    KaiBB
    Ahh. Then in that case I think the code I gave you should do the trick. However, you might want to not do exact match for the player check since players usually just type in all lower case letters. Not sure if it will matter though.
     
  8. Offline

    KaiBB

    Strange, upon using r0306's method, my world file corrupted itself and was no longer in GZIP format. It's probably something else, but good thing I'm running it on a test server!

    getPlayerExact works for lowercase, I believe. Plus, if I don't do exact, chat is greedy. If I typed "k" into chat, it would ping every op with a name starting with k

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
Thread Status:
Not open for further replies.

Share This Page