Search in sources :

Example 21 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.

the class PlayerListMixin method impl$callRespawnPlayerRecreateEvent.

@Redirect(method = "respawn", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerGamePacketListenerImpl;send(Lnet/minecraft/network/protocol/Packet;)V", ordinal = 1))
private void impl$callRespawnPlayerRecreateEvent(final ServerGamePacketListenerImpl serverPlayNetHandler, final Packet<?> packetIn, final net.minecraft.server.level.ServerPlayer originalPlayer, final boolean keepAllPlayerData) {
    final net.minecraft.server.level.ServerPlayer recreatedPlayer = serverPlayNetHandler.player;
    final Vector3d originalPosition = VecHelper.toVector3d(originalPlayer.position());
    final Vector3d destinationPosition = VecHelper.toVector3d(recreatedPlayer.position());
    final org.spongepowered.api.world.server.ServerWorld originalWorld = (org.spongepowered.api.world.server.ServerWorld) originalPlayer.level;
    final org.spongepowered.api.world.server.ServerWorld originalDestinationWorld = (org.spongepowered.api.world.server.ServerWorld) this.server.getLevel(this.impl$originalDestination == null ? Level.OVERWORLD : this.impl$originalDestination);
    final org.spongepowered.api.world.server.ServerWorld destinationWorld = (org.spongepowered.api.world.server.ServerWorld) this.server.getLevel(this.impl$newDestination == null ? Level.OVERWORLD : this.impl$newDestination);
    final RespawnPlayerEvent.Recreate event = SpongeEventFactory.createRespawnPlayerEventRecreate(PhaseTracker.getCauseStackManager().currentCause(), destinationPosition, originalWorld, originalPosition, destinationWorld, originalDestinationWorld, destinationPosition, (ServerPlayer) originalPlayer, (ServerPlayer) recreatedPlayer, this.impl$isGameMechanicRespawn, !keepAllPlayerData);
    SpongeCommon.post(event);
    recreatedPlayer.setPos(event.destinationPosition().x(), event.destinationPosition().y(), event.destinationPosition().z());
    this.impl$isGameMechanicRespawn = false;
    this.impl$originalDestination = null;
    this.impl$newDestination = null;
    final ServerLevel targetWorld = (ServerLevel) event.destinationWorld();
    ((ServerPlayerBridge) recreatedPlayer).bridge$sendChangeDimension(targetWorld.dimensionType(), ((ClientboundRespawnPacketAccessor) packetIn).accessor$dimension(), ((ClientboundRespawnPacketAccessor) packetIn).accessor$seed(), recreatedPlayer.gameMode.getGameModeForPlayer(), recreatedPlayer.gameMode.getPreviousGameModeForPlayer(), targetWorld.isDebug(), targetWorld.isFlat(), keepAllPlayerData);
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) RespawnPlayerEvent(org.spongepowered.api.event.entity.living.player.RespawnPlayerEvent) ServerPlayerBridge(org.spongepowered.common.bridge.server.level.ServerPlayerBridge) Vector3d(org.spongepowered.math.vector.Vector3d) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 22 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.

the class ServerChunkCacheMixin method impl$useSerializationBehaviorWhenSaving.

// @formatter:on
@Redirect(method = "save", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ChunkMap;saveAllChunks(Z)V"))
private void impl$useSerializationBehaviorWhenSaving(final ChunkMap chunkManager, final boolean flush) {
    final ServerLevel world = this.level;
    final SerializationBehavior serializationBehavior = ((ServerWorldProperties) world.getLevelData()).serializationBehavior();
    if (serializationBehavior == SerializationBehavior.AUTOMATIC || serializationBehavior == SerializationBehavior.MANUAL) {
        ((ChunkMapAccessor) chunkManager).invoker$saveAllChunks(flush);
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) ServerWorldProperties(org.spongepowered.api.world.server.storage.ServerWorldProperties) ChunkMapAccessor(org.spongepowered.common.accessor.server.level.ChunkMapAccessor) SerializationBehavior(org.spongepowered.api.world.SerializationBehavior) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 23 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.

the class ServerLevelMixin method save.

/**
 * @author zidane - December 17th, 2020 - Minecraft 1.16.4
 * @reason Honor our serialization behavior in performing saves
 */
@Overwrite
public void save(@Nullable final ProgressListener progress, final boolean flush, final boolean skipSave) {
    final Cause currentCause = Sponge.server().causeStackManager().currentCause();
    if (Sponge.eventManager().post(SpongeEventFactory.createSaveWorldEventPre(currentCause, ((ServerWorld) this)))) {
        // cancelled save
        return;
    }
    final PrimaryLevelData levelData = (PrimaryLevelData) this.shadow$getLevelData();
    final ServerChunkCache chunkProvider = ((ServerLevel) (Object) this).getChunkSource();
    if (!skipSave) {
        final SerializationBehavior behavior = ((PrimaryLevelDataBridge) levelData).bridge$serializationBehavior().orElse(SerializationBehavior.AUTOMATIC);
        if (progress != null) {
            progress.progressStartNoAbort(new TranslatableComponent("menu.savingLevel"));
        }
        // We always save the metadata unless it is NONE
        if (behavior != SerializationBehavior.NONE) {
            this.shadow$saveLevelData();
            // Sponge Start - We do per-world WorldInfo/WorldBorders/BossBars
            levelData.setWorldBorder(this.getWorldBorder().createSettings());
            levelData.setCustomBossEvents(((ServerLevelBridge) this).bridge$getBossBarManager().save());
            ((ServerLevelBridge) this).bridge$getLevelSave().saveDataTag(SpongeCommon.server().registryAccess(), (PrimaryLevelData) this.shadow$getLevelData(), this.shadow$dimension() == Level.OVERWORLD ? SpongeCommon.server().getPlayerList().getSingleplayerData() : null);
        // Sponge End
        }
        if (progress != null) {
            progress.progressStage(new TranslatableComponent("menu.savingChunks"));
        }
        final boolean canAutomaticallySave = !this.impl$isManualSave && behavior == SerializationBehavior.AUTOMATIC;
        final boolean canManuallySave = this.impl$isManualSave && behavior == SerializationBehavior.MANUAL;
        if (canAutomaticallySave || canManuallySave) {
            chunkProvider.save(flush);
        }
        Sponge.eventManager().post(SpongeEventFactory.createSaveWorldEventPost(currentCause, ((ServerWorld) this)));
    }
    this.impl$isManualSave = false;
}
Also used : PrimaryLevelData(net.minecraft.world.level.storage.PrimaryLevelData) ServerLevel(net.minecraft.server.level.ServerLevel) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Cause(org.spongepowered.api.event.Cause) SerializationBehavior(org.spongepowered.api.world.SerializationBehavior) ServerChunkCache(net.minecraft.server.level.ServerChunkCache) ServerLevelBridge(org.spongepowered.common.bridge.server.level.ServerLevelBridge) PlatformServerLevelBridge(org.spongepowered.common.bridge.world.level.PlatformServerLevelBridge) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 24 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.

the class TransactionSink method logScheduledUpdate.

@SuppressWarnings("ConstantConditions")
default void logScheduledUpdate(final ServerLevel serverWorld, final TickNextTickData<?> data) {
    final WeakReference<ServerLevel> worldRef = new WeakReference<>(serverWorld);
    final Supplier<ServerLevel> worldSupplier = () -> Objects.requireNonNull(worldRef.get(), "ServerWorld dereferenced");
    @Nullable final BlockEntity tileEntity = serverWorld.getBlockEntity(data.pos);
    final BlockState existing = serverWorld.getBlockState(data.pos);
    final SpongeBlockSnapshot original = TrackingUtil.createPooledSnapshot(existing, data.pos, BlockChangeFlags.NONE, Constants.World.DEFAULT_BLOCK_CHANGE_LIMIT, tileEntity, worldSupplier, Optional::empty, Optional::empty);
    original.blockChange = BlockChange.MODIFY;
    final ScheduleUpdateTransaction transaction = new ScheduleUpdateTransaction(original, data);
    this.logTransaction(transaction);
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) BlockState(net.minecraft.world.level.block.state.BlockState) Optional(java.util.Optional) ScheduleUpdateTransaction(org.spongepowered.common.event.tracking.context.transaction.block.ScheduleUpdateTransaction) WeakReference(java.lang.ref.WeakReference) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) RemoveBlockEntity(org.spongepowered.common.event.tracking.context.transaction.block.RemoveBlockEntity) ReplaceBlockEntity(org.spongepowered.common.event.tracking.context.transaction.block.ReplaceBlockEntity)

Example 25 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.

the class TrackingUtil method randomTickBlock.

@SuppressWarnings("rawtypes")
public static void randomTickBlock(final TrackedWorldBridge mixinWorld, final net.minecraft.world.level.block.state.BlockState state, final BlockPos pos, final Random random) {
    final ServerLevel world = (ServerLevel) mixinWorld;
    final org.spongepowered.api.world.server.ServerWorld apiWorld = (org.spongepowered.api.world.server.ServerWorld) world;
    if (ShouldFire.TICK_BLOCK_EVENT) {
        final BlockSnapshot currentTickBlock = mixinWorld.bridge$createSnapshot(state, pos, BlockChangeFlags.NONE);
        final TickBlockEvent event = SpongeEventFactory.createTickBlockEventRandom(PhaseTracker.getCauseStackManager().currentCause(), currentTickBlock);
        SpongeCommon.post(event);
        if (event.isCancelled()) {
            return;
        }
    }
    final LocatableBlock locatable = new SpongeLocatableBlockBuilder().world(apiWorld).position(pos.getX(), pos.getY(), pos.getZ()).state((BlockState) state).build();
    final BlockTickContext phaseContext = TickPhase.Tick.RANDOM_BLOCK.createPhaseContext(PhaseTracker.SERVER).source(locatable);
    // We have to associate any notifiers in case of scheduled block updates from other sources
    final PhaseContext<@NonNull ?> currentContext = PhaseTracker.getInstance().getPhaseContext();
    currentContext.appendNotifierPreBlockTick(world, pos, phaseContext);
    // Now actually switch to the new phase
    try (final PhaseContext<@NonNull ?> context = phaseContext) {
        context.buildAndSwitch();
        PhaseTracker.LOGGER.trace(TrackingUtil.BLOCK_TICK, "Wrapping Random Block Tick: {}", state);
        state.randomTick(world, pos, random);
    } catch (final Exception | NoClassDefFoundError e) {
        PhasePrinter.printExceptionFromPhase(PhaseTracker.getInstance().stack, e, phaseContext);
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) SpongeLocatableBlockBuilder(org.spongepowered.common.world.server.SpongeLocatableBlockBuilder) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) TickBlockEvent(org.spongepowered.api.event.block.TickBlockEvent) BlockState(org.spongepowered.api.block.BlockState) BlockTickContext(org.spongepowered.common.event.tracking.phase.tick.BlockTickContext) LocatableBlock(org.spongepowered.api.world.LocatableBlock)

Aggregations

ServerLevel (net.minecraft.server.level.ServerLevel)115 BlockPos (net.minecraft.core.BlockPos)30 Nullable (org.checkerframework.checker.nullness.qual.Nullable)21 Level (net.minecraft.world.level.Level)20 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)16 Entity (net.minecraft.world.entity.Entity)14 ServerPlayer (net.minecraft.server.level.ServerPlayer)11 ItemStack (net.minecraft.world.item.ItemStack)10 SpongeBlockSnapshot (org.spongepowered.common.block.SpongeBlockSnapshot)10 IOException (java.io.IOException)9 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)9 CraftWorld (org.bukkit.craftbukkit.v1_18_R1.CraftWorld)9 ArrayList (java.util.ArrayList)8 BlockState (net.minecraft.world.level.block.state.BlockState)8 List (java.util.List)7 Optional (java.util.Optional)7 UUID (java.util.UUID)7 Vec3 (net.minecraft.world.phys.Vec3)7 CraftWorld (org.bukkit.craftbukkit.v1_17_R1.CraftWorld)7 Cause (org.spongepowered.api.event.Cause)7