Search in sources :

Example 1 with ServerLocation

use of org.spongepowered.api.world.server.ServerLocation in project SpongeCommon by SpongePowered.

the class SpongeTeleportHelper method getBlockLocations.

private Stream<Vector3i> getBlockLocations(ServerLocation worldLocation, int height, int width) {
    // We don't want to warp outside of the world border, so we want to check that we're within it.
    final WorldBorder.Settings worldBorder = (WorldBorder.Settings) worldLocation.world().properties().worldBorder();
    final double radius = worldBorder.getSize() / 2.0D;
    int worldBorderMinX = GenericMath.floor(worldBorder.getCenterX() - radius);
    int worldBorderMinZ = GenericMath.floor(worldBorder.getCenterZ() - radius);
    int worldBorderMaxX = GenericMath.floor(worldBorder.getCenterX() + radius);
    int worldBorderMaxZ = GenericMath.floor(worldBorder.getCenterZ() + radius);
    // Get the World and get the maximum Y value.
    int worldMaxY = worldLocation.world().max().y();
    Vector3i vectorLocation = worldLocation.blockPosition();
    // We use clamp to remain within the world confines, so we don't waste time checking blocks outside of the
    // world border and the world height.
    int minY = GenericMath.clamp(vectorLocation.y() - height, 0, worldMaxY);
    int maxY = GenericMath.clamp(vectorLocation.y() + height, 0, worldMaxY);
    int minX = GenericMath.clamp(vectorLocation.x() - width, worldBorderMinX, worldBorderMaxX);
    int maxX = GenericMath.clamp(vectorLocation.x() + width, worldBorderMinX, worldBorderMaxX);
    int minZ = GenericMath.clamp(vectorLocation.z() - width, worldBorderMinZ, worldBorderMaxZ);
    int maxZ = GenericMath.clamp(vectorLocation.z() + width, worldBorderMinZ, worldBorderMaxZ);
    // We now iterate over all possible x, y and z positions to get all possible vectors.
    List<Vector3i> vectors = new ArrayList<>();
    for (int y = minY; y <= maxY; y++) {
        for (int x = minX; x <= maxX; x++) {
            for (int z = minZ; z <= maxZ; z++) {
                vectors.add(new Vector3i(x, y, z));
            }
        }
    }
    Comparator<Vector3i> c = Comparator.comparingInt(vectorLocation::distanceSquared);
    // The compiler seems to need this to be a new line.
    // We check to see what the y location is, preferring changes in Y over X and Z, and higher over lower locations.
    c = c.thenComparing(x -> -Math.abs(vectorLocation.y() - x.y())).thenComparing(x -> -x.y());
    // Sort them according to the distance to the provided worldLocation.
    return vectors.stream().sorted(c);
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) GenericMath(org.spongepowered.math.GenericMath) Collection(java.util.Collection) Set(java.util.Set) HashMap(java.util.HashMap) Sets(com.google.common.collect.Sets) Tristate(org.spongepowered.api.util.Tristate) ArrayList(java.util.ArrayList) BlockState(org.spongepowered.api.block.BlockState) TeleportHelper(org.spongepowered.api.world.teleport.TeleportHelper) TeleportHelperFilter(org.spongepowered.api.world.teleport.TeleportHelperFilter) List(java.util.List) Stream(java.util.stream.Stream) TeleportHelperFilters(org.spongepowered.api.world.teleport.TeleportHelperFilters) WorldBorder(net.minecraft.world.level.border.WorldBorder) World(org.spongepowered.api.world.World) Map(java.util.Map) SpongeConfigs(org.spongepowered.common.applaunch.config.core.SpongeConfigs) Optional(java.util.Optional) Comparator(java.util.Comparator) Singleton(com.google.inject.Singleton) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Vector3i(org.spongepowered.math.vector.Vector3i) WorldBorder(net.minecraft.world.level.border.WorldBorder) Vector3i(org.spongepowered.math.vector.Vector3i) ArrayList(java.util.ArrayList)

Example 2 with ServerLocation

use of org.spongepowered.api.world.server.ServerLocation in project SpongeCommon by SpongePowered.

the class ServerLevelMixin method impl$constructPostEventForEntityAdd.

