Search in sources :

Example 1 with InjectedValueStore

use of org.enginehub.piston.inject.InjectedValueStore in project FastAsyncWorldEdit by IntellectualSites.

the class BukkitCommandInspector method testPermission.

@Override
public boolean testPermission(CommandSender sender, Command command) {
    Optional<org.enginehub.piston.Command> mapping = dispatcher.getCommand(command.getName());
    if (mapping.isPresent()) {
        InjectedValueStore store = MapBackedValueStore.create();
        store.injectValue(Key.of(Actor.class), context -> Optional.of(plugin.wrapCommandSender(sender)));
        return mapping.get().getCondition().satisfied(store);
    } else {
        LOGGER.warn("BukkitCommandInspector doesn't know how about the command '" + command + "'");
        return false;
    }
}
Also used : InjectedValueStore(org.enginehub.piston.inject.InjectedValueStore) Command(org.bukkit.command.Command) Actor(com.sk89q.worldedit.extension.platform.Actor)

Example 2 with InjectedValueStore

use of org.enginehub.piston.inject.InjectedValueStore in project FastAsyncWorldEdit by IntellectualSites.

the class PlatformCommandManager method initializeInjectedValues.

// FAWE end
// FAWE start - Event & suggestions, make method public
public MemoizingValueAccess initializeInjectedValues(Arguments arguments, Actor actor, Event event, boolean isSuggestions) {
    // FAWE end
    InjectedValueStore store = MapBackedValueStore.create();
    store.injectValue(Key.of(Actor.class), ValueProvider.constant(actor));
    if (actor instanceof Player) {
        store.injectValue(Key.of(Player.class), ValueProvider.constant((Player) actor));
    } else {
        store.injectValue(Key.of(Player.class), context -> {
            throw new CommandException(Caption.of("worldedit.command.player-only"), ImmutableList.of());
        });
    }
    store.injectValue(Key.of(Arguments.class), ValueProvider.constant(arguments));
    store.injectValue(Key.of(LocalSession.class), context -> {
        LocalSession localSession = worldEdit.getSessionManager().get(actor);
        localSession.tellVersion(actor);
        return Optional.of(localSession);
    });
    store.injectValue(Key.of(boolean.class), context -> Optional.of(isSuggestions));
    store.injectValue(Key.of(InjectedValueStore.class), ValueProvider.constant(store));
    store.injectValue(Key.of(Event.class), ValueProvider.constant(event));
    // FAWE start - allow giving editsessions
    if (event instanceof CommandEvent) {
        EditSession session = ((CommandEvent) event).getSession();
        if (session != null) {
            store.injectValue(Key.of(EditSession.class), context -> Optional.of(session));
        }
    }
    // FAWE end
    return MemoizingValueAccess.wrap(MergedValueAccess.of(store, globalInjectedValues));
}
Also used : InjectedValueStore(org.enginehub.piston.inject.InjectedValueStore) Player(com.sk89q.worldedit.entity.Player) Arguments(com.sk89q.worldedit.command.argument.Arguments) LocalSession(com.sk89q.worldedit.LocalSession) CommandEvent(com.sk89q.worldedit.event.platform.CommandEvent) CommandSuggestionEvent(com.sk89q.worldedit.event.platform.CommandSuggestionEvent) Event(com.sk89q.worldedit.event.Event) CommandEvent(com.sk89q.worldedit.event.platform.CommandEvent) CommandException(org.enginehub.piston.exception.CommandException) EditSession(com.sk89q.worldedit.EditSession)

Example 3 with InjectedValueStore

use of org.enginehub.piston.inject.InjectedValueStore in project FastAsyncWorldEdit by IntellectualSites.

the class WorldEditListener method onPlayerCommandSend.

