Implements commands for overriding payouts

This commit is contained in:
Kristian Knarvik 2023-12-18 17:21:04 +01:00
parent 9ba486bab4
commit dfba6b2125
10 changed files with 319 additions and 33 deletions

View File

@ -2,6 +2,8 @@ package net.knarcraft.timeismoney;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import net.knarcraft.timeismoney.command.ReloadCommand; 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.config.Configuration;
import net.knarcraft.timeismoney.listener.PlayerJoinListener; import net.knarcraft.timeismoney.listener.PlayerJoinListener;
import net.knarcraft.timeismoney.manager.EconomyManager; import net.knarcraft.timeismoney.manager.EconomyManager;
@ -11,19 +13,23 @@ import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Statistic; import org.bukkit.Statistic;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicesManager; import org.bukkit.plugin.ServicesManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.logging.Level; import java.util.logging.Level;
/** /**
* Time is money's main class * Time is money's main class
*/ */
@SuppressWarnings("unused")
public final class TimeIsMoney extends JavaPlugin { public final class TimeIsMoney extends JavaPlugin {
private Configuration configuration; private Configuration configuration;
@ -34,10 +40,11 @@ public final class TimeIsMoney extends JavaPlugin {
public void onEnable() { public void onEnable() {
timeIsMoney = this; timeIsMoney = this;
this.saveDefaultConfig(); this.saveDefaultConfig();
this.reloadConfig();
this.saveConfig();
FileConfiguration fileConfiguration = this.getConfig(); FileConfiguration fileConfiguration = this.getConfig();
fileConfiguration.options().copyDefaults(true); fileConfiguration.options().copyDefaults(true);
this.saveConfig(); this.saveConfig();
this.reloadConfig();
this.configuration = new Configuration(fileConfiguration); this.configuration = new Configuration(fileConfiguration);
// Plugin startup logic // Plugin startup logic
@ -57,20 +64,39 @@ public final class TimeIsMoney extends JavaPlugin {
} }
ReloadCommand reloadCommand = new ReloadCommand(); ReloadCommand reloadCommand = new ReloadCommand();
PluginCommand command = getCommand("reload"); registerCommand(getCommand("reload"), reloadCommand, reloadCommand);
if (command != null) { registerCommand(getCommand("setGroupPayout"), new SetGroupPaymentCommand(configuration), null);
command.setExecutor(reloadCommand); registerCommand(getCommand("setPlayerPayout"), new SetPlayerPaymentCommand(configuration), null);
command.setTabCompleter(reloadCommand);
}
Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(), this); Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(), this);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this::payPlayers, 20, 20); Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this::payPlayers, 20, 20);
} }
@Override /**
public void onDisable() { * Registers a new command
// Plugin shutdown logic *
* @param command <p>The command to register a command executor for</p>
* @param executor <p>The executor to register to the command</p>
* @param tabCompleter <p>The tab completer to register to the command, or null</p>
*/
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 <p>An instance of this plugin</p>
*/
public static TimeIsMoney getInstance() {
return timeIsMoney;
} }
/** /**
@ -125,7 +151,7 @@ public final class TimeIsMoney extends JavaPlugin {
* @param player <p>The player to check</p> * @param player <p>The player to check</p>
* @return <p>The player's payout multiplier</p> * @return <p>The player's payout multiplier</p>
*/ */
private double getPayoutMultiplier(Player player) { private double getPayoutMultiplier(@NotNull Player player) {
if (configuration.getHoursUntilBonus() < 0) { if (configuration.getHoursUntilBonus() < 0) {
return 1; return 1;
} }

View File

@ -1,5 +1,44 @@
package net.knarcraft.timeismoney.command; package net.knarcraft.timeismoney.command;
public class SetGroupPaymentCommand { import net.knarcraft.timeismoney.config.Configuration;
// TODO: Implement this 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 <p>The configuration to use</p>
*/
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;
}
}
} }

View File

@ -1,5 +1,62 @@
package net.knarcraft.timeismoney.command; package net.knarcraft.timeismoney.command;
public class SetPlayerPaymentCommand { import net.knarcraft.timeismoney.config.Configuration;
//TODO: Implement this 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 <p>The configuration to use</p>
*/
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;
}
} }

View File

