use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.
the class AbstractMutableBlockEntityArchetypeBuffer method blockStateStream.
@Override
public VolumeStream<BlockEntityArchetypeVolume.Mutable, BlockState> blockStateStream(final Vector3i min, final Vector3i max, final StreamOptions options) {
VolumeStreamUtils.validateStreamArgs(min, max, this.min(), this.max(), options);
final ArrayMutableBlockBuffer buffer;
if (options.carbonCopy()) {
buffer = this.blockBuffer.copy();
} else {
buffer = this.blockBuffer;
}
final Stream<VolumeElement<BlockEntityArchetypeVolume.Mutable, BlockState>> stateStream = IntStream.range(min.x(), max.x() + 1).mapToObj(x -> IntStream.range(min.z(), max.z() + 1).mapToObj(z -> IntStream.range(min.y(), max.y() + 1).mapToObj(y -> VolumeElement.of((BlockEntityArchetypeVolume.Mutable) this, () -> buffer.block(x, y, z), new Vector3d(x, y, z)))).flatMap(Function.identity())).flatMap(Function.identity());
return new SpongeVolumeStream<>(stateStream, () -> this);
}
use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.
the class LivingData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(LivingEntity.class).create(Keys.ABSORPTION).get(h -> (double) h.getAbsorptionAmount()).setAnd((h, v) -> {
if (v < 0) {
return false;
}
h.setAbsorptionAmount(v.floatValue());
return true;
}).create(Keys.ACTIVE_ITEM).get(h -> ItemStackUtil.snapshotOf(h.getUseItem())).setAnd((h, v) -> {
if (v.isEmpty()) {
h.releaseUsingItem();
return true;
}
return false;
}).delete(LivingEntity::releaseUsingItem).create(Keys.AUTO_SPIN_ATTACK_TICKS).get(h -> Ticks.of(((LivingEntityAccessor) h).accessor$autoSpinAttackTicks())).set((h, v) -> h.startAutoSpinAttack((int) v.ticks())).create(Keys.BODY_ROTATIONS).get(h -> {
final double headYaw = h.getYHeadRot();
final double pitch = h.xRot;
final double yaw = h.yRot;
return ImmutableMap.of(BodyParts.HEAD.get(), new Vector3d(pitch, headYaw, 0), BodyParts.CHEST.get(), new Vector3d(pitch, yaw, 0));
}).set((h, v) -> {
final Vector3d headRotation = v.get(BodyParts.HEAD.get());
final Vector3d bodyRotation = v.get(BodyParts.CHEST.get());
if (bodyRotation != null) {
h.yRot = (float) bodyRotation.y();
h.xRot = (float) bodyRotation.x();
}
if (headRotation != null) {
h.yHeadRot = (float) headRotation.y();
h.xRot = (float) headRotation.x();
}
}).create(Keys.CHEST_ROTATION).get(h -> new Vector3d(h.xRot, h.yRot, 0)).set((h, v) -> {
final float yaw = (float) v.y();
final float pitch = (float) v.x();
h.yRot = yaw;
h.xRot = pitch;
}).create(Keys.HEAD_ROTATION).get(h -> new Vector3d(h.xRot, h.getYHeadRot(), 0)).set((h, v) -> {
final float headYaw = (float) v.y();
final float pitch = (float) v.x();
h.yHeadRot = headYaw;
h.xRot = pitch;
}).create(Keys.HEALTH).get(h -> (double) h.getHealth()).setAnd((h, v) -> {
final double maxHealth = h.getMaxHealth();
// Check bounds
if (v < 0 || v > maxHealth) {
return false;
}
if (v == 0) {
// Cause DestructEntityEvent to fire first
h.hurt((DamageSource) SpongeDamageSources.IGNORED, Float.MAX_VALUE);
}
h.setHealth(v.floatValue());
return true;
}).create(Keys.IS_AUTO_SPIN_ATTACK).get(LivingEntity::isAutoSpinAttack).create(Keys.IS_ELYTRA_FLYING).get(LivingEntity::isFallFlying).set((h, v) -> ((EntityAccessor) h).invoker$setSharedFlag(Constants.Entity.ELYTRA_FLYING_FLAG, v)).create(Keys.LAST_ATTACKER).get(h -> (Entity) h.getLastHurtByMob()).setAnd((h, v) -> {
if (v instanceof LivingEntity) {
h.setLastHurtByMob((LivingEntity) v);
return true;
}
return false;
}).delete(h -> h.setLastHurtByMob(null)).create(Keys.MAX_HEALTH).get(h -> (double) h.getMaxHealth()).set((h, v) -> h.getAttribute(Attributes.MAX_HEALTH).setBaseValue(v)).create(Keys.POTION_EFFECTS).get(h -> {
final Collection<MobEffectInstance> effects = h.getActiveEffects();
return PotionEffectUtil.copyAsPotionEffects(effects);
}).set((h, v) -> {
h.removeAllEffects();
for (final PotionEffect effect : v) {
h.addEffect(PotionEffectUtil.copyAsEffectInstance(effect));
}
}).create(Keys.SCALE).get(h -> (double) h.getScale()).create(Keys.STUCK_ARROWS).get(LivingEntity::getArrowCount).setAnd((h, v) -> {
if (v < 0 || v > Integer.MAX_VALUE) {
return false;
}
h.setArrowCount(v);
return true;
}).create(Keys.WALKING_SPEED).get(h -> h.getAttribute(Attributes.MOVEMENT_SPEED).getValue()).setAnd((h, v) -> {
if (v < 0) {
return false;
}
h.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(v);
return true;
}).asMutable(LivingEntityAccessor.class).create(Keys.LAST_DAMAGE_RECEIVED).get(h -> (double) h.accessor$lastHurt()).set((h, v) -> h.accessor$lastHurt(v.floatValue()));
}
use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.
the class ArmorStandData method apply.
// @formatter:on
private static void apply(final Map<BodyPart, Vector3d> value, final BodyPart part, final Consumer<Rotations> consumer) {
final Vector3d vec = value.get(part);
if (vec == null) {
return;
}
consumer.accept(VecHelper.toRotation(vec));
}
use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.
the class DataUtil method getPosition3d.
public static Vector3d getPosition3d(final DataView view, @Nullable final DataQuery query) {
final DataView internal = query == null ? view : view.getView(query).orElseThrow(DataUtil.dataNotFound());
final double x = internal.getDouble(Queries.POSITION_X).orElseThrow(DataUtil.dataNotFound());
final double y = internal.getDouble(Queries.POSITION_Y).orElseThrow(DataUtil.dataNotFound());
final double z = internal.getDouble(Queries.POSITION_Z).orElseThrow(DataUtil.dataNotFound());
return new Vector3d(x, y, z);
}
use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.
the class FallingBlockMixin method impl$checkFallable.
@Redirect(method = "tick(Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/core/BlockPos;Ljava/util/Random;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/core/BlockPos;getY()I"))
public int impl$checkFallable(final BlockPos pos, final BlockState state, final ServerLevel world, final BlockPos samePos, final Random random, final CallbackInfo ci) {
if (pos.getY() < 0) {
return pos.getY();
}
if (!ShouldFire.CONSTRUCT_ENTITY_EVENT_PRE) {
return pos.getY();
}
final EntityType<org.spongepowered.api.entity.FallingBlock> fallingBlock = EntityTypes.FALLING_BLOCK.get();
final org.spongepowered.api.world.server.ServerWorld spongeWorld = (org.spongepowered.api.world.server.ServerWorld) world;
final BlockSnapshot snapshot = spongeWorld.createSnapshot(pos.getX(), pos.getY(), pos.getZ());
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
frame.pushCause(snapshot);
frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.FALLING_BLOCK);
final ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(frame.currentCause(), ServerLocation.of((org.spongepowered.api.world.server.ServerWorld) world, pos.getX() + 0.5D, pos.getY(), pos.getZ() + 0.5D), new Vector3d(0, 0, 0), fallingBlock);
if (SpongeCommon.post(event)) {
return -1;
}
return pos.getY();
}
}
Aggregations