Time Changing Check

Discussion in 'Plugin Development' started by TheYamsMan, Dec 27, 2014.

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

    TheYamsMan

    EDIT: Skip this first post and skip down to the plugin running error please.

    How would I check to find if the current world is at a certain time? I want to change the time of the world every time it gets to, lets say 15000. I don't know of any events that can do this...ALSO please tag me if you do reply. Have had issues recently with that...

    IF NOT POSSIBLE:
    This is my code so far, excuse my playerMoveEventCheating, I don't know what else to try:
    Code:
    package me.yamerins.DC;
    
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class DC extends JavaPlugin implements Listener{
          
            @Override
            public void onDisable(){
                PluginDescriptionFile pdfFile = this.getDescription();
                this.getLogger().info(pdfFile.getName() + " Has Been Disabled");
            }
          
            @Override
            public void onEnable(){
                PluginDescriptionFile pdfFile = this.getDescription();
                this.getLogger();
                this.getLogger().info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled");
                getServer().getPluginManager().registerEvents(this, this);
            }
          
            boolean toggle = true;
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                if(commandLabel.equalsIgnoreCase("alwaysday")){
                    if(toggle){
                        toggle = false;
                        sender.sendMessage("Always day has been set to" + ChatColor.RED + " false.");
                        return true;
                    } else {
                        toggle= true;
                        sender.sendMessage("Always day has been set to" + ChatColor.GREEN + " true.");
                        return true;
                    }
                }
              
                if(commandLabel.equalsIgnoreCase("adstate")){
                    sender.sendMessage("Alwaysday is currently set to: " + ChatColor.BLUE + ChatColor.BOLD + toggle);
                    return true;
                }
                return false;
            }
          
            public void onMoveCheck(PlayerMoveEvent event){
                World w = event.getPlayer().getWorld();
                Player p = event.getPlayer();
                if(w.getTime() >= 11000){
                    w.setTime(100);
                    p.sendMessage("Time has been reset.");
                } else {
                    p.sendMessage("Time change failed.");
                }
            }
    }
    
    I am adding the Time change messages in the event to see if it worked and neither are showing when I move. The event is not working, so the event is not registering. Any ideas why? I have a listener and registered the events...
     
    Last edited: Dec 27, 2014
  2. Offline

    Theodossis

    You forgot @EventHandler
     
    VG.Developments likes this.
  3. Offline

    mine-care

    @TheYamsMan you dont need enable disable messages because bukkit does it for you, its aslso good not to spam console.
    Also instead of using a e vent called 3000 times in a second by players moving arround and therefor causing lag, prefer the use of a Repeating task. More info can be found @ BukkitScheduler programing tutorial.
     
  4. Offline

    TheYamsMan

    @mine-care That was a temporary solution while I figured some other stuff out. Thanks.
    Can you link it to me so I have the same thing and not some crappy one?
     
  5. Offline

    mine-care

  6. Offline

    TheYamsMan

    Got the scheduler, but throwing up this error every time I try to run the plugin.
    [16:27:51 INFO]: [DayCycle] Enabling DayCycle v1.0
    [16:27:51 ERROR]: Error occurred while enabling DayCycle v1.0 (Is it up to date?)
    java.lang.NullPointerException
    at me.yamerins.DC.DC$1.<init>(DC.java:25) ~[?:?]
    at me.yamerins.DC.DC.onEnable(DC.java:24) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) ~[craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:350) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:389) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugin(CraftServer.java:439) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.enablePlugins(CraftServer.java:375) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.reload(CraftServer.java:773) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.Bukkit.reload(Bukkit.java:279) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:175) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:683) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchServerCommand(CraftServer.java:670) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.aw(DedicatedServer.java:286) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:251) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]

    My code:
    Code:
    package me.yamerins.DC;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitScheduler;
    
    public class DC extends JavaPlugin{
       
        private DC plugin;
           
        @Override
        public void onDisable(){
        }
           
        @Override
        public void onEnable(){
            this.getLogger();
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
            World w = (World) plugin.getServer();
            Player p = w.getPlayers().get(1);
                @Override
                public void run() {
                    if(toggle){
                        if(w.getTime() >= 11000){
                            w.setTime(100);
                            p.sendMessage("Time has been reset.");
                        } else {
                            p.sendMessage("Time change failed.");
                        }
                    }
                }
            }, 0L, 20L);
        }
           
        boolean toggle = true;
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("alwaysday")){
                if(toggle){
                    toggle = false;
                    sender.sendMessage("Always day has been set to" + ChatColor.RED + " false.");
                    return true;   
                } else {
                    toggle= true;
                    sender.sendMessage("Always day has been set to" + ChatColor.GREEN + " true.");
                    return true;
                }
            }
           
            if(commandLabel.equalsIgnoreCase("adstate")){
                sender.sendMessage("Alwaysday is currently set to: " + ChatColor.BLUE + ChatColor.BOLD + toggle);
                return true;
            }
            return false;
        }
    }
    
    My commands and permissions are fine.
    Does anyone know why I might be getting the error or know what I need to change? I know it comes from my onEnable method where I put the scheduler, but I don't understand what the issue is.

    I have been working on this for a while now and still can't figure it out, no matter what I do I can never keep the plugin functional and start the scheduler when my plugin starts running either. Anybody?

    EDIT by Timtower: merged posts, please use the edit button instead.
     
    Last edited by a moderator: Dec 28, 2014
  7. Offline

    mythbusterma

  8. Offline

    timbragg12

    @TheYamsMan I don't think it matters (and it isn't the reason for your error) but I certainly found it quite odd that you had your onDisable() before your onEnable() XD
     
  9. Offline

    Gamesareme

    @TheYamsMan Try making another method, and call it dayCountDown, or something like that. Then put what you have in the onEnable() into this. (I mean the count down stuff). Then in the onEnable() call the function dayCountDown when you have done every thing else you need. Also I believe that you would be better off using the
    Code:
    scheduleAsyncRepeatingTask
    Than the sync one.

    @TheYamsMan Hang on, found the problem, but you should still do what I said above.
    Your error is because of this line.
    Code:
    World w = (World) plugin.getServer();
    In this line you are making the server it self into a world object, WHICH it is not.
    You need to do this instead.
    Code:
    plugin.getServer().getWorld("your world name here");
    Now hopefully you understand what you did wrong.

    EDIT by Timtower: merged posts, please use the edit button instead.
     
    Last edited by a moderator: Dec 28, 2014
  10. Offline

    mythbusterma

    @Gamesareme

    If you read the stack trace, that isn't the issue. Although that would still cause an issue.



    On what grounds do you base such an ignorant statement?
     
  11. Offline

    Gamesareme

  12. Offline

    mythbusterma

    @Gamesareme
    Even according to the thread you linked, he's accessing the API and doing a task that takes no time at all.

    More specifically, you could violate the thread safety of the server by setting the time from another thread, because the time is likely to change between those checks and modifying the value outside the thread isn't a good idea, albeit in this particular case neither of those are an issue, it's still not safe nor something you should be encouraging.
     
  13. Offline

    Gamesareme

    @mythbusterma Hang on, have I made a mistake? I though that what they said was that a task running and looping for a long time would crash the server, not a task that has a long delay.
     
  14. Offline

    mythbusterma

    @Gamesareme

    There doesn't seem to be any expensive operations in that snippet of code, nor does it contain any loops. It's simply a block of code that gets executed every tick.
     
    Gamesareme likes this.
  15. Offline

    Gamesareme

    @mythbusterma Ok, I will respect your statement, because you are probably right. But still...
     
  16. Offline

    mythbusterma

    @Gamesareme

    But still, thread safety is a very fickle concept to wrestle with. You generally want to avoid doing things asynchronously if you can, because unless you really know what you're doing, you really open up a can of worms as far as issues go.

    The Bukkit API was not designed to be used across multiple threads, and, as limiting as that is, you have to respect that if you want to write decent software.
     
  17. Offline

    TheYamsMan

    @Gamesareme I know this sounds nooby, but where would I put my toggle boolean? Does that go in my onEnable and onDisable methods?
     
  18. Offline

    Gamesareme

    @TheYamsMan I mean do something like this.
    Add this below you command function.
    Code:
    public void dayCountDown(){
    BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
            World w = (World) plugin.getServer().getWorld("Your world name");
            Player p = w.getPlayers().get(1);
                @Override
                public void run() {
                    if(toggle){
                        if(w.getTime() >= 11000){
                            w.setTime(100);
                            p.sendMessage("Time has been reset.");
                        } else {
                            p.sendMessage("Time change failed.");
                        }
                    }
                }
            }, 0L, 20L);
    }
    Then call this in your onEnable() like so.

    Code:
    public void onEnable(){
    dayCountDown();//Just put this line in YOUR onEnable()
    }
    You should have your toggle variable right below this line of code.

    Code:
     private DC plugin;
    So it will look something like this.
    Code:
     private DC plugin;
    private boolean toggle = true;
    Hopefully this makes sense. If you do not understand what I have done, then I would recommend you look into a few java, and Bukkit tutorials.
     
  19. Offline

    TheYamsMan

    @Gamesareme I understand perfectly, sorry. I should have before looking back at my code, but I had a brain fart I guess. I hate mornings...

    EDIT: I tried it, and I get this error:
    [21:23:51 ERROR]: Error occurred while enabling DayCycle v1.0 (Is it up to date?)
    java.lang.NullPointerException
    at me.yamerins.DC.DC$1.<init>(DC.java:19) ~[?:?]
    at me.yamerins.DC.DC.dayCountDown(DC.java:18) ~[?:?]
    at me.yamerins.DC.DC.onEnable(DC.java:42) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) ~[craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:350) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:389) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugin(CraftServer.java:439) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.enablePlugins(CraftServer.java:375) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.reload(CraftServer.java:773) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.Bukkit.reload(Bukkit.java:279) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:175) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:683) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchServerCommand(CraftServer.java:670) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.aw(DedicatedServer.java:286) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:251) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit-1.7.2-R0.3-20140208.212113-36.jar:git-Bukkit-1.7.2-R0.2-40-g6e27629-b3011jnks]

    It looks like the same error as before mostly if not fully, but again I don't understand what part of my onEnable is going wrong.

    Code right now:
    Code:
    package me.yamerins.DC;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitScheduler;
    
    public class DC extends JavaPlugin{
        public void dayCountDown(){
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
                    scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                    World w = (World) plugin.getServer().getWorld("Your world name");
                    Player p = w.getPlayers().get(1);
                        @Override
                        public void run() {
                            if(toggle){
                                if(w.getTime() >= 11000){
                                    w.setTime(100);
                                    p.sendMessage("Time has been reset.");
                                } else {
                                    p.sendMessage("Time change failed.");
                                }
                            }
                        }
                    }, 0L, 200L);
            }
      
        private DC plugin;
        boolean toggle = true;
      
        @Override
        public void onDisable(){
        }
          
        @Override
        public void onEnable(){
            this.getLogger();
            dayCountDown();
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("alwaysday")){
                if(toggle){
                    toggle = false;
                    sender.sendMessage("Always day has been set to" + ChatColor.RED + " false.");
                    return true;  
                } else {
                    toggle= true;
                    sender.sendMessage("Always day has been set to" + ChatColor.GREEN + " true.");
                    return true;
                }
            }
          
            if(commandLabel.equalsIgnoreCase("adstate")){
                sender.sendMessage("Alwaysday is currently set to: " + ChatColor.BLUE + ChatColor.BOLD + toggle);
                return true;
            }
            return false;
        }
    }
    
     
    Last edited: Dec 28, 2014
  20. Offline

    WarmakerT

    Code:
    package me.yamerins.DC;
    
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class DC extends JavaPlugin {
    
        boolean toggle = false;
    
        public void dayCountdown(){
            new BukkitRunnable() {
                public void run() {
                    if(toggle == true){
                       getServer().getWorlds().get(0).setTime(100);
                     }
                }
            }.runTaskTimer(this, 0L, 10900L);
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if (commandLabel.equalsIgnoreCase("alwaysday")){
                if (toggle = true)  {
                    toggle = false;
                    sender.sendMessage("Always day has been set to" + ChatColor.RED + " false.");
                } else {
                    toggle = true;
                    sender.sendMessage("Always day has been set to" + ChatColor.GREEN + " true.");
                }
                return true;
            }
    
            if (commandLabel.equalsIgnoreCase("adstate")) {
                sender.sendMessage("\"Alwaysday\" is currently set to: " + ChatColor.BLUE + ChatColor.BOLD + toggle);
                return true;
            }
            return false;
        }
    }
    There you go. Make sure you read it and check if it works.
    Right now, you haven't been reading the code. Your previous attempt didn't work because there isn't a world named Your world name. If you had read the code, you would have changed that to world or whatever.

    My attempt chooses the main world, so you don't fail again.
     
    Last edited: Dec 29, 2014
  21. Offline

    ColonelHedgehog

    I think everyone might be missing the fact that when the plugin is enabled, there are no worlds loaded. :p

    However, to change this, you can go to your plugin.yml and add:

    load: POSTWORLD
     
    WarmakerT likes this.
  22. Offline

    TheYamsMan

    @WarmakerT I understood that I needed to add the world name, but I wasn't sure what name to use since this is a plugin request. Thanks.

    EDIT: I tried the code.
    if(toggle == true) this.getServer().getWorlds().get(0).setTime(100); gives an error on this.getServer().
    It thinks it is a method and is undefined for bukkitRunnable. Quick fix is make a method for it, but I don't know if that is the fix needed. Anybody know?
     
    Last edited: Dec 29, 2014
  23. Offline

    Gamesareme

    @TheYamsManGo into your plugin folder, and find the name of your world. By defualt it will be called world. If it is leave it. Then go to your code, and find this line.
    Code:
    this.getServer().getWorlds().get(0).setTime(100);
    Change it to this.
    Code:
    this.getServer().getWorld("then the name of your world").setTime(100);
    Then the code should work.
    If not then post your current script and I will see what you are doing wrong.
     
  24. Offline

    WarmakerT

    Remove the this. . My bad.
     
  25. Offline

    TheYamsMan

    @WarmakerT Yeah, I wasn't sure about that...XD. Well, tha works now. Another plugin issue now, but that is other coding so im not worried about it.
     
    Last edited: Dec 29, 2014
Thread Status:
Not open for further replies.

Share This Page