From be8bba8ff81a8bf68e5309ba679d8bfc886604c0 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Mon, 22 Jan 2024 05:01:09 +0000 Subject: [PATCH] Complete template files --- packages/create/create.js | 19 ++++++-- .../create/templates/plugin-ts/controller.ts | 42 +++++++++++++++++ packages/create/templates/plugin-ts/ctl.ts | 26 +++++++++++ packages/create/templates/plugin-ts/host.ts | 30 +++++++++++++ .../create/templates/plugin-ts/instance.ts | 38 ++++++++++++++++ .../create/templates/plugin-ts/messages.ts | 10 ++--- .../templates/plugin-ts/module/control.lua | 24 ++++++++++ .../plugin-ts/module/module_exports.lua | 3 +- .../create/templates/plugin-ts/web/plugin.tsx | 45 +++++++++++++++++++ 9 files changed, 227 insertions(+), 10 deletions(-) diff --git a/packages/create/create.js b/packages/create/create.js index 9fb960ba..b8c328ed 100755 --- a/packages/create/create.js +++ b/packages/create/create.js @@ -521,7 +521,7 @@ async function copyPluginTemplateFile(src, dst, props) { await fs.outputFile(dst, outputData); } -async function copyPluginTemplate(templates) { +async function copyPluginTemplate(pluginName, templates) { logger.info(`Please wait, coping templates for ${templates.join(", ")}`); const noTypescript = templates.includes("js"); const entryPoints = []; @@ -581,7 +581,7 @@ async function copyPluginTemplate(templates) { // Props that will be replaced in the template files, found with "// prop_name //" const props = { - plugin_name: "TBC", + plugin_name: pluginName, entry_points: entryPoints.join("\n\t"), }; @@ -745,6 +745,9 @@ async function inquirerMissingArgs(args) { if (args["plugin-template"]) { answers.pluginTemplate = args["plugin-template"]; } + if (args["plugin-name"]) { + answers.pluginName = args["plugin-name"]; + } answers.plugins = []; answers = await inquirer.prompt([ { @@ -762,6 +765,13 @@ async function inquirerMissingArgs(args) { ], }, ], answers); + answers = await inquirer.prompt([ + { + type: "input", + name: "pluginName", + message: "Name of your new plugin", + }, + ], answers); } if (dev) { @@ -869,6 +879,9 @@ async function main() { .option("plugin-template", { 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() ; @@ -905,7 +918,7 @@ async function main() { logger.verbose(JSON.stringify(answers)); if (answers.pluginTemplate) { - await copyPluginTemplate(answers.pluginTemplate); + await copyPluginTemplate(answers.pluginName, answers.pluginTemplate); return; } diff --git a/packages/create/templates/plugin-ts/controller.ts b/packages/create/templates/plugin-ts/controller.ts index e69de29b..ee6d56b7 100644 --- a/packages/create/templates/plugin-ts/controller.ts +++ b/packages/create/templates/plugin-ts/controller.ts @@ -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, + }; + } +} diff --git a/packages/create/templates/plugin-ts/ctl.ts b/packages/create/templates/plugin-ts/ctl.ts index e69de29b..adbc0d05 100644 --- a/packages/create/templates/plugin-ts/ctl.ts +++ b/packages/create/templates/plugin-ts/ctl.ts @@ -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 ", "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); + } +} diff --git a/packages/create/templates/plugin-ts/host.ts b/packages/create/templates/plugin-ts/host.ts index e69de29b..bdfc05b5 100644 --- a/packages/create/templates/plugin-ts/host.ts +++ b/packages/create/templates/plugin-ts/host.ts @@ -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, + }; + } +} diff --git a/packages/create/templates/plugin-ts/instance.ts b/packages/create/templates/plugin-ts/instance.ts index e69de29b..f89429dd 100644 --- a/packages/create/templates/plugin-ts/instance.ts +++ b/packages/create/templates/plugin-ts/instance.ts @@ -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, + }; + } +} diff --git a/packages/create/templates/plugin-ts/messages.ts b/packages/create/templates/plugin-ts/messages.ts index f207d6a1..d357e31e 100644 --- a/packages/create/templates/plugin-ts/messages.ts +++ b/packages/create/templates/plugin-ts/messages.ts @@ -4,8 +4,8 @@ import { Type, Static } from "@sinclair/typebox"; export class PluginExampleEvent { declare ["constructor"]: typeof PluginExampleEvent; static type = "event" as const; - static src = ["host"] as const; - static dst = "controller" as const; + static src = ["host", "control"] as const; + static dst = ["controller", "host"] as const; static plugin = "// plugin_name //" as const; static permission = "// plugin_name //.example.permission.event"; @@ -26,10 +26,10 @@ export class PluginExampleEvent { } export class PluginExampleRequest { - declare ["constructor"]: typeof PluginExampleEvent; + declare ["constructor"]: typeof PluginExampleRequest; static type = "request" as const; - static src = ["host"] as const; - static dst = "controller" as const; + static src = ["host", "control"] as const; + static dst = ["controller", "host"] as const; static plugin = "// plugin_name //" as const; static permission = "// plugin_name //.example.permission.request"; diff --git a/packages/create/templates/plugin-ts/module/control.lua b/packages/create/templates/plugin-ts/module/control.lua index e69de29b..ab32acd6 100644 --- a/packages/create/templates/plugin-ts/module/control.lua +++ b/packages/create/templates/plugin-ts/module/control.lua @@ -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 diff --git a/packages/create/templates/plugin-ts/module/module_exports.lua b/packages/create/templates/plugin-ts/module/module_exports.lua index 29cbb493..cc0c5632 100644 --- a/packages/create/templates/plugin-ts/module/module_exports.lua +++ b/packages/create/templates/plugin-ts/module/module_exports.lua @@ -1,3 +1,2 @@ -local Control = require("./control") -return Control +return require("./control") diff --git a/packages/create/templates/plugin-ts/web/plugin.tsx b/packages/create/templates/plugin-ts/web/plugin.tsx index e69de29b..3d329883 100644 --- a/packages/create/templates/plugin-ts/web/plugin.tsx +++ b/packages/create/templates/plugin-ts/web/plugin.tsx @@ -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 +

// plugin_name //

+
; +} + +export class WebPlugin extends BaseWebPlugin { + async init() { + this.pages = [ + { + path: "/// plugin_name //", + sidebarName: "// plugin_name //", + permission: "// plugin_name //.page.view", + content: , + }, + ]; + + 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, + }; + } +}