### Install SocketBE Source: https://github.com/tutinoko2048/socketbe/blob/main/README.md Installs the SocketBE library using npm. Requires Node.js v18 or later. ```bash npm install socket-be ``` -------------------------------- ### Basic Server Example Source: https://github.com/tutinoko2048/socketbe/blob/main/README_ja.md A basic example demonstrating how to set up a SocketBE server, listen for player chat events, and send messages back to the Minecraft client. It logs messages to the console and responds to 'ping' commands. ```javascript import { Server, ServerEvent } from 'socket-be'; const server = new Server({ port: 8000 }) server.on(ServerEvent.Open, () => { console.log('Server started') }); server.on(ServerEvent.PlayerChat, async ev => { const { sender, message, world } = ev; if (sender.name === '外部') return; // prevents spam loop console.log(`<${sender.name}> ${message}`); if (message === 'ping') { await world.sendMessage('Pong!'); } }); ``` -------------------------------- ### Install SocketBE Source: https://github.com/tutinoko2048/socketbe/blob/main/README_ja.md Installs the SocketBE library using npm. Requires Node.js v18 or higher. ```bash npm i socket-be ``` -------------------------------- ### Server Class Properties Source: https://github.com/tutinoko2048/socketbe/wiki/Server Defines the properties of the Server class, including event handlers, start time, logger instance, and the server's IP address. ```typescript events: Events; startTime: number; logger: Logger; ip: string; ``` -------------------------------- ### Basic Server Usage Source: https://github.com/tutinoko2048/socketbe/blob/main/README.md Demonstrates how to create a SocketBE server, listen for connections, and handle player chat events. It logs messages to the console and responds to 'ping' commands with 'Pong!'. ```javascript import { Server, ServerEvent } from 'socket-be'; const server = new Server({ port: 8000 }) server.on(ServerEvent.Open, () => { console.log('Server started') }); server.on(ServerEvent.PlayerChat, async ev => { const { sender, message, world } = ev; if (sender.name === 'External') return; // prevents spam loop console.log(`<${sender.name}> ${message}`); if (message === 'ping') { await world.sendMessage('Pong!'); } }); ``` -------------------------------- ### World Class Methods Source: https://github.com/tutinoko2048/socketbe/wiki/World Outlines the methods available in the World Class for interacting with the game world, such as executing commands, sending messages, retrieving player data, and managing events. ```APIDOC World Class Methods: runCommand(command: string): Promise - Executes a given command within the world. - Parameters: - command: The command string to execute. - Returns: A promise that resolves with the result of the command. sendMessage(message: string | any, target?: string): Promise - Sends a message to a specific target or all players. - Parameters: - message: The message content, can be a string or any serializable object. - target: Optional. The identifier of the target player or entity. - Returns: A promise that resolves when the message is sent. getPlayerList(): Promise - Retrieves a list of all players currently in the world. - Returns: A promise that resolves with the PlayerList object. getPlayers(): Promise - Retrieves an array of player identifiers. - Returns: A promise that resolves with an array of player IDs. getLocalPlayer(): Promise - Retrieves the identifier of the local player. - Returns: A promise that resolves with the local player's ID. getTags(player: string): Promise - Retrieves the tags associated with a specific player. - Parameters: - player: The identifier of the player. - Returns: A promise that resolves with an array of tags. hasTag(player: string, tag: string): Promise - Checks if a specific player has a given tag. - Parameters: - player: The identifier of the player. - tag: The tag to check for. - Returns: A promise that resolves with a boolean indicating if the player has the tag. getPlayerDetail(): Promise - Retrieves detailed information about the local player. - Returns: A promise that resolves with the PlayerDetail object. sendPacket(packet: ServerPacket): void - Sends a raw server packet. - Parameters: - packet: The ServerPacket object to send. subscribeEvent(eventName: string): void - Subscribes to a specific game event. - Parameters: - eventName: The name of the event to subscribe to. unsubscribeEvent(eventName: string): void - Unsubscribes from a specific game event. - Parameters: - eventName: The name of the event to unsubscribe from. close(): void - Closes the WebSocket connection associated with the world. ``` -------------------------------- ### Server Class Methods Source: https://github.com/tutinoko2048/socketbe/wiki/Server Details the methods available in the Server class for initialization, retrieving world data, running commands, and sending messages. ```APIDOC Server: __init__(option: ServerOption): Server Constructs a new Server instance. Parameters: option: ServerOption - Configuration options for the server. getWorlds(): World[] Retrieves all available worlds. Returns: An array of World objects. getWorld(worldId: string): World | undefined Retrieves a specific world by its ID. Parameters: worldId: string - The unique identifier of the world. Returns: The World object if found, otherwise undefined. runCommand(command: string): Promise Executes a given command on the server. Parameters: command: string - The command to execute. Returns: A Promise that resolves with an array of results from the command execution. sendMessage(message: string, target: string): Promise Sends a message to a specified target. Parameters: message: string - The message content to send. target: string - The identifier of the target recipient. ``` -------------------------------- ### World Class Properties Source: https://github.com/tutinoko2048/socketbe/wiki/World Defines the properties of the World Class, including its name, logger, player management details, connection information, and WebSocket instance. ```typescript name: string; logger: Logger; lastPlayers: string[]; maxPlayers: number; scoreboards: ScoreboardManager; connectedAt: number; id: string; localPlayer: string | null; ping: number; ws: WebSocket.WebSocket; ``` -------------------------------- ### ServerOption Interface Definition Source: https://github.com/tutinoko2048/socketbe/wiki/ServerOption Defines the structure for server configuration options. Includes boolean for debug mode, string for timezone, numbers for timeouts and intervals, and a VersionResolvable type for command versioning. ```typescript interface ServerOption { debug?: boolean; timezone?: string; packetTimeout?: number; listUpdateInterval?: number; commandVersion?: VersionResolvable; } ``` -------------------------------- ### Socket.IO Event Handling Methods Source: https://github.com/tutinoko2048/socketbe/wiki/Events Provides methods for managing server-side events in Socket.IO. The `on` method subscribes a listener to a specific event, `off` unsubscribes a listener, and `emit` triggers an event with optional arguments. ```APIDOC Events Class: on(eventName: K, fn: (arg: ServerEvents[K]) => void): (arg: ServerEvents[K]) => void; Subscribes a listener function to a specific event. Parameters: - eventName: The name of the event to listen for. Must be a key of ServerEvents. - fn: The listener function to be called when the event is emitted. Returns: The listener function that was registered. Related: - off: Unsubscribes a listener. off(eventName: K, fn: (arg: ServerEvents[K]) => void): void; Unsubscribes a listener function from a specific event. Parameters: - eventName: The name of the event to stop listening to. Must be a key of ServerEvents. - fn: The listener function to remove. Returns: void. Related: - on: Subscribes a listener. emit(eventName: string, ...args: any[]): any; Emits an event to the server with optional arguments. Parameters: - eventName: The name of the event to emit. - args: Any arguments to pass to the event listeners. Returns: The result of the event emission. Related: - on: Subscribes a listener. ``` -------------------------------- ### Server Events Definitions Source: https://github.com/tutinoko2048/socketbe/wiki/ServerEvents Defines the structure and types for various server events, including player actions, world management, packet handling, errors, chat messages, titles, and server ticks. ```APIDOC Events.PlayerJoin: players: string[] world: World Description: Fires when player(s) has joined the world Events.PlayerLeave: players: string[] world: World Description: Fires when player(s) has left the world Events.ServerOpen: Description: Fires when the server is ready Events.ServerClose: Description: Fires when the server is closed Events.WorldAdd: world: World Description: Fires when new world has connected Events.WorldRemove: world: World Description: Fires when the world has disconnected Events.PacketReceive: packet: any world: World Description: Fires when every packet has received Events.Error: Error: Error Description: Fires when something error has occured Events.PlayerChat: type: 'chat' | 'say' | 'me' | 'tell' message: string sender: string receiver: string world: World Description: Fires when the player has sent/received? a chat message Events.PlayerTitle: type: 'title' message: string sender: string receiver: string world: World Description: Fires when the player has sent/received? a title Events.Tick: Description: Fires every 500ms (20 times per second) ``` -------------------------------- ### ScoreboardManager API Source: https://github.com/tutinoko2048/socketbe/wiki/ScoreboardManager Provides methods for managing scoreboard objectives and player scores. Includes functionalities for resolving, retrieving, adding, removing, and updating objectives and scores, as well as setting display properties. ```APIDOC ScoreboardManager: resolveObjective(objective: string | ScoreboardObjective): string Resolves an objective to its string identifier. Parameters: objective: The objective to resolve, can be a string ID or a ScoreboardObjective object. Returns: The string identifier of the objective. getObjectives(): Promise Retrieves all scoreboard objectives. Returns: A promise that resolves to an array of ScoreboardObjective objects. getObjective(objectiveId: string): Promise Retrieves a specific scoreboard objective by its ID. Parameters: objectiveId: The ID of the objective to retrieve. Returns: A promise that resolves to the ScoreboardObjective object or undefined if not found. addObjective(objectiveId: string, displayName?: string): Promise Adds a new scoreboard objective. Parameters: objectiveId: The unique ID for the new objective. displayName: An optional display name for the objective. Returns: A promise that resolves to the newly created ScoreboardObjective object or null if creation failed. removeObjective(objectiveId: string | ScoreboardObjective): Promise Removes a scoreboard objective. Parameters: objectiveId: The ID or ScoreboardObjective object of the objective to remove. Returns: A promise that resolves to true if the objective was removed successfully, false otherwise. getScores(player: string): Promise<{ [objective: string]: number }> Retrieves all scores for a given player across all objectives. Parameters: player: The name of the player. Returns: A promise that resolves to an object where keys are objective IDs and values are scores. getScore(player: string, objectiveId: string | ScoreboardObjective): Promise Retrieves a player's score for a specific objective. Parameters: player: The name of the player. objectiveId: The ID or ScoreboardObjective object of the objective. Returns: A promise that resolves to the player's score or null if not found. setScore(player: string, objectiveId: string | ScoreboardObjective, score: number): Promise Sets a player's score for a specific objective. Parameters: player: The name of the player. objectiveId: The ID or ScoreboardObjective object of the objective. score: The score to set. Returns: A promise that resolves to the updated score or null if setting failed. addScore(player: string, objectiveId: string | ScoreboardObjective, score: number): Promise Adds a score to a player's existing score for a specific objective. Parameters: player: The name of the player. objectiveId: The ID or ScoreboardObjective object of the objective. score: The score to add. Returns: A promise that resolves to the new score or null if adding failed. removeScore(player: string, objectiveId: string | ScoreboardObjective, score: number): Promise Removes a score from a player's existing score for a specific objective. Parameters: player: The name of the player. objectiveId: The ID or ScoreboardObjective object of the objective. score: The score to remove. Returns: A promise that resolves to the updated score or null if removing failed. resetScore(player: string, objectiveId?: string | ScoreboardObjective): Promise Resets a player's score for a specific objective or all objectives. Parameters: player: The name of the player. objectiveId: An optional ID or ScoreboardObjective object of the objective to reset. Returns: A promise that resolves to true if the score was reset successfully, false otherwise. setDisplay(displaySlotId: string, objectiveId: string | ScoreboardObjective | undefined, sortOrder: 'ascending' | 'descending' | undefined): Promise Sets the display properties for a scoreboard slot. Parameters: displaySlotId: The ID of the display slot (e.g., 'sidebar', 'list', 'belowName'). objectiveId: The ID or ScoreboardObjective object of the objective to display, or undefined to clear the slot. sortOrder: The sorting order for the objective ('ascending', 'descending', or undefined). Returns: A promise that resolves to true if the display was set successfully, false otherwise. ``` -------------------------------- ### VersionResolvable Type Definition Source: https://github.com/tutinoko2048/socketbe/wiki/VersionResolvable Defines the possible formats for a version identifier. It can be a simple string, a numerical representation, or a more complex array of numbers. ```APIDOC VersionResolvable: Type: string | number | number[] Description: Represents a version identifier. Can be a string (e.g., "1.2.3"), a number (e.g., 123), or an array of numbers (e.g., [1, 2, 3]). ``` -------------------------------- ### Give Apple on Player Chat and Score Source: https://github.com/tutinoko2048/socketbe/wiki/Example:-give-apple-with-score-conditions This code snippet demonstrates how to handle the 'playerChat' event in a Socket.IO server. It checks if the sender is not '外部' (external), if the message is 'apple', and if the player's score in the 'objective' scoreboard is 10 or greater. If all conditions are met, it executes a server command to give the player an apple. This is useful for in-game events triggered by player actions. ```js server.events.on('playerChat', async (event) => { const { sender, message, world } = event; if (sender === '外部') return; if (message === 'apple') { const score = await world.scoreboards.getScore(sender, 'objective'); if (score >= 10) await world.runCommand(`give "${sender}" apple`); } }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.