@ -1,12 +1,15 @@
package net.knarcraft.timeismoney.config; package net.knarcraft.timeismoney.config;
import net.knarcraft.timeismoney.TimeIsMoney;
import net.knarcraft.timeismoney.manager.PermissionManager; import net.knarcraft.timeismoney.manager.PermissionManager;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -15,6 +18,7 @@ import java.util.UUID;
*/ */
public class Configuration { public class Configuration {
private final FileConfiguration fileConfiguration;
private final Map<String, Double> groupPayouts; private final Map<String, Double> groupPayouts;
private final Map<UUID, Double> playerPayouts; private final Map<UUID, Double> playerPayouts;
private final double defaultPayout; private final double defaultPayout;
@ -29,27 +33,30 @@ public class Configuration {
* *
* @param fileConfiguration <p>The file configuration to read values from</p> * @param fileConfiguration <p>The file configuration to read values from</p>
*/ */
public Configuration(FileConfiguration fileConfiguration) { public Configuration(@NotNull FileConfiguration fileConfiguration) {
groupPayouts = new HashMap<>(); groupPayouts = new HashMap<>();
playerPayouts = new HashMap<>(); playerPayouts = new HashMap<>();
ConfigurationSection groupPayoutsSection = fileConfiguration.getConfigurationSection("groupPayouts"); this.fileConfiguration = fileConfiguration;
ConfigurationSection groupPayoutsSection = fileConfiguration.getConfigurationSection(
ConfigurationKey.GROUP_PAYOUTS.getPath());
if (groupPayoutsSection != null) { if (groupPayoutsSection != null) {
for (String key : groupPayoutsSection.getKeys(false)) { for (String key : groupPayoutsSection.getKeys(false)) {
groupPayouts.put(key, groupPayoutsSection.getDouble(key)); groupPayouts.put(key, groupPayoutsSection.getDouble(key));
} }
} }
ConfigurationSection playerPayoutsSection = fileConfiguration.getConfigurationSection("playerPayouts"); ConfigurationSection playerPayoutsSection = fileConfiguration.getConfigurationSection(
ConfigurationKey.PLAYER_PAYOUTS.getPath());
if (playerPayoutsSection != null) { if (playerPayoutsSection != null) {
for (String key : playerPayoutsSection.getKeys(false)) { for (String key : playerPayoutsSection.getKeys(false)) {
playerPayouts.put(UUID.fromString(key), playerPayoutsSection.getDouble(key)); playerPayouts.put(UUID.fromString(key), playerPayoutsSection.getDouble(key));
} }
} }
this.defaultPayout = fileConfiguration.getDouble("defaultPayout", 10); this.defaultPayout = fileConfiguration.getDouble(ConfigurationKey.DEFAULT_PAYOUT.getPath(), 10);
this.hoursUntilBonus = fileConfiguration.getInt("hoursUntilBonus", 100); this.hoursUntilBonus = fileConfiguration.getInt(ConfigurationKey.HOURS_UNTIL_BONUS.getPath(), 100);
this.bonusMultiplier = fileConfiguration.getDouble("bonusMultiplier", 1); this.bonusMultiplier = fileConfiguration.getDouble(ConfigurationKey.BONUS_MULTIPLIER.getPath(), 1);
this.paymentDelay = fileConfiguration.getInt("paymentDelay", 60); this.paymentDelay = fileConfiguration.getInt(ConfigurationKey.PAYMENT_DELAY.getPath(), 60);
this.afkPercentage = fileConfiguration.getDouble("afkPercentage", 0); this.afkPercentage = fileConfiguration.getDouble(ConfigurationKey.AFK_PERCENTAGE.getPath(), 0);
this.displayPaymentMessage = fileConfiguration.getBoolean("displayPaymentMessage", true); 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 <p>The delay between payments</p> * @return <p>The delay between payments</p>
*/ */
@ -128,4 +135,73 @@ public class Configuration {
return displayPaymentMessage; return displayPaymentMessage;
} }
/**
* Sets the payout given to a specific player
*
* @param playerId <p>The id of the player to set payout for</p>
* @param payout <p>The payout to set for the player</p>
*/
public void setPlayerPayout(@NotNull UUID playerId, @Nullable Double payout) {
this.playerPayouts.put(playerId, payout);
}
/**
* Sets the payout given to a specific group
*
* @param groupName <p>The name of the group to set payout for</p>
* @param payout <p>The payout to set for the group</p>
*/
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<UUID, Double> 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<String, Double> 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();
}
} }

View File

@ -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 <p>The path of this configuration in the configuration file</p>
*/
ConfigurationKey(@NotNull String path) {
this.path = path;
}
/**
* Gets the path in the configuration file of this configuration key
*
* @return <p>This key's path</p>
*/
public @NotNull String getPath() {
return this.path;
}
}

