@zardoy/flying-squid 0.0.128 → 0.0.130

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.
@@ -287,6 +287,11 @@ const server = async function (serv, options) {
287
287
  }
288
288
  }
289
289
  }
290
+ for (const [key, entity] of Object.entries(c.blockEntities ?? {})) {
291
+ const y = Number(key.split(',')[1]);
292
+ if (y >= minY && y < minY + height)
293
+ out.blockEntities[key] = entity;
294
+ }
290
295
  return out;
291
296
  }
292
297
  };
@@ -386,6 +391,11 @@ const server = async function (serv, options) {
386
391
  0: 'minecraft:overworld',
387
392
  1: 'minecraft:the_end'
388
393
  };
394
+ /** `dimension` / `worldName` fields of the login and respawn packets for a world (custom worlds get their own level identifier so the client rebuilds its level) */
395
+ serv.worldClientInfo = (world, dimension = world?.dimensionId ?? 0) => ({
396
+ dimension: serv.supportFeature('dimensionIsAString') ? serv.dimensionNames[dimension || 0] : serv.supportFeature('dimensionIsAnInt') ? dimension || 0 : (0, dimensionCodec_1.dimensionNbt)(dimension || 0),
397
+ worldName: ['overworld', 'nether', 'end'].includes(world?.worldName) || !world?.worldName ? serv.dimensionNames[dimension || 0] : `flyingsquid:${world.worldName.toLowerCase()}`
398
+ });
389
399
  serv.overworld.blockEntityData = {};
390
400
  serv.overworld.portals = [];
