Solved how would i make a timer

Discussion in 'Plugin Development' started by ahuby09, Dec 8, 2012.

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

    ahuby09

    im tryingto make a timer then when the timers up the player gets a diamond heres my code so far
    (all the code for my plugin)
    Code:
    package me.blueninjn.TpD;
     
    import java.util.Timer;
     
    import net.minecraft.server.PlayerAbilities;
    import net.minecraft.server.World;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Sign;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.block.Sign;
    public class TpD extends JavaPlugin{
    public void onEnable() {
    PluginInfo("Enabled");
    }
     
    public void onDisable() {
    PluginInfo("Disabled");
    }
     
    public void PluginInfo(String message){
    String V ="0.1";
    System.out.print("TpD" + V + message);
    }
     
     
     
     
     
     
     
     
     
     
     
    public boolean onCommand(CommandSender sender, Command command,
    String Commandlabel, String[] args) {
     
     
     
     
     
     
     
     
     
     
    if(Commandlabel.equalsIgnoreCase("TpD")){
    if( args.length == 1){
    Player Player = (Player) sender;
    Player targetplayer = getServer().getPlayer(args [0]);
    if (Player.getItemInHand().getType() == Material.DIAMOND){
    Player.teleport(targetplayer);
    Player.getInventory().remove(Material.DIAMOND);
    }
     
     
    else if (args.length == 0 ){
    Player.sendMessage("Please type the players name in ") ;
    }else{
    Player.sendMessage("Please hold a diamond in your hand");
    }
     
    }
     
    }
    return true;
    }
     
    }
     
    
     
  2. Offline

    lenis0012

    put this in onEnable():
    Code:
    timer();
    
    this repeats once a min:
    Code:
        public void timer()
        {
            this.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable()
            {
                public void run()
                {
                    for(Player player : Bukkit.getServer().getOnlinePlayers())
                    {
                        player.getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
                    }
                }
            }
            , 1200, 1200);
        }
    
    the 1200, 1200 is the delay. 20 is 1 sec, so 20, 20 is once a sec (20 * 60 = 1200 = 1 min)
     
  3. Offline

    ahuby09

    thanks dude ur amazing also can u explain the code as i dont wont what i dont know if you get twhat i mean i wana learn
     
  4. Offline

    lenis0012

    you said you wanted a code to give all players a diamond in a timer
    thats the code
     
  5. Offline

    ahuby09

    ok but i sitll dont understand the timing cause i put it 20,30 and i dont get a diamond every second
     
  6. Offline

    lenis0012

    20, 20 is 1 sec
     
  7. Offline

    ahuby09

    still aint working join my server 94.14.164.81
     
  8. Offline

    lenis0012

    and you msut have the timer(); in the on Enable

    example main class:
    Code:
    package me.lenis0012.test.Main
     
    import org.bukkit.Bukkit;
    import org.bukkit.JavaPlugin;
     
    public class Main extends JavaPlugin
    {
        @Override
        public void onEnable()
        {
            timer();
        }
     
        public void timer()
        {
            this.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable()
            {
                public void run()
                {
                    for(Player player : Bukkit.getServer().getOnlinePlayers())
                    {
                        player.getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
                    }
                }
            }
            , 20, 20);
        }
    }
    
     
  9. Offline

    ahuby09

    ahhh i see thanks i kinda get the code

    also so 12000 be 10 mins

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  10. Offline

    lenis0012

    yes. 12000, 12000 is 10 min
     
  11. Offline

    ahuby09

    thanks for your help mate your name is going to be in the credits for my plugin when i release it :D
     
  12. Offline

    lenis0012

    Awesone :)
     
  13. Offline

    ahuby09

    hey dude do u want to be a alpha tester as well
     
  14. Offline

    lenis0012

    sure, pm me if you want to tell me things, or send downloads
     
  15. Offline

    unforgiven5232

    The word plugin in this statement gives me an error :/
    this.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable()
     
  16. Offline

    fireblast709

    ahuby09 lenis0012 Don't use an Async task combined with bukkit api methods... Put this in your onEnable()
    Code:java
    1. new BukkitRunnable()
    2. {
    3. @Override
    4. public void run()
    5. {
    6. for(Player player : Bukkit.getOnlinePlayers())
    7. {
    8. player.getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
    9. }
    10. }
    11. }.runTaskTimer(this, <initial delay*>, <delay between runs**>);

    Whereas:
    • *This is the delay from when it starts. So if you put it on 20L, it will wait 1 second before calling it the first time
    • **This is the delay between runs. So if you put it on 20L, it will run each second (from the first run)
    20L, aka 20 server ticks, equals 1 second.
    unforgiven5232 because scheduleAsyncRepeatingTask is depreciated. Use the method I posted a few lines above (Note that this is a sync method, which runs on the main thread. This should be used if you use methods from the Bukkit API that are not thread-safe (which is most of the methods)
     
  17. Offline

    lenis0012

    if its in your Main Class use this instead of plugin

    Thats what i found out, i crashed my own server for using api methods in a heavy Async task :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  18. Offline

    unforgiven5232

    I changed the word "plugin" to "this" and it fixed, So far no crashes but it is a simple task so it should'nt be to much of an issue
     
Thread Status:
Not open for further replies.

Share This Page