Search in sources :

Example 1 with MinecraftServerAccessor

use of org.spongepowered.common.accessor.server.MinecraftServerAccessor in project SpongeCommon by SpongePowered.

the class DifficultyCommandMixin method setDifficulty.

// @formatter:on
/**
 * @author Zidane
 * @reason Only apply difficulty for the world the command was ran in (as in Sponge, all worlds have difficulties)
 */
@Overwrite
public static int setDifficulty(CommandSourceStack source, Difficulty difficulty) throws CommandSyntaxException {
    if (source.getLevel().getDifficulty() == difficulty) {
        throw DifficultyCommandMixin.ERROR_ALREADY_DIFFICULT.create(difficulty.getKey());
    } else {
        final LevelData levelData = source.getLevel().getLevelData();
        ((ServerWorldProperties) levelData).setDifficulty((org.spongepowered.api.world.difficulty.Difficulty) (Object) difficulty);
        source.getLevel().setSpawnSettings(((MinecraftServerAccessor) SpongeCommon.server()).invoker$isSpawningMonsters(), SpongeCommon.server().isSpawningAnimals());
        source.getLevel().getPlayers(p -> true).forEach(p -> p.connection.send(new ClientboundChangeDifficultyPacket(levelData.getDifficulty(), levelData.isDifficultyLocked())));
        source.sendSuccess(new TranslatableComponent("commands.difficulty.success", difficulty.getDisplayName()), true);
        return 0;
    }
}
Also used : CommandSourceStack(net.minecraft.commands.CommandSourceStack) MinecraftServerAccessor(org.spongepowered.common.accessor.server.MinecraftServerAccessor) Overwrite(org.spongepowered.asm.mixin.Overwrite) SpongeCommon(org.spongepowered.common.SpongeCommon) Final(org.spongepowered.asm.mixin.Final) ServerWorldProperties(org.spongepowered.api.world.server.storage.ServerWorldProperties) LevelData(net.minecraft.world.level.storage.LevelData) Mixin(org.spongepowered.asm.mixin.Mixin) DynamicCommandExceptionType(com.mojang.brigadier.exceptions.DynamicCommandExceptionType) Shadow(org.spongepowered.asm.mixin.Shadow) ClientboundChangeDifficultyPacket(net.minecraft.network.protocol.game.ClientboundChangeDifficultyPacket) DifficultyCommand(net.minecraft.server.commands.DifficultyCommand) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException) Difficulty(net.minecraft.world.Difficulty) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) ServerWorldProperties(org.spongepowered.api.world.server.storage.ServerWorldProperties) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) ClientboundChangeDifficultyPacket(net.minecraft.network.protocol.game.ClientboundChangeDifficultyPacket) LevelData(net.minecraft.world.level.storage.LevelData) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 2 with MinecraftServerAccessor

use of org.spongepowered.common.accessor.server.MinecraftServerAccessor in project SpongeCommon by SpongePowered.

the class PrimaryLevelDataMixin method impl$updateWorldForDifficultyChange.

void impl$updateWorldForDifficultyChange(final ServerLevel world, final boolean isLocked) {
    if (world == null) {
        return;
    }
    final MinecraftServer server = world.getServer();
    final Difficulty difficulty = ((LevelData) this).getDifficulty();
    if (difficulty == Difficulty.HARD) {
        world.setSpawnSettings(true, true);
    } else if (server.isSingleplayer()) {
        world.setSpawnSettings(difficulty != Difficulty.PEACEFUL, true);
    } else {
        world.setSpawnSettings(((MinecraftServerAccessor) server).invoker$isSpawningMonsters(), server.isSpawningAnimals());
    }
    world.players().forEach(player -> player.connection.send(new ClientboundChangeDifficultyPacket(difficulty, isLocked)));
}
Also used : MinecraftServerAccessor(org.spongepowered.common.accessor.server.MinecraftServerAccessor) Difficulty(net.minecraft.world.Difficulty) ClientboundChangeDifficultyPacket(net.minecraft.network.protocol.game.ClientboundChangeDifficultyPacket) PrimaryLevelData(net.minecraft.world.level.storage.PrimaryLevelData) ServerLevelData(net.minecraft.world.level.storage.ServerLevelData) LevelData(net.minecraft.world.level.storage.LevelData) MinecraftServer(net.minecraft.server.MinecraftServer)

