Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions class/Server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ const extensions = [
name: "ExtEntityTeleport",
version: 1,
},
{
name: "SelectionCuboid",
version: 1,
},
]
function isTrustedWebSocketProxy(remoteAddress) {
if (remoteAddress == "::ffff:34.223.5.250") return true // ClassiCube's WebSocket proxy
Expand Down
54 changes: 54 additions & 0 deletions class/extensions/SelectionCuboid.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// @ts-check
import { SmartBuffer } from "smart-buffer"
import { DataTypes } from "../DataTypes.mjs"
import { BaseExtension } from "../BaseExtension.mjs"
/** @import { Client } from "../Client.mjs" */
/** @import { selection } from "./extensionTypes.mjs" */

/** I define SelectionCuboid. */
export class SelectionCuboid extends BaseExtension {
/**
* @param {Client} client
* @param {number} version
*/
constructor(client, version) {
super(client, version)
}
static addSelectionPacketIdentifier = 0x1a
/**
* @param {selection} selection - The selection.
*/
addSelection(selection) {
const buffer = new SmartBuffer({ size: 86 }).writeUInt8(SelectionCuboid.addSelectionPacketIdentifier)
buffer.writeUInt8(selection.id)
buffer.writeBuffer(DataTypes.padString(selection.name ?? ""))
buffer.writeUInt16BE(selection.startX)
buffer.writeUInt16BE(selection.startY)
buffer.writeUInt16BE(selection.startZ)
buffer.writeUInt16BE(selection.endX)
buffer.writeUInt16BE(selection.endY)
buffer.writeUInt16BE(selection.endZ)
buffer.writeUInt16BE(selection.red ?? 128)
buffer.writeUInt16BE(selection.green ?? 128)
buffer.writeUInt16BE(selection.blue ?? 128)
buffer.writeUInt16BE(selection.opacity ?? 128)
this.client.socket.write(buffer.toBuffer())
}
static removeSelectionPacketIdentifier = 0x1b
/**
* @param {selection | number} selection - The selection or selection ID.
*/
removeSelection(selection) {
let selectionId
if (typeof selection === "number") {
selectionId = selection
} else {
selectionId = selection.id
}
const buffer = new SmartBuffer({ size: 2 }).writeUInt8(SelectionCuboid.removeSelectionPacketIdentifier)
buffer.writeUInt8(selectionId)
this.client.socket.write(buffer.toBuffer())
}
}

export default SelectionCuboid
22 changes: 22 additions & 0 deletions class/extensions/extensionTypes.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { FullCodePage437 } from "./FullCodePage437.mjs"
import { HeldBlock } from "./HeldBlock.mjs"
import { InventoryOrder } from "./InventoryOrder.mjs"
import { LevelEnvironment } from "./LevelEnvironment.mjs"
import { SelectionCuboid } from "./SelectionCuboid.mjs"
import { SetHotbar } from "./SetHotbar.mjs"

export type defaultExtensions = {
Expand All @@ -25,5 +26,26 @@ export type defaultExtensions = {
HeldBlock: HeldBlock
InventoryOrder: InventoryOrder
LevelEnvironment: LevelEnvironment
SelectionCuboid: SelectionCuboid
SetHotbar: SetHotbar
}

export interface selection {
/** A byte. */
id: number
name?: string
startX: number
startY: number
startZ: number
endX: number
endY: number
endZ: number
/** A color component of 0 to 255. */
red?: number
/** A color component of 0 to 255. */
green?: number
/** A color component of 0 to 255. */
blue?: number
/** The opacity from 0 to 255. 255 is fully opaque. */
opacity?: number
}
Comment on lines +33 to +51
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk should a class be made? classicborne uses arrays for positions but classicborne-server-protocol uses object fields wizh varying names.
but also, classicborne-server-protocol isn't too low-level, it does prefer floats over fixed-points. Color is sort of fixed point, right? maybe it should instead be 0-1? but whatever. follow what block definition does for fog colors for now

1 change: 1 addition & 0 deletions class/extensions/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export { FullCodePage437 } from "./FullCodePage437.mjs"
export { HeldBlock } from "./HeldBlock.mjs"
export { InventoryOrder } from "./InventoryOrder.mjs"
export { LevelEnvironment } from "./LevelEnvironment.mjs"
export { SelectionCuboid } from "./SelectionCuboid.mjs"
export { SetHotbar } from "./SetHotbar.mjs"
4 changes: 4 additions & 0 deletions class/extensions/packetHandlers/DefineProtocolExtension.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { FullCodePage437 } from "../FullCodePage437.mjs"
import { HeldBlock } from "../HeldBlock.mjs"
import { InventoryOrder } from "../InventoryOrder.mjs"
import { LevelEnvironment } from "../LevelEnvironment.mjs"
import { SelectionCuboid } from "../SelectionCuboid.mjs"
import { SetHotbar } from "../SetHotbar.mjs"
/** @import { SmartBuffer } from "smart-buffer" */
/** @import { Client } from "../../Client.mjs" */
Expand Down Expand Up @@ -76,6 +77,9 @@ export class DefineProtocolExtension extends BasePacketHandler {
case "EnvMapAspect":
this.client.extensions.set("LevelEnvironment", new LevelEnvironment(this.client, extension.version))
break
case "SelectionCuboid":
this.client.extensions.set("SelectionCuboid", new SelectionCuboid(this.client, extension.version))
break
case "SetHotbar":
this.client.extensions.set("SetHotbar", new SetHotbar(this.client, extension.version))
break
Expand Down