diff --git a/actions/ActionBus.ts b/actions/ActionBus.ts index 7f19d23..7f9d514 100644 --- a/actions/ActionBus.ts +++ b/actions/ActionBus.ts @@ -1,4 +1,5 @@ import Action from "./Action"; +import Subscriber from "./Subscriber"; /** * The ActionBus is the bus to allow different elements of your program to be notified when a new Action is published, @@ -7,43 +8,39 @@ import Action from "./Action"; class ActionBus { /** - * The subscribed functions. Map of Action IDs to all the subscribed Functions for that Action ID. + * The subscribed Subscribers. Map of Action IDs to all the Subscribers for that Action ID. */ - private subscribers : Record = {} + private subscribers : Record = {} /** - * Subscribe to listen for new Actions of a certain type to come in, at which point the passed function - * will be called. The same Function can be subscribed multiple times. + * Subscribe to listen for new Actions of a certain type to come in, at which point the passed Subscriber + * will be notified. The same Subscriber can be subscribed multiple times. * @param action {typeof Action|number} The Action type to listen for. Can be an Action class, or an Action ID. - * @param fn {Function} The function to be called when the Action is received. Should be non-null. Should take at - * least one argument, which is the Action. Further arbitrary "rest" arguments can be passed to {@link publish}, - * which can be received by this function. + * @param sub {Subscriber} The Subscriber to be notified when the specified Action is published. * @returns {boolean} True if the subscriber is added successfully, false otherwise. */ - public subscribe(action: typeof Action|number, fn: Function) : boolean { - if(action == null || fn == null) { + public subscribe(action: typeof Action|number, sub: Subscriber) : boolean { + if(action == null || sub == null) { return false } const actionId = typeof action == "number" ? action : action.id if(this.subscribers[actionId] == null) { this.subscribers[actionId] = [] } - this.subscribers[actionId].push(fn) + this.subscribers[actionId].push(sub) return true } /** - * Unsubscribe to stop listening for a type of Action on a specific Function. Will only unsubscribe the Function - * once, so if the passed Function was subscribed multiple times, you will need to unsubscribe multiple times. + * Unsubscribe to stop listening for a type of Action on a specific Subscriber. Will only unsubscribe the Subscriber + * once, so if the passed Subscriber was subscribed multiple times, you will need to unsubscribe multiple times. * @param action {typeof Action|number} The Action type to listen for. Can be an Action class, or an Action ID. - * @param fn {Function} The function to be called when the Action is received. Should be non-null. Should take at - * least one argument, which is the Action. Further arbitrary "rest" arguments can be passed to {@link publish}, - * which can be received by this function. + * @param sub {Subscriber} The Subscriber to be notified when the specified Action is published. * @returns {boolean} True if the subscriber is added successfully, false otherwise. Returns true if the * passed Function was never subscribed to the passed Action in the first place. */ - public unsubscribe(action: typeof Action|number, fn: Function) : boolean { - if(action == null || fn == null) { + public unsubscribe(action: typeof Action|number, sub: Subscriber) : boolean { + if(action == null || sub == null) { return true } @@ -51,7 +48,7 @@ class ActionBus { if(this.subscribers[actionId] == null) { return true } - const index = this.subscribers[actionId].indexOf(fn) + const index = this.subscribers[actionId].indexOf(sub) if(index < 0 || index >= this.subscribers[actionId].length) { return true } @@ -61,16 +58,16 @@ class ActionBus { } /** - * Publish a new Action to this ActionBus, notifying all Functions subscribed to the type of Action published. + * Publish a new Action to this ActionBus, notifying all Subscribers subscribed to the type of Action published. * @param action {Action} The action to publish. Should be non-null. - * @param args {Object[]} Arbitrary arguments to pass to the subscriber + * @param args {Object[]} Arbitrary arguments to pass to the subscriber. */ - public publish(action: Action, ... args: Object[]) : void { + public async publish(action: Action, ... args: Object[]) : Promise { if(action == null || this.subscribers[action.id] == null) { return } for(let i = 0; i < this.subscribers[action.id].length; i++) { - this.subscribers[action.id][i](action, ...args) + await this.subscribers[action.id][i].run(action, ...args) } } } diff --git a/actions/Subscriber.ts b/actions/Subscriber.ts new file mode 100644 index 0000000..0d0f0dd --- /dev/null +++ b/actions/Subscriber.ts @@ -0,0 +1,16 @@ +import Action from "./Action"; + +/** + * Subscribers can be subscribed to the ActionBus to be notified when a specific Action is published to the bus. + */ +abstract class Subscriber { + /** + * Run function called when the ActionBus this Subscriber is called to notifies this Subscriber of a new Action. + * @param action {Action} The Action that caused this Subscriber to be notified. + * @param args {Object[]} Arbitrary arguments provided by the ActionBus publisher. + */ + abstract async run(action: Action, ... args: Object[]): Promise +} + +export default Subscriber + diff --git a/actions/clientbound/AuthBeginHandshakeAction.ts b/actions/clientbound/AuthBeginHandshakeAction.ts index e718ca1..36db69b 100644 --- a/actions/clientbound/AuthBeginHandshakeAction.ts +++ b/actions/clientbound/AuthBeginHandshakeAction.ts @@ -10,6 +10,8 @@ import {Buffer} from 'buffer' */ class AuthBeginHandshakeAction extends Action { + static id = 26 + /** * Create a new AuthBeginHandshakeAction. * @param handshakeToken {string} Handshake token to send diff --git a/actions/clientbound/AuthCompleteAction.ts b/actions/clientbound/AuthCompleteAction.ts index 7c73aa4..238f868 100644 --- a/actions/clientbound/AuthCompleteAction.ts +++ b/actions/clientbound/AuthCompleteAction.ts @@ -11,6 +11,8 @@ import {Buffer} from 'buffer' */ class AuthCompleteAction extends Action { + static id = 28 + /** * Create a new AuthCompleteAction. * @param sessionToken {string} Session token to send diff --git a/actions/clientbound/DisableModAction.ts b/actions/clientbound/DisableModAction.ts index 8d2ccde..7f219a9 100644 --- a/actions/clientbound/DisableModAction.ts +++ b/actions/clientbound/DisableModAction.ts @@ -10,6 +10,8 @@ import {Buffer} from 'buffer' */ class DisableModAction extends Action { + static id = 2 + /** * Create a new DisableModAction. * @param reason {string} The reason the mod was disabled diff --git a/actions/clientbound/EnableModAction.ts b/actions/clientbound/EnableModAction.ts index b0dba5d..291d344 100644 --- a/actions/clientbound/EnableModAction.ts +++ b/actions/clientbound/EnableModAction.ts @@ -6,6 +6,8 @@ import Action from '../Action' */ class EnableModAction extends Action { + static id = 1 + /** * Create a new EnableModAction. */ diff --git a/actions/clientbound/OpenGuiAction.ts b/actions/clientbound/OpenGuiAction.ts index 5b02816..8d9fb6f 100644 --- a/actions/clientbound/OpenGuiAction.ts +++ b/actions/clientbound/OpenGuiAction.ts @@ -11,6 +11,8 @@ import {Buffer} from 'buffer' */ class OpenGuiAction extends Action { + static id = 10 + /** * Create a new OpenGuiAction. * @param classpath {string} The path of the class to open as a GUI. diff --git a/actions/clientbound/OpenScreenAction.ts b/actions/clientbound/OpenScreenAction.ts index 31b3848..76edbc6 100644 --- a/actions/clientbound/OpenScreenAction.ts +++ b/actions/clientbound/OpenScreenAction.ts @@ -10,6 +10,8 @@ import {Buffer} from 'buffer' */ class OpenScreenAction extends Action { + static id = 11 + /** * Create a new OpenScreenAction. * @param screenName {string} The name of the screen that the client should open. diff --git a/actions/clientbound/RefreshCacheAction.ts b/actions/clientbound/RefreshCacheAction.ts index b23e0de..e441942 100644 --- a/actions/clientbound/RefreshCacheAction.ts +++ b/actions/clientbound/RefreshCacheAction.ts @@ -8,6 +8,8 @@ import Action from '../Action' */ class RefreshCacheAction extends Action { + static id = 12 + /** * Create a new RefreshCacheAction. */ diff --git a/actions/clientbound/ResetConfigAction.ts b/actions/clientbound/ResetConfigAction.ts index a2c42db..70a06b9 100644 --- a/actions/clientbound/ResetConfigAction.ts +++ b/actions/clientbound/ResetConfigAction.ts @@ -6,6 +6,8 @@ import Action from '../Action' */ class ResetConfigAction extends Action { + static id = 5 + /** * Create a new ResetConfigAction. */ diff --git a/actions/clientbound/SendChatCommandAction.ts b/actions/clientbound/SendChatCommandAction.ts index d80c695..6c7dcfe 100644 --- a/actions/clientbound/SendChatCommandAction.ts +++ b/actions/clientbound/SendChatCommandAction.ts @@ -10,6 +10,8 @@ import {Buffer} from 'buffer' */ class SendChatCommandAction extends Action { + static id = 6 + /** * Create a new SendChatCommandAction. * @param cmd {string} Command to send. Beginning slash will automatically be removed if provided, diff --git a/actions/clientbound/SendChatComponentAction.ts b/actions/clientbound/SendChatComponentAction.ts index 50cab1e..e922f08 100644 --- a/actions/clientbound/SendChatComponentAction.ts +++ b/actions/clientbound/SendChatComponentAction.ts @@ -11,6 +11,8 @@ import {Buffer} from 'buffer' */ class SendChatComponentAction extends Action { + static id = 3 + /** * Create a new SendChatComponentAction. * @param component {Message} Chat component message for the client to send diff --git a/actions/clientbound/SetAliasedActionAction.ts b/actions/clientbound/SetAliasedActionAction.ts index bb24fac..a7c6201 100644 --- a/actions/clientbound/SetAliasedActionAction.ts +++ b/actions/clientbound/SetAliasedActionAction.ts @@ -13,6 +13,8 @@ import {Buffer} from 'buffer' */ class SetAliasedActionAction extends Action { + static id = 7 + /** * Create a new SetAliasedActionAction. * @param aliasedAction {AliasedAction} Aliased action to be saved to the client. diff --git a/actions/clientbound/SetButtonAction.ts b/actions/clientbound/SetButtonAction.ts index e46fc98..f4b52f6 100644 --- a/actions/clientbound/SetButtonAction.ts +++ b/actions/clientbound/SetButtonAction.ts @@ -15,6 +15,8 @@ import {Buffer} from 'buffer' */ class SetButtonAction extends Action { + static id = 8 + /** * Create a new SetButtonAction. * @param button {Button} Button to be saved to the client. diff --git a/actions/clientbound/SetCurrentServerAction.ts b/actions/clientbound/SetCurrentServerAction.ts index 89bd46b..f6f6463 100644 --- a/actions/clientbound/SetCurrentServerAction.ts +++ b/actions/clientbound/SetCurrentServerAction.ts @@ -15,6 +15,8 @@ import {Buffer} from 'buffer' */ class SetCurrentServerAction extends Action { + static id = 13 + /** * Create a new SetCurrentServerAction. * @param serverName {string} The name of the server that the client has connected to. diff --git a/actions/clientbound/SetGlyphForUserAction.ts b/actions/clientbound/SetGlyphForUserAction.ts index b863d8d..263153e 100644 --- a/actions/clientbound/SetGlyphForUserAction.ts +++ b/actions/clientbound/SetGlyphForUserAction.ts @@ -14,6 +14,8 @@ import {Buffer} from 'buffer' */ class SetGlyphForUserAction extends Action { + static id = 14 + /** * Create a new SetGlyphForUserAction. * @param uuid {string} The UUID of the user for which this Glyph belongs. diff --git a/actions/clientbound/SetKeybindsAction.ts b/actions/clientbound/SetKeybindsAction.ts index 9abd0af..85579d0 100644 --- a/actions/clientbound/SetKeybindsAction.ts +++ b/actions/clientbound/SetKeybindsAction.ts @@ -12,6 +12,8 @@ import {Buffer} from 'buffer' */ class SetKeybindsAction extends Action { + static id = 15 + /** * Create a new SetKeybindsAction. * @param keybinds {Record[]} New keybinds to serialize and send to the client. diff --git a/actions/clientbound/SetPremiumAboutAction.ts b/actions/clientbound/SetPremiumAboutAction.ts index 2ed8fb6..91e19b7 100644 --- a/actions/clientbound/SetPremiumAboutAction.ts +++ b/actions/clientbound/SetPremiumAboutAction.ts @@ -11,6 +11,8 @@ import {Buffer} from 'buffer' */ class SetPremiumAboutAction extends Action { + static id = 16 + /** * Create a new SetPremiumAboutAction. * @param component {ChatComponent} Chat component to set the about text to. diff --git a/actions/clientbound/SetScreenAction.ts b/actions/clientbound/SetScreenAction.ts index 1bc8dc7..68b8ef0 100644 --- a/actions/clientbound/SetScreenAction.ts +++ b/actions/clientbound/SetScreenAction.ts @@ -17,6 +17,8 @@ import {Buffer} from 'buffer' */ class SetScreenAction extends Action { + static id = 9 + /** * Create a new SetScreenAction. * @param screen {Screen} Screen to be saved to the client. diff --git a/actions/clientbound/SetTranslationAction.ts b/actions/clientbound/SetTranslationAction.ts index d1f525f..8b7be6f 100644 --- a/actions/clientbound/SetTranslationAction.ts +++ b/actions/clientbound/SetTranslationAction.ts @@ -12,6 +12,8 @@ import {Buffer} from 'buffer' */ class SetTranslationAction extends Action { + static id = 17 + /** * Create a new SetTranslationAction. * @param key {string} The key of the translation to set. diff --git a/actions/clientbound/SystemOutAction.ts b/actions/clientbound/SystemOutAction.ts index 7764438..b2c03fe 100644 --- a/actions/clientbound/SystemOutAction.ts +++ b/actions/clientbound/SystemOutAction.ts @@ -7,6 +7,8 @@ import {Buffer} from 'buffer' */ class SystemOutAction extends Action { + static id = 4 + /** * Create a new SystemOutAction. * @param message {string} the message to send to the client's logs diff --git a/actions/serverbound/AuthEndHandshakeAction.ts b/actions/serverbound/AuthEndHandshakeAction.ts index a2e7493..c8a32e4 100644 --- a/actions/serverbound/AuthEndHandshakeAction.ts +++ b/actions/serverbound/AuthEndHandshakeAction.ts @@ -2,7 +2,6 @@ import Action from '../Action' import {Buffer} from 'buffer' /** - * SERVERBOUND - Server should not instantiate. * ID: 27 * Received by the server when the client is done authenticating with * Mojang's servers, and Quickplay's backend should check for authenticity. @@ -12,6 +11,7 @@ import {Buffer} from 'buffer' */ class AuthEndHandshakeAction extends Action { + static id = 27 /** * Create a new AuthEndHandshakeAction. * @param username {string} The current client's username diff --git a/actions/serverbound/ButtonPressedAction.ts b/actions/serverbound/ButtonPressedAction.ts index f3992b0..3132959 100644 --- a/actions/serverbound/ButtonPressedAction.ts +++ b/actions/serverbound/ButtonPressedAction.ts @@ -2,7 +2,6 @@ import Action from '../Action' import {Buffer} from 'buffer' /** - * SERVERBOUND - Server should not instantiate. * ID: 18 * Received by the server when the client has pressed a Quickplay button, or a keybind which points to a button. * @@ -11,6 +10,8 @@ import {Buffer} from 'buffer' */ class ButtonPressedAction extends Action { + static id = 18 + /** * Create a new ButtonPressedAction. * @param buttonKey {string} The key of the button which was pressed diff --git a/actions/serverbound/ExceptionThrownAction.ts b/actions/serverbound/ExceptionThrownAction.ts index a0e14a1..54000db 100644 --- a/actions/serverbound/ExceptionThrownAction.ts +++ b/actions/serverbound/ExceptionThrownAction.ts @@ -2,7 +2,6 @@ import Action from '../Action' import {Buffer} from 'buffer' /** - * SERVERBOUND - Server should not instantiate. * ID: 19 * Received by the server when an exception is reported by the client. * @@ -27,6 +26,8 @@ import {Buffer} from 'buffer' */ class ExceptionThrownAction extends Action { + static id = 19 + /** * Create a new ExceptionThrownAction. * @param type {string} The type of this exception. diff --git a/actions/serverbound/HypixelLocationChangedAction.ts b/actions/serverbound/HypixelLocationChangedAction.ts index 5b43aa4..0a7eb2f 100644 --- a/actions/serverbound/HypixelLocationChangedAction.ts +++ b/actions/serverbound/HypixelLocationChangedAction.ts @@ -2,7 +2,6 @@ import Action from '../Action' import {Buffer} from 'buffer' /** - * SERVERBOUND - Server should not instantiate. * ID: 20 * Received by the server when the client changes locations on Hypixel. * @@ -11,6 +10,8 @@ import {Buffer} from 'buffer' */ class HypixelLocationChangedAction extends Action { + static id = 20 + /** * Create a new HypixelLocationChangedAction. * @param locationJson {string} JSON about this player's location. diff --git a/actions/serverbound/InitializeClientAction.ts b/actions/serverbound/InitializeClientAction.ts index b7c6b66..39018e0 100644 --- a/actions/serverbound/InitializeClientAction.ts +++ b/actions/serverbound/InitializeClientAction.ts @@ -2,7 +2,6 @@ import Action from '../Action' import {Buffer} from 'buffer' /** - * SERVERBOUND - Server should not instantiate. * ID: 25 * Received by the server when the client first initializes the socket. Intended to send client metadata. * @@ -34,6 +33,8 @@ import {Buffer} from 'buffer' */ class InitializeClientAction extends Action { + static id = 25 + /** * Create a new InitializeClientAction. */ diff --git a/actions/serverbound/LanguageChangedAction.ts b/actions/serverbound/LanguageChangedAction.ts index f4a8fc1..373fbb0 100644 --- a/actions/serverbound/LanguageChangedAction.ts +++ b/actions/serverbound/LanguageChangedAction.ts @@ -2,7 +2,6 @@ import Action from '../Action' import {Buffer} from 'buffer' /** - * SERVERBOUND - Server should not instantiate. * ID: 22 * Received by the server when the client changes languages. * @@ -11,6 +10,8 @@ import {Buffer} from 'buffer' */ class LanguageChangedAction extends Action { + static id = 22 + /** * Create a new LanguageChangedAction. * @param langId {string} New language ID diff --git a/actions/serverbound/MigrateKeybindsAction.ts b/actions/serverbound/MigrateKeybindsAction.ts index bbb5d06..49ba255 100644 --- a/actions/serverbound/MigrateKeybindsAction.ts +++ b/actions/serverbound/MigrateKeybindsAction.ts @@ -2,7 +2,6 @@ import Action from '../Action' import {Buffer} from 'buffer' /** - * SERVERBOUND - Server should not instantiate. * ID: 21 * Send the list keybinds to the server so the server can respond with a migrated keybinds list. * This is currently only used to migrate keybinds from pre-2.1.0 to post-2.1.0. @@ -13,6 +12,8 @@ import {Buffer} from 'buffer' */ class MigrateKeybindsAction extends Action { + static id = 21 + /** * Create a new MigrateKeybindsAction. * @param keybinds {Record[]} New keybinds to serialize and send to the server. diff --git a/actions/serverbound/ServerJoinedAction.ts b/actions/serverbound/ServerJoinedAction.ts index 45834fe..2d763c2 100644 --- a/actions/serverbound/ServerJoinedAction.ts +++ b/actions/serverbound/ServerJoinedAction.ts @@ -2,7 +2,6 @@ import Action from '../Action' import {Buffer} from 'buffer' /** - * SERVERBOUND - Server should not instantiate. * ID: 23 * Received by the server when the client connects to a new Minecraft server. * Could be singleplayer, in which case the IP is "singleplayer". @@ -13,6 +12,8 @@ import {Buffer} from 'buffer' */ class ServerJoinedAction extends Action { + static id = 23 + /** * Create a new ServerJoinedAction. * @param ip {string} The IP of the server the client joined. diff --git a/actions/serverbound/ServerLeftAction.ts b/actions/serverbound/ServerLeftAction.ts index e38fedc..ccac052 100644 --- a/actions/serverbound/ServerLeftAction.ts +++ b/actions/serverbound/ServerLeftAction.ts @@ -1,12 +1,13 @@ import Action from '../Action' /** - * SERVERBOUND - Server should not instantiate. * ID: 24 * Received by the server when the client disconnects from the server they were previously on. */ class ServerLeftAction extends Action { + static id = 24 + /** * Create a new ServerLeftAction. */ diff --git a/index.ts b/index.ts index b902152..5ff97b0 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,7 @@ import Action from "./actions/Action"; import Resolver from "./actions/Resolver"; import ActionBus from "./actions/ActionBus"; +import Subscriber from "./actions/Subscriber"; import Button from "./Button"; import Screen from "./Screen"; import AliasedAction from "./AliasedAction"; @@ -40,6 +41,7 @@ import AuthEndHandshakeAction from "./actions/serverbound/AuthEndHandshakeAction export {Action} export {Resolver} export {ActionBus} +export {Subscriber} export {Button} export {Screen} export {AliasedAction} diff --git a/package.json b/package.json index d093ce0..2702195 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@quickplaymod/quickplay-actions-js", - "version": "1.1.1", + "version": "1.2.0", "description": "Quickplay's central Action controller, written in TypeScript.", "main": "dist/index.js", "types": "dist/index.d.ts",