Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Commit

Permalink
refactor: ♻️ complete refactor of the whole library
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardDorian committed Feb 16, 2022
1 parent 2451165 commit e5e6d25
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 93 deletions.
39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@solar-tweaks/minecraft-protocol-lunarclient",
"version": "1.0.1",
"version": "1.1.0",
"description": "Custom packets used by Lunar Client for node-minecraft-protocol",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node ./test/proxy.js"
},
"repository": {
"type": "git",
Expand All @@ -28,5 +28,8 @@
},
"publishConfig": {
"registry": "https://npm.pkg.github.com"
},
"devDependencies": {
"prismarine-proxy": "^1.1.1"
}
}
111 changes: 111 additions & 0 deletions src/LCPlayer.js
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,
};
127 changes: 122 additions & 5 deletions src/index.d.ts
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;
}
Loading

0 comments on commit e5e6d25

Please sign in to comment.