2013-03-28 17:57:49 +01:00
|
|
|
package com.gmail.nossr50.commands;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.command.TabExecutor;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.util.StringUtil;
|
|
|
|
|
|
|
|
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
2013-08-20 20:10:49 +02:00
|
|
|
import com.gmail.nossr50.util.Misc;
|
2013-03-28 17:57:49 +01:00
|
|
|
import com.gmail.nossr50.util.commands.CommandUtils;
|
|
|
|
import com.gmail.nossr50.util.player.UserManager;
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
|
|
|
|
public abstract class ToggleCommand implements TabExecutor {
|
|
|
|
protected McMMOPlayer mcMMOPlayer;
|
|
|
|
protected Player player;
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
2013-05-28 19:33:20 +02:00
|
|
|
player = (Player) sender;
|
|
|
|
mcMMOPlayer = UserManager.getPlayer(player);
|
2013-03-28 17:57:49 +01:00
|
|
|
|
|
|
|
applyCommandAction();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
if (!hasOtherPermission(sender)) {
|
|
|
|
sender.sendMessage(command.getPermissionMessage());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-20 20:10:49 +02:00
|
|
|
String playerName = Misc.getMatchedPlayerName(args[0]);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
player = mcMMOPlayer.getPlayer();
|
|
|
|
|
|
|
|
if (CommandUtils.isOffline(sender, player)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
applyCommandAction();
|
|
|
|
sendSuccessMessage(sender);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
|
|
|
switch (args.length) {
|
|
|
|
case 1:
|
2013-07-15 15:14:23 +02:00
|
|
|
Set<String> playerNames = UserManager.getPlayerNames();
|
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);
|
|
|
|
protected abstract void applyCommandAction();
|
|
|
|
protected abstract void sendSuccessMessage(CommandSender sender);
|
|
|
|
}
|