Solved Task Timer Problem

Discussion in 'Plugin Development' started by SuperboyDev, Oct 3, 2015.

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

    SuperboyDev

    Hi My Bukkit Task Timer doesn't seem to be working. I'm trying to make a plugin that teleports players when they join. But my task timer doesn't seem to work. I'm trying to make it so that it cancels itself when the timer ends. This is my code.
    Code:
    public void onPlayerJoin(PlayerJoinEvent event) {
            ItemStack is = new ItemStack(Material.ENCHANTED_BOOK);
            ItemMeta isM = is.getItemMeta();
            isM.addEnchant(Enchantment.LUCK, 1, true);
            is.setItemMeta(isM);
            Player player = event.getPlayer();
            ticks = 10;
            int tasks = Bukkit.getServer().getScheduler().runTaskTimer(this, new Runnable() {
                public void run() {
                        ticks--;
                        if (ticks == 0) {
                            Random random = new Random();
                            int x = random.nextInt(2000) - 1000;
                            int z = random.nextInt(2000) - 1000;
    
                            Location teleportLocation = new Location(player
                                    .getWorld(), x, (player.getWorld()
                                    .getHighestBlockYAt(0, 0) + 1), z);
                            player.teleport(teleportLocation);
                            player.getInventory().addItem(is);
                            Bukkit.getServer().getScheduler().cancelAllTasks();
                        }
                        Bukkit.broadcastMessage(ChatColor.GREEN + "Time Left: "
                                + ChatColor.WHITE + ticks);
                }
            }, 0L, 20L);
        }
    Also, after this timer ends, I want it to start another timer that goes on for ten minutes before pvp is enabled. Please help!
     
  2. Offline

    boysnnoco

    Make 2 seperate timers, 1 that goes for 10 ticks then teleports, 1 that goes for 10 minutes and takes away pvp
     
  3. Offline

    SuperboyDev

    @boysnnoco I have made the timers, but the problem lies on this line:
    Code:
    int tasks = Bukkit.getServer().getScheduler().runTaskTimer(this, new Runnable() {}
    I want my task to cancel itself. But it doesn't seem to be working.
     
  4. Offline

    teej107

    I wouldn't do that. You'll be messing with a lot of plugins that way. Look into BukkitRunnable. It'll have what you need.
     
  5. Offline

    SuperboyDev

    I implemented some of the stuff given there, and made this. But I still can't cancel it. I can't figure out whats wrong.
    Code:
        private BukkitTask task;
    task = Bukkit.getServer().getScheduler().runTaskTimer(this, new Runnable() {
    
                @Override
                public void run() {
                    if (Bukkit.getServer().getOnlinePlayers().size() == getConfig().getInt("playerstillgamestarts")) {
                        ticks--;
                        if (ticks == 0) {
                            Random random = new Random();
                            int x = random.nextInt(2000) - 1000;
                            int z = random.nextInt(2000) - 1000;
    
                            Location teleportLocation = new Location(player
                                    .getWorld(), x, (player.getWorld()
                                    .getHighestBlockYAt(0, 0) + 1), z);
                            player.teleport(teleportLocation);
                            player.getInventory().addItem(is);
                            Bukkit.getServer().getScheduler().cancelTask(task);
                        }
                        Bukkit.broadcastMessage(ChatColor.GREEN + "Time Left: "
                                + ChatColor.WHITE + ticks);
                    }
                }
             
            }, 0L, 20L);
    I can't cancel the task as there is a red line over it. It says to change task into an int, and when I do, my entire code gets a red line @teej107
     
  6. Offline

    teej107

  7. Offline

    SuperboyDev

    @teej107 is it meant to be a Runnable or a BukkitRunnable? could you give me the basic structure of a bukkit runnable? I hope that's not considered spoon feeding :p

    EDIT: Also, I still can't cancel this task...
    EDIT2: @teej107 But I still have another problem. I want to start another task when this task ends. How do I do that?

    @teej107 Thanks. I was able to cancel the task. :) But I want to start another timer when this ends. I still can't figure out how to do that. I'm trying to make pvp disabled for 10 minutes until the (second) timer ends.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 30, 2015
  8. Offline

    teej107

    Unless you are going to notify the players when PvP becomes enabled again, using a cooldown would be better than a timer.
     
  9. Offline

    boysnnoco

    ... I said make 2 SEPERATE tasks instead of ONE task that tries to cancel itself make TWO that get called X amount of time LATER.
     
Thread Status:
Not open for further replies.

Share This Page