Update GUI formatting

This commit is contained in:
NotAlexNoyle committed 2026-03-09 04:47:59 -04:00
1 parent 7cb97e7c1d
commit a84b72071a
4 files changed
+480 -487

No files matched your search

@@ -4,6 +4,7 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@@ -23,102 +24,97 @@ import net.trueog.utilitiesog.UtilitiesOG;
public class BountyCommand implements CommandExecutor, TabCompleter {
private final PlayerBountiesOG plugin;
private final DiamondBankAPIJava diamondBankAPI;
private final PlayerBountiesOG plugin;
private final DiamondBankAPIJava diamondBankAPI;
public BountyCommand(PlayerBountiesOG plugin, DiamondBankAPIJava diamondBankAPI) {
public BountyCommand(PlayerBountiesOG plugin, DiamondBankAPIJava diamondBankAPI) {
this.plugin = plugin;
this.diamondBankAPI = diamondBankAPI;
this.plugin = plugin;
this.diamondBankAPI = diamondBankAPI;
}
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) {
final Player player;
if (!(sender instanceof Player)) {
if (!(sender instanceof Player)) {
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), "&cERROR: Only players can use this command.");
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), "&cERROR: Only players can use this command.");
return true;
return true;
} else {
}
player = (Player) sender;
if (args.length < 1) {
}
// If "/bounty" is run without a valid subcommand, treat it as "/bounty top".
return BountyTopCmd.handleCmd(plugin, diamondBankAPI, sender, cmd, cmdName, args);
if (args.length < 1) {
}
UtilitiesOG.trueogMessage(player, plugin.getLang().getColored("command.bounty.no-action"));
return true;
final String action = StringUtils.lowerCase(args[0]);
switch (action) {
}
case "set" -> {
final String action = args[0].toLowerCase();
switch (action) {
return BountySetCmd.handleCmd(plugin, sender, cmd, cmdName, args);
case "set" -> {
}
return BountySetCmd.handleCmd(plugin, sender, cmd, cmdName, args);
case "top" -> {
}
return BountyTopCmd.handleCmd(plugin, diamondBankAPI, sender, cmd, cmdName, args);
case "top" -> {
}
return BountyTopCmd.handleCmd(plugin, diamondBankAPI, sender, cmd, cmdName, args);
case "check" -> {
}
return BountyCheckCmd.handleCmd(plugin, sender, cmd, cmdName, args);
case "check" -> {
}
default -> {
return BountyCheckCmd.handleCmd(plugin, sender, cmd, cmdName, args);
// If "/bounty" is run without a valid subcommand, treat it as "/bounty top".
return BountyTopCmd.handleCmd(plugin, diamondBankAPI, sender, cmd, cmdName, args);
}
}
default -> {
}
// If "/bounty" is run without a valid subcommand, treat it as "/bounty top".
return BountyTopCmd.handleCmd(plugin, diamondBankAPI, sender, cmd, cmdName, args);
}
}
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args)
{
}
if (args.length == 1) {
}
return Lists.newArrayList("set", "top", "check").stream()
.filter(action -> StringUtils.startsWith(action, StringUtils.lowerCase(args[0]))).collect(Collectors.toList());
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args)
{
}
else if (args.length > 1) {
if (args.length == 1) {
final String subCommand = StringUtils.lowerCase(args[0]);
return switch (subCommand) {
return Lists.newArrayList("set", "top", "check").stream()
.filter(action -> action.startsWith(args[0].toLowerCase())).collect(Collectors.toList());
case "set" -> BountySetCmd.onTabComplete(sender, args).stream()
.filter(suggestion -> StringUtils.startsWith(StringUtils.lowerCase(suggestion), StringUtils.lowerCase(args[1])))
.collect(Collectors.toList());
case "check" -> BountyCheckCmd.onTabComplete(sender, args).stream()
.filter(suggestion -> StringUtils.startsWith(StringUtils.lowerCase(suggestion), StringUtils.lowerCase(args[1])))
.collect(Collectors.toList());
case "top" -> Collections.emptyList();
default -> Collections.emptyList();
} else if (args.length > 1) {
};
final String subCommand = args[0].toLowerCase();
return switch (subCommand) {
}
case "set" -> BountySetCmd.onTabComplete(sender, args).stream()
.filter(suggestion -> suggestion.toLowerCase().startsWith(args[1].toLowerCase()))
.collect(Collectors.toList());
case "check" -> BountyCheckCmd.onTabComplete(sender, args).stream()
.filter(suggestion -> suggestion.toLowerCase().startsWith(args[1].toLowerCase()))
.collect(Collectors.toList());
case "top" -> Collections.emptyList();
default -> Collections.emptyList();
return Collections.emptyList();
};
}
return Collections.emptyList();
}
}
}
@@ -20,269 +20,269 @@ import net.trueog.utilitiesog.UtilitiesOG;
public class BountySetCmd {
public static boolean handleCmd(PlayerBountiesOG plugin, CommandSender sender, Command cmd, String cmdName,
String[] args)
{
public static boolean handleCmd(PlayerBountiesOG plugin, CommandSender sender, Command cmd, String cmdName,
String[] args)
{
if (args.length < 3) {
if (args.length < 3) {
final String missingArgumentsMessage = plugin.getLang().getColored("command.bounty.set.missing-args");
if (!(sender instanceof Player)) {
final String missingArgumentsMessage = plugin.getLang().getColored("command.bounty.set.missing-args");
if (!(sender instanceof Player)) {
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), missingArgumentsMessage);
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), missingArgumentsMessage);
} else {
} else {
UtilitiesOG.trueogMessage((Player) sender, missingArgumentsMessage);
UtilitiesOG.trueogMessage((Player) sender, missingArgumentsMessage);
}
}
return true;
return true;
}
}
if (sender instanceof Player && !sender.hasPermission("playerbountiesplus.command.bounty.set")) {
if (sender instanceof Player && !sender.hasPermission("playerbountiesplus.command.bounty.set")) {
final String noPerm = plugin.getLang().getColored("command.no-permission");
final String noPermDetailed = plugin.getLang().getColored("command.no-permission-detailed")
.replace("{no-permission-msg}", noPerm)
.replace("{permission}", "playerbountiesplus.command.bounty.set");
if (!(sender instanceof Player)) {
final String noPerm = plugin.getLang().getColored("command.no-permission");
final String noPermDetailed = plugin.getLang().getColored("command.no-permission-detailed")
.replace("{no-permission-msg}", noPerm)
.replace("{permission}", "playerbountiesplus.command.bounty.set");
if (!(sender instanceof Player)) {
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), noPermDetailed);
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), noPermDetailed);
} else {
} else {
UtilitiesOG.trueogMessage((Player) sender, noPermDetailed);
UtilitiesOG.trueogMessage((Player) sender, noPermDetailed);
}
}
return true;
return true;
}
}
final String playerNameArg = args[1];
final double parsedAmount;
try {
final String playerNameArg = args[1];
final double parsedAmount;
try {
parsedAmount = Integer.parseInt(args[2]);
parsedAmount = Integer.parseInt(args[2]);
} catch (NumberFormatException numberFormatException) {
} catch (NumberFormatException numberFormatException) {
final String notNumberMessage = plugin.getLang().getColored("command.bounty.set.amount-nan");
if (!(sender instanceof Player)) {
final String notNumberMessage = plugin.getLang().getColored("command.bounty.set.amount-nan");
if (!(sender instanceof Player)) {
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), notNumberMessage);
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), notNumberMessage);
} else {
} else {
UtilitiesOG.trueogMessage((Player) sender, notNumberMessage);
UtilitiesOG.trueogMessage((Player) sender, notNumberMessage);
}
}
return true;
return true;
}
}
final Player target = plugin.getServer().getPlayerExact(playerNameArg);
if (target == null) {
final Player target = plugin.getServer().getPlayerExact(playerNameArg);
if (target == null) {
final String playerNotFoundMessage = plugin.getLang().getColored("command.bounty.set.player-not-found");
if (!(sender instanceof Player)) {
final String playerNotFoundMessage = plugin.getLang().getColored("command.bounty.set.player-not-found");
if (!(sender instanceof Player)) {
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), playerNotFoundMessage);
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), playerNotFoundMessage);
} else {
} else {
UtilitiesOG.trueogMessage((Player) sender, playerNotFoundMessage);
UtilitiesOG.trueogMessage((Player) sender, playerNotFoundMessage);
}
}
return true;
return true;
}
}
// Check limits.
final double minimum = plugin.getConfig().getDouble("bounty-minimum", 1.0);
if (parsedAmount < minimum) {
// Check limits.
final double minimum = plugin.getConfig().getDouble("bounty-minimum", 1.0);
if (parsedAmount < minimum) {
final String underMinimumMessage = plugin.getLang().getColored("command.bounty.set.under-minimum")
.replace("{minimum}", String.valueOf(minimum));
if (!(sender instanceof Player)) {
final String underMinimumMessage = plugin.getLang().getColored("command.bounty.set.under-minimum")
.replace("{minimum}", String.valueOf(minimum));
if (!(sender instanceof Player)) {
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), underMinimumMessage);
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), underMinimumMessage);
} else {
} else {
UtilitiesOG.trueogMessage((Player) sender, underMinimumMessage);
UtilitiesOG.trueogMessage((Player) sender, underMinimumMessage);
}
}
return true;
return true;
}
}
final double maximum = plugin.getConfig().getDouble("bounty-maximum", 1000000.0);
if (parsedAmount > maximum) {
final double maximum = plugin.getConfig().getDouble("bounty-maximum", 1000000.0);
if (parsedAmount > maximum) {
final String overMaximumMessage = plugin.getLang().getColored("command.bounty.set.over-maximum")
.replace("{maximum}", String.valueOf(maximum));
if (!(sender instanceof Player)) {
final String overMaximumMessage = plugin.getLang().getColored("command.bounty.set.over-maximum")
.replace("{maximum}", String.valueOf(maximum));
if (!(sender instanceof Player)) {
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), overMaximumMessage);
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), overMaximumMessage);
} else {
} else {
UtilitiesOG.trueogMessage((Player) sender, overMaximumMessage);
UtilitiesOG.trueogMessage((Player) sender, overMaximumMessage);
}
}
return true;
return true;
}
}
// Apply bounty multiplier.
double amount = parsedAmount * plugin.getConfig().getDouble("bounty-multiplier", 1.0);
// Apply bounty multiplier.
double amount = parsedAmount * plugin.getConfig().getDouble("bounty-multiplier", 1.0);
// Trigger bounty set event.
final BountySetEvent event = new BountySetEvent(sender instanceof Player ? (Player) sender : null, target,
(float) amount);
plugin.getServer().getPluginManager().callEvent(event);
amount = event.getAmount();
// Trigger bounty set event.
final BountySetEvent event = new BountySetEvent(sender instanceof Player ? (Player) sender : null, target,
(float) amount);
plugin.getServer().getPluginManager().callEvent(event);
amount = event.getAmount();
// Check if event was cancelled.
if (event.isCancelled()) {
// Check if event was cancelled.
if (event.isCancelled()) {
final String cancelledBounty = plugin.getLang().getColored("command.bounty.set.cancelled");
if (!(sender instanceof Player)) {
final String cancelledBounty = plugin.getLang().getColored("command.bounty.set.cancelled");
if (!(sender instanceof Player)) {
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), cancelledBounty);
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), cancelledBounty);
} else {
} else {
UtilitiesOG.trueogMessage((Player) sender, cancelledBounty);
UtilitiesOG.trueogMessage((Player) sender, cancelledBounty);
}
}
return true;
return true;
}
}
// Sanity check final amount.
if (amount <= 0) {
// Sanity check final amount.
if (amount <= 0) {
final String invalidValueMessage = plugin.getLang().getColored("command.bounty.set.internal-invalid-value");
if (!(sender instanceof Player)) {
final String invalidValueMessage = plugin.getLang().getColored("command.bounty.set.internal-invalid-value");
if (!(sender instanceof Player)) {
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), invalidValueMessage);
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), invalidValueMessage);
} else {
} else {
UtilitiesOG.trueogMessage((Player) sender, invalidValueMessage);
UtilitiesOG.trueogMessage((Player) sender, invalidValueMessage);
}
}
return true;
return true;
}
}
// Check economy. Only charge if sender is a player.
if (sender instanceof Player player) {
// Check economy. Only charge if sender is a player.
if (sender instanceof Player player) {
final boolean allowed = plugin.getEcoHook().takeEco(player, target, amount);
final boolean allowed = plugin.getEcoHook().takeEco(player, target, amount);
if (!allowed) {
if (!allowed) {
final String notEnoughDiamondsMessage = plugin.getLang()
.getColored("command.bounty.set.not-enough-money");
if (!(sender instanceof Player)) {
final String notEnoughDiamondsMessage = plugin.getLang()
.getColored("command.bounty.set.not-enough-money");
if (!(sender instanceof Player)) {
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), notEnoughDiamondsMessage);
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), notEnoughDiamondsMessage);
} else {
} else {
UtilitiesOG.trueogMessage((Player) sender, notEnoughDiamondsMessage);
UtilitiesOG.trueogMessage((Player) sender, notEnoughDiamondsMessage);
}
}
return true;
return true;
}
}
}
}
final UUID playerUUID = target.getUniqueId();
final UUID playerUUID = target.getUniqueId();
// Calculate total bounty including previous bounties.
final BountyDataManager bountyDataManager = plugin.getBountyDataManager();
final double bountyAlreadyPresent = bountyDataManager.getBounty(playerUUID);
final double chargeAndBountyAmount = (int) amount;
final double totalBounty = chargeAndBountyAmount + bountyAlreadyPresent;
bountyDataManager.setBounty(playerUUID, totalBounty);
// Calculate total bounty including previous bounties.
final BountyDataManager bountyDataManager = plugin.getBountyDataManager();
final double bountyAlreadyPresent = bountyDataManager.getBounty(playerUUID);
final double chargeAndBountyAmount = (int) amount;
final double totalBounty = chargeAndBountyAmount + bountyAlreadyPresent;
bountyDataManager.setBounty(playerUUID, totalBounty);
// Confirmation.
final String bountySetSuccessMessage = plugin.getLang().getColored("command.bounty.set.success")
.replace("{bounty}", String.valueOf(chargeAndBountyAmount))
.replace("{total}", String.valueOf(totalBounty)).replace("{target}", target.getName())
.replace("{player}", sender.getName());
if (!(sender instanceof Player)) {
// Confirmation.
final String bountySetSuccessMessage = plugin.getLang().getColored("command.bounty.set.success")
.replace("{bounty}", String.valueOf(chargeAndBountyAmount))
.replace("{total}", String.valueOf(totalBounty)).replace("{target}", target.getName())
.replace("{player}", sender.getName());
if (!(sender instanceof Player)) {
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), bountySetSuccessMessage);
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), bountySetSuccessMessage);
} else {
} else {
UtilitiesOG.trueogMessage((Player) sender, bountySetSuccessMessage);
UtilitiesOG.trueogMessage((Player) sender, bountySetSuccessMessage);
}
}
// Announcement.
final String extra;
if (bountyAlreadyPresent == 0) {
// Announcement.
final String extra;
if (bountyAlreadyPresent == 0) {
extra = "";
extra = "";
} else {
} else {
extra = plugin.getLang().getColored("command.bounty.set.announce-extra").replace("{total}",
String.valueOf(totalBounty));
extra = plugin.getLang().getColored("command.bounty.set.announce-extra").replace("{total}",
String.valueOf(totalBounty));
}
}
if (plugin.getConfig().getBoolean("bounty-placed-announce", true)) {
if (plugin.getConfig().getBoolean("bounty-placed-announce", true)) {
Bukkit.getOnlinePlayers()
.forEach((Player player) -> UtilitiesOG.trueogMessage(player,
plugin.getLang().getColored("command.bounty.set.announce")
.replace("{bounty}", String.valueOf(chargeAndBountyAmount))
.replace("{target}", target.getName()).replace("{player}", sender.getName())
.replace("{extra}", extra)));
Bukkit.getOnlinePlayers()
.forEach((Player player) -> UtilitiesOG.trueogMessage(player, PlayerBountiesOG.getPrefix() + "&a" +
plugin.getLang().getColored("command.bounty.set.announce")
.replace("{bounty}", String.valueOf(chargeAndBountyAmount))
.replace("{target}", target.getName()).replace("{player}", sender.getName())
.replace("{extra}", extra)));
}
}
bountyDataManager.saveBountiesAsync();
bountyDataManager.saveBountiesAsync();
return true;
return true;
}
}
@Nullable
public static List<String> onTabComplete(@NotNull CommandSender sender, @NotNull String[] args) {
@Nullable
public static List<String> onTabComplete(@NotNull CommandSender sender, @NotNull String[] args) {
if (args.length == 2) {
if (args.length == 2) {
// Suggest online player names for the username.
return sender.getServer().getOnlinePlayers().stream().map(Player::getName)
.filter(name -> name.toLowerCase().startsWith(args[1].toLowerCase())).collect(Collectors.toList());
// Suggest online player names for the username.
return sender.getServer().getOnlinePlayers().stream().map(Player::getName)
.filter(name -> name.toLowerCase().startsWith(args[1].toLowerCase())).collect(Collectors.toList());
} else if (args.length == 3) {
} else if (args.length == 3) {
// Suggest a placeholder for the amount.
return Collections.singletonList("<amount>");
// Suggest a placeholder for the amount.
return Collections.singletonList("<amount>");
}
}
return Collections.emptyList();
return Collections.emptyList();
}
}
}
@@ -6,6 +6,7 @@ import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@@ -19,115 +20,115 @@ import net.trueog.utilitiesog.UtilitiesOG;
public class BountyTopCmd {
public static boolean handleCmd(PlayerBountiesOG plugin, DiamondBankAPIJava diamondBankAPI, CommandSender sender,
Command cmd, String cmdName, String[] args)
{
public static boolean handleCmd(PlayerBountiesOG plugin, DiamondBankAPIJava diamondBankAPI, CommandSender sender,
Command cmd, String cmdName, String[] args)
{
if (sender instanceof Player player) {
if (sender instanceof Player player) {
new MainBountyGui(plugin, player, () -> buildGuiEntries(plugin), diamondBankAPI).open();
new MainBountyGui(plugin, player, () -> buildGuiEntries(plugin), diamondBankAPI).open();
return true;
return true;
}
}
sendTextTopList(plugin, diamondBankAPI, sender);
sendTextTopList(plugin, diamondBankAPI, sender);
return true;
return true;
}
}
private static List<MainBountyGui.BountyGuiEntry> buildGuiEntries(PlayerBountiesOG plugin) {
private static List<MainBountyGui.BountyGuiEntry> buildGuiEntries(PlayerBountiesOG plugin) {
final Set<Map.Entry<UUID, Double>> bountiesSet = plugin.getBountyDataManager().getBounties().entrySet();
final List<Map.Entry<UUID, Double>> bounties = new ArrayList<>(bountiesSet);
final Set<Map.Entry<UUID, Double>> bountiesSet = plugin.getBountyDataManager().getBounties().entrySet();
final List<Map.Entry<UUID, Double>> bounties = new ArrayList<>(bountiesSet);
bounties.sort((a, b) -> Double.compare(b.getValue(), a.getValue()));
bounties.sort((a, b) -> Double.compare(b.getValue(), a.getValue()));
final List<MainBountyGui.BountyGuiEntry> entries = new ArrayList<>();
for (Map.Entry<UUID, Double> entry : bounties) {
final List<MainBountyGui.BountyGuiEntry> entries = new ArrayList<>();
for (Map.Entry<UUID, Double> entry : bounties) {
if (entry.getValue() == null || entry.getValue() <= 0D) {
if (entry.getValue() == null || entry.getValue() <= 0D) {
continue;
continue;
}
}
final OfflinePlayer offlinePlayer = plugin.getServer().getOfflinePlayer(entry.getKey());
String targetName = offlinePlayer.getName();
final OfflinePlayer offlinePlayer = plugin.getServer().getOfflinePlayer(entry.getKey());
String targetName = offlinePlayer.getName();
if (targetName == null || targetName.isBlank()) {
if (targetName == null || targetName.isBlank()) {
targetName = "Unknown Player";
targetName = "Unknown Player";
}
}
entries.add(new MainBountyGui.BountyGuiEntry(entry.getKey(), targetName, entry.getValue()));
entries.add(new MainBountyGui.BountyGuiEntry(entry.getKey(), targetName, entry.getValue()));
}
}
return entries;
return entries;
}
}
private static void sendTextTopList(PlayerBountiesOG plugin, DiamondBankAPIJava diamondBankAPI,
CommandSender sender)
{
private static void sendTextTopList(PlayerBountiesOG plugin, DiamondBankAPIJava diamondBankAPI,
CommandSender sender)
{
final Set<Map.Entry<UUID, Double>> bountiesSet = plugin.getBountyDataManager().getBounties().entrySet();
final List<Map.Entry<UUID, Double>> bounties = new ArrayList<>(bountiesSet);
final Set<Map.Entry<UUID, Double>> bountiesSet = plugin.getBountyDataManager().getBounties().entrySet();
final List<Map.Entry<UUID, Double>> bounties = new ArrayList<>(bountiesSet);
bounties.sort((a, b) -> Double.compare(b.getValue(), a.getValue()));
bounties.sort((a, b) -> Double.compare(b.getValue(), a.getValue()));
final StringBuilder strb = new StringBuilder();
strb.append(plugin.getLang().getColored("command.bounty.top.top-10")).append('\n');
final StringBuilder strb = new StringBuilder();
strb.append(plugin.getLang().getColored("command.bounty.top.top-10")).append('\n');
final int bountiesSize = bounties.size();
final int maxInList = Math.min(10, bountiesSize);
if (bountiesSize > 0) {
final int bountiesSize = bounties.size();
final int maxInList = Math.min(10, bountiesSize);
if (bountiesSize > 0) {
for (int i = 0; i < maxInList; i++) {
for (int i = 0; i < maxInList; i++) {
final Map.Entry<UUID, Double> entry = bounties.get(i);
final OfflinePlayer offlinePlayer = plugin.getServer().getOfflinePlayer(entry.getKey());
final Map.Entry<UUID, Double> entry = bounties.get(i);
final OfflinePlayer offlinePlayer = plugin.getServer().getOfflinePlayer(entry.getKey());
String targetName = offlinePlayer.getName();
if (targetName == null || targetName.isBlank()) {
String targetName = offlinePlayer.getName();
if (targetName == null || targetName.isBlank()) {
targetName = "Unknown Player";
targetName = "Unknown Player";
}
}
final String formattedBounty = diamondBankAPI
.shardsToDiamonds(diamondBankAPI.diamondsToShards(entry.getValue()));
final String formattedBounty = diamondBankAPI
.shardsToDiamonds(diamondBankAPI.diamondsToShards(entry.getValue()));
strb.append("&7 - ");
strb.append(targetName);
strb.append(": ");
strb.append(formattedBounty);
strb.append('\n');
strb.append("&7 - ");
strb.append(targetName);
strb.append(": ");
strb.append(formattedBounty);
strb.append('\n');
}
}
} else {
} else {
strb.append(plugin.getLang().getColored("command.bounty.top.no-bounties")).append('\n');
strb.append(plugin.getLang().getColored("command.bounty.top.no-bounties")).append('\n');
}
}
String message = strb.toString();
message = message.substring(0, message.length() - 1);
String message = strb.toString();
message = StringUtils.substring(message, 0, message.length() - 1);
if (!(sender instanceof Player)) {
if (!(sender instanceof Player)) {
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), message);
UtilitiesOG.logToConsole(PlayerBountiesOG.getPrefix(), message);
} else {
} else {
UtilitiesOG.trueogMessage((Player) sender, message);
UtilitiesOG.trueogMessage((Player) sender, message);
}
}
}
}
}
@@ -20,339 +20,335 @@ import net.trueog.gxui.GUIItem;
public class MainBountyGui extends GUIBase {
private static final List<Integer> PYRAMID_SLOTS = List.of(4, 12, 13, 14, 20, 21, 22, 23, 24, 28, 29, 30, 31, 32,
33, 34, 36, 37, 38, 39, 40, 41, 42, 43, 44);
private static final List<Integer> PYRAMID_SLOTS = List.of(4, 12, 13, 14, 20, 21, 22, 23, 24, 28, 29, 30, 31, 32,
33, 34, 36, 37, 38, 39, 40, 41, 42, 43, 44);
private static final int PREVIOUS_PAGE_SLOT = 47;
private static final int PAGE_INFO_SLOT = 49;
private static final int NEXT_PAGE_SLOT = 51;
private static final int CLOSE_SLOT = 53;
private static final int PREVIOUS_PAGE_SLOT = 47;
private static final int PAGE_INFO_SLOT = 49;
private static final int NEXT_PAGE_SLOT = 51;
private static final int CLOSE_SLOT = 53;
private static final Pattern MINECRAFT_USERNAME_PATTERN = Pattern.compile("^[A-Za-z0-9_]{1,16}$");
private static final Pattern MINECRAFT_USERNAME_PATTERN = Pattern.compile("^[A-Za-z0-9_]{1,16}$");
private final PlayerBountiesOG plugin;
private final Player viewer;
private final Supplier<List<BountyGuiEntry>> bountySupplier;
private final DiamondBankAPIJava diamondBankAPI;
private final int page;
private final PlayerBountiesOG plugin;
private final Player viewer;
private final Supplier<List<BountyGuiEntry>> bountySupplier;
private final DiamondBankAPIJava diamondBankAPI;
private final int page;
public MainBountyGui(PlayerBountiesOG plugin, Player viewer, Supplier<List<BountyGuiEntry>> bountySupplier,
DiamondBankAPIJava diamondBankAPI)
{
public MainBountyGui(PlayerBountiesOG plugin, Player viewer, Supplier<List<BountyGuiEntry>> bountySupplier,
DiamondBankAPIJava diamondBankAPI) {
this(plugin, viewer, bountySupplier, 0, diamondBankAPI);
this(plugin, viewer, bountySupplier, 0, diamondBankAPI);
}
}
private MainBountyGui(PlayerBountiesOG plugin, Player viewer, Supplier<List<BountyGuiEntry>> bountySupplier,
int page, DiamondBankAPIJava diamondBankAPITransporter)
{
private MainBountyGui(PlayerBountiesOG plugin, Player viewer, Supplier<List<BountyGuiEntry>> bountySupplier,
int page, DiamondBankAPIJava diamondBankAPITransporter) {
super(plugin, viewer, plugin.getLang().getColored("gui.main.title"), 54, true);
super(plugin, viewer, plugin.getLang().getColored("gui.main.title"), 54, true);
this.plugin = plugin;
this.viewer = viewer;
this.bountySupplier = bountySupplier;
this.diamondBankAPI = diamondBankAPITransporter;
this.page = Math.max(0, page);
this.plugin = plugin;
this.viewer = viewer;
this.bountySupplier = bountySupplier;
this.diamondBankAPI = diamondBankAPITransporter;
this.page = Math.max(0, page);
}
}
public void open() {
public void open() {
super.open(false);
super.open(false);
}
}
@Override
public void setupItems() {
@Override
public void setupItems() {
final List<BountyGuiEntry> sortedBounties = getSortedBounties();
final int maxPage = getMaxPage(sortedBounties);
final int currentPage = Math.min(page, maxPage);
final List<BountyGuiEntry> sortedBounties = getSortedBounties();
final int maxPage = getMaxPage(sortedBounties);
final int currentPage = Math.min(page, maxPage);
addNavigationItems(currentPage, maxPage, sortedBounties.size());
addNavigationItems(currentPage, maxPage, sortedBounties.size());
if (sortedBounties.isEmpty()) {
if (sortedBounties.isEmpty()) {
addItem(22,
createStaticItem(Material.WRITABLE_BOOK, "&e&lNo Active Bounties",
List.of("&7Nobody currently has a bounty.", "&7Place one with your bounty command to get",
"&7this leaderboard moving again.")));
return;
addItem(22,
createStaticItem(Material.WRITABLE_BOOK, "&6&lNo Active Bounties",
List.of("&7Nobody currently has a bounty.", "&7Place one with your bounty command to get",
"&7this leaderboard populated again.")));
return;
}
}
final int startIndex = currentPage * PYRAMID_SLOTS.size();
final int endIndex = Math.min(startIndex + PYRAMID_SLOTS.size(), sortedBounties.size());
final int startIndex = currentPage * PYRAMID_SLOTS.size();
final int endIndex = Math.min(startIndex + PYRAMID_SLOTS.size(), sortedBounties.size());
int pyramidIndex = 0;
for (int i = startIndex; i < endIndex; i++) {
int pyramidIndex = 0;
for (int i = startIndex; i < endIndex; i++) {
final BountyGuiEntry entry = sortedBounties.get(i);
final int globalRank = i + 1;
final BountyGuiEntry entry = sortedBounties.get(i);
final int globalRank = i + 1;
addItem(PYRAMID_SLOTS.get(pyramidIndex), createBountyItem(entry, globalRank));
addItem(PYRAMID_SLOTS.get(pyramidIndex), createBountyItem(entry, globalRank));
pyramidIndex++;
pyramidIndex++;
}
}
}
}
private void addNavigationItems(int currentPage, int maxPage, int totalBounties) {
private void addNavigationItems(int currentPage, int maxPage, int totalBounties) {
if (currentPage > 0) {
if (currentPage > 0) {
addItem(PREVIOUS_PAGE_SLOT, createButtonItem(Material.ARROW, "&a&lPrevious Page",
List.of("&7Go to page &f" + currentPage), new GUIButton()
{
addItem(PREVIOUS_PAGE_SLOT, createButtonItem(Material.ARROW, "&a&lPrevious Page",
List.of("&7Go to page &f" + currentPage), new GUIButton() {
@Override
public boolean leftClick() {
@Override
public boolean leftClick() {
return openPage(currentPage - 1);
return openPage(currentPage - 1);
}
}
@Override
public boolean leftClickShift() {
@Override
public boolean leftClickShift() {
return openPage(currentPage - 1);
return openPage(currentPage - 1);
}
}
@Override
public boolean rightClick() {
@Override
public boolean rightClick() {
return openPage(currentPage - 1);
return openPage(currentPage - 1);
}
}
@Override
public boolean rightClickShift() {
@Override
public boolean rightClickShift() {
return openPage(currentPage - 1);
return openPage(currentPage - 1);
}
}
}));
}));
} else {
} else {
addItem(PREVIOUS_PAGE_SLOT,
createStaticItem(Material.GRAY_DYE, "&7Previous Page", List.of("&8Already on the first page.")));
addItem(PREVIOUS_PAGE_SLOT,
createStaticItem(Material.GRAY_DYE, "&7Previous Page", List.of("&8Already on the first page.")));
}
}
addItem(PAGE_INFO_SLOT,
createStaticItem(Material.BOOK, "&e&lPage " + (currentPage + 1) + "/" + (maxPage + 1),
List.of("&7Active bounties: &f" + totalBounties, "&7Shown per page: &f" + PYRAMID_SLOTS.size(),
"&7Sorted from highest to lowest.")));
addItem(PAGE_INFO_SLOT,
createStaticItem(Material.BOOK, "&3&lPage " + (currentPage + 1) + "/" + (maxPage + 1),
List.of("&6Active bounties: &c" + totalBounties)));
if (currentPage < maxPage) {
if (currentPage < maxPage) {
addItem(NEXT_PAGE_SLOT, createButtonItem(Material.ARROW, "&a&lNext Page",
List.of("&7Go to page &f" + (currentPage + 2)), new GUIButton()
{
addItem(NEXT_PAGE_SLOT, createButtonItem(Material.ARROW, "&a&lNext Page",
List.of("&7Go to page &f" + (currentPage + 2)), new GUIButton()
{
@Override
public boolean leftClick() {
@Override
public boolean leftClick() {
return openPage(currentPage + 1);
return openPage(currentPage + 1);
}
}
@Override
public boolean leftClickShift() {
@Override
public boolean leftClickShift() {
return openPage(currentPage + 1);
return openPage(currentPage + 1);
}
}
@Override
public boolean rightClick() {
@Override
public boolean rightClick() {
return openPage(currentPage + 1);
return openPage(currentPage + 1);
}
}
@Override
public boolean rightClickShift() {
@Override
public boolean rightClickShift() {
return openPage(currentPage + 1);
return openPage(currentPage + 1);
}
}
}));
}));
} else {
} else {
addItem(NEXT_PAGE_SLOT,
createStaticItem(Material.GRAY_DYE, "&7Next Page", List.of("&8Already on the last page.")));
addItem(NEXT_PAGE_SLOT,
createStaticItem(Material.GRAY_DYE, "&7Next Page", List.of("&8Already on the last page.")));
}
}
addItem(CLOSE_SLOT,
createButtonItem(Material.BARRIER, "&c&lClose", List.of("&7Close the bounty menu."), new GUIButton()
{
addItem(CLOSE_SLOT,
createButtonItem(Material.BARRIER, "&c&lClose", List.of("&7Close the bounty menu."), new GUIButton()
{
@Override
public boolean leftClick() {
@Override
public boolean leftClick() {
viewer.closeInventory();
viewer.closeInventory();
return true;
return true;
}
}
@Override
public boolean leftClickShift() {
@Override
public boolean leftClickShift() {
viewer.closeInventory();
viewer.closeInventory();
return true;
return true;
}
}
@Override
public boolean rightClick() {
@Override
public boolean rightClick() {
viewer.closeInventory();
viewer.closeInventory();
return true;
return true;
}
}
@Override
public boolean rightClickShift() {
@Override
public boolean rightClickShift() {
viewer.closeInventory();
viewer.closeInventory();
return true;
return true;
}
}
}));
}));
}
}
private boolean openPage(int targetPage) {
private boolean openPage(int targetPage) {
new MainBountyGui(plugin, viewer, bountySupplier, targetPage, diamondBankAPI).open();
new MainBountyGui(plugin, viewer, bountySupplier, targetPage, diamondBankAPI).open();
return true;
return true;
}
}
private GUIItem createBountyItem(BountyGuiEntry entry, int rank) {
private GUIItem createBountyItem(BountyGuiEntry entry, int rank) {
final String targetName = safeName(entry.targetName());
final ArrayList<String> lore = new ArrayList<>();
final String targetName = safeName(entry.targetName());
final ArrayList<String> lore = new ArrayList<>();
lore.add("&7Rank: &f#" + rank);
lore.add("&7Target: &f" + targetName);
lore.add("&7Bounty: &6" + formatBounty(entry.bountyDiamonds()));
lore.add("");
lore.add("&8Highest bounty sits at the top.");
lore.add("&cRank: &f#" + rank);
lore.add("&cTarget: &f" + targetName);
lore.add("&cBounty: &b" + formatBounty(entry.bountyDiamonds()));
lore.add("");
lore.add("&6Claiming the bounty will give you a 50% chance of beheading the victim.");
final GUIItem item;
if (isUsableHeadName(targetName)) {
final GUIItem item;
if (isUsableHeadName(targetName)) {
item = new GUIItem(Material.PLAYER_HEAD, 1, "&6&l#" + rank + " &f" + targetName, targetName);
item = new GUIItem(Material.PLAYER_HEAD, 1, " &f" + targetName, targetName);
} else {
} else {
item = new GUIItem(Material.PAPER, 1, "&6&l#" + rank + " &f" + targetName);
item = new GUIItem(Material.PAPER, 1, "&6&l#" + rank + " &f" + targetName);
}
}
item.lore(lore);
item.setPlayErrorSound(false);
item.lore(lore);
item.setPlayErrorSound(false);
return item;
return item;
}
}
private String formatBounty(double bountyDiamonds) {
private String formatBounty(double bountyDiamonds) {
final long shards = diamondBankAPI.diamondsToShards(bountyDiamonds);
final long shards = diamondBankAPI.diamondsToShards(bountyDiamonds);
return diamondBankAPI.shardsToDiamonds(shards);
return diamondBankAPI.shardsToDiamonds(shards) + " Diamonds";
}
}
private GUIItem createStaticItem(Material material, String displayName, List<String> lore) {
private GUIItem createStaticItem(Material material, String displayName, List<String> lore) {
final GUIItem item = new GUIItem(material, 1, displayName);
item.lore(new ArrayList<>(lore));
item.setPlayErrorSound(false);
final GUIItem item = new GUIItem(material, 1, displayName);
item.lore(new ArrayList<>(lore));
item.setPlayErrorSound(false);
return item;
return item;
}
}
private GUIItem createButtonItem(Material material, String displayName, List<String> lore, GUIButton button) {
private GUIItem createButtonItem(Material material, String displayName, List<String> lore, GUIButton button) {
final GUIItem item = new GUIItem(material, 1, displayName);
item.lore(new ArrayList<>(lore));
item.setButton(button);
item.setPlayErrorSound(false);
final GUIItem item = new GUIItem(material, 1, displayName);
item.lore(new ArrayList<>(lore));
item.setButton(button);
item.setPlayErrorSound(false);
return item;
return item;
}
}
private List<BountyGuiEntry> getSortedBounties() {
private List<BountyGuiEntry> getSortedBounties() {
final List<BountyGuiEntry> supplied = bountySupplier.get();
if (supplied == null || supplied.isEmpty()) {
final List<BountyGuiEntry> supplied = bountySupplier.get();
if (supplied == null || supplied.isEmpty()) {
return Collections.emptyList();
return Collections.emptyList();
}
}
final List<BountyGuiEntry> sorted = new ArrayList<>(supplied);
sorted.removeIf(entry -> entry == null || entry.bountyDiamonds() <= 0D);
sorted.sort(Comparator.comparingDouble(BountyGuiEntry::bountyDiamonds).reversed()
.thenComparing(entry -> safeName(entry.targetName()), String.CASE_INSENSITIVE_ORDER));
final List<BountyGuiEntry> sorted = new ArrayList<>(supplied);
sorted.removeIf(entry -> entry == null || entry.bountyDiamonds() <= 0D);
sorted.sort(Comparator.comparingDouble(BountyGuiEntry::bountyDiamonds).reversed()
.thenComparing(entry -> safeName(entry.targetName()), String.CASE_INSENSITIVE_ORDER));
return sorted;
return sorted;
}
}
private int getMaxPage(List<BountyGuiEntry> bounties) {
private int getMaxPage(List<BountyGuiEntry> bounties) {
if (bounties.isEmpty()) {
if (bounties.isEmpty()) {
return 0;
return 0;
}
}
return (bounties.size() - 1) / PYRAMID_SLOTS.size();
return (bounties.size() - 1) / PYRAMID_SLOTS.size();
}
}
private String safeName(String name) {
private String safeName(String name) {
if (name == null || name.isBlank()) {
if (name == null || name.isBlank()) {
return "Unknown Player";
return "Unknown Player";
}
}
return name;
return name;
}
}
private boolean isUsableHeadName(String name) {
private boolean isUsableHeadName(String name) {
return name != null && !name.isBlank() && !"Unknown Player".equals(name)
&& MINECRAFT_USERNAME_PATTERN.matcher(name).matches();
return name != null && !name.isBlank() && !"Unknown Player".equals(name)
&& MINECRAFT_USERNAME_PATTERN.matcher(name).matches();
}
}
public static record BountyGuiEntry(UUID targetUuid, String targetName, double bountyDiamonds) {
}
public static record BountyGuiEntry(UUID targetUuid, String targetName, double bountyDiamonds) {
}
}