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);
}
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);
}
}
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;
}
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);
}
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);
}
}
Aggregations