Skip to content

Commit

Permalink
Add enderpearl cooldown placeholder support
Browse files Browse the repository at this point in the history
  • Loading branch information
kernitus committed Jan 12, 2024
1 parent 431ca86 commit f387796
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 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.ModuleDisableEnderpearlCooldown;
import kernitus.plugin.OldCombatMechanics.module.ModuleGoldenApple;
import kernitus.plugin.OldCombatMechanics.utilities.storage.PlayerData;
import kernitus.plugin.OldCombatMechanics.utilities.storage.PlayerStorage;
Expand Down Expand Up @@ -49,29 +50,36 @@ public boolean persist() {
public String onPlaceholderRequest(Player player, @NotNull String identifier) {
if (player == null) return null;

switch (identifier){
switch (identifier) {
case "modeset":
return getModeset(player);
case "gapple_cooldown":
return getGappleCooldown(player);
case "napple_cooldown":
return getNappleCooldown(player);
case "enderpearl_cooldown":
return getEnderpearlCooldown(player);
}

return null;
}

private String getGappleCooldown(Player player){
private String getGappleCooldown(Player player) {
final long seconds = ModuleGoldenApple.getInstance().getGappleCooldown(player.getUniqueId());
return seconds > 0 ? String.valueOf(seconds) : "No cooldown";
return seconds > 0 ? String.valueOf(seconds) : "None";
}

private String getNappleCooldown(Player player){
private String getNappleCooldown(Player player) {
final long seconds = ModuleGoldenApple.getInstance().getNappleCooldown(player.getUniqueId());
return seconds > 0 ? String.valueOf(seconds) : "No cooldown";
return seconds > 0 ? String.valueOf(seconds) : "None";
}

private String getModeset(Player player){
private String getEnderpearlCooldown(Player player) {
final long seconds = ModuleDisableEnderpearlCooldown.getInstance().getEnderpearlCooldown(player.getUniqueId());
return seconds > 0 ? String.valueOf(seconds) : "None";
}

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 @@ -35,9 +35,11 @@ public class ModuleDisableEnderpearlCooldown extends OCMModule {
private Map<UUID, Long> lastLaunched;
private int cooldown;
private String message;
private static ModuleDisableEnderpearlCooldown INSTANCE;

public ModuleDisableEnderpearlCooldown(OCMMain plugin) {
super(plugin, "disable-enderpearl-cooldown");
INSTANCE = this;
reload();
}

Expand All @@ -50,6 +52,10 @@ public void reload() {
message = module().getBoolean("showMessage") ? module().getString("message") : null;
}

public static ModuleDisableEnderpearlCooldown getInstance() {
return INSTANCE;
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerShoot(ProjectileLaunchEvent e) {
if (e.isCancelled()) return; // For compatibility with other plugins
Expand Down Expand Up @@ -112,4 +118,20 @@ private boolean isEnderPearl(ItemStack itemStack) {
public void onPlayerQuit(PlayerQuitEvent e) {
if (lastLaunched != null) lastLaunched.remove(e.getPlayer().getUniqueId());
}

/**
* Get the remaining cooldown time for ender pearls for a given player.
* @param playerUUID The UUID of the player to check the cooldown for.
* @return The remaining cooldown time in seconds, or 0 if there is no cooldown or it has expired.
*/
public long getEnderpearlCooldown(UUID playerUUID) {
if (lastLaunched != null && lastLaunched.containsKey(playerUUID)) {
final long currentTime = System.currentTimeMillis() / 1000; // Current time in seconds
final long lastLaunchTime = lastLaunched.get(playerUUID); // Last launch time in seconds
final long elapsedSeconds = currentTime - lastLaunchTime;
final long cooldownRemaining = cooldown - elapsedSeconds;
return Math.max(cooldownRemaining, 0); // Return the remaining cooldown or 0 if it has expired
}
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ public void onPlayerQuit(PlayerQuitEvent e) {

/**
* Get player's current golden apple cooldown
* @param playerUUID The player's UUID
* @return The current cooldown, in seconds
* @param playerUUID The UUID of the player to check the cooldown for.
* @return The remaining cooldown time in seconds, or 0 if there is no cooldown or it has expired.
*/
public long getGappleCooldown(UUID playerUUID) {
final LastEaten lastEatenInfo = lastEaten.get(playerUUID);
Expand All @@ -248,8 +248,8 @@ public long getGappleCooldown(UUID playerUUID) {

/**
* Get player's current enchanted golden apple cooldown
* @param playerUUID The player's UUID
* @return The current cooldown, in seconds
* @param playerUUID The UUID of the player to check the cooldown for.
* @return The remaining cooldown time in seconds, or 0 if there is no cooldown or it has expired.
*/
public long getNappleCooldown(UUID playerUUID) {
final LastEaten lastEatenInfo = lastEaten.get(playerUUID);
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ modesets:
# If not PvP, the modeset of the defending entity is checked.
# Of course, the module must also be enabled in its own config section.
# Sample below has modules enabled for "old" combat modeset and disabled for "new" combat modeset
# PlaceholderAPI: %ocm_modeset%
old:
- "disable-attack-cooldown"
- "disable-sword-sweep"
Expand Down Expand Up @@ -273,10 +274,12 @@ old-golden-apples:
# Cooldown between eating the apples, in seconds
cooldown:
# The cooldown for normal golden apples
# PlaceholderAPI: %ocm_gapple_cooldown%
normal: 0
# Message when user tries to eat golden apple during cooldown. Leave empty to disable.
message-normal: "&ePlease wait %seconds%s before eating another golden apple."
# The cooldown for enchanted golden apples
# PlaceholderAPI: %ocm_napple_cooldown%
enchanted: 0
# Message when user tries to eat enchanted golden apple during cooldown. Leave empty to disable.
message-enchanted: "&ePlease wait %seconds%s before eating another enchanted golden apple."
Expand Down Expand Up @@ -506,6 +509,7 @@ disable-enderpearl-cooldown:
# Disables enderpearl cooldown
enabled: true
# The cooldown, in seconds
# PlaceholderAPI: %ocm_enderpearl_cooldown%
cooldown: 0
# Show the user a message if they try to use an enderpearl and the cooldown has not expired yet
showMessage: true
Expand Down

0 comments on commit f387796

Please sign in to comment.