diff --git a/README.md b/README.md index e69de29..385c523 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,32 @@ +## World Creator +This Plugin allows you to manage worlds. + +Tested server version: Paper 1.20.1 + +### Commands +The Main-Command is ```/wc``` or ```/worldcreator``` + +- ```create ``` Create a new world, type can be ```DEFAULT```, ```FLAT```, ```LARGEBIOMES``` + or ```AMPLIFIED``` +- ```import ``` Import an existing world +- ```tp ``` Teleport to world spawn +- ```delete ``` Unload and delete an existing world and all its files +- ```load ``` Load an existing world +- ```unload ``` Unload an existing world +- ```rule ``` Change the gamerule for the given world +- ```list``` Shows a list of all available worlds +- ```help``` Shows a list of all available commands +- ```version``` Shows the plugin version + +### Permissions +Permissions for the commands: +- ```wc.create``` +- ```wc.import``` +- ```wc.delete``` +- ```wc.load``` +- ```wc.unload``` +- ```wc.list``` +- ```wc.help``` +- ```wc.version``` +- ```wc.teleport``` +- ```wc.admin``` Permission to use all commands \ No newline at end of file diff --git a/pom.xml b/pom.xml index df9b292..0a4826b 100644 --- a/pom.xml +++ b/pom.xml @@ -2,75 +2,75 @@ - 4.0.0 + 4.0.0 - de.uweb95 - WorldCreator - 1.0-SNAPSHOT - jar + de.uweb95 + WorldCreator + 1.0 + jar - WorldCreator + WorldCreator A simple plugin to generate and import worlds - 1.8 - UTF-8 - + 1.8 + UTF-8 + https://mc.lunar-software.eu - - - org.apache.maven.plugins - maven-compiler-plugin - 3.8.1 - - ${java.version} - ${java.version} - - - - org.apache.maven.plugins - maven-shade-plugin - 3.2.4 - - - package - - shade - - - false - - - - - - - - src/main/resources - true - - - + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 16 + 16 + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + + + + + + + + src/main/resources + true + + + - - - papermc-repo - https://repo.papermc.io/repository/maven-public/ - - - sonatype - https://oss.sonatype.org/content/groups/public/ - - + + + papermc-repo + https://repo.papermc.io/repository/maven-public/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + - - - io.papermc.paper - paper-api - 1.20-R0.1-SNAPSHOT - provided - - + + + io.papermc.paper + paper-api + 1.20-R0.1-SNAPSHOT + provided + + diff --git a/src/main/java/de/uweb95/worldcreator/WorldCreator.java b/src/main/java/de/uweb95/worldcreator/WorldCreator.java index c03d980..638e946 100644 --- a/src/main/java/de/uweb95/worldcreator/WorldCreator.java +++ b/src/main/java/de/uweb95/worldcreator/WorldCreator.java @@ -1,17 +1,79 @@ package de.uweb95.worldcreator; +import de.uweb95.worldcreator.commands.*; +import de.uweb95.worldcreator.util.Configuration; +import de.uweb95.worldcreator.util.UpdateChecker; +import de.uweb95.worldcreator.util.WorldConfiguration; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; -public final class WorldCreator extends JavaPlugin { +import java.util.List; +import java.util.logging.Logger; + +public final class WorldCreator extends JavaPlugin implements CommandExecutor, TabCompleter { + private static Logger logger; + private static WorldCreator instance; + CommandManager commandManager; @Override public void onEnable() { - // Plugin startup logic + instance = this; + logger = getLogger(); + UpdateChecker.CheckForUpdate(); + Configuration.loadConfig("worlds.yml"); + + commandManager = new CommandManager(); + registerCommands(); + + // Initialize configuration object and load all enabled worlds + WorldConfiguration.getInstance(); + } + private void registerCommands() { + commandManager.registerCommand("create", new CreateWorldCommand()); + commandManager.registerCommand("import", new ImportWorldCommand()); + commandManager.registerCommand("tp", new TeleportCommand()); + commandManager.registerCommand("delete", new DeleteWorldCommand()); + commandManager.registerCommand("load", new LoadWorldCommand()); + commandManager.registerCommand("unload", new UnloadWorldCommand()); + commandManager.registerCommand("rule", new RuleCommand()); + commandManager.registerCommand("list", new ListWorldsCommand()); + commandManager.registerCommand("help", new HelpCommand()); + commandManager.registerCommand("version", new VersionCommand()); } @Override - public void onDisable() { - // Plugin shutdown logic + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (command.getLabel().equalsIgnoreCase("wc") || command.getLabel().equalsIgnoreCase("worldcreator")) { + if (sender instanceof Player && !commandManager.checkPermissions((Player) sender)) { + return false; + } + + return commandManager.executeCommand(sender, command, label, args); + } + + return false; + } + + public List onTabComplete(@NotNull CommandSender sender, Command command, @NotNull String alias, String[] args) { + if (command.getLabel().equalsIgnoreCase("wc") || command.getLabel().equalsIgnoreCase("worldcreator")) { + if (sender instanceof Player && commandManager.checkPermissions((Player) sender)) + return commandManager.onTabComplete(sender, command, alias, args); + } + + return null; + } + + public static Logger logger() { + return logger; + } + + public static WorldCreator getInstance() { + return instance; } } diff --git a/src/main/java/de/uweb95/worldcreator/commands/CommandInterface.java b/src/main/java/de/uweb95/worldcreator/commands/CommandInterface.java index c843bed..1a44546 100644 --- a/src/main/java/de/uweb95/worldcreator/commands/CommandInterface.java +++ b/src/main/java/de/uweb95/worldcreator/commands/CommandInterface.java @@ -1,4 +1,4 @@ -package de.uweb95.minebay.commands; +package de.uweb95.worldcreator.commands; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; diff --git a/src/main/java/de/uweb95/worldcreator/commands/CommandManager.java b/src/main/java/de/uweb95/worldcreator/commands/CommandManager.java index 60c317b..2c4e30a 100644 --- a/src/main/java/de/uweb95/worldcreator/commands/CommandManager.java +++ b/src/main/java/de/uweb95/worldcreator/commands/CommandManager.java @@ -1,27 +1,40 @@ -package de.uweb95.minebay.commands; +package de.uweb95.worldcreator.commands; -import de.uweb95.minebay.util.Message; -import de.uweb95.minebay.util.Translation; +import de.uweb95.worldcreator.util.Message; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class CommandManager implements CommandInterface { private final Map commands = new HashMap<>(); + /** + * Register a command + * + * @param command command name + * @param commandClass command class instance + */ public void registerCommand(String command, CommandInterface commandClass) { commands.put(command, commandClass); } + /** + * Remove a registered command + * + * @param command command name + */ public void unregisterCommand(String command) { commands.remove(command); } @Override public boolean checkPermissions(Player player) { - return player.hasPermission("minebay"); + return true; } @Override @@ -33,7 +46,8 @@ public boolean executeCommand(CommandSender sender, Command command, String comm } if (selectedCommand != null) { - if (sender instanceof Player && !selectedCommand.checkPermissions((Player) sender)) { + if (sender instanceof Player && (!sender.isOp() && !sender.hasPermission("wc.admin") && !selectedCommand.checkPermissions((Player) sender))) { + sender.sendMessage(Message.pluginMessage("You don't have the permissions to use this command.")); return false; } @@ -66,10 +80,9 @@ public List onTabComplete(CommandSender sender, Command command, String } private void sendHelpMessage(CommandSender sender) { - StringBuilder builder = new StringBuilder(Translation.getTranslation("help")).append("\n"); - + StringBuilder builder = new StringBuilder("World creator options:\n"); commands.forEach((key, value) -> builder.append(key).append("\n")); - + builder.append("Use the 'help' command to see further information for each command."); sender.sendMessage(Message.pluginMessage(builder.toString())); } } diff --git a/src/main/java/de/uweb95/worldcreator/commands/CreateWorldCommand.java b/src/main/java/de/uweb95/worldcreator/commands/CreateWorldCommand.java index 0f7778e..4edd16f 100644 --- a/src/main/java/de/uweb95/worldcreator/commands/CreateWorldCommand.java +++ b/src/main/java/de/uweb95/worldcreator/commands/CreateWorldCommand.java @@ -1,34 +1,61 @@ -package de.uweb95.minebay.commands; +package de.uweb95.worldcreator.commands; -import de.uweb95.minebay.util.Message; -import org.bukkit.Bukkit; -import org.bukkit.World; +import de.uweb95.worldcreator.util.Message; +import de.uweb95.worldcreator.util.WorldConfiguration; +import de.uweb95.worldcreator.util.WorldHelper; import org.bukkit.WorldCreator; import org.bukkit.WorldType; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.util.ArrayList; import java.util.List; public class CreateWorldCommand implements CommandInterface { @Override public boolean checkPermissions(Player player) { - return player.hasPermission("minebay.user") || player.hasPermission("minebay.admin"); + return player.hasPermission("wc.create"); } @Override public boolean executeCommand(CommandSender sender, Command command, String commandLabel, String[] args) { + if (args.length < 2) { + sender.sendMessage(Message.pluginMessage("Not enough options, see /wc help for usage.")); + return true; + } - WorldCreator worldCreator = new WorldCreator("world_test"); - worldCreator.type(WorldType.NORMAL); + String worldName = WorldHelper.getWorldFromArgs(args); + WorldType type = WorldType.getByName(args[2]); + + if (type == null) { + sender.sendMessage(Message.pluginMessage("World type must be DEFAULT, FLAT, LARGEBIOMES or AMPLIFIED!")); + return true; + } + + WorldCreator worldCreator = new WorldCreator(worldName); + worldCreator.type(type); worldCreator.createWorld(); + WorldConfiguration.getInstance().setWorldLoad(worldName, true); + + sender.sendMessage(Message.pluginMessage("World created successfully!")); return true; } @Override public List onTabComplete(CommandSender sender, Command command, String commandLabel, String[] args) { - return null; + List options = new ArrayList<>(); + + if (args.length == 2) { + options.add(""); + } else if (args.length == 3) { + options.add("DEFAULT"); + options.add("FLAT"); + options.add("LARGEBIOMES"); + options.add("AMPLIFIED"); + } + + return options; } } diff --git a/src/main/java/de/uweb95/worldcreator/commands/DeleteWorldCommand.java b/src/main/java/de/uweb95/worldcreator/commands/DeleteWorldCommand.java index f141312..c60a17d 100644 --- a/src/main/java/de/uweb95/worldcreator/commands/DeleteWorldCommand.java +++ b/src/main/java/de/uweb95/worldcreator/commands/DeleteWorldCommand.java @@ -1,33 +1,72 @@ package de.uweb95.worldcreator.commands; +import de.uweb95.worldcreator.WorldCreator; +import de.uweb95.worldcreator.util.Message; +import de.uweb95.worldcreator.util.WorldConfiguration; +import de.uweb95.worldcreator.util.WorldHelper; +import org.apache.commons.lang3.SystemUtils; import org.bukkit.Bukkit; +import org.bukkit.GameRule; import org.bukkit.World; -import org.bukkit.WorldCreator; -import org.bukkit.WorldType; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.io.File; +import java.util.ArrayList; import java.util.List; -public class ImportWorldCommand implements CommandInterface { +public class DeleteWorldCommand implements CommandInterface { @Override public boolean checkPermissions(Player player) { - return player.hasPermission("minebay.user") || player.hasPermission("minebay.admin"); + return player.hasPermission("wc.delete"); } @Override public boolean executeCommand(CommandSender sender, Command command, String commandLabel, String[] args) { - Bukkit.createWorld(WorldCreator.name("world_test")); + String worldName = WorldHelper.getWorldFromArgs(args); + + if (worldName == null) { + sender.sendMessage(Message.pluginMessage("/wc help")); + return true; + } + + World world = Bukkit.getWorld(worldName); + + if (world == null) { + sender.sendMessage(Message.pluginMessage(String.format("The world '%s' does not exist", worldName))); + return true; + } + + File worldFolder = world.getWorldFolder(); + Bukkit.unloadWorld(world, false); + WorldConfiguration.getInstance().deleteWorld(worldName); + + if (worldFolder.delete()) { + sender.sendMessage(Message.pluginMessage(String.format("The folder for world '%s' does not exist. It might not be deleted.", worldName))); + return true; + } + + sender.sendMessage(Message.pluginMessage("World deleted successfully!")); + + if (SystemUtils.OS_NAME.contains("Windows") && worldFolder.exists()) { + worldFolder.deleteOnExit(); + sender.sendMessage(Message.pluginMessage("Deleting the world folder does not work correctly on Windows. We'll try to delete it when the server stops.")); + } - Bukkit.unloadWorld("world_test", false); - Bukkit.getWorldContainer(); - Bukkit.getWorld("world name").getWorldFolder().delete(); return true; } @Override public List onTabComplete(CommandSender sender, Command command, String commandLabel, String[] args) { - return null; + List options = new ArrayList<>(); + + if (args.length == 2) { + for (World world : Bukkit.getWorlds()) { + options.add(world.getName()); + } + } + + return options; } } diff --git a/src/main/java/de/uweb95/worldcreator/commands/HelpCommand.java b/src/main/java/de/uweb95/worldcreator/commands/HelpCommand.java index 3054696..fefe09e 100644 --- a/src/main/java/de/uweb95/worldcreator/commands/HelpCommand.java +++ b/src/main/java/de/uweb95/worldcreator/commands/HelpCommand.java @@ -1,24 +1,76 @@ package de.uweb95.worldcreator.commands; import de.uweb95.worldcreator.util.Message; -import org.bukkit.Bukkit; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.Style; +import net.kyori.adventure.text.format.TextColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.util.List; -public class ListWorldsCommand implements CommandInterface { +public class HelpCommand implements CommandInterface { @Override public boolean checkPermissions(Player player) { - return player.hasPermission("wc.unload") || player.hasPermission("wc.admin"); + return player.hasPermission("wc.help"); } @Override public boolean executeCommand(CommandSender sender, Command command, String commandLabel, String[] args) { - StringBuilder output = new StringBuilder(""); + // I hate all of this... + Component output = Component.text("[WorldCreator] ", Style.style(TextColor.fromCSSHexString("#00ffbf"))); + output = output.append(Component.text("Available options for /wc:\n", Style.style(TextColor.fromCSSHexString("#ffffff")))); - sender.sendMessage(Message.pluginMessage(output.toString())); + Style commandStyle = Style.style(TextColor.fromCSSHexString("#fdffff")); + Style secondParameterStyle = Style.style(TextColor.fromCSSHexString("#00ffbf")); + Style thirdParameterStyle = Style.style(TextColor.fromCSSHexString("#656bff")); + Style fourthParameterStyle = Style.style(TextColor.fromCSSHexString("#ffa918")); + Style descriptionStyle = Style.style(TextColor.fromCSSHexString("#c8c8c8")); + + Component worldName = Component.text(" ", secondParameterStyle); + + output = output.append(Component.text("create ", commandStyle)); + output = output.append(worldName); + output = output.append(Component.text("\n", thirdParameterStyle)); + output = output.append(Component.text("Create a new world, type can be DEFAULT, FLAT, LARGEBIOMES or AMPLIFIED.\n", descriptionStyle)); + + output = output.append(Component.text("import ", commandStyle)); + output = output.append(worldName); + output = output.append(Component.text("\nImport an existing world.\n", descriptionStyle)); + + output = output.append(Component.text("tp ", commandStyle)); + output = output.append(worldName); + output = output.append(Component.text("\nTeleport to world spawn.\n", descriptionStyle)); + + output = output.append(Component.text("delete ", commandStyle)); + output = output.append(worldName); + output = output.append(Component.text("\nUnload and delete an existing world and all its files.\n", descriptionStyle)); + + output = output.append(Component.text("load ", commandStyle)); + output = output.append(worldName); + output = output.append(Component.text("\nLoad an existing world.\n", descriptionStyle)); + + output = output.append(Component.text("unload ", commandStyle)); + output = output.append(worldName); + output = output.append(Component.text("\nUnload an existing world.\n", descriptionStyle)); + + output = output.append(Component.text("rule ", commandStyle)); + output = output.append(worldName); + output = output.append(Component.text(" ", thirdParameterStyle)); + output = output.append(Component.text("\n", fourthParameterStyle)); + output = output.append(Component.text("Change the gamerule for the given world.\n", descriptionStyle)); + + output = output.append(Component.text("list\n", commandStyle)); + output = output.append(Component.text("Shows a list of all available worlds.\n", descriptionStyle)); + + output = output.append(Component.text("help\n", commandStyle)); + output = output.append(Component.text("Shows this list.\n", descriptionStyle)); + + output = output.append(Component.text("version\n", commandStyle)); + output = output.append(Component.text("Shows the plugins version.", descriptionStyle)); + + sender.sendMessage(output); return true; } diff --git a/src/main/java/de/uweb95/worldcreator/commands/ImportWorldCommand.java b/src/main/java/de/uweb95/worldcreator/commands/ImportWorldCommand.java index 5f1dcde..75ffcfa 100644 --- a/src/main/java/de/uweb95/worldcreator/commands/ImportWorldCommand.java +++ b/src/main/java/de/uweb95/worldcreator/commands/ImportWorldCommand.java @@ -1,30 +1,55 @@ package de.uweb95.worldcreator.commands; +import de.uweb95.worldcreator.util.Message; +import de.uweb95.worldcreator.util.WorldConfiguration; +import de.uweb95.worldcreator.util.WorldHelper; +import org.bukkit.Bukkit; +import org.bukkit.World; import org.bukkit.WorldCreator; import org.bukkit.WorldType; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.io.File; +import java.util.ArrayList; import java.util.List; -public class CreateWorldCommand implements CommandInterface { +public class ImportWorldCommand implements CommandInterface { @Override public boolean checkPermissions(Player player) { - return player.hasPermission("minebay.user") || player.hasPermission("minebay.admin"); + return player.hasPermission("wc.import"); } @Override public boolean executeCommand(CommandSender sender, Command command, String commandLabel, String[] args) { - WorldCreator worldCreator = new WorldCreator("world_test"); - worldCreator.type(WorldType.NORMAL); - worldCreator.createWorld(); + String worldName = WorldHelper.getWorldFromArgs(args); + if (worldName == null) { + sender.sendMessage(Message.pluginMessage("/wc help")); + return true; + } + + if (!WorldHelper.checkIfWorldFolderExists(worldName)) { + sender.sendMessage(Message.pluginMessage(String.format("The folder for world '%s' does not exist.", worldName))); + return true; + } + + WorldCreator.name(worldName).createWorld(); + WorldConfiguration.getInstance().setWorldLoad(worldName, true); + + sender.sendMessage(Message.pluginMessage("World imported successfully!")); return true; } @Override public List onTabComplete(CommandSender sender, Command command, String commandLabel, String[] args) { - return null; + List options = new ArrayList<>(); + + if (args.length == 2) { + options.add(""); + } + + return options; } } diff --git a/src/main/java/de/uweb95/worldcreator/commands/ListWorldsCommand.java b/src/main/java/de/uweb95/worldcreator/commands/ListWorldsCommand.java index d69d703..e39eed0 100644 --- a/src/main/java/de/uweb95/worldcreator/commands/ListWorldsCommand.java +++ b/src/main/java/de/uweb95/worldcreator/commands/ListWorldsCommand.java @@ -7,19 +7,25 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import java.io.File; import java.util.List; -public class UnloadWorldCommand implements CommandInterface { +public class ListWorldsCommand implements CommandInterface { @Override public boolean checkPermissions(Player player) { - return player.hasPermission("wc.unload") || player.hasPermission("wc.admin"); + return player.hasPermission("wc.list"); } @Override public boolean executeCommand(CommandSender sender, Command command, String commandLabel, String[] args) { - Bukkit.unloadWorld("world_test", true); + StringBuilder output = new StringBuilder("Loaded worlds:\n"); + for (World world : Bukkit.getWorlds()) { + output.append(world.getName()).append("\n"); + } + + output.deleteCharAt(output.length() - 1); + + sender.sendMessage(Message.pluginMessage(output.toString())); return true; } diff --git a/src/main/java/de/uweb95/worldcreator/commands/LoadWorldCommand.java b/src/main/java/de/uweb95/worldcreator/commands/LoadWorldCommand.java index fd7aee5..5927a43 100644 --- a/src/main/java/de/uweb95/worldcreator/commands/LoadWorldCommand.java +++ b/src/main/java/de/uweb95/worldcreator/commands/LoadWorldCommand.java @@ -1,30 +1,48 @@ package de.uweb95.worldcreator.commands; import de.uweb95.worldcreator.util.Message; +import de.uweb95.worldcreator.util.WorldConfiguration; +import de.uweb95.worldcreator.util.WorldHelper; import org.bukkit.Bukkit; -import org.bukkit.World; +import org.bukkit.WorldCreator; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import java.io.File; +import java.util.ArrayList; import java.util.List; -public class UnloadWorldCommand implements CommandInterface { +public class LoadWorldCommand implements CommandInterface { @Override public boolean checkPermissions(Player player) { - return player.hasPermission("wc.unload") || player.hasPermission("wc.admin"); + return player.hasPermission("wc.load"); } @Override public boolean executeCommand(CommandSender sender, Command command, String commandLabel, String[] args) { - Bukkit.unloadWorld("world_test", false); + String worldName = WorldHelper.getWorldFromArgs(args); + + if (worldName == null) { + sender.sendMessage(Message.pluginMessage("/wc help")); + return true; + } + + new WorldCreator(worldName).createWorld(); + WorldConfiguration.getInstance().setWorldLoad(worldName, true); + + sender.sendMessage(Message.pluginMessage("World loaded successfully!")); return true; } @Override public List onTabComplete(CommandSender sender, Command command, String commandLabel, String[] args) { - return null; + List options = new ArrayList<>(); + + if (args.length == 2) { + options.add(""); + } + + return options; } } diff --git a/src/main/java/de/uweb95/worldcreator/commands/RuleCommand.java b/src/main/java/de/uweb95/worldcreator/commands/RuleCommand.java index f507b79..c74f39f 100644 --- a/src/main/java/de/uweb95/worldcreator/commands/RuleCommand.java +++ b/src/main/java/de/uweb95/worldcreator/commands/RuleCommand.java @@ -4,6 +4,7 @@ import de.uweb95.worldcreator.util.WorldHelper; import org.apache.commons.lang3.SystemUtils; import org.bukkit.Bukkit; +import org.bukkit.GameRule; import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -12,21 +13,23 @@ import java.io.File; import java.util.ArrayList; import java.util.List; +import java.util.Objects; -public class GameRuleCommand implements CommandInterface { +public class RuleCommand implements CommandInterface { @Override public boolean checkPermissions(Player player) { return player.hasPermission("wc.delete"); } @Override + @SuppressWarnings("unchecked") public boolean executeCommand(CommandSender sender, Command command, String commandLabel, String[] args) { - String worldName = WorldHelper.getWorldFromArgs(args); - - if (worldName == null) { + if (args.length < 3) { + sender.sendMessage(Message.pluginMessage("Not enough options, see /wc help for usage.")); return true; } + String worldName = WorldHelper.getWorldFromArgs(args); World world = Bukkit.getWorld(worldName); if (world == null) { @@ -34,21 +37,28 @@ public boolean executeCommand(CommandSender sender, Command command, String comm return true; } - File worldFolder = world.getWorldFolder(); - Bukkit.unloadWorld(world, false); + GameRule selectedRule = GameRule.getByName(args[2]); - if (worldFolder.delete()) { - sender.sendMessage(Message.pluginMessage(String.format("The folder for world '%s' does not exist. It might not be deleted.", worldName))); + if (selectedRule == null) { + sender.sendMessage(Message.pluginMessage(String.format("The game rule '%s' does not exist", args[2]))); return true; } - sender.sendMessage(Message.pluginMessage("World deleted successfully!")); + if (args.length == 4) { + String newValue = args[3]; + + if (selectedRule.getType() == Boolean.class) { + world.setGameRule((GameRule) selectedRule, Objects.equals(newValue, "true")); + } else if (selectedRule.getType() == Integer.class) { + world.setGameRule((GameRule) selectedRule, Integer.getInteger(newValue)); + } - if (SystemUtils.OS_NAME.contains("Windows") && worldFolder.exists()) { - worldFolder.deleteOnExit(); - sender.sendMessage(Message.pluginMessage("Deleting the world folder does not work correctly on Windows. We'll try to delete it when the server stops.")); + sender.sendMessage(Message.pluginMessage(selectedRule.getName() + " is now " + world.getGameRuleValue(selectedRule))); + return true; } + sender.sendMessage(Message.pluginMessage(selectedRule.getName() + ": " + world.getGameRuleValue(selectedRule))); + return true; } @@ -60,6 +70,25 @@ public List onTabComplete(CommandSender sender, Command command, String for (World world : Bukkit.getWorlds()) { options.add(world.getName()); } + } else if (args.length == 3) { + for (GameRule rule : GameRule.values()) { + if(rule.getName().contains(args[2])){ + options.add(rule.getName()); + } + } + } else if (args.length == 4) { + GameRule selectedRule = GameRule.getByName(args[2]); + + if (selectedRule != null) { + if (selectedRule.getType() == Boolean.class) { + options.add("true"); + options.add("false"); + } else { + options.add(""); + } + } else { + options.add("INVALID GAME RULE!"); + } } return options; diff --git a/src/main/java/de/uweb95/worldcreator/commands/TeleportCommand.java b/src/main/java/de/uweb95/worldcreator/commands/TeleportCommand.java index 5927a43..767abe2 100644 --- a/src/main/java/de/uweb95/worldcreator/commands/TeleportCommand.java +++ b/src/main/java/de/uweb95/worldcreator/commands/TeleportCommand.java @@ -4,6 +4,8 @@ import de.uweb95.worldcreator.util.WorldConfiguration; import de.uweb95.worldcreator.util.WorldHelper; import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.WorldCreator; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -12,7 +14,7 @@ import java.util.ArrayList; import java.util.List; -public class LoadWorldCommand implements CommandInterface { +public class TeleportCommand implements CommandInterface { @Override public boolean checkPermissions(Player player) { return player.hasPermission("wc.load"); @@ -27,10 +29,20 @@ public boolean executeCommand(CommandSender sender, Command command, String comm return true; } - new WorldCreator(worldName).createWorld(); - WorldConfiguration.getInstance().setWorldLoad(worldName, true); + if (!(sender instanceof Player)) { + sender.sendMessage(Message.pluginMessage("This command only works ingame.")); + return true; + } + + Player player = (Player) sender; + World targetWorld = Bukkit.getWorld(worldName); + + if (targetWorld == null) { + sender.sendMessage(Message.pluginMessage("This world does not exist.")); + return true; + } - sender.sendMessage(Message.pluginMessage("World loaded successfully!")); + player.teleport(targetWorld.getSpawnLocation()); return true; } @@ -40,7 +52,9 @@ public List onTabComplete(CommandSender sender, Command command, String List options = new ArrayList<>(); if (args.length == 2) { - options.add(""); + for (World world : Bukkit.getWorlds()) { + options.add(world.getName()); + } } return options; diff --git a/src/main/java/de/uweb95/worldcreator/commands/UnloadWorldCommand.java b/src/main/java/de/uweb95/worldcreator/commands/UnloadWorldCommand.java index 3524701..a09582b 100644 --- a/src/main/java/de/uweb95/worldcreator/commands/UnloadWorldCommand.java +++ b/src/main/java/de/uweb95/worldcreator/commands/UnloadWorldCommand.java @@ -1,7 +1,8 @@ package de.uweb95.worldcreator.commands; -import de.uweb95.worldcreator.WorldCreator; import de.uweb95.worldcreator.util.Message; +import de.uweb95.worldcreator.util.WorldConfiguration; +import de.uweb95.worldcreator.util.WorldHelper; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.command.Command; @@ -9,35 +10,42 @@ import org.bukkit.entity.Player; import java.io.File; +import java.util.ArrayList; import java.util.List; -public class DeleteWorldCommand implements CommandInterface { +public class UnloadWorldCommand implements CommandInterface { @Override public boolean checkPermissions(Player player) { - return player.hasPermission("minebay.user") || player.hasPermission("minebay.admin"); + return player.hasPermission("wc.unload"); } @Override public boolean executeCommand(CommandSender sender, Command command, String commandLabel, String[] args) { - World world = Bukkit.getWorld("world_test"); + String worldName = WorldHelper.getWorldFromArgs(args); - if (world == null) { - sender.sendMessage(Message.pluginMessage(String.format("The world '%s' does not exist", "world_test"))); - return false; + if (worldName == null) { + sender.sendMessage(Message.pluginMessage("/wc help")); + return true; } - Bukkit.unloadWorld(world, false); - File worldFolder = world.getWorldFolder(); + Bukkit.unloadWorld(worldName, true); + WorldConfiguration.getInstance().setWorldLoad(worldName, false); - if (worldFolder.delete()) { - sender.sendMessage(Message.pluginMessage(String.format("The folder for world '%s' does not exist. It might not be deleted.", "world_test"))); - } + sender.sendMessage(Message.pluginMessage("World unloaded successfully!")); return true; } @Override public List onTabComplete(CommandSender sender, Command command, String commandLabel, String[] args) { - return null; + List options = new ArrayList<>(); + + if (args.length == 2) { + for (World world : Bukkit.getWorlds()) { + options.add(world.getName()); + } + } + + return options; } } diff --git a/src/main/java/de/uweb95/worldcreator/commands/VersionCommand.java b/src/main/java/de/uweb95/worldcreator/commands/VersionCommand.java index 05d58ad..0ad8e20 100644 --- a/src/main/java/de/uweb95/worldcreator/commands/VersionCommand.java +++ b/src/main/java/de/uweb95/worldcreator/commands/VersionCommand.java @@ -1,6 +1,6 @@ -package de.uweb95.minebay.commands; +package de.uweb95.worldcreator.commands; -import de.uweb95.minebay.util.Message; +import de.uweb95.worldcreator.util.Message; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -10,7 +10,7 @@ public class VersionCommand implements CommandInterface { @Override public boolean checkPermissions(Player player) { - return player.hasPermission("minebay.user") || player.hasPermission("minebay.admin"); + return player.hasPermission("wc.version"); } @Override diff --git a/src/main/java/de/uweb95/worldcreator/util/Configuration.java b/src/main/java/de/uweb95/worldcreator/util/Configuration.java index 8cb8d28..abdb2ba 100644 --- a/src/main/java/de/uweb95/worldcreator/util/Configuration.java +++ b/src/main/java/de/uweb95/worldcreator/util/Configuration.java @@ -1,6 +1,6 @@ -package de.uweb95.minebay.util; +package de.uweb95.worldcreator.util; -import de.uweb95.minebay.Minebay; +import de.uweb95.worldcreator.WorldCreator; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -11,8 +11,11 @@ public class Configuration { private static Map configFiles = new HashMap<>(); + /** + * Load default plugin config file + */ public static void loadPluginConfig() { - Minebay plugin = Minebay.getInstance(); + WorldCreator plugin = WorldCreator.getInstance(); File dataFolder = plugin.getDataFolder(); if (!dataFolder.exists()) dataFolder.mkdir(); @@ -26,21 +29,28 @@ public static void loadPluginConfig() { } } + /** + * Load a yaml configuration file + */ public static void loadConfig(String filename) { if (configFiles.containsKey(filename)) { return; } - File configFile = new File(Minebay.getInstance().getDataFolder(), filename); + File configFile = getFileHandle(filename); if (!configFile.exists()) { configFile.getParentFile().mkdirs(); - Minebay.getInstance().saveResource(filename, false); + WorldCreator.getInstance().saveResource(filename, false); } configFiles.put(filename, YamlConfiguration.loadConfiguration(configFile)); } + public static File getFileHandle(String filename) { + return new File(WorldCreator.getInstance().getDataFolder(), filename); + } + public static FileConfiguration getConfig(String filename) { return configFiles.get(filename); } diff --git a/src/main/java/de/uweb95/worldcreator/util/Message.java b/src/main/java/de/uweb95/worldcreator/util/Message.java index c1b3d8a..3372fc9 100644 --- a/src/main/java/de/uweb95/worldcreator/util/Message.java +++ b/src/main/java/de/uweb95/worldcreator/util/Message.java @@ -1,4 +1,4 @@ -package de.uweb95.minebay.util; +package de.uweb95.worldcreator.util; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.Style; @@ -6,7 +6,7 @@ public class Message { public static Component pluginMessage(String text, String color) { - Component prefix = Component.text("[Minebay] ", Style.style(TextColor.fromCSSHexString("#00bfff"))); + Component prefix = Component.text("[WorldCreator] ", Style.style(TextColor.fromCSSHexString("#00ffbf"))); Component message = Component.text(text, Style.style(TextColor.fromCSSHexString(color))); return prefix.append(message); } diff --git a/src/main/java/de/uweb95/worldcreator/util/UpdateChecker.java b/src/main/java/de/uweb95/worldcreator/util/UpdateChecker.java index 2d730ec..46d6c22 100644 --- a/src/main/java/de/uweb95/worldcreator/util/UpdateChecker.java +++ b/src/main/java/de/uweb95/worldcreator/util/UpdateChecker.java @@ -1,2 +1,46 @@ -package de.uweb95.worldcreator.util;public class UpdateChecker { +package de.uweb95.worldcreator.util; + +import de.uweb95.worldcreator.WorldCreator; + +import java.net.URL; +import java.util.Scanner; + +public class UpdateChecker { + private static final String currentVersionUrl = "https://mc.lunar-software.eu/worldcreator/currentVersion"; + private static final String installedVersion = "1.0"; + + public static void CheckForUpdate() { + URL currentVersion; + String outputMessage; + + try { + currentVersion = new URL(currentVersionUrl); + Scanner scanner = new Scanner(currentVersion.openStream()); + + StringBuffer buffer = new StringBuffer(); + + while (scanner.hasNext()) { + buffer.append(scanner.next()); + } + + String currentAvailableVersion = buffer.toString(); + + if (currentAvailableVersion.equals("")) { + outputMessage = "Can't check for new version!"; + } else { + boolean upToDate = installedVersion.equalsIgnoreCase(currentAvailableVersion); + + if (upToDate) { + outputMessage = "Installed version is up to date."; + } else { + outputMessage = "New Version available! Installed Version: " + installedVersion + ", New Version: " + currentAvailableVersion; + } + } + + } catch (Exception e) { + outputMessage = "Can't check for new version!"; + } + + WorldCreator.logger().warning(outputMessage); + } } diff --git a/src/main/java/de/uweb95/worldcreator/util/WorldConfiguration.java b/src/main/java/de/uweb95/worldcreator/util/WorldConfiguration.java index af7906c..3e9b6ad 100644 --- a/src/main/java/de/uweb95/worldcreator/util/WorldConfiguration.java +++ b/src/main/java/de/uweb95/worldcreator/util/WorldConfiguration.java @@ -1,2 +1,103 @@ -package de.uweb95.worldcreator.util;public class WorldConfiguration { +package de.uweb95.worldcreator.util; + +import de.uweb95.worldcreator.WorldCreator; +import org.bukkit.configuration.file.FileConfiguration; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class WorldConfiguration { + private static WorldConfiguration instance; + private final String configFileName = "worlds.yml"; + private FileConfiguration configFile; + + private List worldConfigurationEntries = new ArrayList<>(); + + private WorldConfiguration() { + configFile = Configuration.getConfig(configFileName); + loadConfigEntries(); + } + + public static WorldConfiguration getInstance() { + if (instance == null) { + instance = new WorldConfiguration(); + } + + return instance; + } + + /** + * Get the configuration entry by world name + */ + public WorldConfigurationEntry getWorldConfigByName(String name) { + for (WorldConfigurationEntry entry : worldConfigurationEntries) { + if (entry.worldName.equals(name)) return entry; + } + + WorldConfigurationEntry entry = new WorldConfigurationEntry(); + entry.worldName = name; + worldConfigurationEntries.add(entry); + + return entry; + } + + /** + * Set a world loaded + */ + public void setWorldLoad(String world, boolean load) { + WorldConfigurationEntry entry = getWorldConfigByName(world); + entry.loaded = load; + saveConfigEntries(); + } + + /** + * Delete a world from the configuration + */ + public void deleteWorld(String world) { + WorldConfigurationEntry entry = getWorldConfigByName(world); + + if (entry == null) { + return; + } + + worldConfigurationEntries.remove(entry); + saveConfigEntries(); + } + + /** + * Load all worlds from the config and load them, when enabled + */ + private void loadConfigEntries() { + List entries = configFile.getStringList("worlds"); + + for (String entry : entries) { + WorldConfigurationEntry worldConfigurationEntry = WorldConfigurationEntry.deserialize(entry); + + if (worldConfigurationEntry.loaded) { + new org.bukkit.WorldCreator(worldConfigurationEntry.worldName).createWorld(); + } + + worldConfigurationEntries.add(worldConfigurationEntry); + } + } + + /** + * Serialize and save the configuration + */ + private void saveConfigEntries() { + List serializedEntries = new ArrayList<>(); + + for (WorldConfigurationEntry entry : worldConfigurationEntries) { + serializedEntries.add(entry.serialize()); + } + + configFile.set("worlds", serializedEntries); + + try { + configFile.save(Configuration.getFileHandle(configFileName)); + } catch (IOException e) { + WorldCreator.logger().severe(String.format("Can't save config file '%s': %s", configFileName, e.getMessage())); + } + } } diff --git a/src/main/java/de/uweb95/worldcreator/util/WorldConfigurationEntry.java b/src/main/java/de/uweb95/worldcreator/util/WorldConfigurationEntry.java index a28d669..69e819e 100644 --- a/src/main/java/de/uweb95/worldcreator/util/WorldConfigurationEntry.java +++ b/src/main/java/de/uweb95/worldcreator/util/WorldConfigurationEntry.java @@ -1,2 +1,35 @@ -package de.uweb95.worldcreator.util;public class WorldConfigurationEntry { +package de.uweb95.worldcreator.util; + +import java.util.Base64; + +public class WorldConfigurationEntry { + public String worldName; + public boolean loaded; + + public String serialize() { + StringBuilder builder = new StringBuilder(); + builder.append("worldName:").append(worldName).append("|"); + builder.append("loaded:").append(loaded); + + return Base64.getEncoder().encodeToString(builder.toString().getBytes()); + } + + public static WorldConfigurationEntry deserialize(String serialized) { + String rawData = new String(Base64.getDecoder().decode(serialized)); + String[] data = rawData.split("\\|"); + WorldConfigurationEntry entry = new WorldConfigurationEntry(); + + for (String row : data) { + String[] rowData = row.split(":"); + + switch (rowData[0]) { + case "worldName": + entry.worldName = rowData[1]; + case "loaded": + entry.loaded = Boolean.parseBoolean(rowData[1]); + } + } + + return entry; + } } diff --git a/src/main/java/de/uweb95/worldcreator/util/WorldHelper.java b/src/main/java/de/uweb95/worldcreator/util/WorldHelper.java index 7eb1648..562316e 100644 --- a/src/main/java/de/uweb95/worldcreator/util/WorldHelper.java +++ b/src/main/java/de/uweb95/worldcreator/util/WorldHelper.java @@ -4,8 +4,7 @@ import java.io.File; -public class World { - +public class WorldHelper { public static String getWorldFromArgs(String[] args) { String world = null; @@ -17,7 +16,7 @@ public static String getWorldFromArgs(String[] args) { } public static boolean checkIfWorldFolderExists(String name) { - String path = Bukkit.getWorldContainer().getPath() + "/" + name; + String path = Bukkit.getWorldContainer().getPath() + File.separator + name; File folder = new File(path); return folder.exists() && folder.isDirectory(); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index b8271c6..dc8a1a1 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,8 +1,48 @@ name: WorldCreator version: '${project.version}' main: de.uweb95.worldcreator.WorldCreator -api-version: 1.20 +api-version: "1.19" prefix: WorldCreator -authors: [Uweb95] +authors: [ Uweb95 ] description: A simple plugin to generate and import worlds website: https://mc.lunar-software.eu +commands: + wc: + description: Plugin main command + usage: /wc + permission: wc + worldcreator: + description: Alternative plugin main command + usage: /worldcreator + permission: wc +permissions: + wc.create: + description: Permission to use the '/wc create' command + default: false + wc.import: + description: Permission to use the '/wc import' command + default: false + wc.delete: + description: Permission to use the '/wc delete' command + default: false + wc.load: + description: Permission to use the '/wc load' command + default: false + wc.unload: + description: Permission to use the '/wc unload' command + default: false + wc.list: + description: Permission to use the '/wc list' command + default: false + wc.help: + description: Permission to use the '/wc help' command + default: true + wc.version: + description: Permission to use the '/wc version' command + default: true + wc.teleport: + description: Permission to use the '/wc tp' command + default: false + wc.admin: + description: Permission to use all /wc commands + default: false \ No newline at end of file