From 6c0a799198df995bec0c4925ace57e0e3028b647 Mon Sep 17 00:00:00 2001 From: Zeshan Aslam Date: Tue, 8 Jun 2021 18:57:07 -0400 Subject: [PATCH] Added alterative placeholders in case being overridden --- config.yml | 1 + plugin.yml | 2 +- .../zeshanaslam/actionhealth/LookThread.java | 2 +- .../actionhealth/action/ActionHelper.java | 3 +- .../actionhealth/action/ActionListener.java | 12 +------ .../actionhealth/action/ActionStore.java | 11 ++++--- .../actionhealth/action/ActionTask.java | 2 +- .../actionhealth/commands/HealthCommand.java | 5 +-- .../actionhealth/events/HealthListeners.java | 2 -- .../actionhealth/utils/HealthUtil.java | 31 ++++++++++--------- 10 files changed, 32 insertions(+), 39 deletions(-) diff --git a/config.yml b/config.yml index 9c16fef..d6ef5f8 100644 --- a/config.yml +++ b/config.yml @@ -6,6 +6,7 @@ # {displayname} will use player/mob custom name. # {opponentlastdamage} the amount of damage the enemy last received. # {percenthealth} displays the percentage of health left. +# Note: if placeholders are being overridden by other plugins you can add 'ah' in front of it i.e {ahhealth} # # For PlaceholderAPI or MVdWPlaceholderAPI: # By default placeholders will be retrieved as the attacking player. diff --git a/plugin.yml b/plugin.yml index 82664c5..382e713 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ name: ActionHealth main: com.zeshanaslam.actionhealth.Main -version: 3.5.2 +version: 3.5.3 softdepend: [PlaceholderAPI, MVdWPlaceholderAPI, WorldGuard, mcMMO, MythicMobs, LangUtils] commands: Actionhealth: diff --git a/src/com/zeshanaslam/actionhealth/LookThread.java b/src/com/zeshanaslam/actionhealth/LookThread.java index e03d4c5..886f3c4 100644 --- a/src/com/zeshanaslam/actionhealth/LookThread.java +++ b/src/com/zeshanaslam/actionhealth/LookThread.java @@ -35,7 +35,7 @@ public class LookThread extends BukkitRunnable { for (Player player : Bukkit.getOnlinePlayers()) { if (plugin.toggle.contains(player.getUniqueId())) { if (plugin.configStore.toggleMessage != null && !plugin.configStore.toggleMessage.equals("")) { - plugin.healthUtil.sendActionBar(player, plugin.configStore.toggleMessage.replace("{name}", player.getName())); + plugin.healthUtil.sendActionBar(player, plugin.healthUtil.replacePlaceholders(plugin.configStore.toggleMessage, "name", player.getName())); } continue; } diff --git a/src/com/zeshanaslam/actionhealth/action/ActionHelper.java b/src/com/zeshanaslam/actionhealth/action/ActionHelper.java index 4a86e22..7b7fdbe 100644 --- a/src/com/zeshanaslam/actionhealth/action/ActionHelper.java +++ b/src/com/zeshanaslam/actionhealth/action/ActionHelper.java @@ -16,7 +16,6 @@ import org.bukkit.projectiles.ProjectileSource; import java.util.ArrayList; import java.util.List; import java.util.Optional; -import java.util.UUID; public class ActionHelper { @@ -28,7 +27,7 @@ public class ActionHelper { public void executeTriggers(ActionStore.ActionType actionType, Player player, ItemStack itemStack) { if (itemStack != null) { - for (String name: getName(itemStack)) + for (String name : getName(itemStack)) executeTriggers(actionType, player, name); } } diff --git a/src/com/zeshanaslam/actionhealth/action/ActionListener.java b/src/com/zeshanaslam/actionhealth/action/ActionListener.java index 75b0f27..2cf1532 100644 --- a/src/com/zeshanaslam/actionhealth/action/ActionListener.java +++ b/src/com/zeshanaslam/actionhealth/action/ActionListener.java @@ -4,7 +4,6 @@ import com.zeshanaslam.actionhealth.Main; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; -import org.bukkit.entity.Projectile; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; @@ -15,15 +14,6 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerItemConsumeEvent; import org.bukkit.event.player.PlayerItemHeldEvent; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.inventory.meta.PotionMeta; -import org.bukkit.potion.Potion; -import org.bukkit.potion.PotionData; -import org.bukkit.potion.PotionEffect; -import org.bukkit.projectiles.ProjectileSource; - -import java.util.ArrayList; -import java.util.List; public class ActionListener implements Listener { @@ -113,7 +103,7 @@ public class ActionListener implements Listener { ActionStore.ActionType actionType = ActionStore.ActionType.RIGHTCLICK; actionHelper.executeTriggers(actionType, player, itemStack); - } else if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) { + } else if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) { ActionStore.ActionType actionType = ActionStore.ActionType.LEFTCLICK; actionHelper.executeTriggers(actionType, player, itemStack); } diff --git a/src/com/zeshanaslam/actionhealth/action/ActionStore.java b/src/com/zeshanaslam/actionhealth/action/ActionStore.java index 33461da..29a139c 100644 --- a/src/com/zeshanaslam/actionhealth/action/ActionStore.java +++ b/src/com/zeshanaslam/actionhealth/action/ActionStore.java @@ -1,4 +1,5 @@ package com.zeshanaslam.actionhealth.action; + import com.zeshanaslam.actionhealth.Main; import com.zeshanaslam.actionhealth.action.data.Action; import com.zeshanaslam.actionhealth.action.data.Tagged; @@ -22,11 +23,11 @@ public class ActionStore { this.main = main; enabled = main.getConfig().getBoolean("Action.Enabled"); tagLength = main.getConfig().getInt("Action.TagLength"); - tagAmount = main.getConfig().getInt("Action.TagAmount"); + tagAmount = main.getConfig().getInt("Action.TagAmount"); events = new HashMap<>(); - for (String action: main.getConfig().getConfigurationSection("Action.Events").getKeys(false)) { - for (String type: main.getConfig().getConfigurationSection("Action.Events." + action).getKeys(false)) { + for (String action : main.getConfig().getConfigurationSection("Action.Events").getKeys(false)) { + for (String type : main.getConfig().getConfigurationSection("Action.Events." + action).getKeys(false)) { String output = main.getConfig().getString("Action.Events." + action + "." + type); ActionType actionType = ActionType.valueOf(action); @@ -70,8 +71,8 @@ public class ActionStore { } private void sendMessage(LivingEntity entity, String message, Optional health) { - for (List taggedList: tagged.values()) { - for (Tagged tagged: taggedList) { + for (List taggedList : tagged.values()) { + for (Tagged tagged : taggedList) { if (tagged.damaged.equals(entity.getUniqueId())) { Player damager = Bukkit.getServer().getPlayer(tagged.damager); diff --git a/src/com/zeshanaslam/actionhealth/action/ActionTask.java b/src/com/zeshanaslam/actionhealth/action/ActionTask.java index a1329d9..117a450 100644 --- a/src/com/zeshanaslam/actionhealth/action/ActionTask.java +++ b/src/com/zeshanaslam/actionhealth/action/ActionTask.java @@ -17,7 +17,7 @@ public class ActionTask extends BukkitRunnable { @Override public void run() { - for (UUID key: main.configStore.actionStore.tagged.keySet()) { + for (UUID key : main.configStore.actionStore.tagged.keySet()) { Iterator taggedIterator = main.configStore.actionStore.tagged.get(key).iterator(); while (taggedIterator.hasNext()) { Tagged tagged = taggedIterator.next(); diff --git a/src/com/zeshanaslam/actionhealth/commands/HealthCommand.java b/src/com/zeshanaslam/actionhealth/commands/HealthCommand.java index 2618f81..225a7f8 100644 --- a/src/com/zeshanaslam/actionhealth/commands/HealthCommand.java +++ b/src/com/zeshanaslam/actionhealth/commands/HealthCommand.java @@ -42,11 +42,12 @@ public class HealthCommand implements CommandExecutor { if (plugin.toggle.contains(player.getUniqueId())) { plugin.toggle.remove(player.getUniqueId()); - player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.configStore.enableMessage).replace("{name}", player.getName())); + + player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.healthUtil.replacePlaceholders(plugin.configStore.enableMessage, "name", player.getName()))); } else { plugin.toggle.add(player.getUniqueId()); - player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.configStore.disableMessage).replace("{name}", player.getName())); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.healthUtil.replacePlaceholders(plugin.configStore.disableMessage, "name", player.getName()))); } if (plugin.configStore.rememberToggle) { diff --git a/src/com/zeshanaslam/actionhealth/events/HealthListeners.java b/src/com/zeshanaslam/actionhealth/events/HealthListeners.java index 343fccd..755dbb7 100644 --- a/src/com/zeshanaslam/actionhealth/events/HealthListeners.java +++ b/src/com/zeshanaslam/actionhealth/events/HealthListeners.java @@ -2,7 +2,6 @@ package com.zeshanaslam.actionhealth.events; import com.zeshanaslam.actionhealth.Main; import com.zeshanaslam.actionhealth.utils.FileHandler; -import org.bukkit.Material; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -11,7 +10,6 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.bukkit.event.player.PlayerItemConsumeEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; diff --git a/src/com/zeshanaslam/actionhealth/utils/HealthUtil.java b/src/com/zeshanaslam/actionhealth/utils/HealthUtil.java index b541ed5..ab8e9ca 100644 --- a/src/com/zeshanaslam/actionhealth/utils/HealthUtil.java +++ b/src/com/zeshanaslam/actionhealth/utils/HealthUtil.java @@ -1,6 +1,5 @@ package com.zeshanaslam.actionhealth.utils; -import be.maximvdw.placeholderapi.PlaceholderAPI; import com.zeshanaslam.actionhealth.Main; import com.zeshanaslam.actionhealth.api.HealthSendEvent; import com.zeshanaslam.actionhealth.support.*; @@ -103,16 +102,16 @@ public class HealthUtil { displayName = player.getDisplayName(); } - output = output.replace("{displayname}", displayName); + output = replacePlaceholders(output, "displayname", displayName); // Set placeholders as attacker if (plugin.configStore.hasMVdWPlaceholderAPI) { - output = output.replace("ATTACKEDPLAYER_", ""); + output = replacePlaceholders(output, "ATTACKEDPLAYER_", ""); output = be.maximvdw.placeholderapi.PlaceholderAPI.replacePlaceholders(player, output); } if (plugin.configStore.hasPlaceholderAPI) { - output = output.replace("ATTACKEDPLAYER_", ""); + output = replacePlaceholders(output, "ATTACKEDPLAYER_", ""); output = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, output); } } else { @@ -120,7 +119,7 @@ public class HealthUtil { output = plugin.configStore.healthMessageOther; } - output = output.replace("{displayname}", name); + output = replacePlaceholders(output, "displayname", name); } // Set placeholders as receiver @@ -132,13 +131,13 @@ public class HealthUtil { output = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(receiver, output); } - output = output.replace("{name}", name); - output = output.replace("{health}", String.valueOf((int) health)); - output = output.replace("{maxhealth}", String.valueOf((int) maxHealth)); - output = output.replace("{percenthealth}", String.valueOf((int) ((health / maxHealth) * 100.0))); - output = output.replace("{opponentlastdamage}", String.valueOf((int) entity.getLastDamage())); + output = replacePlaceholders(output, "name", name); + output = replacePlaceholders(output, "health", String.valueOf((int) health)); + output = replacePlaceholders(output, "maxhealth", String.valueOf((int) maxHealth)); + output = replacePlaceholders(output, "percenthealth", String.valueOf((int) ((health / maxHealth) * 100.0))); + output = replacePlaceholders(output, "opponentlastdamage", String.valueOf((int) entity.getLastDamage())); - if (output.contains("{usestyle}")) { + if (output.contains("usestyle")) { StringBuilder style = new StringBuilder(); int left = getLimitHealth(maxHealth); double heart = maxHealth / getLimitHealth(maxHealth); @@ -176,7 +175,7 @@ public class HealthUtil { } } - output = output.replace("{usestyle}", style.toString()); + output = replacePlaceholders(output, "usestyle", style.toString()); } HealthSendEvent healthSendEvent = new HealthSendEvent(receiver, entity, output); @@ -234,6 +233,10 @@ public class HealthUtil { return name; } + public String replacePlaceholders(String s, String key, String value) { + return s.replace("{" + key + "}", value).replace("{ah" + key + "}", value); + } + private String getNameReflection(LivingEntity entity) { String name; Method getName = null; @@ -265,7 +268,7 @@ public class HealthUtil { message = ChatColor.translateAlternateColorCodes('&', message); try { - if(plugin.configStore.mcVersion.equals("v1_16_R1") || plugin.configStore.mcVersion.equals("v1_16_R2") || plugin.configStore.mcVersion.equals("v1_16_R3")){ + if (plugin.configStore.mcVersion.equals("v1_16_R1") || plugin.configStore.mcVersion.equals("v1_16_R2") || plugin.configStore.mcVersion.equals("v1_16_R3")) { new PreAction(player, message); } else if (plugin.configStore.mcVersion.equals("v1_12_R1") || plugin.configStore.mcVersion.startsWith("v1_13") || plugin.configStore.mcVersion.startsWith("v1_14_") || plugin.configStore.mcVersion.startsWith("v1_15_")) { new LegacyPreAction(player, message); @@ -373,7 +376,7 @@ public class HealthUtil { private void sendMessage(Player player) { if (plugin.configStore.toggleMessage != null && !plugin.configStore.toggleMessage.equals("")) { - plugin.healthUtil.sendActionBar(player, plugin.configStore.toggleMessage.replace("{name}", player.getName())); + plugin.healthUtil.sendActionBar(player, replacePlaceholders(plugin.configStore.toggleMessage, "name", player.getName())); } }