mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-02-11 11:17:11 +07:00
unified extend and extendContent to work for any class
This commit is contained in:
parent
522a7f4728
commit
09eed078cd
@ -1,6 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
const log = function(context, obj){
|
||||
function log(context, obj){
|
||||
Vars.mods.scripts.log(context, String(obj))
|
||||
}
|
||||
|
||||
@ -19,13 +19,26 @@ let modName = "none"
|
||||
|
||||
const print = text => log(modName + "/" + scriptName, text);
|
||||
|
||||
const extendContent = function(classType, name, params){
|
||||
return new JavaAdapter(classType, params, name)
|
||||
// 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;
|
||||
}
|
||||
|
||||
const extend = function(classType, params){
|
||||
return new JavaAdapter(classType, params)
|
||||
}
|
||||
// For backwards compatibility, use extend instead
|
||||
const extendContent = extend;
|
||||
|
||||
//these are not sctrictly necessary, but are kept for edge cases
|
||||
const run = method => new java.lang.Runnable(){run: method}
|
||||
@ -38,4 +51,4 @@ const prov = method => new Prov(){get: method}
|
||||
const func = method => new Func(){get: method}
|
||||
|
||||
const newEffect = (lifetime, renderer) => new Effects.Effect(lifetime, new Effects.EffectRenderer({render: renderer}))
|
||||
Call = Packages.mindustry.gen.Call
|
||||
const Call = Packages.mindustry.gen.Call
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
"use strict";
|
||||
|
||||
const log = function(context, obj){
|
||||
function log(context, obj){
|
||||
Vars.mods.getScripts().log(context, String(obj))
|
||||
}
|
||||
|
||||
@ -21,13 +21,26 @@ let modName = "none"
|
||||
|
||||
const print = text => log(modName + "/" + scriptName, text);
|
||||
|
||||
const extendContent = function(classType, name, params){
|
||||
return new JavaAdapter(classType, params, name)
|
||||
// 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;
|
||||
}
|
||||
|
||||
const extend = function(classType, params){
|
||||
return new JavaAdapter(classType, params)
|
||||
}
|
||||
// For backwards compatibility, use extend instead
|
||||
const extendContent = extend;
|
||||
|
||||
//these are not sctrictly necessary, but are kept for edge cases
|
||||
const run = method => new java.lang.Runnable(){run: method}
|
||||
@ -40,7 +53,7 @@ const prov = method => new Prov(){get: method}
|
||||
const func = method => new Func(){get: method}
|
||||
|
||||
const newEffect = (lifetime, renderer) => new Effects.Effect(lifetime, new Effects.EffectRenderer({render: renderer}))
|
||||
Call = Packages.mindustry.gen.Call
|
||||
const Call = Packages.mindustry.gen.Call
|
||||
|
||||
importPackage(Packages.arc)
|
||||
importPackage(Packages.arc.func)
|
||||
|
Loading…
Reference in New Issue
Block a user