Solved UHC Episode Timer

Discussion in 'Plugin Development' started by LordVakar, Jan 28, 2014.

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

    LordVakar

    So if I was making a UHC plugin, how would I display a message every 20 minutes (configurable with config) saying "MARK _ MINUTES IN"?
    So like after 20 minutes, it would say MARK 20 MINUTES IN, and 20 minutes after that, it would say MARK 40 MINUTES IN.
    UHC stands for Ultra HardCore, btw.
     
  2. Offline

    Maurdekye

    Have a global timer;
    Code:java
    1. public int timer;

    And schedule a repeating task every minute that ticks the timer up by one. if it's minute count is a multiple of the separation time in your config file, print a global message;
    Code:java
    1. timer = 0;
    2. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    3. public void run() {
    4. timer++;
    5. if (timer%getConfig().getInt("broadcast_delay") == 0) {
    6. getServer().broadcastMessage("MARK " + timer + " MINUTES IN");
    7. }
    8. }
    9. }, 0, 1200);
     
  3. Offline

    LordVakar

    Maurdekye
    So I wrote some code, but it doesn't work for some reason:
    Code:java
    1. int ammoutIn = 0;
    2. UHC main;
    3.  
    4. public CmdTimerStart(UHC plugin) {
    5. }
    6.  
    7. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    8. if(command.getName().equals("timerstart")){
    9. Bukkit.getScheduler().scheduleSyncRepeatingTask((Plugin) main, new Runnable() {
    10. public void run() {
    11. ammoutIn = ammoutIn +20;
    12. Bukkit.broadcastMessage("MARK " + ammoutIn + " MINUTES IN!");
    13. }
    14. }, 0, 1200);
    15. return true;
    16. }
    17. return false;
    18. }


    I keep getting this stacktrace:
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'timerstart' in plugin UHC v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:196) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:542) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:932) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:203) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.validate(CraftScheduler.java:391) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:120) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.scheduleSyncRepeatingTask(CraftScheduler.java:116) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at me.LordVakar.UHC.Commands.CmdTimerStart.onCommand(CmdTimerStart.java:21) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        ... 13 more
     
  4. Offline

    Maurdekye

    LordVakar The variable 'main' isn't initialized. You have to assign it a value in your constructor;
    Code:java
    1. this.main = plugin;

    Also, you don't need to cast it to a plugin, the scheduler will accept it as-is.
     
  5. Offline

    LordVakar

    Maurdekye
    It broadcasted MARK 20 MINS IN as soon as I executed the command and adds 20 as soon as I use the command.
    How do I make it say MARK 0 MINS IN and then 20 mins after that add 20.
    EDIT: I thought I had it, and then I didn't :p
     
  6. Offline

    Maurdekye

    LordVakar What you have will broadcast the count every minute incorrectly, ex. it'll say MARK 20 MINS IN when you schedule it, then MARK 40 MINS IN 2 minutes after, etc. You should instead be adding 1 each time, and then checking if it's a multiple of 20. If so, only then do you display the message.
     
  7. Offline

    LordVakar

    Maurdekye

    I think I thought of a simpler way:
    Code:java
    1.  
    2. int ammountIn = 0;
    3. UHC main;
    4. boolean timerStarted = false;
    5.  
    6. public CmdTimerStart(UHC plugin) {
    7. }
    8. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    9. if(command.getName().equals("timerstart")){
    10. this.main = (UHC) UHC.pl;
    11. Bukkit.broadcastMessage("MARK " + ammountIn + " MINUTES IN!");
    12. Bukkit.getScheduler().scheduleSyncRepeatingTask((Plugin)main, new Runnable() {
    13. public void run() {
    14. if (timerStarted = true) {
    15. ammountIn = ammountIn +20;
    16. Bukkit.broadcastMessage("MARK " + ammountIn + " MINUTES IN!");
    17. }
    18. else if (timerStarted = false) {
    19. Bukkit.broadcastMessage("MARK " + ammountIn + " MINUTES IN!");
    20. timerStarted = true;
    21. }
    22. }
    23. },(60 * 20) * 20, (60 * 20) * 20);
    24. return true;
    25. }
    26. return false;
    27. }


    Do you think it works? I'll have to wait 20 mins to find out. And your timer kept executing every minute instead of every 20 minutes, so I changed the ticks a bit.
     
  8. Offline

    Maurdekye

    LordVakar No, it executed every minute because it added to the timer every minute. Then, when the timer reached 20, 20 minutes would have gone by, and the message would've been printed.
     
  9. Offline

    LordVakar

    Well when I used it, it printed 20 mins every min o_o
     
  10. Offline

    Maurdekye

    LordVakar You didn't use my method; you used your own, which was different. That one printed it every 20 minutes. Mine works fine.
     
  11. Offline

    LordVakar

    Maurdekye

    I got it to run every 20 mins
    Thanks for your help. (Just tested, waited 20 mins, and it worked)
     
  12. Offline

    mattt__

    Is this a plugin you have shared or would be willing to share? :)
     
  13. Offline

    LordVakar

    mattt__
    I might in the future
    The plugin that I was working on was meant for youtuber UHC's ;3
     
Thread Status:
Not open for further replies.

Share This Page