Server lag when trying to remove armorstand entities

Discussion in 'Spigot Plugin Help' started by HeroArmy, Mar 25, 2020.

Thread Status:
Not open for further replies.
  1. My problem is that my server kind of lags (players aren't moving anymore and you cannot open chests but tps are still at 20.0 and commands are still working), when I remove armorstands. When I enter the entity list command on paper, I can see that the armorstands get removed. Could anyone help me with my problem?

    Here's the code in the PlayerJoinListener, where the armorstand is set:
    Code (Java):
        public static boolean cancelJoin = false;
        public static ArrayList<String> alivePlayers = new ArrayList<>();
        private static ArrayList<ArmorStand> armorstands = new ArrayList<>();
        public static ArmorStand armorStand;

        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player joinPlayer = event.getPlayer();
            alivePlayers.add(joinPlayer.getName());
            joinPlayer.setStatistic(Statistic.PLAYER_KILLS, 0);
            event.setJoinMessage(Main.getPrefix() + joinPlayer.getName() + " hat das Spiel betreten.");
            int i = 0;
            for (String players : alivePlayers) {
                Objects.requireNonNull(Bukkit.getPlayer(players)).teleport(ShuffleSpawnLocations.getSpawnPos().get(i));
                i++;
            }
            armorStand = (ArmorStand) joinPlayer.getWorld().spawnEntity(joinPlayer.getLocation(), EntityType.ARMOR_STAND);
            armorStand.addPassenger(joinPlayer);
            armorstands.add(armorStand);
            armorStand.setVisible(false);
            armorStand.setGravity(false);
            armorStand.setAI(false);
            armorStand.setSilent(true);
            armorStand.setBasePlate(false);
        }
    Here's the code where I remove the armorstand:
    Code (Java):
                    switch (countdown) {
                        case 30:
                        case 20:
                        case 10:
                        case 5:
                        case 4:
                        case 3:
                        case 2:
                            Bukkit.getServer().broadcastMessage(Main.getPrefix() + "Das Spiel startet in " + countdown + " Sekunden.");
                            for (Player players : Bukkit.getServer().getOnlinePlayers()) {
                                players.playSound(players.getLocation(), Sound.UI_BUTTON_CLICK, 1.0F, 2.0F);
                            }
                            break;
                        case 1:
                            Bukkit.getServer().broadcastMessage(Main.getPrefix() + "Das Spiel startet in einer Sekunde.");
                            for (Player players : Bukkit.getServer().getOnlinePlayers()) {
                                players.playSound(players.getLocation(), Sound.UI_BUTTON_CLICK, 1.0F, 2.0F);
                            }
                            break;
                        case 0:
                            for (ArmorStand armorStands : PlayerJoinListener.getArmorstands()) {
                                armorStands.getPassengers().clear();
                                armorStands.remove();
                            }
                            Bukkit.getServer().broadcastMessage(Main.getPrefix() + "Das Spiel beginnt!");
                            PlayerJoinListener.cancelJoin = true;
                            Bukkit.getServer().getScheduler().runTaskLater(Main.getPlugin(), () -> {
                                EntityDamageListener.cancelDamage = false;
                                Bukkit.getServer().broadcastMessage(Main.getPrefix() + "Die Schutzphase ist nun vorbei.");
                            }, 400L);
                            taskId3 = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(), () -> {
                                if (PlayerJoinListener.alivePlayers.size() == 1) {
                                    Bukkit.broadcastMessage(Main.getPrefix() + PlayerJoinListener.alivePlayers.get(0) + " hat das Spiel gewonnen!");

                                    Bukkit.getServer().getScheduler().cancelTask(taskId3);
                                    Bukkit.getServer().getScheduler().runTaskLater(Main.getPlugin(), () -> {
                                        for (Player players : Bukkit.getServer().getOnlinePlayers()) {
                                            players.kickPlayer(Main.getPrefix() + "Der Server wurde gestoppt.");
                                        }
                                        Bukkit.getServer().shutdown();
                                    }, 200L);
                                }
                            }, 0L, 60L);
                            Bukkit.getServer().getScheduler().cancelTask(taskId2);
                            return;
                        default:
                            break;
                    }
     
  2. Alright, I've solved my problem! The problem was, that I had an EntityDismountListener Class and I didn't check if the entity is alive.
     
Thread Status:
Not open for further replies.