0

I'm writing a Minecraft plugin where I want to work a lot with schedulers and I always get the problem when 2 players carry out an activity at the same time where a scheduler is installed, then the animation of the player who started the activity first breaks off for him and the player who started it last starts it, how can I fix this?

Here is an example where this problem occurs in the code.

public class AutoNick
        implements Listener {
    Player player;

    @EventHandler
    public void handlePlayerNick(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        int taskID = CreateDataPlayer.getPlayerID(player.getUniqueId());
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK ||
                    event.getAction() == Action.RIGHT_CLICK_AIR) {
                if (event.getItem().getItemMeta().getDisplayName().equals(StringList.inv_player_i1)) {

                    taskID = Bukkit.getScheduler().scheduleSyncDelayedTask(LobbySystem.getInstance(), new                  Runnable() {
                        @Override
                        public void run() {
                            player.getInventory().setBoots(new LootBuilder(Material.GOLD_BOOTS, 1).createDisplayName("§6Aktiviere AutoNick...").build());
                            player.playSound(player.getLocation(), Sound.EXPLODE, 0.5F, 1.2F);
                            player.playEffect(player.getLocation(), Effect.EXPLOSION_LARGE, 255);
                        }
                    }, 20L);
                    taskID = Bukkit.getScheduler().scheduleSyncDelayedTask(LobbySystem.getInstance(), new Runnable() {
                        @Override
                        public void run() {
                            player.getInventory().setLeggings(new LootBuilder(Material.GOLD_LEGGINGS, 1).createDisplayName("§6Lade Datenbank...").build());
                            player.playSound(player.getLocation(), Sound.EXPLODE, 0.5F, 1.2F);
                            player.playEffect(player.getLocation(), Effect.EXPLOSION_LARGE, 255);
                        }
                    }, 40L);
                    taskID = Bukkit.getScheduler().scheduleSyncDelayedTask(LobbySystem.getInstance(), new Runnable() {
                        @Override
                        public void run() {
                            player.getInventory().setChestplate(new LootBuilder(Material.GOLD_CHESTPLATE, 1).createDisplayName("§6Lade Skin...").build());
                            player.playSound(player.getLocation(), Sound.EXPLODE, 0.5F, 1.2F);
                            player.playEffect(player.getLocation(), Effect.EXPLOSION_LARGE, 255);
                        }
                    }, 60L);
                    taskID = Bukkit.getScheduler().scheduleSyncDelayedTask(LobbySystem.getInstance(), new Runnable() {
                        @Override
                        public void run() {
                            player.getInventory().setHelmet(new LootBuilder(Material.GOLD_HELMET, 1).createDisplayName("§6Übertrage Namen...").build());
                            player.playSound(player.getLocation(), Sound.EXPLODE, 0.5F, 1.2F);
                            player.playEffect(player.getLocation(), Effect.EXPLOSION_LARGE, 255);
                        }
                    }, 80L);
                    int finalTaskID = taskID;
                    taskID = Bukkit.getScheduler().scheduleSyncDelayedTask(LobbySystem.getInstance(), new Runnable() {
                        @Override
                        public void run() {
                            player.playSound(player.getLocation(), Sound.LEVEL_UP, 0.5F, 1.2F);
                            player.playEffect(player.getLocation(), Effect.EXPLOSION_LARGE, 255);
                            player.getInventory().setHelmet(new LootBuilder(Material.SNOW_BALL, 1).createDisplayName("§fNo animation..").build());
                            player.getInventory().setChestplate(new LootBuilder(Material.SNOW_BALL, 1).createDisplayName("§fNo animation..").build());
                            player.getInventory().setLeggings(new LootBuilder(Material.SNOW_BALL, 1).createDisplayName("§fNo animation..").build());
                            player.getInventory().setBoots(new LootBuilder(Material.SNOW_BALL, 1).createDisplayName("§fNo animation..").build());
                            player.sendMessage(StringList.nicknames_prefix + "§cKonnte keine Namen/Skins aus Datenbank laden");
                        }
                    }, 100L);

                }
            }

    }
}

I gave the player a random ID in the database and call it up every time a scheduler is started and was hoping that the scheduler would only run for the player since it is tied to the player ID.

ALTF99
  • 1
  • 1
  • What are you using the `taskID` variable for, and why are you cancelling `finalTaskID` in the last task? – CoderMuffin May 19 '23 at 12:06
  • So my plan was to assign the scheduler to a specific ID the taskID which is set by the playerID. I would like to achieve that the scheduler only runs for the player and does not stop as soon as another player performs the same activity. And I stop the finalTaskID at the end eif to be on the safe side so that the scheduler cannot continue eif due to a bug. – ALTF99 May 19 '23 at 12:35
  • Have you tried it without cancelling `finalTaskID`? – CoderMuffin May 19 '23 at 13:03
  • No but i will try now – ALTF99 May 19 '23 at 13:41
  • 1
    So `#scheduleSyncDelayedTask` will only run one time, it's not a repeating task. For that reason, there's not much reason to cancel them after they've occured. I'd be curious to see further debugging information around the issue you're describing, particularly the animation example (since that's visible). – Rogue May 19 '23 at 13:55
  • Why don't do only one repeating task which each time it run, it do something else? As you are doing something at 40, 60, 80 etc ticks – Elikill58 May 22 '23 at 06:18
  • My problem is that if one player is running this animation and another player starts this animation while the other player is in the animation, the animation will be canceled for the player who started the animation first and the player who started it last has executed, then has the animation. – ALTF99 Jun 11 '23 at 21:11

0 Answers0