2013-03-28 17:57:49 +01:00
|
|
|
package com.gmail.nossr50.commands;
|
|
|
|
|
2019-01-15 07:11:58 +01:00
|
|
|
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
|
|
|
import com.gmail.nossr50.util.commands.CommandUtils;
|
|
|
|
import com.gmail.nossr50.util.player.UserManager;
|
|
|
|
import com.google.common.collect.ImmutableList;
|
2013-03-28 17:57:49 +01:00
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.command.TabExecutor;
|
|
|
|
import org.bukkit.util.StringUtil;
|
|
|
|
|
2019-01-15 07:11:58 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2013-03-28 17:57:49 +01:00
|
|
|
|
|
|
|
public abstract class ToggleCommand implements TabExecutor {
|
|
|
|
@Override
|
|
|
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
|
|
|
switch (args.length) {
|
|
|
|
case 0:
|
|
|
|
if (CommandUtils.noConsoleUsage(sender)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasSelfPermission(sender)) {
|
|
|
|
sender.sendMessage(command.getPermissionMessage());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-20 06:23:19 +02:00
|
|
|
if (!CommandUtils.hasPlayerDataKey(sender)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-29 16:45:27 +01:00
|
|
|
applyCommandAction(UserManager.getPlayer(sender.getName()));
|
2013-03-28 17:57:49 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
if (!hasOtherPermission(sender)) {
|
|
|
|
sender.sendMessage(command.getPermissionMessage());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-03 17:07:13 +01:00
|
|
|
String playerName = CommandUtils.getMatchedPlayerName(args[0]);
|
2013-10-29 16:45:27 +01:00
|
|
|
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(playerName);
|
2013-03-28 17:57:49 +01:00
|
|
|
|
2013-08-20 20:10:49 +02:00
|
|
|
if (!CommandUtils.checkPlayerExistence(sender, playerName, mcMMOPlayer)) {
|
2013-03-28 17:57:49 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-29 16:45:27 +01:00
|
|
|
if (CommandUtils.isOffline(sender, mcMMOPlayer.getPlayer())) {
|
2013-03-28 17:57:49 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-29 16:45:27 +01:00
|
|
|
applyCommandAction(mcMMOPlayer);
|
|
|
|
sendSuccessMessage(sender, playerName);
|
2013-03-28 17:57:49 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
|
|
|
switch (args.length) {
|
|
|
|
case 1:
|
2014-02-28 21:55:32 +01:00
|
|
|
List<String> playerNames = CommandUtils.getOnlinePlayerNames(sender);
|
2013-03-28 17:57:49 +01:00
|
|
|
return StringUtil.copyPartialMatches(args[0], playerNames, new ArrayList<String>(playerNames.size()));
|
|
|
|
default:
|
|
|
|
return ImmutableList.of();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract boolean hasOtherPermission(CommandSender sender);
|
|
|
|
protected abstract boolean hasSelfPermission(CommandSender sender);
|
2013-10-29 16:45:27 +01:00
|
|
|
protected abstract void applyCommandAction(McMMOPlayer mcMMOPlayer);
|
|
|
|
protected abstract void sendSuccessMessage(CommandSender sender, String playerName);
|
2013-03-28 17:57:49 +01:00
|
|
|
}
|