CommandContext + Confirmation

Use InjectedValueAccess instead
Fix command confirmation
This commit is contained in:
Jesse Boyd
2019-07-19 06:03:27 +10:00
parent 81298bf555
commit f500b5e056
92 changed files with 163 additions and 7137 deletions

View File

@ -1,63 +0,0 @@
/*
* 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 com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.util.command.argument.CommandArgs;
import com.sk89q.worldedit.util.command.composition.CommandExecutor;
import java.util.Collections;
import java.util.List;
public class BooleanFlag implements CommandExecutor<Boolean> {
private final String description;
public BooleanFlag(String description) {
this.description = description;
}
@Override
public Boolean call(CommandArgs args, CommandLocals locals) throws CommandException {
return true;
}
@Override
public List<String> getSuggestions(CommandArgs args, CommandLocals locals) {
return Collections.emptyList();
}
@Override
public String getUsage() {
return "";
}
@Override
public String getDescription() {
return description;
}
@Override
public boolean testPermission(CommandLocals locals) {
return true;
}
}

View File

@ -1,82 +0,0 @@
/*
* 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 com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.util.command.argument.CommandArgs;
import com.sk89q.worldedit.util.command.composition.SimpleCommand;
import com.sk89q.worldedit.world.World;
public class ItemParser extends SimpleCommand<BaseItem> {
private final StringParser stringParser;
public ItemParser(String name) {
stringParser = addParameter(new StringParser(name, "The item name", null));
}
public ItemParser(String name, String defaultSuggestion) {
stringParser = addParameter(new StringParser(name, "The item name", defaultSuggestion));
}
@Override
public BaseItem call(CommandArgs args, CommandLocals locals) throws CommandException {
String itemString = stringParser.call(args, locals);
Actor actor = locals.get(Actor.class);
LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor);
ParserContext parserContext = new ParserContext();
parserContext.setActor(actor);
if (actor instanceof Entity) {
Extent extent = ((Entity) actor).getExtent();
if (extent instanceof World) {
parserContext.setWorld((World) extent);
}
}
parserContext.setSession(session);
try {
return WorldEdit.getInstance().getItemFactory().parseFromInput(itemString, parserContext);
} catch (InputParseException e) {
throw new CommandException(e.getMessage(), e);
}
}
@Override
public String getDescription() {
return "Match an item";
}
@Override
protected boolean testPermission0(CommandLocals locals) {
return true;
}
}

View File

@ -1,90 +0,0 @@
/*
* 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 com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.function.Contextual;
import com.sk89q.worldedit.function.EditContext;
import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.util.command.argument.CommandArgs;
import com.sk89q.worldedit.util.command.composition.SimpleCommand;
import com.sk89q.worldedit.world.World;
public class ItemUseParser extends SimpleCommand<Contextual<RegionFunction>> {
private final ItemParser itemParser = addParameter(new ItemParser("item", "minecraft:bone_meal"));
@Override
public Contextual<RegionFunction> call(CommandArgs args, CommandLocals locals) throws CommandException {
BaseItem item = itemParser.call(args, locals);
return new ItemUseFactory(item);
}
@Override
public String getDescription() {
return "Applies an item";
}
@Override
protected boolean testPermission0(CommandLocals locals) {
return true;
}
private static final class ItemUseFactory implements Contextual<RegionFunction> {
private final BaseItem item;
private ItemUseFactory(BaseItem item) {
this.item = item;
}
@Override
public RegionFunction createFromContext(EditContext input) {
World world = ((EditSession) input.getDestination()).getWorld();
return new ItemUseFunction(world, item);
}
@Override
public String toString() {
return "application of the item " + item.getType() + ":" + item.getNbtData();
}
}
private static final class ItemUseFunction implements RegionFunction {
private final World world;
private final BaseItem item;
private ItemUseFunction(World world, BaseItem item) {
this.world = world;
this.item = item;
}
@Override
public boolean apply(BlockVector3 position) throws WorldEditException {
return world.useItem(position, item, Direction.UP);
}
}
}

View File

@ -1,83 +0,0 @@
/*
* 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 com.google.common.collect.Lists;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.util.command.argument.CommandArgs;
import com.sk89q.worldedit.util.command.argument.MissingArgumentException;
import com.sk89q.worldedit.util.command.composition.CommandExecutor;
import java.util.Collections;
import java.util.List;
public class NumberParser implements CommandExecutor<Number> {
private final String name;
private final String description;
private final String defaultSuggestion;
public NumberParser(String name, String description) {
this(name, description, null);
}
public NumberParser(String name, String description, String defaultSuggestion) {
this.name = name;
this.description = description;
this.defaultSuggestion = defaultSuggestion;
}
@Override
public Number call(CommandArgs args, CommandLocals locals) throws CommandException {
try {
String next = args.next();
try {
return Double.parseDouble(next);
} catch (NumberFormatException ignored) {
throw new CommandException("The value for <" + name + "> should be a number. '" + next + "' is not a number.");
}
} catch (MissingArgumentException e) {
throw new CommandException("Missing value for <" + name + "> (try a number).");
}
}
@Override
public List<String> getSuggestions(CommandArgs args, CommandLocals locals) throws MissingArgumentException {
String value = args.next();
return value.isEmpty() && defaultSuggestion != null ? Lists.newArrayList(defaultSuggestion) : Collections.emptyList();
}
@Override
public String getUsage() {
return "<" + name + ">";
}
@Override
public String getDescription() {
return description;
}
@Override
public boolean testPermission(CommandLocals locals) {
return true;
}
}

View File

@ -1,78 +0,0 @@
/*
* 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 com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.util.command.argument.CommandArgs;
import com.sk89q.worldedit.util.command.composition.SimpleCommand;
import com.sk89q.worldedit.world.World;
public class PatternParser extends SimpleCommand<Pattern> {
private final StringParser stringParser;
public PatternParser(String name) {
stringParser = addParameter(new StringParser(name, "The pattern"));
}
@Override
public Pattern call(CommandArgs args, CommandLocals locals) throws CommandException {
String patternString = stringParser.call(args, locals);
Actor actor = locals.get(Actor.class);
LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor);
ParserContext parserContext = new ParserContext();
parserContext.setActor(actor);
if (actor instanceof Entity) {
Extent extent = ((Entity) actor).getExtent();
if (extent instanceof World) {
parserContext.setWorld((World) extent);
}
}
parserContext.setSession(session);
try {
return WorldEdit.getInstance().getPatternFactory().parseFromInput(patternString, parserContext);
} catch (InputParseException e) {
throw new CommandException(e.getMessage(), e);
}
}
@Override
public String getDescription() {
return "Choose a pattern";
}
@Override
public boolean testPermission0(CommandLocals locals) {
return true;
}
}

View File

@ -1,81 +0,0 @@
/*
* 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 com.google.common.collect.Lists;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.regions.factory.CuboidRegionFactory;
import com.sk89q.worldedit.regions.factory.CylinderRegionFactory;
import com.sk89q.worldedit.regions.factory.RegionFactory;
import com.sk89q.worldedit.regions.factory.SphereRegionFactory;
import com.sk89q.worldedit.util.command.argument.ArgumentUtils;
import com.sk89q.worldedit.util.command.argument.CommandArgs;
import com.sk89q.worldedit.util.command.argument.MissingArgumentException;
import com.sk89q.worldedit.util.command.composition.CommandExecutor;
import java.util.List;
public class RegionFactoryParser implements CommandExecutor<RegionFactory> {
@Override
public RegionFactory call(CommandArgs args, CommandLocals locals) throws CommandException {
try {
String type = args.next();
switch (type) {
case "cuboid":
return new CuboidRegionFactory();
case "sphere":
return new SphereRegionFactory();
case "cyl":
case "cylinder":
return new CylinderRegionFactory(1); // TODO: Adjustable height
default:
throw new CommandException("Unknown shape type: " + type + " (try one of " + getUsage() + ")");
}
} catch (MissingArgumentException e) {
throw new CommandException("Missing shape type (try one of " + getUsage() + ")");
}
}
@Override
public List<String> getSuggestions(CommandArgs args, CommandLocals locals) throws MissingArgumentException {
return ArgumentUtils.getMatchingSuggestions(Lists.newArrayList("cuboid", "sphere", "cyl"), args.next());
}
@Override
public String getUsage() {
return "(cuboid | sphere | cyl)";
}
@Override
public String getDescription() {
return "Defines a region";
}
@Override
public boolean testPermission(CommandLocals locals) {
return true;
}
}

View File

@ -1,40 +0,0 @@
/*
* 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 com.sk89q.worldedit.function.Contextual;
import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.util.command.composition.BranchingCommand;
public class RegionFunctionParser extends BranchingCommand<Contextual<? extends RegionFunction>> {
public RegionFunctionParser() {
super("functionTpe");
putOption(new TreeGeneratorParser("treeType"), "forest");
putOption(new ItemUseParser(), "item");
putOption(new ReplaceParser(), "set");
}
@Override
public String getDescription() {
return "Choose a block function";
}
}

View File

@ -1,76 +0,0 @@
/*
* 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 static com.sk89q.worldedit.util.GuavaUtil.firstNonNull;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.extent.NullExtent;
import com.sk89q.worldedit.function.Contextual;
import com.sk89q.worldedit.function.EditContext;
import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.function.block.BlockReplace;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.util.command.argument.CommandArgs;
import com.sk89q.worldedit.util.command.composition.SimpleCommand;
public class ReplaceParser extends SimpleCommand<Contextual<? extends RegionFunction>> {
private final PatternParser fillArg = addParameter(new PatternParser("fillPattern"));
@Override
public Contextual<RegionFunction> call(CommandArgs args, CommandLocals locals) throws CommandException {
Pattern fill = fillArg.call(args, locals);
return new ReplaceFactory(fill);
}
@Override
public String getDescription() {
return "Replaces blocks";
}
@Override
protected boolean testPermission0(CommandLocals locals) {
return true;
}
private static class ReplaceFactory implements Contextual<RegionFunction> {
private final Pattern fill;
private ReplaceFactory(Pattern fill) {
this.fill = fill;
}
@Override
public RegionFunction createFromContext(EditContext context) {
return new BlockReplace(
firstNonNull(context.getDestination(), new NullExtent()),
firstNonNull(context.getFill(), fill));
}
@Override
public String toString() {
return "replace blocks";
}
}
}

View File

@ -1,78 +0,0 @@
/*
* 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 com.google.common.collect.Lists;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.util.command.argument.CommandArgs;
import com.sk89q.worldedit.util.command.argument.MissingArgumentException;
import com.sk89q.worldedit.util.command.composition.CommandExecutor;
import java.util.Collections;
import java.util.List;
public class StringParser implements CommandExecutor<String> {
private final String name;
private final String description;
private final String defaultSuggestion;
public StringParser(String name, String description) {
this(name, description, null);
}
public StringParser(String name, String description, String defaultSuggestion) {
this.name = name;
this.description = description;
this.defaultSuggestion = defaultSuggestion;
}
@Override
public String call(CommandArgs args, CommandLocals locals) throws CommandException {
try {
return args.next();
} catch (MissingArgumentException e) {
throw new CommandException("Missing value for <" + name + ">.");
}
}
@Override
public List<String> getSuggestions(CommandArgs args, CommandLocals locals) throws MissingArgumentException {
String value = args.next();
return value.isEmpty() && defaultSuggestion != null ? Lists.newArrayList(defaultSuggestion) : Collections.emptyList();
}
@Override
public String getUsage() {
return "<" + name + ">";
}
@Override
public String getDescription() {
return description;
}
@Override
public boolean testPermission(CommandLocals locals) {
return true;
}
}

View File

@ -1,6 +0,0 @@
package com.sk89q.worldedit.command.argument;
import org.enginehub.piston.converter.ArgumentConverter;
public class StringValidator implements ArgumentConverter<String> {
}

View File

@ -1,106 +0,0 @@
/*
* 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 com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.function.Contextual;
import com.sk89q.worldedit.function.EditContext;
import com.sk89q.worldedit.function.generator.ForestGenerator;
import com.sk89q.worldedit.util.TreeGenerator;
import com.sk89q.worldedit.util.TreeGenerator.TreeType;
import com.sk89q.worldedit.util.command.argument.ArgumentUtils;
import com.sk89q.worldedit.util.command.argument.CommandArgs;
import com.sk89q.worldedit.util.command.argument.MissingArgumentException;
import com.sk89q.worldedit.util.command.composition.CommandExecutor;
import java.util.Arrays;
import java.util.List;
public class TreeGeneratorParser implements CommandExecutor<Contextual<ForestGenerator>> {
private final String name;
public TreeGeneratorParser(String name) {
this.name = name;
}
private String getOptionsList() {
return Joiner.on(" | ").join(Arrays.asList(TreeType.values()));
}
@Override
public Contextual<ForestGenerator> call(CommandArgs args, CommandLocals locals) throws CommandException {
try {
String input = args.next();
TreeType type = TreeGenerator.lookup(input);
if (type != null) {
return new GeneratorFactory(type);
} else {
throw new CommandException("Unknown value for <" + name + "> (try one of " + getOptionsList() + ").");
}
} catch (MissingArgumentException e) {
throw new CommandException("Missing value for <" + name + "> (try one of " + getOptionsList() + ").");
}
}
@Override
public List<String> getSuggestions(CommandArgs args, CommandLocals locals) throws MissingArgumentException {
String s = args.next();
return s.isEmpty() ? Lists.newArrayList(TreeType.getPrimaryAliases()) : ArgumentUtils.getMatchingSuggestions(TreeType.getAliases(), s);
}
@Override
public String getUsage() {
return "<" + name + ">";
}
@Override
public String getDescription() {
return "Choose a tree generator";
}
@Override
public boolean testPermission(CommandLocals locals) {
return true;
}
private static final class GeneratorFactory implements Contextual<ForestGenerator> {
private final TreeType type;
private GeneratorFactory(TreeType type) {
this.type = type;
}
@Override
public ForestGenerator createFromContext(EditContext input) {
return new ForestGenerator((EditSession) input.getDestination(), type);
}
@Override
public String toString() {
return "tree of type " + type;
}
}
}