View File

@ -2,6 +2,7 @@ package net.knarcraft.timeismoney.manager;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
/** /**
* A manager that performs all Economy tasks * A manager that performs all Economy tasks
@ -19,7 +20,7 @@ public final class EconomyManager {
* *
* @param economy <p>The economy object to use for everything economy-related</p> * @param economy <p>The economy object to use for everything economy-related</p>
*/ */
public static void initialize(Economy economy) { public static void initialize(@NotNull Economy economy) {
EconomyManager.economy = economy; EconomyManager.economy = economy;
} }
@ -48,7 +49,7 @@ public final class EconomyManager {
* @param player <p>The player to deposit money to</p> * @param player <p>The player to deposit money to</p>
* @param sum <p>The amount of money to deposit</p> * @param sum <p>The amount of money to deposit</p>
*/ */
public static void deposit(OfflinePlayer player, double sum) { public static void deposit(@NotNull OfflinePlayer player, double sum) {
economy.depositPlayer(player, sum); economy.depositPlayer(player, sum);
} }

View File

@ -20,7 +20,7 @@ public final class PermissionManager {
* *
* @param permission <p>The permission object to use for everything permission-related</p> * @param permission <p>The permission object to use for everything permission-related</p>
*/ */
public static void initialize(Permission permission) { public static void initialize(@NotNull Permission permission) {
PermissionManager.permission = permission; PermissionManager.permission = permission;
} }
@ -39,7 +39,7 @@ public final class PermissionManager {
* @param player <p>The player to get the groups for</p> * @param player <p>The player to get the groups for</p>
* @return <p>The player's groups</p> * @return <p>The player's groups</p>
*/ */
public static String[] getPlayerGroups(@NotNull Player player) { public static @NotNull String[] getPlayerGroups(@NotNull Player player) {
return permission.getPlayerGroups(player); return permission.getPlayerGroups(player);
} }

View File

@ -1,6 +1,8 @@
package net.knarcraft.timeismoney.manager; package net.knarcraft.timeismoney.manager;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -23,7 +25,7 @@ public final class PlayerTracker {
* *
* @param player <p>The player to track</p> * @param player <p>The player to track</p>
*/ */
public static void trackPlayer(Player player) { public static void trackPlayer(@NotNull Player player) {
playerLastPaid.put(player, System.currentTimeMillis()); playerLastPaid.put(player, System.currentTimeMillis());
} }
@ -35,7 +37,7 @@ public final class PlayerTracker {
* @param player <p>The player to check player time for</p> * @param player <p>The player to check player time for</p>
* @return <p>The last time the player was paid</p> * @return <p>The last time the player was paid</p>
*/ */
public static Long getPaymentTime(Player player) { public static @Nullable Long getPaymentTime(@NotNull Player player) {
return playerLastPaid.get(player); return playerLastPaid.get(player);
} }

View File

@ -1,4 +1,4 @@
# The default payout if the player has no overrides # The default payout if the player has no overrides
defaultPayout: 10 defaultPayout: 10
# The amount of minutes to wait between each payment # The amount of minutes to wait between each payment
paymentDelay: 60 paymentDelay: 60
@ -6,9 +6,9 @@ paymentDelay: 60
displayPaymentMessage: true displayPaymentMessage: true
# The amount of hours until a bonus is given. Set to -1 to disable. # The amount of hours until a bonus is given. Set to -1 to disable.
hoursUntilBonus: 100 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 bonusMultiplier: 1
# The percentage of the payment to pay AFK players # The percentage of their normal payout to pay AFK players
afkPercentage: 0 afkPercentage: 0
# Overrides for specific groups # Overrides for specific groups
groupPayouts: [ ] groupPayouts: [ ]

View File

@ -14,8 +14,25 @@ commands:
permission: timeismoney.reload permission: timeismoney.reload
description: Reloads the plugin description: Reloads the plugin
usage: /<command> usage: /<command>
setgrouppayout:
permission: timeismoney.admin
description: Sets the payout for a permission group
usage: /<command> <group name> <payout>
setplayerpayout:
permission: timeismoney.admin
description: Sets the payout for a player
usage: /<command> <player name/uuid> <payout>
permissions: permissions:
timeismoney.*:
description: Allows usage of all commands
default: false
children:
- timeismoney.reload
- timeismoney.admin
timeismoney.reload: timeismoney.reload:
description: Allows usage of the /reload command description: Allows usage of the /reload command
default: false
timeismoney.admin:
description: Allows usage of configuration commands
default: false default: false