Solved Repeating Task

Discussion in 'Plugin Development' started by ERROR372, Jul 26, 2013.

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

    ERROR372

    Alright, I'm a beginner with schedulers. I've read the tiny "tutorial" (if that's what you want to even call it), and I've tried to find something, anything that can help me.

    So basically, I need a repeating scheduler that checks every 10 seconds if a player is past certain x and z locations, and if so teleport them within the boundaries and tell them they can't go any further.

    I have my main class, and a class that I had previously used PlayerMoveEvent. I'm wanting to replace the PlayerMoveEvent with this scheduler. Thing is... I have no freaking clue how. All I have is this code:

    Code:java
    1. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    2. @Override
    3. public void run(){
    4. //check player locations
    5. }
    6. }, 200);


    Thing is, I haven't a clue *where* to put that... Do I put it in a method? If I do that, when do I call that method? Please help ._.




    Also, off-topic question regarding inventories... I've seen people do updateInventory() after messing with people's inventories (adding things, taking things away, etc). In my plugins, I do remove ItemStacks from player's inventories (like when the durability is low, they run out of the item, etc), but I've never had to do updateInventory() to make it actually be removed. So... why do people do updateInventory()?
     
  2. Offline

    blablubbabc

    Code:java
    1. @Override
    2. public void onEnable() {
    3.  
    4. getServer().getScheduler().runTaskTimer(this, new Runnable() {
    5.  
    6. @Override
    7. public void run() {
    8. // check all players:
    9. for (Player player : getServer().getOnlinePlayers()) {
    10. // if not in bounds:
    11. Location location = player.getLocation();
    12. double x = location.getX();
    13. double z = location.getZ();
    14.  
    15. if (x > xMax || x < xMin || z > zMax || z < zMin) {
    16. // to be sure..
    17. player.leaveVehicle();
    18.  
    19. // determine where the player shall go:
    20. World world = location.getWorld();
    21. int newX = (int) Math.min(xMax, Math.max(xMin, x));
    22. int newZ = (int) Math.min(zMax, Math.max(zMin, z));
    23. int newY = world.getHighestBlockYAt(newX, newZ);
    24.  
    25. // woosh
    26. player.teleport(new Location(world, newX, newY, newZ));
    27.  
    28. player.sendMessage("Don't go that far!");
    29. }
    30. }
    31. }
    32. }, 1L, 200L);
    33. }


    updateInventory basicly lets the client know about the current inventory items. Often the clients doesn't recieve the information about any changes automatically currently.
     
  3. Offline

    ERROR372

    blablubbabc

    It works perfectly. Thank you very much. Strange about the inventory thing that I've not had to use updateInventory(). Maybe I should put that in just in case =P

    This is solved now, but is there a way I can put this into another class? I like to keep my main class "clean", and a bukkit scheduler just isn't very clean =P
     
Thread Status:
Not open for further replies.

Share This Page