diff --git a/src/main/java/com/songoda/skyblock/command/CommandManager.java b/src/main/java/com/songoda/skyblock/command/CommandManager.java index 8e8b6f02..c541b7da 100644 --- a/src/main/java/com/songoda/skyblock/command/CommandManager.java +++ b/src/main/java/com/songoda/skyblock/command/CommandManager.java @@ -118,7 +118,8 @@ public class CommandManager implements CommandExecutor, TabCompleter { new StackableCommand(), new AdminBank(), new SetMaxMembers(), - new ChatSpyCommand() + new ChatSpyCommand(), + new UpdateAllIslandsCommand() ); } diff --git a/src/main/java/com/songoda/skyblock/command/commands/admin/UpdateAllIslandsCommand.java b/src/main/java/com/songoda/skyblock/command/commands/admin/UpdateAllIslandsCommand.java new file mode 100644 index 00000000..7660612d --- /dev/null +++ b/src/main/java/com/songoda/skyblock/command/commands/admin/UpdateAllIslandsCommand.java @@ -0,0 +1,98 @@ +package com.songoda.skyblock.command.commands.admin; + +import com.songoda.core.compatibility.CompatibleSound; +import com.songoda.skyblock.command.SubCommand; +import com.songoda.skyblock.config.FileManager; +import com.songoda.skyblock.island.IslandManager; +import com.songoda.skyblock.message.MessageManager; +import com.songoda.skyblock.sound.SoundManager; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; + +import java.io.File; + +public class UpdateAllIslandsCommand extends SubCommand { + + @Override + public void onCommandByPlayer(Player player, String[] args) { + processCommand(player, args); + } + + @Override + public void onCommandByConsole(ConsoleCommandSender sender, String[] args) { + processCommand(sender, args); + } + + public void processCommand(CommandSender sender, String[] args) { + MessageManager messageManager = plugin.getMessageManager(); + IslandManager islandManager = plugin.getIslandManager(); + FileManager fileManager = plugin.getFileManager(); + SoundManager soundManager = plugin.getSoundManager(); + + FileManager.Config config = fileManager.getConfig(new File(plugin.getDataFolder(), "language.yml")); + FileConfiguration configLoad = config.getFileConfiguration(); + + if (args.length < 1) { + messageManager.sendMessage(sender, configLoad.getString("Command.Island.Admin.UpdateAllIslands.NoArgs.Message")); + soundManager.playSound(sender, CompatibleSound.BLOCK_ANVIL_LAND.getSound(), 1f, 1f); + } else { + switch (args[0].toLowerCase()) { + case "setsize": + if (args.length >= 2) { + int size; + try { + size = Integer.parseInt(args[1]); + messageManager.sendMessage(sender, configLoad.getString("Command.Island.Admin.UpdateAllIslands.Start.Message")); + islandManager.setAllIslandsSize(Math.abs(size), () -> + messageManager.sendMessage(sender, configLoad.getString("Command.Island.Admin.UpdateAllIslands.Finished.Message"))); + } catch (NumberFormatException ignored) { + messageManager.sendMessage(sender, configLoad.getString("Command.Island.Admin.UpdateAllIslands.Unexpected.Message")); + } + } else { + messageManager.sendMessage(sender, configLoad.getString("Command.Island.Admin.UpdateAllIslands.NotANumber.Message")); + } + return; + case "adjustsize": + if (args.length >= 2) { + int size; + try { + size = Integer.parseInt(args[1]); + messageManager.sendMessage(sender, configLoad.getString("Command.Island.Admin.UpdateAllIslands.Start.Message")); + islandManager.adjustAllIslandsSize(size, () -> + messageManager.sendMessage(sender, configLoad.getString("Command.Island.Admin.UpdateAllIslands.Finished.Message"))); + } catch (NumberFormatException ignored) { + messageManager.sendMessage(sender, configLoad.getString("Command.Island.Admin.UpdateAllIslands.NotANumber.Message")); + } + } else { + messageManager.sendMessage(sender, configLoad.getString("Command.Island.Admin.UpdateAllIslands.Unexpected.Message")); + } + return; + default: + messageManager.sendMessage(sender, configLoad.getString("Command.Island.Admin.UpdateAllIslands.Unexpected.Message")); + soundManager.playSound(sender, CompatibleSound.BLOCK_ANVIL_LAND.getSound(), 1f, 1f); + } + } + } + + @Override + public String getName() { + return "updateallIslands"; + } + + @Override + public String getInfoMessagePath() { + return "Command.Island.Admin.UpdateAllIslands.Info.Message"; + } + + @Override + public String[] getAliases() { + return new String[0]; + } + + @Override + public String[] getArguments() { + return new String[] {"SetSize", "AdjustSize"}; + } +} diff --git a/src/main/java/com/songoda/skyblock/island/IslandManager.java b/src/main/java/com/songoda/skyblock/island/IslandManager.java index 0ac45ca5..c655fe2f 100644 --- a/src/main/java/com/songoda/skyblock/island/IslandManager.java +++ b/src/main/java/com/songoda/skyblock/island/IslandManager.java @@ -55,11 +55,15 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.io.File; import java.io.IOException; import java.util.*; +import java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Consumer; import java.util.stream.Collectors; public class IslandManager { @@ -742,6 +746,88 @@ public class IslandManager { } } + public void adjustAllIslandsSize(@Nonnull int diff, @Nullable Runnable callback) { + FileManager fileManager = plugin.getFileManager(); + File islandConfigDir = new File(plugin.getDataFolder().toString() + "/island-data"); + + if (!islandConfigDir.exists()) return; + + File[] files = islandConfigDir.listFiles(); + if(files == null) return; + + for (File file : files) { + if (file != null && file.getName().contains(".yml") && file.getName().length() > 35) { + try { + Config config = new FileManager.Config(fileManager, file); + FileConfiguration islandConfigLoad = config.getFileConfiguration(); + + UUID islandOwnerUUID = FastUUID.parseUUID(islandConfigLoad.getString("Island.Owner", "")); + + Island island = getIslandByUUID(islandOwnerUUID); + + if(island != null) { + island.setSize(island.getSize() + diff); + island.save(); + } else { + loadIsland(file); + island = getIslandByUUID(islandOwnerUUID); + + island.setSize(island.getSize() + diff); + island.save(); + unloadIsland(island, null); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + if(callback != null) { + callback.run(); + } + } + + public void setAllIslandsSize(@Nonnull int size, @Nullable Runnable callback) { + FileManager fileManager = plugin.getFileManager(); + File islandConfigDir = new File(plugin.getDataFolder().toString() + "/island-data"); + + if (!islandConfigDir.exists()) return; + + File[] files = islandConfigDir.listFiles(); + if(files == null) return; + + for (File file : files) { + if (file != null && file.getName().contains(".yml") && file.getName().length() > 35) { + try { + Config config = new FileManager.Config(fileManager, file); + FileConfiguration islandConfigLoad = config.getFileConfiguration(); + + UUID islandOwnerUUID = FastUUID.parseUUID(islandConfigLoad.getString("Island.Owner", "")); + + Island island = getIslandByUUID(islandOwnerUUID); + + if(island != null) { + island.setSize(size); + island.save(); + } else { + loadIsland(file); + island = getIslandByUUID(islandOwnerUUID); + + island.setSize(size); + island.save(); + unloadIsland(island, null); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + if(callback != null) { + callback.run(); + } + } + public void loadIsland(File islandFile) { VisitManager visitManager = plugin.getVisitManager(); FileManager fileManager = plugin.getFileManager(); diff --git a/src/main/resources/language.yml b/src/main/resources/language.yml index eee4d2c3..67f62100 100644 --- a/src/main/resources/language.yml +++ b/src/main/resources/language.yml @@ -189,6 +189,19 @@ Command: Permission: Message: '&bSkyBlock &8| &cError&8: &eYou do not have the right to unban players from the Island.' Admin: + UpdateAllIslands: + Unexpected: + Message: '&bSkyBlock &8| &cError&8: &eUnexpected values' + NoArgs: + Message: '&bSkyBlock &8| &cError&8: &eInvalid: /island admin UpdateAllIslands ' + Start: + Message: '&bSkyBlock &8| &aInfo&8: &eIslands update started. It could take some time.' + Finished: + Message: '&bSkyBlock &8| &aInfo&8: &eIslands update finished.' + NotANumber: + Message: '&bSkyBlock &8| &cError&8: &eThe provided value is not valid.' + Info: + Message: '&f&oEdit all the islands in one time.' Bank: Short01: Message: '&bSkyBlock &8| &cError&8: &eInvalid: /island admin bank []' @@ -206,6 +219,8 @@ Command: Message: 'Succesfully removed %ammount% from %player%''s island' SuccesDeposit: Message: 'Succesfully added %ammount% to %player%''s island' + Unexpected: + Message: '&bSkyBlock &8| &cError&8: &eUnexpected values' ChatSpy: Info: Message: '&f&oToggle island chat spy.'