mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 00:13:12 +09:00
Complete template files
This commit is contained in:
9 files changed
+227
-10
No files matched your search
@@ -521,7 +521,7 @@ async function copyPluginTemplateFile(src, dst, props) {
|
|||||||
await fs.outputFile(dst, outputData);
|
await fs.outputFile(dst, outputData);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function copyPluginTemplate(templates) {
|
async function copyPluginTemplate(pluginName, templates) {
|
||||||
logger.info(`Please wait, coping templates for ${templates.join(", ")}`);
|
logger.info(`Please wait, coping templates for ${templates.join(", ")}`);
|
||||||
const noTypescript = templates.includes("js");
|
const noTypescript = templates.includes("js");
|
||||||
const entryPoints = [];
|
const entryPoints = [];
|
||||||
@@ -581,7 +581,7 @@ async function copyPluginTemplate(templates) {
|
|||||||
|
|
||||||
// Props that will be replaced in the template files, found with "// prop_name //"
|
// Props that will be replaced in the template files, found with "// prop_name //"
|
||||||
const props = {
|
const props = {
|
||||||
plugin_name: "TBC",
|
plugin_name: pluginName,
|
||||||
entry_points: entryPoints.join("\n\t"),
|
entry_points: entryPoints.join("\n\t"),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -745,6 +745,9 @@ async function inquirerMissingArgs(args) {
|
|||||||
if (args["plugin-template"]) {
|
if (args["plugin-template"]) {
|
||||||
answers.pluginTemplate = args["plugin-template"];
|
answers.pluginTemplate = args["plugin-template"];
|
||||||
}
|
}
|
||||||
|
if (args["plugin-name"]) {
|
||||||
|
answers.pluginName = args["plugin-name"];
|
||||||
|
}
|
||||||
answers.plugins = [];
|
answers.plugins = [];
|
||||||
answers = await inquirer.prompt([
|
answers = await inquirer.prompt([
|
||||||
{
|
{
|
||||||
@@ -762,6 +765,13 @@ async function inquirerMissingArgs(args) {
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
], answers);
|
], answers);
|
||||||
|
answers = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
type: "input",
|
||||||
|
name: "pluginName",
|
||||||
|
message: "Name of your new plugin",
|
||||||
|
},
|
||||||
|
], answers);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dev) {
|
if (dev) {
|
||||||
@@ -869,6 +879,9 @@ async function main() {
|
|||||||
.option("plugin-template", {
|
.option("plugin-template", {
|
||||||
array: true, describe: "Plugin template to use [plugin-template]", type: "string",
|
array: true, describe: "Plugin template to use [plugin-template]", type: "string",
|
||||||
})
|
})
|
||||||
|
.option("plugin-name", {
|
||||||
|
nargs: 1, describe: "Name of your new plugin [plugin-template]", type: "string",
|
||||||
|
})
|
||||||
.strict()
|
.strict()
|
||||||
;
|
;
|
||||||
|
|
||||||
@@ -905,7 +918,7 @@ async function main() {
|
|||||||
logger.verbose(JSON.stringify(answers));
|
logger.verbose(JSON.stringify(answers));
|
||||||
|
|
||||||
if (answers.pluginTemplate) {
|
if (answers.pluginTemplate) {
|
||||||
await copyPluginTemplate(answers.pluginTemplate);
|
await copyPluginTemplate(answers.pluginName, answers.pluginTemplate);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
import * as lib from "@clusterio/lib";
|
||||||
|
import { BaseControllerPlugin, InstanceInfo } from "@clusterio/controller";
|
||||||
|
import { PluginExampleEvent, PluginExampleRequest } from "./messages";
|
||||||
|
|
||||||
|
export class ControllerPlugin extends BaseControllerPlugin {
|
||||||
|
async init() {
|
||||||
|
this.controller.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
|
||||||
|
this.controller.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
async onControllerConfigFieldChanged(field: string, curr: unknown, prev: unknown) {
|
||||||
|
this.logger.info(`controller::onControllerConfigFieldChanged ${field}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async onInstanceConfigFieldChanged(instance: InstanceInfo, field: string, curr: unknown, prev: unknown) {
|
||||||
|
this.logger.info(`controller::onInstanceConfigFieldChanged ${instance.id} ${field}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async onSaveData() {
|
||||||
|
this.logger.info("controller::onSaveData");
|
||||||
|
}
|
||||||
|
|
||||||
|
async onShutdown() {
|
||||||
|
this.logger.info("controller::onShutdown");
|
||||||
|
}
|
||||||
|
|
||||||
|
async onPlayerEvent(instance: InstanceInfo, event: lib.PlayerEvent) {
|
||||||
|
this.logger.info(`controller::onPlayerEvent ${instance.id} ${JSON.stringify(event)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async handlePluginExampleEvent(event: PluginExampleEvent) {
|
||||||
|
this.logger.info(JSON.stringify(event));
|
||||||
|
}
|
||||||
|
|
||||||
|
async handlePluginExampleRequest(request: PluginExampleRequest) {
|
||||||
|
this.logger.info(JSON.stringify(request));
|
||||||
|
return {
|
||||||
|
myResponseString: request.myString,
|
||||||
|
myResponseNumbers: request.myNumberArray,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
type Control = any; // import type { Control } from "@clusterio/ctl";
|
||||||
|
import { BaseCtlPlugin } from "@clusterio/ctl";
|
||||||
|
import { CommandTree, Command } from "@clusterio/lib";
|
||||||
|
import { PluginExampleEvent, PluginExampleRequest } from "./messages";
|
||||||
|
/* eslint-disable no-console */
|
||||||
|
|
||||||
|
const pluginCommands = new CommandTree({
|
||||||
|
name: "// plugin_name //", description: "The default description I forgot to change",
|
||||||
|
});
|
||||||
|
|
||||||
|
pluginCommands.add(new Command({
|
||||||
|
definition: ["my-command <foo>", "My command description that I really should change", (yargs) => {
|
||||||
|
yargs.positional("foo", { describe: "foo is foo!", type: "string" });
|
||||||
|
}],
|
||||||
|
handler: async function(args: { foo: string }, control: Control) {
|
||||||
|
await control.sendTo("controller", new PluginExampleEvent(args.foo, [1, 2, 3]));
|
||||||
|
console.log(args);
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
/* eslint-enable no-console */
|
||||||
|
export class CtlPlugin extends BaseCtlPlugin {
|
||||||
|
async addCommands(rootCommand: CommandTree) {
|
||||||
|
rootCommand.add(pluginCommands);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import * as lib from "@clusterio/lib";
|
||||||
|
import { BaseHostPlugin } from "@clusterio/host";
|
||||||
|
import { PluginExampleEvent, PluginExampleRequest } from "./messages";
|
||||||
|
|
||||||
|
export class HostPlugin extends BaseHostPlugin {
|
||||||
|
async init() {
|
||||||
|
this.host.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
|
||||||
|
this.host.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
async onHostConfigFieldChanged(field: string, curr: unknown, prev: unknown) {
|
||||||
|
this.logger.info(`host::onInstanceConfigFieldChanged ${field}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async onShutdown() {
|
||||||
|
this.logger.info("host::onShutdown");
|
||||||
|
}
|
||||||
|
|
||||||
|
async handlePluginExampleEvent(event: PluginExampleEvent) {
|
||||||
|
this.logger.info(JSON.stringify(event));
|
||||||
|
}
|
||||||
|
|
||||||
|
async handlePluginExampleRequest(request: PluginExampleRequest) {
|
||||||
|
this.logger.info(JSON.stringify(request));
|
||||||
|
return {
|
||||||
|
myResponseString: request.myString,
|
||||||
|
myResponseNumbers: request.myNumberArray,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
import * as lib from "@clusterio/lib";
|
||||||
|
import { BaseInstancePlugin } from "@clusterio/host";
|
||||||
|
import { PluginExampleEvent, PluginExampleRequest } from "./messages";
|
||||||
|
|
||||||
|
export class InstancePlugin extends BaseInstancePlugin {
|
||||||
|
async init() {
|
||||||
|
this.instance.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
|
||||||
|
this.instance.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
async onInstanceConfigFieldChanged(field: string, curr: unknown, prev: unknown) {
|
||||||
|
this.logger.info(`instance::onInstanceConfigFieldChanged ${field}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async onStart() {
|
||||||
|
this.logger.info("instance::onStart");
|
||||||
|
}
|
||||||
|
|
||||||
|
async onStop() {
|
||||||
|
this.logger.info("instance::onStop");
|
||||||
|
}
|
||||||
|
|
||||||
|
async onPlayerEvent(event: lib.PlayerEvent) {
|
||||||
|
this.logger.info(`onPlayerEvent::onPlayerEvent ${JSON.stringify(event)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async handlePluginExampleEvent(event: PluginExampleEvent) {
|
||||||
|
this.logger.info(JSON.stringify(event));
|
||||||
|
}
|
||||||
|
|
||||||
|
async handlePluginExampleRequest(request: PluginExampleRequest) {
|
||||||
|
this.logger.info(JSON.stringify(request));
|
||||||
|
return {
|
||||||
|
myResponseString: request.myString,
|
||||||
|
myResponseNumbers: request.myNumberArray,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,8 +4,8 @@ import { Type, Static } from "@sinclair/typebox";
|
|||||||
export class PluginExampleEvent {
|
export class PluginExampleEvent {
|
||||||
declare ["constructor"]: typeof PluginExampleEvent;
|
declare ["constructor"]: typeof PluginExampleEvent;
|
||||||
static type = "event" as const;
|
static type = "event" as const;
|
||||||
static src = ["host"] as const;
|
static src = ["host", "control"] as const;
|
||||||
static dst = "controller" as const;
|
static dst = ["controller", "host"] as const;
|
||||||
static plugin = "// plugin_name //" as const;
|
static plugin = "// plugin_name //" as const;
|
||||||
static permission = "// plugin_name //.example.permission.event";
|
static permission = "// plugin_name //.example.permission.event";
|
||||||
|
|
||||||
@@ -26,10 +26,10 @@ export class PluginExampleEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class PluginExampleRequest {
|
export class PluginExampleRequest {
|
||||||
declare ["constructor"]: typeof PluginExampleEvent;
|
declare ["constructor"]: typeof PluginExampleRequest;
|
||||||
static type = "request" as const;
|
static type = "request" as const;
|
||||||
static src = ["host"] as const;
|
static src = ["host", "control"] as const;
|
||||||
static dst = "controller" as const;
|
static dst = ["controller", "host"] as const;
|
||||||
static plugin = "// plugin_name //" as const;
|
static plugin = "// plugin_name //" as const;
|
||||||
static permission = "// plugin_name //.example.permission.request";
|
static permission = "// plugin_name //.example.permission.request";
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
local clusterio_api = require("modules/clusterio/api")
|
||||||
|
|
||||||
|
local MyModule = {
|
||||||
|
events = {},
|
||||||
|
on_nth_tick = {},
|
||||||
|
}
|
||||||
|
|
||||||
|
function MyModule.foo()
|
||||||
|
game.print("foo")
|
||||||
|
end
|
||||||
|
|
||||||
|
MyModule.events[clusterio_api.events.on_server_startup] = function(event)
|
||||||
|
game.print(game.table_to_json(event))
|
||||||
|
end
|
||||||
|
|
||||||
|
MyModule.events[defines.events.on_player_crafted_item] = function(event)
|
||||||
|
game.print(game.table_to_json(event))
|
||||||
|
end
|
||||||
|
|
||||||
|
MyModule.on_nth_tick[300] = function()
|
||||||
|
game.print(game.tick)
|
||||||
|
end
|
||||||
|
|
||||||
|
return MyModule
|
||||||
@@ -1,3 +1,2 @@
|
|||||||
local Control = require("./control")
|
|
||||||
|
|
||||||
return Control
|
return require("./control")
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import React, { useContext, useEffect, useState } from "react";
|
||||||
|
// import { } from "antd";
|
||||||
|
|
||||||
|
import {
|
||||||
|
BaseWebPlugin, PageLayout, Control, ControlContext, notifyErrorHandler
|
||||||
|
} from "@clusterio/web_ui";
|
||||||
|
|
||||||
|
import * as lib from "@clusterio/lib";
|
||||||
|
import { PluginExampleEvent, PluginExampleRequest } from "../messages";
|
||||||
|
|
||||||
|
function MyTemplatePage() {
|
||||||
|
let control = useContext(ControlContext);
|
||||||
|
|
||||||
|
return <PageLayout nav={[{ name: "// plugin_name //" }]}>
|
||||||
|
<h2>// plugin_name //</h2>
|
||||||
|
</PageLayout>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class WebPlugin extends BaseWebPlugin {
|
||||||
|
async init() {
|
||||||
|
this.pages = [
|
||||||
|
{
|
||||||
|
path: "/// plugin_name //",
|
||||||
|
sidebarName: "// plugin_name //",
|
||||||
|
permission: "// plugin_name //.page.view",
|
||||||
|
content: <MyTemplatePage/>,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
this.control.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
|
||||||
|
this.control.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
async handlePluginExampleEvent(event: PluginExampleEvent) {
|
||||||
|
this.logger.info(JSON.stringify(event));
|
||||||
|
}
|
||||||
|
|
||||||
|
async handlePluginExampleRequest(request: PluginExampleRequest) {
|
||||||
|
this.logger.info(JSON.stringify(request));
|
||||||
|
return {
|
||||||
|
myResponseString: request.myString,
|
||||||
|
myResponseNumbers: request.myNumberArray,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user