MDT-JS模組制作入門
前言
在MDT里,json-mod能達到的效果相對有限,而js-mod相對來說比較自由。在這里,我們先來簡單的入個門,語法用的是Rhino.js。

制作前提
所有文件都應(yīng)放置在你模組根目錄下的`scripts/*`里,且必須要有一個模組的入口,命名為`main.js`

內(nèi)容創(chuàng)建
內(nèi)容的創(chuàng)建可以有如下的兩種形式,中括號([])內(nèi)的內(nèi)容可選,現(xiàn)在MDT已經(jīng)不支持`extendContent`函數(shù)
// 形式1
const @1 = extend(@2[, @3], {[@4]})
@5
// 形式2
const @1 = new @2([@3])
@5
在MDT里,js代碼結(jié)尾處可以不用加分號(;)
@1:內(nèi)容所擁有的變量名
@2:類名,內(nèi)容的類型
若是你欲制作類型本來在源碼中存在,卻無法使用,可以嘗試使用(Packages.包名.類型進行使用)
@3:可選 內(nèi)容構(gòu)造時需要傳入的參數(shù)(可以沒有或者有多個,參數(shù)間以逗號(,)隔開,要傳入的參數(shù)請自行閱讀源碼)
@4:可選 用于覆蓋內(nèi)容原先擁有的函數(shù)等
@5:其他的代碼,如修改內(nèi)容的其他屬性,或其他的內(nèi)容

內(nèi)容導(dǎo)出/引用
內(nèi)容的導(dǎo)出和引用可以更好的來分類文件,至少看起來不是很亂。當(dāng)然也可以不用,你可以把全部的內(nèi)容全部寫在js模組入口`main.js`

導(dǎo)出
至少我看的其他js-mod大多數(shù)都是這么干的
// 形式1
exports.@1 = @1
[/*或者更多, 以換行符隔開*/]
// 形式2
module.exports = {
@1 : @1[, /*或者更多, 用半角逗號(,)隔開*/]
}

引用
路徑的后面直接寫文件干名稱,不用后綴;在其他與`main.js`不同目錄所用的文件引用,均以`main.js`為開始目錄(其實就是把其他文件換了一個地方執(zhí)行)
const @1 = require('路徑')
[/*或更多*/]
// 后續(xù)的使用
@1.導(dǎo)出的內(nèi)容

其他
全局可用的函數(shù)/變量
這個文件可在`Mindustry-master\core\assets\scripts\global.js`找到
//Generated class. Do not modify.
"use strict";
let scriptName = "base.js"
let modName = "none"
const log = (context, obj) => Vars.mods.scripts.log(context, String(obj))
const print = text => log(modName + "/" + scriptName, text)
const newFloats = cap => Vars.mods.getScripts().newFloats(cap);
//these are not strictly necessary, but are kept for edge cases
const run = method => new java.lang.Runnable(){run: method}
const boolf = method => new Boolf(){get: method}
const boolp = method => new Boolp(){get: method}
const floatf = method => new Floatf(){get: method}
const floatp = method => new Floatp(){get: method}
const cons = method => new Cons(){get: method}
const prov = method => new Prov(){get: method}
const func = method => new Func(){get: method}
const newEffect = (lifetime, renderer) => new Effect.Effect(lifetime, new Effect.EffectRenderer({render: renderer}))
Call = Packages.mindustry.gen.Call
//js 'extend(Base, ..., {})' = java 'new Base(...) {}'
function extend(/*Base, ..., def*/){
? ?const Base = arguments[0]
? ?const def = arguments[arguments.length - 1]
? ?//swap order from Base, def, ... to Base, ..., def
? ?const args = [Base, def].concat(Array.from(arguments).splice(1, arguments.length - 2))
? ?//forward constructor arguments to new JavaAdapter
? ?const instance = JavaAdapter.apply(null, args)
? ?//JavaAdapter only overrides functions; set fields too
? ?for(var i in def){
? ? ? ?if(typeof(def[i]) != "function"){
? ? ? ? ? ?instance[i] = def[i]
? ? ? ?}
? ?}
? ?return instance
}
...

后言
既然你想制作js-mod,那源碼的閱讀能力總要有吧,源碼可以去serctl.com輔助下載,如果你連GitHub都登不上的話就踢我吧。
在java中可以省去的東西在js中要寫完整,比如在源碼中有一段是這樣的`ui.listfrag.toggle()`,你需要找到這個消失的東西并且改寫成這樣`Vars.ui.listfrag.toggle()`,一般都可以在Vars(MDT源碼中)或者Core(Arc源碼中)文件里找到