Skip to content

Commit

Permalink
First attempt at integrating Piston as the only command system
Browse files Browse the repository at this point in the history
  • Loading branch information
octylFractal committed Apr 15, 2019
1 parent da35b3c commit 267ccf2
Show file tree
Hide file tree
Showing 28 changed files with 494 additions and 390 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ subprojects {
mavenCentral()
maven { url "http://maven.sk89q.com/repo/" }
maven { url "http://repo.maven.apache.org/maven2" }
// temporary, for Piston
mavenLocal()
}

if (JavaVersion.current().isJava8Compatible()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,29 @@

package com.sk89q.worldedit.bukkit;

import com.google.common.collect.ImmutableMap;
import com.google.inject.Key;
import com.sk89q.bukkit.util.CommandInspector;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.util.command.CommandMapping;
import com.sk89q.worldedit.util.command.Description;
import com.sk89q.worldedit.util.command.Dispatcher;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.CommandParameters;
import org.enginehub.piston.NoInputCommandParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;

import static com.google.common.base.Preconditions.checkNotNull;

class BukkitCommandInspector implements CommandInspector {

private static final Logger logger = LoggerFactory.getLogger(BukkitCommandInspector.class);
private final WorldEditPlugin plugin;
private final Dispatcher dispatcher;
private final CommandManager dispatcher;

BukkitCommandInspector(WorldEditPlugin plugin, Dispatcher dispatcher) {
BukkitCommandInspector(WorldEditPlugin plugin, CommandManager dispatcher) {
checkNotNull(plugin);
checkNotNull(dispatcher);
this.plugin = plugin;
Expand All @@ -47,9 +50,9 @@ class BukkitCommandInspector implements CommandInspector {

@Override
public String getShortText(Command command) {
CommandMapping mapping = dispatcher.get(command.getName());
if (mapping != null) {
return mapping.getDescription().getDescription();
Optional<org.enginehub.piston.Command> mapping = dispatcher.getCommand(command.getName());
if (mapping.isPresent()) {
return mapping.get().getDescription();
} else {
logger.warn("BukkitCommandInspector doesn't know how about the command '" + command + "'");
return "Help text not available";
Expand All @@ -58,10 +61,9 @@ public String getShortText(Command command) {

@Override
public String getFullText(Command command) {
CommandMapping mapping = dispatcher.get(command.getName());
if (mapping != null) {
Description description = mapping.getDescription();
return "Usage: " + description.getUsage() + (description.getHelp() != null ? "\n" + description.getHelp() : "");
Optional<org.enginehub.piston.Command> mapping = dispatcher.getCommand(command.getName());
if (mapping.isPresent()) {
return mapping.get().getFullHelp();
} else {
logger.warn("BukkitCommandInspector doesn't know how about the command '" + command + "'");
return "Help text not available";
Expand All @@ -70,11 +72,14 @@ public String getFullText(Command command) {

@Override
public boolean testPermission(CommandSender sender, Command command) {
CommandMapping mapping = dispatcher.get(command.getName());
if (mapping != null) {
CommandLocals locals = new CommandLocals();
locals.put(Actor.class, plugin.wrapCommandSender(sender));
return mapping.getCallable().testPermission(locals);
Optional<org.enginehub.piston.Command> mapping = dispatcher.getCommand(command.getName());
if (mapping.isPresent()) {
CommandParameters parameters = NoInputCommandParameters.builder()
.injectedValues(ImmutableMap.of(
Key.get(Actor.class), plugin.wrapCommandSender(sender)
))
.build();
return mapping.get().getCondition().satisfied(parameters);
} else {
logger.warn("BukkitCommandInspector doesn't know how about the command '" + command + "'");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,28 @@
import com.sk89q.bukkit.util.CommandInfo;
import com.sk89q.bukkit.util.CommandRegistration;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.command.util.PermissionCondition;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extension.platform.MultiUserPlatform;
import com.sk89q.worldedit.extension.platform.Preference;
import com.sk89q.worldedit.util.command.CommandMapping;
import com.sk89q.worldedit.util.command.Description;
import com.sk89q.worldedit.util.command.Dispatcher;
import com.sk89q.worldedit.world.registry.Registries;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.enginehub.piston.CommandManager;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class BukkitServerInterface implements MultiUserPlatform {
public Server server;
Expand Down Expand Up @@ -116,20 +117,25 @@ public BukkitWorld matchWorld(com.sk89q.worldedit.world.World world) {
}

@Override
public void registerCommands(Dispatcher dispatcher) {
List<CommandInfo> toRegister = new ArrayList<>();
public void registerCommands(CommandManager dispatcher) {
BukkitCommandInspector inspector = new BukkitCommandInspector(plugin, dispatcher);

for (CommandMapping command : dispatcher.getCommands()) {
Description description = command.getDescription();
List<String> permissions = description.getPermissions();
String[] permissionsArray = new String[permissions.size()];
permissions.toArray(permissionsArray);

toRegister.add(new CommandInfo(description.getUsage(), description.getDescription(), command.getAllAliases(), inspector, permissionsArray));
}

dynamicCommands.register(toRegister);
dynamicCommands.register(dispatcher.getAllCommands()
.map(command -> {
String[] permissionsArray = command.getCondition()
.as(PermissionCondition.class)
.map(PermissionCondition::getPermissions)
.map(s -> s.toArray(new String[0]))
.orElseGet(() -> new String[0]);

String[] aliases = Stream.concat(
Stream.of(command.getName()),
command.getAliases().stream()
).toArray(String[]::new);
return new CommandInfo(command.getUsage(),
command.getDescription(), aliases,
inspector, permissionsArray);
}).collect(Collectors.toList()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

package com.sk89q.worldedit.bukkit;

import com.sk89q.minecraft.util.commands.CommandLocals;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Key;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.command.CommandMapping;
import com.sk89q.worldedit.world.World;
import org.bukkit.block.Block;
import org.bukkit.event.Event.Result;
Expand All @@ -40,9 +40,9 @@
import org.bukkit.event.player.PlayerGameModeChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;

import java.util.Set;
import java.util.stream.Collectors;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.CommandParameters;
import org.enginehub.piston.NoInputCommandParameters;

/**
* Handles all events thrown in relation to a Player
Expand Down Expand Up @@ -87,7 +87,7 @@ public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {

if (split.length > 0) {
split[0] = split[0].substring(1);
split = plugin.getWorldEdit().getPlatformManager().getCommandManager().commandDetection(split);
split = plugin.getWorldEdit().getPlatformManager().getPlatformCommandMananger().commandDetection(split);
}

final String newMessage = "/" + StringUtil.joinString(split, " ");
Expand All @@ -108,13 +108,18 @@ public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {

@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerCommand(PlayerCommandSendEvent event) {
CommandLocals locals = new CommandLocals();
locals.put(Actor.class, plugin.wrapCommandSender(event.getPlayer()));
Set<String> toRemove = plugin.getWorldEdit().getPlatformManager().getCommandManager().getDispatcher().getCommands().stream()
.filter(commandMapping -> !commandMapping.getCallable().testPermission(locals))
.map(CommandMapping::getPrimaryAlias)
.collect(Collectors.toSet());
event.getCommands().removeIf(toRemove::contains);
CommandParameters parameters = NoInputCommandParameters.builder()
.injectedValues(ImmutableMap.of(
Key.get(Actor.class), plugin.wrapCommandSender(event.getPlayer())
))
.build();
CommandManager commandManager = plugin.getWorldEdit().getPlatformManager().getPlatformCommandMananger().getCommandManager();
event.getCommands().removeIf(name ->
// remove if in the manager and not satisfied
commandManager.getCommand(name)
.filter(command -> !command.getCondition().satisfied(parameters))
.isPresent()
);
}

/**
Expand Down
11 changes: 4 additions & 7 deletions worldedit-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id("net.ltgt.apt") version "0.21"
}

apply plugin: 'java-library'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'net.ltgt.apt-eclipse'
Expand All @@ -13,11 +14,6 @@ configurations.all { Configuration it ->
}
}

repositories {
// temporary, for Piston
mavenLocal()
}

dependencies {
compile 'de.schlichtherle:truezip:6.8.3'
compile 'rhino:js:1.7R2'
Expand All @@ -32,10 +28,11 @@ dependencies {
compile 'org.slf4j:slf4j-api:1.7.26'

def pistonVersion = '0.0.1-SNAPSHOT'
api "org.enginehub.piston:core:$pistonVersion"
compileOnly "org.enginehub.piston.core-ap:annotations:$pistonVersion"
compile "org.enginehub.piston.core-ap:runtime:$pistonVersion"
implementation "org.enginehub.piston.core-ap:runtime:$pistonVersion"
annotationProcessor "org.enginehub.piston.core-ap:processor:$pistonVersion"
compile "org.enginehub.piston:default-impl:$pistonVersion"
api "org.enginehub.piston:default-impl:$pistonVersion"
//compile 'net.sf.trove4j:trove4j:3.0.3'
testCompile 'org.mockito:mockito-core:1.9.0-rc1'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extension.platform.CommandManager;
import com.sk89q.worldedit.extension.platform.PlatformCommandMananger;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.function.pattern.BlockPattern;
Expand Down Expand Up @@ -67,6 +67,7 @@
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockTypes;
import org.enginehub.piston.CommandManager;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -598,7 +599,10 @@ private static CommandMapping detectCommand(Dispatcher dispatcher, String comman
}

public static void help(CommandContext args, WorldEdit we, Actor actor) {
CommandCallable callable = we.getPlatformManager().getCommandManager().getDispatcher();
CommandManager manager = we.getPlatformManager().getPlatformCommandMananger().getCommandManager();

// TODO this will be implemented as a special utility in the manager
/*
int page = 0;
final int perPage = actor instanceof Player ? 8 : 20; // More pages for console
Expand Down Expand Up @@ -626,15 +630,15 @@ public static void help(CommandContext args, WorldEdit we, Actor actor) {
for (int i = 0; i < effectiveLength; i++) {
String command = args.getString(i);
if (callable instanceof Dispatcher) {
if (manager instanceof Dispatcher) {
// Chop off the beginning / if we're are the root level
if (isRootLevel && command.length() > 1 && command.charAt(0) == '/') {
command = command.substring(1);
}
CommandMapping mapping = detectCommand((Dispatcher) callable, command, isRootLevel);
CommandMapping mapping = detectCommand((Dispatcher) manager, command, isRootLevel);
if (mapping != null) {
callable = mapping.getCallable();
manager = mapping.getCallable();
} else {
if (isRootLevel) {
actor.printError(String.format("The command '%s' could not be found.", args.getString(i)));
Expand All @@ -656,12 +660,12 @@ public static void help(CommandContext args, WorldEdit we, Actor actor) {
}
// Create the message
if (callable instanceof Dispatcher) {
Dispatcher dispatcher = (Dispatcher) callable;
if (manager instanceof Dispatcher) {
Dispatcher dispatcher = (Dispatcher) manager;
// Get a list of aliases
List<CommandMapping> aliases = new ArrayList<>(dispatcher.getCommands());
aliases.sort(new PrimaryAliasComparator(CommandManager.COMMAND_CLEAN_PATTERN));
aliases.sort(new PrimaryAliasComparator(PlatformCommandMananger.COMMAND_CLEAN_PATTERN));
// Calculate pagination
int offset = perPage * page;
Expand Down Expand Up @@ -698,9 +702,10 @@ public static void help(CommandContext args, WorldEdit we, Actor actor) {
actor.printRaw(ColorCodeBuilder.asColorCodes(box));
} else {
CommandUsageBox box = new CommandUsageBox(callable, Joiner.on(" ").join(visited));
CommandUsageBox box = new CommandUsageBox(manager, Joiner.on(" ").join(visited));
actor.printRaw(ColorCodeBuilder.asColorCodes(box));
}
*/
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void report(Actor actor, CommandContext args) throws WorldEditException {
actor.checkPermission("worldedit.report.pastebin");
ActorCallbackPaste.pastebin(
we.getSupervisor(), actor, result, "WorldEdit report: %s.report",
WorldEdit.getInstance().getPlatformManager().getCommandManager().getExceptionConverter()
WorldEdit.getInstance().getPlatformManager().getPlatformCommandMananger().getExceptionConverter()
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.command.argument;

import org.enginehub.piston.InjectedValueAccess;

/**
* Key-interface for {@link InjectedValueAccess} for the String arguments.
*/
public interface Arguments {

String get();

}
Loading

0 comments on commit 267ccf2

Please sign in to comment.