Solved Only run code if a variable in the main class is true

Discussion in 'Plugin Development' started by TheKingElessar, Jul 28, 2018.

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

    TheKingElessar

    I'm trying to make a command that enables and disables my plugin. Because I can't actually use a command in the plugin to enable the plugin after it's disabled, I'm using if statements to run code only if it's disabled. In my Main class, I've created the variable used to track if it's disabled or not.

    Code in the Main class:
    Code:
    public class Main extends JavaPlugin {
        public static boolean wdEnabled;
       
        @Override
        public void onEnable() {
            wdEnabled = true;
        }
    
        @Override
        public void onDisable() {
            wdEnabled = false;
        }
    
    // There are also commands to enable, disable, and check the status of the plugin, but I didn't include them since they seem to work fine.
    }
    Code in the Listener class:
    Code:
    public void witherSkullHit(EntityDamageByEntityEvent damageEvent) {
        if(Main.wdEnabled) {
            // Do stuff
        }
    }
    
    However, the code in the above if statement is never executed. Why is this?

    I do not import the Main class at all, if that's a problem.

    I realize that this question is probably more of a question about Java in general, but I figured I'd try here first.

    Thanks!
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    johnny boy

    maybe use something "better", like a BukkitRunnable. (To check if the plugin is enabled). If that's too difficult for you or something, just go with your method.

    And if you didn't know about BukkitRunnables well wow you learn something great every day.
     
  4. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner How would a BukkitRunnable be better than a variable check?
     
    A5H73Y likes this.
  5. Offline

    TheKingElessar

    @timtower

    EDIT 2: I am so dumb. After doing a bunch of testing, I realized I had actually changed a small thing in the event that I had forgotten about. When I took that into account, it all worked perfectly. Thanks for your help, and sorry about wasting your time! :)

    My listener class was registered:

    Code:
    PluginManager pm = getServer().getPluginManager();
    listener listenerVariable = new listener(this);
    pm.registerEvents(listenerVariable, this);
    
    And the second snippet of code in my original post is part of the listener class.

    I don't really understand what the above code is doing, so that could be my problem. It was just copy-pasted from somewhere else.

    Edit: So I looked into registering events some more. To make my plugin easier to follow, I split the listener class into separate classes for each event, then registered each separately using this:
    Code:
    getServer().getPluginManager().registerEvents(new EntityDamage(), this);
     
    Last edited: Jul 29, 2018
Thread Status:
Not open for further replies.

Share This Page