3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-11-05 02:50:05 +01:00

Implement suggestions for Bukkit + Sponge

Dieser Commit ist enthalten in:
Kenzie Togami 2019-05-05 22:06:20 -07:00 committet von Kenzie Togami
Ursprung 1c54a04fd1
Commit 8a3e6a12b9
15 geänderte Dateien mit 295 neuen und 133 gelöschten Zeilen

Datei anzeigen

@ -80,36 +80,6 @@ public class WorldEditListener implements Listener {
WorldEdit.getInstance().getSessionManager().get(plugin.wrapPlayer(event.getPlayer()));
}
/**
* Called when a player attempts to use a command
*
* @param event Relevant event details
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
String[] split = event.getMessage().split(" ");
if (split.length > 0) {
split[0] = split[0].substring(1);
split = plugin.getWorldEdit().getPlatformManager().getPlatformCommandManager().commandDetection(split);
}
final String newMessage = "/" + StringUtil.joinString(split, " ");
if (!newMessage.equals(event.getMessage())) {
event.setMessage(newMessage);
plugin.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
if (!event.getMessage().isEmpty()) {
plugin.getServer().dispatchCommand(event.getPlayer(), event.getMessage().substring(1));
}
event.setCancelled(true);
}
}
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerCommand(PlayerCommandSendEvent event) {
InjectedValueStore store = MapBackedValueStore.create();

Datei anzeigen

@ -37,6 +37,7 @@ import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.internal.command.CommandUtil;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockCategory;
@ -313,9 +314,10 @@ public class WorldEditPlugin extends JavaPlugin implements TabCompleter {
System.arraycopy(args, 0, split, 1, args.length);
split[0] = "/" + cmd.getName();
CommandSuggestionEvent event = new CommandSuggestionEvent(wrapCommandSender(sender), Joiner.on(" ").join(split));
String arguments = Joiner.on(" ").join(split);
CommandSuggestionEvent event = new CommandSuggestionEvent(wrapCommandSender(sender), arguments);
getWorldEdit().getEventBus().post(event);
return event.getSuggestions();
return CommandUtil.fixSuggestions(arguments, event.getSuggestions());
}
/**

Datei anzeigen

@ -58,7 +58,6 @@ public abstract class LocalConfiguration {
public String wandItem = "minecraft:wooden_axe";
public boolean superPickaxeDrop = true;
public boolean superPickaxeManyDrop = true;
public boolean noDoubleSlash = false;
public boolean useInventory = false;
public boolean useInventoryOverride = false;
public boolean useInventoryCreativeOverride = false;

Datei anzeigen

@ -19,14 +19,15 @@
package com.sk89q.worldedit.event.platform;
import static com.google.common.base.Preconditions.checkNotNull;
import com.sk89q.worldedit.event.Event;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.internal.util.Substring;
import java.util.Collections;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Posted when suggestions for auto-completion are requested for command input.
*/
@ -34,7 +35,7 @@ public class CommandSuggestionEvent extends Event {
private final Actor actor;
private final String arguments;
private List<String> suggestions = Collections.emptyList();
private List<Substring> suggestions = Collections.emptyList();
/**
* Create a new instance.
@ -71,9 +72,14 @@ public class CommandSuggestionEvent extends Event {
/**
* Get the list of suggestions that are to be presented.
*
* <p>
* Each Substring holds the replacement as the substring,
* and the replacement range as the original substring range.
* </p>
*
* @return the list of suggestions
*/
public List<String> getSuggestions() {
public List<Substring> getSuggestions() {
return suggestions;
}
@ -82,7 +88,7 @@ public class CommandSuggestionEvent extends Event {
*
* @param suggestions the list of suggestions
*/
public void setSuggestions(List<String> suggestions) {
public void setSuggestions(List<Substring> suggestions) {
checkNotNull(suggestions);
this.suggestions = suggestions;
}

Datei anzeigen

@ -91,6 +91,7 @@ import com.sk89q.worldedit.internal.command.CommandLoggingHandler;
import com.sk89q.worldedit.internal.command.CommandRegistrationHandler;
import com.sk89q.worldedit.internal.command.exception.ExceptionConverter;
import com.sk89q.worldedit.internal.command.exception.WorldEditExceptionConverter;
import com.sk89q.worldedit.internal.util.Substring;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.session.request.Request;
import com.sk89q.worldedit.util.eventbus.Subscribe;
@ -125,15 +126,14 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.google.common.base.Preconditions.checkNotNull;
@ -419,34 +419,8 @@ public final class PlatformCommandManager {
dynamicHandler.setHandler(null);
}
public String[] commandDetection(String[] split) {
// Quick script shortcut
if (split[0].matches("^[^/].*\\.js$")) {
String[] newSplit = new String[split.length + 1];
System.arraycopy(split, 0, newSplit, 1, split.length);
newSplit[0] = "cs";
newSplit[1] = newSplit[1];
split = newSplit;
}
String searchCmd = split[0].toLowerCase(Locale.ROOT);
// Try to detect the command
if (!commandManager.containsCommand(searchCmd)) {
if (worldEdit.getConfiguration().noDoubleSlash && commandManager.containsCommand("/" + searchCmd)) {
split[0] = "/" + split[0];
} else if (searchCmd.length() >= 2 && searchCmd.charAt(0) == '/' && commandManager.containsCommand(searchCmd.substring(1))) {
split[0] = split[0].substring(1);
}
}
return split;
}
private String[] parseArgs(String input) {
return commandDetection(new CommandArgParser(input.substring(1))
.parseArgs()
.toArray(String[]::new));
private Stream<Substring> parseArgs(String input) {
return new CommandArgParser(CommandArgParser.spaceSplit(input.substring(1))).parseArgs();
}
@Subscribe
@ -454,7 +428,9 @@ public final class PlatformCommandManager {
Request.reset();
Actor actor = platformManager.createProxyActor(event.getActor());
String[] split = parseArgs(event.getArguments());
String[] split = parseArgs(event.getArguments())
.map(Substring::getSubstring)
.toArray(String[]::new);
// No command found!
if (!commandManager.containsCommand(split[0])) {
@ -581,13 +557,26 @@ public final class PlatformCommandManager {
@Subscribe
public void handleCommandSuggestion(CommandSuggestionEvent event) {
try {
String[] split = parseArgs(event.getArguments());
MemoizingValueAccess access = initializeInjectedValues(event::getArguments, event.getActor());
ImmutableSet<Suggestion> suggestions = commandManager.getSuggestions(access, Arrays.asList(split));
String arguments = event.getArguments();
List<Substring> split = parseArgs(arguments).collect(Collectors.toList());
List<String> argStrings = split.stream()
.map(Substring::getSubstring)
.collect(Collectors.toList());
MemoizingValueAccess access = initializeInjectedValues(() -> arguments, event.getActor());
ImmutableSet<Suggestion> suggestions = commandManager.getSuggestions(access, argStrings);
log.debug("For input: {}", event.getArguments());
log.debug("I would suggest this: {}", suggestions);
// TODO send back suggestions
event.setSuggestions(suggestions.stream()
.map(suggestion -> {
Substring original = suggestion.getReplacedArgument() == split.size()
? Substring.from(arguments, arguments.length() - 1)
: split.get(suggestion.getReplacedArgument());
// increase original points by 1, for removed `/` in `parseArgs`
return Substring.wrap(
suggestion.getSuggestion(),
original.getStart() + 1,
original.getEnd() + 1
);
}).collect(Collectors.toList()));
} catch (CommandException e) {
event.getActor().printError(e.getMessage());
}

Datei anzeigen

@ -19,86 +19,94 @@
package com.sk89q.worldedit.internal.command;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.sk89q.worldedit.internal.util.Substring;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CommandArgParser {
public static ImmutableList<Substring> spaceSplit(String string) {
ImmutableList.Builder<Substring> result = ImmutableList.builder();
int index = 0;
for (String part : Splitter.on(' ').split(string)) {
result.add(Substring.from(string, index, index + part.length()));
index += part.length() + 1;
}
return result.build();
}
private enum State {
NORMAL,
QUOTE
}
private final Stream.Builder<String> args = Stream.builder();
private final StringBuilder currentArg = new StringBuilder();
private final String input;
private final Stream.Builder<Substring> args = Stream.builder();
private final List<Substring> input;
private final List<Substring> currentArg = new ArrayList<>();
private int index = 0;
private State state = State.NORMAL;
public CommandArgParser(String input) {
public CommandArgParser(List<Substring> input) {
this.input = input;
}
public Stream<String> parseArgs() {
for (; index < input.length(); index++) {
char c = input.charAt(index);
public Stream<Substring> parseArgs() {
for (; index < input.size(); index++) {
Substring nextPart = input.get(index);
switch (state) {
case NORMAL:
handleNormal(c);
handleNormal(nextPart);
break;
case QUOTE:
handleQuote(c);
handleQuote(nextPart);
}
}
finishArg(true);
return args.build();
}
private void handleNormal(char c) {
switch (c) {
case '"':
state = State.QUOTE;
break;
case ' ':
finishArg(true);
break;
case '\\':
if (index + 1 < input.length()) {
index++;
}
appendChar(input.charAt(index));
break;
default:
appendChar(c);
private void handleNormal(Substring part) {
if (part.getSubstring().startsWith("\"")) {
state = State.QUOTE;
currentArg.add(Substring.wrap(
part.getSubstring().substring(1),
part.getStart(), part.getEnd()
));
} else {
currentArg.add(part);
finishArg();
}
}
private void handleQuote(char c) {
switch (c) {
case '"':
state = State.NORMAL;
finishArg(false);
break;
case '\\':
if (index + 1 < input.length()) {
index++;
}
appendChar(input.charAt(index));
break;
default:
appendChar(c);
private void handleQuote(Substring part) {
if (part.getSubstring().endsWith("\"")) {
state = State.NORMAL;
currentArg.add(Substring.wrap(
part.getSubstring().substring(0, part.getSubstring().length() - 1),
part.getStart(), part.getEnd()
));
finishArg();
} else {
currentArg.add(part);
}
}
private void finishArg(boolean requireText) {
if (currentArg.length() == 0 && requireText) {
return;
}
args.add(currentArg.toString());
currentArg.setLength(0);
}
private void appendChar(char c) {
currentArg.append(c);
private void finishArg() {
// Merge the arguments into a single, space-joined, string
// Keep the original start + end points.
int start = currentArg.get(0).getStart();
int end = Iterables.getLast(currentArg).getEnd();
args.add(Substring.wrap(currentArg.stream()
.map(Substring::getSubstring)
.collect(Collectors.joining(" ")),
start, end
));
currentArg.clear();
}
}

Datei anzeigen

@ -19,15 +19,22 @@
package com.sk89q.worldedit.internal.command;
import com.google.common.collect.Iterables;
import com.sk89q.worldedit.extension.platform.PlatformCommandManager;
import com.sk89q.worldedit.internal.util.Substring;
import org.enginehub.piston.Command;
import org.enginehub.piston.part.SubCommandPart;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkState;
import static java.util.stream.Collectors.toList;
public class CommandUtil {
public static Map<String, Command> getSubCommands(Command currentCommand) {
@ -48,6 +55,43 @@ public class CommandUtil {
return BY_CLEAN_NAME;
}
/**
* Fix {@code suggestions} to replace the last space-separated word in {@code arguments}.
*/
public static List<String> fixSuggestions(String arguments, List<Substring> suggestions) {
Substring lastArg = Iterables.getLast(CommandArgParser.spaceSplit(arguments));
return suggestions.stream()
.map(suggestion -> CommandUtil.suggestLast(lastArg, suggestion))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(toList());
}
/**
* Given the last word of a command, mutate the suggestion to replace the last word, if
* possible.
*/
private static Optional<String> suggestLast(Substring last, Substring suggestion) {
if (suggestion.getStart() == last.getEnd()) {
// this suggestion is for the next argument.
if (last.getSubstring().isEmpty()) {
return Optional.of(suggestion.getSubstring());
}
return Optional.of(last.getSubstring() + " " + suggestion.getSubstring());
}
StringBuilder builder = new StringBuilder(last.getSubstring());
int start = suggestion.getStart() - last.getStart();
int end = suggestion.getEnd() - last.getStart();
if (start < 0) {
// Quoted suggestion, can't complete it here.
return Optional.empty();
}
checkState(end <= builder.length(),
"Suggestion ends too late, last=%s, suggestion=", last, suggestion);
builder.replace(start, end, suggestion.getSubstring());
return Optional.of(builder.toString());
}
private CommandUtil() {
}
}

Datei anzeigen

@ -0,0 +1,97 @@
/*
* 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.internal.util;
import java.util.Objects;
/**
* An explicit substring. Provides the range from which it was taken.
*/
public final class Substring {
/**
* Take a substring from {@code original}, and {@link #wrap(String, int, int)} it into
* a Substring.
*/
public static Substring from(String original, int start) {
return wrap(original.substring(start), start, original.length());
}
/**
* Take a substring from {@code original}, and {@link #wrap(String, int, int)} it into
* a Substring.
*/
public static Substring from(String original, int start, int end) {
return wrap(original.substring(start, end), start, end);
}
/**
* Wrap the given parameters into a Substring instance.
*/
public static Substring wrap(String substring, int start, int end) {
return new Substring(substring, start, end);
}
private final String substring;
private final int start;
private final int end;
private Substring(String substring, int start, int end) {
this.substring = substring;
this.start = start;
this.end = end;
}
public String getSubstring() {
return substring;
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Substring substring1 = (Substring) o;
return start == substring1.start &&
end == substring1.end &&
substring.equals(substring1.substring);
}
@Override
public int hashCode() {
return Objects.hash(substring, start, end);
}
@Override
public String toString() {
return "Substring{" +
"substring='" + substring + "'" +
",start=" + start +
",end=" + end +
"}";
}
}

Datei anzeigen

@ -99,7 +99,6 @@ public class PropertiesConfiguration extends LocalConfiguration {
}
superPickaxeDrop = getBool("super-pickaxe-drop-items", superPickaxeDrop);
superPickaxeManyDrop = getBool("super-pickaxe-many-drop-items", superPickaxeManyDrop);
noDoubleSlash = getBool("no-double-slash", noDoubleSlash);
useInventory = getBool("use-inventory", useInventory);
useInventoryOverride = getBool("use-inventory-override", useInventoryOverride);
useInventoryCreativeOverride = getBool("use-inventory-creative-override", useInventoryCreativeOverride);

Datei anzeigen

@ -91,8 +91,6 @@ public class YAMLConfiguration extends LocalConfiguration {
superPickaxeManyDrop = config.getBoolean(
"super-pickaxe.many-drop-items", superPickaxeManyDrop);
noDoubleSlash = config.getBoolean("no-double-slash", noDoubleSlash);
useInventory = config.getBoolean("use-inventory.enable", useInventory);
useInventoryOverride = config.getBoolean("use-inventory.allow-override",
useInventoryOverride);

Datei anzeigen

@ -0,0 +1,51 @@
/*
* 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.internal.command;
import com.google.common.collect.ImmutableList;
import com.sk89q.worldedit.internal.util.Substring;
import org.junit.Test;
import static com.sk89q.worldedit.internal.command.CommandArgParser.spaceSplit;
import static org.junit.Assert.assertEquals;
public class CommandArgParserTest {
@Test
public void testSpaceSplit() {
assertEquals(ImmutableList.of(
Substring.wrap("", 0, 0)
), spaceSplit(""));
assertEquals(ImmutableList.of(
Substring.wrap("ab", 0, 2)
), spaceSplit("ab"));
assertEquals(ImmutableList.of(
Substring.wrap("", 0, 0),
Substring.wrap("", 1, 1)
), spaceSplit(" "));
assertEquals(ImmutableList.of(
Substring.wrap("a", 0, 1),
Substring.wrap("", 2, 2)
), spaceSplit("a "));
assertEquals(ImmutableList.of(
Substring.wrap("a", 0, 1),
Substring.wrap("b", 2, 3)
), spaceSplit("a b"));
}
}

Datei anzeigen

@ -24,7 +24,7 @@ configurations.all { Configuration it ->
dependencies {
compile project(':worldedit-core')
compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.8.1'
compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.11.2'
minecraft "net.minecraftforge:forge:${minecraftVersion}-${forgeVersion}"

Datei anzeigen

@ -90,7 +90,7 @@ configure(subprojects + project("core:ap")) {
def textVersion = "3.0.0"
project("core") {
def pistonVersion = '0.2.0'
def pistonVersion = '0.2.1'
dependencies {
shade "net.kyori:text-api:$textVersion"

Datei anzeigen

@ -29,6 +29,7 @@ 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.internal.command.CommandUtil;
import com.sk89q.worldedit.sponge.config.SpongeConfiguration;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.registry.Registries;
@ -144,7 +145,7 @@ class SpongePlatform extends AbstractPlatform implements MultiUserPlatform {
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.getName() + " " + arguments);
WorldEdit.getInstance().getEventBus().post(weEvent);
return weEvent.getSuggestions();
return CommandUtil.fixSuggestions(arguments, weEvent.getSuggestions());
}
};
ImmutableList.Builder<String> aliases = ImmutableList.builder();

Datei anzeigen

@ -98,8 +98,6 @@ public class ConfigurateConfiguration extends LocalConfiguration {
superPickaxeDrop = node.getNode("super-pickaxe", "drop-items").getBoolean(superPickaxeDrop);
superPickaxeManyDrop = node.getNode("super-pickaxe", "many-drop-items").getBoolean(superPickaxeManyDrop);
noDoubleSlash = node.getNode("no-double-slash").getBoolean(noDoubleSlash);
useInventory = node.getNode("use-inventory", "enable").getBoolean(useInventory);
useInventoryOverride = node.getNode("use-inventory", "allow-override").getBoolean(useInventoryOverride);
useInventoryCreativeOverride = node.getNode("use-inventory", "creative-mode-overrides").getBoolean(useInventoryCreativeOverride);