| 1 | package net.minecraft.server;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.io.PrintStream;
|
| 5 | import java.time.Duration;
|
| 6 | import java.time.Instant;
|
| 7 | import java.util.Set;
|
| 8 | import java.util.TreeSet;
|
| 9 | import java.util.concurrent.atomic.AtomicLong;
|
| 10 | import java.util.function.Function;
|
| 11 | import java.util.function.Supplier;
|
| 12 | import net.minecraft.SharedConstants;
|
| 13 | import net.minecraft.SuppressForbidden;
|
| 14 | import net.minecraft.commands.Commands;
|
| 15 | import net.minecraft.commands.arguments.selector.options.EntitySelectorOptions;
|
| 16 | import net.minecraft.core.cauldron.CauldronInteractions;
|
| 17 | import net.minecraft.core.dispenser.DispenseItemBehavior;
|
| 18 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 19 | import net.minecraft.locale.Language;
|
| 20 | import net.minecraft.world.effect.MobEffect;
|
| 21 | import net.minecraft.world.entity.EntityType;
|
| 22 | import net.minecraft.world.entity.EntityTypes;
|
| 23 | import net.minecraft.world.entity.ai.attributes.Attribute;
|
| 24 | import net.minecraft.world.entity.ai.attributes.DefaultAttributes;
|
| 25 | import net.minecraft.world.flag.FeatureFlags;
|
| 26 | import net.minecraft.world.item.CreativeModeTabs;
|
| 27 | import net.minecraft.world.item.Item;
|
| 28 | import net.minecraft.world.level.block.FireBlock;
|
| 29 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 30 | import net.minecraft.world.level.gamerules.GameRule;
|
| 31 | import net.minecraft.world.level.gamerules.GameRuleTypeVisitor;
|
| 32 | import net.minecraft.world.level.gamerules.GameRules;
|
| 33 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 34 | import org.slf4j.Logger;
|
| 35 |
|
| 36 | @SuppressForbidden(reason = "System.out setup")
|
| 37 | public class Bootstrap {
|
| 38 | public static final PrintStream STDOUT = System.out;
|
| 39 | private static volatile boolean isBootstrapped;
|
| 40 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 41 | public static final AtomicLong bootstrapDuration = new AtomicLong(-1L);
|
| 42 |
|
| 43 | public static void bootStrap() {
|
| 44 | if (!isBootstrapped) {
|
| 45 | isBootstrapped = true;
|
| 46 | Instant start = Instant.now();
|
| 47 | if (BuiltInRegistries.REGISTRY.keySet().isEmpty()) {
|
| 48 | throw new IllegalStateException("Unable to load registries");
|
| 49 | }
|
| 50 |
|
| 51 | FireBlock.bootStrap();
|
| 52 | if (EntityType.getKey(EntityTypes.PLAYER) == null) {
|
| 53 | throw new IllegalStateException("Failed loading EntityTypes");
|
| 54 | }
|
| 55 |
|
| 56 | EntitySelectorOptions.bootStrap();
|
| 57 | DispenseItemBehavior.bootStrap();
|
| 58 | CauldronInteractions.bootStrap();
|
| 59 | BuiltInRegistries.bootStrap();
|
| 60 | CreativeModeTabs.validate();
|
| 61 | LootContextParamSets.validate();
|
| 62 | wrapStreams();
|
| 63 | bootstrapDuration.set(Duration.between(start, Instant.now()).toMillis());
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | private static <T> void checkTranslations(
|
| 68 | final Language language, final Iterable<T> registry, final Function<T, String> descriptionGetter, final Set<String> output
|
| 69 | ) {
|
| 70 | registry.forEach(t -> {
|
| 71 | String id = descriptionGetter.apply((T)t);
|
| 72 | if (!language.has(id)) {
|
| 73 | output.add(id);
|
| 74 | }
|
| 75 | });
|
| 76 | }
|
| 77 |
|
| 78 | private static void checkGameruleTranslations(final Language language, final Set<String> missing) {
|
| 79 | GameRules rules = new GameRules(FeatureFlags.REGISTRY.allFlags());
|
| 80 | rules.visitGameRuleTypes(new GameRuleTypeVisitor() {
|
| 81 | @Override
|
| 82 | public <T> void visit(final GameRule<T> gameRule) {
|
| 83 | if (!language.has(gameRule.getDescriptionId())) {
|
| 84 | missing.add(gameRule.id());
|
| 85 | }
|
| 86 | }
|
| 87 | });
|
| 88 | }
|
| 89 |
|
| 90 | public static Set<String> getMissingTranslations(final Language language) {
|
| 91 | Set<String> missing = new TreeSet<>();
|
| 92 | checkTranslations(language, BuiltInRegistries.ATTRIBUTE, Attribute::getDescriptionId, missing);
|
| 93 | checkTranslations(language, BuiltInRegistries.ENTITY_TYPE, EntityType::getDescriptionId, missing);
|
| 94 | checkTranslations(language, BuiltInRegistries.MOB_EFFECT, MobEffect::getDescriptionId, missing);
|
| 95 | checkTranslations(language, BuiltInRegistries.ITEM, Item::getDescriptionId, missing);
|
| 96 | checkTranslations(language, BuiltInRegistries.BLOCK, BlockBehaviour::getDescriptionId, missing);
|
| 97 | checkTranslations(language, BuiltInRegistries.CUSTOM_STAT, id -> "stat." + id.toString().replace(':', '.'), missing);
|
| 98 | checkGameruleTranslations(language, missing);
|
| 99 | return missing;
|
| 100 | }
|
| 101 |
|
| 102 | public static void checkBootstrapCalled(final Supplier<String> location) {
|
| 103 | if (!isBootstrapped) {
|
| 104 | throw createBootstrapException(location);
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | private static RuntimeException createBootstrapException(final Supplier<String> location) {
|
| 109 | try {
|
| 110 | String resolvedLocation = location.get();
|
| 111 | return new IllegalArgumentException("Not bootstrapped (called from " + resolvedLocation + ")");
|
| 112 | } catch (Exception e) {
|
| 113 | RuntimeException result = new IllegalArgumentException("Not bootstrapped (failed to resolve location)");
|
| 114 | result.addSuppressed(e);
|
| 115 | return result;
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | public static void validate() {
|
| 120 | checkBootstrapCalled(() -> "validate");
|
| 121 | if (SharedConstants.IS_RUNNING_IN_IDE) {
|
| 122 | getMissingTranslations(Language.DEFAULT_INSTANCE).forEach(key -> LOGGER.error("Missing translations: {}", key));
|
| 123 | Commands.validate();
|
| 124 | }
|
| 125 |
|
| 126 | DefaultAttributes.validate();
|
| 127 | }
|
| 128 |
|
| 129 | private static void wrapStreams() {
|
| 130 | if (LOGGER.isDebugEnabled()) {
|
| 131 | System.setErr(new DebugLoggedPrintStream("STDERR", System.err));
|
| 132 | System.setOut(new DebugLoggedPrintStream("STDOUT", STDOUT));
|
| 133 | } else {
|
| 134 | System.setErr(new LoggedPrintStream("STDERR", System.err));
|
| 135 | System.setOut(new LoggedPrintStream("STDOUT", STDOUT));
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | public static void realStdoutPrintln(final String string) {
|
| 140 | STDOUT.println(string);
|
| 141 | }
|
| 142 |
|
| 143 | public static void shutdownStdout() {
|
| 144 | STDOUT.close();
|
| 145 | }
|
| 146 | }
|
| 147 |
|