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

88 lines
3.0 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-06-09 01:06:07 +02:00
import org.spongepowered.api.world.World;
2015-07-30 16:25:16 +02:00
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 {
2016-06-09 01:06:07 +02:00
2015-07-30 16:25:16 +02:00
@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
2016-03-26 20:47:34 +01:00
TaskManager.runTask(() -> {
String id = source.getIdentifier();
PlotPlayer plotPlayer = null;
2016-03-26 20:47:34 +01:00
try {
2016-04-02 07:30:26 +02:00
UUID uuid = UUID.fromString(id);
Optional<Player> player = SpongeMain.THIS.getServer().getPlayer(uuid);
if (player.isPresent()) {
plotPlayer = SpongeUtil.getPlayer(player.get());
}
2016-05-12 23:09:35 +02:00
} catch (Exception ignored) {
plotPlayer = ConsolePlayer.getConsole();
}
MainCommand.onCommand(plotPlayer, arguments.isEmpty() ? new String[]{} : arguments.split(" "));
});
return CommandResult.success();
2015-07-30 16:25:16 +02:00
}
2016-06-09 01:06:07 +02:00
2015-07-30 16:25:16 +02:00
@Override
2016-06-09 01:06:07 +02:00
public List<String> getSuggestions(CommandSource source, String arguments, org.spongepowered.api.world.Location<World> targetPosition)
throws CommandException {
if (!(source instanceof Player)) {
return null;
}
2016-04-02 07:30:26 +02:00
PlotPlayer player = SpongeUtil.getPlayer((Player) source);
String[] args = arguments.split(" ");
2016-03-26 20:47:34 +01:00
if (args.length == 0) {
return Collections.singletonList(MainCommand.getInstance().toString());
}
Collection objects = MainCommand.getInstance().tab(player, args, arguments.endsWith(" "));
2016-03-26 20:47:34 +01:00
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;
2016-06-09 01:06:07 +02:00
}
2015-07-30 16:25:16 +02:00
@Override
public boolean testPermission(CommandSource source) {
2015-07-30 16:25:16 +02:00
return true;
}
2016-06-09 01:06:07 +02:00
2015-07-30 16:25:16 +02:00
@Override
public Optional<Text> getShortDescription(CommandSource source) {
2016-02-19 18:45:43 +01:00
return Optional.of(Text.of("Shows plot help"));
2015-07-30 16:25:16 +02:00
}
2016-06-09 01:06:07 +02:00
2015-07-30 16:25:16 +02:00
@Override
public Optional<Text> getHelp(CommandSource source) {
2016-03-26 20:47:34 +01:00
return Optional.of(Text.of("/plot"));
2015-07-30 16:25:16 +02:00
}
2016-06-09 01:06:07 +02:00
2015-07-30 16:25:16 +02:00
@Override
public Text getUsage(CommandSource source) {
2016-02-19 18:45:43 +01:00
return Text.of("/plot <command>");
2015-07-30 16:25:16 +02:00
}
2016-06-09 01:06:07 +02:00
2015-07-30 16:25:16 +02:00
}