Files
src
build.gradle
settings.gradle
Code
9 762
src/main/java/com/fenixstudio/fxbounty/FxBounty.java
package com.fenixstudio.fxbounty;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FxBounty extends JavaPlugin implements Listener {
public static final UUID ADMIN = new UUID(0, 0);
public final Map<UUID, Bounty> bounties = new HashMap<>();
public final Set<UUID> muted = new HashSet<>();
public Economy economy;
private Storage storage;
public BountyMenu menu;
private static final Pattern HEX = Pattern.compile("&#([A-Fa-f0-9]{6})");
@Override public void onEnable() {
saveDefaultConfig();
RegisteredServiceProvider<Economy> provider = getServer().getServicesManager().getRegistration(Economy.class);
if (provider == null || provider.getProvider() == null) {
getLogger().severe("Vault economy provider not found.");
getServer().getPluginManager().disablePlugin(this); return;
}
economy = provider.getProvider();
try { storage = new Storage(this); storage.load(bounties, muted); }
catch (Exception e) { getLogger().severe("Cannot load bounty storage: " + e); getServer().getPluginManager().disablePlugin(this); return; }
menu = new BountyMenu(this);
BountyCommand command = new BountyCommand(this);
Objects.requireNonNull(getCommand("bounty")).setExecutor(command);
Objects.requireNonNull(getCommand("bounty")).setTabCompleter(command);
Objects.requireNonNull(getCommand("fxbounty")).setExecutor(command);
Objects.requireNonNull(getCommand("fxbounty")).setTabCompleter(command);
getServer().getPluginManager().registerEvents(this, this);
getServer().getPluginManager().registerEvents(menu, this);
}
public boolean save() {
try { storage.save(bounties, muted); return true; }
catch (Exception e) { getLogger().severe("Unable to save bounties: " + e); return false; }
}
public String color(String input) {
Matcher matcher = HEX.matcher(input == null ? "" : input);
StringBuffer out = new StringBuffer();
while (matcher.find()) matcher.appendReplacement(out, Matcher.quoteReplacement(net.md_5.bungee.api.ChatColor.of("#" + matcher.group(1)).toString()));
matcher.appendTail(out);
return ChatColor.translateAlternateColorCodes('&', out.toString());
}
public String text(String input, Map<String, String> vars) {
String result = input == null ? "" : input;
result = result.replace("%prefix%", getConfig().getString("prefix", ""));
for (Map.Entry<String, String> entry : vars.entrySet()) result = result.replace("%" + entry.getKey() + "%", entry.getValue());
return color(result);
}
public void message(CommandSender sender, String key, Map<String, String> vars) {
sender.sendMessage(text(getConfig().getString("messages." + key, key), vars));
}
public void message(CommandSender sender, String key) { message(sender, key, Map.of()); }
public String amount(double value) {
if (!getConfig().getBoolean("format", true)) return new DecimalFormat("#,##0.##").format(value);
List<String> suffixes = getConfig().getStringList("format-in");
int i = -1;
while (value >= 1000 && i + 1 < suffixes.size()) { value /= 1000; i++; }
return new DecimalFormat("0.##").format(value) + (i < 0 ? "" : suffixes.get(i));
}
public String name(UUID id) {
if (id.equals(ADMIN)) return "Admin";
String name = Bukkit.getOfflinePlayer(id).getName();
return name == null ? id.toString() : name;
}
public Map<String, String> vars(Bounty b, double amount) {
return Map.of("player", name(b.target), "target", name(b.target), "amount", amount(amount), "bounty_admin", String.join(", ", b.contributions.keySet().stream().map(this::name).toList()));
}
public void sound(Player p, String key) {
String path = "sounds." + key;
if (getConfig().getBoolean("sounds.enabled", true) && getConfig().getBoolean(path + ".enabled", true)) {
String sound = getConfig().getString(path + ".sound", "");
p.playSound(p.getLocation(), sound.toLowerCase(Locale.ROOT).replace('_', '.'), (float) getConfig().getDouble(path + ".volume", 1), (float) getConfig().getDouble(path + ".pitch", 1));
}
}
public void broadcast(String type, Player actor, Bounty bounty, double value) {
String path = "broadcast." + type;
if (!getConfig().getBoolean(path + ".enabled", true)) return;
Map<String, String> vars = new HashMap<>(vars(bounty, value));
vars.put("player", actor.getName());
for (Player online : Bukkit.getOnlinePlayers()) {
if (muted.contains(online.getUniqueId())) continue;
for (String line : getConfig().getStringList(path + ".message")) {
String message = text(line, vars);
if (getConfig().getBoolean(path + ".chat", true)) online.sendMessage(message);
if (getConfig().getBoolean(path + ".actionbar", false)) online.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message));
}
}
}
public boolean claim(Player killer, UUID target) {
Bounty bounty = bounties.get(target);
if (bounty == null || killer.getUniqueId().equals(target)) return false;
double total = bounty.total();
if (!economy.depositPlayer(killer, total).transactionSuccess()) { message(killer, "transaction-failed"); return false; }
bounties.remove(target);
if (!save()) {
bounties.put(target, bounty);
if (!economy.withdrawPlayer(killer, total).transactionSuccess()) getLogger().severe("CRITICAL: failed to reverse payout to " + killer.getUniqueId());
message(killer, "storage-failed"); return false;
}
message(killer, "bounty-claim", vars(bounty, total));
broadcast("bounty-claim", killer, bounty, total);
sound(killer, "bounty-claimed"); menu.refreshAll();
return true;
}
@EventHandler public void death(PlayerDeathEvent event) {
Player killer = event.getEntity().getKiller();
if (killer != null && bounties.containsKey(event.getEntity().getUniqueId())) claim(killer, event.getEntity().getUniqueId());
}
}