2015-07-30 16:25:16 +02:00
|
|
|
package com.plotsquared.sponge.util;
|
|
|
|
|
2016-02-14 02:01:18 +01:00
|
|
|
import com.intellectualcrafters.plot.commands.MainCommand;
|
|
|
|
import com.intellectualcrafters.plot.object.ConsolePlayer;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
2016-02-23 19:12:07 +01:00
|
|
|
import com.intellectualcrafters.plot.util.Permissions;
|
|
|
|
import com.intellectualcrafters.plot.util.StringComparison;
|
2016-02-25 10:13:07 +01:00
|
|
|
import com.intellectualcrafters.plot.util.TaskManager;
|
2016-02-23 19:12:07 +01:00
|
|
|
import com.plotsquared.general.commands.Command;
|
2016-02-14 02:01:18 +01:00
|
|
|
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;
|
|
|
|
|
2016-02-23 19:12:07 +01:00
|
|
|
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 {
|
2016-02-25 10:13:07 +01:00
|
|
|
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
|
2016-02-23 19:12:07 +01:00
|
|
|
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(" ");
|
2016-02-25 10:13:07 +01:00
|
|
|
if (split.length < 1) {
|
2016-02-23 19:12:07 +01:00
|
|
|
return Collections.singletonList("plots");
|
|
|
|
}
|
2016-02-25 10:13:07 +01:00
|
|
|
if (split.length > 1) {
|
|
|
|
return Collections.emptyList();
|
2016-02-23 19:12:07 +01:00
|
|
|
}
|
|
|
|
final Set<String> tabOptions = new HashSet<>();
|
2016-02-25 10:13:07 +01:00
|
|
|
final String arg = split[0].toLowerCase();
|
2016-02-23 19:12:07 +01:00
|
|
|
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);
|
|
|
|
}
|
2016-02-25 10:13:07 +01:00
|
|
|
return Collections.emptyList();
|
2016-02-23 19:12:07 +01: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 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
|
|
|
}
|