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");
}
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);
}
Aggregations