Search in sources :

Example 16 with Vector3d

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);
}
Also used : IntStream(java.util.stream.IntStream) AbstractBlockBuffer(org.spongepowered.common.world.volume.buffer.block.AbstractBlockBuffer) VolumeStream(org.spongepowered.api.world.volume.stream.VolumeStream) FluidState(org.spongepowered.api.fluid.FluidState) BlockEntityArchetypeVolume(org.spongepowered.api.world.volume.archetype.block.entity.BlockEntityArchetypeVolume) ArrayMutableBlockBuffer(org.spongepowered.common.world.volume.buffer.block.ArrayMutableBlockBuffer) StreamOptions(org.spongepowered.api.world.volume.stream.StreamOptions) VolumeElement(org.spongepowered.api.world.volume.stream.VolumeElement) Palette(org.spongepowered.api.world.schematic.Palette) Function(java.util.function.Function) BlockState(org.spongepowered.api.block.BlockState) Stream(java.util.stream.Stream) Vector3d(org.spongepowered.math.vector.Vector3d) VolumeStreamUtils(org.spongepowered.common.world.volume.VolumeStreamUtils) BlockType(org.spongepowered.api.block.BlockType) SpongeVolumeStream(org.spongepowered.common.world.volume.SpongeVolumeStream) BlockStateBridge(org.spongepowered.common.bridge.world.level.block.state.BlockStateBridge) Vector3i(org.spongepowered.math.vector.Vector3i) Vector3d(org.spongepowered.math.vector.Vector3d) BlockEntityArchetypeVolume(org.spongepowered.api.world.volume.archetype.block.entity.BlockEntityArchetypeVolume) ArrayMutableBlockBuffer(org.spongepowered.common.world.volume.buffer.block.ArrayMutableBlockBuffer) VolumeElement(org.spongepowered.api.world.volume.stream.VolumeElement) SpongeVolumeStream(org.spongepowered.common.world.volume.SpongeVolumeStream)

Example 17 with Vector3d

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()));
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) Ticks(org.spongepowered.api.util.Ticks) LivingEntity(net.minecraft.world.entity.LivingEntity) PotionEffectUtil(org.spongepowered.common.util.PotionEffectUtil) ImmutableMap(com.google.common.collect.ImmutableMap) Constants(org.spongepowered.common.util.Constants) SpongeDamageSources(org.spongepowered.common.event.cause.entity.damage.SpongeDamageSources) BodyParts(org.spongepowered.api.data.type.BodyParts) Collection(java.util.Collection) LivingEntityAccessor(org.spongepowered.common.accessor.world.entity.LivingEntityAccessor) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) Entity(org.spongepowered.api.entity.Entity) Keys(org.spongepowered.api.data.Keys) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) Vector3d(org.spongepowered.math.vector.Vector3d) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) DamageSource(net.minecraft.world.damagesource.DamageSource) Attributes(net.minecraft.world.entity.ai.attributes.Attributes) EntityAccessor(org.spongepowered.common.accessor.world.entity.EntityAccessor) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) LivingEntityBridge(org.spongepowered.common.bridge.world.entity.LivingEntityBridge) LivingEntityAccessor(org.spongepowered.common.accessor.world.entity.LivingEntityAccessor) DamageSource(net.minecraft.world.damagesource.DamageSource) Vector3d(org.spongepowered.math.vector.Vector3d) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Collection(java.util.Collection)

Example 18 with Vector3d

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));
}
Also used : Vector3d(org.spongepowered.math.vector.Vector3d)

Example 19 with Vector3d

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);
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) Vector3d(org.spongepowered.math.vector.Vector3d)

Example 20 with Vector3d

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();
    }
}
Also used : BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) FallingBlock(net.minecraft.world.level.block.FallingBlock) ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) Vector3d(org.spongepowered.math.vector.Vector3d) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

Vector3d (org.spongepowered.math.vector.Vector3d)71 Vector3i (org.spongepowered.math.vector.Vector3i)16 CauseStackManager (org.spongepowered.api.event.CauseStackManager)14 AABB (org.spongepowered.api.util.AABB)14 Test (org.junit.jupiter.api.Test)13 ServerWorld (org.spongepowered.api.world.server.ServerWorld)13 BlockPos (net.minecraft.core.BlockPos)8 ServerLevel (net.minecraft.server.level.ServerLevel)7 BlockState (org.spongepowered.api.block.BlockState)7 Transformation (org.spongepowered.api.util.transformation.Transformation)7 Function (java.util.function.Function)6 IntStream (java.util.stream.IntStream)6 Stream (java.util.stream.Stream)6 Entity (org.spongepowered.api.entity.Entity)6 MoveEntityEvent (org.spongepowered.api.event.entity.MoveEntityEvent)6 ServerLocation (org.spongepowered.api.world.server.ServerLocation)6 Nullable (org.checkerframework.checker.nullness.qual.Nullable)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 MethodSource (org.junit.jupiter.params.provider.MethodSource)5 BlockType (org.spongepowered.api.block.BlockType)5