From dfba6b21257012bf7f071ddf511c9ac35f0cd176 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 18 Dec 2023 17:21:04 +0100 Subject: [PATCH] Implements commands for overriding payouts --- .../knarcraft/timeismoney/TimeIsMoney.java | 46 +++++++-- .../command/SetGroupPaymentCommand.java | 43 ++++++++- .../command/SetPlayerPaymentCommand.java | 61 +++++++++++- .../timeismoney/config/Configuration.java | 96 +++++++++++++++++-- .../timeismoney/config/ConfigurationKey.java | 68 +++++++++++++ .../timeismoney/manager/EconomyManager.java | 5 +- .../manager/PermissionManager.java | 4 +- .../timeismoney/manager/PlayerTracker.java | 6 +- src/main/resources/config.yml | 6 +- src/main/resources/plugin.yml | 17 ++++ 10 files changed, 319 insertions(+), 33 deletions(-) create mode 100644 src/main/java/net/knarcraft/timeismoney/config/ConfigurationKey.java diff --git a/src/main/java/net/knarcraft/timeismoney/TimeIsMoney.java b/src/main/java/net/knarcraft/timeismoney/TimeIsMoney.java index 5636645..1a5b720 100644 --- a/src/main/java/net/knarcraft/timeismoney/TimeIsMoney.java +++ b/src/main/java/net/knarcraft/timeismoney/TimeIsMoney.java @@ -2,6 +2,8 @@ package net.knarcraft.timeismoney; import com.earth2me.essentials.IEssentials; import net.knarcraft.timeismoney.command.ReloadCommand; +import net.knarcraft.timeismoney.command.SetGroupPaymentCommand; +import net.knarcraft.timeismoney.command.SetPlayerPaymentCommand; import net.knarcraft.timeismoney.config.Configuration; import net.knarcraft.timeismoney.listener.PlayerJoinListener; import net.knarcraft.timeismoney.manager.EconomyManager; @@ -11,19 +13,23 @@ import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; import org.bukkit.Statistic; +import org.bukkit.command.CommandExecutor; import org.bukkit.command.PluginCommand; +import org.bukkit.command.TabCompleter; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.ServicesManager; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.logging.Level; /** * Time is money's main class */ +@SuppressWarnings("unused") public final class TimeIsMoney extends JavaPlugin { private Configuration configuration; @@ -34,10 +40,11 @@ public final class TimeIsMoney extends JavaPlugin { public void onEnable() { timeIsMoney = this; this.saveDefaultConfig(); + this.reloadConfig(); + this.saveConfig(); FileConfiguration fileConfiguration = this.getConfig(); fileConfiguration.options().copyDefaults(true); this.saveConfig(); - this.reloadConfig(); this.configuration = new Configuration(fileConfiguration); // Plugin startup logic @@ -57,20 +64,39 @@ public final class TimeIsMoney extends JavaPlugin { } ReloadCommand reloadCommand = new ReloadCommand(); - PluginCommand command = getCommand("reload"); - if (command != null) { - command.setExecutor(reloadCommand); - command.setTabCompleter(reloadCommand); - } + registerCommand(getCommand("reload"), reloadCommand, reloadCommand); + registerCommand(getCommand("setGroupPayout"), new SetGroupPaymentCommand(configuration), null); + registerCommand(getCommand("setPlayerPayout"), new SetPlayerPaymentCommand(configuration), null); Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(), this); Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this::payPlayers, 20, 20); } - @Override - public void onDisable() { - // Plugin shutdown logic + /** + * Registers a new command + * + * @param command

The command to register a command executor for

+ * @param executor

The executor to register to the command

+ * @param tabCompleter

The tab completer to register to the command, or null

+ */ + private void registerCommand(@Nullable PluginCommand command, @NotNull CommandExecutor executor, + @Nullable TabCompleter tabCompleter) { + if (command != null) { + command.setExecutor(executor); + if (tabCompleter != null) { + command.setTabCompleter(tabCompleter); + } + } + } + + /** + * Gets an instance of this plugin + * + * @return

An instance of this plugin

+ */ + public static TimeIsMoney getInstance() { + return timeIsMoney; } /** @@ -125,7 +151,7 @@ public final class TimeIsMoney extends JavaPlugin { * @param player

The player to check

* @return

The player's payout multiplier

*/ - private double getPayoutMultiplier(Player player) { + private double getPayoutMultiplier(@NotNull Player player) { if (configuration.getHoursUntilBonus() < 0) { return 1; } diff --git a/src/main/java/net/knarcraft/timeismoney/command/SetGroupPaymentCommand.java b/src/main/java/net/knarcraft/timeismoney/command/SetGroupPaymentCommand.java index f6c6709..32994ec 100644 --- a/src/main/java/net/knarcraft/timeismoney/command/SetGroupPaymentCommand.java +++ b/src/main/java/net/knarcraft/timeismoney/command/SetGroupPaymentCommand.java @@ -1,5 +1,44 @@ package net.knarcraft.timeismoney.command; -public class SetGroupPaymentCommand { - // TODO: Implement this +import net.knarcraft.timeismoney.config.Configuration; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +/** + * A command for overriding payments for specific groups + */ +public class SetGroupPaymentCommand implements CommandExecutor { + + private final Configuration configuration; + + /** + * Instantiates a new set group payment command + * + * @param configuration

The configuration to use

+ */ + public SetGroupPaymentCommand(@NotNull Configuration configuration) { + this.configuration = configuration; + } + + @Override + public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, + @NotNull String[] arguments) { + if (arguments.length < 2) { + return false; + } + + try { + String group = arguments[0]; + Double payout = Double.parseDouble(arguments[1]); + configuration.setGroupPayout(group, payout); + configuration.save(); + return true; + } catch (NumberFormatException exception) { + commandSender.sendMessage("Payout must be a number"); + return false; + } + } + } diff --git a/src/main/java/net/knarcraft/timeismoney/command/SetPlayerPaymentCommand.java b/src/main/java/net/knarcraft/timeismoney/command/SetPlayerPaymentCommand.java index 44260c3..74dbc87 100644 --- a/src/main/java/net/knarcraft/timeismoney/command/SetPlayerPaymentCommand.java +++ b/src/main/java/net/knarcraft/timeismoney/command/SetPlayerPaymentCommand.java @@ -1,5 +1,62 @@ package net.knarcraft.timeismoney.command; -public class SetPlayerPaymentCommand { - //TODO: Implement this +import net.knarcraft.timeismoney.config.Configuration; +import org.bukkit.Bukkit; +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.UUID; + +/** + * A command for overriding payments for specific players + */ +public class SetPlayerPaymentCommand implements CommandExecutor { + + private final Configuration configuration; + + /** + * Instantiates a new set player payment command + * + * @param configuration

The configuration to use

+ */ + public SetPlayerPaymentCommand(Configuration configuration) { + this.configuration = configuration; + } + + @Override + public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, + @NotNull String[] arguments) { + if (arguments.length < 2) { + return false; + } + + UUID playerId; + try { + playerId = UUID.fromString(arguments[0]); + } catch (IllegalArgumentException exception) { + Player player = Bukkit.getPlayer(arguments[0]); + if (player != null) { + playerId = player.getUniqueId(); + } else { + commandSender.sendMessage("You must supply a valid name of an online player, or a UUID"); + return false; + } + } + + double payout; + try { + payout = Double.parseDouble(arguments[1]); + } catch (NumberFormatException exception) { + commandSender.sendMessage("Payout must be a number"); + return false; + } + + configuration.setPlayerPayout(playerId, payout); + configuration.save(); + return true; + } + } diff --git a/src/main/java/net/knarcraft/timeismoney/config/Configuration.java b/src/main/java/net/knarcraft/timeismoney/config/Configuration.java index b510d36..97e4080 100644 --- a/src/main/java/net/knarcraft/timeismoney/config/Configuration.java +++ b/src/main/java/net/knarcraft/timeismoney/config/Configuration.java @@ -1,12 +1,15 @@ package net.knarcraft.timeismoney.config; +import net.knarcraft.timeismoney.TimeIsMoney; import net.knarcraft.timeismoney.manager.PermissionManager; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.UUID; @@ -15,6 +18,7 @@ import java.util.UUID; */ public class Configuration { + private final FileConfiguration fileConfiguration; private final Map groupPayouts; private final Map playerPayouts; private final double defaultPayout; @@ -29,27 +33,30 @@ public class Configuration { * * @param fileConfiguration

The file configuration to read values from

*/ - public Configuration(FileConfiguration fileConfiguration) { + public Configuration(@NotNull FileConfiguration fileConfiguration) { groupPayouts = new HashMap<>(); playerPayouts = new HashMap<>(); - ConfigurationSection groupPayoutsSection = fileConfiguration.getConfigurationSection("groupPayouts"); + this.fileConfiguration = fileConfiguration; + ConfigurationSection groupPayoutsSection = fileConfiguration.getConfigurationSection( + ConfigurationKey.GROUP_PAYOUTS.getPath()); if (groupPayoutsSection != null) { for (String key : groupPayoutsSection.getKeys(false)) { groupPayouts.put(key, groupPayoutsSection.getDouble(key)); } } - ConfigurationSection playerPayoutsSection = fileConfiguration.getConfigurationSection("playerPayouts"); + ConfigurationSection playerPayoutsSection = fileConfiguration.getConfigurationSection( + ConfigurationKey.PLAYER_PAYOUTS.getPath()); if (playerPayoutsSection != null) { for (String key : playerPayoutsSection.getKeys(false)) { playerPayouts.put(UUID.fromString(key), playerPayoutsSection.getDouble(key)); } } - this.defaultPayout = fileConfiguration.getDouble("defaultPayout", 10); - this.hoursUntilBonus = fileConfiguration.getInt("hoursUntilBonus", 100); - this.bonusMultiplier = fileConfiguration.getDouble("bonusMultiplier", 1); - this.paymentDelay = fileConfiguration.getInt("paymentDelay", 60); - this.afkPercentage = fileConfiguration.getDouble("afkPercentage", 0); - this.displayPaymentMessage = fileConfiguration.getBoolean("displayPaymentMessage", true); + 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.paymentDelay = fileConfiguration.getInt(ConfigurationKey.PAYMENT_DELAY.getPath(), 60); + this.afkPercentage = fileConfiguration.getDouble(ConfigurationKey.AFK_PERCENTAGE.getPath(), 0); + this.displayPaymentMessage = fileConfiguration.getBoolean(ConfigurationKey.DISPLAY_PAYMENT_MESSAGE.getPath(), true); } /** @@ -102,7 +109,7 @@ public class Configuration { } /** - * The delay between each time a player is paid + * The delay between each time a player is paid in minutes * * @return

The delay between payments

*/ @@ -128,4 +135,73 @@ public class Configuration { return displayPaymentMessage; } + /** + * Sets the payout given to a specific player + * + * @param playerId

The id of the player to set payout for

+ * @param payout

The payout to set for the player

+ */ + public void setPlayerPayout(@NotNull UUID playerId, @Nullable Double payout) { + this.playerPayouts.put(playerId, payout); + } + + /** + * Sets the payout given to a specific group + * + * @param groupName

The name of the group to set payout for

+ * @param payout

The payout to set for the group

+ */ + public void setGroupPayout(@NotNull String groupName, @Nullable Double payout) { + this.groupPayouts.put(groupName, payout); + } + + /** + * Saves this configuration to disk + */ + public void save() { + fileConfiguration.set(ConfigurationKey.PAYMENT_DELAY.getPath(), this.paymentDelay); + fileConfiguration.setComments(ConfigurationKey.PAYMENT_DELAY.getPath(), + List.of("The amount of minutes to wait between each payment")); + + fileConfiguration.set(ConfigurationKey.BONUS_MULTIPLIER.getPath(), this.bonusMultiplier); + fileConfiguration.setComments(ConfigurationKey.BONUS_MULTIPLIER.getPath(), + List.of("A multiplier used to increase or decrease the time bonus ((hours played / hours until bonus) * bonusMultiplier) + payout")); + + fileConfiguration.set(ConfigurationKey.DEFAULT_PAYOUT.getPath(), this.defaultPayout); + fileConfiguration.setComments(ConfigurationKey.DEFAULT_PAYOUT.getPath(), + List.of("The default payout if the player has no overrides")); + + fileConfiguration.set(ConfigurationKey.DISPLAY_PAYMENT_MESSAGE.getPath(), this.displayPaymentMessage); + fileConfiguration.setComments(ConfigurationKey.DISPLAY_PAYMENT_MESSAGE.getPath(), + List.of("Whether to announce to a player that they've just been paid")); + + fileConfiguration.set(ConfigurationKey.AFK_PERCENTAGE.getPath(), this.afkPercentage); + fileConfiguration.setComments(ConfigurationKey.AFK_PERCENTAGE.getPath(), + List.of("The percentage of their normal payout to pay AFK players")); + + fileConfiguration.set(ConfigurationKey.PAYMENT_DELAY.getPath(), this.paymentDelay); + fileConfiguration.setComments(ConfigurationKey.PAYMENT_DELAY.getPath(), + List.of("The amount of minutes to wait between each payment")); + + fileConfiguration.set(ConfigurationKey.HOURS_UNTIL_BONUS.getPath(), this.hoursUntilBonus); + fileConfiguration.setComments(ConfigurationKey.HOURS_UNTIL_BONUS.getPath(), + List.of("The amount of hours until a bonus is given. Set to -1 to disable.")); + + for (Map.Entry playerPayout : this.playerPayouts.entrySet()) { + fileConfiguration.set(ConfigurationKey.PLAYER_PAYOUTS.getPath() + "." + playerPayout.getKey().toString(), + playerPayout.getValue()); + } + fileConfiguration.setComments(ConfigurationKey.PLAYER_PAYOUTS.getPath(), + List.of("Overrides for specific players")); + + for (Map.Entry groupPayout : this.groupPayouts.entrySet()) { + fileConfiguration.set(ConfigurationKey.GROUP_PAYOUTS.getPath() + "." + groupPayout.getKey(), + groupPayout.getValue()); + } + fileConfiguration.setComments(ConfigurationKey.GROUP_PAYOUTS.getPath(), + List.of("Overrides for specific groups")); + + TimeIsMoney.getInstance().saveConfig(); + } + } diff --git a/src/main/java/net/knarcraft/timeismoney/config/ConfigurationKey.java b/src/main/java/net/knarcraft/timeismoney/config/ConfigurationKey.java new file mode 100644 index 0000000..212dbf4 --- /dev/null +++ b/src/main/java/net/knarcraft/timeismoney/config/ConfigurationKey.java @@ -0,0 +1,68 @@ +package net.knarcraft.timeismoney.config; + +import org.jetbrains.annotations.NotNull; + +public enum ConfigurationKey { + + /** + * The default base pay for every player + */ + DEFAULT_PAYOUT("defaultPayout"), + + /** + * The amount of hours a player needs to pay until they get a payment bonus + */ + HOURS_UNTIL_BONUS("hoursUntilBonus"), + + /** + * The bonus multiplier applied for hours played ((hours played / hours until bonus) * bonusMultiplier) + payout + */ + BONUS_MULTIPLIER("bonusMultiplier"), + + /** + * The amount of minutes between each payout + */ + PAYMENT_DELAY("paymentDelay"), + + /** + * The percentage of the payment to pay AFK players + */ + AFK_PERCENTAGE("afkPercentage"), + + /** + * Whether to inform the user each time they receive payment + */ + DISPLAY_PAYMENT_MESSAGE("displayPaymentMessage"), + + /** + * Payout overrides for each group + */ + GROUP_PAYOUTS("groupPayouts"), + + /** + * Payout override for each player + */ + PLAYER_PAYOUTS("playerPayouts"), + ; + + private final String path; + + /** + * Instantiates a new configuration key + * + * @param path

The path of this configuration in the configuration file

+ */ + ConfigurationKey(@NotNull String path) { + this.path = path; + } + + /** + * Gets the path in the configuration file of this configuration key + * + * @return

This key's path

+ */ + public @NotNull String getPath() { + return this.path; + } + +} diff --git a/src/main/java/net/knarcraft/timeismoney/manager/EconomyManager.java b/src/main/java/net/knarcraft/timeismoney/manager/EconomyManager.java index 8674f27..a3415c8 100644 --- a/src/main/java/net/knarcraft/timeismoney/manager/EconomyManager.java +++ b/src/main/java/net/knarcraft/timeismoney/manager/EconomyManager.java @@ -2,6 +2,7 @@ package net.knarcraft.timeismoney.manager; import net.milkbowl.vault.economy.Economy; import org.bukkit.OfflinePlayer; +import org.jetbrains.annotations.NotNull; /** * A manager that performs all Economy tasks @@ -19,7 +20,7 @@ public final class EconomyManager { * * @param economy

The economy object to use for everything economy-related

*/ - public static void initialize(Economy economy) { + public static void initialize(@NotNull Economy economy) { EconomyManager.economy = economy; } @@ -48,7 +49,7 @@ public final class EconomyManager { * @param player

The player to deposit money to

* @param sum

The amount of money to deposit

*/ - public static void deposit(OfflinePlayer player, double sum) { + public static void deposit(@NotNull OfflinePlayer player, double sum) { economy.depositPlayer(player, sum); } diff --git a/src/main/java/net/knarcraft/timeismoney/manager/PermissionManager.java b/src/main/java/net/knarcraft/timeismoney/manager/PermissionManager.java index 94bb8c5..a29eeea 100644 --- a/src/main/java/net/knarcraft/timeismoney/manager/PermissionManager.java +++ b/src/main/java/net/knarcraft/timeismoney/manager/PermissionManager.java @@ -20,7 +20,7 @@ public final class PermissionManager { * * @param permission

The permission object to use for everything permission-related

*/ - public static void initialize(Permission permission) { + public static void initialize(@NotNull Permission permission) { PermissionManager.permission = permission; } @@ -39,7 +39,7 @@ public final class PermissionManager { * @param player

The player to get the groups for

* @return

The player's groups

*/ - public static String[] getPlayerGroups(@NotNull Player player) { + public static @NotNull String[] getPlayerGroups(@NotNull Player player) { return permission.getPlayerGroups(player); } diff --git a/src/main/java/net/knarcraft/timeismoney/manager/PlayerTracker.java b/src/main/java/net/knarcraft/timeismoney/manager/PlayerTracker.java index 847c816..1e3b476 100644 --- a/src/main/java/net/knarcraft/timeismoney/manager/PlayerTracker.java +++ b/src/main/java/net/knarcraft/timeismoney/manager/PlayerTracker.java @@ -1,6 +1,8 @@ package net.knarcraft.timeismoney.manager; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.Map; @@ -23,7 +25,7 @@ public final class PlayerTracker { * * @param player

The player to track

*/ - public static void trackPlayer(Player player) { + public static void trackPlayer(@NotNull Player player) { playerLastPaid.put(player, System.currentTimeMillis()); } @@ -35,7 +37,7 @@ public final class PlayerTracker { * @param player

The player to check player time for

* @return

The last time the player was paid

*/ - public static Long getPaymentTime(Player player) { + public static @Nullable Long getPaymentTime(@NotNull Player player) { return playerLastPaid.get(player); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index c617326..d6ab5ec 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,4 +1,4 @@ -# The default payout if the player has no overrides +# The default payout if the player has no overrides defaultPayout: 10 # The amount of minutes to wait between each payment paymentDelay: 60 @@ -6,9 +6,9 @@ paymentDelay: 60 displayPaymentMessage: true # The amount of hours until a bonus is given. Set to -1 to disable. hoursUntilBonus: 100 -# A multiplier used to increase or decrease the time bonus +# A multiplier used to increase or decrease the time bonus ((hours played / hours until bonus) * bonusMultiplier) + payout bonusMultiplier: 1 -# The percentage of the payment to pay AFK players +# The percentage of their normal payout to pay AFK players afkPercentage: 0 # Overrides for specific groups groupPayouts: [ ] diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 38ee0c1..c2f1e92 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -14,8 +14,25 @@ commands: permission: timeismoney.reload description: Reloads the plugin usage: / + setgrouppayout: + permission: timeismoney.admin + description: Sets the payout for a permission group + usage: / + setplayerpayout: + permission: timeismoney.admin + description: Sets the payout for a player + usage: / permissions: + timeismoney.*: + description: Allows usage of all commands + default: false + children: + - timeismoney.reload + - timeismoney.admin timeismoney.reload: description: Allows usage of the /reload command + default: false + timeismoney.admin: + description: Allows usage of configuration commands default: false \ No newline at end of file