@Inject(method = "addEntity", at = @At("HEAD"))
private void impl$constructPostEventForEntityAdd(final Entity entity, final CallbackInfoReturnable<Boolean> cir) {
    if (!(entity instanceof EntityBridge)) {
        return;
    }
    if (!((EntityBridge) entity).bridge$isConstructing()) {
        return;
    }
    ((EntityBridge) entity).bridge$fireConstructors();
    final Vec3 position = entity.position();
    final ServerLocation location = ServerLocation.of((ServerWorld) this, position.x(), position.y(), position.z());
    final Vec2 rotationVector = entity.getRotationVector();
    final Vector3d rotation = new Vector3d(rotationVector.x, rotationVector.y, 0);
    try (final CauseStackManager.StackFrame frame = PhaseTracker.SERVER.pushCauseFrame()) {
        frame.pushCause(entity);
        final Event construct = SpongeEventFactory.createConstructEntityEventPost(frame.currentCause(), (org.spongepowered.api.entity.Entity) entity, location, rotation, ((EntityType<?>) entity.getType()));
        SpongeCommon.post(construct);
    }
}
Also used : EntityType(org.spongepowered.api.entity.EntityType) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Vector3d(org.spongepowered.math.vector.Vector3d) Vec2(net.minecraft.world.phys.Vec2) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Vec3(net.minecraft.world.phys.Vec3) ExplosionEvent(org.spongepowered.api.event.world.ExplosionEvent) PlaySoundEvent(org.spongepowered.api.event.sound.PlaySoundEvent) ChangeWeatherEvent(org.spongepowered.api.event.world.ChangeWeatherEvent) LightningEvent(org.spongepowered.api.event.action.LightningEvent) Event(org.spongepowered.api.event.Event) EntityBridge(org.spongepowered.common.bridge.world.entity.EntityBridge) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 3 with ServerLocation

use of org.spongepowered.api.world.server.ServerLocation in project SpongeCommon by SpongePowered.

the class CommandSourceStackMixin method impl$updateCauseOnWithPosition.

@Inject(method = "withPosition", at = @At("RETURN"))
private void impl$updateCauseOnWithPosition(final Vec3 pos, final CallbackInfoReturnable<CommandSourceStack> cir) {
    if (cir.getReturnValue() != (Object) this) {
        final org.spongepowered.math.vector.Vector3d position = VecHelper.toVector3d(pos);
        final ServerLocation location = this.impl$cause.context().get(EventContextKeys.LOCATION).map(x -> ServerLocation.of(x.world(), position)).orElseGet(() -> ServerLocation.of((org.spongepowered.api.world.server.ServerWorld) cir.getReturnValue().getLevel(), position));
        ((CommandSourceStackBridge) cir.getReturnValue()).bridge$setCause(this.impl$applyToCause(EventContextKeys.LOCATION, location));
    }
}
Also used : EventContextKey(org.spongepowered.api.event.EventContextKey) CommandSourceStackAccessor(org.spongepowered.common.accessor.commands.CommandSourceStackAccessor) EventContextKeys(org.spongepowered.api.event.EventContextKeys) CommandSourceStack(net.minecraft.commands.CommandSourceStack) Inject(org.spongepowered.asm.mixin.injection.Inject) HashMap(java.util.HashMap) ServerLevel(net.minecraft.server.level.ServerLevel) EventContext(org.spongepowered.api.event.EventContext) Supplier(java.util.function.Supplier) EntityAnchorArgument(net.minecraft.commands.arguments.EntityAnchorArgument) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) MinecraftServer(net.minecraft.server.MinecraftServer) Mixin(org.spongepowered.asm.mixin.Mixin) CommandSourceStackBridge(org.spongepowered.common.bridge.commands.CommandSourceStackBridge) Map(java.util.Map) EntityAccessor(org.spongepowered.common.accessor.world.entity.EntityAccessor) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Nullable(org.checkerframework.checker.nullness.qual.Nullable) At(org.spongepowered.asm.mixin.injection.At) Mutable(org.spongepowered.asm.mixin.Mutable) Component(net.minecraft.network.chat.Component) Sponge(org.spongepowered.api.Sponge) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Final(org.spongepowered.asm.mixin.Final) SpongePermissions(org.spongepowered.common.service.server.permission.SpongePermissions) CommandSourceBridge(org.spongepowered.common.bridge.commands.CommandSourceBridge) Cause(org.spongepowered.api.event.Cause) CommandCause(org.spongepowered.api.command.CommandCause) Entity(net.minecraft.world.entity.Entity) Vec2(net.minecraft.world.phys.Vec2) Vec3(net.minecraft.world.phys.Vec3) VecHelper(org.spongepowered.common.util.VecHelper) Shadow(org.spongepowered.asm.mixin.Shadow) CommandSource(net.minecraft.commands.CommandSource) ResultConsumer(com.mojang.brigadier.ResultConsumer) ServerLocation(org.spongepowered.api.world.server.ServerLocation) ServerLocation(org.spongepowered.api.world.server.ServerLocation) CommandSourceStackBridge(org.spongepowered.common.bridge.commands.CommandSourceStackBridge) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 4 with ServerLocation

