@zardoy/flying-squid 0.0.102 → 0.0.104

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,4 +15,8 @@ declare global {
15
15
  /** Get current log file path */
16
16
  "getLogPath": () => string | null;
17
17
  }
18
+ interface Options {
19
+ /** Whether to redirect console.log output to server log file. Defaults to true. */
20
+ noConsoleLogRedirect?: boolean;
21
+ }
18
22
  }
@@ -185,16 +185,22 @@ const server = function (serv, settings) {
185
185
  const orig = console.log;
186
186
  return function () {
187
187
  readline.cursorTo(process.stdout, 0);
188
- // let tmp
189
- // try {
190
- // tmp = process.stdout
191
- // // @ts-ignore
192
- // process.stdout = process.stderr
193
- // orig.apply(console, arguments)
194
- // } finally {
195
- // process.stdout = tmp
196
- // }
188
+ // Apply original console.log to stdout
197
189
  orig.apply(console, arguments);
190
+ // Redirect to server log if enabled and not disabled
191
+ if (!settings.noConsoleLogRedirect && logStream?.writable) {
192
+ const args = Array.from(arguments);
193
+ const message = args.map(arg => typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg)).join(' ');
194
+ // Add timestamp and prefix for console.log entries
195
+ const timestamp = new Date().toISOString();
196
+ const logEntry = `[${timestamp}] [CONSOLE]: ${message}\n`;
197
+ try {
198
+ logStream.write(logEntry);
199
+ }
200
+ catch (err) {
201
+ // Silently fail if log writing fails to avoid infinite loops
202
+ }
203
+ }
198
204
  rl.prompt(true);
199
205
  };
200
206
  })();
@@ -250,19 +250,24 @@ const player = async function (player, serv, settings) {
250
250
  const theItem = registry.itemsByName[itemName] || registry.blocksByName[itemName];
251
251
  // todo test with undefined values (need to preserve!)
252
252
  if (!theItem) {
253
- console.warn(`Unknown item ${itemName} (id in player ${player.username} inventory ${itemValue})`);
253
+ console.warn(`Failed to restore inventory slot. Unknown item ${itemName} (id in player ${player.username} inventory ${itemValue})`);
254
254
  return;
255
255
  }
256
- let newItem;
257
- // todo use supports
258
- if (mcData.version['<']('1.13'))
259
- newItem = new Item(theItem.id, item.Count.value, item.Damage.value);
260
- else if (item.tag)
261
- newItem = new Item(theItem.id, item.Count.value, item.tag);
262
- else
263
- newItem = new Item(theItem.id, item.Count.value);
264
- const slot = convertInventorySlotId.fromNBT(item.Slot.value);
265
- player.inventory.updateSlot(slot, newItem);
256
+ try {
257
+ let newItem;
258
+ // todo use supports
259
+ if (mcData.version['<']('1.13'))
260
+ newItem = new Item(theItem.id, (item.Count ?? item.count)?.value ?? 1, (item.Damage ?? item.damage).value);
261
+ else if (item.tag)
262
+ newItem = new Item(theItem.id, (item.Count ?? item.count)?.value ?? 1, item.tag);
263
+ else
264
+ newItem = new Item(theItem.id, (item.Count ?? item.count)?.value ?? 1);
265
+ const slot = convertInventorySlotId.fromNBT((item.Slot ?? item.slot).value);
266
+ player.inventory.updateSlot(slot, newItem);
267
+ }
268
+ catch (err) {
269
+ console.warn(`Failed to restore inventory slot. Error while restoring item ${itemName} (id in player ${player.username} inventory ${itemValue})`, err);
270
+ }
266
271
  });
