Trying to implement listener

Discussion in 'Spigot Help' started by Warriorn, Aug 15, 2018.

  1. Im trying to get an Listener to work depending on the players rank (class). Im trying to register it in a onCommand. Clearly it is not working but i get no errors. Does anyone know how i shoud code it or where i can find and a answer?

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

    Player player = (Player) sender;

    if (Main.getfileManager().getRank(player).equals(Rank.ASSASIN)) {

    Bukkit.getPluginManager().registerEvents(new EventsClass(), (Plugin) this);

    } ]

    [public class EventsClass implements Listener {

    public void onMove(PlayerMoveEvent event) {
    Player player = event.getPlayer();

    PotionEffect effect = new PotionEffect(PotionEffectType.SPEED, 50, 1);


    } ]

    If anyone would like to know my project feel free to coment!
    #1 Warriorn, Aug 15, 2018
    Last edited: Aug 15, 2018
  2. Would you mind putting your code in [ code ] tags? That would make it way easier to read.

    Typically, you don't register events in a command. You'd register them all once, in the onEnable method of your plugin class. The event handler itself can then determine whether to act based on some variable. For example, in the command, you would store the player's UUID somewhere, and then in the event handler, see if one of the stored UUIDs belongs to the player which moved, then take action.
    • Agree Agree x 1
  3. I have stored the players UUID in a data.yml file. How would i get the event handler to cheak for that? I tried to make the code in the Main class and in the event class but i got allot of errors because of the Player player = (Player) sender as i understand it can only be used in a onCommand
  4. Strahan


    Basically, when you make your event, it will have an event object passed to it. Like public void onPlayerJoin(PlayerJoinEvent e). The e variable in that statement holds data pertinent to the event that was triggered. (Doesn't have to be "e" btw, that's just what I typically use). If the event has anything to do with a player, it will expose the Player object through e. You can either read the javadocs or, like I do when I am working with an event I never used before, just type e. and look at the list of stuff that comes up for auto complete and it generally is self explanatory.

    Not to mention you can get a Player object through a variety of non-event related methods, though context of what you are doing is what determines where you want to get it.
  5. Thanks, I managed to make it work using

    public void onMove(PlayerMoveEvent e) {
    Player player = e.getPlayer();
    Rank rank = Main.getfileManager().getRank(player);
    PotionEffect effect = new PotionEffect(PotionEffectType.SPEED, 50, 1);

    if (rank.equals(Rank.ASSASIN)) {