EngineHub / Intake

IoC-oriented Java command parsing library
GNU Lesser General Public License v3.0
98 stars 21 forks source link

Annotation that marks provider to consume #7

Closed AustinLMayes closed 9 years ago

AustinLMayes commented 9 years ago

I have two provider classes (since I couldn't find a way to mark the other as consuming at runtime.)

    protected void configure() {
        bind(CommandSender.class).annotatedWith(Sender.class).toProvider(new CommandSenderProvidedProvider());
        bind(CommandSender.class).toProvider(new CommandSenderProvider());
    }
    @Command(aliases = "test", desc = "test")
    public void test(@Sender CommandSender sender) {
        sender.sendMessage("HELLO");
    }

The problem is, when I run the command with no args, I get an error saying I need more arguments. I have isProvided to true in the CommandSenderProvidedProvider, but the binding seems to ignore the annotation and just use the base binder.

Am I doing something wrong, or is something broken?

sk89q commented 9 years ago

Does @Sender have @Retention and @Classifier on them?

AustinLMayes commented 9 years ago

@sk89q

@Classifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER, ElementType.FIELD })
public @interface Sender {
}
sk89q commented 9 years ago

Could you post the code for your providers?

AustinLMayes commented 9 years ago

Here are the providers

package me.austinlm.aplugin.utils.intake.ioc.commandsender;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.sk89q.intake.argument.ArgumentException;
import com.sk89q.intake.argument.CommandArgs;
import com.sk89q.intake.parametric.Provider;
import com.sk89q.intake.parametric.ProvisionException;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import javax.annotation.Nullable;
import java.lang.annotation.Annotation;
import java.util.List;

public class CommandSenderProvidedProvider implements Provider<CommandSender> {

    public CommandSenderProvidedProvider() {
    }

    @Override
    public boolean isProvided() {
        return true;
    }

    @Nullable
    @Override
    public CommandSender get(CommandArgs arguments, List<? extends Annotation> modifiers) throws ArgumentException, ProvisionException {
        return arguments.getNamespace().get(CommandSender.class);
    }

    @Override
    public List<String> getSuggestions(String prefix) {
        List<String> suggestions = Lists.newArrayList();
        suggestions.add("console");
        for (Player p : Bukkit.getOnlinePlayers()) {
            suggestions.add(p.getName());
        }
        return ImmutableList.copyOf(suggestions);
    }
}
package me.austinlm.aplugin.utils.intake.ioc.commandsender;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.sk89q.intake.argument.ArgumentException;
import com.sk89q.intake.argument.ArgumentParseException;
import com.sk89q.intake.argument.CommandArgs;
import com.sk89q.intake.parametric.Provider;
import com.sk89q.intake.parametric.ProvisionException;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import javax.annotation.Nullable;
import java.lang.annotation.Annotation;
import java.util.List;

public class CommandSenderProvider implements Provider<CommandSender> {

    public CommandSenderProvider() {
    }

    @Override
    public boolean isProvided() {
        return false;
    }

    @Nullable
    @Override
    public CommandSender get(CommandArgs arguments, List<? extends Annotation> modifiers) throws ArgumentException, ProvisionException {
        String name = arguments.next();
        CommandSender result;
        if (name.equalsIgnoreCase("console")) {
            result = Bukkit.getConsoleSender();
        } else result = Bukkit.getPlayer(name);
        if (result != null) {
            return result;
        } else {
            throw new ArgumentParseException("Could not find the specified player. NOTE: To reference the console, use 'console'.");
        }
    }

    @Override
    public List<String> getSuggestions(String prefix) {
        List<String> suggestions = Lists.newArrayList();
        suggestions.add("console");
        for (Player p : Bukkit.getOnlinePlayers()) {
            suggestions.add(p.getName());
        }
        return ImmutableList.copyOf(suggestions);
    }
}
sk89q commented 9 years ago

I can't see an issue.

Are you able to get the stack trace for MissingArgumentException? It should show me where it's being triggered.

AustinLMayes commented 9 years ago
>test
[22:41:58 INFO]: Too few arguments! No value found for parameter 'sender'
[22:41:58 INFO]: /test com.sk89q.intake.ImmutableParameter@49c5d897
[22:41:58 WARN]: com.sk89q.intake.InvalidUsageException: Too few arguments! No value found for parameter 'sender'
[22:41:58 WARN]:    at com.sk89q.intake.parametric.AbstractParametricCallable.call(AbstractParametricCallable.java:216)
[22:41:58 WARN]:    at com.sk89q.intake.dispatcher.SimpleDispatcher.call(SimpleDispatcher.java:138)
[22:41:58 WARN]:    at me.austinlm.aplugin.APlugin.onCommand(APlugin.java:62)
[22:41:58 WARN]:    at me.austinlm.aplugin.utils.intake.registration.DynamicBukkitCommand.execute(DynamicBukkitCommand.java:21)
[22:41:58 WARN]:    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
[22:41:58 WARN]:    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:642)
[22:41:58 WARN]:    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:628)
[22:41:58 WARN]:    at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:404)
[22:41:58 WARN]:    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:368)
[22:41:58 WARN]:    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657)
[22:41:58 WARN]:    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560)
[22:41:58 WARN]:    at java.lang.Thread.run(Thread.java:724)
sk89q commented 9 years ago

Oh geeze... it looks like AbstractParametricCallable eats the original exception. I'm going to have to think of something else.

sk89q commented 9 years ago

Are you able to update your Intake dep to the latest snapshot build? It shouldn't eat exceptions anymore.

AustinLMayes commented 9 years ago

@sk89q Now it just works.... Building from the latest revision instead of the version in the maven repo made it work. Any ideas why this could happen?

sk89q commented 9 years ago

Classifiers were broken prior to commit bd571f0d776202fa32b4642cb93c0229a6cdeab2

Something might be caching old artifact data on my Maven repo.

keir-nellyer commented 9 years ago

Sorry for necro-posting but I had the same issue as @AustinLMayes. Turns out it was due to a sneaky namespace change due to modularisation of the project on the maven repo.

Intake was originally in the group "org.sk89q" as seen here but now it's in "org.sk89q.intake" as seen here.

Just for future reference.