package net.minecraft.server; import com.mojang.logging.LogUtils; import java.io.PrintStream; import java.time.Duration; import java.time.Instant; import java.util.Set; import java.util.TreeSet; import java.util.concurrent.atomic.AtomicLong; import java.util.function.Function; import java.util.function.Supplier; import net.minecraft.SharedConstants; import net.minecraft.SuppressForbidden; import net.minecraft.commands.Commands; import net.minecraft.commands.arguments.selector.options.EntitySelectorOptions; import net.minecraft.core.cauldron.CauldronInteractions; import net.minecraft.core.dispenser.DispenseItemBehavior; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.locale.Language; import net.minecraft.world.effect.MobEffect; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.EntityTypes; import net.minecraft.world.entity.ai.attributes.Attribute; import net.minecraft.world.entity.ai.attributes.DefaultAttributes; import net.minecraft.world.flag.FeatureFlags; import net.minecraft.world.item.CreativeModeTabs; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.ComposterBlock; import net.minecraft.world.level.block.FireBlock; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.gamerules.GameRule; import net.minecraft.world.level.gamerules.GameRuleTypeVisitor; import net.minecraft.world.level.gamerules.GameRules; import org.slf4j.Logger; @SuppressForbidden( reason = "System.out setup" ) public class Bootstrap { public static final PrintStream STDOUT = System.out; private static volatile boolean isBootstrapped; private static final Logger LOGGER = LogUtils.getLogger(); public static final AtomicLong bootstrapDuration = new AtomicLong(-1L); public static void bootStrap() { if (!isBootstrapped) { isBootstrapped = true; Instant start = Instant.now(); if (BuiltInRegistries.REGISTRY.keySet().isEmpty()) { throw new IllegalStateException("Unable to load registries"); } else { FireBlock.bootStrap(); ComposterBlock.bootStrap(); if (EntityType.getKey(EntityTypes.PLAYER) == null) { throw new IllegalStateException("Failed loading EntityTypes"); } else { EntitySelectorOptions.bootStrap(); DispenseItemBehavior.bootStrap(); CauldronInteractions.bootStrap(); BuiltInRegistries.bootStrap(); CreativeModeTabs.validate(); wrapStreams(); bootstrapDuration.set(Duration.between(start, Instant.now()).toMillis()); } } } } private static void checkTranslations( final Language language, final Iterable registry, final Function descriptionGetter, final Set output ) { registry.forEach(t -> { String id = (String)descriptionGetter.apply(t); if (!language.has(id)) { output.add(id); } }); } private static void checkGameruleTranslations(final Language language, final Set missing) { GameRules rules = new GameRules(FeatureFlags.REGISTRY.allFlags()); rules.visitGameRuleTypes(new GameRuleTypeVisitor() { @Override public void visit(final GameRule gameRule) { if (!language.has(gameRule.getDescriptionId())) { missing.add(gameRule.id()); } } }); } public static Set getMissingTranslations(final Language language) { Set missing = new TreeSet(); checkTranslations(language, BuiltInRegistries.ATTRIBUTE, Attribute::getDescriptionId, missing); checkTranslations(language, BuiltInRegistries.ENTITY_TYPE, EntityType::getDescriptionId, missing); checkTranslations(language, BuiltInRegistries.MOB_EFFECT, MobEffect::getDescriptionId, missing); checkTranslations(language, BuiltInRegistries.ITEM, Item::getDescriptionId, missing); checkTranslations(language, BuiltInRegistries.BLOCK, BlockBehaviour::getDescriptionId, missing); checkTranslations(language, BuiltInRegistries.CUSTOM_STAT, id -> "stat." + id.toString().replace(':', '.'), missing); checkGameruleTranslations(language, missing); return missing; } public static void checkBootstrapCalled(final Supplier location) { if (!isBootstrapped) { throw createBootstrapException(location); } } private static RuntimeException createBootstrapException(final Supplier location) { try { String resolvedLocation = (String)location.get(); return new IllegalArgumentException("Not bootstrapped (called from " + resolvedLocation + ")"); } catch (Exception var3) { RuntimeException result = new IllegalArgumentException("Not bootstrapped (failed to resolve location)"); result.addSuppressed(var3); return result; } } public static void validate() { checkBootstrapCalled(() -> "validate"); if (SharedConstants.IS_RUNNING_IN_IDE) { getMissingTranslations(Language.DEFAULT_INSTANCE).forEach(key -> LOGGER.error("Missing translations: {}", key)); Commands.validate(); } DefaultAttributes.validate(); } private static void wrapStreams() { if (LOGGER.isDebugEnabled()) { System.setErr(new DebugLoggedPrintStream("STDERR", System.err)); System.setOut(new DebugLoggedPrintStream("STDOUT", STDOUT)); } else { System.setErr(new LoggedPrintStream("STDERR", System.err)); System.setOut(new LoggedPrintStream("STDOUT", STDOUT)); } } public static void realStdoutPrintln(final String string) { STDOUT.println(string); } public static void shutdownStdout() { STDOUT.close(); } }