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

83 lines
2.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.TaskManager;
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-04-02 07:30:26 +02:00
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
2015-09-13 06:04:31 +02:00
public class SpongeCommand implements CommandCallable {
2015-07-30 16:25:16 +02:00
@Override
2016-04-02 07:30:26 +02:00
public CommandResult process(CommandSource cmd, String string) throws CommandException {
2016-03-26 20:47:34 +01:00
TaskManager.runTask(() -> {
2016-04-02 07:30:26 +02:00
String id = cmd.getIdentifier();
2016-03-26 20:47:34 +01:00
PlotPlayer pp;
try {
2016-04-02 07:30:26 +02:00
UUID uuid = UUID.fromString(id);
Player player = SpongeMain.THIS.getServer().getPlayer(uuid).get();
2016-03-26 20:47:34 +01:00
pp = SpongeUtil.getPlayer(player);
2016-04-02 07:30:26 +02:00
} catch (Exception e) {
2016-03-26 20:47:34 +01:00
pp = ConsolePlayer.getConsole();
}
2016-03-26 20:47:34 +01:00
MainCommand.onCommand(pp, 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-04-02 07:30:26 +02:00
public List<String> getSuggestions(CommandSource source, String s) throws CommandException {
if (!(source instanceof Player)) {
return null;
}
2016-04-02 07:30:26 +02:00
PlotPlayer player = SpongeUtil.getPlayer((Player) source);
2016-03-26 20:47:34 +01:00
String[] args = s.split(" ");
if (args.length == 0) {
return Collections.singletonList(MainCommand.getInstance().toString());
}
2016-03-26 20:47:34 +01:00
Collection objects = MainCommand.getInstance().tab(player, args, s.endsWith(" "));
if (objects == null) {
return null;
}
2016-03-26 20:47:34 +01:00
List<String> result = new ArrayList<>();
for (Object o : objects) {
result.add(o.toString());
}
2016-04-02 07:30:26 +02:00
return result.isEmpty() ? null : result;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2016-04-02 07:30:26 +02:00
public boolean testPermission(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
2016-04-02 07:30:26 +02:00
public Optional<? extends Text> getShortDescription(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
2016-04-02 07:30:26 +02:00
public Optional<? extends Text> getHelp(CommandSource cmd) {
2016-03-26 20:47:34 +01:00
return Optional.of(Text.of("/plot"));
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-04-02 07:30:26 +02:00
public Text getUsage(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
}