Solved How to get args in a event?

Discussion in 'Spigot Plugin Development' started by ShishKaBobz, Apr 23, 2017.

Thread Status:
Not open for further replies.
  1. Hello! I would like to know how i can get a argument in a event class? so instead of add the command Mumble Jumble i can find the args, Such as

    Player p;
    p = e.getPlayer();
    is a alternate to adding the Command Stuff...
    Thanks in advance!
     
  2. You shoul better use CommandExecutors for all such stuff as it's much more stable
    Code (Text):
    ublic class MyCoolCommandClass implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                //TODO
                }
                return true;
            }
            return false;
        }
    }
     
    • Funny Funny x 1
  3. But i dont want that in my event class
     
  4. Are you trying to get the args of a command that was run?
     
  5. maldahleh

    Wiki Team

    What are you trying to do? There are no arguments in an event. You can get attributes of the event using the getters of the event.
     
  6. Sorry, but I don't understand what you want to do.
     
  7. But it is not for event class

    It's a separate class for command(-s).
     
  8. In my event Class (WIP)

    Code (Text):
    package com.ionmc.Bot;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    public class OnChat implements Listener {
       
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e){
            Player p;
            p = e.getPlayer();
            String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
            boolean search = e.getMessage().contains("* google");

            if(e.getMessage().contains("*")){
                e.setCancelled(true);
                p.sendMessage("§7§l[§6§lIon§b§lBot§7§l] §8§lList Of Command's §4§l>>");
                p.sendMessage("* Teleport me to spawn");
                p.sendMessage("");
               
            }
            if(e.getMessage().contains("* Teleport me to spawn")){
                e.setCancelled(true);
                p.sendMessage("§7§l[§6§lIon§b§lBot§7§l] Teleporting");
                Bukkit.dispatchCommand(p, "/spawn");
            }
            }

        }


     
    I want to be able to access the players arguments such as "1010bob: Hi Im 1010bob" How can i get Im and or 1010bob
     
  9. LOL
    They are not arguments. It's just plain String you get in this event
     
  10. You're trying to use the chat event to make commands? Could you explain WHY this is your goal? Btw you shouldn't use .contains("*"), you should use .startsWith("*") for that.
     
    • Agree Agree x 1
    • Winner Winner x 1
  11. Instead of creating a new 'BRAND-NEW-BOT-1337-HARDCODED-UNHACKABLE-UNCONFIGURABLE' try studying Java and SpigotAPI with it's Configuration system and (of course) terminology
     
  12. Therefore you have to split the message:

    Code (Text):
    String[] args = e.getMessage().split(" ");
    But that way isn't nice!
     
    • Agree Agree x 1
Thread Status:
Not open for further replies.