@zardoy/flying-squid 0.0.134 → 0.0.136

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.
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Buckets and the obsidian that comes with them: pick up a water or lava source, pour it somewhere else.
3
+ *
4
+ * Fluids do not flow in flying-squid, so a poured bucket is one still source block and the standard recipes are replaced by their result:
5
+ * - water poured onto / next to a lava *source* turns the lava into obsidian, next to *flowing* lava into cobblestone,
6
+ * - water poured into lava does the same at that spot (no water is placed),
7
+ * - lava poured next to water becomes obsidian.
8
+ * That makes obsidian (nether portals) obtainable: iron -> bucket, water from any ocean or river, a lava lake in a cave, pour, mine with a pickaxe.
9
+ *
10
+ * Survival players swap the bucket; creative players keep their bucket (like vanilla). Fish / milk / powder snow buckets are not handled.
11
+ */
12
+ export declare const server: (serv: Server) => void;
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.server = void 0;
4
+ const vec3_1 = require("vec3");
5
+ const projectiles_1 = require("./projectiles");
6
+ /**
7
+ * Buckets and the obsidian that comes with them: pick up a water or lava source, pour it somewhere else.
8
+ *
9
+ * Fluids do not flow in flying-squid, so a poured bucket is one still source block and the standard recipes are replaced by their result:
10
+ * - water poured onto / next to a lava *source* turns the lava into obsidian, next to *flowing* lava into cobblestone,
11
+ * - water poured into lava does the same at that spot (no water is placed),
12
+ * - lava poured next to water becomes obsidian.
13
+ * That makes obsidian (nether portals) obtainable: iron -> bucket, water from any ocean or river, a lava lake in a cave, pour, mine with a pickaxe.
14
+ *
15
+ * Survival players swap the bucket; creative players keep their bucket (like vanilla). Fish / milk / powder snow buckets are not handled.
16
+ */
17
+ const server = function (serv) {
18
+ const mcData = serv.mcData;
19
+ const flat = serv.supportFeature('theFlattening');
20
+ const Item = serv.PrismarineItem;
21
+ if (!mcData.itemsByName.bucket || !mcData.blocksByName.water || !mcData.blocksByName.lava)
22
+ return;
23
+ const stateOf = (name) => {
24
+ const b = mcData.blocksByName[name];
25
+ return flat ? b.minStateId : b.id << 4;
26
+ };
27
+ const isFluid = (block, kind) => block?.name === kind || (!flat && block?.name === `flowing_${kind}`);
28
+ /** a full still source block (flattened: `level` 0, before: the still block with data 0) */
29
+ const isSource = (block, kind) => block?.name === kind && (flat ? Number(block.getProperties?.().level ?? 0) === 0 : block.metadata === 0);
30
+ const FLUIDS = ['water', 'lava'];
31
+ const kindOf = (block) => FLUIDS.find(k => isFluid(block, k));
32
+ const getBlock = (world, p) => world.sync.getBlock(p);
33
+ const sound = (world, p, name) => { try {
34
+ serv.playSound(name, world, p.offset(0.5, 0.5, 0.5));
35
+ }
36
+ catch { } };
37
+ /** first fluid source (`sources`) or solid block along the look ray, with the free cell in front of it */
38
+ const trace = (world, from, dir, range, sources) => {
39
+ let prev;
40
+ for (let d = 0; d <= range; d += 0.05) {
41
+ const pt = from.plus(dir.scaled(d));
42
+ const cell = new vec3_1.Vec3(Math.floor(pt.x), Math.floor(pt.y), Math.floor(pt.z));
43
+ if (prev?.equals(cell))
44
+ continue;
45
+ const block = getBlock(world, cell);
46
+ if (!block)
47
+ return undefined; // not loaded
48
+ if (sources && FLUIDS.some(k => isSource(block, k)))
49
+ return { cell, block, prev };
50
+ if (block.boundingBox === 'block')
51
+ return { cell, block, prev, solid: true };
52
+ prev = cell;
53
+ }
54
+ return undefined;
55
+ };
56
+ const NEIGHBOURS = [new vec3_1.Vec3(1, 0, 0), new vec3_1.Vec3(-1, 0, 0), new vec3_1.Vec3(0, 1, 0), new vec3_1.Vec3(0, -1, 0), new vec3_1.Vec3(0, 0, 1), new vec3_1.Vec3(0, 0, -1)];
57
+ /** lava that water touches becomes obsidian (source) or cobblestone (flowing) */
58
+ const hardenLava = async (world, pos) => {
59
+ const block = getBlock(world, pos);
60
+ if (!isFluid(block, 'lava'))
61
+ return false;
62
+ await serv.setBlock(world, pos, stateOf(isSource(block, 'lava') ? 'obsidian' : 'cobblestone'));
63
+ sound(world, pos, 'block.lava.extinguish');
64
+ return true;
65
+ };
66
+ const swap = (player, slot, to) => {
67
+ if (player.gameMode === 1)
68
+ return;
69
+ const index = slot === 45 ? 45 : 36 + slot;
70
+ const held = player.inventory.slots[index];
71
+ const result = new Item(mcData.itemsByName[to].id, 1);
72
+ if (held && held.count > 1) {
73
+ held.count--;
74
+ player.inventory.updateSlot(index, held);
75
+ const free = [...Array.from({ length: 9 }, (_, i) => 36 + i), ...Array.from({ length: 27 }, (_, i) => 9 + i)].find(i => !player.inventory.slots[i]);
76
+ if (free !== undefined)
77
+ player.inventory.updateSlot(free, result);
78
+ else
79
+ serv.dropItem(player.world, player.position.offset(0, 1, 0), to, 1);
80
+ }
81
+ else
82
+ player.inventory.updateSlot(index, result);
83
+ };
84
+ const eye = (player) => player.position.offset(0, 1.62, 0);
85
+ serv.once('asap', () => {
86
+ // ---------------------------------------------------------------- empty bucket: picks up a source
87
+ serv.onItemUse('bucket', async ({ player, slot }) => {
88
+ if (player.gameMode >= 2)
89
+ return;
90
+ const world = player.world;
91
+ const hit = trace(world, eye(player), (0, projectiles_1.lookDirection)(player), 5, true);
92
+ if (!hit || hit.solid)
93
+ return;
94
+ const kind = FLUIDS.find(k => isSource(hit.block, k));
95
+ if (!kind)
96
+ return;
97
+ await serv.setBlock(world, hit.cell, 0);
98
+ swap(player, slot, `${kind}_bucket`);
99
+ sound(world, hit.cell, kind === 'lava' ? 'item.bucket.fill_lava' : 'item.bucket.fill');
100
+ });
101
+ // ---------------------------------------------------------------- full buckets: pour
102
+ for (const kind of FLUIDS) {
103
+ const bucket = `${kind}_bucket`;
104
+ if (!mcData.itemsByName[bucket])
105
+ continue;
106
+ serv.onItemUse(bucket, async ({ player, slot }) => {
107
+ if (player.gameMode >= 2)
108
+ return;
109
+ const world = player.world;
110
+ // the ray stops at fluid sources too: aiming at the surface of a lava lake pours onto the surface (vanilla would pour at the bottom of it)
111
+ const hit = trace(world, eye(player), (0, projectiles_1.lookDirection)(player), 5, true);
112
+ if (!hit)
113
+ return;
114
+ const target = hit.solid ? hit.prev : hit.cell;
115
+ if (!target)
116
+ return;
117
+ const block = getBlock(world, target);
118
+ if (!block || block.boundingBox === 'block')
119
+ return;
120
+ const other = kind === 'water' ? 'lava' : 'water';
121
+ if (isFluid(block, other)) {
122
+ // water into lava / lava into water: the result is solid, nothing is placed
123
+ if (kind === 'water')
124
+ await hardenLava(world, target);
125
+ else {
126
+ await serv.setBlock(world, target, stateOf('obsidian'));
127
+ sound(world, target, 'block.lava.extinguish');
128
+ }
129
+ }
130
+ else if (isSource(block, kind)) {
131
+ return; // already there
132
+ }
133
+ else {
134
+ await serv.setBlock(world, target, stateOf(kind));
135
+ sound(world, target, kind === 'lava' ? 'item.bucket.empty_lava' : 'item.bucket.empty');
136
+ for (const d of NEIGHBOURS) {
137
+ const p = target.plus(d);
138
+ const nb = getBlock(world, p);
139
+ if (kind === 'water' && isFluid(nb, 'lava'))
140
+ await hardenLava(world, p);
141
+ else if (kind === 'lava' && isFluid(nb, 'water')) {
142
+ await serv.setBlock(world, target, stateOf('obsidian'));
143
+ sound(world, target, 'block.lava.extinguish');
144
+ break;
145
+ }
146
+ }
147
+ }
148
+ swap(player, slot, 'bucket');
149
+ });
150
+ }
151
+ });
152
+ };
153
+ exports.server = server;
@@ -223,6 +223,9 @@ const player = function (player, serv, { version }) {
223
223
  directionVector
224
224
  }, async (data) => {
225
225
  player.changeBlock(data.position, 0, 0);
226
+ // a tool loses one point per block (swords two), none for blocks that break instantly
227
+ if ((currentlyDugBlock.hardness ?? 1) > 0)
228
+ player.damageHeldItem(/sword$/.test(player.inventory.slots[36 + player.heldItemSlot]?.name ?? '') ? 2 : 1);
226
229
  const aboveBlock = await player.world.getBlock(data.position.offset(0, 1, 0));
227
230
  if (aboveBlock.material === 'plant') {
228
231
  await player.setBlock(data.position.offset(0, 1, 0), 0);
@@ -0,0 +1,7 @@
1
+ export declare const player: (player: Player, serv: Server) => void;
2
+ declare global {
3
+ interface Player {
4
+ /** wears the tool in the main hand (survival / adventure); true when it broke and was removed */
5
+ damageHeldItem: (amount?: number) => boolean;
6
+ }
7
+ }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.player = void 0;
4
+ /**
5
+ * Tool wear: mining blocks and hitting mobs use up the durability of the tool in the hand (survival and adventure only). A tool that is used up is removed and the
6
+ * clients get the "item broke" status (`entity_status` 47 main hand / 48 off hand): the client plays the break sound and shows the particles.
7
+ * No unbreaking / mending; armour does not wear (yet).
8
+ */
9
+ const TOOL = /(pickaxe|_axe|shovel|hoe|sword|shears|trident|fishing_rod|flint_and_steel|bow|crossbow)$/;
10
+ const player = function (player, serv) {
11
+ const mcData = serv.mcData;
12
+ /** wears the item in the main hand by `amount`; returns true when it broke */
13
+ player.damageHeldItem = (amount = 1) => {
14
+ if (player.gameMode === 1 || player.gameMode === 3)
15
+ return false;
16
+ const slot = 36 + player.heldItemSlot;
17
+ const item = player.inventory.slots[slot];
18
+ if (!item || !TOOL.test(item.name))
19
+ return false;
20
+ const max = item.maxDurability ?? mcData.items[item.type]?.maxDurability;
21
+ if (!max)
22
+ return false;
23
+ const used = (item.durabilityUsed ?? 0) + amount;
24
+ item.durabilityUsed = used;
25
+ if (used >= max) {
26
+ player.inventory.updateSlot(slot, null);
27
+ player._client.write('entity_status', { entityId: player.id, entityStatus: 47 });
28
+ player._writeOthersNearby('entity_status', { entityId: player.id, entityStatus: 47 });
29
+ player.emit('toolBroke', item);
30
+ return true;
31
+ }
32
+ player.inventory.updateSlot(slot, item);
33
+ return false;
34
+ };
35
+ };
36
+ exports.player = player;
@@ -8,6 +8,7 @@ module.exports = {
8
8
  'blocks': require('./blocks'),
9
9
  'books': require('./books'),
10
10
  'bossBars': require('./bossBars'),
11
+ 'buckets': require('./buckets'),
11
12
  'channels': require('./channels'),
12
13
  'chat': require('./chat'),
13
14
  'commandBlocks': require('./commandBlocks'),
@@ -22,6 +23,7 @@ module.exports = {
22
23
  'daycycle': require('./daycycle'),
23
24
  'digging': require('./digging'),
24
25
  'dimensionCodec': require('./dimensionCodec'),
26
+ 'durability': require('./durability'),
25
27
  'effects': require('./effects'),
26
28
  'endFight': require('./endFight'),
27
29
  'entities': require('./entities'),
@@ -255,10 +255,19 @@ const player = function (player, serv, { version }) {
255
255
  player.setBlock(placedPosition, stateId);
256
256
  });
257
257
  let lastItemUse = 0;
258
- player._client.on('use_item', async ({ hand = 0 }) => {
258
+ player._client.on('use_item', async ({ hand = 0, rotation }) => {
259
259
  if (Date.now() - lastItemUse < 15)
260
260
  return;
261
261
  lastItemUse = Date.now();
262
+ // the click carries the look direction (1.21.2+): it can arrive before the look packet of the same tick, buckets / pearls aim with it
263
+ if (typeof rotation?.x === 'number' && typeof rotation?.y === 'number') {
264
+ const toByte = (f) => { let b = Math.floor((f % 360) * 256 / 360); if (b < -128)
265
+ b += 256;
266
+ else if (b > 127)
267
+ b -= 256; return b; };
268
+ player.yaw = toByte(rotation.x);
269
+ player.pitch = toByte(rotation.y);
270
+ }
262
271
  const heldItem = player.inventory.slots[hand === 0 ? 36 + player.heldItemSlot : 45];
263
272
  if (!heldItem || heldItem.type === -1)
264
273
  return;
@@ -45,6 +45,8 @@ const player = function (player, serv) {
45
45
  const critical = !player.onGround && player.gameMode !== 1;
46
46
  const damage = (0, mobs_1.weaponDamage)(player.inventory.slots[36 + player.heldItemSlot]?.name) * (critical ? 1.5 : 1);
47
47
  player.behavior('attack', { attackedEntity, velocity, damage }, (o) => {
48
+ // swords lose one point per hit, other tools two
49
+ player.damageHeldItem(/sword$/.test(player.inventory.slots[36 + player.heldItemSlot]?.name ?? '') ? 1 : 2);
48
50
  attackedMob.attackedBy?.(player, o.damage ?? damage);
49
51
  serv.hurt(attackedMob, o.damage ?? damage, { source: player, from: player.position, knockback: 5 });
50
52
  });
package/dist/types.d.ts CHANGED
@@ -5,6 +5,7 @@ import "./lib/modules/blockUpdates.d.ts"
5
5
  import "./lib/modules/blocks.d.ts"
6
6
  import "./lib/modules/books.d.ts"
7
7
  import "./lib/modules/bossBars.d.ts"
8
+ import "./lib/modules/buckets.d.ts"
8
9
  import "./lib/modules/channels.d.ts"
9
10
  import "./lib/modules/chat.d.ts"
10
11
  import "./lib/modules/commandBlocks.d.ts"
@@ -19,6 +20,7 @@ import "./lib/modules/database.d.ts"
19
20
  import "./lib/modules/daycycle.d.ts"
20
21
  import "./lib/modules/digging.d.ts"
21
22
  import "./lib/modules/dimensionCodec.d.ts"
23
+ import "./lib/modules/durability.d.ts"
22
24
  import "./lib/modules/effects.d.ts"
23
25
  import "./lib/modules/endFight.d.ts"
24
26
  import "./lib/modules/entities.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.134",
4
+ "version": "0.0.136",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "bin": "dist/app.js",