// FAWE end
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerCommandSend(PlayerCommandSendEvent event) {
    InjectedValueStore store = MapBackedValueStore.create();
    store.injectValue(Key.of(Actor.class), context -> Optional.of(plugin.wrapCommandSender(event.getPlayer())));
    CommandManager commandManager = plugin.getWorldEdit().getPlatformManager().getPlatformCommandManager().getCommandManager();
    event.getCommands().removeIf(name -> commandManager.getCommand(name).filter(command -> !command.getCondition().satisfied(store)).isPresent());
}
Also used : InjectedValueStore(org.enginehub.piston.inject.InjectedValueStore) CommandManager(org.enginehub.piston.CommandManager) Actor(com.sk89q.worldedit.extension.platform.Actor) EventHandler(org.bukkit.event.EventHandler)

Example 4 with InjectedValueStore

use of org.enginehub.piston.inject.InjectedValueStore in project FastAsyncWorldEdit by IntellectualSites.

the class MethodInjector method beforeCall.

@Override
public void beforeCall(Method commandMethod, CommandParameters parameters) {
    InjectedValueStore store = parameters.injectedValue(Key.of(InjectedValueStore.class)).get();
    store.injectValue(Key.of(Method.class), ValueProvider.constant(commandMethod));
}
Also used : InjectedValueStore(org.enginehub.piston.inject.InjectedValueStore) Method(java.lang.reflect.Method)

Example 5 with InjectedValueStore

use of org.enginehub.piston.inject.InjectedValueStore in project FastAsyncWorldEdit by IntellectualSites.

the class PrintCommandHelp method printCommands.

private static void printCommands(int page, Stream<Command> commandStream, Actor actor, List<Command> commandList, String helpRootCommand) throws InvalidComponentException {
    InjectedValueStore store = MapBackedValueStore.create();
    store.injectValue(Key.of(Actor.class), context -> Optional.of(actor));
    // Get a list of aliases
    List<Command> commands = commandStream.filter(command -> command.getCondition().satisfied(store)).sorted(byCleanName()).collect(toList());
    String used = commandList.isEmpty() ? null : toCommandString(commandList);
    CommandListBox box = new CommandListBox((used == null ? "Help" : "Subcommands: " + used), helpRootCommand + " -s -p %page%" + (used == null ? "" : " " + used), helpRootCommand);
    if (!actor.isPlayer()) {
        box.formatForConsole();
    }
    for (Command mapping : commands) {
        String alias = (commandList.isEmpty() ? "/" : "") + mapping.getName();
        String command = Stream.concat(commandList.stream(), Stream.of(mapping)).map(Command::getName).collect(Collectors.joining(" ", "/", ""));
        box.appendCommand(alias, mapping.getDescription(), command);
    }
    actor.print(box.create(page));
}
Also used : InjectedValueStore(org.enginehub.piston.inject.InjectedValueStore) CommandListBox(com.sk89q.worldedit.util.formatting.component.CommandListBox) Command(org.enginehub.piston.Command) Actor(com.sk89q.worldedit.extension.platform.Actor)

Aggregations

InjectedValueStore (org.enginehub.piston.inject.InjectedValueStore)5 Actor (com.sk89q.worldedit.extension.platform.Actor)3 EditSession (com.sk89q.worldedit.EditSession)1 LocalSession (com.sk89q.worldedit.LocalSession)1 Arguments (com.sk89q.worldedit.command.argument.Arguments)1 Player (com.sk89q.worldedit.entity.Player)1 Event (com.sk89q.worldedit.event.Event)1 CommandEvent (com.sk89q.worldedit.event.platform.CommandEvent)1 CommandSuggestionEvent (com.sk89q.worldedit.event.platform.CommandSuggestionEvent)1 CommandListBox (com.sk89q.worldedit.util.formatting.component.CommandListBox)1 Method (java.lang.reflect.Method)1 Command (org.bukkit.command.Command)1 EventHandler (org.bukkit.event.EventHandler)1 Command (org.enginehub.piston.Command)1 CommandManager (org.enginehub.piston.CommandManager)1 CommandException (org.enginehub.piston.exception.CommandException)1