--- a/net/minecraft/server/MinecraftServer.java +++ b/net/minecraft/server/MinecraftServer.java @@ -281,7 +_,7 @@ public static S spin(Function p_129873_) { AtomicReference atomicreference = new AtomicReference<>(); - Thread thread = new Thread(() -> atomicreference.get().runServer(), "Server thread"); + Thread thread = new Thread(net.minecraftforge.fml.util.thread.SidedThreadGroups.SERVER, () -> atomicreference.get().runServer(), "Server thread"); thread.setUncaughtExceptionHandler((p_177909_, p_177910_) -> LOGGER.error("Uncaught exception in server thread", p_177910_)); if (Runtime.getRuntime().availableProcessors() > 4) { thread.setPriority(8); @@ -408,6 +_,7 @@ DimensionDataStorage dimensiondatastorage = serverlevel.getDataStorage(); this.readScoreboard(dimensiondatastorage); this.commandStorage = new CommandStorage(dimensiondatastorage); + net.minecraftforge.event.ForgeEventFactory.onLevelLoad(levels.get(Level.OVERWORLD)); if (!serverleveldata.isInitialized()) { try { setInitialSpawn(serverlevel, serverleveldata, worldoptions.generateBonusChest(), flag, this.levelLoadListener); @@ -448,6 +_,7 @@ this, this.executor, this.storageSource, derivedleveldata, resourcekey1, entry.getValue(), flag, j, ImmutableList.of(), false, randomsequences ); this.levels.put(resourcekey1, serverlevel1); + net.minecraftforge.event.ForgeEventFactory.onLevelLoad(serverlevel1); } else { serverlevel1 = serverlevel; } @@ -491,6 +_,7 @@ p_177898_.setSpawn(LevelData.RespawnData.of(p_177897_.dimension(), BlockPos.ZERO.above(80), 0.0F, 0.0F)); } else { ServerChunkCache serverchunkcache = p_177897_.getChunkSource(); + if (net.minecraftforge.event.ForgeEventFactory.onCreateWorldSpawn(p_177897_, p_177898_)) return; ChunkPos chunkpos = new ChunkPos(serverchunkcache.randomState().sampler().findSpawnPosition()); p_431711_.start(LevelLoadListener.Stage.PREPARE_GLOBAL_SPAWN, 0); p_431711_.updateFocus(p_177897_.dimension(), chunkpos); @@ -675,6 +_,7 @@ for (ServerLevel serverlevel2 : this.getAllLevels()) { if (serverlevel2 != null) { try { + net.minecraftforge.event.ForgeEventFactory.onLevelUnload(serverlevel2); serverlevel2.close(); } catch (IOException ioexception1) { LOGGER.error("Exception closing the level", (Throwable)ioexception1); @@ -721,9 +_,11 @@ throw new IllegalStateException("Failed to initialize server"); } + net.minecraftforge.server.ServerLifecycleHooks.handleServerStarted(this); this.nextTickTimeNanos = Util.getNanos(); this.statusIcon = this.loadStatusIcon().orElse(null); this.status = this.buildServerStatus(); + resetStatusCache(status); while (this.running) { long i; @@ -778,6 +_,8 @@ this.isReady = true; JvmProfiler.INSTANCE.onServerTick(this.smoothedTickTimeMillis); } + net.minecraftforge.server.ServerLifecycleHooks.handleServerStopping(this); + net.minecraftforge.server.ServerLifecycleHooks.expectServerStopped(); // Forge: Has to come before MinecraftServer#onServerCrash to avoid race conditions } catch (Throwable throwable2) { LOGGER.error("Encountered an unexpected exception", throwable2); CrashReport crashreport = constructOrExtractCrashReport(throwable2); @@ -789,6 +_,7 @@ LOGGER.error("We were unable to save this crash report to disk."); } + net.minecraftforge.server.ServerLifecycleHooks.expectServerStopped(); // Forge: Has to come before MinecraftServer#onServerCrash to avoid race conditions this.onServerCrash(crashreport); } finally { try { @@ -797,6 +_,7 @@ } catch (Throwable throwable) { LOGGER.error("Exception stopping the server", throwable); } finally { + net.minecraftforge.server.ServerLifecycleHooks.handleServerStopped(this); this.onServerExit(); } } @@ -994,12 +_,14 @@ } } + net.minecraftforge.event.ForgeEventFactory.onPreServerTick(p_129871_, this); this.tickCount++; this.tickRateManager.tick(); this.tickChildren(p_129871_); if (i - this.lastServerStatus >= STATUS_EXPIRE_TIME_NANOS) { this.lastServerStatus = i; this.status = this.buildServerStatus(); + resetStatusCache(status); } this.ticksUntilAutosave--; @@ -1017,6 +_,7 @@ this.smoothedTickTimeMillis = this.smoothedTickTimeMillis * 0.8F + (float)k / (float)TimeUtil.NANOSECONDS_PER_MILLISECOND * 0.19999999F; this.logTickMethodTime(i); profilerfiller.pop(); + net.minecraftforge.event.ForgeEventFactory.onPostServerTick(p_129871_, this); } private void autoSave() { @@ -1066,7 +_,8 @@ Optional.of(serverstatus$players), Optional.of(ServerStatus.Version.current()), Optional.ofNullable(this.statusIcon), - this.enforceSecureProfile() + this.enforceSecureProfile(), + Optional.of(new net.minecraftforge.network.ServerStatusPing()) ); } @@ -1098,7 +_,8 @@ profilerfiller.popPush("levels"); this.updateEffectiveRespawnData(); - for (ServerLevel serverlevel : this.getAllLevels()) { + for (ServerLevel serverlevel : this.getWorldArray()) { + long tickStart = Util.getNanos(); profilerfiller.push(() -> serverlevel + " " + serverlevel.dimension().location()); if (this.tickCount % 20 == 0) { profilerfiller.push("timeSync"); @@ -1107,6 +_,7 @@ } profilerfiller.push("tick"); + net.minecraftforge.event.ForgeEventFactory.onPreLevelTick(serverlevel, p_129954_); try { serverlevel.tick(p_129954_); @@ -1115,9 +_,11 @@ serverlevel.fillReportDetails(crashreport); throw new ReportedException(crashreport); } + net.minecraftforge.event.ForgeEventFactory.onPostLevelTick(serverlevel, p_129954_); profilerfiller.pop(); profilerfiller.pop(); + perWorldTickTimes.computeIfAbsent(serverlevel.dimension(), k -> new long[100])[this.tickCount % 100] = Util.getNanos() - tickStart; } profilerfiller.popPush("connection"); @@ -1126,7 +_,7 @@ this.playerList.tick(); profilerfiller.popPush("debugSubscribers"); this.debugSubscribers.tick(); - if (this.tickRateManager.runsNormally()) { + if (net.minecraftforge.gametest.ForgeGameTestHooks.isGametestEnabled() && this.tickRateManager.runsNormally()) { profilerfiller.popPush("gameTests"); GameTestTicker.SINGLETON.tick(); } @@ -1229,7 +_,7 @@ @DontObfuscate public String getServerModName() { - return "vanilla"; + return net.minecraftforge.internal.BrandingControl.getBranding(); } public SystemReport fillSystemReport(SystemReport p_177936_) { @@ -1579,6 +_,7 @@ this.functionManager.replaceLibrary(this.resources.managers.getFunctionLibrary()); this.structureTemplateManager.onResourceManagerReload(this.resources.resourceManager); this.fuelValues = FuelValues.vanillaBurnTimes(this.registries.compositeAccess(), this.worldData.enabledFeatures()); + this.getPlayerList().getPlayers().forEach(this.getPlayerList()::sendPlayerPermissionLevel); //Forge: Fix newly added/modified commands not being sent to the client when commands reload. }, this); if (this.isSameThread()) { this.managedBlock(completablefuture::isDone); @@ -1588,12 +_,15 @@ } public static WorldDataConfiguration configurePackRepository(PackRepository p_248681_, WorldDataConfiguration p_331931_, boolean p_249869_, boolean p_330480_) { + net.minecraftforge.resource.ResourcePackLoader.loadResourcePacks(p_248681_, false); DataPackConfig datapackconfig = p_331931_.dataPacks(); FeatureFlagSet featureflagset = p_249869_ ? FeatureFlagSet.of() : p_331931_.enabledFeatures(); FeatureFlagSet featureflagset1 = p_249869_ ? FeatureFlags.REGISTRY.allFlags() : p_331931_.enabledFeatures(); p_248681_.reload(); + DataPackConfig.DEFAULT.addModPacks(net.minecraftforge.common.ForgeHooks.getModPacks()); + datapackconfig.addModPacks(net.minecraftforge.common.ForgeHooks.getModPacks()); if (p_330480_) { - return configureRepositoryWithSelection(p_248681_, List.of("vanilla"), featureflagset, false); + return configureRepositoryWithSelection(p_248681_, net.minecraftforge.common.ForgeHooks.getModPacksWithVanilla(), featureflagset, false); } else { Set set = Sets.newLinkedHashSet(); @@ -2158,6 +_,48 @@ public ServerLinks serverLinks() { return ServerLinks.EMPTY; + } + + private static final com.google.gson.Gson GSON = new com.google.gson.Gson(); + private String cachedServerStatus; // FORGE: cache the server status json in case a client spams requests + private void resetStatusCache(ServerStatus status) { + this.cachedServerStatus = GSON.toJson(ServerStatus.CODEC.encodeStart(com.mojang.serialization.JsonOps.INSTANCE, status).result().orElseThrow()); + } + + public String getStatusJson() { + return cachedServerStatus; + } + + private Map, long[]> perWorldTickTimes = Maps.newIdentityHashMap(); + @Nullable + public long[] getTickTime(ResourceKey dim) { + return perWorldTickTimes.get(dim); + } + + @Deprecated //Forge Internal use Only, You can screw up a lot of things if you mess with this map. + public synchronized Map, ServerLevel> forgeGetWorldMap() { + return this.levels; + } + + private int worldArrayMarker = 0; + private int worldArrayLast = -1; + private ServerLevel[] worldArray; + + @Deprecated //Forge Internal use Only, use to protect against concurrent modifications in the world tick loop. + public synchronized void markWorldsDirty() { + worldArrayMarker++; + } + + private ServerLevel[] getWorldArray() { + if (worldArrayMarker == worldArrayLast && worldArray != null) + return worldArray; + worldArray = this.levels.values().stream().toArray(ServerLevel[]::new); + worldArrayLast = worldArrayMarker; + return worldArray; + } + + public MinecraftServer.ReloadableResources getServerResources() { + return resources; } protected int pauseWhenEmptySeconds() {