@zardoy/flying-squid 0.0.101 → 0.0.102

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.
@@ -1,7 +1,24 @@
1
1
  import { Vec3 } from 'vec3';
2
+ import { CustomWorld } from './world';
3
+ import { IndexedData } from 'minecraft-data';
2
4
  export declare const server: (serv: Server) => void;
3
5
  export declare const player: (player: Player, serv: Server, { basePositionAntiCheat, positionAntiCheatNotifyPlayer }: Options) => void;
6
+ export declare function isPlayerInsideBlock(player: Player, serv: Server, position: Vec3): boolean;
7
+ export interface SpeedValidationResult {
8
+ isValid: boolean;
9
+ reason: string;
10
+ distanceSquared: number;
11
+ speedLimit: number;
12
+ currentVelocitySquared: number;
13
+ }
14
+ export declare function validateMovementSpeed(player: Player, lastPosition: Vec3, newPosition: Vec3, packetCount: number): SpeedValidationResult;
15
+ export declare function findSafePosition(playerWorld: CustomWorld, mcData: IndexedData, attemptedPosition: Vec3): Vec3 | undefined;
4
16
  export declare function isInsideStrict(pos: Vec3, box: AABB): boolean;
17
+ export declare function isInsideExpanded(pos: Vec3, box: AABB, threshold: number): boolean;
18
+ export declare function clampToAABBFromDirection(pos: Vec3, box: AABB): Vec3;
19
+ export declare function clamp(v: number, min: number, max: number): number;
20
+ export declare function distanceToAABB(p: Vec3, box: AABB): number;
21
+ export declare function dist1D(v: number, min: number, max: number): number;
5
22
  export type AABB = {
6
23
  min: Vec3;
7
24
  max: Vec3;
@@ -1,7 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.player = exports.server = void 0;
4
+ exports.isPlayerInsideBlock = isPlayerInsideBlock;
5
+ exports.validateMovementSpeed = validateMovementSpeed;
6
+ exports.findSafePosition = findSafePosition;
4
7
  exports.isInsideStrict = isInsideStrict;
8
+ exports.isInsideExpanded = isInsideExpanded;
9
+ exports.clampToAABBFromDirection = clampToAABBFromDirection;
10
+ exports.clamp = clamp;
11
+ exports.distanceToAABB = distanceToAABB;
12
+ exports.dist1D = dist1D;
5
13
  const vec3_1 = require("vec3");
6
14
  // Module to make positions safe to move to.
7
15
  // If a client sends a position outside by more than threshold, ignore and teleport back inside.
@@ -41,10 +49,17 @@ const player = (player, serv, { basePositionAntiCheat = false, positionAntiCheat
41
49
  const currentTime = Date.now();
42
50
  // Check if this is a response to our teleport
43
51
  if (player.pendingTeleport) {
44
- if (position.distanceTo(player.pendingTeleport) < 0.1) {
45
- lastMovementTime = currentTime;
46
- // player.knownPosition = position.clone()
47
- player.pendingTeleport = null;
52
+ cancel(false); // while waiting for teleport confirmation, ignore old movements
53
+ return;
54
+ }
55
+ if (player.validateNextPosition) {
56
+ if (player.validateNextPosition.distanceTo(position) < 0.1) {
57
+ player.validateNextPosition = undefined;
58
+ }
59
+ else {
60
+ player.teleport(player.validateNextPosition);
61
+ cancel(false);
62
+ return;
48
63
  }
49
64
  return;
50
65
  }
@@ -61,7 +76,19 @@ const player = (player, serv, { basePositionAntiCheat = false, positionAntiCheat
61
76
  if (positionAntiCheatNotifyPlayer) {
62
77
  player.chat(`[safeZones] Block collision detected. Teleporting to safe position.`);
63
78
  }
64
- player.teleport(isPlayerInsideBlock(player, serv, lastPosition) ? lastPosition : findSafePosition(player, serv, position));
79
+ let safePosition;
80
+ if (isPlayerInsideBlock(player, serv, lastPosition)) {
81
+ safePosition = lastPosition;
82
+ }
83
+ else {
84
+ safePosition = findSafePosition(player.world, serv.mcData, position);
85
+ if (!safePosition) {
86
+ // dont do anything about it, let player get out themselves
87
+ }
88
+ }
89
+ if (safePosition) {
90
+ player.teleport(safePosition);
91
+ }
65
92
  return;
66
93
  }
67
94
  // Anti-cheat: Movement speed limit (vanilla-like approach)
@@ -135,7 +162,7 @@ const player = (player, serv, { basePositionAntiCheat = false, positionAntiCheat
135
162
  });
136
163
  };
137
164
  exports.player = player;
138
- // Anti-cheat helper functions
165
+ // Anti-cheat helper functions - EXPORTED FOR TESTING
139
166
  function isPlayerInsideBlock(player, serv, position) {
140
167
  const blocks = serv.mcData.blocksByStateId;
141
168
  const chunk = player.world.getLoadedColumnAt(position);
@@ -217,50 +244,55 @@ function validateMovementSpeed(player, lastPosition, newPosition, packetCount) {
217
244
  currentVelocitySquared: estimatedVelocitySquared
218
245
  };
219
246
  }
220
- function findSafePosition(player, serv, attemptedPosition) {
247
+ function findSafePosition(playerWorld, mcData, attemptedPosition) {
221
248
  // Start from the attempted position and search nearby for a safe spot
222
249
  const basePosition = attemptedPosition.floored();
223
250
  const getBlock = (pos) => {
224
- const chunk = player.world.getLoadedColumnAt(pos);
251
+ const chunk = playerWorld.getLoadedColumnAt(pos);
225
252
  if (!chunk)
226
253
  return null;
227
254
  return chunk.getBlockStateId(new vec3_1.Vec3(Math.floor(pos.x) & 15, Math.floor(pos.y), Math.floor(pos.z) & 15));
228
255
  };
229
- // Search in expanding radius for a safe position
230
- for (let radius = 0; radius <= 3; radius++) {
256
+ const SEARCH_RADIUS = 5;
257
+ // Search in expanding radius in all directions except downward (x, z, y+)
258
+ // Start with radius 0 (current position), then expand outward
259
+ for (let radius = 0; radius <= SEARCH_RADIUS; radius++) {
260
+ // Search in a cube pattern but only allow y >= 0 (no downward search)
231
261
  for (let x = -radius; x <= radius; x++) {
232
262
  for (let z = -radius; z <= radius; z++) {
233
- if (Math.abs(x) === radius || Math.abs(z) === radius) { // Only check edge positions for efficiency
234
- const testPos = basePosition.offset(x, 0, z);
235
- // Check if position is safe (not inside blocks)
236
- let isSafe = true;
237
- for (let y = 0; y < Math.ceil(PLAYER_SIZE.y); y++) {
238
- const checkPos = testPos.offset(0, y, 0);
239
- try {
240
- const blockStateId = getBlock(checkPos);
241
- if (blockStateId === null || serv.mcData.blocksByStateId[blockStateId]?.boundingBox === 'block') {
263
+ for (let y = 0; y <= radius; y++) { // Only search upward and at same level, never down
264
+ // Only check positions at the current radius for efficiency
265
+ if (Math.abs(x) === radius || Math.abs(z) === radius || y === radius) {
266
+ const testPos = basePosition.offset(x, y, z);
267
+ // Check if position is safe (not inside blocks)
268
+ let isSafe = true;
269
+ for (let checkY = 0; checkY < Math.ceil(PLAYER_SIZE.y); checkY++) {
270
+ const checkPos = testPos.offset(0, checkY, 0);
271
+ try {
272
+ const blockStateId = getBlock(checkPos);
273
+ if (blockStateId === null || mcData.blocksByStateId[blockStateId]?.boundingBox === 'block') {
274
+ isSafe = false;
275
+ break;
276
+ }
277
+ }
278
+ catch {
279
+ // If we can't check, assume unsafe
242
280
  isSafe = false;
243
281
  break;
244
282
  }
245
283
  }
246
- catch {
247
- // If we can't check, assume unsafe
248
- isSafe = false;
249
- break;
284
+ if (isSafe) {
285
+ return testPos.offset(0.5, 0, 0.5); // Center in block
250
286
  }
251
287
  }
252
- if (isSafe) {
253
- return testPos.offset(0.5, 0, 0.5); // Center in block
254
- }
255
288
  }
256
289
  }
257
290
  }
258
291
  }
259
- serv.warn('[safeZones] no safe position found');
260
- // If no safe position found, return player's current position
261
- return player.position;
292
+ // Return undefined if no safe position found
293
+ return undefined;
262
294
  }
263
- // Original helper functions
295
+ // Helper functions - EXPORTED FOR TESTING
264
296
  function isInsideStrict(pos, box) {
265
297
  return pos.x >= box.min.x && pos.x <= box.max.x &&
266
298
  pos.y >= box.min.y && pos.y <= box.max.y &&
@@ -3,9 +3,14 @@ export declare const player: (player: Player) => void;
3
3
  export declare const entity: (entity: Entity, serv: Server) => void;
4
4
  declare global {
5
5
  interface Player {
6
+ lastTeleportId: number;
7
+ validateNextPosition?: Vec3;
6
8
  "sendAbilities": () => void;
7
9
  /** Position we're expecting the client to move to after teleport */
8
- pendingTeleport?: Vec3 | null;
10
+ pendingTeleport?: {
11
+ position: Vec3;
12
+ teleportId: number;
13
+ };
9
14
  }
10
15
  interface Entity {
11
16
  /** ID of entity on server */
@@ -56,6 +56,7 @@ const player = function (player) {
56
56
  player.sendPosition((new vec3_1.Vec3(x, y, z)), onGround);
57
57
  receivedLook(yaw, pitch, onGround);
58
58
  });
59
+ player.lastTeleportId ??= 0;
59
60
  player.sendSelfPosition = (sendChunks = true) => {
60
61
  // double position in all versions
61
62
  player._client.write('position', {
@@ -65,28 +66,31 @@ const player = function (player) {
65
66
  yaw: convToClient(player.yaw),
66
67
  pitch: convToClient(player.pitch),
67
68
  flags: 0x00,
68
- teleportId: 1
69
+ teleportId: ++player.lastTeleportId
69
70
  });
70
71
  if (sendChunks)
71
72
  player.sendChunkWhenMove();
72
73
  };
73
- let oldSetTimeout;
74
74
  player.teleport = async (position) => {
75
- // Track pending teleport for anti-cheat
76
- if (oldSetTimeout)
77
- clearTimeout(oldSetTimeout);
78
- player.pendingTeleport = position.clone();
79
75
  const notCancelled = await player.sendPosition(position, false, true);
80
76
  if (!notCancelled)
81
77
  return;
82
78
  player.sendSelfPosition();
83
- // Clear pending teleport after a short delay to handle network latency
84
- oldSetTimeout = player.setTimeout(() => {
85
- if (player.pendingTeleport?.equals(position)) {
86
- player.pendingTeleport = null;
87
- }
88
- }, 1000); // 1 second timeout
79
+ player.pendingTeleport = {
80
+ position: position.clone(),
81
+ teleportId: player.lastTeleportId
82
+ };
89
83
  };
84
+ player.onPacket('teleport_confirm', ({ teleportId }) => {
85
+ if (teleportId === player.lastTeleportId && player.pendingTeleport) {
86
+ // validate the position
87
+ player.validateNextPosition = player.pendingTeleport.position;
88
+ player.pendingTeleport = undefined;
89
+ }
90
+ else {
91
+ console.log('Invalid teleport confirm packet received', player.pendingTeleport, 'received:', teleportId);
92
+ }
93
+ });
90
94
  player.sendAbilities = () => {
91
95
  const isInvulnerable = player.gameMode === 1 || player.gameMode === 3; // Creative or Spectator
92
96
  const canFly = player.gameMode === 1 || player.gameMode === 3;
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.101",
4
+ "version": "0.0.102",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "bin": "dist/app.js",