commandSender() within listener

Discussion in 'Plugin Development' started by Euphi_, Oct 12, 2011.

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

    Euphi_

    Ok, hopefully someone can help me out here. The plugin i'm working on will take a command, then spawn a listener, then based on what they do, call a command from another plugin. That's right, i'm going inception style.

    Here's an example

    Code:
    ///Ok so here they typed in command create
    private void commandCreate(CommandSender sender, String[] args)
    {
    ///Argument 2 was "test"
        if(args[1].equalsIgnoreCase("test"))
        {
            PluginManager pm = this.getServer().getPluginManager();
    
    ////Here i call the listener, I added the argument sender because that's what i need, but it doesnt help, i will show you
    
            final TestPlayerListener playerListener = new TestPlayerListener(this, sender);
    
    ///I also looked at adding it here, but registerEvent wont take more variables, and i'm not gonna change the api or write my own
            pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener, Event.Priority.Normal, this);
    
        }
    }
    Now here is my listener class

    Code:
    public class TestPlayerListener extends PlayerListener
    {
        Server server;
        public final test plugin;
    
    ////Here is where the sender argument got passed, how do I make this usable below
        public TestPlayerListener(test instance, CommandSender sender)
        {
    
            this.plugin = instance;
        }
        public void onPlayerInteract(PlayerInteractEvent event)
        {
            //    plugin.getServer().broadcastMessage("In Event");
                if(event.getAction() == Action.RIGHT_CLICK_BLOCK)
                {
                          //They right-clicked so i want to run command /ping so i need the command sender, but it is isolated above
                    server = this.plugin.getServer().dispatchCommand(sender, "ping");
                }
                if(event.getAction() == Action.LEFT_CLICK_BLOCK)
                {
    //This is why i need it to be in listener, i might also want to run command pong, otherwise i would leave it in main code
                    server = this.plugin.getServer().dispatchCommand(sender, "pong");
                }
        }
    }
    Any ideas? either how i can pass the CommandSender to the playerinteract, or how i can call commandSender through some weird Bukkit magic?
     
  2. Offline

    Father Of Time

    You sure can, here is how!

    Code:
    public class TestPlayerListener extends PlayerListener
    {
        Server server;
        public final test plugin;
        private CommandSender Sender;    // << create local variable to assign the CommandSender to
    
        public TestPlayerListener(test instance, CommandSender sender) // << the CommandSender input
        {
            this.plugin = instance;
            Sender = sender;    // << Sets the local variable to the input CommandSender
            }
    
        public voud CustomFunction()
        {
            Sender.sendMessage("How YOU doin?!?");
        }
        }
    }
    The sender is already being passed to your Listener class, so its just a matter of making a local variable and then assigning the sender thats being passed to your listener to the local variable. Then any time you need to manipulate the player simply refer to the local variable!

    Hope this helps, Good luck! :D

    EDIT: omg the forums deleted my code example... *sigh* Give me a few minutes and I will jot up another example for ya... brb
     
    Euphi_ likes this.
  3. Offline

    Euphi_

    I feel stupid for not catching that.... thank you very much Father of Time
     
  4. Offline

    Father Of Time

    You shouldn't, when I was first learning to program I struggled with wrapping my mind around variable passing, local variable storing and accessing variables that are outside the scope of a function. This is very common entry level confusion and I’m just proud of you for taking the initiative to overcome the problem!

    You are very welcome, It’s my pleasure to share my experience. Take care and good luck with your project!
     
  5. Offline

    nisovin

    Just as a warning, creating a Listener when a command is sent is not a good idea. You'll end up with a lot of listeners, and there's no way to remove them without doing a full /reload. You'd be better off rethinking your logic and using just one listener.
     
  6. Offline

    Euphi_

    At the moment i Just have the 1 listener that handles quite a few functions, however the problem i now have, and based on your post I need to redesign, is that i wanted to send a message back to the function that started the listener once an event is detected. I was planning on having that particular event detection call a function back in the main file, set a global variable, then call the function that started the listen using the global variable as a bypass. I need to find a way to kill that listener though....work work work
     
  7. Offline

    Euphi_

    I dont know if this is a bug, but when I spawn a listener from a class other than main, then have that function call a different command from my plugin, it cancels the listener. This is very useful
     
Thread Status:
Not open for further replies.

Share This Page