From 12e803cde02c67ba208bef1f8179b28d44f03d19 Mon Sep 17 00:00:00 2001 From: EinfachBeez Date: Fri, 24 May 2024 03:41:04 +0200 Subject: [PATCH] fix: unmodified health bar that is now only controlled by minecraft --- mongo-connector/pom.xml | 1 - .../setting/HealthDisplaySetting.java | 64 +++---------------- 2 files changed, 9 insertions(+), 56 deletions(-) diff --git a/mongo-connector/pom.xml b/mongo-connector/pom.xml index 714211026..8f97c7b90 100644 --- a/mongo-connector/pom.xml +++ b/mongo-connector/pom.xml @@ -127,7 +127,6 @@ - org.apache.maven.plugins maven-resources-plugin diff --git a/plugin/src/main/java/net/codingarea/challenges/plugin/challenges/implementation/setting/HealthDisplaySetting.java b/plugin/src/main/java/net/codingarea/challenges/plugin/challenges/implementation/setting/HealthDisplaySetting.java index 2957bfc0e..8be845fba 100644 --- a/plugin/src/main/java/net/codingarea/challenges/plugin/challenges/implementation/setting/HealthDisplaySetting.java +++ b/plugin/src/main/java/net/codingarea/challenges/plugin/challenges/implementation/setting/HealthDisplaySetting.java @@ -1,25 +1,25 @@ package net.codingarea.challenges.plugin.challenges.implementation.setting; -import net.anweisen.utilities.bukkit.utils.misc.BukkitReflectionUtils; +import javax.annotation.Nonnull; +import net.anweisen.utilities.bukkit.utils.misc.MinecraftVersion; import net.anweisen.utilities.common.annotations.Since; import net.codingarea.challenges.plugin.Challenges; import net.codingarea.challenges.plugin.challenges.type.abstraction.Setting; import net.codingarea.challenges.plugin.content.Message; import net.codingarea.challenges.plugin.management.menu.MenuType; -import net.codingarea.challenges.plugin.management.scheduler.task.ScheduledTask; import net.codingarea.challenges.plugin.utils.item.ItemBuilder; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; -import org.bukkit.event.entity.EntityDamageEvent; -import org.bukkit.event.entity.EntityPotionEffectEvent; -import org.bukkit.event.entity.EntityRegainHealthEvent; import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.scoreboard.*; - -import javax.annotation.Nonnull; +import org.bukkit.scoreboard.Criteria; +import org.bukkit.scoreboard.DisplaySlot; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.RenderType; +import org.bukkit.scoreboard.Scoreboard; +import org.bukkit.scoreboard.ScoreboardManager; /** * @author anweisen | https://github.com/anweisen @@ -54,55 +54,12 @@ protected void onDisable() { public void onJoin(@Nonnull PlayerJoinEvent event) { if (isEnabled()) { show(event.getPlayer()); - updatePlayerHealth(event.getPlayer()); } else { hide(event.getPlayer()); } } - @EventHandler(priority = EventPriority.MONITOR) - public void onDamage(@Nonnull EntityDamageEvent event) { - if (!(event.getEntity() instanceof Player)) return; - - Bukkit.getScheduler().runTaskLater(Challenges.getInstance(), () -> { - updatePlayerHealth((Player) event.getEntity()); - }, 1); - } - - @EventHandler(priority = EventPriority.MONITOR) - public void onRegain(@Nonnull EntityRegainHealthEvent event) { - if (!(event.getEntity() instanceof Player)) return; - - Bukkit.getScheduler().runTaskLater(Challenges.getInstance(), () -> { - updatePlayerHealth((Player) event.getEntity()); - }, 1); - } - - @EventHandler(priority = EventPriority.MONITOR) - public void onEffect(@Nonnull EntityPotionEffectEvent event) { - if (!(event.getEntity() instanceof Player)) return; - - Bukkit.getScheduler().runTaskLater(Challenges.getInstance(), () -> { - updatePlayerHealth((Player) event.getEntity()); - }, 1); - } - - - @ScheduledTask(ticks = 10) - public void updateAllPlayersHealth() { - Bukkit.getOnlinePlayers().forEach(this::updatePlayerHealth); - } - - protected void updatePlayerHealth(@Nonnull Player player) { - for (Player current : Bukkit.getOnlinePlayers()) { - Objective objective = current.getScoreboard().getObjective(OBJECTIVE_NAME); - if (objective == null) continue; - objective.getScore(player.getName()).setScore((int) (player.getHealth() + BukkitReflectionUtils.getAbsorptionAmount(player))); - } - } - private void show(@Nonnull Player player) { - Scoreboard scoreboard = player.getScoreboard(); ScoreboardManager manager = Bukkit.getScoreboardManager(); if (manager == null) return; @@ -112,6 +69,7 @@ private void show(@Nonnull Player player) { Objective objective = scoreboard.getObjective(OBJECTIVE_NAME); if (objective == null) objective = scoreboard.registerNewObjective(OBJECTIVE_NAME, "health", OBJECTIVE_NAME); + // Criteria interface only available since 1.20 objective.setDisplaySlot(DisplaySlot.PLAYER_LIST); @@ -122,10 +80,6 @@ private void show(@Nonnull Player player) { // In some versions of spigot RenderType does not exist } - for (Player current : Bukkit.getOnlinePlayers()) { - objective.getScore(current.getName()).setScore((int) current.getHealth()); - } - } private void hide(@Nonnull Player player) {