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

89 lines
3.2 KiB
Java
Raw Normal View History

2016-03-01 22:08:07 +01:00
/*
2022-01-08 23:02:57 +01:00
* Copyright (C) 2012-2022 Frank Baumann
2016-03-01 22:08:07 +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-03-01 22:08:07 +01:00
2018-05-03 21:54:25 +02:00
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.dungeon.Game;
2020-04-25 21:49:44 +02:00
import de.erethon.dungeonsxl.api.event.group.GroupPlayerLeaveEvent;
2020-11-02 21:17:19 +01:00
import de.erethon.dungeonsxl.api.event.player.EditPlayerLeaveEvent;
import de.erethon.dungeonsxl.api.player.EditPlayer;
import de.erethon.dungeonsxl.api.player.GamePlayer;
import de.erethon.dungeonsxl.api.player.GlobalPlayer;
import de.erethon.dungeonsxl.api.player.PlayerGroup;
2018-05-03 21:54:25 +02:00
import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.player.DPermission;
2022-04-03 18:09:12 +02:00
import de.erethon.bedrock.chat.MessageUtil;
import org.bukkit.Bukkit;
2016-03-01 22:08:07 +01:00
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
/**
* @author Frank Baumann, Daniel Saukel
*/
2018-08-18 16:52:51 +02:00
public class LeaveCommand extends DCommand {
2016-03-01 22:08:07 +01:00
2018-08-18 16:52:51 +02:00
public LeaveCommand(DungeonsXL plugin) {
super(plugin);
2016-03-01 22:08:07 +01:00
setCommand("leave");
setMinArgs(0);
setMaxArgs(0);
setHelp(DMessage.CMD_LEAVE_HELP.getMessage());
2017-11-16 18:25:10 +01:00
setPermission(DPermission.LEAVE.getNode());
2016-03-01 22:08:07 +01:00
setPlayerCommand(true);
}
@Override
public void onExecute(String[] args, CommandSender sender) {
Player player = (Player) sender;
2020-11-02 21:17:19 +01:00
GlobalPlayer globalPlayer = dPlayers.get(player);
Game game = plugin.getGame(player);
2016-03-01 22:08:07 +01:00
2016-09-10 14:01:11 +02:00
if (game != null && game.isTutorial()) {
2017-07-21 18:33:21 +02:00
MessageUtil.sendMessage(player, DMessage.ERROR_NO_LEAVE_IN_TUTORIAL.getMessage());
return;
2016-03-01 22:08:07 +01:00
}
2020-11-02 21:17:19 +01:00
PlayerGroup group = globalPlayer.getGroup();
2020-11-02 21:17:19 +01:00
if (group == null && !(globalPlayer instanceof EditPlayer)) {
2017-07-21 18:33:21 +02:00
MessageUtil.sendMessage(player, DMessage.ERROR_JOIN_GROUP.getMessage());
2016-05-21 23:33:17 +02:00
return;
}
2016-03-01 22:08:07 +01:00
2020-11-02 21:17:19 +01:00
if (globalPlayer instanceof GamePlayer) {
GroupPlayerLeaveEvent event = new GroupPlayerLeaveEvent(group, globalPlayer);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}
((GamePlayer) globalPlayer).leave();
} else if (globalPlayer instanceof EditPlayer) {
EditPlayerLeaveEvent event = new EditPlayerLeaveEvent((EditPlayer) globalPlayer, false, true);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}
((EditPlayer) globalPlayer).leave(event.getUnloadIfEmpty());
2016-03-01 22:08:07 +01:00
} else {
2020-11-02 21:17:19 +01:00
group.removeMember(player);
2016-03-01 22:08:07 +01:00
}
2016-05-21 23:33:17 +02:00
2017-07-21 18:33:21 +02:00
MessageUtil.sendMessage(player, DMessage.CMD_LEAVE_SUCCESS.getMessage());
2016-03-01 22:08:07 +01:00
}
}