Scheduled Task Issue

Discussion in 'Plugin Development' started by Fl1pzta, Aug 18, 2012.

  1. Offline

    Fl1pzta

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I'm having an issue where I have a ScheduledTask within a method, I've used scheduled tasks before so I'm pretty used to them, I just can't seem to get this one to work properly.

    The following is a part of an if statement within a PlayerInteractEvent. The statement is valid because the for loop above it works perfectly fine. I will say this though, I'm not sure exactly what final does to a variable, but the scheduled task wanted player to be final so I did so. I'm not sure if that could cause an issue or not, although I'm pretty sure it wouldn't because I've tested just broadcasting a message to the server.

    Here it is...
    Code:
    else if(e.getMaterial()== Material.REDSTONE_TORCH_OFF){
                    long time = Bukkit.getServer().getWorld("world").getTime();
                    final Player player = e.getPlayer();
                    if(time <= 20000){
                        e.getPlayer().sendMessage(ChatColor.YELLOW + "You only need this when affected by blindness.");
                    }else{//night
                        if (e.getPlayer().getLevel()==0){
                            e.getPlayer().sendMessage(ChatColor.RED + " I should probably get some batteries...");
                        }else{
                            //Get location where redstone torch off is, swap with ON, start scheduledTask, store in Hashmap for other uses.
    //LOOP WORKS FINE
                            for (ItemStack item : player.getInventory()) {
                                if(item.getTypeId() == 75) {
                                    item.setTypeId(76);
                                }
                            }
    //HERE IS WHERE THE ISSUE IS
                            int id = this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
                               
                                public void run() {
                                   
                                      player.setLevel(player.getLevel()-1);;
                                      }}, 4L, 1000L);
                            threads.put(e.getPlayer().getName(), id);
                        }
                    }
                }
    If you guys need any more information I'd be happy to do so. Thanks so much!

    Also, my ItemStack loop is only grabbing items from a player's hot-bar, and not from their full inventory if anyone knows a quick fix for that, thanks in advance!
  2. Online

    chaseoes BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
  3. Offline

    Fl1pzta

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Sorry about that. Here
    Code:
    else{
                            //Get location where redstone torch off is, swap with ON, start scheduledTask, store in Hashmap for other uses.
                            for (ItemStack item : player.getInventory()) {
                                if(item.getTypeId() == 75) {
                                    item.setTypeId(76);
                                }
                            }
                            int id = this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
                                public void run() {
                                    Bukkit.getServer().broadcastMessage("THIS IS A TEST");
                                    player.setLevel(player.getLevel()-1);;
                                }}, 4L, 1000L);
                            threads.put(e.getPlayer().getName(), id);
                        }

    This post has been edited 1 time. It was last edited by Fl1pzta Aug 18, 2012.
  4. Offline

    Fl1pzta

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Anybody?
  5. Offline

    Courier

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    You should use sync task, not async task.

    The modifier "final" tells Java that the value of a variable cannot change once it is set.
    Code:java
    1. //now "num5" ALWAYS represents 5...
    2. //forever...
    3. //you cannot set it again
    4. final int num5 = 5;
    5.  
    6. //in this case, the "variable" is a pointer to an object
    7. final Player creator = Bukkit.getPlayerExact("Notch");
    8. //now "creator" will always refer to the player object
    9. //you just got
    10. //aka it is always Notch
    11. //you cannot reassign the variable
    12. player = Bukkit.getPlayerExact("jeb_"); //THIS WILL NOT COMPILE
    13. //but you can still call the methods and change fields
    14. //of the object, since that doesn't reassign to your variable
    15. creator.setGameMode(GameMode.CREATIVE); //this is fine
    You need to declare variables as final to access them from an anonymous inner class. When an anonymous inner class's object is instantiated, all variables which are declared "final" are copied to it.
    Code:java
    1. new Runnable()
    2. {
    3. //THIS IS AN ANONYMOUS INNER CLASS
    4. }

    This post has been edited 1 time. It was last edited by Courier Aug 18, 2012.
  6. Offline

    Fl1pzta

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Thanks, I understand that now, but how about switching async to sync. Does that explain why it doesn't run at all?
  7. Offline

    Courier

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    It doesn't work because you are using id before it exists... I would do something like this.
    Code:java
    1. final MutableInt id = new MutableInt(-1);
    2. id.value = Bukkit.scheduleSyncDelayedTask(new Runnable()
    3. {
    4. Bukkit.broadcastMessage("the scheduled ID is: " + id.value);
    5. });
    6.  
    7. ...
    8.  
    9. class MutableInt
    10. {
    11. public int value;
    12. public MutableInt(int value) {this.value = value;}
    13. }
  8. Offline

    Fl1pzta

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I did all of the following and still it changed nothing.
    Here it is now...
    Code:
    final MutableInt id = new MutableInt(-1);
    id.value = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                                public void run() {
                                    Bukkit.getServer().broadcastMessage("THIS IS A TEST");
                                    e.getPlayer().setLevel(player.getLevel()-1);;
                                }},60L);
                            //threads.put(e.getPlayer().getName(), id);
    ...and the class
    Code:
    class MutableInt
    {
    public int value;
    public MutableInt(int value) {this.value = value;}
    }
    
  9. Offline

    Fl1pzta

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Anybody?

Share This Page