SpongeAPI icon indicating copy to clipboard operation
SpongeAPI copied to clipboard

Allow custom selector filters

Open dualspiral opened this issue 4 years ago • 4 comments

Caveat: I do not know how well this will work with Minecraft client completion... yet.

It would be nice to be able to add selector filters. For example, in a skills plugin, it might be nice to support the following:

@p[skill_woodcutting=35..]

allowing for the selection of the nearest player with a skill level of 35 or over.

As an implementation note: I'd want to do this by registering more objects into the EntityOptions class.

We may also be able to support other selector types, such as @c for cause stack member, for example, but again, that requires more thought and looking into the system. I'm not sure if multi letter selector strings will work though looking at the impl. All for investigation though!

dualspiral avatar Jul 30 '20 19:07 dualspiral

Hey,

I dig up this issue to know if you had any plans about this and to ask if this would be possible to look for the problem I encoutered about scoreboards filters that only rely on the server scoreboard.

Also, I wonder why the Selector class isn't generic. The current implementation is based on the fact every selectors are EntitySelector. For example, I can't build a custom selector @w to select all worlds. Someone asked me this for my plugin, because it can be annoying to setup properties or world settings world by world.

thibaulthenry avatar Feb 05 '21 17:02 thibaulthenry

I dig up this issue to know if you had any plans about this and to ask if this would be possible to look for the problem I encoutered about scoreboards filters that only rely on the server scoreboard.

That doesn't really belong here, if you can do that in Vanilla but not using this system, that's an entirely different issue.

Also, I wonder why the Selector class isn't generic. The current implementation is based on the fact every selectors are EntitySelector.

Which they are - this is exposing something in Vanilla. A selector is for selecting entities, not anything else. If you want to build your own selector for selecting something other than an entity, you need to do it yourself.

dualspiral avatar Feb 05 '21 21:02 dualspiral

That doesn't really belong here, if you can do that in Vanilla but not using this system, that's an entirely different issue.

I may write a new issue for this so. The Vanilla system is completly stuck to the server scoreboard. Thing is in API-8 we are only using this Vanilla system, whereas in API-7 we had method to describe filter behaviors

Even API-7 was stuck with server scoreboard, and I never realized that :

private void addTeamFilters(List<Predicate<Entity>> filters) {
    Optional<Invertible<String>> teamOpt = this.selector.getArgument(ArgumentTypes.TEAM);
    if (teamOpt.isPresent()) {
        Invertible<String> teamArg = teamOpt.get();
        final boolean inverted = teamArg.isInverted();
        filters.add(input -> {
            if (!(input instanceof TeamMember)) return teamArg.getValue().isEmpty() && inverted;

            Optional<Scoreboard> scoreboard = Sponge.getGame().getServer().getServerScoreboard();
            if (!scoreboard.isPresent()) return false;

            Optional<Team> team = scoreboard.get().getMemberTeam(((TeamMember) input).getTeamRepresentation());
            if (teamArg.getValue().isEmpty()) {
                return inverted ^ team.isPresent();
            } else {
                return inverted ^ (team.isPresent() && team.get().getName().equals(teamArg.getValue()));
            }
        });
    }
}

But at least we could have fix this pretty easily. Here I don't see any solution to my problems. There is no concept of "multiple scoreboard" in Vanilla

Which they are - this is exposing something in Vanilla. A selector is for selecting entities, not anything else. If you want to build your own selector for selecting something other than an entity, you need to do it yourself.

I don't completely agree with that. It's true that currently, there is only one type of selector in Minecraft, which is the entity selector. So we can admit that a Selector (Sponge) is an EntitySelector (Minecraft), but the day they decide to implement a new type, you have to break the api :

selector

thibaulthenry avatar Feb 05 '21 21:02 thibaulthenry

What you're asking is pretty much nothing to do with this issue, but I'll explain why that's not something I think we should be doing.

I wonder why the Selector class isn't generic

Almost all of the methods on the selector builder are specific to entities. A generic here adds little to no value and adds some complexity. If a world selector was created, I don't think it'd use any of these filters.

I don't completely agree with that. It's true that currently, there is only one type of selector in Minecraft, which is the entity selector. So we can admit that a Selector (Sponge) is an EntitySelector (Minecraft), but the day they decide to implement a new type, you have to break the api

So we'll break the API. We're not really interested in doing an API 7->8 style break again, but such a thing would be minor enough that we could do it if it made sense to do so (it may very well be that we just crreate a WorldSelector instead).

However, I don't think they will.

To go back on topic slightly:

But at least we could have fix this pretty easily. Here I don't see any solution to my problems. There is no concept of "multiple scoreboard" in Vanilla

That's a new issue really. This issue is about custom (aka plugin provided) selection filters.

dualspiral avatar Feb 06 '21 12:02 dualspiral