diff --git a/mod_examples/modify_existing_building.js b/mod_examples/modify_existing_building.js new file mode 100644 index 00000000..cf8452ae --- /dev/null +++ b/mod_examples/modify_existing_building.js @@ -0,0 +1,36 @@ +/** + * This shows how to modify an existing building + */ +registerMod(() => { + return class ModImpl extends shapez.Mod { + constructor(app, modLoader) { + super( + app, + { + website: "https://tobspr.io", + author: "tobspr", + name: "Mod Example: Modify existing building", + version: "1", + id: "modify-existing-building", + description: "Shows how to modify an existing building", + }, + modLoader + ); + } + + init() { + // Make Rotator always unlocked + this.modInterface.replaceMethod(shapez.MetaRotaterBuilding, "getIsUnlocked", function () { + return true; + }); + + // Add some custom stats to the info panel when selecting the building + this.modInterface.replaceMethod(shapez.MetaRotaterBuilding, "getAdditionalStatistics", function ( + root, + variant + ) { + return [["Awesomeness", 5]]; + }); + } + }; +}); diff --git a/src/js/game/component_registry.js b/src/js/game/component_registry.js index 9c9247e6..1add879e 100644 --- a/src/js/game/component_registry.js +++ b/src/js/game/component_registry.js @@ -22,31 +22,34 @@ import { ItemProducerComponent } from "./components/item_producer"; import { GoalAcceptorComponent } from "./components/goal_acceptor"; export function initComponentRegistry() { - gComponentRegistry.register(StaticMapEntityComponent); - gComponentRegistry.register(BeltComponent); - gComponentRegistry.register(ItemEjectorComponent); - gComponentRegistry.register(ItemAcceptorComponent); - gComponentRegistry.register(MinerComponent); - gComponentRegistry.register(ItemProcessorComponent); - gComponentRegistry.register(UndergroundBeltComponent); - gComponentRegistry.register(HubComponent); - gComponentRegistry.register(StorageComponent); - gComponentRegistry.register(WiredPinsComponent); - gComponentRegistry.register(BeltUnderlaysComponent); - gComponentRegistry.register(WireComponent); - gComponentRegistry.register(ConstantSignalComponent); - gComponentRegistry.register(LogicGateComponent); - gComponentRegistry.register(LeverComponent); - gComponentRegistry.register(WireTunnelComponent); - gComponentRegistry.register(DisplayComponent); - gComponentRegistry.register(BeltReaderComponent); - gComponentRegistry.register(FilterComponent); - gComponentRegistry.register(ItemProducerComponent); - gComponentRegistry.register(GoalAcceptorComponent); + const components = [ + StaticMapEntityComponent, + BeltComponent, + ItemEjectorComponent, + ItemAcceptorComponent, + MinerComponent, + ItemProcessorComponent, + UndergroundBeltComponent, + HubComponent, + StorageComponent, + WiredPinsComponent, + BeltUnderlaysComponent, + WireComponent, + ConstantSignalComponent, + LogicGateComponent, + LeverComponent, + WireTunnelComponent, + DisplayComponent, + BeltReaderComponent, + FilterComponent, + ItemProducerComponent, + GoalAcceptorComponent, + ]; + components.forEach(component => gComponentRegistry.register(component)); // IMPORTANT ^^^^^ UPDATE ENTITY COMPONENT STORAGE AFTERWARDS - // Sanity check - If this is thrown, you (=me, lol) forgot to add a new component here + // Sanity check - If this is thrown, you forgot to add a new component here assert( // @ts-ignore diff --git a/src/js/main.js b/src/js/main.js index 3ae76844..0f80f527 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -21,37 +21,6 @@ if (window.coreThreadLoadedCb) { window.coreThreadLoadedCb(); } -// Logrocket -// if (!G_IS_DEV && !G_IS_STANDALONE) { -// const monthlyUsers = 300; // thousand -// const logrocketLimit = 10; // thousand -// const percentageOfUsers = logrocketLimit / monthlyUsers; - -// if (Math.random() <= percentageOfUsers) { -// logger.log("Analyzing this session with logrocket"); -// const logrocket = require("logrocket"); -// logrocket.init("p1x9zh/shapezio"); - -// try { -// logrocket.getSessionURL(function (sessionURL) { -// logger.log("Connected lockrocket to GA"); -// // @ts-ignore -// try { -// window.ga("send", { -// hitType: "event", -// eventCategory: "LogRocket", -// eventAction: sessionURL, -// }); -// } catch (ex) { -// logger.warn("Logrocket connection to analytics failed:", ex); -// } -// }); -// } catch (ex) { -// logger.warn("Logrocket connection to analytics failed:", ex); -// } -// } -// } - console.log( `%cshapez.io ️%c\n© 2022 tobspr Games\nCommit %c${G_BUILD_COMMIT_HASH}%c on %c${new Date( G_BUILD_TIME diff --git a/src/js/mods/mod_interface.js b/src/js/mods/mod_interface.js index f7db8b95..895d64c7 100644 --- a/src/js/mods/mod_interface.js +++ b/src/js/mods/mod_interface.js @@ -355,4 +355,11 @@ export class ModInterface { } }); } + + /** + * Patches a method on a given object + */ + replaceMethod(classHandle, methodName, override) { + classHandle.prototype[methodName] = override; + } }