From 565be7c908108a80111ee36e3b8ee1968e65662e Mon Sep 17 00:00:00 2001 From: Shyanne Date: Thu, 29 Feb 2024 09:53:46 -0500 Subject: [PATCH 1/4] Created the ClearData command - self explanatory, but requires a change to the SQL manager to fix an error --- .../commands/ClearDataCommand.java | 331 ++++++++++++++++++ .../iridiumskyblock/configs/Commands.java | 6 +- .../iridiumskyblock/configs/Messages.java | 3 + .../managers/CommandManager.java | 1 + 4 files changed, 337 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java diff --git a/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java b/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java new file mode 100644 index 000000000..9c478be0e --- /dev/null +++ b/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java @@ -0,0 +1,331 @@ +package com.iridium.iridiumskyblock.commands; + +import com.iridium.iridiumcore.dependencies.xseries.XMaterial; +import com.iridium.iridiumcore.utils.StringUtils; +import com.iridium.iridiumskyblock.IridiumSkyblock; +import com.iridium.iridiumskyblock.database.Island; +import com.iridium.iridiumskyblock.database.LostItems; +import com.iridium.iridiumskyblock.database.User; +import com.iridium.iridiumteams.IridiumTeams; +import com.iridium.iridiumteams.bank.BankItem; +import com.iridium.iridiumteams.commands.Command; +import com.iridium.iridiumteams.database.*; +import com.iridium.iridiumteams.gui.ConfirmationGUI; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.*; +import java.util.stream.Collectors; + +public class ClearDataCommand extends Command { + + public String adminPermission; + + private List deleteTables = Arrays.asList("all", "team", "team_bank", "team_blocks", "team_spawners", + "team_enhancements", "team_invites", "team_trusts", "team_missions", + "team_permissions", "team_settings", "team_warps", "lost_items"); + + public ClearDataCommand() { + super(Collections.singletonList("cleardata"), "Deletes data from database", "%prefix% &7/is cleardata --skip-confirm", "", 10); + this.adminPermission = "iridiumSkyblock.clearData"; + } + + @Override + public boolean execute(User user, String[] args, IridiumTeams iridiumTeams) { + + Player player = user.getPlayer(); + + if(!player.hasPermission(adminPermission)) { + player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().noPermission)); + } + + // COMMAND MAP >> ------------ 0 1 2 + // SCENARIO 0 >> /is cleardata + // SCENARIO 1 >> /is cleardata table + // SCENARIO 2 >> /is cleardata table --skip-confirm + // SCENARIO 3 >> /is cleardata table team + // SCENARIO 4 >> /is cleardata table team --skip-confirm + + // SCENARIO 0 >> /is cleardata + if(args.length == 0 || deleteTables.stream().noneMatch(table -> table.equalsIgnoreCase(args[0]))) { + player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().specifyData + .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))); + return false; + } + + // SCENARIO 1 >> /is cleardata table + if(args.length == 1) { + return confirmDataDeletion(user, args[0]); + } + + // SCENARIO 2 >> /is cleardata table --skip-confirm + if(args.length == 2 && args[1].equalsIgnoreCase("--skip-confirm")) { + return deleteData(user, args[0]); + } + + Optional island = IridiumSkyblock.getInstance().getIslandManager().getTeamViaNameOrPlayer(args[1]); + if(!island.isPresent()) { + player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().teamDoesntExistByName + .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))); + return false; + } + + // SCENARIO 3 >> /is cleardata table team + if(args.length == 2) { + return confirmDataDeletion(user, island.get(), args[0]); + } + + // SCENARIO 4 >> /is cleardata table team --skip-confirm + if(args.length == 3 && args[2].equalsIgnoreCase("--skip-confirm")) { + return deleteData(user, island.get(), args[0]); + } + + player.sendMessage(StringUtils.color(syntax + .replace("%prefix%", iridiumTeams.getConfiguration().prefix) + )); + + return false; + + } + + private boolean confirmDataDeletion(User user, String table) { + Player player = user.getPlayer(); + player.getPlayer().sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().confirmDataDeletion + .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) + .replace("%table%", table) + .replace("%island%", "all"))); + + Bukkit.getScheduler().scheduleSyncDelayedTask(IridiumSkyblock.getInstance(), () -> + player.openInventory(new ConfirmationGUI<>(() -> { + if (!deleteData(user, table)) return; + }, IridiumSkyblock.getInstance()).getInventory()), 100); + + return true; + } + + private boolean deleteData(User user, String table) { + Player player = user.getPlayer(); + boolean clearAllData = false; + + try { + switch (table) { + case "all": { + clearAllData = true; + } + case "team": { + IridiumSkyblock.getInstance().getDatabaseManager().getIslandTableManager().delete( + IridiumSkyblock.getInstance().getIslandManager().getTeams()); + if (!clearAllData) break; + } + case "team_bank": { + for(TeamBank bank : IridiumSkyblock.getInstance().getDatabaseManager().getBankTableManager().getEntries()) { + IridiumSkyblock.getInstance().getDatabaseManager().getBankTableManager().delete(bank); + } + if (!clearAllData) break; + } + case "team_blocks": { + for (TeamBlock block : IridiumSkyblock.getInstance().getDatabaseManager().getTeamBlockTableManager().getEntries()) { + IridiumSkyblock.getInstance().getDatabaseManager().getTeamBlockTableManager().delete(block); + } + if (!clearAllData) break; + } + case "team_enhancements": { + for (TeamEnhancement enhancement : IridiumSkyblock.getInstance().getDatabaseManager().getEnhancementTableManager().getEntries()) { + IridiumSkyblock.getInstance().getDatabaseManager().getEnhancementTableManager().delete(enhancement); + } + if (!clearAllData) break; + } + case "team_invites": { + for (TeamInvite invite : IridiumSkyblock.getInstance().getDatabaseManager().getInvitesTableManager().getEntries()) { + IridiumSkyblock.getInstance().getIslandManager().deleteTeamInvite(invite); + } + if (!clearAllData) break; + } + case "team_missions": { + for (TeamMission mission : IridiumSkyblock.getInstance().getDatabaseManager().getTeamMissionTableManager().getEntries()) { + IridiumSkyblock.getInstance().getIslandManager().deleteTeamMission(mission); + } + if (!clearAllData) break; + } + case "team_permissions": { + for (TeamPermission permission : IridiumSkyblock.getInstance().getDatabaseManager().getPermissionsTableManager().getEntries()) { + IridiumSkyblock.getInstance().getDatabaseManager().getPermissionsTableManager().delete(permission); + } + if (!clearAllData) break; + } + case "team_settings": { + for (TeamSetting setting : IridiumSkyblock.getInstance().getDatabaseManager().getTeamSettingsTableManager().getEntries()) { + IridiumSkyblock.getInstance().getDatabaseManager().getTeamSettingsTableManager().delete(setting); + } + if (!clearAllData) break; + } + case "team_spawners": { + for (TeamSpawners spawner : IridiumSkyblock.getInstance().getDatabaseManager().getTeamSpawnerTableManager().getEntries()) { + IridiumSkyblock.getInstance().getDatabaseManager().getTeamSpawnerTableManager().delete(spawner); + } + if (!clearAllData) break; + } + case "team_trusts": { + for (TeamTrust trust : IridiumSkyblock.getInstance().getDatabaseManager().getTrustTableManager().getEntries()) { + IridiumSkyblock.getInstance().getIslandManager().deleteTeamTrust(trust); + } + if (!clearAllData) break; + } + case "team_warps": { + for (TeamWarp warp : IridiumSkyblock.getInstance().getDatabaseManager().getTeamWarpTableManager().getEntries()) { + IridiumSkyblock.getInstance().getIslandManager().deleteWarp(warp); + } + if (!clearAllData) break; + } + case "lost_items": { + for (LostItems lostItems : IridiumSkyblock.getInstance().getDatabaseManager().getLostItemsTableManager().getEntries()) { + IridiumSkyblock.getInstance().getDatabaseManager().getLostItemsTableManager().delete(lostItems); + } + if (!clearAllData) break; + } + } + } catch (Exception e) { + IridiumSkyblock.getInstance().getLogger().warning(e.getMessage()); + return false; + } + + player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().dataDeletion + .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) + .replace("%player%", player.getName()))); + + return true; + } + + private boolean confirmDataDeletion(User user, Island island, String table) { + Player player = user.getPlayer(); + player.getPlayer().sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().confirmDataDeletion + .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) + .replace("%table%", table) + .replace("%island%", island.getName()))); + + Bukkit.getScheduler().scheduleSyncDelayedTask(IridiumSkyblock.getInstance(), () -> + player.openInventory(new ConfirmationGUI<>(() -> { + if (!deleteData(user, island, table)) return; + }, IridiumSkyblock.getInstance()).getInventory()), 100); + + return true; + } + + private boolean deleteData(User user, Island island, String table) { + Player player = user.getPlayer(); + boolean clearAllData = false; + + try { + switch (table) { + case "all": { + clearAllData = true; + } + case "team": { + IridiumSkyblock.getInstance().getDatabaseManager().getIslandTableManager().delete(island); + if (!clearAllData) break; + } + case "team_bank": { + for (String bankItem : IridiumSkyblock.getInstance().getBankItemList().stream().map(BankItem::getName).collect(Collectors.toList())) { + IridiumSkyblock.getInstance().getDatabaseManager().getBankTableManager().delete( + IridiumSkyblock.getInstance().getIslandManager().getTeamBank(island, bankItem)); + } + if (!clearAllData) break; + } + case "team_blocks": { + for (XMaterial material : XMaterial.values()) { + IridiumSkyblock.getInstance().getDatabaseManager().getTeamBlockTableManager().delete( + IridiumSkyblock.getInstance().getIslandManager().getTeamBlock(island, material)); + } + if (!clearAllData) break; + } + case "team_enhancements": { + for (TeamEnhancement enhancement : IridiumSkyblock.getInstance().getDatabaseManager().getEnhancementTableManager().getEntries(island)) { + IridiumSkyblock.getInstance().getDatabaseManager().getEnhancementTableManager().delete(enhancement); + } + if (!clearAllData) break; + } + case "team_invites": { + for (TeamInvite invite : IridiumSkyblock.getInstance().getDatabaseManager().getInvitesTableManager().getEntries(island)) { + IridiumSkyblock.getInstance().getIslandManager().deleteTeamInvite(invite); + } + if (!clearAllData) break; + } + case "team_missions": { + for (TeamMission mission : IridiumSkyblock.getInstance().getIslandManager().getTeamMissions(island)) { + IridiumSkyblock.getInstance().getIslandManager().deleteTeamMission(mission); + } + if (!clearAllData) break; + } + case "team_permissions": { + for (TeamPermission permission : IridiumSkyblock.getInstance().getDatabaseManager().getPermissionsTableManager().getEntries(island)) { + IridiumSkyblock.getInstance().getDatabaseManager().getPermissionsTableManager().delete(permission); + } + if (!clearAllData) break; + } + case "team_settings": { + for (TeamSetting setting : IridiumSkyblock.getInstance().getDatabaseManager().getTeamSettingsTableManager().getEntries(island)) { + IridiumSkyblock.getInstance().getDatabaseManager().getTeamSettingsTableManager().delete(setting); + } + if (!clearAllData) break; + } + case "team_spawners": { + for (TeamSpawners spawner : IridiumSkyblock.getInstance().getDatabaseManager().getTeamSpawnerTableManager().getEntries(island)) { + IridiumSkyblock.getInstance().getDatabaseManager().getTeamSpawnerTableManager().delete(spawner); + } + if (!clearAllData) break; + } + case "team_trusts": { + for (TeamTrust trust : IridiumSkyblock.getInstance().getIslandManager().getTeamTrusts(island)) { + IridiumSkyblock.getInstance().getIslandManager().deleteTeamTrust(trust); + } + if (!clearAllData) break; + } + case "team_warps": { + for (TeamWarp warp : IridiumSkyblock.getInstance().getIslandManager().getTeamWarps(island)) { + IridiumSkyblock.getInstance().getIslandManager().deleteWarp(warp); + } + if (!clearAllData) break; + } + case "lost_items": { + for(User member : IridiumSkyblock.getInstance().getIslandManager().getTeamMembers(island)) { + IridiumSkyblock.getInstance().getDatabaseManager().getLostItemsTableManager().getEntries() + .stream().filter(lostItems -> lostItems.getUuid().equals(member.getUuid())) + .forEach(lostItems -> IridiumSkyblock.getInstance().getDatabaseManager().getLostItemsTableManager().delete(lostItems)); + } + if (!clearAllData) break; + } + } + } catch (Exception e) { + IridiumSkyblock.getInstance().getLogger().warning(e.getMessage()); + return false; + } + + player.getPlayer().sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().dataDeletion + .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) + .replace("%player%", player.getName()))); + + return true; + } + + @Override + public List onTabComplete(CommandSender commandSender, String[] args, IridiumTeams iridiumTeams) { + if (!commandSender.hasPermission(adminPermission)) return Collections.emptyList(); + + switch (args.length) { + case 1: + return deleteTables; + case 2: { + List fullTabComplete = new ArrayList<>(); + fullTabComplete.addAll(IridiumSkyblock.getInstance().getIslandManager().getTeams().stream().map(Team::getName).collect(Collectors.toList())); + fullTabComplete.addAll(Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList())); + fullTabComplete.add("--skip-confirm"); + return fullTabComplete; + } + case 3: + return Collections.singletonList("--skip-confirm"); + default: + return Collections.emptyList(); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/iridium/iridiumskyblock/configs/Commands.java b/src/main/java/com/iridium/iridiumskyblock/configs/Commands.java index e4b955193..10dded96c 100644 --- a/src/main/java/com/iridium/iridiumskyblock/configs/Commands.java +++ b/src/main/java/com/iridium/iridiumskyblock/configs/Commands.java @@ -1,9 +1,6 @@ package com.iridium.iridiumskyblock.configs; -import com.iridium.iridiumskyblock.commands.BiomeCommand; -import com.iridium.iridiumskyblock.commands.BorderCommand; -import com.iridium.iridiumskyblock.commands.RegenCommand; -import com.iridium.iridiumskyblock.commands.VisitCommand; +import com.iridium.iridiumskyblock.commands.*; import com.iridium.iridiumskyblock.database.Island; import com.iridium.iridiumskyblock.database.User; @@ -16,4 +13,5 @@ public Commands() { public BorderCommand borderCommand = new BorderCommand(); public RegenCommand regenCommand = new RegenCommand(); public BiomeCommand biomeCommand = new BiomeCommand(); + public ClearDataCommand clearDataCommand = new ClearDataCommand(); } diff --git a/src/main/java/com/iridium/iridiumskyblock/configs/Messages.java b/src/main/java/com/iridium/iridiumskyblock/configs/Messages.java index e08245983..76ca1cedc 100644 --- a/src/main/java/com/iridium/iridiumskyblock/configs/Messages.java +++ b/src/main/java/com/iridium/iridiumskyblock/configs/Messages.java @@ -29,4 +29,7 @@ public Messages() { public String noBiomeCategory = "%prefix% &7No biome category with that name."; public String noBiome = "%prefix% &7No biome with that name."; public String changedBiome = "%prefix% &7%player% successfully changed your Island biome to %biome%."; + public String specifyData = "%prefix% &7Due to the nature of this destructive command, you must specify the data to be deleted (or specify \"all\")."; + public String confirmDataDeletion = "%prefix% &7You are about to delete the following data: %table% | [for: %island%] in 5 seconds. &cAre you sure you want to do this? (DATA IS &4&lUNRECOVERABLE&r&c)"; + public String dataDeletion = "%prefix% &cData has been deleted successfully. &7You may need to restart your server for it to take effect."; } diff --git a/src/main/java/com/iridium/iridiumskyblock/managers/CommandManager.java b/src/main/java/com/iridium/iridiumskyblock/managers/CommandManager.java index b92aee648..435017797 100644 --- a/src/main/java/com/iridium/iridiumskyblock/managers/CommandManager.java +++ b/src/main/java/com/iridium/iridiumskyblock/managers/CommandManager.java @@ -21,6 +21,7 @@ public void registerCommands() { registerCommand(IridiumSkyblock.getInstance().getCommands().borderCommand); registerCommand(IridiumSkyblock.getInstance().getCommands().regenCommand); registerCommand(IridiumSkyblock.getInstance().getCommands().biomeCommand); + registerCommand(IridiumSkyblock.getInstance().getCommands().clearDataCommand); } @Override From 3fbd9c218c3c4c528af892b9d42e7bdf01bcb1da Mon Sep 17 00:00:00 2001 From: Shyanne Date: Sat, 18 May 2024 01:50:51 -0400 Subject: [PATCH 2/4] Fixed permissions, refactored, implemented enums, etc. - there is a weird issue where a transaction issue spams the console on specific requests... unsure of the cause, but likely not related to this implementation - should we remove ISLAND from the command? since we have ``/is delete``? --- .../commands/ClearDataCommand.java | 398 ++++++++---------- 1 file changed, 171 insertions(+), 227 deletions(-) diff --git a/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java b/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java index 9c478be0e..e0e5c6601 100644 --- a/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java +++ b/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java @@ -1,13 +1,12 @@ package com.iridium.iridiumskyblock.commands; -import com.iridium.iridiumcore.dependencies.xseries.XMaterial; import com.iridium.iridiumcore.utils.StringUtils; import com.iridium.iridiumskyblock.IridiumSkyblock; import com.iridium.iridiumskyblock.database.Island; -import com.iridium.iridiumskyblock.database.LostItems; import com.iridium.iridiumskyblock.database.User; +import com.iridium.iridiumskyblock.managers.DatabaseManager; +import com.iridium.iridiumskyblock.managers.tablemanagers.*; import com.iridium.iridiumteams.IridiumTeams; -import com.iridium.iridiumteams.bank.BankItem; import com.iridium.iridiumteams.commands.Command; import com.iridium.iridiumteams.database.*; import com.iridium.iridiumteams.gui.ConfirmationGUI; @@ -22,278 +21,205 @@ public class ClearDataCommand extends Command { public String adminPermission; - private List deleteTables = Arrays.asList("all", "team", "team_bank", "team_blocks", "team_spawners", - "team_enhancements", "team_invites", "team_trusts", "team_missions", - "team_permissions", "team_settings", "team_warps", "lost_items"); - public ClearDataCommand() { super(Collections.singletonList("cleardata"), "Deletes data from database", "%prefix% &7/is cleardata
--skip-confirm", "", 10); this.adminPermission = "iridiumSkyblock.clearData"; } @Override - public boolean execute(User user, String[] args, IridiumTeams iridiumTeams) { - - Player player = user.getPlayer(); - - if(!player.hasPermission(adminPermission)) { - player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().noPermission)); - } - - // COMMAND MAP >> ------------ 0 1 2 - // SCENARIO 0 >> /is cleardata - // SCENARIO 1 >> /is cleardata table - // SCENARIO 2 >> /is cleardata table --skip-confirm - // SCENARIO 3 >> /is cleardata table team - // SCENARIO 4 >> /is cleardata table team --skip-confirm + public boolean execute(CommandSender sender, String[] args, IridiumTeams iridiumTeams) { - // SCENARIO 0 >> /is cleardata - if(args.length == 0 || deleteTables.stream().noneMatch(table -> table.equalsIgnoreCase(args[0]))) { - player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().specifyData + if (!sender.hasPermission(adminPermission)) { + sender.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().noPermission .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))); return false; } - // SCENARIO 1 >> /is cleardata table - if(args.length == 1) { - return confirmDataDeletion(user, args[0]); - } - - // SCENARIO 2 >> /is cleardata table --skip-confirm - if(args.length == 2 && args[1].equalsIgnoreCase("--skip-confirm")) { - return deleteData(user, args[0]); - } + DataTable table = DataTable.ALL; + boolean skipConfirmation = false; + Optional island = Optional.empty(); - Optional island = IridiumSkyblock.getInstance().getIslandManager().getTeamViaNameOrPlayer(args[1]); - if(!island.isPresent()) { - player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().teamDoesntExistByName - .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))); + if (args.length == 0 || Arrays.stream(DataTable.values()).noneMatch(dataTable -> dataTable.name().equalsIgnoreCase(args[0]))) { + sender.sendMessage(StringUtils.color(syntax.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))); return false; } - // SCENARIO 3 >> /is cleardata table team - if(args.length == 2) { - return confirmDataDeletion(user, island.get(), args[0]); - } - - // SCENARIO 4 >> /is cleardata table team --skip-confirm - if(args.length == 3 && args[2].equalsIgnoreCase("--skip-confirm")) { - return deleteData(user, island.get(), args[0]); - } - - player.sendMessage(StringUtils.color(syntax - .replace("%prefix%", iridiumTeams.getConfiguration().prefix) - )); - - return false; - - } - - private boolean confirmDataDeletion(User user, String table) { - Player player = user.getPlayer(); - player.getPlayer().sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().confirmDataDeletion - .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) - .replace("%table%", table) - .replace("%island%", "all"))); - - Bukkit.getScheduler().scheduleSyncDelayedTask(IridiumSkyblock.getInstance(), () -> - player.openInventory(new ConfirmationGUI<>(() -> { - if (!deleteData(user, table)) return; - }, IridiumSkyblock.getInstance()).getInventory()), 100); - - return true; - } - - private boolean deleteData(User user, String table) { - Player player = user.getPlayer(); - boolean clearAllData = false; - - try { - switch (table) { - case "all": { - clearAllData = true; - } - case "team": { - IridiumSkyblock.getInstance().getDatabaseManager().getIslandTableManager().delete( - IridiumSkyblock.getInstance().getIslandManager().getTeams()); - if (!clearAllData) break; - } - case "team_bank": { - for(TeamBank bank : IridiumSkyblock.getInstance().getDatabaseManager().getBankTableManager().getEntries()) { - IridiumSkyblock.getInstance().getDatabaseManager().getBankTableManager().delete(bank); - } - if (!clearAllData) break; - } - case "team_blocks": { - for (TeamBlock block : IridiumSkyblock.getInstance().getDatabaseManager().getTeamBlockTableManager().getEntries()) { - IridiumSkyblock.getInstance().getDatabaseManager().getTeamBlockTableManager().delete(block); - } - if (!clearAllData) break; - } - case "team_enhancements": { - for (TeamEnhancement enhancement : IridiumSkyblock.getInstance().getDatabaseManager().getEnhancementTableManager().getEntries()) { - IridiumSkyblock.getInstance().getDatabaseManager().getEnhancementTableManager().delete(enhancement); - } - if (!clearAllData) break; - } - case "team_invites": { - for (TeamInvite invite : IridiumSkyblock.getInstance().getDatabaseManager().getInvitesTableManager().getEntries()) { - IridiumSkyblock.getInstance().getIslandManager().deleteTeamInvite(invite); - } - if (!clearAllData) break; - } - case "team_missions": { - for (TeamMission mission : IridiumSkyblock.getInstance().getDatabaseManager().getTeamMissionTableManager().getEntries()) { - IridiumSkyblock.getInstance().getIslandManager().deleteTeamMission(mission); - } - if (!clearAllData) break; - } - case "team_permissions": { - for (TeamPermission permission : IridiumSkyblock.getInstance().getDatabaseManager().getPermissionsTableManager().getEntries()) { - IridiumSkyblock.getInstance().getDatabaseManager().getPermissionsTableManager().delete(permission); - } - if (!clearAllData) break; - } - case "team_settings": { - for (TeamSetting setting : IridiumSkyblock.getInstance().getDatabaseManager().getTeamSettingsTableManager().getEntries()) { - IridiumSkyblock.getInstance().getDatabaseManager().getTeamSettingsTableManager().delete(setting); - } - if (!clearAllData) break; - } - case "team_spawners": { - for (TeamSpawners spawner : IridiumSkyblock.getInstance().getDatabaseManager().getTeamSpawnerTableManager().getEntries()) { - IridiumSkyblock.getInstance().getDatabaseManager().getTeamSpawnerTableManager().delete(spawner); - } - if (!clearAllData) break; - } - case "team_trusts": { - for (TeamTrust trust : IridiumSkyblock.getInstance().getDatabaseManager().getTrustTableManager().getEntries()) { - IridiumSkyblock.getInstance().getIslandManager().deleteTeamTrust(trust); - } - if (!clearAllData) break; - } - case "team_warps": { - for (TeamWarp warp : IridiumSkyblock.getInstance().getDatabaseManager().getTeamWarpTableManager().getEntries()) { - IridiumSkyblock.getInstance().getIslandManager().deleteWarp(warp); + switch (args.length) { + case 2: { + if (!args[1].equalsIgnoreCase("--skip-confirm")) { + island = IridiumSkyblock.getInstance().getIslandManager().getTeamViaNameOrPlayer(args[1]); + if (!island.isPresent()) { + sender.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().teamDoesntExistByName + .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))); + return false; } - if (!clearAllData) break; } - case "lost_items": { - for (LostItems lostItems : IridiumSkyblock.getInstance().getDatabaseManager().getLostItemsTableManager().getEntries()) { - IridiumSkyblock.getInstance().getDatabaseManager().getLostItemsTableManager().delete(lostItems); - } - if (!clearAllData) break; + } + case 1: { + try { + if (Arrays.stream(DataTable.values()).anyMatch(dataTable -> dataTable.name().equalsIgnoreCase(args[0]))) + table = DataTable.valueOf(args[0]); + } catch (IllegalArgumentException e) { + IridiumSkyblock.getInstance().getLogger().warning(e.getMessage()); + return false; } } - } catch (Exception e) { - IridiumSkyblock.getInstance().getLogger().warning(e.getMessage()); - return false; } - player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().dataDeletion - .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) - .replace("%player%", player.getName()))); + if (Arrays.stream(args).anyMatch(argument -> argument.equalsIgnoreCase("--skip-confirm"))) + skipConfirmation = true; - return true; + if(sender instanceof Player && !skipConfirmation) { + confirmDataDeletion((Player) sender, island, table); + return true; + } + return deleteData(sender, island, table); } - private boolean confirmDataDeletion(User user, Island island, String table) { - Player player = user.getPlayer(); - player.getPlayer().sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().confirmDataDeletion + private void confirmDataDeletion(Player player, Optional island, DataTable table) { + String islandName = "ALL"; + if (island.isPresent()) islandName = island.get().getName() + " (" + island.get().getOwner().get().getName() + ")"; + player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().confirmDataDeletion .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) - .replace("%table%", table) - .replace("%island%", island.getName()))); + .replace("%table%", table.name()) + .replace("%island%", islandName))); Bukkit.getScheduler().scheduleSyncDelayedTask(IridiumSkyblock.getInstance(), () -> player.openInventory(new ConfirmationGUI<>(() -> { - if (!deleteData(user, island, table)) return; + if (!deleteData(player, island, table)) return; }, IridiumSkyblock.getInstance()).getInventory()), 100); - - return true; } - private boolean deleteData(User user, Island island, String table) { - Player player = user.getPlayer(); - boolean clearAllData = false; + private boolean deleteData(CommandSender sender, Optional island, DataTable table) { + + List islands = new ArrayList<>(); + if (!island.isPresent()) { islands = IridiumSkyblock.getInstance().getIslandManager().getTeams(); } + else islands.add(island.get()); + + boolean all = table == DataTable.ALL; + + DatabaseManager databaseManager = IridiumSkyblock.getInstance().getDatabaseManager(); try { switch (table) { - case "all": { - clearAllData = true; + case ALL: { } - case "team": { - IridiumSkyblock.getInstance().getDatabaseManager().getIslandTableManager().delete(island); - if (!clearAllData) break; + case ISLAND: { + databaseManager.getIslandTableManager().delete(islands); + if (!all) break; } - case "team_bank": { - for (String bankItem : IridiumSkyblock.getInstance().getBankItemList().stream().map(BankItem::getName).collect(Collectors.toList())) { - IridiumSkyblock.getInstance().getDatabaseManager().getBankTableManager().delete( - IridiumSkyblock.getInstance().getIslandManager().getTeamBank(island, bankItem)); + case INVITE: { + ForeignIslandTableManager inviteTableManager = databaseManager.getInvitesTableManager(); + for (Island islandEntry : islands) { + for (TeamInvite teamInvite : inviteTableManager.getEntries(islandEntry)) { + inviteTableManager.delete(teamInvite); + teamInvite.setChanged(true); + } } - if (!clearAllData) break; + if (!all) break; } - case "team_blocks": { - for (XMaterial material : XMaterial.values()) { - IridiumSkyblock.getInstance().getDatabaseManager().getTeamBlockTableManager().delete( - IridiumSkyblock.getInstance().getIslandManager().getTeamBlock(island, material)); + case TRUST: { + ForeignIslandTableManager trustTableManager = databaseManager.getTrustTableManager(); + for (Island islandEntry : islands) { + for (TeamTrust teamTrust : trustTableManager.getEntries(islandEntry)) { + trustTableManager.delete(teamTrust); + teamTrust.setChanged(true); + } } - if (!clearAllData) break; + if (!all) break; } - case "team_enhancements": { - for (TeamEnhancement enhancement : IridiumSkyblock.getInstance().getDatabaseManager().getEnhancementTableManager().getEntries(island)) { - IridiumSkyblock.getInstance().getDatabaseManager().getEnhancementTableManager().delete(enhancement); + case PERMISSION: { + ForeignIslandTableManager permissionTableManager = databaseManager.getPermissionsTableManager(); + for (Island islandEntry : islands) { + for (TeamPermission teamPermission : permissionTableManager.getEntries(islandEntry)) { + permissionTableManager.delete(teamPermission); + teamPermission.setChanged(true); + } } - if (!clearAllData) break; + if (!all) break; } - case "team_invites": { - for (TeamInvite invite : IridiumSkyblock.getInstance().getDatabaseManager().getInvitesTableManager().getEntries(island)) { - IridiumSkyblock.getInstance().getIslandManager().deleteTeamInvite(invite); + case BANK: { + ForeignIslandTableManager bankTableManager = databaseManager.getBankTableManager(); + for (Island islandEntry : islands) { + for (TeamBank teamBank : bankTableManager.getEntries(islandEntry)) { + bankTableManager.delete(teamBank); + teamBank.setChanged(true); + } } - if (!clearAllData) break; + if (!all) break; } - case "team_missions": { - for (TeamMission mission : IridiumSkyblock.getInstance().getIslandManager().getTeamMissions(island)) { - IridiumSkyblock.getInstance().getIslandManager().deleteTeamMission(mission); + case ENHANCEMENT: { + ForeignIslandTableManager enhancementTableManager = databaseManager.getEnhancementTableManager(); + for (Island islandEntry : islands) { + for (TeamEnhancement teamEnhancement : enhancementTableManager.getEntries(islandEntry)) { + enhancementTableManager.delete(teamEnhancement); + teamEnhancement.setChanged(true); + } } - if (!clearAllData) break; + if (!all) break; } - case "team_permissions": { - for (TeamPermission permission : IridiumSkyblock.getInstance().getDatabaseManager().getPermissionsTableManager().getEntries(island)) { - IridiumSkyblock.getInstance().getDatabaseManager().getPermissionsTableManager().delete(permission); + case TEAM_BLOCK: { + ForeignIslandTableManager blockTableManager = databaseManager.getTeamBlockTableManager(); + for (Island islandEntry : islands) { + for (TeamBlock teamBlock : blockTableManager.getEntries(islandEntry)) { + blockTableManager.delete(teamBlock); + teamBlock.setChanged(true); + } } - if (!clearAllData) break; + if (!all) break; } - case "team_settings": { - for (TeamSetting setting : IridiumSkyblock.getInstance().getDatabaseManager().getTeamSettingsTableManager().getEntries(island)) { - IridiumSkyblock.getInstance().getDatabaseManager().getTeamSettingsTableManager().delete(setting); + case TEAM_SPAWNER: { + ForeignIslandTableManager spawnerTableManager = databaseManager.getTeamSpawnerTableManager(); + for (Island islandEntry : islands) { + for (TeamSpawners teamSpawners : spawnerTableManager.getEntries(islandEntry)) { + spawnerTableManager.delete(teamSpawners); + teamSpawners.setChanged(true); + } } - if (!clearAllData) break; + if (!all) break; } - case "team_spawners": { - for (TeamSpawners spawner : IridiumSkyblock.getInstance().getDatabaseManager().getTeamSpawnerTableManager().getEntries(island)) { - IridiumSkyblock.getInstance().getDatabaseManager().getTeamSpawnerTableManager().delete(spawner); + case TEAM_WARP: { + ForeignIslandTableManager warpTableManager = databaseManager.getTeamWarpTableManager(); + for (Island islandEntry : islands) { + for (TeamWarp teamWarp : warpTableManager.getEntries(islandEntry)) { + warpTableManager.delete(teamWarp); + teamWarp.setChanged(true); + } } - if (!clearAllData) break; + if (!all) break; } - case "team_trusts": { - for (TeamTrust trust : IridiumSkyblock.getInstance().getIslandManager().getTeamTrusts(island)) { - IridiumSkyblock.getInstance().getIslandManager().deleteTeamTrust(trust); + case TEAM_MISSION: { + ForeignIslandTableManager missionTableManager = databaseManager.getTeamMissionTableManager(); + for (Island islandEntry : islands) { + for (TeamMission teamMission : missionTableManager.getEntries(islandEntry)) { + missionTableManager.delete(teamMission); + teamMission.setChanged(true); + } } - if (!clearAllData) break; + // there is not a break here, because if you're deleting missions, you want to also delete their data. + if (!all) break; } - case "team_warps": { - for (TeamWarp warp : IridiumSkyblock.getInstance().getIslandManager().getTeamWarps(island)) { - IridiumSkyblock.getInstance().getIslandManager().deleteWarp(warp); + + // TODO: team mission data needs some coddling apparently + + case TEAM_REWARDS: { + ForeignIslandTableManager rewardTableManager = databaseManager.getTeamRewardsTableManager(); + for (Island islandEntry : islands) { + for (TeamReward teamReward : rewardTableManager.getEntries(islandEntry)) { + rewardTableManager.delete(teamReward); + teamReward.setChanged(true); + } } - if (!clearAllData) break; + if (!all) break; } - case "lost_items": { - for(User member : IridiumSkyblock.getInstance().getIslandManager().getTeamMembers(island)) { - IridiumSkyblock.getInstance().getDatabaseManager().getLostItemsTableManager().getEntries() - .stream().filter(lostItems -> lostItems.getUuid().equals(member.getUuid())) - .forEach(lostItems -> IridiumSkyblock.getInstance().getDatabaseManager().getLostItemsTableManager().delete(lostItems)); + case TEAM_SETTINGS: { + ForeignIslandTableManager settingTableManager = databaseManager.getTeamSettingsTableManager(); + for (Island islandEntry : islands) { + for (TeamSetting teamSetting : settingTableManager.getEntries(islandEntry)) { + settingTableManager.delete(teamSetting); + teamSetting.setChanged(true); + } } - if (!clearAllData) break; } } } catch (Exception e) { @@ -301,10 +227,8 @@ private boolean deleteData(User user, Island island, String table) { return false; } - player.getPlayer().sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().dataDeletion - .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) - .replace("%player%", player.getName()))); - + sender.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().dataDeletion + .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))); return true; } @@ -313,12 +237,15 @@ public List onTabComplete(CommandSender commandSender, String[] args, Ir if (!commandSender.hasPermission(adminPermission)) return Collections.emptyList(); switch (args.length) { - case 1: - return deleteTables; + case 1: { + List tables = new ArrayList<>(); + for(DataTable table : DataTable.values()) { + tables.add(table.name()); + } + return tables; + } case 2: { - List fullTabComplete = new ArrayList<>(); - fullTabComplete.addAll(IridiumSkyblock.getInstance().getIslandManager().getTeams().stream().map(Team::getName).collect(Collectors.toList())); - fullTabComplete.addAll(Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList())); + List fullTabComplete = Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()); fullTabComplete.add("--skip-confirm"); return fullTabComplete; } @@ -328,4 +255,21 @@ public List onTabComplete(CommandSender commandSender, String[] args, Ir return Collections.emptyList(); } } +} + +enum DataTable { + ALL, + ISLAND, + // TEAM_MISSION_DATA, + INVITE, + TRUST, + PERMISSION, + BANK, + ENHANCEMENT, + TEAM_BLOCK, + TEAM_SPAWNER, + TEAM_WARP, + TEAM_MISSION, + TEAM_REWARDS, + TEAM_SETTINGS; } \ No newline at end of file From a8dafbd23a0193d13cad94929752c1f784f11339 Mon Sep 17 00:00:00 2001 From: Shyanne Date: Sat, 18 May 2024 11:32:35 -0400 Subject: [PATCH 3/4] Added database backup --- .../commands/ClearDataCommand.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java b/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java index e0e5c6601..eb84648b1 100644 --- a/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java +++ b/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java @@ -14,6 +14,10 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.util.*; import java.util.stream.Collectors; @@ -92,6 +96,8 @@ private void confirmDataDeletion(Player player, Optional island, DataTab private boolean deleteData(CommandSender sender, Optional island, DataTable table) { + if (!backupDatabaseFile()) return false; + List islands = new ArrayList<>(); if (!island.isPresent()) { islands = IridiumSkyblock.getInstance().getIslandManager().getTeams(); } else islands.add(island.get()); @@ -232,6 +238,26 @@ private boolean deleteData(CommandSender sender, Optional island, DataTa return true; } + private boolean backupDatabaseFile() { + IridiumSkyblock.getInstance().getLogger().info("Creating a backup for IridiumSkyblock.db in \"backups\" folder..."); + + File pluginFolder = new File(IridiumSkyblock.getInstance().getDataFolder().getPath()); + File file = new File(pluginFolder + File.separator + "IridiumSkyblock.db"); + File backupFolder = new File(pluginFolder.getPath() + File.separator + "backups"); + File backupDatabaseFile = new File(backupFolder + File.separator + file.getName()); + + try { + if (!backupFolder.exists()) backupFolder.mkdir(); + Files.copy(file.toPath(), backupDatabaseFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + IridiumSkyblock.getInstance().getLogger().info("Success! Backup \"IridiumSkyblock.db\" created, check \"" + backupFolder.getPath() + "\"."); + return true; + } catch (IOException exception) { + IridiumSkyblock.getInstance().getLogger().severe("Failed to move \"IridiumSkyblock.db\" to " + backupFolder + ": " + + exception.getMessage()); + return false; + } + } + @Override public List onTabComplete(CommandSender commandSender, String[] args, IridiumTeams iridiumTeams) { if (!commandSender.hasPermission(adminPermission)) return Collections.emptyList(); From 6b4aec443891d001e1b5b53f69f795d199b9b802 Mon Sep 17 00:00:00 2001 From: Peaches_MLG Date: Sat, 28 Dec 2024 14:53:23 +0000 Subject: [PATCH 4/4] . --- .../commands/ClearDataCommand.java | 257 ++++++++---------- 1 file changed, 116 insertions(+), 141 deletions(-) diff --git a/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java b/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java index eb84648b1..a5d275293 100644 --- a/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java +++ b/src/main/java/com/iridium/iridiumskyblock/commands/ClearDataCommand.java @@ -19,29 +19,20 @@ import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; -public class ClearDataCommand extends Command { - - public String adminPermission; +public class ClearDataCommand extends Command < Island, User > { public ClearDataCommand() { - super(Collections.singletonList("cleardata"), "Deletes data from database", "%prefix% &7/is cleardata
--skip-confirm", "", 10); - this.adminPermission = "iridiumSkyblock.clearData"; + super(Collections.singletonList("cleardata"), "Deletes data from database", "%prefix% &7/is cleardata
--skip-confirm", "iridiumSkyblock.clearData", 10); } @Override - public boolean execute(CommandSender sender, String[] args, IridiumTeams iridiumTeams) { - - if (!sender.hasPermission(adminPermission)) { - sender.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().noPermission - .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))); - return false; - } - + public boolean execute(CommandSender sender, String[] args, IridiumTeams < Island, User > iridiumTeams) { DataTable table = DataTable.ALL; boolean skipConfirmation = false; - Optional island = Optional.empty(); + Optional < Island > island = Optional.empty(); if (args.length == 0 || Arrays.stream(DataTable.values()).noneMatch(dataTable -> dataTable.name().equalsIgnoreCase(args[0]))) { sender.sendMessage(StringUtils.color(syntax.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))); @@ -73,14 +64,16 @@ public boolean execute(CommandSender sender, String[] args, IridiumTeams argument.equalsIgnoreCase("--skip-confirm"))) skipConfirmation = true; - if(sender instanceof Player && !skipConfirmation) { + if (sender instanceof Player && !skipConfirmation) { confirmDataDeletion((Player) sender, island, table); return true; } - return deleteData(sender, island, table); + deleteData(sender, island, table); + + return true; } - private void confirmDataDeletion(Player player, Optional island, DataTable table) { + private void confirmDataDeletion(Player player, Optional < Island > island, DataTable table) { String islandName = "ALL"; if (island.isPresent()) islandName = island.get().getName() + " (" + island.get().getOwner().get().getName() + ")"; player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().confirmDataDeletion @@ -88,154 +81,139 @@ private void confirmDataDeletion(Player player, Optional island, DataTab .replace("%table%", table.name()) .replace("%island%", islandName))); - Bukkit.getScheduler().scheduleSyncDelayedTask(IridiumSkyblock.getInstance(), () -> - player.openInventory(new ConfirmationGUI<>(() -> { - if (!deleteData(player, island, table)) return; - }, IridiumSkyblock.getInstance()).getInventory()), 100); + player.openInventory(new ConfirmationGUI < > (() -> deleteData(player, island, table), IridiumSkyblock.getInstance()).getInventory()); } - private boolean deleteData(CommandSender sender, Optional island, DataTable table) { + private CompletableFuture < Void > deleteData(CommandSender sender, Optional < Island > island, DataTable table) { + return CompletableFuture.runAsync(() -> { - if (!backupDatabaseFile()) return false; + if (!backupDatabaseFile()) return; - List islands = new ArrayList<>(); - if (!island.isPresent()) { islands = IridiumSkyblock.getInstance().getIslandManager().getTeams(); } - else islands.add(island.get()); + List < Island > islands = new ArrayList < > (); + if (!island.isPresent()) { + islands = IridiumSkyblock.getInstance().getIslandManager().getTeams(); + } else islands.add(island.get()); - boolean all = table == DataTable.ALL; + boolean all = table == DataTable.ALL; - DatabaseManager databaseManager = IridiumSkyblock.getInstance().getDatabaseManager(); + DatabaseManager databaseManager = IridiumSkyblock.getInstance().getDatabaseManager(); - try { - switch (table) { - case ALL: { - } - case ISLAND: { - databaseManager.getIslandTableManager().delete(islands); - if (!all) break; - } - case INVITE: { - ForeignIslandTableManager inviteTableManager = databaseManager.getInvitesTableManager(); - for (Island islandEntry : islands) { - for (TeamInvite teamInvite : inviteTableManager.getEntries(islandEntry)) { - inviteTableManager.delete(teamInvite); - teamInvite.setChanged(true); + try { + switch (table) { + case ALL: + case ISLAND: { + databaseManager.getIslandTableManager().delete(islands).join(); + if (!all) break; + } + case INVITE: { + ForeignIslandTableManager < String, TeamInvite > inviteTableManager = databaseManager.getInvitesTableManager(); + for (Island islandEntry: islands) { + for (TeamInvite teamInvite: inviteTableManager.getEntries(islandEntry)) { + inviteTableManager.delete(teamInvite).join(); + } } + if (!all) break; } - if (!all) break; - } - case TRUST: { - ForeignIslandTableManager trustTableManager = databaseManager.getTrustTableManager(); - for (Island islandEntry : islands) { - for (TeamTrust teamTrust : trustTableManager.getEntries(islandEntry)) { - trustTableManager.delete(teamTrust); - teamTrust.setChanged(true); + case TRUST: { + ForeignIslandTableManager < String, TeamTrust > trustTableManager = databaseManager.getTrustTableManager(); + for (Island islandEntry: islands) { + for (TeamTrust teamTrust: trustTableManager.getEntries(islandEntry)) { + trustTableManager.delete(teamTrust).join(); + } } + if (!all) break; } - if (!all) break; - } - case PERMISSION: { - ForeignIslandTableManager permissionTableManager = databaseManager.getPermissionsTableManager(); - for (Island islandEntry : islands) { - for (TeamPermission teamPermission : permissionTableManager.getEntries(islandEntry)) { - permissionTableManager.delete(teamPermission); - teamPermission.setChanged(true); + case PERMISSION: { + ForeignIslandTableManager < String, TeamPermission > permissionTableManager = databaseManager.getPermissionsTableManager(); + for (Island islandEntry: islands) { + for (TeamPermission teamPermission: permissionTableManager.getEntries(islandEntry)) { + permissionTableManager.delete(teamPermission).join(); + } } + if (!all) break; } - if (!all) break; - } - case BANK: { - ForeignIslandTableManager bankTableManager = databaseManager.getBankTableManager(); - for (Island islandEntry : islands) { - for (TeamBank teamBank : bankTableManager.getEntries(islandEntry)) { - bankTableManager.delete(teamBank); - teamBank.setChanged(true); + case BANK: { + ForeignIslandTableManager < String, TeamBank > bankTableManager = databaseManager.getBankTableManager(); + for (Island islandEntry: islands) { + for (TeamBank teamBank: bankTableManager.getEntries(islandEntry)) { + bankTableManager.delete(teamBank).join(); + } } + if (!all) break; } - if (!all) break; - } - case ENHANCEMENT: { - ForeignIslandTableManager enhancementTableManager = databaseManager.getEnhancementTableManager(); - for (Island islandEntry : islands) { - for (TeamEnhancement teamEnhancement : enhancementTableManager.getEntries(islandEntry)) { - enhancementTableManager.delete(teamEnhancement); - teamEnhancement.setChanged(true); + case ENHANCEMENT: { + ForeignIslandTableManager < String, TeamEnhancement > enhancementTableManager = databaseManager.getEnhancementTableManager(); + for (Island islandEntry: islands) { + for (TeamEnhancement teamEnhancement: enhancementTableManager.getEntries(islandEntry)) { + enhancementTableManager.delete(teamEnhancement).join(); + } } + if (!all) break; } - if (!all) break; - } - case TEAM_BLOCK: { - ForeignIslandTableManager blockTableManager = databaseManager.getTeamBlockTableManager(); - for (Island islandEntry : islands) { - for (TeamBlock teamBlock : blockTableManager.getEntries(islandEntry)) { - blockTableManager.delete(teamBlock); - teamBlock.setChanged(true); + case TEAM_BLOCK: { + ForeignIslandTableManager < String, TeamBlock > blockTableManager = databaseManager.getTeamBlockTableManager(); + for (Island islandEntry: islands) { + for (TeamBlock teamBlock: blockTableManager.getEntries(islandEntry)) { + blockTableManager.delete(teamBlock).join(); + } } + if (!all) break; } - if (!all) break; - } - case TEAM_SPAWNER: { - ForeignIslandTableManager spawnerTableManager = databaseManager.getTeamSpawnerTableManager(); - for (Island islandEntry : islands) { - for (TeamSpawners teamSpawners : spawnerTableManager.getEntries(islandEntry)) { - spawnerTableManager.delete(teamSpawners); - teamSpawners.setChanged(true); + case TEAM_SPAWNER: { + ForeignIslandTableManager < String, TeamSpawners > spawnerTableManager = databaseManager.getTeamSpawnerTableManager(); + for (Island islandEntry: islands) { + for (TeamSpawners teamSpawners: spawnerTableManager.getEntries(islandEntry)) { + spawnerTableManager.delete(teamSpawners).join(); + } } + if (!all) break; } - if (!all) break; - } - case TEAM_WARP: { - ForeignIslandTableManager warpTableManager = databaseManager.getTeamWarpTableManager(); - for (Island islandEntry : islands) { - for (TeamWarp teamWarp : warpTableManager.getEntries(islandEntry)) { - warpTableManager.delete(teamWarp); - teamWarp.setChanged(true); + case TEAM_WARP: { + ForeignIslandTableManager < String, TeamWarp > warpTableManager = databaseManager.getTeamWarpTableManager(); + for (Island islandEntry: islands) { + for (TeamWarp teamWarp: warpTableManager.getEntries(islandEntry)) { + warpTableManager.delete(teamWarp).join(); + } } + if (!all) break; } - if (!all) break; - } - case TEAM_MISSION: { - ForeignIslandTableManager missionTableManager = databaseManager.getTeamMissionTableManager(); - for (Island islandEntry : islands) { - for (TeamMission teamMission : missionTableManager.getEntries(islandEntry)) { - missionTableManager.delete(teamMission); - teamMission.setChanged(true); + case TEAM_MISSION: { + ForeignIslandTableManager < String, TeamMission > missionTableManager = databaseManager.getTeamMissionTableManager(); + for (Island islandEntry: islands) { + for (TeamMission teamMission: missionTableManager.getEntries(islandEntry)) { + IridiumSkyblock.getInstance().getIslandManager().deleteTeamMission(teamMission); + IridiumSkyblock.getInstance().getIslandManager().deleteTeamMissionData(teamMission); + } } + if (!all) break; } - // there is not a break here, because if you're deleting missions, you want to also delete their data. - if (!all) break; - } - // TODO: team mission data needs some coddling apparently - - case TEAM_REWARDS: { - ForeignIslandTableManager rewardTableManager = databaseManager.getTeamRewardsTableManager(); - for (Island islandEntry : islands) { - for (TeamReward teamReward : rewardTableManager.getEntries(islandEntry)) { - rewardTableManager.delete(teamReward); - teamReward.setChanged(true); + case TEAM_REWARDS: { + ForeignIslandTableManager < String, TeamReward > rewardTableManager = databaseManager.getTeamRewardsTableManager(); + for (Island islandEntry: islands) { + for (TeamReward teamReward: rewardTableManager.getEntries(islandEntry)) { + rewardTableManager.delete(teamReward).join(); + } } + if (!all) break; } - if (!all) break; - } - case TEAM_SETTINGS: { - ForeignIslandTableManager settingTableManager = databaseManager.getTeamSettingsTableManager(); - for (Island islandEntry : islands) { - for (TeamSetting teamSetting : settingTableManager.getEntries(islandEntry)) { - settingTableManager.delete(teamSetting); - teamSetting.setChanged(true); + case TEAM_SETTINGS: { + ForeignIslandTableManager < String, TeamSetting > settingTableManager = databaseManager.getTeamSettingsTableManager(); + for (Island islandEntry: islands) { + for (TeamSetting teamSetting: settingTableManager.getEntries(islandEntry)) { + settingTableManager.delete(teamSetting).join(); + } } } } + } catch (Exception e) { + IridiumSkyblock.getInstance().getLogger().warning(e.getMessage()); + return; } - } catch (Exception e) { - IridiumSkyblock.getInstance().getLogger().warning(e.getMessage()); - return false; - } - sender.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().dataDeletion - .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))); - return true; + sender.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().dataDeletion + .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))); + }); } private boolean backupDatabaseFile() { @@ -252,26 +230,24 @@ private boolean backupDatabaseFile() { IridiumSkyblock.getInstance().getLogger().info("Success! Backup \"IridiumSkyblock.db\" created, check \"" + backupFolder.getPath() + "\"."); return true; } catch (IOException exception) { - IridiumSkyblock.getInstance().getLogger().severe("Failed to move \"IridiumSkyblock.db\" to " + backupFolder + ": " - + exception.getMessage()); + IridiumSkyblock.getInstance().getLogger().severe("Failed to move \"IridiumSkyblock.db\" to " + backupFolder + ": " + + exception.getMessage()); return false; } } @Override - public List onTabComplete(CommandSender commandSender, String[] args, IridiumTeams iridiumTeams) { - if (!commandSender.hasPermission(adminPermission)) return Collections.emptyList(); - + public List < String > onTabComplete(CommandSender commandSender, String[] args, IridiumTeams < Island, User > iridiumTeams) { switch (args.length) { case 1: { - List tables = new ArrayList<>(); - for(DataTable table : DataTable.values()) { + List < String > tables = new ArrayList < > (); + for (DataTable table: DataTable.values()) { tables.add(table.name()); } return tables; } case 2: { - List fullTabComplete = Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()); + List < String > fullTabComplete = Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()); fullTabComplete.add("--skip-confirm"); return fullTabComplete; } @@ -286,7 +262,6 @@ public List onTabComplete(CommandSender commandSender, String[] args, Ir enum DataTable { ALL, ISLAND, - // TEAM_MISSION_DATA, INVITE, TRUST, PERMISSION,