| 1 | package net.minecraft;
|
| 2 |
|
| 3 | import com.google.gson.JsonObject;
|
| 4 | import com.google.gson.JsonParseException;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.io.IOException;
|
| 7 | import java.io.InputStream;
|
| 8 | import java.io.InputStreamReader;
|
| 9 | import java.nio.charset.StandardCharsets;
|
| 10 | import java.time.ZonedDateTime;
|
| 11 | import java.util.Date;
|
| 12 | import java.util.UUID;
|
| 13 | import net.minecraft.server.packs.metadata.pack.PackFormat;
|
| 14 | import net.minecraft.util.GsonHelper;
|
| 15 | import net.minecraft.world.level.storage.DataVersion;
|
| 16 | import org.slf4j.Logger;
|
| 17 |
|
| 18 | public class DetectedVersion {
|
| 19 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 20 | public static final WorldVersion BUILT_IN = createBuiltIn(UUID.randomUUID().toString().replaceAll("-", ""), "Development Version");
|
| 21 |
|
| 22 | public static WorldVersion createBuiltIn(final String id, final String name) {
|
| 23 | return createBuiltIn(id, name, true);
|
| 24 | }
|
| 25 |
|
| 26 | public static WorldVersion createBuiltIn(final String id, final String name, final boolean stable) {
|
| 27 | return new WorldVersion.Simple(
|
| 28 | id, name, new DataVersion(5023, "main"), SharedConstants.getProtocolVersion(), PackFormat.of(97, 1), PackFormat.of(121, 0), new Date(), stable
|
| 29 | );
|
| 30 | }
|
| 31 |
|
| 32 | private static WorldVersion createFromJson(final JsonObject root) {
|
| 33 | JsonObject packVersion = GsonHelper.getAsJsonObject(root, "pack_version");
|
| 34 | return new WorldVersion.Simple(
|
| 35 | GsonHelper.getAsString(root, "id"),
|
| 36 | GsonHelper.getAsString(root, "name"),
|
| 37 | new DataVersion(GsonHelper.getAsInt(root, "world_version"), GsonHelper.getAsString(root, "series_id", "main")),
|
| 38 | GsonHelper.getAsInt(root, "protocol_version"),
|
| 39 | PackFormat.of(GsonHelper.getAsInt(packVersion, "resource_major"), GsonHelper.getAsInt(packVersion, "resource_minor")),
|
| 40 | PackFormat.of(GsonHelper.getAsInt(packVersion, "data_major"), GsonHelper.getAsInt(packVersion, "data_minor")),
|
| 41 | Date.from(ZonedDateTime.parse(GsonHelper.getAsString(root, "build_time")).toInstant()),
|
| 42 | GsonHelper.getAsBoolean(root, "stable")
|
| 43 | );
|
| 44 | }
|
| 45 |
|
| 46 | public static WorldVersion tryDetectVersion() {
|
| 47 | try (InputStream stream = DetectedVersion.class.getResourceAsStream("/version.json")) {
|
| 48 | if (stream == null) {
|
| 49 | LOGGER.warn("Missing version information!");
|
| 50 | return BUILT_IN;
|
| 51 | }
|
| 52 |
|
| 53 | try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
|
| 54 | return createFromJson(GsonHelper.parse(reader));
|
| 55 | }
|
| 56 | } catch (IOException | JsonParseException e) {
|
| 57 | throw new IllegalStateException("Game version information is corrupt", e);
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 |
|