Example 3 with MinecraftServerAccessor

use of org.spongepowered.common.accessor.server.MinecraftServerAccessor in project SpongeCommon by SpongePowered.

the class SpongeUserData method create.

public static SpongeUserData create(final GameProfile profile) throws IOException {
    final ServerLevel world = SpongeCommon.server().overworld();
    if (world == null) {
        SpongeCommon.logger().warn("Overworld not initialized, cannot create users!");
        throw new IllegalStateException("Overworld not initialized, cannot create users!");
    }
    final LevelStorageSource.LevelStorageAccess storageSource = ((MinecraftServerAccessor) Sponge.server()).accessor$storageSource();
    final File file = storageSource.getLevelPath(LevelResource.PLAYER_DATA_DIR).resolve(profile.getId().toString() + ".dat").toFile();
    if (!file.exists()) {
        return new SpongeUserData(profile, new CompoundTag());
    }
    try {
        final CompoundTag compound;
        try (final FileInputStream in = new FileInputStream(file)) {
            compound = NbtIo.readCompressed(in);
        }
        // See PlayerDataAccess - keep this line up to date.
        final int version = compound.contains("DataVersion", 3) ? compound.getInt("DataVersion") : -1;
        NbtUtils.update(DataFixers.getDataFixer(), DataFixTypes.PLAYER, compound, version);
        return new SpongeUserData(profile, compound);
    } catch (final IOException e) {
        SpongeCommon.logger().warn("Unable to load corrupt user file '{}'!", file.toPath().relativize(Paths.get("")).toString(), e);
        FileUtil.copyCorruptedFile(file);
        throw e;
    }
}
Also used : MinecraftServerAccessor(org.spongepowered.common.accessor.server.MinecraftServerAccessor) ServerLevel(net.minecraft.server.level.ServerLevel) LevelStorageSource(net.minecraft.world.level.storage.LevelStorageSource) IOException(java.io.IOException) File(java.io.File) CompoundTag(net.minecraft.nbt.CompoundTag) FileInputStream(java.io.FileInputStream)

Example 4 with MinecraftServerAccessor

use of org.spongepowered.common.accessor.server.MinecraftServerAccessor in project SpongeCommon by SpongePowered.

the class SpongeWorldManager method prepareWorld.

private ServerLevel prepareWorld(final ServerLevel world, final boolean isDebugGeneration) {
    final boolean isDefaultWorld = Level.OVERWORLD.equals(world.dimension());
    final PrimaryLevelData levelData = (PrimaryLevelData) world.getLevelData();
    if (isDefaultWorld) {
        // Initialize scoreboard data. This will hook to the ServerScoreboard, needs to be made multi-world aware
        ((MinecraftServerAccessor) this.server).accessor$readScoreboard(world.getDataStorage());
        ((MinecraftServerAccessor) this.server).accessor$commandStorage(new CommandStorage(world.getDataStorage()));
    }
    final boolean isInitialized = levelData.isInitialized();
    SpongeCommon.post(SpongeEventFactory.createLoadWorldEvent(PhaseTracker.getCauseStackManager().currentCause(), (org.spongepowered.api.world.server.ServerWorld) world, isInitialized));
    PlatformHooks.INSTANCE.getWorldHooks().postLoadWorld(world);
    // Set the view distance back on it's self to trigger the logic
    ((PrimaryLevelDataBridge) world.getLevelData()).bridge$viewDistance().ifPresent(v -> ((PrimaryLevelDataBridge) world.getLevelData()).bridge$setViewDistance(v));
    world.getWorldBorder().applySettings(levelData.getWorldBorder());
    if (!isInitialized) {
        try {
            final boolean hasSpawnAlready = ((PrimaryLevelDataBridge) world.getLevelData()).bridge$customSpawnPosition();
            if (!hasSpawnAlready) {
                if (isDefaultWorld || ((ServerWorldProperties) world.getLevelData()).performsSpawnLogic()) {
                    MinecraftServerAccessor.invoker$setInitialSpawn(world, levelData, levelData.worldGenSettings().generateBonusChest(), isDebugGeneration, !isDebugGeneration);
                } else if (Level.END.equals(world.dimension())) {
                    ((PrimaryLevelData) world.getLevelData()).setSpawn(ServerLevel.END_SPAWN_POINT, 0);
                }
            } else {
                Features.BONUS_CHEST.place(world, world.getChunkSource().getGenerator(), world.random, new BlockPos(levelData.getXSpawn(), levelData.getYSpawn(), levelData.getZSpawn()));
            }
            levelData.setInitialized(true);
            if (isDebugGeneration) {
                ((MinecraftServerAccessor) this.server).invoker$setupDebugLevel(levelData);
            }
        } catch (final Throwable throwable) {
            final CrashReport crashReport = CrashReport.forThrowable(throwable, "Exception initializing world '" + world.dimension().location() + "'");
            try {
                world.fillReportDetails(crashReport);
            } catch (final Throwable ignore) {
            }
            throw new ReportedException(crashReport);
        }
        levelData.setInitialized(true);
    }
    // Initialize PlayerData in PlayerList, add WorldBorder listener. We change the method in PlayerList to handle per-world border
    this.server.getPlayerList().setLevel(world);
    if (levelData.getCustomBossEvents() != null) {
        ((ServerLevelBridge) world).bridge$getBossBarManager().load(levelData.getCustomBossEvents());
    }
    return world;
}
Also used : PrimaryLevelData(net.minecraft.world.level.storage.PrimaryLevelData) MinecraftServerAccessor(org.spongepowered.common.accessor.server.MinecraftServerAccessor) CrashReport(net.minecraft.CrashReport) CommandStorage(net.minecraft.world.level.storage.CommandStorage) BlockPos(net.minecraft.core.BlockPos) PrimaryLevelDataBridge(org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge) ReportedException(net.minecraft.ReportedException)

