Formatting

This commit is contained in:
NotAlexNoyle committed 2026-02-11 13:34:21 -05:00
1 parent b9887b5476
commit a66a1123f2
4 files changed
+216 -105

No files matched your search

@@ -132,8 +132,9 @@ public class BountySetCmd {
// Confirmation
sender.sendMessage(plugin.getLang().getColored("command.bounty.set.success").content()
.replace("{bounty}", String.valueOf(chargeAndBountyAmount)).replace("{total}", String.valueOf(totalBounty))
.replace("{target}", target.getName()).replace("{player}", sender.getName()));
.replace("{bounty}", String.valueOf(chargeAndBountyAmount))
.replace("{total}", String.valueOf(totalBounty)).replace("{target}", target.getName())
.replace("{player}", sender.getName()));
// Announce
final String extra;
@@ -153,8 +154,9 @@ public class BountySetCmd {
Bukkit.getOnlinePlayers()
.forEach((Player player) -> UtilitiesOG.trueogMessage(player,
plugin.getLang().getColored("command.bounty.set.announce").content()
.replace("{bounty}", String.valueOf(chargeAndBountyAmount)).replace("{target}", target.getName())
.replace("{player}", sender.getName()).replace("{extra}", extra)));
.replace("{bounty}", String.valueOf(chargeAndBountyAmount))
.replace("{target}", target.getName()).replace("{player}", sender.getName())
.replace("{extra}", extra)));
}
@@ -19,116 +19,164 @@ import net.trueog.diamondbankog.DiamondBankException.PlayerNotOnlineException;
public class DiamondBankOGHook implements EconomyHook {
private static final long SHARDS_PER_DIAMOND = 9L;
private static final long SHARDS_PER_DIAMOND = 9L;
private final PlayerBountiesOG plugin;
private final DiamondBankAPIJava diamondBank;
private final PlayerBountiesOG plugin;
private final DiamondBankAPIJava diamondBank;
public DiamondBankOGHook(PlayerBountiesOG plugin, DiamondBankAPIJava diamondBankAPITransporter) {
this.plugin = plugin;
this.diamondBank = diamondBankAPITransporter;
}
public DiamondBankOGHook(PlayerBountiesOG plugin, DiamondBankAPIJava diamondBankAPITransporter) {
public boolean init() {
return this.plugin.getServer().getPluginManager().getPlugin("Vault") != null;
}
this.plugin = plugin;
this.diamondBank = diamondBankAPITransporter;
@Override
public boolean isValid() {
return diamondBank != null;
}
}
@Override
public void giveEco(Player player, Player victim, double amount) {
long diamonds = toWholeDiamonds(amount);
if (diamonds <= 0L) {
return;
}
public boolean init() {
long shards;
try {
shards = Math.multiplyExact(diamonds, SHARDS_PER_DIAMOND);
} catch (ArithmeticException e) {
PlayerBountiesOG.disableSelf("[PlayerBounties-OG] Overflow converting diamonds to shards for payout.");
return;
}
return this.plugin.getServer().getPluginManager().getPlugin("Vault") != null;
try {
diamondBank.addToPlayerBankShards(
player.getUniqueId(),
shards,
"Player " + player.getName() + " defeated " + victim.getName() + " and earned a bounty of " + diamonds + " Diamonds.",
"Plugin: PlayerBounties-OG"
);
} catch (EconomyDisabledException | DatabaseException error) {
PlayerBountiesOG.disableSelf("[PlayerBounties-OG] Paying " + player.getName() + " for killing " + victim.getName()
+ " failed with error: " + error.getMessage());
error.printStackTrace();
}
}
}
@Override
public void takeEco(Player player, Player victim, double amount) {
this.takeEco(player, victim, amount, false);
}
@Override
public boolean isValid() {
@Override
public boolean takeEco(Player player, Player victim, double amount, boolean force) {
long diamonds = toWholeDiamonds(amount);
if (diamonds <= 0L) {
return true;
}
return diamondBank != null;
try {
diamondBank.consumeFromPlayer(
player.getUniqueId(),
diamonds,
"Player " + player.getName() + " contributed " + diamonds + " Diamonds toward a bounty on " + victim.getName() + ".",
"Plugin: PlayerBounties-OG"
);
return true;
} catch (InsufficientFundsException | InsufficientInventorySpaceException | InvalidPlayerException | PlayerNotOnlineException e) {
return false;
} catch (EconomyDisabledException | DatabaseException e) {
PlayerBountiesOG.disableSelf("[PlayerBounties-OG] Collecting " + diamonds + " Diamonds from " + player.getName()
+ " for a bounty on " + victim.getName() + " failed with error: " + e.getMessage());
e.printStackTrace();
return false;
}
}
}
public CompletableFuture<Double> getBalanceAsync(UUID uuid) {
CompletableFuture<Double> future = new CompletableFuture<>();
@Override
public void giveEco(Player player, Player victim, double amount) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
long totalShards = diamondBank.getTotalShards(uuid);
future.complete(totalShards / (double) SHARDS_PER_DIAMOND);
} catch (DiamondBankException.EconomyDisabledException error) {
PlayerBountiesOG.disableSelf("The DiamondBank-OG economy is disabled — disabling " + plugin.getName() + "!");
future.completeExceptionally(error);
} catch (DiamondBankException.DatabaseException error) {
PlayerBountiesOG.disableSelf("DiamondBank-OG database error — disabling " + plugin.getName()
+ ". Cause: " + error.getMessage());
future.completeExceptionally(error);
} catch (Throwable t) {
future.completeExceptionally(t);
}
});
long diamonds = toWholeDiamonds(amount);
if (diamonds <= 0L) {
return future;
}
return;
public CompletableFuture<Double> getBalanceAsync(Player player) {
return getBalanceAsync(player.getUniqueId());
}
}
private static long toWholeDiamonds(double diamonds) {
if (!Double.isFinite(diamonds) || diamonds <= 0.0) {
return 0L;
}
long whole = (long) Math.floor(diamonds);
return Math.max(0L, whole);
}
long shards;
try {
shards = Math.multiplyExact(diamonds, SHARDS_PER_DIAMOND);
} catch (ArithmeticException e) {
PlayerBountiesOG.disableSelf("[PlayerBounties-OG] Overflow converting diamonds to shards for payout.");
return;
}
try {
diamondBank
.addToPlayerBankShards(
player.getUniqueId(), shards, "Player " + player.getName() + " defeated " + victim.getName()
+ " and earned a bounty of " + diamonds + " Diamonds.",
"Plugin: PlayerBounties-OG");
} catch (EconomyDisabledException | DatabaseException error) {
PlayerBountiesOG.disableSelf("[PlayerBounties-OG] Paying " + player.getName() + " for killing "
+ victim.getName() + " failed with error: " + error.getMessage());
error.printStackTrace();
}
}
@Override
public void takeEco(Player player, Player victim, double amount) {
this.takeEco(player, victim, amount, false);
}
@Override
public boolean takeEco(Player player, Player victim, double amount, boolean force) {
long diamonds = toWholeDiamonds(amount);
if (diamonds <= 0L) {
return true;
}
try {
diamondBank.consumeFromPlayer(player.getUniqueId(), diamonds, "Player " + player.getName() + " contributed "
+ diamonds + " Diamonds toward a bounty on " + victim.getName() + ".", "Plugin: PlayerBounties-OG");
return true;
} catch (InsufficientFundsException | InsufficientInventorySpaceException | InvalidPlayerException
| PlayerNotOnlineException e)
{
return false;
} catch (EconomyDisabledException | DatabaseException e) {
PlayerBountiesOG
.disableSelf("[PlayerBounties-OG] Collecting " + diamonds + " Diamonds from " + player.getName()
+ " for a bounty on " + victim.getName() + " failed with error: " + e.getMessage());
e.printStackTrace();
return false;
}
}
public CompletableFuture<Double> getBalanceAsync(UUID uuid) {
CompletableFuture<Double> future = new CompletableFuture<>();
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
long totalShards = diamondBank.getTotalShards(uuid);
future.complete(totalShards / (double) SHARDS_PER_DIAMOND);
} catch (DiamondBankException.EconomyDisabledException error) {
PlayerBountiesOG
.disableSelf("The DiamondBank-OG economy is disabled — disabling " + plugin.getName() + "!");
future.completeExceptionally(error);
} catch (DiamondBankException.DatabaseException error) {
PlayerBountiesOG.disableSelf("DiamondBank-OG database error — disabling " + plugin.getName()
+ ". Cause: " + error.getMessage());
future.completeExceptionally(error);
} catch (Throwable t) {
future.completeExceptionally(t);
}
});
return future;
}
public CompletableFuture<Double> getBalanceAsync(Player player) {
return getBalanceAsync(player.getUniqueId());
}
private static long toWholeDiamonds(double diamonds) {
if (!Double.isFinite(diamonds) || diamonds <= 0.0) {
return 0L;
}
long whole = (long) Math.floor(diamonds);
return Math.max(0L, whole);
}
}
@@ -21,61 +21,81 @@ public class DeathListener implements Listener {
private final PlayerBountiesOG plugin;
public DeathListener(PlayerBountiesOG plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.HIGH)
public void onDeath(PlayerDeathEvent event) {
final Player victim = event.getEntity();
final Player killer = victim.getKiller();
if (killer == null) {
return;
}
if (!killer.hasPermission("playerbountiesplus.event.claim")) {
killer.sendMessage(plugin.getLang().getColored("death.no-permission"));
return;
}
final BountyDataManager bountyDataManager = this.plugin.getBountyDataManager();
final UUID victimId = victim.getUniqueId();
if (!bountyDataManager.hasBounty(victimId)) {
return;
}
final int bounty = bountyDataManager.getBounty(victimId);
final TeamHook teamHook = this.plugin.getTeamHook();
if (teamHook != null && teamHook.isFriendly(killer, victim)) {
killer.sendMessage(plugin.getLang().getColored("death.same-team"));
return;
}
final BountyClaimEvent claimEvent = new BountyClaimEvent(killer, victim, bounty);
this.plugin.getServer().getPluginManager().callEvent(claimEvent);
if (claimEvent.isCancelled()) {
return;
}
if (plugin.getConfig().getBoolean("bounty-claimable", true)) {
final double claimMultiplier = plugin.getConfig().getDouble("bounty-claim-multiplier", 1.0);
final double awardedAmount = bounty * claimMultiplier;
if (awardedAmount > 0) {
this.plugin.getEcoHook().giveEco(killer, victim, awardedAmount);
}
}
if (plugin.getConfig().getBoolean("bounty-claimed-announce", true)) {
Bukkit.getOnlinePlayers().forEach((Player player) ->
UtilitiesOG.trueogMessage(player, killer.getName() + "killed " + victim.getName()
+ " and earned the " + String.valueOf(bounty) + " Diamond bounty!"));
Bukkit.getOnlinePlayers().forEach((Player player) -> UtilitiesOG.trueogMessage(player, killer.getName()
+ "killed " + victim.getName() + " and earned the " + String.valueOf(bounty) + " Diamond bounty!"));
}
bountyDataManager.removeBounty(victimId);
bountyDataManager.saveBountiesAsync();
}
}
@@ -25,76 +25,117 @@ public class BountyDataManager {
private final ConcurrentHashMap<UUID, Integer> bounties;
public BountyDataManager(PlayerBountiesOG plugin) {
this.plugin = plugin;
this.bounties = new ConcurrentHashMap<>();
}
public void init() {
final String bountiesFileName = "bounties.yml";
bountiesFile = new File(this.plugin.getDataFolder(), bountiesFileName);
if (!bountiesFile.exists()) {
this.plugin.saveResource(bountiesFileName, false);
}
bountiesConfig = YamlConfiguration.loadConfiguration(bountiesFile);
final Set<String> keys = bountiesConfig.getKeys(false);
keys.forEach(key -> {
try {
this.bounties.put(UUID.fromString(key), bountiesConfig.getInt(key));
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public ConcurrentHashMap<UUID, Integer> getBounties() {
return this.bounties;
}
public boolean hasBounty(UUID player) {
return this.bounties.containsKey(player) && this.bounties.get(player) > 0;
}
public void setBounty(UUID player, int amount) {
this.bounties.put(player, amount);
}
public int getBounty(UUID player) {
return this.bounties.getOrDefault(player, 0);
}
public void removeBounty(UUID player) {
this.bounties.remove(player);
}
public void saveBounties() {
synchronized (this.savingFileLock) {
final Set<String> keys = bountiesConfig.getKeys(false);
keys.forEach(key -> bountiesConfig.set(key, null));
this.bounties.entrySet().forEach(entry ->
this.bountiesConfig.set(entry.getKey().toString(), entry.getValue()));
this.bounties.entrySet()
.forEach(entry -> this.bountiesConfig.set(entry.getKey().toString(), entry.getValue()));
try {
this.bountiesConfig.save(this.bountiesFile);
} catch (IOException error) {
error.printStackTrace();
}
}
}
public void saveBountiesAsync() {
if (!this.savingAsync.compareAndSet(false, true)) {
return;
}
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
try {
this.saveBounties();
} finally {
this.savingAsync.set(false);
}
});
}
}