mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 08:23:12 +09:00
Add IPC to templates and handle helper function server
This commit is contained in:
11 files changed
+78
-19
No files matched your search
@@ -24,10 +24,10 @@ lib.definePermission({
|
|||||||
const plugin = {
|
const plugin = {
|
||||||
name: "// plugin_name //",
|
name: "// plugin_name //",
|
||||||
title: "// plugin_name //",
|
title: "// plugin_name //",
|
||||||
description: "I didn't update my description",
|
description: "I didn't update my description",// [entry_points] //
|
||||||
// entry_points //
|
// entry_points //
|
||||||
// [controller] //
|
// [] //// [controller] //
|
||||||
controlConfigFields: {
|
controllerConfigFields: {
|
||||||
"// plugin_name //.myControllerField": {
|
"// plugin_name //.myControllerField": {
|
||||||
title: "My Controller Field",
|
title: "My Controller Field",
|
||||||
description: "This should be removed",
|
description: "This should be removed",
|
||||||
@@ -50,6 +50,14 @@ const plugin = {
|
|||||||
type: "string",
|
type: "string",
|
||||||
initialValue: "Remove Me",
|
initialValue: "Remove Me",
|
||||||
},
|
},
|
||||||
|
},// [] //// [ctl] //
|
||||||
|
controlConfigFields: {
|
||||||
|
"// plugin_name //.myControlField": {
|
||||||
|
title: "My Control Field",
|
||||||
|
description: "This should be removed",
|
||||||
|
type: "string",
|
||||||
|
initialValue: "Remove Me",
|
||||||
|
},
|
||||||
},// [] //
|
},// [] //
|
||||||
|
|
||||||
messages: [
|
messages: [
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ const { PluginExampleEvent, PluginExampleRequest } = require("./messages");
|
|||||||
class InstancePlugin extends BaseInstancePlugin {
|
class InstancePlugin extends BaseInstancePlugin {
|
||||||
async init() {
|
async init() {
|
||||||
this.instance.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
|
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) {
|
async onInstanceConfigFieldChanged(field, curr, prev) {
|
||||||
@@ -26,7 +27,8 @@ class InstancePlugin extends BaseInstancePlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async handlePluginExampleEvent(event) {
|
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) {
|
async handlePluginExampleRequest(request) {
|
||||||
@@ -35,7 +37,11 @@ class InstancePlugin extends BaseInstancePlugin {
|
|||||||
myResponseString: request.myString,
|
myResponseString: request.myString,
|
||||||
myResponseNumbers: request.myNumberArray,
|
myResponseNumbers: request.myNumberArray,
|
||||||
};
|
};
|
||||||
}
|
}// [module] //
|
||||||
|
|
||||||
|
async handlePluginExampleIPC(event) {
|
||||||
|
this.logger.info(JSON.stringify(event));
|
||||||
|
}// [] //
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
@@ -14,7 +14,10 @@ MyModule.events[clusterio_api.events.on_server_startup] = function(event)
|
|||||||
end
|
end
|
||||||
|
|
||||||
MyModule.events[defines.events.on_player_crafted_item] = function(event)
|
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
|
end
|
||||||
|
|
||||||
MyModule.on_nth_tick[300] = function()
|
MyModule.on_nth_tick[300] = function()
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
"control.lua"
|
"control.lua"
|
||||||
],
|
],
|
||||||
"require": [
|
"require": [
|
||||||
|
// [instance] //"module_exports.lua"// [] //
|
||||||
],
|
],
|
||||||
"dependencies": [
|
"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
|
||||||
@@ -29,16 +29,19 @@ declare module "@clusterio/lib" {// [controller] //
|
|||||||
}// [] //// [instance] //
|
}// [] //// [instance] //
|
||||||
export interface InstanceConfigFields {
|
export interface InstanceConfigFields {
|
||||||
"// plugin_name //.myInstanceField": string;
|
"// plugin_name //.myInstanceField": string;
|
||||||
|
}// [] //// [ctl] //
|
||||||
|
export interface ControlConfigFields {
|
||||||
|
"// plugin_name //.myControlField": string;
|
||||||
}// [] //
|
}// [] //
|
||||||
}
|
}
|
||||||
|
|
||||||
export const plugin: lib.PluginDeclaration = {
|
export const plugin: lib.PluginDeclaration = {
|
||||||
name: "// plugin_name //",
|
name: "// plugin_name //",
|
||||||
title: "// plugin_name //",
|
title: "// plugin_name //",
|
||||||
description: "I didn't update my description",
|
description: "I didn't update my description",// [entry_points] //
|
||||||
// entry_points //
|
// entry_points //
|
||||||
// [controller] //
|
// [] //// [controller] //
|
||||||
controlConfigFields: {
|
controllerConfigFields: {
|
||||||
"// plugin_name //.myControllerField": {
|
"// plugin_name //.myControllerField": {
|
||||||
title: "My Controller Field",
|
title: "My Controller Field",
|
||||||
description: "This should be removed",
|
description: "This should be removed",
|
||||||
@@ -61,6 +64,14 @@ export const plugin: lib.PluginDeclaration = {
|
|||||||
type: "string",
|
type: "string",
|
||||||
initialValue: "Remove Me",
|
initialValue: "Remove Me",
|
||||||
},
|
},
|
||||||
|
},// [] //// [ctl] //
|
||||||
|
controlConfigFields: {
|
||||||
|
"// plugin_name //.myControlField": {
|
||||||
|
title: "My Control Field",
|
||||||
|
description: "This should be removed",
|
||||||
|
type: "string",
|
||||||
|
initialValue: "Remove Me",
|
||||||
|
},
|
||||||
},// [] //
|
},// [] //
|
||||||
|
|
||||||
messages: [
|
messages: [
|
||||||
|
|||||||
@@ -1,11 +1,17 @@
|
|||||||
import * as lib from "@clusterio/lib";
|
import * as lib from "@clusterio/lib";
|
||||||
import { BaseInstancePlugin } from "@clusterio/host";
|
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 {
|
export class InstancePlugin extends BaseInstancePlugin {
|
||||||
async init() {
|
async init() {
|
||||||
this.instance.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
|
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) {
|
async onInstanceConfigFieldChanged(field: string, curr: unknown, prev: unknown) {
|
||||||
@@ -21,7 +27,8 @@ export class InstancePlugin extends BaseInstancePlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async onPlayerEvent(event: lib.PlayerEvent) {
|
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) {
|
async handlePluginExampleEvent(event: PluginExampleEvent) {
|
||||||
@@ -34,5 +41,9 @@ export class InstancePlugin extends BaseInstancePlugin {
|
|||||||
myResponseString: request.myString,
|
myResponseString: request.myString,
|
||||||
myResponseNumbers: request.myNumberArray,
|
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
|
end
|
||||||
|
|
||||||
MyModule.events[defines.events.on_player_crafted_item] = function(event)
|
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
|
end
|
||||||
|
|
||||||
MyModule.on_nth_tick[300] = function()
|
MyModule.on_nth_tick[300] = function()
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
"control.lua"
|
"control.lua"
|
||||||
],
|
],
|
||||||
"require": [
|
"require": [
|
||||||
|
// [instance] //"module_exports.lua"// [] //
|
||||||
],
|
],
|
||||||
"dependencies": [
|
"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
|
||||||
@@ -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) {
|
async _handleIpc(line: Buffer) {
|
||||||
let channelEnd = line.indexOf("?");
|
let channelEnd = line.indexOf("?");
|
||||||
if (channelEnd === -1) {
|
if (channelEnd === -1) {
|
||||||
|
|||||||
Reference in new issue
Block a user