Add command suggestion support.
This commit is contained in:
parent
d70f151db3
commit
8d9b62cd45
27
dist/index.ts
vendored
27
dist/index.ts
vendored
@ -3,7 +3,9 @@
|
|||||||
import {
|
import {
|
||||||
Command,
|
Command,
|
||||||
CommandSender,
|
CommandSender,
|
||||||
|
CommandSuggestions,
|
||||||
ConsumedArgs,
|
ConsumedArgs,
|
||||||
|
SuggestionRequest,
|
||||||
} from "pumpkin:plugin/command@0.1.0";
|
} from "pumpkin:plugin/command@0.1.0";
|
||||||
import { Context } from "pumpkin:plugin/context@0.1.0";
|
import { Context } from "pumpkin:plugin/context@0.1.0";
|
||||||
import { Event, EventType, EventPriority } from "pumpkin:plugin/event@0.1.0";
|
import { Event, EventType, EventPriority } from "pumpkin:plugin/event@0.1.0";
|
||||||
@ -23,6 +25,11 @@ export type CommandHandler = (
|
|||||||
srv: Server,
|
srv: Server,
|
||||||
args: ConsumedArgs,
|
args: ConsumedArgs,
|
||||||
) => number;
|
) => number;
|
||||||
|
export type CommandSuggestionHandler = (
|
||||||
|
sender: CommandSender,
|
||||||
|
srv: Server,
|
||||||
|
request: SuggestionRequest,
|
||||||
|
) => CommandSuggestions;
|
||||||
export type TaskHandler = (srv: Server) => void;
|
export type TaskHandler = (srv: Server) => void;
|
||||||
export interface AiGoal {
|
export interface AiGoal {
|
||||||
canStart: (server: Server, entity: Entity) => boolean;
|
canStart: (server: Server, entity: Entity) => boolean;
|
||||||
@ -39,6 +46,7 @@ export type ChunkGenerator = (
|
|||||||
let pluginInstance: Plugin | null = null;
|
let pluginInstance: Plugin | null = null;
|
||||||
const eventHandlers = new Map<number, EventHandler>();
|
const eventHandlers = new Map<number, EventHandler>();
|
||||||
const commandHandlers = new Map<number, CommandHandler>();
|
const commandHandlers = new Map<number, CommandHandler>();
|
||||||
|
const commandSuggestionHandlers = new Map<number, CommandSuggestionHandler>();
|
||||||
const taskHandlers = new Map<number, TaskHandler>();
|
const taskHandlers = new Map<number, TaskHandler>();
|
||||||
const generators = new Map<number, ChunkGenerator>();
|
const generators = new Map<number, ChunkGenerator>();
|
||||||
const aiGoals = new Map<number, AiGoal>();
|
const aiGoals = new Map<number, AiGoal>();
|
||||||
@ -95,6 +103,12 @@ export abstract class Plugin {
|
|||||||
ctx.registerCommand(cmd, permission);
|
ctx.registerCommand(cmd, permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registerCommandSuggestionHandler(handler: CommandSuggestionHandler): number {
|
||||||
|
const handlerId = getNextHandlerId();
|
||||||
|
commandSuggestionHandlers.set(handlerId, handler);
|
||||||
|
return handlerId;
|
||||||
|
}
|
||||||
|
|
||||||
scheduleDelayedTask(
|
scheduleDelayedTask(
|
||||||
delayTicks: number | bigint,
|
delayTicks: number | bigint,
|
||||||
handler: TaskHandler,
|
handler: TaskHandler,
|
||||||
@ -174,6 +188,19 @@ export function handleCommand(
|
|||||||
throw new Error(`No handler for command ID ${commandId}`);
|
throw new Error(`No handler for command ID ${commandId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function handleCommandSuggestion(
|
||||||
|
handlerId: number,
|
||||||
|
sender: CommandSender,
|
||||||
|
srv: Server,
|
||||||
|
request: SuggestionRequest,
|
||||||
|
): CommandSuggestions {
|
||||||
|
const handler = commandSuggestionHandlers.get(handlerId);
|
||||||
|
if (handler) {
|
||||||
|
return handler(sender, srv, request);
|
||||||
|
}
|
||||||
|
throw new Error(`No command suggestion handler registered for ID ${handler}`);
|
||||||
|
}
|
||||||
|
|
||||||
export function handleTask(handlerId: number, srv: Server): void {
|
export function handleTask(handlerId: number, srv: Server): void {
|
||||||
const handler = taskHandlers.get(handlerId);
|
const handler = taskHandlers.get(handlerId);
|
||||||
if (handler) {
|
if (handler) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user