267
272
  player._client.write('held_item_slot', {
268
273
  slot: player.heldItemSlot
@@ -14,7 +14,7 @@ export interface SpeedValidationResult {
14
14
  export declare function validateMovementSpeed(player: Player, lastPosition: Vec3, newPosition: Vec3, packetCount: number): SpeedValidationResult;
15
15
  export declare function findSafePosition(playerWorld: CustomWorld, mcData: IndexedData, attemptedPosition: Vec3): Vec3 | undefined;
16
16
  export declare function isInsideStrict(pos: Vec3, box: AABB): boolean;
17
- export declare function isInsideExpanded(pos: Vec3, box: AABB, threshold: number): boolean;
17
+ export declare function isInsideExpanded(pos: Vec3, box: AABB, threshold?: number): boolean;
18
18
  export declare function clampToAABBFromDirection(pos: Vec3, box: AABB): Vec3;
19
19
  export declare function clamp(v: number, min: number, max: number): number;
20
20
  export declare function distanceToAABB(p: Vec3, box: AABB): number;
@@ -77,7 +77,7 @@ const player = (player, serv, { basePositionAntiCheat = false, positionAntiCheat
77
77
  player.chat(`[safeZones] Block collision detected. Teleporting to safe position.`);
78
78
  }
79
79
  let safePosition;
80
- if (isPlayerInsideBlock(player, serv, lastPosition)) {
80
+ if (!isPlayerInsideBlock(player, serv, lastPosition)) {
81
81
  safePosition = lastPosition;
82
82
  }
83
83
  else {
@@ -148,11 +148,16 @@ const player = (player, serv, { basePositionAntiCheat = false, positionAntiCheat
148
148
  return;
149
149
  }
150
150
  // If new position is within expanded bounds (threshold), allow it
151
- if (isInsideExpanded(position, zone, THRESHOLD))
151
+ if (isInsideExpanded(position, zone))
152
152
  return;
153
153
  // Otherwise, clamp back inside from direction of attempted position
154
154
  let corrected = clampToAABBFromDirection(position, zone);
155
- corrected = serv.onSafeZoneViolation?.(player) ?? corrected;
155
+ let customCorrection = serv.onSafeZoneViolation?.(player);
156
+ if (customCorrection && !isInsideExpanded(customCorrection, zone)) {
157
+ serv.emit('error', new Error('Invalid custom zone pos correction!'));
158
+ customCorrection = undefined;
159
+ }
160
+ corrected = customCorrection ?? corrected;
156
161
  // Prevent default cancel handling and perform corrective teleport ourselves
157
162
  if (positionAntiCheatNotifyPlayer) {
158
163
  player.chat(`[safeZones] Out of bounds ${zone.min.toString()} <-> ${zone.max.toString()}: ${position.toString()} -> ${corrected.toString()}. Teleporting to safe position.`);
@@ -298,7 +303,7 @@ function isInsideStrict(pos, box) {
298
303
  pos.y >= box.min.y && pos.y <= box.max.y &&
299
304
  pos.z >= box.min.z && pos.z <= box.max.z;
300
305
  }
301
- function isInsideExpanded(pos, box, threshold) {
306
+ function isInsideExpanded(pos, box, threshold = THRESHOLD) {
302
307
  return pos.x >= (box.min.x - threshold) && pos.x <= (box.max.x + threshold) &&
303
308
  pos.y >= (box.min.y - threshold) && pos.y <= (box.max.y + threshold) &&
304
309
  pos.z >= (box.min.z - threshold) && pos.z <= (box.max.z + threshold);
@@ -82,13 +82,15 @@ const player = function (player) {
82
82
  };
83
83
  };
84
84
  player.onPacket('teleport_confirm', ({ teleportId }) => {
85
+ if (!player.lastTeleportId)
86
+ return;
85
87
  if (teleportId === player.lastTeleportId && player.pendingTeleport) {
86
88
  // validate the position
87
89
  player.validateNextPosition = player.pendingTeleport.position;
88
90
  player.pendingTeleport = undefined;
89
91
  }
90
92
  else {
91
- console.log('Invalid teleport confirm packet received', player.pendingTeleport, 'received:', teleportId);
93
+ // console.log('Invalid teleport confirm packet received', player.pendingTeleport, 'received:', teleportId)
92
94
  }
93
95
  });
94
96
  player.sendAbilities = () => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zardoy/flying-squid",
3
3
  "description": "A minecraft server written in TS",
4
- "version": "0.0.102",
4
+ "version": "0.0.104",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "bin": "dist/app.js",