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

Commit

Permalink
fix: 🐛 add replacement methods for cooldowns
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardDorian committed Feb 18, 2022
1 parent 7047243 commit f0a3f17
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
17 changes: 14 additions & 3 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solar-tweaks/minecraft-protocol-lunarclient",
"version": "1.1.1",
"version": "1.1.2",
"description": "Custom packets used by Lunar Client for node-minecraft-protocol",
"main": "src/index.js",
"scripts": {
Expand All @@ -24,7 +24,8 @@
},
"homepage": "https://github.com/Solar-Tweaks/minecraft-protocol-lunarclient#readme",
"dependencies": {
"minecraft-protocol": "^1.30.0"
"minecraft-protocol": "^1.30.0",
"varint": "^6.0.0"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com"
Expand Down
38 changes: 38 additions & 0 deletions src/LCPlayer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const varint = require('varint');
const scheme = require('./scheme');

class LCPlayer {
Expand Down Expand Up @@ -125,6 +126,43 @@ class LCPlayer {
});
return true;
}

addCooldownManual(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.write('custom_payload', {
channel: this.channel,
data: this.#buildCooldownPacket(id, durationMs, iconId),
});
return true;
}

removeCooldownManual(id) {
if (!this.cooldowns.find((c) => c === id)) return false;
this.cooldowns = this.cooldowns.filter((c) => c !== id);
this.client.write('custom_payload', {
channel: this.channel,
data: this.#buildCooldownPacket(id, 0, 0),
});
return true;
}

#buildCooldownPacket(id, durationMs, iconId) {
const packet = Buffer.alloc(14 + id.length);
packet.write('03', 'hex');
varint.encode(id.length, packet, 1);
packet.write(id, 2);
let durationMsHex = durationMs.toString(16);
for (let index = 0; index < 4 - durationMsHex.length; index++) {
durationMsHex = '0' + durationMsHex;
}
packet.write(durationMsHex, 8 + id.length, 'hex');
packet.write('0' + iconId.toString(16), 12 + id.length, 'hex');
return packet;
}
}

const WaypointColor = {
Expand Down
14 changes: 14 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ export declare class LCPlayer {
* @returns True if successful
*/
removeCooldown(id: string): boolean;
/**
* Add a cooldown to the client. (The packet is built manually by the library, this should not be used for production, it's a temporary replacement)
* @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
*/
addCooldownManual(id: string, durationMs: number, iconId: number): boolean;
/**
* Remove a cooldown from the client. (The packet is built manually by the library, this should not be used for production, it's a temporary replacement)
* @param id String id of the cooldown
* @returns True if successful
*/
removeCooldownManual(id: string): boolean;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions test/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ proxy.on('outgoing', (data, meta, toClient, toServer) => {

proxy.on('start', (client) => {
const player = new LCPlayer(client);
player.addCooldownManual('bow', 9000, 261);
player.addCooldownManual('pearl', 16000, 368);
setTimeout(() => {
player.removeCooldownManual('pearl');
}, 1500);
});

0 comments on commit f0a3f17

Please sign in to comment.