Example 5 with MinecraftServerAccessor

use of org.spongepowered.common.accessor.server.MinecraftServerAccessor in project SpongeCommon by SpongePowered.

the class SpongeUserData method save.

public void save() throws IOException {
    synchronized (this) {
        final SpongeUserManager userManager = ((SpongeServer) SpongeCommon.server()).userManager();
        final LevelStorageSource.LevelStorageAccess storageSource = ((MinecraftServerAccessor) Sponge.server()).accessor$storageSource();
        final File file = storageSource.getLevelPath(LevelResource.PLAYER_DATA_DIR).resolve(this.uniqueId() + ".dat").toFile();
        this.writeCompound(this.compound);
        try (final FileOutputStream out = new FileOutputStream(file)) {
            NbtIo.writeCompressed(this.compound, out);
            userManager.unmarkDirty(this);
        } catch (final IOException e) {
            // We log the message here because the error may be swallowed by a completable future.
            SpongeCommon.logger().warn("Failed to save user file [{}]!", file, e);
            throw e;
        }
    }
}
Also used : MinecraftServerAccessor(org.spongepowered.common.accessor.server.MinecraftServerAccessor) SpongeServer(org.spongepowered.common.SpongeServer) FileOutputStream(java.io.FileOutputStream) LevelStorageSource(net.minecraft.world.level.storage.LevelStorageSource) IOException(java.io.IOException) File(java.io.File) SpongeUserManager(org.spongepowered.common.user.SpongeUserManager)

Aggregations

MinecraftServerAccessor (org.spongepowered.common.accessor.server.MinecraftServerAccessor)8 IOException (java.io.IOException)4 Difficulty (net.minecraft.world.Difficulty)4 LevelStorageSource (net.minecraft.world.level.storage.LevelStorageSource)4 PrimaryLevelData (net.minecraft.world.level.storage.PrimaryLevelData)4 File (java.io.File)3 BlockPos (net.minecraft.core.BlockPos)3 ServerLevel (net.minecraft.server.level.ServerLevel)3 ChunkProgressListener (net.minecraft.server.level.progress.ChunkProgressListener)3 PrimaryLevelDataBridge (org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge)3 SpongeUserManager (org.spongepowered.common.user.SpongeUserManager)3 JsonObject (com.google.gson.JsonObject)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Tag (net.minecraft.nbt.Tag)2 ClientboundChangeDifficultyPacket (net.minecraft.network.protocol.game.ClientboundChangeDifficultyPacket)2 ServerChunkCache (net.minecraft.server.level.ServerChunkCache)2 VillageSiege (net.minecraft.world.entity.ai.village.VillageSiege)2 CatSpawner (net.minecraft.world.entity.npc.CatSpawner)2 WanderingTraderSpawner (net.minecraft.world.entity.npc.WanderingTraderSpawner)2