diff --git a/src/main/java/de/fanta/challenges/challenges/ChallengeEvents/ForceItemChallengeEvent.java b/src/main/java/de/fanta/challenges/challenges/ChallengeEvents/ForceItemChallengeEvent.java index 8cadd33..70b5958 100644 --- a/src/main/java/de/fanta/challenges/challenges/ChallengeEvents/ForceItemChallengeEvent.java +++ b/src/main/java/de/fanta/challenges/challenges/ChallengeEvents/ForceItemChallengeEvent.java @@ -25,7 +25,6 @@ import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; -import org.bukkit.entity.Item; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -82,11 +81,10 @@ private static final ArrayList materials = new ArrayList<>(); - private static final HashMap entityList = new HashMap<>(); - private static final HashMap itemByPlayerList = new HashMap<>(); private static final HashMap> playerMaterials = new HashMap<>(); + private static final ArrayList dismountAllowList = new ArrayList<>(); private static final Map bossBarMap = new HashMap<>(); @@ -148,7 +146,7 @@ Player player = e.getPlayer(); if (e.getNewGameMode() == GameMode.SPECTATOR) { removeArmorStand(player); - } else { + } else if (!plugin.getVanish().isVanish(player)) { addArmorStand(player); } } @@ -160,7 +158,7 @@ Player player = e.getPlayer(); if (e.isVanishing()) { removeArmorStand(player); - } else { + } else if (player.getGameMode() != GameMode.SPECTATOR) { if (plugin.getScoreManager().getScores().containsKey(new ChallengePlayer(player.getUniqueId()))) { plugin.getScoreManager().join(new ChallengePlayer(player.getUniqueId())); } @@ -194,6 +192,7 @@ plugin.getScoreManager().join(new ChallengePlayer(player.getUniqueId())); addArmorStand(player); + updateBossBar(); if (itemByPlayerList.get(player.getUniqueId()) == null) { player.getInventory().setItem(8, skipItem); @@ -431,14 +430,13 @@ @EventHandler public void onClick(PlayerInteractEvent e) { if (isRunning()) { - if (e.getAction() != Action.RIGHT_CLICK_AIR) { - return; - } - Player p = e.getPlayer(); - ItemStack stack = p.getInventory().getItemInMainHand(); - ItemMeta meta = stack.getItemMeta(); - if (meta != null && meta.getPersistentDataContainer().has(skipKey)) { - new AcceptGUI(e.getPlayer()).open(); + if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) { + Player p = e.getPlayer(); + ItemStack stack = p.getInventory().getItemInMainHand(); + ItemMeta meta = stack.getItemMeta(); + if (meta != null && meta.getPersistentDataContainer().has(skipKey)) { + new AcceptGUI(e.getPlayer()).open(); + } } } @@ -447,9 +445,10 @@ @EventHandler public void onDismountEntity(EntityDismountEvent e) { if (isRunning()) { - Entity dismountedEntity = e.getEntity(); - if (entityListContainsEntity(dismountedEntity)) { - e.setCancelled(true); + if (e.getDismounted() instanceof Player player) { + if (!dismountAllowList.contains(player)) { + e.setCancelled(true); + } } } } @@ -468,16 +467,12 @@ return; } - if (plugin.getVanish().isVanish(player)) { - return; - } - Location location = player.getLocation(); Block block = location.getBlock(); if (block.getType() == Material.NETHER_PORTAL || block.getType() == Material.END_PORTAL || block.getType() == Material.END_GATEWAY) { removeArmorStand(player); } else { - if (!entityListContainsUUID(player.getUniqueId())) { + if (player.getPassengers().isEmpty()) { addArmorStand(player); } } @@ -486,7 +481,6 @@ for (Entity entity : player.getPassengers()) { entity.setRotation(location.getYaw(), location.getPitch()); } - if (location.getPitch() < -50) { for (Entity entity : player.getPassengers()) { player.hideEntity(plugin, entity); @@ -509,22 +503,17 @@ stand.setCollidable(false); stand.setPersistent(false); stand.setRemoveWhenFarAway(true); - entityListRemove(player.getUniqueId()); - entityListAdd(player.getUniqueId(), stand); player.addPassenger(stand); } } public static void updateArmorStand(Player player, Material material) { - if (entityListContainsUUID(player.getUniqueId())) { - ArmorStand stand = (ArmorStand) entityListGetEntity(player.getUniqueId()); - stand.getEquipment().setHelmet(new ItemStack(material)); - - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> { - stand.setInvisible(false); - stand.setInvisible(true); - }, 5L); - + if (!player.getPassengers().isEmpty()) { + for (Entity entity : player.getPassengers()) { + if (entity instanceof ArmorStand stand) { + stand.getEquipment().setHelmet(new ItemStack(material)); + } + } updateBossBar(); } } @@ -572,48 +561,34 @@ } } - public static void updateBossBar() { + private static void updateBossBar() { ScoreManager scoreManager = plugin.getScoreManager(); - for (Player player : plugin.getVanish().getPlayerListWithoutVanishPlayers()) { - Material playerItem = itemByPlayerList.get(player.getUniqueId()); + for (Player p : plugin.getVanish().getPlayerListWithoutVanishPlayers()) { + Material playerItem = itemByPlayerList.get(p.getUniqueId()); if (playerItem == null) { return; } - String bossBarTitle = ChatUtil.GREEN + ">> " + ChatUtil.BLUE + playerItem.toString().replace("_", " ") + ChatUtil.GREEN + " <<" + " | " + ChatUtil.GREEN + "Position: " + ChatUtil.BLUE + (scoreManager.getPosition(new ChallengePlayer(player.getUniqueId()))); - BossBar bossBar = bossBarMap.computeIfAbsent(player.getUniqueId(), bossbarPlayer -> { + String bossBarTitle = ChatUtil.GREEN + ">> " + ChatUtil.BLUE + playerItem.toString().replace("_", " ") + ChatUtil.GREEN + " <<" + " | " + ChatUtil.GREEN + "Position: " + ChatUtil.BLUE + (scoreManager.getPosition(new ChallengePlayer(p.getUniqueId()))); + BossBar bossBar = bossBarMap.computeIfAbsent(p.getUniqueId(), player -> { BossBar newBossBar = Bukkit.createBossBar(bossBarTitle, BarColor.GREEN, BarStyle.SOLID); newBossBar.setVisible(true); - newBossBar.addPlayer(player); + newBossBar.addPlayer(p); return newBossBar; }); + if (!bossBar.getPlayers().contains(p)) { + bossBar.addPlayer(p); + } bossBar.setTitle(bossBarTitle); } } - public void entityListAdd(UUID uuid, Entity entity) { - entityList.put(uuid, entity); - } - - public void entityListRemove(UUID uuid) { - entityList.remove(uuid); - } - - public static boolean entityListContainsUUID(UUID uuid) { - return entityList.containsKey(uuid); - } - - public boolean entityListContainsEntity(Entity entity) { - return entityList.containsValue(entity); - } - - public static Entity entityListGetEntity(UUID uuid) { - return entityList.get(uuid); - } - public void removeArmorStand(Player player) { - if (entityListContainsUUID(player.getUniqueId())) { - entityListGetEntity(player.getUniqueId()).remove(); - entityListRemove(player.getUniqueId()); + if (!player.getPassengers().isEmpty()) { + dismountAllowList.add(player); + for (Entity entity : player.getPassengers()) { + entity.remove(); + } + dismountAllowList.remove(player); } } @@ -629,7 +604,6 @@ } } }, 1L); - } public ItemStack createSkipItem() {