use of org.spongepowered.api.world.server.ServerLocation in project SpongeCommon by SpongePowered.

the class CactusBlockMixin method impl$reAssignForBlockDamageSource.

@Redirect(method = "entityInside", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;hurt(Lnet/minecraft/world/damagesource/DamageSource;F)Z"))
private boolean impl$reAssignForBlockDamageSource(final Entity self, final DamageSource source, float damage, final net.minecraft.world.level.block.state.BlockState state, final net.minecraft.world.level.Level world, final BlockPos pos, final Entity entity) {
    if (world.isClientSide()) {
        return entity.hurt(source, damage);
    }
    try {
        final ServerLocation location = ServerLocation.of((ServerWorld) world, pos.getX(), pos.getY(), pos.getZ());
        final MinecraftBlockDamageSource cactus = new MinecraftBlockDamageSource("cactus", location);
        ((DamageSourceBridge) (Object) cactus).bridge$setCactusSource();
        return entity.hurt(DamageSource.CACTUS, damage);
    } finally {
        ((DamageSourceBridge) source).bridge$setCactusSource();
    }
}
Also used : DamageSourceBridge(org.spongepowered.common.bridge.world.damagesource.DamageSourceBridge) ServerLocation(org.spongepowered.api.world.server.ServerLocation) MinecraftBlockDamageSource(org.spongepowered.common.util.MinecraftBlockDamageSource) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 5 with ServerLocation

use of org.spongepowered.api.world.server.ServerLocation in project SpongeCommon by SpongePowered.

the class CampfireBlockMixin method impl$spongeRedirectForFireDamage.

@Redirect(method = "entityInside", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;hurt(Lnet/minecraft/world/damagesource/DamageSource;F)Z"))
private boolean impl$spongeRedirectForFireDamage(final Entity self, final DamageSource source, final float damage, final BlockState blockState, final Level world, final BlockPos blockPos, final Entity entity) {
    if (self.level.isClientSide) {
        // Short Circuit
        return self.hurt(source, damage);
    }
    try {
        final ServerLocation location = ServerLocation.of((ServerWorld) world, blockPos.getX(), blockPos.getY(), blockPos.getZ());
        final DamageSource fire = MinecraftBlockDamageSource.ofFire("inFire", location, true);
        ((DamageSourceBridge) fire).bridge$setFireSource();
        return self.hurt(DamageSource.IN_FIRE, damage);
    } finally {
        // Since "source" is already the DamageSource.IN_FIRE object, we can re-use it to re-assign.
        ((DamageSourceBridge) source).bridge$setFireSource();
    }
}
Also used : DamageSource(net.minecraft.world.damagesource.DamageSource) MinecraftBlockDamageSource(org.spongepowered.common.util.MinecraftBlockDamageSource) DamageSourceBridge(org.spongepowered.common.bridge.world.damagesource.DamageSourceBridge) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

ServerLocation (org.spongepowered.api.world.server.ServerLocation)34 ServerWorld (org.spongepowered.api.world.server.ServerWorld)12 Component (net.kyori.adventure.text.Component)9 Cause (org.spongepowered.api.event.Cause)9 CauseStackManager (org.spongepowered.api.event.CauseStackManager)8 ArrayList (java.util.ArrayList)7 Optional (java.util.Optional)7 Sponge (org.spongepowered.api.Sponge)7 Entity (org.spongepowered.api.entity.Entity)7 Redirect (org.spongepowered.asm.mixin.injection.Redirect)7 Vector3d (org.spongepowered.math.vector.Vector3d)7 Collection (java.util.Collection)6 List (java.util.List)6 BlockPos (net.minecraft.core.BlockPos)6 DamageSource (net.minecraft.world.damagesource.DamageSource)6 Nullable (org.checkerframework.checker.nullness.qual.Nullable)6 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 ServerLevel (net.minecraft.server.level.ServerLevel)5