Complete template files

This commit is contained in:
Cooldude2606 committed 2024-01-22 05:01:09 +00:00
1 parent c81d2b61a6
commit be8bba8ff8
9 files changed
+227 -10

No files matched your search

@@ -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 {
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";
@@ -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,
};
}
}