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