Skip to content

Commit

Permalink
feat: add serv.setWarp
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Jun 18, 2024
1 parent 8220f71 commit 6bf948b
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/lib/modules/warps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ import path from 'path'
import fs from 'fs'
import { parse } from 'yaml'
import { Vec3 } from 'vec3'
import sanitizeFilename from 'sanitize-filename'

export type WorldWarp = {
name: string
/**
* @default world
*/
world?: string
x: number
y: number
z: number
yaw?: number
pitch?: number
lastowner?: string
/** Not shown in the world & faded in map */
disabled?: boolean
color?: string
}

const existsViaStats = async (path: string) => {
Expand Down Expand Up @@ -49,6 +56,19 @@ export const server = async function (serv: Server, options: Options) {
loadWarps(warpsFolder, serv)
}

serv.setWarp = async (warp: WorldWarp) => {
if (!serv.warps.find(w => w.name === warp.name)) {
serv.warps.push(warp)
}

// write to fs, ensure dir
if (!await existsViaStats(warpsFolder)) {
await fs.promises.mkdir(warpsFolder)
const fileNameClean = sanitizeFilename(`${warp.name}.yml`)
await fs.promises.writeFile(path.join(warpsFolder, fileNameClean), `name: ${warp.name}\nworld: ${'world'}\nx: ${warp.x}\ny: ${warp.y}\nz: ${warp.z}\nyaw: ${warp.yaw}\npitch: ${warp.pitch}\nlastowner: ${warp.lastowner}`)
}
}

serv.commands.add({
base: 'warp',
info: 'Teleport to a warp',
Expand All @@ -69,11 +89,16 @@ export const server = async function (serv: Server, options: Options) {
async action ({ name, set }, { player }) {
if (!warpsFolder || !player) return
if (set) {
// write to fs, ensure dir
if (!await existsViaStats(warpsFolder)) {
await fs.promises.mkdir(warpsFolder)
await fs.promises.writeFile(path.join(warpsFolder, `${name}.yml`), `name: ${name}\nworld: ${'world'}\nx: ${player.position.x}\ny: ${player.position.y}\nz: ${player.position.z}\nyaw: ${player.yaw}\npitch: ${player.pitch}\nlastowner: ${player.uuid}`)
}
await serv.setWarp({
name,
world: player.world === serv.overworld ? 'world' : player.world === serv.netherworld ? 'nether' : 'end',
x: player.position.x,
y: player.position.y,
z: player.position.z,
yaw: player.yaw,
pitch: player.pitch,
lastowner: player.uuid
})
return
}
const warp = serv.warps.find(w => w.name === name)
Expand Down Expand Up @@ -110,7 +135,8 @@ export const server = async function (serv: Server, options: Options) {

declare global {
interface Server {
warps: WorldWarp[]
warps: WorldWarp[],
setWarp: (warp: WorldWarp) => Promise<void>
}
}

Expand Down

0 comments on commit 6bf948b

Please sign in to comment.