Skip to content

Commit

Permalink
Add gapple placeholder support
Browse files Browse the repository at this point in the history
  • Loading branch information
kernitus committed Jan 12, 2024
1 parent d310c4b commit 431ca86
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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")
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <modeset> [player] &eto change modeset"
invalid-modeset: "&cPlease specify a valid modeset!"
invalid-player: "&cPlease specify a valid player!"
Expand Down

0 comments on commit 431ca86

Please sign in to comment.