use of com.sk89q.worldedit.command.util.PermissionCondition in project FastAsyncWorldEdit by IntellectualSites.
the class PlatformCommandManager method handleCommandSuggestion.
@Subscribe
public void handleCommandSuggestion(CommandSuggestionEvent event) {
try {
String rawArgs = event.getArguments();
// FAWE start
// Increase the resulting positions by 1 if we remove a leading `/`
final int posOffset = rawArgs.startsWith("/") ? 1 : 0;
String arguments = rawArgs.startsWith("/") ? rawArgs.substring(1) : rawArgs;
// FAWE end
List<Substring> split = parseArgs(arguments).collect(Collectors.toList());
List<String> argStrings = split.stream().map(Substring::getSubstring).collect(Collectors.toList());
// FAWE start - event & suggestion
MemoizingValueAccess access = initializeInjectedValues(() -> arguments, event.getActor(), event, true);
// FAWE end
ImmutableSet<Suggestion> suggestions;
try {
suggestions = commandManager.getSuggestions(access, argStrings);
} catch (Throwable t) {
// catch errors which are *not* command exceptions generated by parsers/suggesters
if (!(t instanceof InputParseException) && !(t instanceof CommandException)) {
Throwable cause = t;
// These exceptions can be very nested, and should not be printed as they are not an actual error.
boolean printError = true;
while ((cause = cause.getCause()) != null) {
if (cause instanceof InputParseException || cause instanceof CommandException) {
printError = false;
break;
}
}
if (printError) {
LOGGER.error("Unexpected error occurred while generating suggestions for input: {}", arguments, t);
return;
}
}
return;
}
event.setSuggestions(suggestions.stream().map(suggestion -> {
int noSlashLength = arguments.length();
Substring original = suggestion.getReplacedArgument() == split.size() ? Substring.from(arguments, noSlashLength, noSlashLength) : split.get(suggestion.getReplacedArgument());
return Substring.wrap(suggestion.getSuggestion(), original.getStart() + posOffset, original.getEnd() + posOffset);
}).collect(Collectors.toList()));
} catch (ConditionFailedException e) {
if (e.getCondition() instanceof PermissionCondition) {
event.setSuggestions(new ArrayList<>());
}
}
}
use of com.sk89q.worldedit.command.util.PermissionCondition in project FastAsyncWorldEdit by IntellectualSites.
the class ExpandCommands method register.
public static void register(CommandRegistrationHandler registration, CommandManager commandManager, CommandManagerService commandManagerService) {
// Collect the general expand command
CommandManager collect = commandManagerService.newCommandManager();
registration.register(collect, ExpandCommandsRegistration.builder(), new ExpandCommands());
Command expandBaseCommand = collect.getCommand("/expand").orElseThrow(() -> new IllegalStateException("No /expand command"));
commandManager.register("/expand", command -> {
command.condition(new PermissionCondition(ImmutableSet.of("worldedit.selection.expand")));
command.addPart(SubCommandPart.builder(Caption.of("vert"), TextComponent.of("Vertical expansion sub-command")).withCommands(ImmutableSet.of(createVertCommand(commandManager))).optional().build());
command.addParts(expandBaseCommand.getParts());
command.action(expandBaseCommand.getAction());
command.description(expandBaseCommand.getDescription());
});
}
use of com.sk89q.worldedit.command.util.PermissionCondition in project FastAsyncWorldEdit by IntellectualSites.
the class PaintBrushCommands method register.
public static void register(CommandManagerService service, CommandManager commandManager, CommandRegistrationHandler registration) {
commandManager.register("paint", builder -> {
builder.description(Caption.of("worldedit.brush.paint.description"));
builder.action(org.enginehub.piston.Command.Action.NULL_ACTION);
CommandManager manager = service.newCommandManager();
registration.register(manager, PaintBrushCommandsRegistration.builder(), new PaintBrushCommands());
builder.condition(new PermissionCondition(ImmutableSet.of("worldedit.brush.paint")));
builder.addParts(REGION_FACTORY, RADIUS, DENSITY);
builder.addPart(SubCommandPart.builder(TranslatableComponent.of("type"), Caption.of("worldedit.brush.paint.type")).withCommands(manager.getAllCommands().collect(Collectors.toList())).required().build());
});
}
use of com.sk89q.worldedit.command.util.PermissionCondition in project FastAsyncWorldEdit by IntellectualSites.
the class ApplyBrushCommands method register.
public static void register(CommandManagerService service, CommandManager commandManager, CommandRegistrationHandler registration) {
commandManager.register("apply", builder -> {
builder.description(Caption.of("worldedit.brush.apply.description"));
builder.action(org.enginehub.piston.Command.Action.NULL_ACTION);
CommandManager manager = service.newCommandManager();
registration.register(manager, ApplyBrushCommandsRegistration.builder(), new ApplyBrushCommands());
builder.condition(new PermissionCondition(ImmutableSet.of("worldedit.brush.apply")));
builder.addParts(REGION_FACTORY, RADIUS);
builder.addPart(SubCommandPart.builder(Caption.of("type"), Caption.of("worldedit.brush.apply.type")).withCommands(manager.getAllCommands().collect(Collectors.toList())).required().build());
});
}
use of com.sk89q.worldedit.command.util.PermissionCondition in project FastAsyncWorldEdit by IntellectualSites.
the class PlatformCommandManager method handleCommandTask.
public void handleCommandTask(ThrowableSupplier<Throwable> task, InjectedValueAccess context, @Nullable LocalSession session, CommandEvent event) {
Actor actor = context.injectedValue(Key.of(Actor.class)).orElseThrow(() -> new IllegalStateException("No player"));
long start = System.currentTimeMillis();
try {
// exceptions and rethrow their converted form, if their is one.
try {
Object result = task.get();
} catch (Throwable t) {
// Use the exception converter to convert the exception if any of its causes
// can be converted, otherwise throw the original exception
Throwable next = t;
do {
exceptionConverter.convert(next);
next = next.getCause();
} while (next != null);
throw t;
}
} catch (ConditionFailedException e) {
if (e.getCondition() instanceof PermissionCondition) {
actor.print(Caption.of("fawe.error.no-perm", StringMan.getString(((PermissionCondition) e.getCondition()).getPermissions())));
} else {
actor.print(e.getRichMessage());
}
} catch (FaweException e) {
actor.print(Caption.of("fawe.cancel.reason", e.getComponent()));
} catch (UsageException e) {
actor.printError(e.getRichMessage());
} catch (CommandExecutionException e) {
handleUnknownException(actor, e.getCause());
} catch (CommandException e) {
if (e.getCause() instanceof FaweException) {
actor.print(Caption.of("fawe.cancel.reason", ((FaweException) e.getCause()).getComponent()));
} else {
Component msg = e.getRichMessage();
if (msg == TextComponent.empty()) {
List<String> argList = parseArgs(event.getArguments()).map(Substring::getSubstring).collect(Collectors.toList());
printUsage(actor, argList);
} else {
actor.printError(msg);
}
}
} catch (Throwable t) {
handleUnknownException(actor, t);
} finally {
if (context instanceof MemoizingValueAccess) {
context = ((MemoizingValueAccess) context).snapshotMemory();
}
Optional<EditSession> editSessionOpt = context.injectedValue(Key.of(EditSession.class));
// Require null CommandEvent#getSession as it means the editsession is being handled somewhere else.
if (editSessionOpt.isPresent() && event.getSession() == null) {
EditSession editSession = editSessionOpt.get();
editSession.close();
session.remember(editSession);
long time = System.currentTimeMillis() - start;
double timeS = (time / 1000.0);
int changed = editSession.getBlockChangeCount();
double throughput = timeS == 0 ? changed : changed / timeS;
if (time > 1000) {
actor.print(Caption.of("worldedit.command.time-elapsed", TextComponent.of(timeS), TextComponent.of(changed), TextComponent.of(Math.round(throughput))));
}
worldEdit.flushBlockBag(actor, editSession);
}
// TODO: Ping @MattBDev to reimplement 2020-02-04
// CFICommands.CFISettings cfi = actor.getMeta("CFISettings");
// if (cfi != null) {
// HeightMapMCAGenerator gen = cfi.getGenerator();
// if (gen != null && gen.isModified()) {
// try {
// gen.update();
// CFIChangeSet set = new CFIChangeSet(gen, actor.getUniqueId());
// session.remember(actor, gen, set, actor.getLimit());
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
Request.reset();
}
event.setCancelled(true);
}
Aggregations