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

Commit

Permalink
feat: ✨ add cooldown method + utility to convert colors
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardDorian committed Feb 17, 2022
1 parent bb5fe01 commit 7047243
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 26 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ player.sendNotification('Hello World!', 5000, 'warning');
player.addTeammate('827f8c48-cdb2-4105-af39-df5a64f93490');
player.addTeammate('64fb990d-5c85-43cd-a3b1-98a44b385493');

// Removing teammates
// Removing a teammate
player.removeTeammate('64fb990d-5c85-43cd-a3b1-98a44b385493');
```

# Protodef is telling me something is wrong ⚠️
You have to disable scheme validation in order to use this library. This is a bug inside the Protodef library.
To disable the scheme validation navigate to `src/client/pluginChannels.js` (inside the `minecraft-protocol` library) and at the line **8** add a false to the `Protodef` constructor like so:
```diff
- const proto = new ProtoDef()
+ const proto = new ProtoDef(false)
```

# Authors 💖
Thanks to Beanes#4501 for the scheme!
28 changes: 28 additions & 0 deletions src/LCPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class LCPlayer {
this.channel = channel;
this.waypoints = [];
this.teammates = [];
this.cooldowns = [];

this.client.write('custom_payload', {
channel: 'REGISTER',
Expand Down Expand Up @@ -97,6 +98,33 @@ class LCPlayer {
players,
});
}

addCooldown(id, durationMs, iconId) {
if (this.cooldowns.find((c) => c === id)) return false;
this.cooldowns.push(id);
setTimeout(() => {
this.cooldowns = this.cooldowns.filter((c) => c !== id);
}, durationMs);
this.client.writeChannel(this.channel, {
id: 'cooldown',
message: id,
durationMs,
iconId,
});
return true;
}

removeCooldown(id) {
if (!this.cooldowns.find((c) => c === id)) return false;
this.cooldowns = this.cooldowns.filter((c) => c !== id);
this.client.writeChannel(this.channel, {
id: 'cooldown',
message: id,
durationMs: 0,
iconId: 0,
});
return true;
}
}

const WaypointColor = {
Expand Down
41 changes: 33 additions & 8 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
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
* 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 class LCPlayer {
/**
* Creates a new LunarClient player
* 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');

/**
* Node Minecraft Protocol Client
* @readonly
*/
client: Client;
readonly client: Client;
/**
* Plugin channel used
* @readonly
*/
channel: 'lunarclient:pm' | 'Lunar-Client';
readonly channel: 'lunarclient:pm' | 'Lunar-Client';
/**
* Waypoints loaded by the client
*/
Expand All @@ -31,15 +29,19 @@ export declare class LCPlayer {
* Current teammates of the client (Array of UUIDs)
*/
teammates: string[];
/**
* Current active cooldowns (Array of ids, strings)
*/
cooldowns: string[];

/**
* Add a waypoint to the client
* 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
* Remove a waypoint from the client.
* @param waypoint Waypoint object or waypoint name
* @returns True if successful
*/
Expand Down Expand Up @@ -75,8 +77,31 @@ export declare class LCPlayer {
* Remove all teammates from the client. The TeamView mod must be enabled for this to work.
*/
removeAllTeammates(): void;
/**
* Add a cooldown to the client.
* @deprecated Protodef is not able to serialize the packet for some reason
* @param id String id of the cooldown, used to remove it later
* @param durationMs Duration of the message in milliseconds
* @param iconId Icon id to use, same system as [minecraft ids](https://minecraft-ids.grahamedgecombe.com/)
* @returns True if successful
*/
addCooldown(id: string, durationMs: number, iconId: number): boolean;
/**
* Remove a cooldown from the client.
* @deprecated Protodef is not able to serialize the packet for some reason
* @param id String id of the cooldown
* @returns True if successful
*/
removeCooldown(id: string): boolean;
}

/**
* Converts a hexadecimal color to a number that can be used as a color (waypoint color for example)
* @param color Hexadecimal representation of the color (works with and without a # at the beginning)
* @returns The number to use or `NaN` if the provided hexadecimal color code is invalid
*/
export declare function convertHexColor(color: string): number;

/**
* Some colors that can be used as a waypoint color
*/
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const scheme = require('./scheme');
const { LCPlayer, WaypointColor } = require('./LCPlayer');

// Utility functions
const convertHexColor = require('./utils/convertHexColor');

// const util = require('util');
// console.log(
// util.inspect(scheme1, { showHidden: false, depth: null, colors: true })
Expand All @@ -10,4 +13,5 @@ module.exports = {
scheme,
LCPlayer,
WaypointColor,
convertHexColor,
};
4 changes: 4 additions & 0 deletions src/utils/convertHexColor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = (color) => {
const _color = color.startsWith('#') ? color.substring(1) : color;
return parseInt(`0x${_color}`);
};
18 changes: 1 addition & 17 deletions test/proxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { InstantConnectProxy } = require('prismarine-proxy');
const { LCPlayer, WaypointColor } = require('../src');
const { LCPlayer, convertHexColor } = require('../src');

const proxy = new InstantConnectProxy({
loginHandler: (client) => {
Expand All @@ -24,20 +24,4 @@ proxy.on('outgoing', (data, meta, toClient, toServer) => {

proxy.on('start', (client) => {
const player = new LCPlayer(client);
player.addWaypoint({
name: 'Spawn',
color: WaypointColor.PINK,
x: 0,
y: 64,
z: 0,
forced: false,
visible: true,
});

player.addTeammate('64fb990d-5c85-43cd-a3b1-98a44b385493');

setTimeout(() => {
player.removeTeammate('64fb990d-5c85-43cd-a3b1-98a44b385493');
player.removeAllWaypoints();
}, 5000);
});

0 comments on commit 7047243

Please sign in to comment.