DungeonsXL/core/src/main/java/de/erethon/dungeonsxl/command/DeleteCommand.java

114 lines
4.5 KiB
Java
Raw Normal View History

2016-12-03 19:54:40 +01:00
/*
2023-09-28 19:16:23 +02:00
* Copyright (C) 2012-2023 Frank Baumann
2016-12-03 19:54:40 +01:00
*
* 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 <http://www.gnu.org/licenses/>.
*/
2018-05-03 21:54:25 +02:00
package de.erethon.dungeonsxl.command;
2016-12-03 19:54:40 +01:00
2018-05-03 21:54:25 +02:00
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.dungeon.Dungeon;
import de.erethon.dungeonsxl.api.world.ResourceWorld;
2018-05-03 21:54:25 +02:00
import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.dungeon.DDungeon;
import de.erethon.dungeonsxl.dungeon.DungeonConfig;
2018-05-03 21:54:25 +02:00
import de.erethon.dungeonsxl.player.DPermission;
2022-04-03 18:09:12 +02:00
import de.erethon.bedrock.chat.MessageUtil;
import de.erethon.bedrock.misc.FileUtil;
2016-12-03 19:54:40 +01:00
import java.io.File;
import java.util.ArrayList;
import java.util.List;
2019-06-11 23:39:27 +02:00
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
2016-12-03 19:54:40 +01:00
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
/**
* @author Daniel Saukel
*/
2018-08-18 16:52:51 +02:00
public class DeleteCommand extends DCommand {
2016-12-03 19:54:40 +01:00
2018-08-18 16:52:51 +02:00
public DeleteCommand(DungeonsXL plugin) {
super(plugin);
2016-12-03 19:54:40 +01:00
setCommand("delete");
setMinArgs(1);
setMaxArgs(2);
setHelp(DMessage.CMD_DELETE_HELP.getMessage());
2017-11-16 18:25:10 +01:00
setPermission(DPermission.DELETE.getNode());
2016-12-03 19:54:40 +01:00
setPlayerCommand(true);
setConsoleCommand(true);
}
@Override
public void onExecute(String[] args, CommandSender sender) {
ResourceWorld resource = plugin.getMapRegistry().get(args[1]);
2016-12-03 19:54:40 +01:00
if (resource == null) {
2017-07-21 18:33:21 +02:00
MessageUtil.sendMessage(sender, DMessage.ERROR_NO_SUCH_MAP.getMessage(args[1]));
2016-12-03 19:54:40 +01:00
return;
}
2018-10-05 21:06:58 +02:00
if (args.length == 2 && sender instanceof Player) {
2016-12-03 19:54:40 +01:00
ClickEvent onClickConfirm = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/dungeonsxl delete " + args[1] + " true");
2020-04-05 15:00:39 +02:00
TextComponent confirm = new TextComponent(DMessage.BUTTON_ACCEPT.getMessage());
2016-12-03 19:54:40 +01:00
confirm.setClickEvent(onClickConfirm);
ClickEvent onClickDeny = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/dungeonsxl delete " + args[1] + " false");
2020-04-05 15:00:39 +02:00
TextComponent deny = new TextComponent(DMessage.BUTTON_DENY.getMessage());
2016-12-03 19:54:40 +01:00
deny.setClickEvent(onClickDeny);
2017-07-21 18:33:21 +02:00
MessageUtil.sendMessage(sender, DMessage.CMD_DELETE_BACKUPS.getMessage());
2022-04-03 18:09:12 +02:00
((Player) sender).spigot().sendMessage(confirm, new TextComponent(" "), deny);
2016-12-03 19:54:40 +01:00
return;
}
if (resource.getEditWorld() != null) {
resource.getEditWorld().delete(false);
2016-12-03 19:54:40 +01:00
}
plugin.getMapRegistry().remove(resource);
2018-02-14 16:31:41 +01:00
FileUtil.removeDir(resource.getFolder());
2016-12-03 19:54:40 +01:00
if (args[2].equalsIgnoreCase("true")) {
for (File file : DungeonsXL.BACKUPS.listFiles()) {
if (file.getName().startsWith(resource.getName() + "-")) {
2018-02-14 16:31:41 +01:00
FileUtil.removeDir(file);
2016-12-03 19:54:40 +01:00
}
}
}
List<Dungeon> toRemove = new ArrayList<>();
for (Dungeon dungeon : plugin.getDungeonRegistry()) {
if (dungeon.getStartFloor().equals(resource)) {
toRemove.add(dungeon);
if (dungeon.isMultiFloor()) {
((DDungeon) dungeon).getConfig().getFile().delete();
}
} else if (dungeon.isMultiFloor() && dungeon.getEndFloor().equals(resource)) {
toRemove.add(dungeon);
((DDungeon) dungeon).getConfig().getFile().delete();
} else if (dungeon.isMultiFloor() && dungeon.getFloors().contains(resource)) {
dungeon.removeFloor(resource);
DungeonConfig config = ((DDungeon) dungeon).getConfig();
List<String> floors = config.getConfig().getStringList("floors");
floors.remove(resource.getName());
config.getConfig().set("floors", floors);
config.save();
}
}
toRemove.forEach(plugin.getDungeonRegistry()::remove);
2017-07-21 18:33:21 +02:00
MessageUtil.sendMessage(sender, DMessage.CMD_DELETE_SUCCESS.getMessage(args[1]));
2016-12-03 19:54:40 +01:00
}
}