First attempt at integrating Piston as the only command system

This commit is contained in:
Kenzie Togami
2019-04-15 01:21:15 -07:00
parent da35b3c174
commit 267ccf2298
28 changed files with 493 additions and 389 deletions

View File

@ -19,23 +19,29 @@
package com.sk89q.worldedit.sponge;
import com.sk89q.worldedit.util.command.CommandMapping;
import com.sk89q.worldedit.command.util.PermissionCondition;
import org.enginehub.piston.Command;
import org.spongepowered.api.command.CommandCallable;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.text.Text;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
public abstract class CommandAdapter implements CommandCallable {
private CommandMapping command;
private Command command;
protected CommandAdapter(CommandMapping command) {
protected CommandAdapter(Command command) {
this.command = command;
}
@Override
public boolean testPermission(CommandSource source) {
for (String perm : command.getDescription().getPermissions()) {
Set<String> permissions = command.getCondition().as(PermissionCondition.class)
.map(PermissionCondition::getPermissions)
.orElseGet(Collections::emptySet);
for (String perm : permissions) {
if (!source.hasPermission(perm)) {
return false;
}
@ -45,8 +51,8 @@ public abstract class CommandAdapter implements CommandCallable {
@Override
public Optional<Text> getShortDescription(CommandSource source) {
String description = command.getDescription().getDescription();
if (description != null && !description.isEmpty()) {
String description = command.getDescription();
if (!description.isEmpty()) {
return Optional.of(Text.of(description));
}
return Optional.empty();
@ -54,8 +60,8 @@ public abstract class CommandAdapter implements CommandCallable {
@Override
public Optional<Text> getHelp(CommandSource source) {
String help = command.getDescription().getHelp();
if (help != null && !help.isEmpty()) {
String help = command.getFullHelp();
if (!help.isEmpty()) {
return Optional.of(Text.of(help));
}
return Optional.empty();
@ -63,6 +69,6 @@ public abstract class CommandAdapter implements CommandCallable {
@Override
public Text getUsage(CommandSource source) {
return Text.of(command.getDescription().getUsage());
return Text.of(command.getUsage());
}
}

View File

@ -19,6 +19,7 @@
package com.sk89q.worldedit.sponge;
import com.google.common.collect.ImmutableList;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.event.platform.CommandEvent;
@ -33,6 +34,8 @@ import com.sk89q.worldedit.util.command.CommandMapping;
import com.sk89q.worldedit.util.command.Dispatcher;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.registry.Registries;
import org.enginehub.piston.Command;
import org.enginehub.piston.CommandManager;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
@ -50,6 +53,8 @@ import java.util.Optional;
import javax.annotation.Nullable;
import static java.util.stream.Collectors.toList;
class SpongePlatform extends AbstractPlatform implements MultiUserPlatform {
private final SpongeWorldEdit mod;
@ -122,24 +127,26 @@ class SpongePlatform extends AbstractPlatform implements MultiUserPlatform {
}
@Override
public void registerCommands(Dispatcher dispatcher) {
for (CommandMapping command : dispatcher.getCommands()) {
public void registerCommands(CommandManager manager) {
for (Command command : manager.getAllCommands().collect(toList())) {
CommandAdapter adapter = new CommandAdapter(command) {
@Override
public CommandResult process(CommandSource source, String arguments) throws org.spongepowered.api.command.CommandException {
CommandEvent weEvent = new CommandEvent(SpongeWorldEdit.inst().wrapCommandSource(source), command.getPrimaryAlias() + " " + arguments);
CommandEvent weEvent = new CommandEvent(SpongeWorldEdit.inst().wrapCommandSource(source), command.getName() + " " + arguments);
WorldEdit.getInstance().getEventBus().post(weEvent);
return weEvent.isCancelled() ? CommandResult.success() : CommandResult.empty();
}
@Override
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<org.spongepowered.api.world.World> targetPosition) throws CommandException {
CommandSuggestionEvent weEvent = new CommandSuggestionEvent(SpongeWorldEdit.inst().wrapCommandSource(source), command.getPrimaryAlias() + " " + arguments);
CommandSuggestionEvent weEvent = new CommandSuggestionEvent(SpongeWorldEdit.inst().wrapCommandSource(source), command.getName() + " " + arguments);
WorldEdit.getInstance().getEventBus().post(weEvent);
return weEvent.getSuggestions();
}
};
Sponge.getCommandManager().register(SpongeWorldEdit.inst(), adapter, command.getAllAliases());
ImmutableList.Builder<String> aliases = ImmutableList.builder();
aliases.add(command.getName()).addAll(command.getAliases());
Sponge.getCommandManager().register(SpongeWorldEdit.inst(), adapter, aliases.build());
}
}