From c8b6f72f49f30eb490e7e71a7aa1e271cabc0426 Mon Sep 17 00:00:00 2001 From: sciwhiz12 Date: Fri, 17 Feb 2023 09:50:30 +0800 Subject: [PATCH 1/3] Add new dynamic versions table This makes use of JS and the new Parchment mappings version endpoint to dynamically list out the latest version for each available Minecraft version. The current implementation sorts the MC versions using the info from the launcher manifest, and falling back to a hardcoded array if that is not available. The current impl. also replaces the rows of the table with new rows, so that the original rows (which uses version badges) of the table remain if the script cannot access the versions endpoint. --- assets/js/versions-table.js | 86 +++++++++++++++++++++++++++++++++++++ docs/getting-started.md | 8 +++- 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 assets/js/versions-table.js diff --git a/assets/js/versions-table.js b/assets/js/versions-table.js new file mode 100644 index 0000000..4394826 --- /dev/null +++ b/assets/js/versions-table.js @@ -0,0 +1,86 @@ + +// URL to the Minecraft versions manfiest (JSON) +const mc_manifest_url = "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json" +// URL to the Parchment versions endpoint (JSON) +const parchment_versions_url = "https://versioning.parchmentmc.org/versions" + +// Fallback sort key array +// When the Minecraft versions manifest cannot be fetched to use as the sort key, this array is used instead +const fallbackSortKey = [ "1.19.3", "1.19.2", "1.18.2", "1.17.1", "1.16.5" ] + +// Element ID of the versions table +const tableId = "versions-table" + +// +// +// + +const table = document.getElementById(tableId) +if (table === null || table.tagName !== "TABLE") { + throw new Error("Couldn't find element with ID " + tableId + ": " + table) +} + +const manifestPromise = fetch(mc_manifest_url) + .then(r => { + if (!r.ok) throw new Error("Response not OK: ", r) + return r.json() + }) + .catch(err => { + console.error("Error trying to fetch Minecraft version manifest: ", err) + return null + }) + +fetch(parchment_versions_url) + .then(r => { + if (!r.ok) throw new Error("Response not OK: ", r) + return r.json() + }) + .then(versions => handleVersions(versions)) + .catch(err => console.error("Error trying to fetch Parchment mappings versions: ", err)) + +async function handleVersions(data) { + var sortKey; + + const manifest = await manifestPromise; + if (manifest !== null) sortKey = manifest.versions.map(v => v.id) + else { + console.log("Using fallback sort key array") + sortKey = fallbackSortKey + } + + const keys = Object.keys(data.releases) + keys.sort((a, b) => sortKey.indexOf(a) - sortKey.indexOf(b)) + + const tbody = table.getElementsByTagName('tbody')[0] + + const newRows = [] + for (const versionKey of keys) { + const mappingsVersion = data.releases[versionKey] + newRows.push(createRow(versionKey, mappingsVersion)) + } + tbody.replaceChildren(...newRows) +} + +function createRow(mcVersion, mappingsVersion) { + const row = document.createElement("tr") + + const mcVersionElem = document.createElement("td") + { + const mcVer = document.createElement("strong") + mcVer.textContent = mcVersion + mcVer.className = "mc-version" + mcVersionElem.appendChild(mcVer) + } + row.appendChild(mcVersionElem) + + const mappingsVersionElem = document.createElement("td") + { + const mappingsVer = document.createElement("code") + mappingsVer.textContent = mappingsVersion + mappingsVer.className = "mappings-version" + mappingsVersionElem.appendChild(mappingsVer) + } + row.appendChild(mappingsVersionElem) + + return row +} diff --git a/docs/getting-started.md b/docs/getting-started.md index 811075a..5f93bd0 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -1,6 +1,8 @@ --- layout: page title: Getting Started +js: + - /assets/js/versions-table.js --- ## Choose a version @@ -31,7 +33,7 @@ The latest version of the release export for a particular Minecraft version can | **1.18.2** | ![Latest release version badge for 1.18.2](https://img.shields.io/maven-metadata/v?color=forestgreen&label=release&metadataUrl=https%3A%2F%2Fldtteam.jfrog.io%2Fartifactory%2Fparchmentmc-internal%2Forg%2Fparchmentmc%2Fdata%2Fparchment-1.18.2%2Fmaven-metadata.xml) | | **1.17.1** | ![Latest release version badge for 1.17.1](https://img.shields.io/maven-metadata/v?color=forestgreen&label=release&metadataUrl=https%3A%2F%2Fldtteam.jfrog.io%2Fartifactory%2Fparchmentmc-internal%2Forg%2Fparchmentmc%2Fdata%2Fparchment-1.17.1%2Fmaven-metadata.xml) | | **1.16.5** | ![Latest release version badge for 1.16.5](https://img.shields.io/maven-metadata/v?color=forestgreen&label=release&metadataUrl=https%3A%2F%2Fldtteam.jfrog.io%2Fartifactory%2Fparchmentmc-internal%2Forg%2Fparchmentmc%2Fdata%2Fparchment-1.16.5%2Fmaven-metadata.xml) | -{:style="margin: auto"} +{:style="margin: auto" #versions-table} When selecting the Parchment mappings version from the version badges above or in the README, please remove the `v` prefix before inserting it into your buildscript. @@ -165,6 +167,10 @@ ParchmentMC provides the [**Librarian**](https://github.com/ParchmentMC/Libraria