Search in sources :

Example 21 with ServerLocation

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

the class DamageEventUtil method generateCauseFor.

/**
 * This applies various contexts based on the type of {@link DamageSource}, whether
 * it's provided by sponge or vanilla. This is not stack neutral, which is why it requires
 * a {@link CauseStackManager.StackFrame} reference to push onto the stack.
 */
public static void generateCauseFor(final DamageSource damageSource, final CauseStackManager.StackFrame frame) {
    if (damageSource instanceof EntityDamageSource) {
        final net.minecraft.world.entity.Entity source = damageSource.getEntity();
        if (!(source instanceof Player) && source instanceof CreatorTrackedBridge) {
            final CreatorTrackedBridge creatorBridge = (CreatorTrackedBridge) source;
            creatorBridge.tracker$getCreatorUUID().ifPresent(creator -> frame.addContext(EventContextKeys.CREATOR, creator));
            creatorBridge.tracker$getNotifierUUID().ifPresent(notifier -> frame.addContext(EventContextKeys.NOTIFIER, notifier));
        }
    } else if (damageSource instanceof BlockDamageSource) {
        final ServerLocation location = ((BlockDamageSource) damageSource).location();
        final BlockPos blockPos = VecHelper.toBlockPos(location);
        final LevelChunkBridge chunkBridge = (LevelChunkBridge) ((net.minecraft.world.level.Level) location.world()).getChunkAt(blockPos);
        chunkBridge.bridge$getBlockCreatorUUID(blockPos).ifPresent(creator -> frame.addContext(EventContextKeys.CREATOR, creator));
        chunkBridge.bridge$getBlockNotifierUUID(blockPos).ifPresent(notifier -> frame.addContext(EventContextKeys.NOTIFIER, notifier));
    }
    frame.pushCause(damageSource);
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) LivingEntity(net.minecraft.world.entity.LivingEntity) AABB(net.minecraft.world.phys.AABB) LivingEntityAccessor(org.spongepowered.common.accessor.world.entity.LivingEntityAccessor) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) MobType(net.minecraft.world.entity.MobType) EventContext(org.spongepowered.api.event.EventContext) Registry(net.minecraft.core.Registry) ChunkSource(net.minecraft.world.level.chunk.ChunkSource) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Player(net.minecraft.world.entity.player.Player) List(java.util.List) BlockPos(net.minecraft.core.BlockPos) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) Optional(java.util.Optional) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Enchantment(net.minecraft.world.item.enchantment.Enchantment) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) EventContextKeys(org.spongepowered.api.event.EventContextKeys) ServerWorld(org.spongepowered.api.world.server.ServerWorld) BlockState(net.minecraft.world.level.block.state.BlockState) HashMap(java.util.HashMap) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) MobEffects(net.minecraft.world.effect.MobEffects) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) EntityDamageSource(net.minecraft.world.damagesource.EntityDamageSource) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) DamageSource(net.minecraft.world.damagesource.DamageSource) EnchantmentHelper(net.minecraft.world.item.enchantment.EnchantmentHelper) CombatRules(net.minecraft.world.damagesource.CombatRules) Server(org.spongepowered.api.Server) CauseStackManager(org.spongepowered.api.event.CauseStackManager) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) Cause(org.spongepowered.api.event.Cause) Entity(net.minecraft.world.entity.Entity) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Attributes(net.minecraft.world.entity.ai.attributes.Attributes) Mth(net.minecraft.util.Mth) Collections(java.util.Collections) ListTag(net.minecraft.nbt.ListTag) Player(net.minecraft.world.entity.player.Player) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ServerLocation(org.spongepowered.api.world.server.ServerLocation) EntityDamageSource(net.minecraft.world.damagesource.EntityDamageSource) Entity(net.minecraft.world.entity.Entity) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) BlockPos(net.minecraft.core.BlockPos) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge)

Example 22 with ServerLocation

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

the class SpongeContextCalculator method accumulateContexts.

@Override
public void accumulateContexts(final Cause causes, final Consumer<Context> accumulator) {
    final ServerLocation /* @Nullable */
    location = causes.context().get(EventContextKeys.LOCATION).orElse(null);
    if (location != null) {
        final ServerWorld world = location.worldIfAvailable().orElse(null);
        if (world != null) {
            accumulator.accept(world.context());
            accumulator.accept(world.worldType().context());
        }
    }
    causes.first(RemoteConnection.class).ifPresent(connection -> {
        // TODO(zml): Wrong way to get a connection, add API?
        this.remoteIpCache.get(connection).forEach(accumulator);
        this.localIpCache.get(connection).forEach(accumulator);
        accumulator.accept(new Context(Context.LOCAL_PORT_KEY, String.valueOf(connection.virtualHost().getPort())));
        accumulator.accept(new Context(Context.LOCAL_HOST_KEY, connection.virtualHost().getHostName()));
    });
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) Context(org.spongepowered.api.service.context.Context) ServerLocation(org.spongepowered.api.world.server.ServerLocation) RemoteConnection(org.spongepowered.api.network.RemoteConnection)

Example 23 with ServerLocation

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

the class CommandSourceStackMixin method impl$updateCauseOnWithWorld.

/*
     * A note on why we're doing this with the cause manually.
     *
     * When the object is first constructed, we get the cause from the stack manager. However, as the command processor
     * works through the nodes, this entire source may get replaced. We want to keep some of the changes in sync,
     * but the original cause may have gone by the time the source changes. Really, this command source is the analogue
     * of our Cause, NOT our CauseStackManager, so we just need to do `Cause.with(...)` along with their select `with*(...)`
     * methods.
     */
@Inject(method = "withLevel", at = @At("RETURN"))
private void impl$updateCauseOnWithWorld(final ServerLevel serverWorld, final CallbackInfoReturnable<CommandSourceStack> cir) {
    if (cir.getReturnValue() != (Object) this) {
        final ServerLocation location = this.impl$cause.context().get(EventContextKeys.LOCATION).map(x -> ServerLocation.of((org.spongepowered.api.world.server.ServerWorld) serverWorld, x.position())).orElseGet(() -> ServerLocation.of((org.spongepowered.api.world.server.ServerWorld) serverWorld, VecHelper.toVector3d(cir.getReturnValue().getPosition())));
        ((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 24 with ServerLocation

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

the class PlayerListMixin method impl$onInitPlayer_getWorld.

@Redirect(method = "placeNewPlayer", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;getLevel(Lnet/minecraft/resources/ResourceKey;)Lnet/minecraft/server/level/ServerLevel;"))
private net.minecraft.server.level.ServerLevel impl$onInitPlayer_getWorld(final MinecraftServer minecraftServer, final ResourceKey<Level> dimension, final Connection networkManager, final net.minecraft.server.level.ServerPlayer mcPlayer) {
    @Nullable final net.minecraft.network.chat.Component kickReason = ((ConnectionBridge) networkManager).bridge$getKickReason();
    final Component disconnectMessage;
    if (kickReason != null) {
        disconnectMessage = SpongeAdventure.asAdventure(kickReason);
    } else {
        disconnectMessage = Component.text("You are not allowed to log in to this server.");
    }
    net.minecraft.server.level.ServerLevel mcWorld = minecraftServer.getLevel(dimension);
    if (mcWorld == null) {
        SpongeCommon.logger().warn("The player '{}' was located in a world that isn't loaded or doesn't exist. This is not safe so " + "the player will be moved to the spawn of the default world.", mcPlayer.getGameProfile().getName());
        mcWorld = minecraftServer.overworld();
        final BlockPos spawnPoint = mcWorld.getSharedSpawnPos();
        mcPlayer.setPos(spawnPoint.getX() + 0.5, spawnPoint.getY() + 0.5, spawnPoint.getZ() + 0.5);
    }
    mcPlayer.setLevel(mcWorld);
    final ServerPlayer player = (ServerPlayer) mcPlayer;
    final ServerLocation location = player.serverLocation();
    final Vector3d rotation = player.rotation();
    // player.connection() cannot be used here, because it's still be null at this point
    final ServerSideConnection connection = (ServerSideConnection) networkManager.getPacketListener();
    // The user is not yet in the player list, so we need to make special provision.
    final User user = SpongeUserView.createLoginEventUser(player);
    final Cause cause = Cause.of(EventContext.empty(), connection, user);
    final ServerSideConnectionEvent.Login event = SpongeEventFactory.createServerSideConnectionEventLogin(cause, disconnectMessage, disconnectMessage, location, location, rotation, rotation, connection, user);
    if (kickReason != null) {
        event.setCancelled(true);
    }
    if (SpongeCommon.post(event)) {
        this.impl$disconnectClient(networkManager, event.message(), player.profile());
        return null;
    }
    final ServerLocation toLocation = event.toLocation();
    final Vector3d toRotation = event.toRotation();
    mcPlayer.absMoveTo(toLocation.x(), toLocation.y(), toLocation.z(), (float) toRotation.y(), (float) toRotation.x());
    return (net.minecraft.server.level.ServerLevel) toLocation.world();
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) User(org.spongepowered.api.entity.living.player.User) ServerLocation(org.spongepowered.api.world.server.ServerLocation) ServerSideConnection(org.spongepowered.api.network.ServerSideConnection) ServerSideConnectionEvent(org.spongepowered.api.event.network.ServerSideConnectionEvent) ServerLevel(net.minecraft.server.level.ServerLevel) Vector3d(org.spongepowered.math.vector.Vector3d) ConnectionBridge(org.spongepowered.common.bridge.network.ConnectionBridge) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) Cause(org.spongepowered.api.event.Cause) BlockPos(net.minecraft.core.BlockPos) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) TextComponent(net.minecraft.network.chat.TextComponent) MutableComponent(net.minecraft.network.chat.MutableComponent) Component(net.kyori.adventure.text.Component) Nullable(javax.annotation.Nullable) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 25 with ServerLocation

use of org.spongepowered.api.world.server.ServerLocation 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());
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Vector3d(org.spongepowered.math.vector.Vector3d) Living(org.spongepowered.api.entity.living.Living) Cause(org.spongepowered.api.event.Cause) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) BlockPos(net.minecraft.core.BlockPos) SleepingEvent(org.spongepowered.api.event.action.SleepingEvent) Inject(org.spongepowered.asm.mixin.injection.Inject)

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