use of org.spongepowered.api.entity.living.Living in project SpongeForge by SpongePowered.
the class SpongeForgeEventFactory method createPlayerUseItemTickEvent.
public static LivingEntityUseItemEvent.Tick createPlayerUseItemTickEvent(Event event) {
UseItemStackEvent.Tick spongeEvent = (UseItemStackEvent.Tick) event;
Optional<Living> living = spongeEvent.getCause().first(Living.class);
if (!living.isPresent()) {
return null;
}
net.minecraft.item.ItemStack itemstack = (net.minecraft.item.ItemStack) (Object) spongeEvent.getItemStackInUse().createStack();
LivingEntityUseItemEvent.Tick forgeEvent = new LivingEntityUseItemEvent.Tick((EntityLivingBase) living.get(), itemstack, spongeEvent.getRemainingDuration());
return forgeEvent;
}
use of org.spongepowered.api.entity.living.Living in project SpongeForge by SpongePowered.
the class SpongeForgeEventFactory method createPlayerUseItemStartEvent.
public static LivingEntityUseItemEvent.Start createPlayerUseItemStartEvent(Event event) {
UseItemStackEvent.Start spongeEvent = (UseItemStackEvent.Start) event;
Optional<Living> living = spongeEvent.getCause().first(Living.class);
if (!living.isPresent()) {
return null;
}
net.minecraft.item.ItemStack itemstack = (net.minecraft.item.ItemStack) (Object) spongeEvent.getItemStackInUse().createStack();
LivingEntityUseItemEvent.Start forgeEvent = new LivingEntityUseItemEvent.Start((EntityLivingBase) living.get(), itemstack, spongeEvent.getRemainingDuration());
return forgeEvent;
}
use of org.spongepowered.api.entity.living.Living in project SpongeCommon by SpongePowered.
the class LivingEntityMixin method impl$callFinishSleepingEvent.
// End implementation of UseItemStackEvent
@Inject(method = "stopSleeping", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;clearSleepingPos()V"))
private void impl$callFinishSleepingEvent(final CallbackInfo ci) {
if (this.level.isClientSide) {
return;
}
final Optional<BlockPos> sleepingPos = this.shadow$getSleepingPos();
if (!sleepingPos.isPresent()) {
return;
}
final BlockSnapshot snapshot = ((ServerWorld) this.level).createSnapshot(sleepingPos.get().getX(), sleepingPos.get().getY(), sleepingPos.get().getZ());
final Cause currentCause = Sponge.server().causeStackManager().currentCause();
final ServerLocation loc = ServerLocation.of((ServerWorld) this.level, VecHelper.toVector3d(this.shadow$position()));
final Vector3d rot = ((Living) this).rotation();
final SleepingEvent.Finish event = SpongeEventFactory.createSleepingEventFinish(currentCause, loc, loc, rot, rot, snapshot, (Living) this);
Sponge.eventManager().post(event);
this.shadow$clearSleepingPos();
if (event.toLocation().world() != this.level) {
throw new UnsupportedOperationException("World change is not supported here.");
}
this.shadow$setPos(event.toLocation().x(), event.toLocation().y(), event.toLocation().z());
((Living) this).setRotation(event.toRotation());
}
use of org.spongepowered.api.entity.living.Living in project SpongeCommon by SpongePowered.
the class ProjectileTest method registerCommand.
@Listener
public void registerCommand(final RegisterCommandEvent<Command.Parameterized> event) {
final Parameter.Value<EntityType<@NonNull ?>> entityTypeParameter = Parameter.registryElement(new TypeToken<EntityType<?>>() {
}, (ctx) -> Sponge.game(), RegistryTypes.ENTITY_TYPE, "minecraft", "sponge").key("type").build();
final Parameter.Value<Boolean> targetParameter = Parameter.bool().key("target").optional().build();
final Command.Parameterized launchCommand = Command.builder().addParameters(entityTypeParameter, targetParameter).executor(context -> {
final Player player = context.cause().first(Player.class).orElseThrow(() -> new CommandException(Component.text("Only a player can execute this command")));
final EntityType<?> entityType = context.requireOne(entityTypeParameter);
final Optional<Projectile> launched;
if (context.one(targetParameter).orElse(false)) {
final Collection<? extends Entity> nearbyEntities = player.nearbyEntities(10, entity -> entity instanceof Living && entity != player);
if (nearbyEntities.isEmpty()) {
return CommandResult.error(Component.text("No entity to target nearby"));
}
final Entity target = nearbyEntities.iterator().next();
launched = player.launchProjectileTo((EntityType<Projectile>) entityType, target);
if (launched.isPresent()) {
player.sendMessage(Identity.nil(), Component.text("Launched projectile to " + RegistryTypes.ENTITY_TYPE.keyFor(Sponge.game(), target.type()).asString()));
return CommandResult.success();
}
} else {
launched = player.launchProjectile((EntityType<Projectile>) entityType);
if (launched.isPresent()) {
player.sendMessage(Identity.nil(), Component.text("Launched projectile"));
return CommandResult.success();
}
}
throw new CommandException(Component.text("Could not launch projectile"));
}).build();
event.register(this.plugin, launchCommand, "launch");
final Command.Parameterized launchToMeCommand = Command.builder().addParameter(entityTypeParameter).executor(context -> {
final Player player = context.cause().first(Player.class).orElseThrow(() -> new CommandException(Component.text("Only a player can execute this command")));
final Collection<? extends ProjectileSource> nearbyProjectileSources = (Collection<? extends ProjectileSource>) player.nearbyEntities(10, entity -> entity instanceof ProjectileSource);
if (nearbyProjectileSources.isEmpty()) {
return CommandResult.error(Component.text("No projectile source nearby"));
}
final ProjectileSource projectileSource = nearbyProjectileSources.iterator().next();
final EntityType<?> entityType = context.requireOne(entityTypeParameter);
final Optional<? extends Projectile> launched = projectileSource.launchProjectileTo((EntityType<Projectile>) entityType, player);
final EntityType<?> type = ((Entity) projectileSource).type();
if (launched.isPresent()) {
final EntityType<?> launchedType = launched.get().type();
player.sendMessage(Identity.nil(), Component.text().append(Component.text("You made a ")).append(Component.text(RegistryTypes.ENTITY_TYPE.keyFor(Sponge.game(), type).asString())).append(Component.text(" shoot a ")).append(Component.text(RegistryTypes.ENTITY_TYPE.keyFor(Sponge.game(), launchedType).asString())).append(Component.text(" at you")).build());
return CommandResult.success();
}
throw new CommandException(Component.text().append(Component.text("Could not launch a ")).append(Component.text(RegistryTypes.ENTITY_TYPE.keyFor(Sponge.game(), type).asString())).append(Component.text(" from a ")).append(Component.text(RegistryTypes.ENTITY_TYPE.keyFor(Sponge.game(), entityType).asString())).append(Component.text(" at you")).build());
}).build();
event.register(this.plugin, launchToMeCommand, "launchtome");
final Parameter.Value<ServerLocation> dispenserParameter = Parameter.location().key("dispenser").build();
final Command.Parameterized triggerDispenserCommand = Command.builder().addParameters(dispenserParameter, entityTypeParameter).executor(context -> {
final Player player = context.cause().first(Player.class).orElseThrow(() -> new CommandException(Component.text("Only a player can execute this command")));
final BlockEntity dispenser = context.requireOne(dispenserParameter).blockEntity().orElse(null);
if (dispenser == null) {
return CommandResult.error(Component.text("Could not find dispenser"));
}
final EntityType<?> entityType = context.requireOne(entityTypeParameter);
final Optional<? extends Projectile> launched = ((Dispenser) dispenser).launchProjectile((EntityType<Projectile>) entityType);
if (launched.isPresent()) {
launched.get().offer(Keys.SHOOTER, player);
player.sendMessage(Identity.nil(), Component.text().append(Component.text("The dispenser launched a ")).append(Component.text(RegistryTypes.ENTITY_TYPE.keyFor(Sponge.game(), launched.get().type()).asString())).build());
return CommandResult.success();
}
return CommandResult.error(Component.text().append(Component.text("Could not make the dispenser launch a ")).append(Component.text(RegistryTypes.ENTITY_TYPE.keyFor(Sponge.game(), entityType).asString())).build());
}).build();
event.register(this.plugin, triggerDispenserCommand, "triggerdispenser");
}
use of org.spongepowered.api.entity.living.Living in project SpongeCommon by SpongePowered.
the class SpongeTargetBlockValueParameter method parseValue.
@Override
@NonNull
public Optional<? extends ServerLocation> parseValue(@NonNull final CommandCause cause, final ArgumentReader.@NonNull Mutable reader) throws ArgumentParseException {
final Object root = cause.cause().root();
if (root instanceof Living) {
final Living living = (Living) root;
final Optional<RayTraceResult<@NonNull LocatableBlock>> rayTraceResult = RayTrace.block().sourceEyePosition(living).direction(living.headDirection()).limit(30).continueWhileBlock(RayTrace.onlyAir()).select(RayTrace.nonAir()).continueWhileEntity(r -> false).execute();
if (rayTraceResult.isPresent()) {
return rayTraceResult.map(x -> x.selectedObject().serverLocation());
}
throw reader.createException(Component.text("The cause root is not looking at a block!"));
}
throw reader.createException(Component.text("The cause root must be a Living!"));
}
Aggregations