From b953f6cbc51fc7553b46200dd2ac80daee7226b4 Mon Sep 17 00:00:00 2001 From: A5H73Y Date: Sat, 5 Nov 2022 18:58:32 +0000 Subject: [PATCH] Added "pac setpropertyforeverysinglecourse (property) [value]" Added "pac setpropertyforeverysingleplayer (property) [value]" --- docs/files/parkourCommands.json | 14 ++++++ .../commands/ParkourConsoleCommands.java | 44 +++++++++++++++++++ .../type/admin/AdministrationManager.java | 11 +---- .../parkour/type/course/CourseManager.java | 12 ++--- .../parkour/type/player/PlayerManager.java | 8 +--- .../upgrade/major/PlayerDataUpgradeTask.java | 5 +-- .../upgrade/minor/PlayerMinorUpgradeTask.java | 5 +-- .../a5h73y/parkour/utility/PlayerUtils.java | 15 ++++++- src/main/resources/parkourCommands.json | 14 ++++++ 9 files changed, 95 insertions(+), 33 deletions(-) diff --git a/docs/files/parkourCommands.json b/docs/files/parkourCommands.json index d2af9995..c0ed47d2 100644 --- a/docs/files/parkourCommands.json +++ b/docs/files/parkourCommands.json @@ -398,6 +398,20 @@ "consoleSyntax": "pac reset (course / player / leaderboard / prize / commands) (argument)", "autoTabSyntax": "[course,player,leaderboard,prize,commands] {1:player=(player),*=(course)}" }, + { + "command": "setpropertyforeverysinglecourse", + "title": "Set Property for every Course", + "description": "Set the specified property for every single known Parkour Course, not providing a value will remove the property.", + "consoleSyntax": "pac setpropertyforeverysingleplayer (property) [value]", + "consoleOnly": true + }, + { + "command": "setpropertyforeverysingleplayer", + "title": "Set Property for every Player", + "description": "Set the specified property for every single known Parkour player, not providing a value will remove the property.", + "consoleSyntax": "pac setpropertyforeverysingleplayer (property) [value]", + "consoleOnly": true + }, { "command": "respawn", "title": "Respawn to last Checkpoint", diff --git a/src/main/java/io/github/a5h73y/parkour/commands/ParkourConsoleCommands.java b/src/main/java/io/github/a5h73y/parkour/commands/ParkourConsoleCommands.java index ed15424c..bb79a9aa 100644 --- a/src/main/java/io/github/a5h73y/parkour/commands/ParkourConsoleCommands.java +++ b/src/main/java/io/github/a5h73y/parkour/commands/ParkourConsoleCommands.java @@ -9,11 +9,14 @@ import io.github.a5h73y.parkour.other.AbstractPluginReceiver; import io.github.a5h73y.parkour.other.PluginBackupUtil; import io.github.a5h73y.parkour.type.course.CourseConfig; +import io.github.a5h73y.parkour.type.player.PlayerConfig; +import io.github.a5h73y.parkour.utility.PlayerUtils; import io.github.a5h73y.parkour.utility.PluginUtils; import io.github.a5h73y.parkour.utility.StringUtils; import io.github.a5h73y.parkour.utility.TranslationUtils; import io.github.a5h73y.parkour.utility.ValidationUtils; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -375,6 +378,22 @@ public boolean onCommand(@NotNull CommandSender commandSender, PluginBackupUtil.backupNow(true); break; + case "setpropertyforeverysingleplayer": + if (!ValidationUtils.validateArgs(commandSender, args, 2)) { + return false; + } + + setPropertyForEverySinglePlayerConfig(args[1], args.length == 3 ? args[2] : null); + break; + + case "setpropertyforeverysinglecourse": + if (!ValidationUtils.validateArgs(commandSender, args, 2, 3)) { + return false; + } + + setPropertyForEverySingleCourseConfig(args[1], args.length == 3 ? args[2] : null); + break; + default: TranslationUtils.sendMessage(commandSender, "Unknown Command. Enter 'pac cmds' to display all console commands."); break; @@ -382,6 +401,31 @@ public boolean onCommand(@NotNull CommandSender commandSender, return true; } + private void setPropertyForEverySinglePlayerConfig(String property, String value) { + parkour.getConfigManager().getAllPlayerUuids() + .forEach(uuid -> { + OfflinePlayer player = PlayerUtils.findPlayer(uuid); + PlayerConfig config = parkour.getConfigManager().getPlayerConfig(player); + if (value == null || value.trim().isEmpty()) { + config.remove(property); + } else { + config.set(property, value); + } + }); + } + + private void setPropertyForEverySingleCourseConfig(String property, String value) { + parkour.getConfigManager().getAllCourseNames() + .forEach(courseName -> { + CourseConfig config = parkour.getConfigManager().getCourseConfig(courseName); + if (value == null || value.trim().isEmpty()) { + config.remove(property); + } else { + config.set(property, value); + } + }); + } + private void displayConsoleCommands() { parkour.getParkourCommands().getCommandUsages().stream() .filter(commandUsage -> commandUsage.getConsoleSyntax() != null) diff --git a/src/main/java/io/github/a5h73y/parkour/type/admin/AdministrationManager.java b/src/main/java/io/github/a5h73y/parkour/type/admin/AdministrationManager.java index 387f479a..4f77e410 100644 --- a/src/main/java/io/github/a5h73y/parkour/type/admin/AdministrationManager.java +++ b/src/main/java/io/github/a5h73y/parkour/type/admin/AdministrationManager.java @@ -6,6 +6,7 @@ import io.github.a5h73y.parkour.Parkour; import io.github.a5h73y.parkour.other.AbstractPluginReceiver; import io.github.a5h73y.parkour.type.player.PlayerConfig; +import io.github.a5h73y.parkour.utility.PlayerUtils; import io.github.a5h73y.parkour.utility.StringUtils; import io.github.a5h73y.parkour.utility.TranslationUtils; import io.github.a5h73y.parkour.utility.ValidationUtils; @@ -13,8 +14,6 @@ import io.github.a5h73y.parkour.utility.permission.PermissionUtils; import java.util.ArrayList; import java.util.List; -import java.util.UUID; -import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -166,13 +165,7 @@ public void processResetCommand(@NotNull CommandSender commandSender, break; case "player": - OfflinePlayer targetPlayer; - - if (ValidationUtils.isUuidFormat(argument)) { - targetPlayer = Bukkit.getOfflinePlayer(UUID.fromString(argument)); - } else { - targetPlayer = Bukkit.getOfflinePlayer(argument); - } + OfflinePlayer targetPlayer = PlayerUtils.findPlayer(argument); if (!PlayerConfig.hasPlayerConfig(targetPlayer)) { TranslationUtils.sendMessage(commandSender, diff --git a/src/main/java/io/github/a5h73y/parkour/type/course/CourseManager.java b/src/main/java/io/github/a5h73y/parkour/type/course/CourseManager.java index dbdc784d..12d6672f 100644 --- a/src/main/java/io/github/a5h73y/parkour/type/course/CourseManager.java +++ b/src/main/java/io/github/a5h73y/parkour/type/course/CourseManager.java @@ -23,7 +23,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; @@ -269,13 +268,7 @@ public void resetPlayerCourseLeaderboards(CommandSender commandSender, String ta return; } - OfflinePlayer targetPlayer; - - if (ValidationUtils.isUuidFormat(targetPlayerId)) { - targetPlayer = Bukkit.getOfflinePlayer(UUID.fromString(targetPlayerId)); - } else { - targetPlayer = Bukkit.getOfflinePlayer(targetPlayerId); - } + OfflinePlayer targetPlayer = PlayerUtils.findPlayer(targetPlayerId); if (!PlayerConfig.hasPlayerConfig(targetPlayer)) { TranslationUtils.sendMessage(commandSender, @@ -311,7 +304,8 @@ public void deleteNthLeaderboardRow(CommandSender commandSender, String courseNa return; } - parkour.getDatabaseManager().deletePlayerCourseTimes(PlayerUtils.findPlayer(result.getPlayerId()), courseName); + parkour.getDatabaseManager().deletePlayerCourseTimes( + PlayerUtils.findDatabasePlayer(result.getPlayerId()), courseName); parkour.getPlaceholderApi().clearCache(); TranslationUtils.sendValueTranslation("Parkour.Delete", "Leaderboard row " + rowNumber, commandSender); PluginUtils.logToFile("Leaderboard row " + rowNumber + " was deleted by " + commandSender.getName()); diff --git a/src/main/java/io/github/a5h73y/parkour/type/player/PlayerManager.java b/src/main/java/io/github/a5h73y/parkour/type/player/PlayerManager.java index 7b128446..8bad2d9b 100644 --- a/src/main/java/io/github/a5h73y/parkour/type/player/PlayerManager.java +++ b/src/main/java/io/github/a5h73y/parkour/type/player/PlayerManager.java @@ -1060,13 +1060,7 @@ public void processSetCommand(CommandSender commandSender, String... args) { * @param targetPlayerId target player identifier */ public void resetPlayer(CommandSender commandSender, String targetPlayerId) { - OfflinePlayer targetPlayer; - - if (ValidationUtils.isUuidFormat(targetPlayerId)) { - targetPlayer = Bukkit.getOfflinePlayer(UUID.fromString(targetPlayerId)); - } else { - targetPlayer = Bukkit.getOfflinePlayer(targetPlayerId); - } + OfflinePlayer targetPlayer = PlayerUtils.findPlayer(targetPlayerId); resetPlayer(targetPlayer); TranslationUtils.sendValueTranslation("Parkour.Reset", targetPlayerId, commandSender); diff --git a/src/main/java/io/github/a5h73y/parkour/upgrade/major/PlayerDataUpgradeTask.java b/src/main/java/io/github/a5h73y/parkour/upgrade/major/PlayerDataUpgradeTask.java index f63e3081..f4ed5a8a 100644 --- a/src/main/java/io/github/a5h73y/parkour/upgrade/major/PlayerDataUpgradeTask.java +++ b/src/main/java/io/github/a5h73y/parkour/upgrade/major/PlayerDataUpgradeTask.java @@ -6,6 +6,7 @@ import io.github.a5h73y.parkour.type.player.session.ParkourSession; import io.github.a5h73y.parkour.type.player.session.ParkourSessionConfig; import io.github.a5h73y.parkour.upgrade.ParkourUpgrader; +import io.github.a5h73y.parkour.utility.PlayerUtils; import io.github.a5h73y.parkour.utility.PluginUtils; import java.io.BufferedInputStream; import java.io.File; @@ -20,9 +21,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import java.util.UUID; import java.util.stream.Stream; -import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; @@ -66,7 +65,7 @@ protected boolean doWork() { } ConfigurationSection playerSection = getConfig().getConfigurationSection(playerId); - OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(playerId)); + OfflinePlayer player = PlayerUtils.findPlayer(playerId); if (playerSection != null) { PlayerConfig newPlayerConfig = PlayerConfig.getConfig(player); diff --git a/src/main/java/io/github/a5h73y/parkour/upgrade/minor/PlayerMinorUpgradeTask.java b/src/main/java/io/github/a5h73y/parkour/upgrade/minor/PlayerMinorUpgradeTask.java index 84e921f2..449311bf 100644 --- a/src/main/java/io/github/a5h73y/parkour/upgrade/minor/PlayerMinorUpgradeTask.java +++ b/src/main/java/io/github/a5h73y/parkour/upgrade/minor/PlayerMinorUpgradeTask.java @@ -5,9 +5,8 @@ import io.github.a5h73y.parkour.type.player.PlayerConfig; import io.github.a5h73y.parkour.upgrade.ParkourUpgrader; import io.github.a5h73y.parkour.upgrade.TimedUpgradeTask; +import io.github.a5h73y.parkour.utility.PlayerUtils; import java.util.List; -import java.util.UUID; -import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; public class PlayerMinorUpgradeTask extends TimedUpgradeTask { @@ -26,7 +25,7 @@ protected boolean doWork() { List uuids = getParkourUpgrader().getNewConfigManager().getAllPlayerUuids(); uuids.forEach(uuid -> { - OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(uuid)); + OfflinePlayer player = PlayerUtils.findPlayer(uuid); PlayerConfig config = getParkourUpgrader().getNewConfigManager().getPlayerConfig(player); updateConfigEntry(config, "Snapshot.JoinLocation", "JoinLocation"); diff --git a/src/main/java/io/github/a5h73y/parkour/utility/PlayerUtils.java b/src/main/java/io/github/a5h73y/parkour/utility/PlayerUtils.java index 4af36f19..e2d6cf61 100644 --- a/src/main/java/io/github/a5h73y/parkour/utility/PlayerUtils.java +++ b/src/main/java/io/github/a5h73y/parkour/utility/PlayerUtils.java @@ -226,7 +226,7 @@ public static String padPlayerUuid(String playerId) { */ @NotNull public static String findPlayerName(String uuid) { - OfflinePlayer player = findPlayer(uuid); + OfflinePlayer player = findDatabasePlayer(uuid); return player.getName() != null ? player.getName() : "Unknown Player"; } @@ -235,10 +235,21 @@ public static String findPlayerName(String uuid) { * @param uuid uuid * @return matching Player */ - public static OfflinePlayer findPlayer(String uuid) { + public static OfflinePlayer findDatabasePlayer(String uuid) { return Bukkit.getOfflinePlayer(UUID.fromString(padPlayerUuid(uuid))); } + public static OfflinePlayer findPlayer(String playerId) { + OfflinePlayer result; + if (ValidationUtils.isUuidFormat(playerId)) { + result = Bukkit.getOfflinePlayer(UUID.fromString(playerId)); + } else { + result = Bukkit.getOfflinePlayer(playerId); + } + + return result; + } + /** * Clear the Player's inventory and Armor. * @param player player diff --git a/src/main/resources/parkourCommands.json b/src/main/resources/parkourCommands.json index d2af9995..c0ed47d2 100644 --- a/src/main/resources/parkourCommands.json +++ b/src/main/resources/parkourCommands.json @@ -398,6 +398,20 @@ "consoleSyntax": "pac reset (course / player / leaderboard / prize / commands) (argument)", "autoTabSyntax": "[course,player,leaderboard,prize,commands] {1:player=(player),*=(course)}" }, + { + "command": "setpropertyforeverysinglecourse", + "title": "Set Property for every Course", + "description": "Set the specified property for every single known Parkour Course, not providing a value will remove the property.", + "consoleSyntax": "pac setpropertyforeverysingleplayer (property) [value]", + "consoleOnly": true + }, + { + "command": "setpropertyforeverysingleplayer", + "title": "Set Property for every Player", + "description": "Set the specified property for every single known Parkour player, not providing a value will remove the property.", + "consoleSyntax": "pac setpropertyforeverysingleplayer (property) [value]", + "consoleOnly": true + }, { "command": "respawn", "title": "Respawn to last Checkpoint",