Timer err

Discussion in 'Programming' started by Chemi, Jun 2, 2015.

Thread Status:
Not open for further replies.
  1. ok.. So I made a method that gets someone's inventory and makes a "drop" item effect
    (sets an item in the next vertical slot) And this method is supposed to be triggered ever time you press a special item in a gui. And the problem is, that if I spam the item clicks, the timer gets stuck and it spawns the item on the first slot and the timer doesn't work for me until I reload. and there is another problem
    (I don't know if the tmer type i'm using is ok for use of multipule players and I don't have anyone to test it with)
    sooo the code is:
    public static void dropBlock(final int slot, final InventoryView inv)
    {
    taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.plugin, new Runnable(){
    int num = 1;
    @Override
    public void run() {
    if(num == 1)
    {
    inv.setItem(slot, new ItemStack(Material.IRON_BLOCK));
    }
    if(num == 2)
    {
    inv.setItem(slot, new ItemStack(Material.AIR));
    inv.setItem(9 + slot, new ItemStack(Material.IRON_BLOCK));
    }
    if(num == 3)
    {
    inv.setItem(9 + slot, new ItemStack(Material.AIR));
    if(!inv.getItem(18 + slot).hasItemMeta())
    inv.setItem(18 + slot, new ItemStack(Material.IRON_BLOCK));
    }
    if(num == 4)
    {
    if(!inv.getItem(18 + slot).hasItemMeta())
    inv.setItem(18 + slot, new ItemStack(Material.AIR));
    inv.setItem(27 + slot, new ItemStack(Material.IRON_BLOCK));
    }
    if(num == 5)
    {
    inv.setItem(27 + slot, new ItemStack(Material.AIR));
    inv.setItem(36 + slot, new ItemStack(Material.IRON_BLOCK));
    }
    if(num == 6)
    {
    inv.setItem(36 + slot, new ItemStack(Material.AIR));
    inv.setItem(45 + slot, new ItemStack(Material.IRON_BLOCK));
    }
    if (num > 6)
    {
    inv.setItem(45 + slot, new ItemStack(Material.AIR));
    Bukkit.getScheduler().cancelTask(taskID);
    }
    num++;
    }
    }, 0, 2);
    }
     
  2. Try this:
    Code (Text):


    static int num = 1;

    public static void dropBlock(final int slot, final InventoryView inv)
    {
    taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.plugin, new Runnable(){
    @Override
    public void run() {
    if(num == 1)
    {
    inv.setItem(slot, new ItemStack(Material.IRON_BLOCK));
    num++;
    }
    if(num == 2)
    {
    inv.setItem(slot, new ItemStack(Material.AIR));
    inv.setItem(9 + slot, new ItemStack(Material.IRON_BLOCK));
    num++;
    }
    if(num == 3)
    {
    inv.setItem(9 + slot, new ItemStack(Material.AIR));
    if(!inv.getItem(18 + slot).hasItemMeta())
    inv.setItem(18 + slot, new ItemStack(Material.IRON_BLOCK));
    num++;
    }
    if(num == 4)
    {
    if(!inv.getItem(18 + slot).hasItemMeta())
    inv.setItem(18 + slot, new ItemStack(Material.AIR));
    inv.setItem(27 + slot, new ItemStack(Material.IRON_BLOCK));
    num++;
    }
    if(num == 5)
    {
    inv.setItem(27 + slot, new ItemStack(Material.AIR));
    inv.setItem(36 + slot, new ItemStack(Material.IRON_BLOCK));
    num++;
    }
    if(num == 6)
    {
    inv.setItem(36 + slot, new ItemStack(Material.AIR));
    inv.setItem(45 + slot, new ItemStack(Material.IRON_BLOCK));
    num++;
    }
    if (num > 6)
    {
    inv.setItem(45 + slot, new ItemStack(Material.AIR));
    Bukkit.getScheduler().cancelTask(taskID);
    num = 1;  // To return num 1
    }
    }
    }, 0, 2);
    }
     
  3. I tried just changing the timer type to BukkitRunnable and used runTaskTimer
    and it works perfectly now. Ty!
     
Thread Status:
Not open for further replies.