Repeat a task until a command is run.

Discussion in 'Plugin Development' started by nicholasntp, Mar 24, 2012.

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

    nicholasntp

    What I want is like a toggle command, but Repeating a playEffect. With a slight delay. Essentially what I'm trying to do is once I run /smoke, I want smoke to follow me around. I know how to do the toggling, with Hashmaps, but I need help with the delay.

    Every few ticks, when command is toggled on:
    Code:java
    1.  
    2. Location loc = player.getLocation();
    3. World world = player.getWorld();
    4. world.playEffect(loc,Effect.SMOKE,4);
    5.  


    Can anyone help?

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

    Eegabooga

    This is a rough explanation of how I would do it, but something like this should get the job done.
    Have a variable that holds whether a player should have smoke. So for example:
    Code:
    ArrayList<Player> isSmokeEnabled = new ArrayList<Player>();
    Then, when a player calls the /smoke command, add their name to the list. Every tick in the game, call the smoke effect on that player.
     
  3. Offline

    nicholasntp

    Thats exactly what I need to know how to do.
     
  4. Offline

    Technius

    Make a list that contains the players with smoke enable:
    Code:java
    1. ArrayList<Player> smoke = new Arraylist<Player>();

    Make a runnable that makes smoke follow them
    Code:java
    1. private class SmokeRunnable implements Runnable
    2. {
    3. public void run()
    4. {
    5. for(Player p:getServer().getOnlinePlayers())
    6. {
    7. if(smoke.contains(p))p.getWorld().playEffect(p.getLocation(), Effect.SMOKE, 0);
    8. }
    9. }
    10. }

    Schedule it on your on enable
    Code:java
    1. getServer().getScheduler().scheduleSyncRepeatingTask(this, new SmokeRunnable(), 0, 20);//The last argument is ticks, 20 ticks = 1 second

    Toggling the list
    Code:java
    1. if(smoke.contains(player))smoke.remove(player);
    2. else smoke.add(player);
     
    nicholasntp likes this.
  5. Offline

    nicholasntp

    Thank you so much guys!!!!
     
  6. well... you can make it a bit faster. Why don't you just go through the list?
     
  7. Offline

    nicholasntp

    I changed it to my needs, also with getting the direction of the player for direction of smoke. But how do u test if the player is standing still?

    Maybe a hashmap

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

    Technius

    The player could be offline, and it would make the smoke look weird.

    Have a HashMap with new locations, and have a HashMap with old locations. If the new location is different from the old location, play the effect.

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

    nicholasntp

    Can u tell me how? I dont get how you would.
     
  10. Offline

    nicholasntp

    Anybody???
     
  11. Offline

    Technius

    Code:java
    1. HashMap<Player, Location> oldloc = new HashMap<Player, Location>();
    2. HashMap<Player, Location> newloc = new HashMap<Player, Location>();
    3.  
    4. @EventHandler(priority = EventPriority.MONITOR)
    5. public void onPlayerMove(PlayerMoveEvent event)
    6. {
    7. if(event.isCancelled())return;
    8. //whatever you need to do here...
    9. //....
    10. newloc.put(event.getPlayer(), event.getPlayer().getLocation());
    11. if(oldloc.containsKey(player))
    12. {
    13. Location o = oldloc.get(player);
    14. Location n = newlod.get(player);
    15. if(o.getX() == n.getX() && o.getY() == n.getY() && o.getZ() == n.getZ())
    16. {
    17. //Use the code here
    18. //.........
    19.  
    20. //This is optional, but if you want to check if the player's is looking in the same place, move the above here
    21. if(o.getYaw() == n.getYaw() && o.getPitch() == n.getPitch())
    22. {
    23.  
    24. }
    25. }
    26. }
    27. }
     
Thread Status:
Not open for further replies.

Share This Page