From 147ffa87fccdfcd837c40b70eeb556b2281eef3b Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Sat, 3 Dec 2016 13:06:05 +0100 Subject: [PATCH 1/2] Added rename command --- .../dre2n/dungeonsxl/command/DCommands.java | 2 + .../dungeonsxl/command/RenameCommand.java | 92 +++++++++++++++++++ .../dre2n/dungeonsxl/config/DMessages.java | 2 + .../dre2n/dungeonsxl/player/DPermissions.java | 3 +- 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/io/github/dre2n/dungeonsxl/command/RenameCommand.java diff --git a/core/src/main/java/io/github/dre2n/dungeonsxl/command/DCommands.java b/core/src/main/java/io/github/dre2n/dungeonsxl/command/DCommands.java index 05e41a3a..687729f6 100644 --- a/core/src/main/java/io/github/dre2n/dungeonsxl/command/DCommands.java +++ b/core/src/main/java/io/github/dre2n/dungeonsxl/command/DCommands.java @@ -48,6 +48,7 @@ public class DCommands extends BRCommands { public static PlayCommand PLAY = new PlayCommand(); public static PortalCommand PORTAL = new PortalCommand(); public static ReloadCommand RELOAD = new ReloadCommand(); + public static RenameCommand RENAME = new RenameCommand(); public static ResourcePackCommand RESOURCE_PACK = new ResourcePackCommand(); public static RewardsCommand REWARDS = new RewardsCommand(); public static SaveCommand SAVE = new SaveCommand(); @@ -79,6 +80,7 @@ public class DCommands extends BRCommands { PLAY, PORTAL, RELOAD, + RENAME, RESOURCE_PACK, REWARDS, SAVE, diff --git a/core/src/main/java/io/github/dre2n/dungeonsxl/command/RenameCommand.java b/core/src/main/java/io/github/dre2n/dungeonsxl/command/RenameCommand.java new file mode 100644 index 00000000..1cbae5b6 --- /dev/null +++ b/core/src/main/java/io/github/dre2n/dungeonsxl/command/RenameCommand.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2012-2016 Frank Baumann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.github.dre2n.dungeonsxl.command; + +import io.github.dre2n.commons.command.BRCommand; +import io.github.dre2n.commons.util.messageutil.MessageUtil; +import io.github.dre2n.dungeonsxl.DungeonsXL; +import io.github.dre2n.dungeonsxl.config.DMessages; +import io.github.dre2n.dungeonsxl.config.DungeonConfig; +import io.github.dre2n.dungeonsxl.dungeon.Dungeon; +import io.github.dre2n.dungeonsxl.player.DPermissions; +import io.github.dre2n.dungeonsxl.world.DResourceWorld; +import java.io.File; +import java.io.IOException; +import java.util.List; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; + +/** + * @author Daniel Saukel + */ +public class RenameCommand extends BRCommand { + + DungeonsXL plugin = DungeonsXL.getInstance(); + + public RenameCommand() { + setCommand("rename"); + setMinArgs(2); + setMaxArgs(2); + setHelp(DMessages.HELP_CMD_RENAME.getMessage()); + setPermission(DPermissions.RENAME.getNode()); + setPlayerCommand(true); + setConsoleCommand(true); + } + + @Override + public void onExecute(String[] args, CommandSender sender) { + DResourceWorld resource = plugin.getDWorlds().getResourceByName(args[1]); + if (resource == null) { + MessageUtil.sendMessage(sender, DMessages.ERROR_NO_SUCH_MAP.getMessage(args[1])); + return; + } + + resource.setName(args[2]); + resource.getFolder().renameTo(new File(DungeonsXL.MAPS, args[2])); + + for (Dungeon dungeon : plugin.getDungeons().getDungeons()) { + DungeonConfig dConfig = dungeon.getConfig(); + FileConfiguration config = dConfig.getConfig(); + File file = dConfig.getFile(); + + if (dConfig.getStartFloor() == resource) { + config.set("startFloor", args[2]); + } + + if (dConfig.getEndFloor() == resource) { + config.set("endFloor", args[2]); + } + + List list = config.getStringList("floors"); + int i = 0; + for (DResourceWorld floor : dConfig.getFloors()) { + if (floor == resource) { + list.set(i, args[2]); + } + i++; + } + config.set("floors", list); + + try { + config.save(file); + } catch (IOException ex) { + } + } + MessageUtil.sendMessage(sender, DMessages.CMD_RENAME_SUCCESS.getMessage(args[1], args[2])); + } + +} diff --git a/core/src/main/java/io/github/dre2n/dungeonsxl/config/DMessages.java b/core/src/main/java/io/github/dre2n/dungeonsxl/config/DMessages.java index 7c8c08b2..6f308630 100644 --- a/core/src/main/java/io/github/dre2n/dungeonsxl/config/DMessages.java +++ b/core/src/main/java/io/github/dre2n/dungeonsxl/config/DMessages.java @@ -53,6 +53,7 @@ public enum DMessages implements Messages { CMD_MSG_ADDED("Cmd_Msg_Added", "&6New Messages (&4&v1&6) added!"), CMD_MSG_UPDATED("Cmd_Msg_Updated", "&6Messages (&4&v1&6) updated!"), CMD_RELOAD_DONE("Cmd_Reload_Done", "&7Successfully reloaded DungeonsXL."), + CMD_RENAME_SUCCESS("Cmd_Rename_Success", "&6Successfully renamed the map &4&v1&6 to &4&v2&6."), CMD_SAVE_SUCCESS("Cmd_Save_Success", "&6Map saved!"), CMD_UNINVITE_SUCCESS("Cmd_Uninvite_Success", "&4&v1&6's permission to edit the map &4&v2&6 has been removed successfully."), ERROR_BED("Error_Bed", "&4You cannot use a bed while in a dungeon!"), @@ -130,6 +131,7 @@ public enum DMessages implements Messages { HELP_CMD_PLAY("Help_Cmd_Play", "/dxl play ([dungeon|map]) [name] - Allows the player to play a dungeon without a portal"), HELP_CMD_PORTAL("Help_Cmd_Portal", "/dxl portal ([material=portal])- Creates a portal that leads into a dungeon"), HELP_CMD_RELOAD("Help_Cmd_Reload", "/dxl reload - Reloads the plugin"), + HELP_CMD_RENAME("Help_Cmd_Rename", "/dxl rename [old name] [new name] - Changes the name of a map to the new one. This command does NOT break dungeons that include this map."), HELP_CMD_REWARDS("Help_Cmd_Rewards", "/dxl rewards - Gives all left item rewards to the player"), HELP_CMD_RESOURCE_PACK("Help_Cmd_ResourcePack", "/dxl resourcepack [ID] - Downloads a resourcepack registered in the main configuration file; use 'reset' to reset"), HELP_CMD_SAVE("Help_Cmd_Save", "/dxl save - Saves the current dungeon"), diff --git a/core/src/main/java/io/github/dre2n/dungeonsxl/player/DPermissions.java b/core/src/main/java/io/github/dre2n/dungeonsxl/player/DPermissions.java index 3f345d4a..2c2cb933 100644 --- a/core/src/main/java/io/github/dre2n/dungeonsxl/player/DPermissions.java +++ b/core/src/main/java/io/github/dre2n/dungeonsxl/player/DPermissions.java @@ -59,6 +59,7 @@ public enum DPermissions { PLAY("play", OP), PORTAL("portal", OP), RELOAD("reload", OP), + RENAME("rename", OP), RESOURCE_PACK("resourcepack", OP), REWARDS("rewards", TRUE), SAVE("save", OP), @@ -85,7 +86,7 @@ public enum DPermissions { // Kits ADMINISTRATOR("*", OP), HALF_EDITOR("halfeditor", OP, ESCAPE, LIST, MESSAGE, SAVE), - FULL_EDITOR("fulleditor", OP, HALF_EDITOR, EDIT, PLAY, SIGN, TEST), + FULL_EDITOR("fulleditor", OP, HALF_EDITOR, EDIT, PLAY, RENAME, SIGN, TEST), HALF_PLAYER("halfplayer", TRUE, CHAT, ESCAPE, GAME, HELP, JOIN, LEAVE, LIVES, MAIN, SETTINGS, SETTINGS_PLAYER), FULL_PLAYER("fullplayer", OP, HALF_PLAYER, GROUP); From 58dca2850e866e1c14e9ff1a0d6cf382a3d52a25 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Sat, 3 Dec 2016 14:12:50 +0100 Subject: [PATCH 2/2] #41: Also apply renaming to data --- .../dungeonsxl/command/RenameCommand.java | 24 +++++++++++++++++++ .../dungeonsxl/world/DResourceWorld.java | 1 + 2 files changed, 25 insertions(+) diff --git a/core/src/main/java/io/github/dre2n/dungeonsxl/command/RenameCommand.java b/core/src/main/java/io/github/dre2n/dungeonsxl/command/RenameCommand.java index 1cbae5b6..4ee0d751 100644 --- a/core/src/main/java/io/github/dre2n/dungeonsxl/command/RenameCommand.java +++ b/core/src/main/java/io/github/dre2n/dungeonsxl/command/RenameCommand.java @@ -22,6 +22,9 @@ import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.DMessages; import io.github.dre2n.dungeonsxl.config.DungeonConfig; import io.github.dre2n.dungeonsxl.dungeon.Dungeon; +import io.github.dre2n.dungeonsxl.global.GameSign; +import io.github.dre2n.dungeonsxl.global.GlobalProtection; +import io.github.dre2n.dungeonsxl.global.GroupSign; import io.github.dre2n.dungeonsxl.player.DPermissions; import io.github.dre2n.dungeonsxl.world.DResourceWorld; import java.io.File; @@ -86,6 +89,27 @@ public class RenameCommand extends BRCommand { } catch (IOException ex) { } } + + boolean changed = false; + for (GlobalProtection protection : plugin.getGlobalProtections().getProtections()) { + if (protection instanceof GroupSign) { + if (((GroupSign) protection).getMapName().equals(args[1])) { + ((GroupSign) protection).setMapName(args[2]); + changed = true; + } + + } else if (protection instanceof GameSign) { + if (((GameSign) protection).getMapName().equals(args[1])) { + ((GameSign) protection).setMapName(args[2]); + changed = true; + } + } + } + + if (changed) { + plugin.getGlobalProtections().saveAll(); + } + MessageUtil.sendMessage(sender, DMessages.CMD_RENAME_SUCCESS.getMessage(args[1], args[2])); } diff --git a/core/src/main/java/io/github/dre2n/dungeonsxl/world/DResourceWorld.java b/core/src/main/java/io/github/dre2n/dungeonsxl/world/DResourceWorld.java index bbeffac2..9c60d925 100644 --- a/core/src/main/java/io/github/dre2n/dungeonsxl/world/DResourceWorld.java +++ b/core/src/main/java/io/github/dre2n/dungeonsxl/world/DResourceWorld.java @@ -98,6 +98,7 @@ public class DResourceWorld { */ public void setName(String name) { folder.renameTo(new File(folder.getParentFile(), name)); + folder = new File(folder.getParentFile(), name); } /**