PlotSquared/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeCommand.java

102 lines
3.7 KiB
Java
Raw Normal View History

2015-07-30 16:25:16 +02:00
package com.plotsquared.sponge.util;
import com.intellectualcrafters.plot.commands.MainCommand;
import com.intellectualcrafters.plot.object.ConsolePlayer;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualcrafters.plot.util.StringComparison;
import com.intellectualcrafters.plot.util.TaskManager;
import com.plotsquared.general.commands.Command;
import com.plotsquared.sponge.SpongeMain;
2015-12-19 20:30:06 +01:00
import org.spongepowered.api.command.CommandCallable;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
2015-10-07 08:33:33 +02:00
import org.spongepowered.api.entity.living.player.Player;
2015-07-30 16:25:16 +02:00
import org.spongepowered.api.text.Text;
import java.util.*;
2015-07-30 16:25:16 +02:00
2015-09-13 06:04:31 +02:00
public class SpongeCommand implements CommandCallable {
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public CommandResult process(final CommandSource cmd, final String string) throws CommandException {
TaskManager.runTask(new Runnable() {
@Override
public void run() {
final String id = cmd.getIdentifier();
PlotPlayer pp;
try {
final UUID uuid = UUID.fromString(id);
final Player player = SpongeMain.THIS.getServer().getPlayer(uuid).get();
pp = SpongeUtil.getPlayer(player);
} catch (final Exception e) {
pp = ConsolePlayer.getConsole();
}
MainCommand.onCommand(pp, cmd.getName(), string.isEmpty() ? new String[]{} : string.split(" "));
}
});
return CommandResult.success();
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
public List<String> getSuggestions(final CommandSource source, final String string) throws CommandException {
if (!(source instanceof Player)) {
return null;
}
final PlotPlayer player = SpongeUtil.getPlayer((Player) source);
String[] split = string.split(" ");
if (split.length < 1) {
return Collections.singletonList("plots");
}
if (split.length > 1) {
return Collections.emptyList();
}
final Set<String> tabOptions = new HashSet<>();
final String arg = split[0].toLowerCase();
ArrayList<String> labels = new ArrayList<>();
for (final Command<PlotPlayer> cmd : MainCommand.getInstance().getCommands()) {
final String label = cmd.getCommand();
HashSet<String> aliases = new HashSet<>(cmd.getAliases());
aliases.add(label);
for (String alias : aliases) {
labels.add(alias);
if (alias.startsWith(arg)) {
if (Permissions.hasPermission(player, cmd.getPermission())) {
tabOptions.add(label);
} else {
break;
}
}
}
}
String best = new StringComparison<>(arg, labels).getBestMatch();
tabOptions.add(best);
if (!tabOptions.isEmpty()) {
return new ArrayList<>(tabOptions);
}
return Collections.emptyList();
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean testPermission(final CommandSource cmd) {
2015-07-30 16:25:16 +02:00
return true;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public Optional<? extends Text> getShortDescription(final CommandSource cmd) {
2016-02-19 18:45:43 +01:00
return Optional.of(Text.of("Shows plot help"));
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public Optional<? extends Text> getHelp(final CommandSource cmd) {
2016-02-19 18:45:43 +01:00
return Optional.of(Text.of("/plot help"));
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public Text getUsage(final CommandSource cmd) {
2016-02-19 18:45:43 +01:00
return Text.of("/plot <command>");
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
}