1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-13 18:21:51 +00:00

Update class extensions

This commit is contained in:
tobspr 2022-01-17 19:11:04 +01:00
parent 1b931808eb
commit 0dac336670
2 changed files with 36 additions and 28 deletions

View File

@ -8,22 +8,24 @@ const METADATA = {
description: "Shows how to extend builtin classes", description: "Shows how to extend builtin classes",
}; };
const BeltExtension = ({ $super, $old }) => ({
getShowWiresLayerPreview() {
// Access the old method
return !$old.getShowWiresLayerPreview();
},
getIsReplaceable() {
// Instead of super, use $super
return $super.getIsReplaceable.call(this);
},
getIsRemoveable() {
return false;
},
});
class Mod extends shapez.Mod { class Mod extends shapez.Mod {
init() { init() {
this.modInterface.extendClass(shapez.MetaBeltBuilding, { this.modInterface.extendClass(shapez.MetaBeltBuilding, BeltExtension);
// this replaces a regular method
getShowWiresLayerPreview() {
return true;
},
// Instead of super, use this.$super()
getIsReplaceable() {
return this.$super.getIsReplaceable.call(this);
},
getIsRemoveable() {
return false;
},
});
} }
} }

View File

@ -389,18 +389,24 @@ export class ModInterface {
}; };
} }
extendClass(classHandle, extensionClass) { /**
const extendPrototype = function (base, extension) { *
const properties = Array.from(Object.getOwnPropertyNames(extension)); * @param {typeof Object} classHandle
base.$super = base.$super || {}; * @param {({ $super, $old }) => any} extender
properties.forEach(propertyName => { */
if (["constructor", "name", "length", "prototype"].includes(propertyName)) { extendClass(classHandle, extender) {
return; const prototype = classHandle.prototype;
}
base.$super[propertyName] = base.$super[propertyName] || base[propertyName]; const $super = Object.getPrototypeOf(prototype);
base[propertyName] = extension[propertyName]; const $old = {};
}); const extensionMethods = extender({ $super, $old });
}; const properties = Array.from(Object.getOwnPropertyNames(extensionMethods));
extendPrototype(classHandle.prototype, extensionClass); properties.forEach(propertyName => {
if (["constructor", "prototype"].includes(propertyName)) {
return;
}
$old[propertyName] = prototype[propertyName];
prototype[propertyName] = extensionMethods[propertyName];
});
} }
} }