Skip to content

Commit

Permalink
Add group support to amount-to-take + reformat code
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
JasonHorkles committed Apr 10, 2024
1 parent 13d51ed commit 1392b30
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.jasonhorkles</groupId>
<artifactId>ExpensiveDeaths</artifactId>
<version>1.3.0</version>
<version>1.4.0-beta</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
39 changes: 26 additions & 13 deletions src/main/java/me/jasonhorkles/expensivedeaths/DeathEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String> 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;
};

Expand Down
14 changes: 8 additions & 6 deletions src/main/java/me/jasonhorkles/expensivedeaths/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import java.util.regex.Pattern;

public abstract class Execution {
private static final Supplier<Boolean> USE_PLACEHOLDERAPI = Suppliers.memoize(
() -> Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI"));
private static final Supplier<Boolean> 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)?");
Expand Down Expand Up @@ -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;
}
Expand All @@ -66,8 +68,8 @@ public void run(Player player, Player agent, Function<String, String> parser, bo

public void run(CommandSender sender, Player player, Player agent, String cmd, Function<String, String> 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);
}
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/me/jasonhorkles/expensivedeaths/ExpensiveDeaths.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,6 +21,7 @@ public static ExpensiveDeaths getInstance() {
}

private Economy econ;
private Permission perms;
private final Map<Execution.Type, Execution> executions = new HashMap<>();
private static ExpensiveDeaths instance;

Expand All @@ -29,6 +30,7 @@ public void onEnable() {
instance = this;

setupEconomy();
setupPermissions();
saveDefaultConfig();
loadExecutions();

Expand All @@ -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<String, String> 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<Economy> rsp = getServer().getServicesManager()
.getRegistration(Economy.class);
if (rsp == null) return;
econ = rsp.getProvider();
}

private void setupPermissions() {
RegisteredServiceProvider<Permission> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public void respawnEvent(PlayerRespawnEvent event) {
public void run() {
if (!player.isOnline() || player.hasPermission("expensivedeaths.bypass")) return;

final Function<String, String> parser = s -> s.replace("{PLAYER}", player.getName())
.replace("{DISPLAYNAME}", player.getDisplayName());
final Function<String, String> 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);
}
Expand Down
18 changes: 12 additions & 6 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1392b30

Please sign in to comment.