SyncRepeatingTask help

Discussion in 'Plugin Development' started by acer5999, Oct 6, 2014.

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

    acer5999

    Hi,

    So essentially, I'm trying I have made a particle trail like thing, and it works fine and dandy, but I'm using a SyncRepeatingTask, which I have seen is cannot be cancelled. What I want to happen is on click of item X (Already implemented), it checks if the player has the particles on (Via ArrayList/HashMap) and if so disables them, otherwise it enables them and keeps them going for until item X is clicked. Just in case my explanation doesn't make sense, here is a code snippet
    Edit: Them being the particles
    Code:java
    1. ArrayList<String> rp = new ArrayList<>();
    2. @EventHandler
    3. public void redClick(PlayerInteractEvent event) {
    4. Player p = event.getPlayer();
    5. String pName = p.getName();
    6. if (event.getAction() == Action.RIGHT_CLICK_AIR && p.getItemInHand().getType().equals(Material.REDSTONE)){
    7. if(rp.contains(pName)) {
    8. event.setCancelled(true);
    9. p.sendMessage(ChatColor.DARK_AQUA + "Perks" + ChatColor.GRAY + "" + ChatColor.BOLD + "> " + ChatColor.GOLD + "Force Field disabled!");
    10. }
    11. else {
    12. boolean b = true;
    13. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    14. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    15. @Override
    16. public void run() {
    17. {
    18. if(b == true) {
    19. rp.add(pName);
    20. boolean b = false;
    21. }
    22. createField(p);
    23. }
    24. }
    25. },0L, 5L);
    26. }
    27. }
    28. }

    Sorry if I hurt your eyes with the code, it's not properly formatted atm
    Cheers
     
  2. acer5999 The method returns an int of the task ID, which can be used to cancel the task - this is all covered in the JavaDocs page for scheduler.
     
  3. Offline

    fireblast709

    BukkitRunnables~
     
    AdamQpzm likes this.
  4. Offline

    acer5999

    Yes, but how do I get said taskId? There doesn't seem to be a method to return it.

    Also, would it better more efficient/better idea to save the players UUID instead of name?
     
  5. acer5999 The method does return it. You're not storing it using the code you've shown, though. And as for the efficiency of UUIDs, I'm not exactly sure. However, when using player names, bare in mind that a player name is not guaranteed to always refer to the same player once they log off. If that's an issue for you, use their UUID instead.
     
  6. Offline

    _Filip

    Careful to only have one bukkitrunnable though.
    You don't want to have one being jealous. I heard that those tend to crash the server
     
  7. Offline

    acer5999

    Solved! I derped really hard and used Runnables when I could have just used a move event with some simple logic and an arraylist, For anyone looking for the solution, Here is the code:
    Code:java
    1. ArrayList<UUID> playerID = new ArrayList<>();
    2. @EventHandler
    3. public void redClick(PlayerInteractEvent event) {
    4. Player p = event.getPlayer();
    5. UUID pID = p.getUniqueId();
    6. if (event.getAction() == Action.RIGHT_CLICK_AIR && p.getItemInHand().getType().equals(Material.REDSTONE)){
    7. {
    8. if(playerID.contains(pID)) {
    9. p.sendMessage(ChatColor.DARK_AQUA + "Perks" + ChatColor.GRAY + "" + ChatColor.BOLD + "> " + ChatColor.GOLD + "Particles have been removed!");
    10. playerID.remove(pID);
    11. }
    12. else {
    13. p.sendMessage(ChatColor.DARK_AQUA + "Perks" + ChatColor.GRAY + "" + ChatColor.BOLD + "> " + ChatColor.GOLD + "Enjoy your Particles!");
    14. playerID.add(pID);
    15. }
    16. }
    17. }
    18. }
    19. @EventHandler
    20. public void moveEvent(PlayerMoveEvent e) {
    21. Player p = e.getPlayer();
    22. UUID pID = p.getUniqueId();
    23. if(e.getFrom().getX() == e.getTo().getX() && e.getFrom().getY()
    24. == e.getTo().getY() && e.getFrom().getZ() == e.getTo().getZ()) {
    25. return;
    26. }
    27. else {
    28. if(playerID.contains(pID)) {
    29. createTrail(p);
    30. //createTrail is a method I declared earlier in the class
    31. }
    32. }
    33. }
     
Thread Status:
Not open for further replies.

Share This Page