Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vanish API 2.0 #2405

Merged
merged 1 commit into from
Jan 8, 2022
Merged

Vanish API 2.0 #2405

merged 1 commit into from
Jan 8, 2022

Conversation

gabizou
Copy link
Member

@gabizou gabizou commented Jan 2, 2022

SpongeAPI | Sponge

This introduces a new form of Vanish as an API. With 1.17+ adding more possible customization chances for Vanish, I felt it necessary to introduce a possible option for how vanish can be customized further out of the box. In hindsight, the added Keys just made for worse experience using the API, so I bring VanishState, which has some relatively simple methods by itself, and it is in itself immutable.

The intended use is simply:

// Vanish entirely
player.offer(Keys.VANISH_STATE, VanishState.vanished());

// Vanish with collisions
player.offer(Keys.VANISH_STATE, VanishState.vanished().ignoreCollisions(false));

// Vanish with targeting
player.offer(Keys.VANISH_STATE, VanishState.vanished().untargetable(false));

// Unvanish (visible)
player.offer(Keys.VANISH_STATE, VanishState.unvanished());

And pulling from the VanishTest test plugin:

@Plugin("vanish-test")
public class VanishTest {

    private final PluginContainer plugin;

    @Inject
    public VanishTest(PluginContainer plugin) {
        this.plugin = plugin;
    }

    @Listener
    public void onRegisterSpongeCommand(final RegisterCommandEvent<Command.Parameterized> event) {
        final Parameter.Value<Entity> entityKey = Parameter.entity()
            .key("entity")
            .usage(key -> "any entity")
            .optional()
            .build();
        final Flag collisionsFlag = Flag.builder()
            .setParameter(entityKey)
            .alias("c")
            .build();
        final Flag targetingFlag = Flag.builder()
            .setParameter(entityKey)
            .alias("t")
            .build();
        event.register(
            this.plugin,
            Command.builder()
                .addParameter(entityKey)
                .addFlag(collisionsFlag)
                .addFlag(targetingFlag)
                .executor(ctx -> {
                    ctx.all(entityKey).forEach(entity -> entity.offer(Keys.VANISH_STATE, VanishState.vanished()
                        .untargetable(ctx.hasFlag(targetingFlag))
                        .ignoreCollisions(ctx.hasFlag(collisionsFlag))));
                    ctx.sendMessage(Identity.nil(), Component.text("Vanished!", NamedTextColor.DARK_AQUA));
                    return CommandResult.success();
                })
                .build(),
            "vanish"
        );
        event.register(
            this.plugin,
            Command.builder()
                .addParameter(entityKey)
                .executor(ctx -> {
                    ctx.all(entityKey).forEach(entity -> entity.offer(Keys.VANISH_STATE, VanishState.unvanished()));
                    ctx.sendMessage(Identity.nil(), Component.text("Unvanished! Huzzah!", NamedTextColor.AQUA));
                    return CommandResult.success();
                })
                .build(),
            "unvanish"
        );
    }

}

@gabizou gabizou force-pushed the api8/vanish-state branch 3 times, most recently from 9288a03 to 53ad421 Compare January 3, 2022 03:37
@gabizou gabizou force-pushed the api8/vanish-state branch 2 times, most recently from c7ce027 to b12734b Compare January 4, 2022 01:39
@gabizou gabizou requested a review from dualspiral January 4, 2022 01:39
@gabizou gabizou force-pushed the api8/vanish-state branch from b12734b to 85f6534 Compare January 7, 2022 23:44
This state keeps track of the extra options and enables a
simpler usage of 'simple vanish' vs customizations of vanish.

Signed-off-by: Gabriel Harris-Rouquette <gabizou@me.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants