Search in sources :

Example 16 with ServerWorld

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

the class SpongeEntitySnapshot method restore.

@Override
public Optional<Entity> restore() {
    if (this.entityReference != null) {
        final Entity entity = this.entityReference.get();
        if (entity != null) {
            return Optional.of(entity);
        }
    }
    final Optional<ServerWorld> world = Sponge.server().worldManager().world(this.worldKey);
    if (!world.isPresent()) {
        return Optional.empty();
    }
    if (this.uniqueId != null) {
        final Optional<Entity> entity = world.get().entity(this.uniqueId);
        if (entity.isPresent()) {
            return entity;
        }
    }
    try (final StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLUGIN);
        final Entity newEntity = world.get().createEntity(this.type(), this.position);
        if (newEntity != null) {
            final net.minecraft.world.entity.Entity nmsEntity = (net.minecraft.world.entity.Entity) newEntity;
            if (this.compound != null) {
                nmsEntity.load(this.compound);
            }
            final boolean spawnResult = world.get().spawnEntity((Entity) nmsEntity);
            if (spawnResult) {
                return Optional.of((Entity) nmsEntity);
            }
        }
    }
    return Optional.empty();
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) Entity(org.spongepowered.api.entity.Entity) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame)

Example 17 with ServerWorld

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

the class EntityUtil method performPostChangePlayerWorldLogic.

public static void performPostChangePlayerWorldLogic(final ServerPlayer player, final ServerLevel fromWorld, final ServerLevel originalToWorld, final ServerLevel toWorld, final boolean isPortal) {
    // Sponge Start - Send any platform dimension data
    ((ServerPlayerBridge) player).bridge$sendDimensionData(player.connection.connection, toWorld.dimensionType(), toWorld.dimension());
    // Sponge End
    final LevelData worldinfo = toWorld.getLevelData();
    // We send dimension change for portals before loading chunks
    if (!isPortal) {
        // Sponge Start - Allow the platform to handle how dimension changes are sent down
        ((ServerPlayerBridge) player).bridge$sendChangeDimension(toWorld.dimensionType(), toWorld.dimension(), BiomeManager.obfuscateSeed(toWorld.getSeed()), player.gameMode.getGameModeForPlayer(), player.gameMode.getPreviousGameModeForPlayer(), toWorld.isDebug(), toWorld.isFlat(), true);
    }
    // Sponge End
    player.connection.send(new ClientboundChangeDifficultyPacket(worldinfo.getDifficulty(), worldinfo.isDifficultyLocked()));
    final PlayerList playerlist = player.getServer().getPlayerList();
    playerlist.sendPlayerPermissionLevel(player);
    // Sponge Start - Have the platform handle removing the entity from the world. Move this to after the event call so
    // that we do not remove the player from the world unless we really have teleported..
    ((PlatformServerLevelBridge) fromWorld).bridge$removeEntity(player, true);
    ((PlatformEntityBridge) player).bridge$revive();
    // Sponge End
    player.setLevel(toWorld);
    toWorld.addDuringPortalTeleport(player);
    if (isPortal) {
        ((ServerPlayerAccessor) player).invoker$triggerDimensionChangeTriggers(toWorld);
    }
    player.gameMode.setLevel(toWorld);
    player.connection.send(new ClientboundPlayerAbilitiesPacket(player.abilities));
    playerlist.sendLevelInfo(player, toWorld);
    playerlist.sendAllPlayerInfo(player);
    for (final MobEffectInstance effectinstance : player.getActiveEffects()) {
        player.connection.send(new ClientboundUpdateMobEffectPacket(player.getId(), effectinstance));
    }
    if (isPortal) {
        player.connection.send(new ClientboundLevelEventPacket(1032, BlockPos.ZERO, 0, false));
    }
    ((ServerLevelBridge) fromWorld).bridge$getBossBarManager().onPlayerDisconnect(player);
    ((ServerLevelBridge) toWorld).bridge$getBossBarManager().onPlayerDisconnect(player);
    ((ServerPlayerAccessor) player).accessor$lastSentExp(-1);
    ((ServerPlayerAccessor) player).accessor$lastSentHealth(-1.0f);
    ((ServerPlayerAccessor) player).accessor$lastSentFood(-1);
    if (!isPortal) {
        player.connection.teleport(player.getX(), player.getY(), player.getZ(), player.yRot, player.xRot);
        player.connection.resetPosition();
    }
    if (player.containerMenu != player.inventoryMenu) {
        player.closeContainer();
    }
    // Sponge Start - Call event
    Sponge.eventManager().post(SpongeEventFactory.createChangeEntityWorldEventPost(PhaseTracker.getCauseStackManager().currentCause(), (org.spongepowered.api.entity.Entity) player, (ServerWorld) fromWorld, (ServerWorld) originalToWorld, (ServerWorld) toWorld));
// Sponge End
}
Also used : Entity(net.minecraft.world.entity.Entity) ItemEntity(net.minecraft.world.entity.item.ItemEntity) PlatformServerLevelBridge(org.spongepowered.common.bridge.world.level.PlatformServerLevelBridge) PlayerList(net.minecraft.server.players.PlayerList) ClientboundLevelEventPacket(net.minecraft.network.protocol.game.ClientboundLevelEventPacket) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) ClientboundChangeDifficultyPacket(net.minecraft.network.protocol.game.ClientboundChangeDifficultyPacket) ClientboundUpdateMobEffectPacket(net.minecraft.network.protocol.game.ClientboundUpdateMobEffectPacket) PlatformEntityBridge(org.spongepowered.common.bridge.world.entity.PlatformEntityBridge) ServerPlayerBridge(org.spongepowered.common.bridge.server.level.ServerPlayerBridge) ServerWorld(org.spongepowered.api.world.server.ServerWorld) ClientboundPlayerAbilitiesPacket(net.minecraft.network.protocol.game.ClientboundPlayerAbilitiesPacket) ServerPlayerAccessor(org.spongepowered.common.accessor.server.level.ServerPlayerAccessor) LevelData(net.minecraft.world.level.storage.LevelData)

Example 18 with ServerWorld

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

the class CompassItemData method register.

static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.LODESTONE).get(stack -> {
        if (CompassItem.isLodestoneCompass(stack)) {
            final CompoundTag tag = stack.getOrCreateTag();
            final Optional<ResourceKey<Level>> dimension = CompassItem.getLodestoneDimension(tag);
            if (dimension.isPresent()) {
                return ServerLocation.of((ServerWorld) SpongeCommon.server().getLevel(dimension.get()), VecHelper.toVector3d(NbtUtils.readBlockPos(tag.getCompound("LodestonePos"))));
            }
        }
        return null;
    }).set((stack, location) -> {
        final CompoundTag tag = stack.getOrCreateTag();
        tag.put("LodestonePos", NbtUtils.writeBlockPos(VecHelper.toBlockPos(location)));
        Level.RESOURCE_KEY_CODEC.encodeStart(NbtOps.INSTANCE, ((net.minecraft.server.level.ServerLevel) location.world()).dimension()).resultOrPartial(SpongeCommon.logger()::error).ifPresent(dimension -> tag.put("LodestoneDimension", dimension));
        tag.putBoolean("LodestoneTracked", true);
    }).delete(stack -> {
        final CompoundTag tag = stack.getTag();
        if (tag != null) {
            tag.remove("LodestoneDimension");
            tag.remove("LodestonePos");
            tag.remove("LodestoneTracked");
        }
    });
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) NbtUtils(net.minecraft.nbt.NbtUtils) SpongeCommon(org.spongepowered.common.SpongeCommon) NbtOps(net.minecraft.nbt.NbtOps) ResourceKey(net.minecraft.resources.ResourceKey) CompassItem(net.minecraft.world.item.CompassItem) Keys(org.spongepowered.api.data.Keys) CompoundTag(net.minecraft.nbt.CompoundTag) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) VecHelper(org.spongepowered.common.util.VecHelper) Optional(java.util.Optional) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) ServerLocation(org.spongepowered.api.world.server.ServerLocation) CompoundTag(net.minecraft.nbt.CompoundTag) ResourceKey(net.minecraft.resources.ResourceKey)

Example 19 with ServerWorld

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

the class SpongePlaceholderContextBuilder method associatedObject.

@Override
@SuppressWarnings("unchecked")
public PlaceholderContext.Builder associatedObject(@Nullable final Object associatedObject) {
    if (associatedObject == null) {
        this.associatedObjectSupplier = null;
    } else if (associatedObject instanceof Supplier) {
        return this.associatedObject((Supplier<Object>) associatedObject);
    } else if (associatedObject instanceof SystemSubject) {
        this.associatedObjectSupplier = Sponge::systemSubject;
    } else if (associatedObject instanceof Server) {
        this.associatedObjectSupplier = Sponge::server;
    } else if (associatedObject instanceof Player) {
        return this.associatedObject((Player) associatedObject);
    } else if (associatedObject instanceof ServerWorld) {
        final ResourceKey key = ((ServerWorld) associatedObject).key();
        this.associatedObjectSupplier = () -> SpongeCommon.game().server().worldManager().world(key).orElse(null);
    } else if (associatedObject instanceof Entity) {
        final Entity entity = ((Entity) associatedObject);
        final ResourceKey key = entity.serverLocation().world().key();
        final UUID entityUuid = ((Entity) associatedObject).uniqueId();
        this.associatedObjectSupplier = () -> SpongeCommon.game().server().worldManager().world(key).flatMap(x -> x.entity(entityUuid)).orElse(null);
    } else {
        // We create a weak reference here so we don't hold on to game objects.
        final WeakReference<Object> objectWeakReference = new WeakReference<>(associatedObject);
        this.associatedObjectSupplier = objectWeakReference::get;
    }
    return this;
}
Also used : Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) Server(org.spongepowered.api.Server) SystemSubject(org.spongepowered.api.SystemSubject) ResourceKey(org.spongepowered.api.ResourceKey) ServerWorld(org.spongepowered.api.world.server.ServerWorld) WeakReference(java.lang.ref.WeakReference) Supplier(java.util.function.Supplier) Sponge(org.spongepowered.api.Sponge) UUID(java.util.UUID)

Example 20 with ServerWorld

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

the class PlaceBlockPacketState method appendNotifierToBlockEvent.

@Override
public void appendNotifierToBlockEvent(final BasicPacketContext context, final TrackedWorldBridge mixinWorldServer, final BlockPos pos, final TrackableBlockEventDataBridge blockEvent) {
    final Player player = PhaseTracker.getCauseStackManager().currentCause().first(Player.class).get();
    final BlockState state = ((ServerWorld) mixinWorldServer).block(pos.getX(), pos.getY(), pos.getZ());
    final LocatableBlock locatable = new SpongeLocatableBlockBuilder().world((ServerWorld) mixinWorldServer).position(pos.getX(), pos.getY(), pos.getZ()).state(state).build();
    blockEvent.bridge$setTickingLocatable(locatable);
    blockEvent.bridge$setSourceUserUUID(player.uniqueId());
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) SpongeLocatableBlockBuilder(org.spongepowered.common.world.server.SpongeLocatableBlockBuilder) Player(org.spongepowered.api.entity.living.player.Player) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) BlockState(org.spongepowered.api.block.BlockState) LocatableBlock(org.spongepowered.api.world.LocatableBlock)

Aggregations

ServerWorld (org.spongepowered.api.world.server.ServerWorld)43 Vector3d (org.spongepowered.math.vector.Vector3d)13 BlockPos (net.minecraft.core.BlockPos)12 Cause (org.spongepowered.api.event.Cause)12 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)11 CauseStackManager (org.spongepowered.api.event.CauseStackManager)11 ServerLocation (org.spongepowered.api.world.server.ServerLocation)11 Inject (org.spongepowered.asm.mixin.injection.Inject)10 ArrayList (java.util.ArrayList)9 NonNull (org.checkerframework.checker.nullness.qual.NonNull)9 ResourceKey (org.spongepowered.api.ResourceKey)8 List (java.util.List)7 Optional (java.util.Optional)7 ServerLevel (net.minecraft.server.level.ServerLevel)7 Entity (net.minecraft.world.entity.Entity)7 Living (org.spongepowered.api.entity.living.Living)7 SpongeCommon (org.spongepowered.common.SpongeCommon)7 Component (net.kyori.adventure.text.Component)6 LivingEntity (net.minecraft.world.entity.LivingEntity)6 ItemStack (net.minecraft.world.item.ItemStack)6