frogmc.dev/template.md

279 lines
9.6 KiB
Markdown

---
title: Mod Template Generator
sidebar: false
prev: false
next: false
editLink: false
lastUpdated: false
outline: false
---
<!-- This is a mess! Someone improve this please. -->
<script setup>
import JSZip from 'jszip'
import { ref, onMounted } from 'vue'
import { saveAs } from 'file-saver'
const assetBaseUrl = "/resources/template"
const showSnapshots = ref()
const versionSelector = ref()
const modIdRef = ref()
const modNameRef = ref()
const mavenGroupRef = ref()
const licenseRef = ref()
const useSnapshots = ref()
let releases = []
let snapshots = []
let versions = []
let latestLoader
let latestPhytotelma
let latestLoaderSnapshot
let latestPhytotelmaSnapshot
async function getGameVersions() {
const response = await fetch("https://piston-meta.mojang.com/mc/game/version_manifest_v2.json")
return await response.json()
}
function getVersions() {
versionSelector.value.innerHTML = ""
for (let version of releases) {
versionSelector.value.innerHTML += "<option value=\""+version.id+"\">"+version.id+"</option>"
}
if (showSnapshots.value) {
for (let version of snapshots) {
versionSelector.value.innerHTML += "<option value=\""+version.id+"\">"+version.id+"</option>"
}
}
}
onMounted(() => {
getGameVersions().then((v) => {
versions = v.versions
for (let version of versions){
if (version.type === "snapshot"){
snapshots.push(version)
} else {
releases.push(version)
}
}
getVersions()
})
getLatestProjectVersions()
})
function getLatestProjectVersions() {
fetchVersionFromMaven("releases", "frogloader").then((v) => {
latestLoader = v
document.getElementById("frogLoaderVersionReleaseBadge").innerHTML = v
}).catch((err) => {
document.getElementById("frogLoaderVersionReleaseBadge").innerHTML = "No version found!"
})
fetchVersionFromMaven("releases", "phytotelma").then((v) => {
latestPhytotelma = v
document.getElementById("phytotelmaVersionReleaseBadge").innerHTML = v
}).catch((err) => {
document.getElementById("phytotelmaVersionReleaseBadge").innerHTML = "No version found!"
})
fetchVersionFromMaven("snapshots", "frogloader").then((v) => {
latestLoaderSnapshot = v
document.getElementById("frogLoaderVersionSnapshotBadge").innerHTML = v
})
fetchVersionFromMaven("snapshots", "phytotelma").then((v) => {
latestPhytotelmaSnapshot = v
document.getElementById("phytotelmaVersionSnapshotBadge").innerHTML = v
})
}
async function fetchVersionFromMaven(repository, name) {
const response = await fetch("https://maven.frogmc.dev/"+repository+"/dev/frogmc/"+name+"/maven-metadata.xml")
const text = await response.text()
const parser = new DOMParser()
const doc = parser.parseFromString(text, "text/xml")
return doc.getElementsByTagName("latest")[0].textContent
}
function generate() {
let missing = []
if (modIdRef.value === undefined) {
missing.push("Mod ID")
}
if (modNameRef.value === undefined) {
missing.push("Mod Name")
}
if (mavenGroupRef.value === undefined) {
missing.push("Maven Group")
}
if (licenseRef.value === undefined) {
missing.push("License")
}
if (missing.length > 0) {
let html = "<div class=\"danger custom-block\"><p class=\"custom-block-title\">Missing Inputs</p><p>"
html += missing.join("</p><p>")
html += "</p></div>"
document.getElementById("missingBox").innerHTML = html
return
}
let zip = JSZip()
let gradle = zip.folder("gradle")
let wrapper = gradle.folder("wrapper")
let promises = []
promises.push(
readAsset("/gradle/libs.versions.toml").then((data) => {
gradle.file("libs.versions.toml", data)
})
)
promises.push(
readAssetBinary("/gradle/wrapper/gradle-wrapper.properties").then((data) => {
wrapper.file("gradle-wrapper.properties", data)
})
)
promises.push(
readAssetBinary("/gradle/wrapper/gradle-wrapper.jar").then((data) => {
wrapper.file("gradle-wrapper.jar", data)
})
)
promises.push(
readAsset("/.gitignore").then((data) => {
zip.file(".gitignore", data)
})
)
promises.push(
readAsset("/build.gradle.kts").then((data) => {
zip.file("build.gradle.kts", data)
})
)
promises.push(
readAssetBinary("/gradlew.sh").then((data) => {
zip.file("gradlew", data)
})
)
promises.push(
readAssetBinary("/gradlew.bat").then((data) => {
zip.file("gradlew.bat", data)
})
)
promises.push(
readAsset("/build.gradle.kts").then((data) => {
zip.file("build.gradle.kts", data)
})
)
promises.push(
readAsset("/settings.gradle.kts").then((data) => {
zip.file("settings.gradle.kts", data)
})
)
let src = zip.folder("src")
let main = src.folder("main")
let java = main.folder("java")
let resources = main.folder("resources")
let classes = java
for (let part of mavenGroupRef.value.split(/\\./g)) {
classes = classes.folder(part)
}
const classFileName = modNameRef.value.replaceAll(" ", "")
promises.push(
readAsset("/src/main/java/Main.java").then((data) => {
classes.file(classFileName+"Main", data)
})
)
promises.push(
readAsset("/src/main/resources/frog.mod.toml").then((data) => {
resources.file("frog.mod.toml", data)
})
)
promises.push(
readAsset("/src/main/resources/example_mod.mixins.json").then((data) => {
resources.file(modIdRef.value+".mixins.json", data)
})
)
Promise.all(promises).then((values) => {
zip.generateAsync({type:"blob"}).then(function (blob) {
saveAs(blob, modIdRef.value+"_template.zip")
}, function (err) {
alert("Failed to download generated zip file, please report this!")
})
})
}
async function readAssetBinary(path) {
const response = await fetch(assetBaseUrl+path)
return await response.blob()
}
async function readAsset(path){
const response = await fetch(assetBaseUrl+path)
return await response.text().then((data) => {
data = data.replaceAll("$mod_id", modIdRef.value)
data = data.replaceAll("$mod_name_pascal", modNameRef.value.replaceAll(" ", ""))
data = data.replaceAll("$mod_name", modNameRef.value)
data = data.replaceAll("$maven_group", mavenGroupRef.value)
data = data.replaceAll("$license", licenseRef.value)
data = data.replaceAll("$loader_version", useSnapshots.value ? latestLoaderSnapshot : latestLoader)
data = data.replaceAll("$phytotelma_version", useSnapshots.value ? latestPhytotelmaSnapshot : latestPhytotelma)
data = data.replaceAll("$game_version", versionSelector.value.value)
return data
})
}
function inputsChanged() {
document.getElementById("missingBox").innerHTML = ""
}
</script>
# {{ $frontmatter.title }}
## Inputs
<div id="inputs">
<table>
<tbody>
<tr>
<td>Mod ID</td>
<td><input v-model="modIdRef" @input="inputsChanged" /></td>
</tr>
<tr>
<td>Mod Name</td>
<td><input v-model="modNameRef" @input="inputsChanged" /></td>
</tr>
<tr>
<td>Maven Group</td>
<td><input v-model="mavenGroupRef" @input="inputsChanged" /></td>
</tr>
<tr>
<td>License (SPDX)</td>
<td><input v-model="licenseRef" @input="inputsChanged" /></td>
</tr>
<tr>
<td>Minecraft Version</td>
<td>
<select ref="versionSelector">
<option value="loading">Loading Versions...</option>
</select>
<input type="checkbox" v-model="showSnapshots" @change="getVersions">Show Snapshots</input>
</td>
</tr>
<tr>
<td>Library Versions</td>
<td>
<input type="checkbox" v-model="useSnapshots">Use Snapshots (Beta Versions)</input>
</td>
</tr>
</tbody>
</table>
<div id="missingBox"></div>
<button @click="generate">Download</button>
</div>
## Latest Versions
[FrogLoader](/contributing/tools.md#frogloader) <Badge type="tip" id="frogLoaderVersionReleaseBadge">Loading Release...</Badge> <Badge type="warning" id="frogLoaderVersionSnapshotBadge">Loading Snapshot...</Badge>
[Phytotelma](/contributing/tools.md#phytotelma) <Badge type="tip" id="phytotelmaVersionReleaseBadge">Loading Release...</Badge> <Badge type="warning" id="phytotelmaVersionSnapshotBadge">Loading Snapshot...</Badge>