This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ♻️ complete refactor of the whole library
- Loading branch information
1 parent
2451165
commit e5e6d25
Showing
7 changed files
with
381 additions
and
93 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
const scheme = require('./scheme'); | ||
|
||
class LCPlayer { | ||
constructor(client, channel = 'lunarclient:pm') { | ||
this.client = client; | ||
this.channel = channel; | ||
this.waypoints = []; | ||
this.teammates = []; | ||
|
||
this.client.write('custom_payload', { | ||
channel: 'REGISTER', | ||
data: Buffer.from(this.channel), | ||
}); | ||
this.client.registerChannel(this.channel, scheme); | ||
} | ||
|
||
addWaypoint(waypoint) { | ||
if (this.waypoints.find((w) => w.name === waypoint.name)) return false; | ||
this.waypoints.push(waypoint); | ||
this.client.writeChannel(this.channel, { | ||
id: 'waypoint_add', | ||
name: waypoint.name, | ||
world: '', | ||
color: waypoint.color, | ||
x: waypoint.x, | ||
y: waypoint.y, | ||
z: waypoint.z, | ||
forced: waypoint.forced, | ||
visible: waypoint.visible, | ||
}); | ||
return true; | ||
} | ||
|
||
removeWaypoint(waypoint) { | ||
const _waypoint = typeof waypoint === 'string' ? waypoint : waypoint.name; | ||
if (!this.waypoints.find((w) => w.name === _waypoint)) return false; | ||
this.waypoints = this.waypoints.filter((w) => w.name !== _waypoint); | ||
this.client.writeChannel(this.channel, { | ||
id: 'waypoint_remove', | ||
name: _waypoint, | ||
world: '', | ||
}); | ||
return true; | ||
} | ||
|
||
removeAllWaypoints() { | ||
this.waypoints.forEach((waypoint) => { | ||
this.removeWaypoint(waypoint.name); | ||
}); | ||
this.waypoints = []; | ||
} | ||
|
||
sendNotification(message, durationMs, level = 'info') { | ||
this.client.writeChannel(this.channel, { | ||
id: 'notification', | ||
message, | ||
durationMs, | ||
level, | ||
}); | ||
} | ||
|
||
addTeammate(uuid) { | ||
if (this.teammates.find((t) => t === uuid)) return false; | ||
this.teammates.push(uuid); | ||
this.#sendTeammateList(); | ||
return true; | ||
} | ||
|
||
removeTeammate(uuid) { | ||
if (!this.teammates.find((t) => t === uuid)) return false; | ||
this.teammates = this.teammates.filter((t) => t !== uuid); | ||
this.#sendTeammateList(); | ||
return true; | ||
} | ||
|
||
#sendTeammateList() { | ||
const players = []; | ||
this.teammates.forEach((uuid) => { | ||
players.push({ | ||
player: uuid, | ||
posMap: [ | ||
{ key: 'x', value: 0 }, | ||
{ key: 'y', value: 0 }, | ||
{ key: 'z', value: 0 }, | ||
], | ||
}); | ||
}); | ||
this.client.writeChannel(this.channel, { | ||
id: 'teammates', | ||
leader: this.client.uuid, | ||
lastMs: 0, | ||
players, | ||
}); | ||
} | ||
} | ||
|
||
const WaypointColor = { | ||
RED: 0xff0000, | ||
BLUE: 0x0000ff, | ||
GREEN: 0x00ff00, | ||
YELLOW: 0xffff00, | ||
AQUA: 0x00ffff, | ||
WHITE: 0xffffff, | ||
PINK: 0xff00ff, | ||
GRAY: 0x808080, | ||
}; | ||
|
||
module.exports = { | ||
LCPlayer, | ||
WaypointColor, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,125 @@ | ||
import { Client } from 'minecraft-protocol'; | ||
|
||
/** | ||
* The protocol scheme used by [`protodef`](https://www.npmjs.com/package/protodef) to define the protocol between the client and the server | ||
*/ | ||
export declare const scheme: { [key: string]: any }; | ||
export declare const channel: string; | ||
export declare function registerClient( | ||
client: Client, | ||
altChannel?: boolean | ||
): void; | ||
|
||
export declare class LCPlayer { | ||
/** | ||
* Creates a new LunarClient player | ||
* @param client Node Minecraft Protocol Client | ||
* @param channel LC Plugin Channel to use | ||
*/ | ||
constructor( | ||
client: Client, | ||
channel: 'lunarclient:pm' | 'Lunar-Client' = 'lunarclient:pm' | ||
); | ||
|
||
/** | ||
* Node Minecraft Protocol Client | ||
* @readonly | ||
*/ | ||
client: Client; | ||
/** | ||
* Plugin channel used | ||
* @readonly | ||
*/ | ||
channel: 'lunarclient:pm' | 'Lunar-Client'; | ||
/** | ||
* Waypoints loaded by the client | ||
*/ | ||
waypoints: Waypoint[]; | ||
/** | ||
* Current teammates of the client (Array of UUIDs) | ||
*/ | ||
teammates: string[]; | ||
|
||
/** | ||
* Add a waypoint to the client | ||
* @param waypoint Waypoint to add | ||
* @returns True if successful | ||
*/ | ||
addWaypoint(waypoint: Waypoint): boolean; | ||
/** | ||
* Remove a waypoint from the client | ||
* @param waypoint Waypoint object or waypoint name | ||
* @returns True if successful | ||
*/ | ||
removeWaypoint(waypoint: Waypoint | string): boolean; | ||
/** | ||
* Remove all waypoints loaded by the player | ||
*/ | ||
removeAllWaypoints(): void; | ||
/** | ||
* Send a notification to the client. This packet is not supported by the client by default! Solar Tweaks does implement this packet, so use this packet with that in mind. | ||
* @param message Message to send | ||
* @param durationMs Duration of the message in milliseconds | ||
* @param level Message level, set to `info` by default | ||
*/ | ||
sendNotification( | ||
message: string, | ||
durationMs: number, | ||
level: 'error' | 'info' | 'success' | 'warning' = 'info' | ||
): void; | ||
/** | ||
* Add a teammate to the client. The TeamView mod must be enabled for this to work. | ||
* @param uuid UUID of the teammate to add (must be a valid UUID with dashes) | ||
* @returns True if successful | ||
*/ | ||
addTeammate(uuid: string): boolean; | ||
/** | ||
* Remove a teammate from the client. The TeamView mod must be enabled for this to work. | ||
* @param uuid UUID of the teammate to remove (must be a valid UUID with dashes) | ||
* @returns True if successful | ||
*/ | ||
removeTeammate(uuid: string): boolean; | ||
} | ||
|
||
/** | ||
* Some colors that can be used as a waypoint color | ||
*/ | ||
export declare enum WaypointColor { | ||
RED = 0xff0000, | ||
BLUE = 0x0000ff, | ||
GREEN = 0x00ff00, | ||
YELLOW = 0xffff00, | ||
AQUA = 0x00ffff, | ||
WHITE = 0xffffff, | ||
PINK = 0xff00ff, | ||
GRAY = 0x808080, | ||
} | ||
|
||
/** | ||
* Waypoint object | ||
*/ | ||
export interface Waypoint { | ||
/** | ||
* Name of the waypoint | ||
*/ | ||
name: string; | ||
/** | ||
* X coordinate of the waypoint | ||
*/ | ||
x: number; | ||
/** | ||
* Y coordinate of the waypoint | ||
*/ | ||
y: number; | ||
/** | ||
* Z coordinate of the waypoint | ||
*/ | ||
z: number; | ||
/** | ||
* Color of the waypoint | ||
*/ | ||
color: WaypointColor | number; | ||
/** | ||
* I don't really know what this is, if you know please tell me or open a pull request with this comment changed | ||
*/ | ||
forced: boolean; | ||
/** | ||
* If the waypoint is visible | ||
*/ | ||
visible: boolean; | ||
} |
Oops, something went wrong.