Running another Class onEnable

Discussion in 'Plugin Development' started by Yummy Juice, Sep 11, 2012.

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

    Yummy Juice

    So in my code I can't get it to run my other class.

    DayM.java
    Code:
    package me.yummyjuice.daym;
    
    import org.bukkit.World;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class DayM extends JavaPlugin {
        
        public DayMTime timeChange;
        
        public void onEnable(){
            System.out.print("[DayM] Enabling DayM.");
            
            this.timeChange = new DayMTime();
        }
        
        public void onDisable(){
            System.out.print("[DayM] Disabling DayM.");
        }
    }
    
    class DayMTime implements Runnable {
        
        private World world;
    
        @Override
        public void run() {
            System.out.print("The Plugin is working!");
            long time = world.getTime();
            if(time == 15000) {
                world.setTime(13000);
            }
            if(time == 2500) {
                world.setTime(5000);
            }
            
        }
        
    }
    
     
  2. Offline

    _Waffles_

    uh. is it just me or are those two classes exactly the same thing?
     
  3. Offline

    Yummy Juice

    Woops. I've changed the code all into one file now anyway but it still wont work. It's updated.

    Edit: My reply is waitng approval. It says: "I get an error when I change it to either of them.
    The second option wants to change run to a static and the first one gives me an error on public DayMTime run() {
     
  4. Offline

    _Waffles_

    this.timeChange = new DayMTime();
    change to this.timeChange = new DayMTime().run();

    should work, otherwise just DayMTime.run();
     
  5. Offline

    Yummy Juice

    I get an error when I change it to either of them.
    The second option wants to change run to a static and the first one gives me an error on public DayMTime run() {
     
  6. DayMTime is a runnable, so you have to run it. You could just do:
    timeChange.run();
    after
    this.timeChange = new DayMTime();
    but this will call it one time (in onEnable) only, I think you want to repeat it, so use bukkits scheduler: http://wiki.bukkit.org/Scheduler_Programming
    In the end it should look something like this:
    Code:java
    1. public class DayM extends JavaPlugin {
    2.  
    3. public void onEnable(){
    4. System.out.print("[DayM] Enabling DayM.");
    5.  
    6. getServer().getScheduler().scheduleSyncRepeatingTask(this, new DayMTime(), 0L, 1L);
    7. }
    8.  
    9. public void onDisable(){
    10. System.out.print("[DayM] Disabling DayM.");
    11. }
    12. }

    Or try the lib24time way (at this example it cuts out sunset and sunrise):
    Code:java
    1. public class DayM extends JavaPlugin implements Listener{
    2.  
    3. public void onEnable(){
    4. System.out.print("[DayM] Enabling DayM.");
    5.  
    6. getServer().getPluginManager().registerEvents(this, this);
    7. }
    8.  
    9. public void onDisable(){
    10. System.out.print("[DayM] Disabling DayM.");
    11. }
    12.  
    13. @EventHandler
    14. public void daytimeChange(DaytimeChangeEvent event)
    15. {
    16. if(event.getDaytime().equals("Sunset"))
    17. lib24time.setTime("19:00");
    18. else if(event.getDaytime().equals("Sunrise")
    19. lib24time.setTime("06:00");
    20. }
    21. }
     
  7. Offline

    Yummy Juice

    Thanks so far but I keep getting errors on public DayMTime run() { that say it's incompatiable with run()
     
  8. I can't find that. ^^ If you mean my first example it should look like this:
    Code:java
    1. public class DayM extends JavaPlugin {
    2.  
    3. public DayMTime timeChange;
    4.  
    5. public void onEnable(){
    6. System.out.print("[DayM] Enabling DayM.");
    7.  
    8. this.timeChange = new DayMTime();
    9. timeChange.run();
    10. }
    11.  
    12. public void onDisable(){
    13. System.out.print("[DayM] Disabling DayM.");
    14. }
    15. }

    Or are you reffering to this (which basically does the same):
    This is invalid code. run() doesn't return a DayMTime(), that's why you have to split it into two lines.

    But keep in mind that your run() will run only one time with this. As you check the current server tick and as you use a Runnable I think what you really want is the scheduler to call run() for you every tick.
     
  9. Offline

    Yummy Juice

    It's further down in what I posted. What would my whole file look like with the scheduler aswell?

    Edit: I think I fixed it.
     
  10. Offline

    _Waffles_

    oh woops I just did that cause just before I was working with threads..
    Code:
    new Thread(TestThread.instance).start();
     
  11. Offline

    Yummy Juice

    Ok it outputs in the console which proves it's working but it won't actually change the time.
     
  12. Cause it's extremely unlikely that the time == 15000 or 2500 at the time your plugin enables. That's why you should use the scheduler.
     
  13. Offline

    Sabersamus

    Also, you never initialized world :p
     
Thread Status:
Not open for further replies.

Share This Page