Solved Custom class for code execution? That does NOT extend/implement

Discussion in 'Plugin Development' started by Larsn, Aug 9, 2015.

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

    Larsn

    I am making a plugin,
    and I am wondering about this.

    I have a scheduler setup in MaybeThis.java:
    Code:
    public class MaybeThis {
       
        private Main plugin = null;
       
        public MaybeThis(Main plugin)
        {
           this.plugin = plugin;
        }
       
        public void Maybe(Boolean startup) {
            //FileConfiguration mineConfig = YamlConfiguration.loadConfiguration(plugin.getMinesFile()); 
           
            Bukkit.broadcastMessage("Broadcasting launched!");
            Bukkit.getConsoleSender().sendMessage("Broadcasting launched...");
           
            new BukkitRunnable()
            {
               public void run()
               {
                   Bukkit.broadcastMessage("Broadcast this in 1 minute again :D");
               }
            }.runTaskTimer(plugin, 0L, 20L*60);
        }
       
    }
    
    But what I am wondering is, how do I set this up in my Main class?
    I have tried implementing Listener for no reason, and then registering but that doesn't seem to work.
    I have also tried this:
    Code:
    public MaybeThis maybe;
    @Override
    public void onEnable(){
    maybe.Maybe(true) // here
    }
    
    But that throws a NPE at the given line.

    After googling I found this:
    Code:
    maybe = new MaybeThis();
    But eclipse tells me that the constructor is undefined. When I make it with this as a constructor it just doesn't work.

    Could someone help me?
     
  2. Offline

    xXCapzXx

    The constructor if undefined because you didn't even use it.
     
  3. Offline

    Larsn

    Code:
    maybe = new MaybeThis(this);
    What I am trying to say in those last lines, is that the above code doesn't work either.
     
  4. Offline

    xXCapzXx

    Can you post your whole class?
     
  5. Offline

    Larsn

    Main.java (open)

    public class Main extends JavaPlugin {

    public static Main plugin;

    // Variables
    public MaybeThis maybe;

    @Override
    public void onEnable() {
    plugin = this;
    Bukkit.getConsoleSender().sendMessage(consolePrefix + "Plugin has been " + ChatColor.GREEN + "enabled!");
    this.getCommand("plugincommand").setExecutor(new PluginCommand(this)); // NOTE: This works, and has nothing to do with it
    maybe = new MaybeThis(this);
    //maybe.Maybe(true);
    }

    @Override
    public void onDisable(){
    Bukkit.getConsoleSender().sendMessage(consolePrefix + "Plugin has been " + ChatColor.RED + "disabled!");
    }

    MaybeThis.java (open)

    public class MaybeThis {

    private Main plugin = null;

    public MaybeThis(Main plugin)
    {
    this.plugin = plugin;
    }

    public void Maybe(Boolean startup) {

    Bukkit.broadcastMessage("Broadcasting launched!");

    new BukkitRunnable()
    {
    public void run()
    {
    Bukkit.broadcastMessage("Broadcast this in 1 minute again :D");
    }
    }.runTaskTimer(plugin, 0L, 20L*60);
    }

    }

     
  6. Offline

    Zombie_Striker

    @Larsn
    It looks like everything should work. What is the line that does not work? Is it the line that you made into a comment?
     
  7. Offline

    Larsn

    The complete MaybeThis.java doesn't get called. When I launch the server, it's not broadcasting any message...
     
  8. Offline

    Zombie_Striker

    @Larsn
    Maybe its because this...
    You commented out .Maybe (see the //) meaning it will not be read. Remove the comment (the // ) it and it will work.

    Also, please stick to Java Naming Conventions.
     
    Larsn likes this.
  9. Offline

    Larsn

    Lmao, I didn't even see/realized that I should maybe remove the comment lines :rolleyes:
     
Thread Status:
Not open for further replies.

Share This Page