diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/hooks/PlaceholderAPIHook.java b/src/main/java/kernitus/plugin/OldCombatMechanics/hooks/PlaceholderAPIHook.java index bf158e85..ef86916a 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/hooks/PlaceholderAPIHook.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/hooks/PlaceholderAPIHook.java @@ -7,6 +7,7 @@ import kernitus.plugin.OldCombatMechanics.OCMMain; import kernitus.plugin.OldCombatMechanics.hooks.api.Hook; +import kernitus.plugin.OldCombatMechanics.module.ModuleGoldenApple; import kernitus.plugin.OldCombatMechanics.utilities.storage.PlayerData; import kernitus.plugin.OldCombatMechanics.utilities.storage.PlayerStorage; import me.clip.placeholderapi.expansion.PlaceholderExpansion; @@ -46,8 +47,31 @@ public boolean persist() { @Override public String onPlaceholderRequest(Player player, @NotNull String identifier) { - if (player == null || !identifier.equals("modeset")) return null; + if (player == null) return null; + switch (identifier){ + case "modeset": + return getModeset(player); + case "gapple_cooldown": + return getGappleCooldown(player); + case "napple_cooldown": + return getNappleCooldown(player); + } + + return null; + } + + private String getGappleCooldown(Player player){ + final long seconds = ModuleGoldenApple.getInstance().getGappleCooldown(player.getUniqueId()); + return seconds > 0 ? String.valueOf(seconds) : "No cooldown"; + } + + private String getNappleCooldown(Player player){ + final long seconds = ModuleGoldenApple.getInstance().getNappleCooldown(player.getUniqueId()); + return seconds > 0 ? String.valueOf(seconds) : "No cooldown"; + } + + private String getModeset(Player player){ final PlayerData playerData = PlayerStorage.getPlayerData(player.getUniqueId()); String modeName = playerData.getModesetForWorld(player.getWorld().getUID()); if (modeName == null || modeName.isEmpty()) modeName = "unknown"; diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleGoldenApple.java b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleGoldenApple.java index aac3a0d0..7c5b3c44 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleGoldenApple.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleGoldenApple.java @@ -25,6 +25,7 @@ import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; +import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.*; @@ -50,9 +51,11 @@ public class ModuleGoldenApple extends OCMModule { private Cooldown cooldown; private String normalCooldownMessage, enchantedCooldownMessage; + private static ModuleGoldenApple INSTANCE; public ModuleGoldenApple(OCMMain plugin) { super(plugin, "old-golden-apples"); + INSTANCE = this; } @SuppressWarnings("deprecated") @@ -87,6 +90,10 @@ public void reload() { registerCrafting(); } + public static ModuleGoldenApple getInstance() { + return ModuleGoldenApple.INSTANCE; + } + private void registerCrafting() { if (isEnabled() && module().getBoolean("enchanted-golden-apple-crafting")) { if (Bukkit.getRecipesFor(ENCHANTED_GOLDEN_APPLE.newInstance()).size() > 0) return; @@ -224,6 +231,36 @@ public void onPlayerQuit(PlayerQuitEvent e) { if (lastEaten != null) lastEaten.remove(uuid); } + /** + * Get player's current golden apple cooldown + * @param playerUUID The player's UUID + * @return The current cooldown, in seconds + */ + public long getGappleCooldown(UUID playerUUID) { + final LastEaten lastEatenInfo = lastEaten.get(playerUUID); + if (lastEatenInfo != null && lastEatenInfo.lastNormalEaten != null) { + long timeElapsedSinceEaten = Duration.between(lastEatenInfo.lastNormalEaten, Instant.now()).getSeconds(); + long cooldownRemaining = cooldown.normal - timeElapsedSinceEaten; + return Math.max(cooldownRemaining, 0); // Return 0 if the cooldown has expired + } + return 0; + } + + /** + * Get player's current enchanted golden apple cooldown + * @param playerUUID The player's UUID + * @return The current cooldown, in seconds + */ + public long getNappleCooldown(UUID playerUUID) { + final LastEaten lastEatenInfo = lastEaten.get(playerUUID); + if (lastEatenInfo != null && lastEatenInfo.lastEnchantedEaten != null) { + long timeElapsedSinceEaten = Duration.between(lastEatenInfo.lastEnchantedEaten, Instant.now()).getSeconds(); + long cooldownRemaining = cooldown.enchanted - timeElapsedSinceEaten; + return Math.max(cooldownRemaining, 0); // Return 0 if the cooldown has expired + } + return 0; + } + private static class LastEaten { private Instant lastNormalEaten; private Instant lastEnchantedEaten; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9d2c441c..19eeef7d 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -46,7 +46,7 @@ worlds: mode-messages: # Messages used when changing player mode - mode-status: '&bYour current modeset is: &7%s' + mode-status: "&bYour current modeset is: &7%s" message-usage: "&eYou can use &c/ocm mode [player] &eto change modeset" invalid-modeset: "&cPlease specify a valid modeset!" invalid-player: "&cPlease specify a valid player!"