Connect commands to PlayerListener

Discussion in 'Plugin Development' started by Jogy34, Aug 18, 2011.

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

    Jogy34

    I have a command that I want to modify blocks every time a player moves. To register if a player moves that has to go through a PlayerListener but PlayerListeners cant handle commands so how do I connect them?
     
  2. Offline

    MadMonkeyCo

    Make a public HashMap<string, boolean> in your main class where the string is the player's name and the boolean is wether his steps should modify blocks. Then in your player listener you check if the specific player that's walking has a boolean value of true, if he does the blocks are modified. To learn more about HashMaps and plugin coding in general check out the HUGE Plugin tutorial: http://wiki.bukkit.org/HUGE_Plugin_Tutorial
     
  3. Offline

    Jogy34

    @MadMonkeyCo I've created the HashMap but how do I check if the boolean value is true in one class from the other class
     
  4. Offline

    MadMonkeyCo

    In your PlayerListener you should have a constructor that takes a parameter of your plugin's main class's type.
    Code:
    private somePlugin pluginInstance
    public somePlayerListener(somePlugin pInstance)
    {
        pluginInstance = pInstance;
    }
    Then you need to pass your plugin as a variable when you initialize your PlayerListener in your main class
    Code:
    public void onEnable()
    {
        private somePlayerListener PListener = new somePlayerListener(this);
        //do stuff
    }
    Now as long as your HashMap is public you can access it from your PlayerListener and any other class that uses a similar constructor. But note that the listener must be initialized in onEnable and not in the class's local scope. It is though possible to declare it outside onEnable but it must be initialized there.
     
  5. Offline

    Jogy34

    @MadMonkeyCo Sorry about this but I'm still somewhat lost and I cant find any explanation of how to do this on the internet. If I wanted to check if a player is in the HashMap from the PlayerListener I'm guessing that I would have to do something along the lines of _Boolean inHM = ???.getValue();_ but I dont know what to put in the ??? part and I don't even know if that's what I would do.
     
  6. Offline

    sddddgjd

    You write if([classname].hashmap.contains(xxxx)) {do stuff}
    Where classname is your main class name!
     
Thread Status:
Not open for further replies.

Share This Page