Add IPC to templates and handle helper function server

This commit is contained in:
Cooldude2606 committed 2024-01-24 00:40:22 +00:00
1 parent 2916dcc58c
commit 58231cc02c
11 files changed
+78 -19

No files matched your search

+11 -3
View File
@@ -24,10 +24,10 @@ lib.definePermission({
const plugin = {
name: "// plugin_name //",
title: "// plugin_name //",
description: "I didn't update my description",
description: "I didn't update my description",// [entry_points] //
// entry_points //
// [controller] //
controlConfigFields: {
// [] //// [controller] //
controllerConfigFields: {
"// plugin_name //.myControllerField": {
title: "My Controller Field",
description: "This should be removed",
@@ -50,6 +50,14 @@ const plugin = {
type: "string",
initialValue: "Remove Me",
},
},// [] //// [ctl] //
controlConfigFields: {
"// plugin_name //.myControlField": {
title: "My Control Field",
description: "This should be removed",
type: "string",
initialValue: "Remove Me",
},
},// [] //
messages: [
@@ -6,7 +6,8 @@ const { PluginExampleEvent, PluginExampleRequest } = require("./messages");
class InstancePlugin extends BaseInstancePlugin {
async init() {
this.instance.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
this.instance.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
this.instance.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));// [module] //
this.instance.server.handle("ipc-// plugin_name //-plugin_example_ipc", this.handlePluginExampleIPC.bind(this));// [] //
}
async onInstanceConfigFieldChanged(field, curr, prev) {
@@ -26,7 +27,8 @@ class InstancePlugin extends BaseInstancePlugin {
}
async handlePluginExampleEvent(event) {
this.logger.info(JSON.stringify(event));
this.logger.info(JSON.stringify(event));// [module] //
this.sendRcon("/sc ipc_// plugin_name //.foo()");// [] //
}
async handlePluginExampleRequest(request) {
@@ -35,7 +37,11 @@ class InstancePlugin extends BaseInstancePlugin {
myResponseString: request.myString,
myResponseNumbers: request.myNumberArray,
};
}
}// [module] //
async handlePluginExampleIPC(event) {
this.logger.info(JSON.stringify(event));
}// [] //
}
module.exports = {
@@ -14,7 +14,10 @@ MyModule.events[clusterio_api.events.on_server_startup] = function(event)
end
MyModule.events[defines.events.on_player_crafted_item] = function(event)
game.print(game.table_to_json(event))
game.print(game.table_to_json(event))// [instance] //
clusterio_api.send_json("// plugin_name //-plugin_example_ipc", {
tick = game.tick, player_name = game.get_player(event.player_index).name
})// [] //
end
MyModule.on_nth_tick[300] = function()
@@ -4,7 +4,7 @@
"control.lua"
],
"require": [
// [instance] //"module_exports.lua"// [] //
],
"dependencies": [
@@ -1,2 +1,7 @@
local MyModule = require("./control")// [instance] //
return require("./control")
-- Can contain anything you plan to use with send_rcon from functions, modules, or data
ipc_// plugin_name // = MyModule// [] //
-- Can contain anything you want to allow other plugins to have access to, this example exposes the whole module
return MyModule
+14 -3
View File
@@ -29,16 +29,19 @@ declare module "@clusterio/lib" {// [controller] //
}// [] //// [instance] //
export interface InstanceConfigFields {
"// plugin_name //.myInstanceField": string;
}// [] //// [ctl] //
export interface ControlConfigFields {
"// plugin_name //.myControlField": string;
}// [] //
}
export const plugin: lib.PluginDeclaration = {
name: "// plugin_name //",
title: "// plugin_name //",
description: "I didn't update my description",
description: "I didn't update my description",// [entry_points] //
// entry_points //
// [controller] //
controlConfigFields: {
// [] //// [controller] //
controllerConfigFields: {
"// plugin_name //.myControllerField": {
title: "My Controller Field",
description: "This should be removed",
@@ -61,6 +64,14 @@ export const plugin: lib.PluginDeclaration = {
type: "string",
initialValue: "Remove Me",
},
},// [] //// [ctl] //
controlConfigFields: {
"// plugin_name //.myControlField": {
title: "My Control Field",
description: "This should be removed",
type: "string",
initialValue: "Remove Me",
},
},// [] //
messages: [
@@ -1,11 +1,17 @@
import * as lib from "@clusterio/lib";
import { BaseInstancePlugin } from "@clusterio/host";
import { PluginExampleEvent, PluginExampleRequest } from "./messages";
import { PluginExampleEvent, PluginExampleRequest } from "./messages";// [module] //
type PuginExampleIPC = {
tick: number,
player_name: string,
};// [] //
export class InstancePlugin extends BaseInstancePlugin {
async init() {
this.instance.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
this.instance.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
this.instance.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));// [module] //
this.instance.server.handle("ipc-// plugin_name //-plugin_example_ipc", this.handlePluginExampleIPC.bind(this));// [] //
}
async onInstanceConfigFieldChanged(field: string, curr: unknown, prev: unknown) {
@@ -21,7 +27,8 @@ export class InstancePlugin extends BaseInstancePlugin {
}
async onPlayerEvent(event: lib.PlayerEvent) {
this.logger.info(`onPlayerEvent::onPlayerEvent ${JSON.stringify(event)}`);
this.logger.info(`onPlayerEvent::onPlayerEvent ${JSON.stringify(event)}`);// [module] //
this.sendRcon("/sc ipc_// plugin_name //.foo()");// [] //
}
async handlePluginExampleEvent(event: PluginExampleEvent) {
@@ -34,5 +41,9 @@ export class InstancePlugin extends BaseInstancePlugin {
myResponseString: request.myString,
myResponseNumbers: request.myNumberArray,
};
}
}// [module] //
async handlePluginExampleIPC(event: PuginExampleIPC) {
this.logger.info(JSON.stringify(event));
}// [] //
}
@@ -14,7 +14,10 @@ MyModule.events[clusterio_api.events.on_server_startup] = function(event)
end
MyModule.events[defines.events.on_player_crafted_item] = function(event)
game.print(game.table_to_json(event))
game.print(game.table_to_json(event))// [instance] //
clusterio_api.send_json("// plugin_name //-plugin_example_ipc", {
tick = game.tick, player_name = game.get_player(event.player_index).name
})// [] //
end
MyModule.on_nth_tick[300] = function()
@@ -4,7 +4,7 @@
"control.lua"
],
"require": [
// [instance] //"module_exports.lua"// [] //
],
"dependencies": [
@@ -1,2 +1,7 @@
local MyModule = require("./control")// [instance] //
return require("./control")
-- Can contain anything you plan to use with send_rcon from functions, modules, or data
ipc_// plugin_name // = MyModule// [] //
-- Can contain anything you want to allow other plugins to have access to, this example exposes the whole module
return MyModule
+7
View File
@@ -562,6 +562,13 @@ export class FactorioServer extends events.EventEmitter {
}
}
// TODO at the moment this is just a helper function, it has the potential for schema checking
handle<Event>(eventName: string, handler: (event: Event) => Promise<void>) {
this.on(eventName, (event) => handler(event).catch(err => {
this._logger.error(`Error handling ipc event:\n${err.stack}`);
}));
}
async _handleIpc(line: Buffer) {
let channelEnd = line.indexOf("?");
if (channelEnd === -1) {