Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dynamic version support using misode/mcmeta #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/lib/globals/versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
type MinecraftVersion = {
id: string;
name: string;
release_target: null | string;
type: string;
stable: boolean;
data_version: number;
protocol_version: number;
data_pack_version: number;
resource_pack_version: number;
build_time: string;
release_time: string;
sha1: string;
};

const response = await fetch(
"https://raw.githubusercontent.com/misode/mcmeta/summary/versions/data.min.json"
);
const minecraftVersionData: MinecraftVersion[] = await response.json();

const minecraftVersions: { format: number, label: string }[] = [];

let isNewSnapshot: boolean = true;
for (
let index = minecraftVersionData[0].data_pack_version;
index > 0;
index--
) {
const mcVs: MinecraftVersion[] = minecraftVersionData.filter(
(version: MinecraftVersion) => version.data_pack_version === index
);
if (mcVs.length === 0) {
continue;
}
const stableMcVs: MinecraftVersion[] = mcVs.filter(
(version: MinecraftVersion) => version.stable
);
if (stableMcVs.length === 1) {
minecraftVersions.push({ format: index, label: stableMcVs[0].id });
isNewSnapshot = false;
} else if (stableMcVs.length > 1) {
minecraftVersions.push({ format: index, label: `${stableMcVs.at(-1)?.id} - ${stableMcVs[0].id}` });
isNewSnapshot = false;
} else if (isNewSnapshot && mcVs.length === 1) {
minecraftVersions.push({ format: index, label: mcVs[0].id});
} else if (isNewSnapshot && mcVs.length > 1) {
minecraftVersions.push({ format: index, label: `${mcVs.at(-1)?.id} - ${mcVs[0].id}`});
}
}

export const getDataPackVersion = (version: string): number => {
for (const dpv of minecraftVersions) {
if (dpv.label === version) {
return dpv.format;
}
}
throw new Error(`Unknown version ${version}`);
};

export const getAllReleases = (format: number, stableOnly: boolean): string[] => {
const mcVs: MinecraftVersion[] = minecraftVersionData.filter(
(version: MinecraftVersion) => version.data_pack_version === format && (stableOnly ? version.stable : true)
);
return mcVs.map((version: MinecraftVersion) => version.id);
}

export { minecraftVersions };
31 changes: 10 additions & 21 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import IconPlus from '~icons/tabler/Plus.svelte';
import IconUpload from '~icons/tabler/Upload.svelte';
import IconVersions from '~icons/tabler/Versions.svelte';
import { minecraftVersions, getDataPackVersion, getAllReleases } from '$lib/globals/versions';

let borderStyle = "border-zinc-800"
let is_drag =false
Expand Down Expand Up @@ -386,7 +387,7 @@
name: title,
version_number: versionCode,
changelog: changelog,
game_versions: mcVersions,
game_versions: generate_modrinth_versions(mcVersions),
loaders: ["datapack"],
featured: true,
project_id: selectedModPack,
Expand Down Expand Up @@ -484,28 +485,16 @@
dpfile = dpFileInput.files?.item(0);
}

const minecraftVersions = [
{"format":4,"label":"1.13"},
{"format":5,"label":"1.15"},
{"format":6,"label":"1.16.2"},
{"format":7,"label":"1.17"},
{"format":8,"label":"1.18"},
{"format":9,"label":"1.18.2"},
{"format":10,"label":"1.19"},
{"format":12,"label":"1.19.4"},
{"format":15,"label":"1.20"},
{"format":18,"label":"1.20.2"},
{"format":26,"label":"1.20.3"},
];

const generate_datapackhub_versions = function(selected: Array<string>) {return selected.map(i => {if(i == "1.17") {return "1.17.x"} else return i })}
const generate_datapackhub_versions = function(selected: Array<string>) {
return selected.map(i => getDataPackVersion(i));
}

const generate_smithed_versions = function(selected: Array<string>) {
let temp = selected
if(temp.includes("1.17")) temp.push("1.17.1")
if(temp.includes("1.18")) temp.push("1.18.1")
if(temp.includes("1.20")) temp.push("1.20.1")
return temp
return selected.flatMap(i => getAllReleases(getDataPackVersion(i), true));
}

const generate_modrinth_versions = function(selected: Array<string>) {
return selected.flatMap(i => getAllReleases(getDataPackVersion(i), false));
}
</script>

Expand Down