Trying to make a string catcher

Discussion in 'Plugin Development' started by tincopper2, Feb 26, 2014.

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

    tincopper2

    Hello, I've been riddling myself to sticks and rubble into this mere matter of simplicity over solving a diluted form of an easy peezy subject, however in my case, not so easy.
    See here is what I am trying to do, I am trying to make a filter that detects a player sending a command and seeing if it is a kick/ban/mute/jail/kill command, then take the players name after it. Well the part I got is the name, however I don't quite know how to tell a full name based on part of the name given like essentials does, for instance, /ban tin will most likely ban tincopper2 if I am the only one online. But what I'm wondering, is how essentials does it, and how I may be able to put it all into the event handler to decode their name based on a slice of it.
    Thank you, tincopper2.
     
  2. Offline

    Gater12

    tincopper2
    I think it's Bukkit.getPlayer("Name"); so that's why everybody uses Bukkit.getPlayerExact("Name"); to get the actual player then Bukkit choosing the closest.
     
  3. Offline

    Jake6177

    Bukkit.getPlayer() is like Google's "I'm Feeling Lucky" in that it will choose the first that shows up in the list of players that start with "tin".

    Bukkit.getPlayerExact() will only retrieve a player if there is a player with the exact name "tin" online. Otherwise, this method will return null.
     
  4. Offline

    tincopper2

    This is exactly what I need because I am practically forming an API around essentials, without actually having it be an API.
     
  5. Offline

    Jake6177

    Good luck! Sounds interesting.
     
  6. Offline

    tincopper2


    Code:java
    1. plugin.getLogger().info(event.getMessage().split(" ")[0]);
    2. if (event.getMessage().split(" ")[0] == "/kick")
    3. {
    4. plugin.getLogger().info("==================================");
    5. plugin.getLogger().info(event.getPlayer().getDisplayName()+" typed /kick!");
    6. plugin.getLogger().info("==================================");
    7. }

    Should this code return whether the player typed /kick <player> in the console no matter the player?
     
  7. Offline

    Jake6177

    If you want to check the command, you can try the PlayerCommandPreprocessEvent and that will return the command used (with the "/" included.)

    edit: I'm not sure, but if you literally type in "/kick <playername>", using e.getCommand() will literally print the string "/kick <playername>" making it easy to tell them exactly what they typed. The good thing about this event also is that you can cancel the command before it is processed instead of after.

    edit2: Just occurred to be that you might already be using that event xD
     
  8. Offline

    Gater12

    tincopper2
    I think you should split using the whitespace regex.
    Don't compare Strings using == use .equals or .equalsIgnoreCase
     
  9. Offline

    tincopper2

    Whitespace Regex?

    EDIT: Wow! thanks! Using .equalsIgnoreCase made it work! Nice job! Props to you on being a helpful person.
     
  10. Offline

    Jake6177

    If you're interested in why, that's because "==" is a reference comparison, e.g. both objects point to the same location in memory. ".equals()" and ".equalsIgnoreCase()" are comparisons of the values in the objects, and not the objects themselves. I read somewhere that they can be read as "meaningfully equivalent."
     
  11. Offline

    tincopper2

    Nice, so now I use the line;
    Code:java
    1. if (event.getMessage().split(" ")[0].equalsIgnoreCase("/kick") && Bukkit.getPlayer(event.getMessage().split(" ")[1]).getDisplayName().equalsIgnoreCase("tincopper2"))

    And it works, perfectly. But how would I go about referencing the same if label to different things, like other than kick, any string from an array perhaps? Say the array cmds[]=["/kick","/ban","/mute"], how can I do event.getMessage().split(" ")[0].equalsIgnoreCase(cmds[])??

    EDIT: Uh oh, now I'm getting an error that the plugin couldn't process the event everytime someone uses the /kick command.

    UPDATE: I see the problem, it is calling for the Bukkit.getPlayer, without the player existing. I do not know how to go around this! Bump!
     
  12. Offline

    Jake6177

    Code and stack trace please? Also, you can use an ArrayList and use the ArrayList.contains method. E.g. if(ArrayList.contains("cmdName")).
     
  13. Offline

    tincopper2

    UPDATE: I see the problem, it is calling for the Bukkit.getPlayer, without the player existing. I do not know how to go around this! Bump!


    Code:
    [23:54:11 ERROR]: Could not pass event PlayerCommandPreprocessEvent to AntiBan v
    0.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:320) ~[spigot.jar:git-Spigot-1279]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-1279]
            at org.bukkit.plugin.TimedRegisteredListener.callEvent(TimedRegisteredLi
    stener.java:30) ~[spigot.jar:git-Spigot-1279]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:482) [spigot.jar:git-Spigot-1279]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:467) [spigot.jar:git-Spigot-1279]
            at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:948) [spigot.jar:git-Spigot-1279]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :817) [spigot.jar:git-Spigot-1279]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [spigot.jar:git-Spigot-1279]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat
    .java:65) [spigot.jar:git-Spigot-1279]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:147
    ) [spigot.jar:git-Spigot-1279]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(ServerConnection.java
    :77) [spigot.jar:git-Spigot-1279]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    98) [spigot.jar:git-Spigot-1279]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    73) [spigot.jar:git-Spigot-1279]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    60) [spigot.jar:git-Spigot-1279]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :467) [spigot.jar:git-Spigot-1279]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [spigot.jar:git-Spigot-1279]
    Caused by: java.lang.NullPointerException
            at com.antiBan.AntiBanListener.onPlayerCommand(AntiBanListener.java:39)
    ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _03]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _03]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_03]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_03]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:318) ~[spigot.jar:git-Spigot-1279]
            ... 15 more
    [23:54:11 INFO]: tincopper2 issued server command: /kick gdfg
    Stack Trace.

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

    Jake6177

    Check for null. if(value != null) or if(value == null) then stop the execution by sending a message and returning.

    BTW: If getPlayer or getPlayerExact returns null, it means the player is not online (or something went terribly terribly wrong)
     
  15. Offline

    tincopper2

    It worked, thank you again. I don't plan on releasing this to the public since they banned me from bukkit dev, but if you would like a private copy I would be more than happy to send you one due to the amount of help you have contributed for me making this.
     
Thread Status:
Not open for further replies.

Share This Page