Search in sources :

Example 1 with VanishState

use of org.spongepowered.api.effect.VanishState in project SpongeCommon by SpongePowered.

the class VanishTest method onRegisterSpongeCommand.

@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();
    final Flag soundFlag = Flag.builder().setParameter(entityKey).alias("s").build();
    final Flag particlesFlag = Flag.builder().setParameter(entityKey).alias("p").build();
    event.register(this.plugin, Command.builder().addParameter(entityKey).addFlags(soundFlag, particlesFlag, collisionsFlag, targetingFlag).extendedDescription(Component.text("Vanishes the entities by the selector. Based on the flags -c and -t will enable collisions or targeting by entities. No entities selected will target the sender.")).executor(ctx -> {
        final Collection<? extends Entity> allEntities = ctx.all(entityKey);
        final VanishState vanishState = VanishState.vanished().untargetable(!ctx.hasFlag(targetingFlag)).ignoreCollisions(!ctx.hasFlag(collisionsFlag)).createParticles(ctx.hasFlag(particlesFlag)).createSounds(ctx.hasFlag(soundFlag));
        allEntities.forEach(entity -> entity.offer(Keys.VANISH_STATE, vanishState));
        if (allEntities.isEmpty()) {
            if (ctx.cause().root() instanceof DataHolder.Mutable) {
                ((DataHolder.Mutable) ctx.cause().root()).offer(Keys.VANISH_STATE, vanishState);
                ctx.sendMessage(Identity.nil(), Component.text("Vanished!", NamedTextColor.DARK_AQUA));
            } else {
                ctx.sendMessage(Identity.nil(), Component.text("Nothing to Vanish!", NamedTextColor.YELLOW));
            }
        } else {
            ctx.sendMessage(Identity.nil(), Component.text("Vanished!", NamedTextColor.DARK_AQUA));
        }
        return CommandResult.success();
    }).build(), "vanish", "v");
    event.register(this.plugin, Command.builder().addParameter(entityKey).executor(ctx -> {
        final Collection<? extends Entity> allEntities = ctx.all(entityKey);
        allEntities.forEach(entity -> entity.offer(Keys.VANISH_STATE, VanishState.unvanished()));
        if (allEntities.isEmpty()) {
            if (ctx.cause().root() instanceof DataHolder.Mutable) {
                ((DataHolder.Mutable) ctx.cause().root()).offer(Keys.VANISH_STATE, VanishState.unvanished());
                ctx.sendMessage(Identity.nil(), Component.text("Unvanished! Huzzah!", NamedTextColor.AQUA));
            } else {
                ctx.sendMessage(Identity.nil(), Component.text("Nothing to Reveal!", NamedTextColor.YELLOW));
            }
        } else {
            ctx.sendMessage(Identity.nil(), Component.text("Unvanished! Huzzah!", NamedTextColor.AQUA));
        }
        return CommandResult.success();
    }).build(), "unvanish", "uv");
}
Also used : Entity(org.spongepowered.api.entity.Entity) VanishState(org.spongepowered.api.effect.VanishState) Parameter(org.spongepowered.api.command.parameter.Parameter) Flag(org.spongepowered.api.command.parameter.managed.Flag) Listener(org.spongepowered.api.event.Listener)

Example 2 with VanishState

use of org.spongepowered.api.effect.VanishState in project SpongeCommon by SpongePowered.

the class VanishableData method register.

// @formatter:off
@SuppressWarnings("unchecked")
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(VanishableBridge.class).create(Keys.IS_INVISIBLE).get(VanishableBridge::bridge$isInvisible).set(VanishableBridge::bridge$setInvisible).create(Keys.VANISH).get(bridge -> bridge.bridge$vanishState().invisible()).setAnd((h, v) -> {
        if (h instanceof Entity && ((Entity) h).level.isClientSide) {
            return false;
        }
        h.bridge$vanishState(v ? VanishState.vanished() : VanishState.unvanished());
        return true;
    }).create(Keys.VANISH_IGNORES_COLLISION).get(b -> b.bridge$vanishState().ignoresCollisions()).setAnd((h, v) -> {
        if (h instanceof Entity && ((Entity) h).level.isClientSide) {
            return false;
        }
        if (!h.bridge$vanishState().invisible()) {
            return false;
        }
        h.bridge$vanishState(h.bridge$vanishState().ignoreCollisions(v));
        return true;
    }).create(Keys.VANISH_PREVENTS_TARGETING).get(b -> b.bridge$vanishState().untargetable()).setAnd((h, v) -> {
        if (h instanceof Entity && ((Entity) h).level.isClientSide) {
            return false;
        }
        if (!h.bridge$vanishState().invisible()) {
            return false;
        }
        h.bridge$vanishState(h.bridge$vanishState().untargetable(v));
        return true;
    }).create(Keys.VANISH_STATE).get(VanishableBridge::bridge$vanishState).setAnd((h, v) -> {
        if (h instanceof Entity && ((Entity) h).level.isClientSide) {
            return false;
        }
        h.bridge$vanishState(v);
        return true;
    });
    final ResourceKey dataStoreKey = ResourceKey.sponge("invisibility");
    registrator.spongeDataStore(dataStoreKey, VanishableBridge.class, Keys.IS_INVISIBLE, Keys.VANISH_STATE);
    SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.IS_INVISIBLE, dataStoreKey, Keys.IS_INVISIBLE);
    SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.IS_VANISHED, dataStoreKey, Keys.VANISH_STATE);
    SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.VANISH_UNCOLLIDEABLE, dataStoreKey, Keys.VANISH_IGNORES_COLLISION);
    SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.VANISH_UNTARGETABLE, dataStoreKey, Keys.VANISH_PREVENTS_TARGETING);
}
Also used : VanishState(org.spongepowered.api.effect.VanishState) Keys(org.spongepowered.api.data.Keys) VanishableBridge(org.spongepowered.common.bridge.data.VanishableBridge) Entity(net.minecraft.world.entity.Entity) Constants(org.spongepowered.common.util.Constants) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) ResourceKey(org.spongepowered.api.ResourceKey) SpongeDataManager(org.spongepowered.common.data.SpongeDataManager) Entity(net.minecraft.world.entity.Entity) VanishableBridge(org.spongepowered.common.bridge.data.VanishableBridge) ResourceKey(org.spongepowered.api.ResourceKey)

Aggregations

VanishState (org.spongepowered.api.effect.VanishState)2 Entity (net.minecraft.world.entity.Entity)1 ResourceKey (org.spongepowered.api.ResourceKey)1 Parameter (org.spongepowered.api.command.parameter.Parameter)1 Flag (org.spongepowered.api.command.parameter.managed.Flag)1 Keys (org.spongepowered.api.data.Keys)1 Entity (org.spongepowered.api.entity.Entity)1 Listener (org.spongepowered.api.event.Listener)1 VanishableBridge (org.spongepowered.common.bridge.data.VanishableBridge)1 SpongeDataManager (org.spongepowered.common.data.SpongeDataManager)1 DataProviderRegistrator (org.spongepowered.common.data.provider.DataProviderRegistrator)1 Constants (org.spongepowered.common.util.Constants)1