@zardoy/flying-squid 0.0.88 → 0.0.89

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.
@@ -2,5 +2,24 @@ export declare const entity: (entity: Entity, serv: Server) => void;
2
2
  export declare const server: (serv: Server, options: Options) => void;
3
3
  declare global {
4
4
  interface Entity {
5
+ "effects": Record<string, {
6
+ amplifier: number;
7
+ duration: number;
8
+ particles: boolean;
9
+ end: number;
10
+ timeout: NodeJS.Timeout;
11
+ } | null>;
12
+ "addEffect": (effectId: number, opt?: {
13
+ amplifier?: number;
14
+ duration?: number;
15
+ particles?: boolean;
16
+ whitelist?: Entity[];
17
+ blacklist?: Entity[];
18
+ override?: boolean;
19
+ }) => boolean;
20
+ removeEffect: (effectId: number, opt?: {
21
+ whitelist?: Entity[];
22
+ blacklist?: Entity[];
23
+ }) => void;
5
24
  }
6
25
  }
@@ -53,6 +53,8 @@ const entity = function (entity, serv) {
53
53
  return false;
54
54
  };
55
55
  entity.removeEffect = (effectId, opt) => {
56
+ if (!entity.effects[effectId])
57
+ return;
56
58
  clearTimeout(entity.effects[effectId].timeout);
57
59
  entity.effects[effectId] = null;
58
60
  entity.sendRemoveEffect(effectId, opt);
@@ -1,11 +1,15 @@
1
1
  import { Vec3 } from 'vec3';
2
2
  export declare const server: (serv: Server) => void;
3
- export declare const player: (player: Player, serv: Server) => void;
3
+ export declare const player: (player: Player, serv: Server, { basePositionAntiCheat, logToChatPositionAntiCheat }: Options) => void;
4
4
  export type AABB = {
5
5
  min: Vec3;
6
6
  max: Vec3;
7
7
  };
8
8
  declare global {
9
+ interface Options {
10
+ basePositionAntiCheat?: boolean;
11
+ logToChatPositionAntiCheat?: boolean;
12
+ }
9
13
  interface Server {
10
14
  safeZones: AABB[];
11
15
  resetSafeZones: () => void;
@@ -4,7 +4,11 @@ exports.player = exports.server = void 0;
4
4
  const vec3_1 = require("vec3");
5
5
  // Module to make positions safe to move to.
6
6
  // If a client sends a position outside by more than threshold, ignore and teleport back inside.
7
+ // Also includes anti-cheat features for block collision and movement speed.
7
8
  const THRESHOLD = 0.5;
9
+ const PLAYER_SIZE = new vec3_1.Vec3(0.6, 1.8, 0.6); // Standard Minecraft player hitbox
10
+ const MAX_MOVEMENT_DISTANCE = 10; // Maximum blocks per tick movement (accounting for speed effects)
11
+ const BASE_WALKING_SPEED = 0.10000000149011612; // Base movement speed from playerDat.js
8
12
  const server = (serv) => {
9
13
  serv.safeZones ??= [];
10
14
  serv.resetSafeZones = () => {
@@ -17,12 +21,42 @@ const server = (serv) => {
17
21
  };
18
22
  };
19
23
  exports.server = server;
20
- const player = (player, serv) => {
24
+ const player = (player, serv, { basePositionAntiCheat = false, logToChatPositionAntiCheat = false }) => {
25
+ let lastMovementTime = Date.now();
26
+ let lastPosition = player.position?.clone();
21
27
  // Enforce safe areas on client-provided movement
22
28
  player.on('move_cancel', ({ position, onGround, teleport }, cancel) => {
23
29
  // Allow server teleports/moves to pass through
24
30
  if (teleport)
25
31
  return;
32
+ // Anti-cheat: Block collision detection
33
+ if (basePositionAntiCheat && isPlayerInsideBlock(player, serv, position)) {
34
+ cancel(false);
35
+ if (logToChatPositionAntiCheat) {
36
+ player.chat(`Position anti-cheat: Block collision detected. Teleporting to safe position.`);
37
+ }
38
+ player.teleport(findSafePosition(player, serv, position));
39
+ return;
40
+ }
41
+ // Anti-cheat: Movement speed limit
42
+ const currentTime = Date.now();
43
+ const deltaTime = (currentTime - lastMovementTime) / 1000; // Convert to seconds
44
+ if (basePositionAntiCheat && lastPosition && !teleport) {
45
+ const maxAllowedDistance = calculateMaxMovementDistance(player, deltaTime);
46
+ const distance = position.distanceTo(lastPosition);
47
+ if (distance > maxAllowedDistance) {
48
+ if (logToChatPositionAntiCheat) {
49
+ player.chat(`Position anti-cheat: Movement speed limit exceeded. Teleporting to safe position.`);
50
+ }
51
+ cancel(false);
52
+ player.teleport(lastPosition);
53
+ return;
54
+ }
55
+ }
56
+ // Update tracking for next movement
57
+ lastMovementTime = currentTime;
58
+ lastPosition = position.clone();
59
+ // Safe zones enforcement (original logic)
26
60
  const zones = serv.safeZones ?? [];
27
61
  if (zones.length === 0)
28
62
  return;
@@ -46,11 +80,103 @@ const player = (player, serv) => {
46
80
  // Otherwise, clamp back inside from direction of attempted position
47
81
  const corrected = clampToAABBFromDirection(position, zone);
48
82
  // Prevent default cancel handling and perform corrective teleport ourselves
83
+ if (logToChatPositionAntiCheat) {
84
+ player.chat(`Position anti-cheat: Out of bounds. Teleporting to safe position.`);
85
+ }
49
86
  cancel(false);
50
87
  player.teleport(corrected);
51
88
  });
52
89
  };
53
90
  exports.player = player;
91
+ // Anti-cheat helper functions
92
+ function isPlayerInsideBlock(player, serv, position) {
93
+ const blocks = serv.mcData.blocksByStateId;
94
+ const chunks = player.world.getColumns();
95
+ const chunk = chunks[position.x >> 4][position.z >> 4];
96
+ if (!chunk)
97
+ return false;
98
+ // Check collision points around player hitbox
99
+ const checkPositions = [
100
+ position, // Center
101
+ position.offset(PLAYER_SIZE.x / 2, 0, PLAYER_SIZE.z / 2), // Top-right corner
102
+ position.offset(-PLAYER_SIZE.x / 2, 0, PLAYER_SIZE.z / 2), // Top-left corner
103
+ position.offset(PLAYER_SIZE.x / 2, 0, -PLAYER_SIZE.z / 2), // Bottom-right corner
104
+ position.offset(-PLAYER_SIZE.x / 2, 0, -PLAYER_SIZE.z / 2), // Bottom-left corner
105
+ position.offset(0, PLAYER_SIZE.y / 2, 0), // Top center
106
+ position.offset(0, PLAYER_SIZE.y, 0) // Head position
107
+ ];
108
+ for (const checkPos of checkPositions) {
109
+ try {
110
+ const pos = new vec3_1.Vec3(Math.floor(checkPos.x) & 15, Math.floor(checkPos.y), Math.floor(checkPos.z) & 15);
111
+ const blockStateId = chunk.getBlockStateId(pos);
112
+ if (blocks[blockStateId]?.boundingBox === 'block') {
113
+ return true;
114
+ }
115
+ }
116
+ catch (error) {
117
+ // If we can't get block type, assume it's safe
118
+ continue;
119
+ }
120
+ }
121
+ return false;
122
+ }
123
+ function calculateMaxMovementDistance(player, deltaTime) {
124
+ let walkingSpeed = BASE_WALKING_SPEED;
125
+ // Apply speed effect if present (effect ID 1)
126
+ if (player.effects?.[1]) {
127
+ const amplifier = player.effects[1].amplifier || 0;
128
+ walkingSpeed = BASE_WALKING_SPEED * (1 + (amplifier + 1) * 0.2);
129
+ }
130
+ // For creative mode or flying, allow higher speed
131
+ if (player.gameMode === 1 || player.gameMode === 3 || player.flying) {
132
+ walkingSpeed *= 10; // Creative/spectator mode multiplier
133
+ }
134
+ // Convert to blocks per second and apply delta time, with maximum limit
135
+ const maxDistance = Math.min(walkingSpeed * 20 * deltaTime, MAX_MOVEMENT_DISTANCE);
136
+ return maxDistance;
137
+ }
138
+ function findSafePosition(player, serv, attemptedPosition) {
139
+ // Start from the attempted position and search nearby for a safe spot
140
+ const basePosition = attemptedPosition.floored();
141
+ const chunks = player.world.getColumns();
142
+ const chunk = chunks[basePosition.x >> 4][basePosition.z >> 4];
143
+ if (!chunk)
144
+ return basePosition;
145
+ // Search in expanding radius for a safe position
146
+ for (let radius = 0; radius <= 3; radius++) {
147
+ for (let x = -radius; x <= radius; x++) {
148
+ for (let z = -radius; z <= radius; z++) {
149
+ if (Math.abs(x) === radius || Math.abs(z) === radius) { // Only check edge positions for efficiency
150
+ const testPos = basePosition.offset(x, 0, z);
151
+ // Check if position is safe (not inside blocks)
152
+ let isSafe = true;
153
+ for (let y = 0; y < Math.ceil(PLAYER_SIZE.y); y++) {
154
+ const checkPos = testPos.offset(0, y, 0);
155
+ const pos = new vec3_1.Vec3(Math.floor(checkPos.x) & 15, Math.floor(checkPos.y), Math.floor(checkPos.z) & 15);
156
+ try {
157
+ const blockStateId = chunk.getBlockStateId(pos);
158
+ if (serv.mcData.blocksByStateId[blockStateId]?.boundingBox === 'block') {
159
+ isSafe = false;
160
+ break;
161
+ }
162
+ }
163
+ catch {
164
+ // If we can't check, assume unsafe
165
+ isSafe = false;
166
+ break;
167
+ }
168
+ }
169
+ if (isSafe) {
170
+ return testPos.offset(0.5, 0, 0.5); // Center in block
171
+ }
172
+ }
173
+ }
174
+ }
175
+ }
176
+ // If no safe position found, return player's current position
177
+ return player.position;
178
+ }
179
+ // Original helper functions
54
180
  function isInsideStrict(pos, box) {
55
181
  return pos.x >= box.min.x && pos.x <= box.max.x &&
56
182
  pos.y >= box.min.y && pos.y <= box.max.y &&
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.88",
4
+ "version": "0.0.89",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "bin": "dist/app.js",