391
401
  serv.pregenWorld = (world, size = 3) => {
@@ -425,23 +435,27 @@ const server = async function (serv, options) {
425
435
  .forEach(player => player.sendBlockAction(position, actionId, actionParam, blockType));
426
436
  };
427
437
  serv.chunksUsed = {};
438
+ // chunk usage is counted per world: the same chunk coordinates in the nether and the overworld are different chunks
439
+ const usedKey = (world, id) => `${world?.worldName ?? 'overworld'}:${id}`;
428
440
  serv._finishPlayerChunkLoading = (chunkX, chunkZ, player) => {
429
441
  const id = chunkX + ',' + chunkZ;
430
442
  if (player.loadedChunks[id])
431
443
  return;
432
- if (!serv.chunksUsed[id]) {
433
- serv.chunksUsed[id] = 0;
444
+ const key = usedKey(player.world, id);
445
+ if (!serv.chunksUsed[key]) {
446
+ serv.chunksUsed[key] = 0;
434
447
  }
435
- serv.chunksUsed[id]++;
448
+ serv.chunksUsed[key]++;
436
449
  player.loadedChunks[id] = true;
437
450
  };
438
451
  serv._unloadPlayerChunk = (chunkX, chunkZ, player) => {
439
452
  const id = chunkX + ',' + chunkZ;
453
+ const key = usedKey(player.world, id);
440
454
  delete player.loadedChunks[id];
441
- if (serv.chunksUsed[id] > 0) {
442
- serv.chunksUsed[id]--;
455
+ if (serv.chunksUsed[key] > 0) {
456
+ serv.chunksUsed[key]--;
443
457
  }
444
- if (!serv.chunksUsed[id] && player.world.storageProvider) {
458
+ if (!serv.chunksUsed[key] && player.world.storageProvider) {
445
459
  // Never delete chunks when we do not have a storage provider! They will be lost
446
460
  player.world.unloadColumn(chunkX, chunkZ);
447
461
  return true;
@@ -566,6 +580,7 @@ const server = async function (serv, options) {
566
580
  try {
567
581
  // Save level.dat if needed
568
582
  await serv.writeLevelDat();
583
+ await serv.savePortals?.();
569
584
  // Save worlds if backend supports it
570
585
  // prismarine-world saves chunks automatically when constructed with anvil; here we ensure flush via stop/start
571
586
  // Save players
@@ -589,6 +604,8 @@ const server = async function (serv, options) {
589
604
  for (const world of Object.values(serv.worlds)) {
590
605
  // world.throwOnReadWrite = true
591
606
  world.stopSaving();
607
+ // chunks changed since the last interval (a chest edited just before closing) are written too
608
+ void world.saveNow?.().catch(() => { });
592
609
  }
593
610
  };
594
611
  };
@@ -687,7 +704,9 @@ const player = function (player, serv, settings) {
687
704
  }, ({ x, z, chunk } /* : {x, z, chunk: import('prismarine-chunk').PCChunk} */) => {
688
705
  serv._finishPlayerChunkLoading(chunkX, chunkZ, player);
689
706
  const newLightsFormat = serv.supportFeature('newLightingDataFormat');
690
- const dumpedLights = chunk.dumpLight();
707
+ // before 1.14 the light is inside the chunk data (dump()): dumpLight() returns nothing there, and every chunk of a 1.12 / 1.13 world
708
+ // failed to send ("reading 'skyLightMask'") so such worlds never loaded
709
+ const dumpedLights = chunk.dumpLight?.() ?? {};
691
710
  const newLightsData = newLightsFormat ? packetLightArrays(chunk, dumpedLights) : undefined;
692
711
  const chunkBuffer = chunk.dump();
693
712
  const bitMap = chunk.getMask();
@@ -849,8 +868,10 @@ const player = function (player, serv, settings) {
849
868
  if (player.world === world && !opt?.force)
850
869
  return Promise.resolve();
851
870
  opt = opt || {};
852
- player.world = world;
871
+ const from = player.world;
872
+ // the chunks are released in the world they belong to: `player.world` is switched only afterwards
853
873
  player._unloadAllChunks();
874
+ player.world = world;
854
875
  if (typeof opt.gamemode !== 'undefined') {
855
876
  if (opt.gamemode !== player.gameMode)
856
877
  player.prevGameMode = player.gameMode;
@@ -858,9 +879,7 @@ const player = function (player, serv, settings) {
858
879
  }
859
880
  player._client.write('respawn', {
860
881
  previousGameMode: player.prevGameMode,
861
- dimension: serv.supportFeature('dimensionIsAString') ? serv.dimensionNames[opt.dimension || 0] : serv.supportFeature('dimensionIsAnInt') ? opt.dimension || 0 : (0, dimensionCodec_1.dimensionNbt)(opt.dimension || 0),
862
- // custom worlds get their own level identifier so the client rebuilds its level even when the dimension type is the same
863
- worldName: ['overworld', 'nether', 'end'].includes(world.worldName) || !world.worldName ? serv.dimensionNames[opt.dimension || 0] : `flyingsquid:${world.worldName.toLowerCase()}`,
882
+ ...serv.worldClientInfo(world, opt.dimension),
864
883
  difficulty: opt.difficulty || serv.difficulty,
865
884
  hashedSeed: serv.hashedSeed,
866
885
  gamemode: opt.gamemode || player.gameMode,
@@ -870,7 +889,8 @@ const player = function (player, serv, settings) {
870
889
  copyMetadata: true
871
890
  });
872
891
  await player.findSpawnPoint();
873
- player.position = opt.position ?? player.spawnPoint;
892
+ // `player.spawnPoint` is the respawn point in the overworld: another world has its own standable entry point
893
+ player.position = opt.position ?? (world === serv.overworld || !serv.worldEntryPoint ? player.spawnPoint : await serv.worldEntryPoint(world, from, player.position));
874
894
  player.sendSpawnPosition();
875
895
  player.updateAndSpawn();
876
896
  await player.sendMap();
@@ -70,6 +70,7 @@ async function read(uuid, getSpawnPoint, worldFolder, useInMemoryStorage) {
70
70
  nbtItem.tag.name = '';
71
71
  return nbtItem;
72
72
  }),
73
+ enderItems: playerData.EnderItems?.value?.value ?? [],
73
74
  new: false
74
75
  };
75
76
  }
@@ -148,7 +149,8 @@ async function save(player, worldFolder, snakeCase, theFlattening, useInMemorySt
148
149
  onGround: player.onGround,
149
150
  flying: player.flying ?? 0
150
151
  },
151
- inventory: playerInventoryToNBT(player.inventory, theFlattening)
152
+ inventory: playerInventoryToNBT(player.inventory, theFlattening),
153
+ enderItems: player.enderChestToNbt?.() ?? []
152
154
  };
153
155
  inMemoryStorage.set(player.uuid, playerData);
154
156
  }
@@ -169,6 +171,8 @@ async function save(player, worldFolder, snakeCase, theFlattening, useInMemorySt
169
171
  newUncompressedData.value.Rotation.value.value = [player.yaw, player.pitch];
170
172
  newUncompressedData.value.OnGround.value = Number(player.onGround);
171
173
  newUncompressedData.value.Inventory.value.value = playerInventoryToNBT(player.inventory, theFlattening);
174
+ const ender = player.enderChestToNbt?.() ?? [];
175
+ newUncompressedData.value.EnderItems = ender.length ? { type: 'list', value: { type: 'compound', value: ender } } : { type: 'list', value: { type: 'end', value: [] } };
172
176
  newUncompressedData.value.abilities.value.flying.value = player.flying ?? 0;
173
177
  const newDataCompressed = await (0, node_gzip_1.gzip)((0, prismarine_nbt_1.writeUncompressed)(newUncompressedData));
174
178
  await fs_1.promises.writeFile(`${worldFolder}/playerdata/${player.uuid}.dat`, newDataCompressed);
package/dist/types.d.ts CHANGED
@@ -6,10 +6,10 @@ import "./lib/modules/books.d.ts"
6
6
  import "./lib/modules/bossBars.d.ts"
7
7
  import "./lib/modules/channels.d.ts"
8
8
  import "./lib/modules/chat.d.ts"
9
- import "./lib/modules/chest.d.ts"
10
9
  import "./lib/modules/commandBlocks.d.ts"
11
10
  import "./lib/modules/commands.d.ts"
12
11
  import "./lib/modules/communication.d.ts"
12
+ import "./lib/modules/containers.d.ts"
13
13
  import "./lib/modules/crafting.d.ts"
14
14
  import "./lib/modules/customBlockBreakTime.d.ts"
15
15
  import "./lib/modules/customSync.d.ts"
@@ -51,6 +51,7 @@ import "./lib/modules/safeZones.d.ts"
51
51
  import "./lib/modules/scoreboard.d.ts"
52
52
  import "./lib/modules/settings.d.ts"
53
53
  import "./lib/modules/signs.d.ts"
54
+ import "./lib/modules/smelting.d.ts"
54
55
  import "./lib/modules/sound.d.ts"
55
56
  import "./lib/modules/spawn.d.ts"
56
57
  import "./lib/modules/stats.d.ts"
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.128",
4
+ "version": "0.0.130",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "bin": "dist/app.js",
@@ -1,6 +0,0 @@
1
- export declare const server: (serv: Server, { version }: Options) => void;
2
- declare global {
3
- interface Player {
4
- windowId: number;
5
- }
6
- }
@@ -1,121 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.server = void 0;
4
- const vec3_1 = require("vec3");
5
- const server = (serv, { version }) => {
6
- serv.once('asap', () => {
7
- // Importing necessary libraries
8
- const registry = require('prismarine-registry')(version);
9
- // Getting ALL supported blocks
10
- // Chests
11
- const blockChest = registry.blocksByName.chest;
12
- const blockEnderChest = registry.blocksByName.ender_chest;
13
- // TODO: Large chest (NOT IMPLEMENTED)
14
- // Shulker boxes
15
- let blockShulkerBox = [];
16
- if (registry.supportFeature('theShulkerBoxes')) {
17
- blockShulkerBox = [registry.blocksByName.shulker_box.id, registry.blocksByName.red_shulker_box.id,
18
- registry.blocksByName.orange_shulker_box.id, registry.blocksByName.yellow_shulker_box.id, registry.blocksByName.lime_shulker_box.id,
19
- registry.blocksByName.green_shulker_box.id, registry.blocksByName.light_blue_shulker_box.id, registry.blocksByName.cyan_shulker_box.id,
20
- registry.blocksByName.blue_shulker_box.id, registry.blocksByName.purple_shulker_box.id, registry.blocksByName.magenta_shulker_box.id,
21
- registry.blocksByName.brown_shulker_box.id, registry.blocksByName.gray_shulker_box.id, registry.blocksByName.light_gray_shulker_box.id,
22
- registry.blocksByName.black_shulker_box.id, registry.blocksByName.white_shulker_box.id, registry.blocksByName.pink_shulker_box.id];
23
- }
24
- // Showing container GUI
25
- const openContainerGUI = (block, player, guiType, title, sound) => {
26
- // Default GUI shape and content
27
- const guiContent = [
28
- { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false },
29
- { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false },
30
- { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }, { present: false }
31
- ];
32
- const invType = 2; // 3 rows, 9 slots
33
- // Filling the GUI content and reshaping the GUI (if necessary)
34
- switch (guiType) {
35
- case 'chest':
36
- // TODO: Gathering of content (NOT IMPLEMENTED)
37
- break;
38
- case 'double_chest':
39
- // TODO: Gathering of content (NOT IMPLEMENTED)
40
- break;
41
- case 'ender_chest':
42
- // TODO: Gathering of content (NOT IMPLEMENTED)
43
- break;
44
- case 'shulker_box':
45
- // TODO: Gathering of content (NOT IMPLEMENTED)
46
- break;
47
- // TODO: Add more GUIs
48
- }
49
- if (sound) {
50
- // Playing provided sound
51
- serv.playSound(sound, player.world, block.position, {});
52
- }
53
- // Opening container GUI window
54
- player._client.write('open_window', {
55
- windowId: player.windowId,
56
- inventoryType: invType,
57
- windowTitle: serv._createChatComponent(title).toNetworkFormat()
58
- });
59
- // Sending container content
60
- player._client.write('window_items', {
61
- windowId: player.windowId,
62
- stateId: 1,
63
- items: guiContent,
64
- carriedItem: { present: false }
65
- });
66
- };
67
- // Interaction with a container block
68
- const containerBlockInteractionHandler = async ({ block, player }) => {
69
- player.chat('Chests are dangerous! Wait for the next update!');
70
- return true; // NEED TO IMPLEMENT FIRST!!!
71
- try {
72
- // Getting current block and block above it
73
- const id = await player.world.getBlockType(block.position);
74
- const blockAbove = await player.world.getBlock(block.position.plus(new vec3_1.Vec3(0, 1, 0)));
75
- // If there is any block directly above container then we can't open it
76
- if (blockAbove?.boundingBox === 'block') {
77
- return true;
78
- }
79
- // Dynamic window ID feature
80
- if (player.windowId === undefined) {
81
- player.windowId = 1;
82
- }
83
- else {
84
- player.windowId = player.windowId + 1;
85
- }
86
- player.windowPos = block.position;
87
- if (id === blockChest.id) {
88
- player.windowType = 'chest';
89
- openContainerGUI(block, player, 'chest', { translate: 'container.chest' }, 'block.chest.open');
90
- return true;
91
- }
92
- if (id === blockEnderChest.id) {
93
- player.windowType = 'ender_chest';
94
- openContainerGUI(block, player, 'ender_chest', { translate: 'container.enderchest' }, 'block.chest.open');
95
- return true;
96
- }
97
- // TODO: Large chest (NOT IMPLEMENTED)
98
- if (blockShulkerBox.includes(id)) {
99
- player.windowType = 'shulker_box';
100
- openContainerGUI(block, player, 'shulker_box', { translate: 'container.shulkerBox' }, 'block.chest.open');
101
- return true;
102
- }
103
- }
104
- catch (err) {
105
- setTimeout(() => { throw err; }, 0);
106
- }
107
- };
108
- // Registering block interaction handlers
109
- // Chests
110
- serv.onBlockInteraction(registry.blocksByName.chest.name, containerBlockInteractionHandler);
111
- serv.onBlockInteraction(registry.blocksByName.ender_chest.name, containerBlockInteractionHandler);
112
- // TODO: Large chest (NOT IMPLEMENTED)
113
- // Shulker boxes
114
- if (registry.supportFeature('theShulkerBoxes')) {
115
- for (const currentShulkerBoxID of blockShulkerBox) {
116
- serv.onBlockInteraction(registry.blocks[currentShulkerBoxID].name, containerBlockInteractionHandler);
117
- }
118
- }
119
- });
120
- };
121
- exports.server = server;