From 1392b30341eeabe657e170631dd4a0830c46ad93 Mon Sep 17 00:00:00 2001 From: Jason H <51760372+JasonHorkles@users.noreply.github.com> Date: Wed, 10 Apr 2024 13:50:31 -0600 Subject: [PATCH] Add group support to amount-to-take + reformat code Closes #6 --- pom.xml | 2 +- .../expensivedeaths/DeathEvent.java | 39 ++++++++++++------- .../expensivedeaths/Execution.java | 14 ++++--- .../expensivedeaths/ExpensiveDeaths.java | 17 ++++++-- .../expensivedeaths/RespawnEvent.java | 4 +- src/main/resources/config.yml | 18 ++++++--- 6 files changed, 63 insertions(+), 31 deletions(-) diff --git a/pom.xml b/pom.xml index 5bc8d91..96556e3 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.jasonhorkles ExpensiveDeaths - 1.3.0 + 1.4.0-beta 17 diff --git a/src/main/java/me/jasonhorkles/expensivedeaths/DeathEvent.java b/src/main/java/me/jasonhorkles/expensivedeaths/DeathEvent.java index 410d146..4106bf2 100644 --- a/src/main/java/me/jasonhorkles/expensivedeaths/DeathEvent.java +++ b/src/main/java/me/jasonhorkles/expensivedeaths/DeathEvent.java @@ -2,6 +2,7 @@ import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.EconomyResponse; +import net.milkbowl.vault.permission.Permission; import org.apache.commons.lang.LocaleUtils; import org.bukkit.ChatColor; import org.bukkit.entity.Player; @@ -21,49 +22,61 @@ public DeathEvent(ExpensiveDeaths plugin) { } private final Economy econ = ExpensiveDeaths.getInstance().getEconomy(); + private final Permission perms = ExpensiveDeaths.getInstance().getPermissions(); private final ExpensiveDeaths plugin; @EventHandler(ignoreCancelled = true) public void deathEvent(PlayerDeathEvent event) { Player player = event.getEntity(); + // Check if the player has the bypass permission if (player.hasPermission("expensivedeaths.bypass")) { - if (!plugin.getConfig().getString("bypass-message").isBlank()) player.sendMessage( - ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("bypass-message"))); + if (!plugin.getConfig().getString("bypass-message").isBlank()) + player.sendMessage(ChatColor.translateAlternateColorCodes('&', + plugin.getConfig().getString("bypass-message"))); return; } + // Get the player's primary group, or "default" if they don't have one from the config + String primaryGroup = perms.getPrimaryGroup(null, player).toLowerCase(); + if (!plugin.getConfig().contains("amount-to-take." + primaryGroup)) primaryGroup = "default"; + EconomyResponse result; - String option = plugin.getConfig().getString("amount-to-take"); + String option = plugin.getConfig().getString("amount-to-take." + primaryGroup); DecimalFormat format = new DecimalFormat(plugin.getConfig().getString("currency-format")); - format.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance( - LocaleUtils.toLocale("en_" + plugin.getConfig().getString("currency-country")))); + format.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(LocaleUtils.toLocale("en_" + plugin + .getConfig().getString("currency-country")))); + // Empty balance if (option.equalsIgnoreCase("ALL")) result = econ.withdrawPlayer(player, econ.getBalance(player)); + // Random percent range else if (option.contains("%")) if (option.contains("-")) { double min = Double.parseDouble(option.replaceAll("-.*", "")); double max = Double.parseDouble(option.replaceAll(".*-", "").replace("%", "")); double r = ThreadLocalRandom.current().nextDouble(min, max + 1); result = econ.withdrawPlayer(player, (r / 100) * econ.getBalance(player)); + // Percent } else result = econ.withdrawPlayer(player, (Double.parseDouble(option.replace("%", "")) / 100) * econ.getBalance(player)); + // Fixed amount else result = econ.withdrawPlayer(player, Double.parseDouble(option)); + // Send the death message final String money = String.valueOf(format.format(result.amount)); final String balance = String.valueOf(format.format(result.balance)); - if (!plugin.getConfig().getString("death-message").isBlank()) player.sendMessage( - ChatColor.translateAlternateColorCodes('&', + if (!plugin.getConfig().getString("death-message").isBlank()) + player.sendMessage(ChatColor.translateAlternateColorCodes( + '&', plugin.getConfig().getString("death-message").replace("{MONEY}", money) .replace("{BALANCE}", balance))); - + // Run the executions Player killer = player.getKiller(); final Function parser = str -> { - String s = str.replace("{PLAYER}", player.getName()) - .replace("{DISPLAYNAME}", player.getDisplayName()).replace("{MONEY}", money) - .replace("{BALANCE}", balance); - if (killer != null) s = s.replace("{KILLER}", killer.getName()) - .replace("{KILLER_DISPLAYNAME}", killer.getDisplayName()); + String s = str.replace("{PLAYER}", player.getName()).replace("{DISPLAYNAME}", + player.getDisplayName()).replace("{MONEY}", money).replace("{BALANCE}", balance); + if (killer != null) s = s.replace("{KILLER}", killer.getName()).replace("{KILLER_DISPLAYNAME}", + killer.getDisplayName()); return s; }; diff --git a/src/main/java/me/jasonhorkles/expensivedeaths/Execution.java b/src/main/java/me/jasonhorkles/expensivedeaths/Execution.java index c6c2ff2..7fda8f1 100644 --- a/src/main/java/me/jasonhorkles/expensivedeaths/Execution.java +++ b/src/main/java/me/jasonhorkles/expensivedeaths/Execution.java @@ -15,8 +15,8 @@ import java.util.regex.Pattern; public abstract class Execution { - private static final Supplier USE_PLACEHOLDERAPI = Suppliers.memoize( - () -> Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")); + private static final Supplier USE_PLACEHOLDERAPI = Suppliers.memoize(() -> Bukkit + .getPluginManager().isPluginEnabled("PlaceholderAPI")); private static final Pattern KEY_CHANCE = Pattern.compile("(?i)(test-?)?(chance|prob(ability)?)"); private static final Pattern KEY_CANCEL = Pattern.compile("(?i)break|stop|cancel(ling)?"); private static final Pattern KEY_PERMISSION = Pattern.compile("(?i)(meet-?)?perm(ission)?"); @@ -52,8 +52,10 @@ else if (KEY_EXECUTION.matcher(key).matches()) { if (execution != null) executions.add(execution); } } - if (!executions.isEmpty()) - return new AdvancedExecution(chance, cancelling, permission, executions); + if (!executions.isEmpty()) return new AdvancedExecution(chance, + cancelling, + permission, + executions); } return null; } @@ -66,8 +68,8 @@ public void run(Player player, Player agent, Function parser, bo public void run(CommandSender sender, Player player, Player agent, String cmd, Function parser) { String s = parser.apply(cmd); - if (USE_PLACEHOLDERAPI.get() && ExpensiveDeaths.getInstance().getConfig() - .getBoolean("bonus.parse-placeholders")) { + if (USE_PLACEHOLDERAPI.get() && ExpensiveDeaths.getInstance().getConfig().getBoolean( + "bonus.parse-placeholders")) { s = PlaceholderAPI.setPlaceholders(player, s); if (agent != null) s = PlaceholderAPI.setBracketPlaceholders(agent, s); } diff --git a/src/main/java/me/jasonhorkles/expensivedeaths/ExpensiveDeaths.java b/src/main/java/me/jasonhorkles/expensivedeaths/ExpensiveDeaths.java index 1114fa3..65535e3 100644 --- a/src/main/java/me/jasonhorkles/expensivedeaths/ExpensiveDeaths.java +++ b/src/main/java/me/jasonhorkles/expensivedeaths/ExpensiveDeaths.java @@ -1,7 +1,7 @@ package me.jasonhorkles.expensivedeaths; import net.milkbowl.vault.economy.Economy; -import org.bukkit.Bukkit; +import net.milkbowl.vault.permission.Permission; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -21,6 +21,7 @@ public static ExpensiveDeaths getInstance() { } private Economy econ; + private Permission perms; private final Map executions = new HashMap<>(); private static ExpensiveDeaths instance; @@ -29,6 +30,7 @@ public void onEnable() { instance = this; setupEconomy(); + setupPermissions(); saveDefaultConfig(); loadExecutions(); @@ -49,20 +51,29 @@ public Economy getEconomy() { return econ; } + public Permission getPermissions() { + return perms; + } + public void run(Execution.Type type, Player player, Player agent, Function parser) { final Execution execution = this.executions.get(type); if (execution != null) execution.run(player, agent, parser, type.isConsole()); } private void setupEconomy() { - if (Bukkit.getPluginManager().getPlugin("Vault") == null) return; - RegisteredServiceProvider rsp = getServer().getServicesManager() .getRegistration(Economy.class); if (rsp == null) return; econ = rsp.getProvider(); } + private void setupPermissions() { + RegisteredServiceProvider rsp = getServer().getServicesManager().getRegistration( + Permission.class); + if (rsp == null) return; + perms = rsp.getProvider(); + } + private void loadExecutions() { this.executions.clear(); loadExecution(Execution.Type.DEATH_CONSOLE, "console-commands-on-death"); diff --git a/src/main/java/me/jasonhorkles/expensivedeaths/RespawnEvent.java b/src/main/java/me/jasonhorkles/expensivedeaths/RespawnEvent.java index d8ed719..954797d 100644 --- a/src/main/java/me/jasonhorkles/expensivedeaths/RespawnEvent.java +++ b/src/main/java/me/jasonhorkles/expensivedeaths/RespawnEvent.java @@ -23,8 +23,8 @@ public void respawnEvent(PlayerRespawnEvent event) { public void run() { if (!player.isOnline() || player.hasPermission("expensivedeaths.bypass")) return; - final Function parser = s -> s.replace("{PLAYER}", player.getName()) - .replace("{DISPLAYNAME}", player.getDisplayName()); + final Function parser = s -> s.replace("{PLAYER}", player.getName()).replace("{DISPLAYNAME}", + player.getDisplayName()); plugin.run(Execution.Type.RESPAWN_CONSOLE, player, null, parser); plugin.run(Execution.Type.RESPAWN_PLAYER, player, null, parser); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 6cfc443..9ccd466 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -5,12 +5,18 @@ death-message: "&cYou lost ${MONEY} because you died!" bypass-message: "&aYou didn't lose money because you're special!" # How much should we take from the player when they die? -# Valid options are: -# amount-to-take: 15% - takes 15% of their balance -# amount-to-take: 0-15% - takes random amount between 0-15% of their balance -# amount-to-take: ALL - resets their balance to 0 -# amount-to-take: 100.00 - takes $100 from their balance -amount-to-take: 0.00 +# Valid examples: +# 15% - takes 15% of their balance +# 0-15% - takes random amount between 0-15% of their balance +# ALL - resets their balance to 0 +# 100.00 - takes $100 from their balance +# +# A different value can be specified for any groups in your permission plugin +# Will use the default value if a player is not in any of the defined groups +# Group names must be lowercase +amount-to-take: + default: 0.00 + #millionaire: 30% # The format to use for displaying currencies # The current format is used by most countries and likely doesn't need touched