Bukkit pm variable problem

Discussion in 'Plugin Development' started by jackwilsdon, Sep 24, 2011.

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

    jackwilsdon

    I am experiencing a problem with PluginManager, and the problem is, that later in the script, pm.registerEvent cannot see it, so I get the error "pm cannot be resolved" in eclipse.

    Here's a snippet of my code. Any help is appreciated!
    PHP:
        public void onEnable() {
     
            
    PluginManager pm this.getServer().getPluginManager();
     
            
    log.info("Lockdown v1.0b Enabled");
         
        }

        
    boolean lockd false// On start, lockdown is off
        // I'm not sure, but when i put this in onenable, it also throws a not found error later on :S
     
        // Get commands

        
    public boolean onCommand(CommandSender senderCommand cmdString commandLabelString[] args){

            
    Player player = (Playersender;

                if(
    cmd.getName().equalsIgnoreCase("lockdown")){

                    if(
    lockd){

                        
    player.sendMessage("Lockdown Disabled");
                        
    lockd false;

                    } else {

                        
    player.sendMessage("Lockdown Enabled");
                        
    lockd true;

                        
    pm.registerEvent(Event.Type.PLAYER_JOINplayerListenerEvent.Priority.Normalthis);

                    }

                    return 
    true;

                }

                return 
    false;

        }
     
  2. Offline

    Bilkokuya

    The problem is just a matter of variable scope.

    If you want the PluginManager pm variable to be used outside of the onEnable method - you need to define it outside of that method. Just define it as a static variable - it doesn't need to have a value. Then you can simply assign it the value within onEnable. From then, the rest of that main class will be able to use it.
    (The other main alternative, is to define it everytime you want to use it).

    The lockd boolean is the same issue. If you define it in the context (the scope) of onEnable, it can only be used within onEnable (hence why you found you needed to define it outside the method). It's generally good practise to keep your class variables defined at the start of the class (below the imports) instead of spread out through the file as well - that way you won't lost track of them.
     
  3. Offline

    jackwilsdon

    thanks! I'm having one more problem though. I have a boolean set in the main class, how can I make it available to an include? Because in the included java file, it says the boolean is unset when it is in the main class. I hope that's not too confusing :S
     
  4. Offline

    Bilkokuya

    If you want the variable to only hold one value - throughout any number of instances of a class - define it as static (meaning that each instance of a class uses the single instance of the static variable; it doesn't create a new instance of it).

    So you'd define lockd as:
    PHP:
    static boolean lockd;

    Whether you initialise it (give it a value) on the same line, or in the onEnable method is up to you.

    If you're interested in reading up more about it all, the topics about it are generally tagged as variable scope, and similar things.

    I hope that helps.
     
  5. Offline

    Nitnelave

    That's not quite the way static works. A static variable does not need an instance, but is only dependent on the class. So most static should be final, for the use we have of them. You don't need (and shouldn't) make your variables static, but you need to pass a reference to you main class. And when you declare a variable, assign a value to it, or make sure one is assigned before trying to read from it.
    bool toto;
    If(toto ==true)

    This will give you a , whereas the right way is :
    bool toto =true;
     
  6. Offline

    jackwilsdon

    I don't completely understand you, as I have 2 files, main.java and mainPlayerListener.java, and main.java includes mainPlayerListener.java. Here's the code;

    main.java
    PHP:
    package com.main;

    import java.util.logging.Logger;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class 
    main extends JavaPlugin {
        static 
    boolean lockd;
        
    Logger log Logger.getLogger("Minecraft");
        
        public 
    void onEnable(){
        
            
    boolean lockd true;
            
            
    log.info("Your plugin has been enabled!");
            if(
    lockd) {
                
    log.info("TRUE");
            } else {
                
    log.info("FALSE");
            }
        }
     
        public 
    void onDisable(){
            
    log.info("Your plugin has been disabled.");
        }
    }

    mainListener.java
    PHP:
    package com.main;

    import com.main.main;
    import java.util.logging.Logger;
    import org.bukkit.plugin.java.JavaPlugin;

    public class 
    mainListener extends JavaPlugin {


        
    Logger log Logger.getLogger("Minecraft");

        public 
    void onEnable(){
            
    log.info("Your plugin has been enabled!");
            if(
    lockd) {
                
    log.info("TRUE");
            } else {
                
    log.info("FALSE");
            }
        }

        public 
    void onDisable(){
            
    log.info("Your plugin has been disabled.");
        }
    }

    The problem is, is that in mainListener.java, there's a red underline saying undefined for if(lockd), but I have included main.java, which has lockd set to true. Please help me!
    Thanks!
     
  7. Offline

    Darkman2412

    Just a question, did you follow any tutorials on plugin/java developing?
     
  8. Offline

    jackwilsdon

    Yeah, a plugin one, all I need is to pass it to the include mainListener, and my plugin will be finished ^_^
     
  9. Offline

    Nitnelave

    Ok, you should get back to learning the basics of java : instances, variables, scopes, constructors, includes, etc... You don't seem to know a lot about it. We're willing to help, but we can't code for you, you have to learn!
     
  10. Offline

    jackwilsdon

    Well, will you please just tell me how to make a boolean global to all includes ^_^. I will attempt to learn java after I have finished this plugin! Thanks!
     
  11. Offline

    ItsHarry

    Ehm, why do you have the same code in your listener class as in your main class?

    Well basically, you make a public static boolean.
    then in your enable code, you make the boolean whatever you want
    so

    public static boolean lockd;

    onEnable() {
    lockd = true;
    }

    then when you want to get lockd from an other class, you just use main.lockd and it will return the boolean
     
  12. Offline

    jackwilsdon

    anyways, now i have a DIFFERENT problem -__-

    In my script, i'm trying to call an event I made in the include.

    main.java
    PHP:
    package com.main;

    import java.util.logging.Logger;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.main.mainListener;

    public class 
    main extends JavaPlugin {
        static 
    boolean lockd;
        
    Logger log Logger.getLogger("Minecraft");

        public 
    void onEnable(){

            
    boolean lockd true;

            
    log.info("Your plugin has been enabled!");
            if(
    lockd) {
                
    mainListener.power();
            } else {
                
    log.info("FALSE");
            }
        }



        public 
    void onDisable(){
            
    log.info("Your plugin has been disabled.");
        }
    }
    mainListener.java
    PHP:
    package com.main;

    import java.util.logging.Logger;

    import org.bukkit.plugin.java.JavaPlugin;

    public class 
    mainListener extends JavaPlugin {

        
    Logger log Logger.getLogger("Minecraft");

        public 
    void onEnable(){
            
    log.info("Your plugin has been enabled!");
            if(
    main.lockd) {
                
    log.info("TRUE");
            } else {
                
    log.info("FALSE");
            }
        }

        public 
    void onDisable(){
            
    log.info("Your plugin has been disabled.");
        }

       public 
    void power(){
           
    log.info("sup?");
         
    // This doesn't get called, as it throws the error "Cannot make static reference to the non-static method" in main.java where it calls power();
       
    }
    }
    So how do I call power() non-statically?
    EDIT:
    I fixed the previous one, thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  13. Offline

    Nitnelave

    You're going to run in a lot of problems like that... You should learn java and THEN code a plugin. You don't run a marathon without learning to run first!
    But if you insist, you have to create a constructor for your listener, and pass the instance of the main plugin to it, so you can do a reference to any of it's variables of methods later on.
     
  14. Offline

    jackwilsdon

    :O i followed your advice, and learnt the basics of instances and constructors, and i think it's working :D

    IT GOT IT WORKING ^_^ i send the constructor the boolean and it works! I see your marathon login Nitnelave!
    EDIT:
    one more question, how do i change my details to Plugin Developer, like it is in your name on the left :confused:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  15. Offline

    Nitnelave

    Ah, well, you have to diserve it ;)
    Once you publish your first plugin, it should come in the weeks after. Be patient, young padawan!
     
    jackwilsdon likes this.
  16. Offline

    jackwilsdon

    thanks! But yet another problem has cropped up! But it's bukkit not java!
    How can I make the command run-able in console, because it throws the error
    PHP:
    byjava.lang.ClassCastExceptionorg.bukkit.craftbukkit.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    Here's my code around the lines it occurs on;
    PHP:
    public boolean onCommand(CommandSender senderCommand cmdString commandLabelString[] args){

            
    //Player player = (Player) sender;

                
    if(cmd.getName().equalsIgnoreCase("lockdown")){

                    if(
    lockd){
                        
    boolean isplayer = (sender instanceof Player);
                        if(
    isplayer){
                            
    log.info("Player!");
                        }
                        
    lockd false;
                        
    log.info("DISABLED");
                    } else {
                        
    lockd true;
                        
    log.info("ENABLED");
                    }
                    
    mainListener.power(lockd);
                    return 
    true;

                }

                return 
    false;

        }
    Yours thankfully, Jack
     
  17. Offline

    Nitnelave

    Could you post the whole stackTrace please, with the line numbers of the error, and point what line exactly is the error? I don't see anything wrong with your code. Btw, you could do if(sender instanceof player) instead of if (isplayer).

    You don't have the line (player) sender, right? It is commented?
     
  18. Offline

    jackwilsdon

    it's commented out.
    and here's the rest;
    PHP:
    package com.main;

    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.main.mainListener;

    public class 
    main extends JavaPlugin {
        static 
    boolean lockd;
        
    Logger log Logger.getLogger("Minecraft");

        public 
    void onEnable(){
      
            
    log.info("Your plugin has been enabled!");

        }

        public 
    boolean onCommand(CommandSender senderCommand cmdString commandLabelString[] args){

            
    //Player player = (Player) sender;

                
    if(cmd.getName().equalsIgnoreCase("lockdown")){

                    if(
    lockd){
                        
    boolean isplayer = (sender instanceof Player);
                        if(
    isplayer){
                            
    log.info("Player!");
                        }
                        
    lockd false;
                        
    log.info("DISABLED");
                    } else {
                        
    lockd true;
                        
    log.info("ENABLED");
                    }
                    
    mainListener.power(lockd);
                    return 
    true;

                }

                return 
    false;

        }

        public 
    void onDisable(){
            
    log.info("Your plugin has been disabled.");
        }
    }
     
  19. Offline

    Nitnelave

    Ok, don't declare your bool lockd static (it is dependant on the instance).
    You have to post the entire stacktrace so we can see where the error is. Next time you post some code, put it between [java] tags (I think it's java).
    Have you tried it again since you commented the line?
     
  20. Offline

    jackwilsdon

    Sorry >.< i know what you meant, i read it wrong; here's the error;
    PHP:
    >lockdown
    16
    :30:21 [WARNINGUnexpected exception while parsing console command
    org
    .bukkit.command.CommandExceptionUnhandled exception executing command 'lockdown' in plugin Test v1.0
        at org
    .bukkit.command.PluginCommand.execute(PluginCommand.java:41)
        
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:163)
        
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:353)
        
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:349)
        
    at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:499)
        
    at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:478)
        
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:374)
        
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
    Caused byjava.lang.ClassCastExceptionorg.bukkit.craftbukkit.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
        at com
    .main.main.onCommand(main.java:26)
        
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:39)
        ... 
    7 more
     
  21. Offline

    Nitnelave

    Once again, I ask you this : have you tried again since commenting the line? Try deleting it completely.
     
  22. Offline

    jackwilsdon

    for some reason, deleting the commented line made it work :confused:
    EDIT:
    I'm guessing bukkit still processes it eh?
     
  23. Offline

    Nitnelave

    Or you didn't comment it in a right way... Before blaming a well-used, trustworthy tool, think about what YOU could have done wrong.
     
  24. Offline

    jackwilsdon

    Isn't // right?
    EDIT:
    In Eclipse, It's green (commented out)
     
  25. Offline

    Nitnelave

    It is. I don't what you did then.
     
  26. Offline

    Technius

    Code:
    //this is how you check if the console sends the command
    
            Player player = null;
            if (sender instanceof Player) {
                player = (Player) sender;
            }
            if (player == null)
            {
                     log.info ("Console sent this command!)";
             }
             else
             {
                    log.info ("Player sent this command!)";
                    player.sendMessage("You sent this, didn't you?)";
              }
    
    
    Don't use player.sendMessage to the console otherwise you get a Null Pointer Exception
     
  27. Offline

    Nitnelave

    You don't really need the player variable, if you won't do anything with it...
     
  28. Offline

    Technius

    Don't use "cmd.getName()", use commandLabel , just because it's shorter :p

    If you try player.sendMessage and the sender is a console(aka null) then you get a null pointer exception. You have to check for things like that.

    Code:
    //this will send a NPE if the console uses this command
    if (commandLabel.equalsIgnoreCase("test"))
    {
           player.sendMessage ("asdf");
    }
    //this will check if the sender is console and avoids NPE
    if (commandLabel.equalsIgnoreCase("test"))
    {
        if (player != null)
        {
            player.sendMessage ("asdf");
        }
        else
        {
            log.info ("asdf");
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  29. Offline

    jackwilsdon

    MY PLUGIN WORKS, I <3 YOU GUYS :D. Without you I couldn't have done it!
     
    Technius likes this.
  30. Offline

    Technius

    No problem :p
     
Thread Status:
Not open for further replies.

Share This Page