Fix #3
All checks were successful
KnarCraft/PlayerPayouts/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-01-25 16:17:26 +01:00
parent 7a817595c1
commit 65d1471a8f
8 changed files with 205 additions and 44 deletions

View File

@ -131,7 +131,7 @@
<dependency>
<groupId>net.knarcraft</groupId>
<artifactId>knarlib</artifactId>
<version>1.2.4</version>
<version>1.2.5</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@ -40,6 +40,7 @@ public final class PlayerPayouts extends JavaPlugin {
private Configuration configuration;
private IEssentials essentials = null;
private Translator translator;
private StringFormatter stringFormatter;
private static PlayerPayouts playerPayouts;
@ -54,7 +55,7 @@ public final class PlayerPayouts extends JavaPlugin {
this.saveConfig();
this.configuration = new Configuration(fileConfiguration);
Translator translator = new Translator();
translator = new Translator();
translator.registerMessageCategory(Translatable.GROUP_PAYOUTS_UNAVAILABLE);
translator.loadLanguages(this.getDataFolder(), "en",
fileConfiguration.getString("language", "en"));
@ -134,7 +135,7 @@ public final class PlayerPayouts extends JavaPlugin {
}
/**
* Gets a string formatter
* Gets the string formatter
*
* @return <p>A string formatter</p>
*/
@ -142,6 +143,15 @@ public final class PlayerPayouts extends JavaPlugin {
return getInstance().stringFormatter;
}
/**
* Gets the translator
*
* @return <p>A translator</p>
*/
public static Translator getTranslator() {
return getInstance().translator;
}
/**
* Pay all players that have been on the server long enough
*/

View File

@ -12,6 +12,8 @@ import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* A command for overriding payments for specific groups
@ -32,10 +34,6 @@ public class SetGroupPaymentCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) {
if (arguments.length < 2) {
return false;
}
StringFormatter stringFormatter = PlayerPayouts.getStringFormatter();
if (!PermissionManager.isInitialized()) {
@ -43,11 +41,38 @@ public class SetGroupPaymentCommand implements CommandExecutor {
return false;
}
// Display all current group payment overrides
if (arguments.length == 0) {
StringBuilder builder = new StringBuilder(stringFormatter.getUnformattedColoredMessage(
Translatable.CURRENT_PAYOUT_OVERRIDE));
for (Map.Entry<String, Double> entry : configuration.getGroupPayouts().entrySet()) {
if (entry.getValue() == null) {
continue;
}
builder.append(stringFormatter.replacePlaceholders(Translatable.PAYOUT_OVERRIDE_LIST_FORMAT,
List.of("{subject}", "{value}"), List.of(entry.getKey(), String.valueOf(entry.getValue()))));
}
stringFormatter.displaySuccessMessage(commandSender, builder.toString());
return true;
}
if (Arrays.stream(PermissionManager.getPermissionGroups()).noneMatch(item -> item.equals(arguments[0]))) {
stringFormatter.displayErrorMessage(commandSender, Translatable.GROUP_INVALID);
return false;
}
// Display the specified group's payment override
if (arguments.length == 1) {
String output = stringFormatter.getUnformattedMessage(Translatable.CURRENT_PAYOUT_OVERRIDE);
Double value = configuration.getGroupPayout(arguments[0]);
if (value != null) {
output += stringFormatter.replacePlaceholders(Translatable.PAYOUT_OVERRIDE_LIST_FORMAT,
List.of("{subject}", "{value}"), List.of(arguments[0], String.valueOf(value)));
}
stringFormatter.displaySuccessMessage(commandSender, output);
return true;
}
try {
String group = arguments[0];
if (StringHelper.isNonValue(arguments[1])) {

View File

@ -4,15 +4,16 @@ import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.playerpayouts.PlayerPayouts;
import net.knarcraft.playerpayouts.config.Configuration;
import net.knarcraft.playerpayouts.config.Translatable;
import net.knarcraft.playerpayouts.util.PlayerIdHelper;
import net.knarcraft.playerpayouts.util.StringHelper;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
@ -34,36 +35,48 @@ public class SetPlayerPaymentCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) {
if (arguments.length < 2) {
return false;
}
String playerName = arguments[0];
StringFormatter stringFormatter = PlayerPayouts.getStringFormatter();
// Parse the player id
UUID playerId;
try {
// Find player from UUID
playerId = UUID.fromString(playerName);
} catch (IllegalArgumentException exception) {
// Get player from player name
Player player = Bukkit.getPlayer(playerName);
if (player != null) {
playerId = player.getUniqueId();
} else {
// Try to match the player name against the offline player list
playerId = null;
for (OfflinePlayer offlinePlayer : Bukkit.getOfflinePlayers()) {
if (offlinePlayer.getName() != null && offlinePlayer.getName().equals(playerName)) {
playerId = offlinePlayer.getUniqueId();
}
// Display all current group payment overrides
if (arguments.length == 0) {
StringBuilder builder = new StringBuilder(stringFormatter.getUnformattedColoredMessage(
Translatable.CURRENT_PAYOUT_OVERRIDE));
for (Map.Entry<UUID, Double> entry : configuration.getPlayerPayouts().entrySet()) {
if (entry.getValue() == null) {
continue;
}
if (playerId == null) {
stringFormatter.displayErrorMessage(commandSender, Translatable.PLAYER_ID_REQUIRED);
return false;
String playerName = entry.getKey().toString();
String possibleName = Bukkit.getOfflinePlayer(entry.getKey()).getName();
if (possibleName != null) {
playerName = possibleName;
}
builder.append(stringFormatter.replacePlaceholders(Translatable.PAYOUT_OVERRIDE_LIST_FORMAT,
List.of("{subject}", "{value}"), List.of(playerName, String.valueOf(entry.getValue()))));
}
stringFormatter.displaySuccessMessage(commandSender, builder.toString());
return true;
}
String playerName = arguments[0];
// Parse the player id
UUID playerId = PlayerIdHelper.getId(playerName);
if (playerId == null) {
stringFormatter.displayErrorMessage(commandSender, Translatable.PLAYER_ID_REQUIRED);
return false;
}
// Display the specified group's payment override
if (arguments.length == 1) {
String output = stringFormatter.getUnformattedMessage(Translatable.CURRENT_PAYOUT_OVERRIDE);
Double value = configuration.getPlayerPayout(playerId);
if (value != null) {
output += stringFormatter.replacePlaceholders(Translatable.PAYOUT_OVERRIDE_LIST_FORMAT,
List.of("{subject}", "{value}"), List.of(playerName, String.valueOf(value)));
}
stringFormatter.displaySuccessMessage(commandSender, output);
return true;
}
// Parse the payout value

View File

@ -55,6 +55,8 @@ public class Configuration {
groupPayouts = new HashMap<>();
playerPayouts = new HashMap<>();
this.fileConfiguration = fileConfiguration;
// Load per-group payouts
ConfigurationSection groupPayoutsSection = fileConfiguration.getConfigurationSection(
ConfigurationKey.GROUP_PAYOUTS.getPath());
if (groupPayoutsSection != null) {
@ -62,6 +64,8 @@ public class Configuration {
groupPayouts.put(key, groupPayoutsSection.getDouble(key));
}
}
// Load per-player payouts
ConfigurationSection playerPayoutsSection = fileConfiguration.getConfigurationSection(
ConfigurationKey.PLAYER_PAYOUTS.getPath());
if (playerPayoutsSection != null) {
@ -69,12 +73,17 @@ public class Configuration {
playerPayouts.put(UUID.fromString(key), playerPayoutsSection.getDouble(key));
}
}
// Load simple configuration values
this.defaultPayout = fileConfiguration.getDouble(ConfigurationKey.DEFAULT_PAYOUT.getPath(), 10);
this.hoursUntilBonus = fileConfiguration.getInt(ConfigurationKey.HOURS_UNTIL_BONUS.getPath(), 100);
this.bonusMultiplier = fileConfiguration.getDouble(ConfigurationKey.BONUS_MULTIPLIER.getPath(), 1);
this.payoutDelay = fileConfiguration.getInt(ConfigurationKey.PAYOUT_DELAY.getPath(), 60);
this.afkPercentage = fileConfiguration.getDouble(ConfigurationKey.AFK_PERCENTAGE.getPath(), 0);
this.displayPaymentMessage = fileConfiguration.getBoolean(ConfigurationKey.DISPLAY_PAYMENT_MESSAGE.getPath(), true);
this.displayPaymentMessage = fileConfiguration.getBoolean(ConfigurationKey.DISPLAY_PAYMENT_MESSAGE.getPath(),
true);
// Parse the payout rules
try {
this.payoutComponent = PayoutActionParser.matchPayoutComponent(fileConfiguration.getString(
ConfigurationKey.PAYOUT_RULES.getPath(), "p,hg,b"));
@ -260,6 +269,44 @@ public class Configuration {
this.save();
}
/**
* Gets all group payout overrides
*
* @return <p>All group payout overrides</p>
*/
public @NotNull Map<String, Double> getGroupPayouts() {
return groupPayouts;
}
/**
* Gets a group payout override
*
* @param groupName <p>The group to get the override of</p>
* @return <p>The overridden payout, or null if missing or cleared</p>
*/
public @Nullable Double getGroupPayout(@NotNull String groupName) {
return groupPayouts.get(groupName);
}
/**
* Gets all player payout overrides
*
* @return <p>All player payout overrides</p>
*/
public @NotNull Map<UUID, Double> getPlayerPayouts() {
return playerPayouts;
}
/**
* Gets a player payout override
*
* @param playerId <p>The id of the player to get the override of</p>
* @return <p>The overridden payout, or null if missing or cleared</p>
*/
public @Nullable Double getPlayerPayout(@NotNull UUID playerId) {
return playerPayouts.get(playerId);
}
/**
* Saves this configuration to disk
*/

View File

@ -58,6 +58,16 @@ public enum Translatable implements TranslatableMessage {
*/
GROUP_INVALID,
/**
* The text displayed when listing current payout overrides
*/
CURRENT_PAYOUT_OVERRIDE,
/**
* The format to use when listing current payout overrides
*/
PAYOUT_OVERRIDE_LIST_FORMAT,
;
@Override

View File

@ -0,0 +1,54 @@
package net.knarcraft.playerpayouts.util;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.UUID;
/**
* A helper class for dealing with player ids
*/
public final class PlayerIdHelper {
private PlayerIdHelper() {
}
/**
* Gets a player's UUID from the player's identifier
*
* @param playerIdentifier <p>The identifier of the player</p>
* @return <p>The id of the player, or null if an invalid UUID or name has been given</p>
*/
public static @Nullable UUID getId(@NotNull String playerIdentifier) {
UUID playerId;
try {
// Find player from UUID
playerId = UUID.fromString(playerIdentifier);
} catch (IllegalArgumentException exception) {
// Get player from player name
Player player = Bukkit.getPlayer(playerIdentifier);
if (player != null) {
playerId = player.getUniqueId();
} else {
// Try to match the player name against the offline player list
playerId = null;
for (OfflinePlayer offlinePlayer : Bukkit.getOfflinePlayers()) {
if (offlinePlayer.getName() != null && offlinePlayer.getName().equals(playerIdentifier)) {
playerId = offlinePlayer.getUniqueId();
}
}
if (playerId == null) {
return null;
}
}
}
return playerId;
}
}

View File

@ -1,11 +1,13 @@
en:
GROUP_PAYOUTS_UNAVAILABLE: "Vault permissions aren't available, and thus per-group payouts are unavailable."
PLUGIN_RELOADED: "Plugin reloaded!"
GROUP_PAYOUT_CLEARED: "Group payout for group {group} has been cleared"
GROUP_PAYOUT_SET: "Group payout for group {group} has been set to {value}"
PLAYER_PAYOUT_CLEARED: "Player payout for player {player} has been cleared"
PLAYER_PAYOUT_SET: "Player payout for player {player} has been set to {value}"
PAYOUT_NUMBER_REQUIRED: "Payout must be a number"
PLAYER_ID_REQUIRED: "You must supply a valid name of an online player, or a UUID"
PAYOUT_RECEIVED: "You got a paycheck of {value}"
GROUP_INVALID: "You have specified an invalid permission group"
GROUP_PAYOUTS_UNAVAILABLE: "&#ECECECVault permissions aren't available, and thus per-group payouts are unavailable."
PLUGIN_RELOADED: "&#ECECECPlugin reloaded!"
GROUP_PAYOUT_CLEARED: "&#ECECECGroup payout for group {group} has been cleared"
GROUP_PAYOUT_SET: "&#ECECECGroup payout for group {group} has been set to {value}"
PLAYER_PAYOUT_CLEARED: "&#ECECECPlayer payout for player {player} has been cleared"
PLAYER_PAYOUT_SET: "&#ECECECPlayer payout for player {player} has been set to {value}"
PAYOUT_NUMBER_REQUIRED: "&#ECECECPayout must be a number"
PLAYER_ID_REQUIRED: "&#ECECECYou must supply a valid name of an online player, or a UUID"
PAYOUT_RECEIVED: "&#ECECECYou got a paycheck of {value}"
GROUP_INVALID: "&#ECECECYou have specified an invalid permission group"
CURRENT_PAYOUT_OVERRIDE: "&#ECECECCurrent payout override(s):&r"
PAYOUT_OVERRIDE_LIST_FORMAT: "\n&#e9e0d0{subject}&r -> &#367c2b{value}&r"