From 881839955e65e81cbd1af7054bcdb552992d74de Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 3 Jul 2022 16:38:21 +0100 Subject: [PATCH] Added player health fixer --- .../com/willfp/eco/core/Prerequisite.java | 10 ++++- .../eco/internal/spigot/EcoSpigotPlugin.kt | 4 +- .../spigot/player/PlayerHealthFixer.kt | 42 +++++++++++++++++++ .../core-plugin/src/main/resources/config.yml | 5 ++- 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/player/PlayerHealthFixer.kt diff --git a/eco-api/src/main/java/com/willfp/eco/core/Prerequisite.java b/eco-api/src/main/java/com/willfp/eco/core/Prerequisite.java index 414ecfaa..80ee236d 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/Prerequisite.java +++ b/eco-api/src/main/java/com/willfp/eco/core/Prerequisite.java @@ -41,11 +41,19 @@ public class Prerequisite { "Requires server to have vault" ); + /** + * Requires the server to be running 1.19. + */ + public static final Prerequisite HAS_1_19 = new Prerequisite( + () -> ProxyConstants.NMS_VERSION.contains("19"), + "Requires server to be running 1.19+" + ); + /** * Requires the server to be running 1.18. */ public static final Prerequisite HAS_1_18 = new Prerequisite( - () -> ProxyConstants.NMS_VERSION.contains("18"), + () -> ProxyConstants.NMS_VERSION.contains("18") || HAS_1_19.isMet(), "Requires server to be running 1.18+" ); diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt index 071f22cc..9e346e64 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt @@ -111,6 +111,7 @@ import com.willfp.eco.internal.spigot.integrations.shop.ShopEconomyShopGUI import com.willfp.eco.internal.spigot.integrations.shop.ShopShopGuiPlus import com.willfp.eco.internal.spigot.integrations.shop.ShopZShop import com.willfp.eco.internal.spigot.math.evaluateExpression +import com.willfp.eco.internal.spigot.player.PlayerHealthFixer import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy import com.willfp.eco.internal.spigot.proxy.SkullProxy import com.willfp.eco.internal.spigot.proxy.TPSProxy @@ -358,7 +359,8 @@ abstract class EcoSpigotPlugin : EcoPlugin() { ArrowDataListener(this), ArmorChangeEventListeners(this), DataListener(this), - PlayerBlockListener(this) + PlayerBlockListener(this), + PlayerHealthFixer(this) ) if (Prerequisite.HAS_PAPER.isMet) { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/player/PlayerHealthFixer.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/player/PlayerHealthFixer.kt new file mode 100644 index 00000000..181d231d --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/player/PlayerHealthFixer.kt @@ -0,0 +1,42 @@ +package com.willfp.eco.internal.spigot.player + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.data.keys.PersistentDataKey +import com.willfp.eco.core.data.keys.PersistentDataKeyType +import com.willfp.eco.core.data.profile +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.player.PlayerJoinEvent +import org.bukkit.event.player.PlayerQuitEvent + +class PlayerHealthFixer( + private val plugin: EcoPlugin +): Listener { + private val lastHealthKey = PersistentDataKey( + plugin.namespacedKeyFactory.create("last_health"), + PersistentDataKeyType.DOUBLE, + 0.0 + ) + + @EventHandler + fun onLeave(event: PlayerQuitEvent) { + if (!plugin.configYml.getBool("health-fixer")) { + return + } + + val player = event.player + player.profile.write(lastHealthKey, player.health) + } + + @EventHandler + fun onJoin(event: PlayerJoinEvent) { + if (!plugin.configYml.getBool("health-fixer")) { + return + } + + val player = event.player + plugin.scheduler.runLater(2) { + player.health = player.profile.read(lastHealthKey) + } + } +} diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index dc0d7ba3..8d689e6f 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -76,4 +76,7 @@ log-full-extension-errors: false # disable this option. Bear in mind that this means the auto-craft preview will fail to # render items nicely, which may degrade the user experience on your server. If you use # a custom crafting table, though, this won't affect anything, and you should disable the option. -displayed-recipes: true \ No newline at end of file +displayed-recipes: true + +# Save health on leave and set it back on join - works around attribute modifiers. +health-fixer: false \ No newline at end of file