@zardoy/flying-squid 0.0.129 → 0.0.131
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.
- package/dist/lib/containers/gameData.d.ts +45 -0
- package/dist/lib/containers/gameData.js +173 -0
- package/dist/lib/containers/loot.d.ts +67 -0
- package/dist/lib/containers/loot.js +388 -0
- package/dist/lib/containers/loot.test.d.ts +1 -0
- package/dist/lib/containers/loot.test.js +61 -0
- package/dist/lib/containers/store.d.ts +33 -0
- package/dist/lib/containers/store.js +190 -0
- package/dist/lib/modules/containers.d.ts +23 -0
- package/dist/lib/modules/containers.js +338 -0
- package/dist/lib/modules/index.js +2 -1
- package/dist/lib/modules/inventory.d.ts +5 -1
- package/dist/lib/modules/inventory.js +6 -5
- package/dist/lib/modules/playerLogin.d.ts +4 -0
- package/dist/lib/modules/playerLogin.js +11 -2
- package/dist/lib/modules/portal.d.ts +5 -1
- package/dist/lib/modules/portal.js +56 -1
- package/dist/lib/modules/respawn.js +8 -0
- package/dist/lib/modules/smelting.d.ts +6 -0
- package/dist/lib/modules/smelting.js +323 -0
- package/dist/lib/modules/world.d.ts +5 -0
- package/dist/lib/modules/world.js +29 -11
- package/dist/lib/playerDat.js +5 -1
- package/dist/types.d.ts +2 -1
- package/package.json +1 -1
- package/dist/lib/modules/chest.d.ts +0 -6
- package/dist/lib/modules/chest.js +0 -121
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const loot_1 = require("./loot");
|
|
5
|
+
const env = {
|
|
6
|
+
getTable: (id) => id === 'minecraft:chests/inner' ? { pools: [{ rolls: 1, entries: [{ type: 'minecraft:item', name: 'minecraft:apple' }] }] } : undefined,
|
|
7
|
+
itemInfo: (name) => ['diamond', 'iron_ingot', 'apple', 'bread', 'iron_sword'].includes(name) ? { stackSize: name === 'iron_sword' ? 1 : 64, maxDurability: name === 'iron_sword' ? 250 : undefined } : undefined
|
|
8
|
+
};
|
|
9
|
+
(0, vitest_1.describe)('JavaRandom', () => {
|
|
10
|
+
(0, vitest_1.it)('matches java.util.Random', () => {
|
|
11
|
+
(0, vitest_1.expect)(new loot_1.JavaRandom(42).nextInt()).toBe(-1170105035);
|
|
12
|
+
const r = new loot_1.JavaRandom(0);
|
|
13
|
+
(0, vitest_1.expect)([r.nextInt(10), r.nextInt(10), r.nextInt(10)]).toEqual([0, 8, 9]);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
(0, vitest_1.describe)('loot tables', () => {
|
|
17
|
+
const table = {
|
|
18
|
+
pools: [
|
|
19
|
+
{ rolls: { min: 2, max: 4 }, entries: [
|
|
20
|
+
{ type: 'minecraft:item', name: 'minecraft:diamond', weight: 1, modifier: { type: 'minecraft:set_count', count: { type: 'minecraft:uniform', min: 1, max: 3 } } },
|
|
21
|
+
{ type: 'minecraft:item', name: 'minecraft:iron_ingot', weight: 9, functions: [{ function: 'minecraft:set_count', count: 5 }] },
|
|
22
|
+
{ type: 'minecraft:empty', weight: 2 },
|
|
23
|
+
{ type: 'minecraft:item', name: 'minecraft:not_in_this_version', weight: 3 }
|
|
24
|
+
] },
|
|
25
|
+
{ rolls: 1, entries: [{ type: 'minecraft:loot_table', value: 'minecraft:chests/inner' }] },
|
|
26
|
+
{ rolls: 1, condition: { type: 'minecraft:random_chance', chance: 0 }, entries: [{ type: 'minecraft:item', name: 'minecraft:bread' }] }
|
|
27
|
+
]
|
|
28
|
+
};
|
|
29
|
+
(0, vitest_1.it)('is repeatable for a seed and fills only existing slots', () => {
|
|
30
|
+
const a = (0, loot_1.fillContainer)(table, 27, 123n, env);
|
|
31
|
+
const b = (0, loot_1.fillContainer)(table, 27, 123n, env);
|
|
32
|
+
(0, vitest_1.expect)(a).toEqual(b);
|
|
33
|
+
const slots = a.map(([slot]) => slot);
|
|
34
|
+
(0, vitest_1.expect)(new Set(slots).size).toBe(slots.length);
|
|
35
|
+
(0, vitest_1.expect)(slots.every(s => s >= 0 && s < 27)).toBe(true);
|
|
36
|
+
// the nested table always gives an apple, the 0 % pool never gives bread, unknown items are dropped
|
|
37
|
+
(0, vitest_1.expect)(a.some(([, s]) => s.name === 'apple')).toBe(true);
|
|
38
|
+
(0, vitest_1.expect)(a.some(([, s]) => s.name === 'bread' || s.name === 'not_in_this_version')).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
(0, vitest_1.it)('differs between seeds', () => {
|
|
41
|
+
const sets = new Set();
|
|
42
|
+
for (let seed = 1; seed <= 20; seed++)
|
|
43
|
+
sets.add(JSON.stringify((0, loot_1.fillContainer)(table, 27, BigInt(seed), env)));
|
|
44
|
+
(0, vitest_1.expect)(sets.size).toBeGreaterThan(10);
|
|
45
|
+
});
|
|
46
|
+
(0, vitest_1.it)('respects the free slots and the total of the rolled count', () => {
|
|
47
|
+
const only = [3, 7, 8];
|
|
48
|
+
const out = (0, loot_1.fillContainer)({ pools: [{ rolls: 1, entries: [{ type: 'item', name: 'iron_ingot', functions: [{ function: 'set_count', count: 20 }] }] }] }, 27, 5n, env, only);
|
|
49
|
+
(0, vitest_1.expect)(out.every(([slot]) => only.includes(slot))).toBe(true);
|
|
50
|
+
(0, vitest_1.expect)(out.reduce((n, [, s]) => n + s.count, 0)).toBe(20);
|
|
51
|
+
});
|
|
52
|
+
(0, vitest_1.it)('splits stacks over the stack size', () => {
|
|
53
|
+
const out = (0, loot_1.fillContainer)({ pools: [{ rolls: 1, entries: [{ type: 'item', name: 'iron_ingot', functions: [{ function: 'set_count', count: 150 }] }] }] }, 27, 9n, env);
|
|
54
|
+
(0, vitest_1.expect)(out.reduce((n, [, s]) => n + s.count, 0)).toBe(150);
|
|
55
|
+
(0, vitest_1.expect)(out.every(([, s]) => s.count <= 64)).toBe(true);
|
|
56
|
+
});
|
|
57
|
+
(0, vitest_1.it)('damages items by a fraction of their durability', () => {
|
|
58
|
+
const out = (0, loot_1.fillContainer)({ pools: [{ rolls: 1, entries: [{ type: 'item', name: 'iron_sword', modifier: { type: 'set_damage', damage: 0.5 } }] }] }, 27, 1n, env);
|
|
59
|
+
(0, vitest_1.expect)(out[0][1].damage).toBe(125);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Vec3 } from 'vec3';
|
|
2
|
+
import type { Item } from 'prismarine-item';
|
|
3
|
+
import { GameData } from './gameData';
|
|
4
|
+
import { LootStack } from './loot';
|
|
5
|
+
/** `[hi, lo]` / bigint / number NBT long as a bigint */
|
|
6
|
+
export declare function longToBigint(v: any): bigint;
|
|
7
|
+
export interface ContainerStore {
|
|
8
|
+
gameData: GameData;
|
|
9
|
+
keyOf(pos: {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
z: number;
|
|
13
|
+
}): string;
|
|
14
|
+
/** the block entity (field map) of a position, created when `create` (block name) is given */
|
|
15
|
+
entityAt(world: any, pos: Vec3, create?: string): Promise<any | undefined>;
|
|
16
|
+
removeEntity(world: any, pos: Vec3): Promise<any | undefined>;
|
|
17
|
+
readSlots(entity: any, size: number): Array<Item | null>;
|
|
18
|
+
writeSlots(entity: any, slots: Array<Item | null>): void;
|
|
19
|
+
itemToNbt(item: Item, slot: number): any;
|
|
20
|
+
itemFromNbt(nbtItem: any): Item | null;
|
|
21
|
+
stackToItem(stack: LootStack): Item | undefined;
|
|
22
|
+
/** replaces a `LootTable` of the block entity by the generated items; returns false when there is no table or it is unknown */
|
|
23
|
+
unpackLoot(world: any, pos: Vec3, size: number): Promise<boolean>;
|
|
24
|
+
/** puts a loot table (and optionally its seed) into a container, the items are generated when it is opened or broken */
|
|
25
|
+
setLootTable(world: any, pos: Vec3, blockName: string, table: string, seed?: bigint): Promise<void>;
|
|
26
|
+
/** the slots of a container block entity as a list of items (unpacks loot first) */
|
|
27
|
+
itemsOf(world: any, pos: Vec3, size: number): Promise<Array<Item | null>>;
|
|
28
|
+
/** items fall out of a container block at `pos` (set by the containers module) */
|
|
29
|
+
dropStacks?: (world: any, pos: Vec3, items: Array<Item | null>) => void;
|
|
30
|
+
setSlot(world: any, pos: Vec3, blockName: string, size: number, slot: number, item: Item | null): Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
export declare function createContainerStore(serv: Server, options: Options): ContainerStore;
|
|
33
|
+
export type { LootStack };
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.longToBigint = longToBigint;
|
|
4
|
+
exports.createContainerStore = createContainerStore;
|
|
5
|
+
const gameData_1 = require("./gameData");
|
|
6
|
+
const loot_1 = require("./loot");
|
|
7
|
+
const blockRenames_1 = require("../../blockRenames");
|
|
8
|
+
/** names of the block entity of each container block (flattened id, pre-1.11 id) */
|
|
9
|
+
const LEGACY_IDS = { chest: 'Chest', trapped_chest: 'Chest', ender_chest: 'EnderChest', hopper: 'Hopper', dispenser: 'Trap', dropper: 'Dropper', furnace: 'Furnace', shulker_box: 'ShulkerBox' };
|
|
10
|
+
const str = (value) => ({ type: 'string', value });
|
|
11
|
+
const int = (value) => ({ type: 'int', value });
|
|
12
|
+
const skipMc = (id) => id.replace(/^minecraft:/, '');
|
|
13
|
+
/** `[hi, lo]` / bigint / number NBT long as a bigint */
|
|
14
|
+
function longToBigint(v) {
|
|
15
|
+
if (Array.isArray(v))
|
|
16
|
+
return (BigInt(v[0]) << 32n) | BigInt(v[1] >>> 0);
|
|
17
|
+
return BigInt(v ?? 0);
|
|
18
|
+
}
|
|
19
|
+
function createContainerStore(serv, options) {
|
|
20
|
+
const mcData = serv.mcData;
|
|
21
|
+
const ItemClass = serv.PrismarineItem;
|
|
22
|
+
const flat = serv.supportFeature('theFlattening');
|
|
23
|
+
const withComponents = serv.supportFeature('itemsWithComponents');
|
|
24
|
+
const version = options.version;
|
|
25
|
+
const gameData = new gameData_1.GameData({ gameDataJars: options.gameDataJars, worldFolder: options.worldFolder, lootTables: options.lootTables, version }, undefined);
|
|
26
|
+
const keyOf = (pos) => `${pos.x},${pos.y},${pos.z}`;
|
|
27
|
+
const renameItem = (name) => {
|
|
28
|
+
try {
|
|
29
|
+
return flat ? (0, blockRenames_1.getRenamedData)('items', name, '26.3', version) ?? name : name;
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return name;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const itemDef = (name) => mcData.itemsByName[name] ?? mcData.itemsByName[renameItem(name)] ?? (mcData.blocksByName[name] && mcData.itemsByName[mcData.blocksByName[name].name]);
|
|
36
|
+
const env = {
|
|
37
|
+
getTable: id => gameData.lootTable(id),
|
|
38
|
+
itemInfo: name => { const d = itemDef(name); return d ? { stackSize: d.stackSize ?? 64, maxDurability: d.maxDurability } : undefined; },
|
|
39
|
+
enchantments: (mcData.enchantmentsArray ?? []).map((e) => ({ name: e.name, maxLevel: e.maxLevel, weight: e.weight, minCost: e.minCost, maxCost: e.maxCost, category: e.category, exclude: e.exclude })),
|
|
40
|
+
getItemTag: id => gameData.itemTag(id)
|
|
41
|
+
};
|
|
42
|
+
const store = {
|
|
43
|
+
gameData,
|
|
44
|
+
keyOf,
|
|
45
|
+
async entityAt(world, pos, create) {
|
|
46
|
+
const column = await world.getColumnAt(pos);
|
|
47
|
+
if (!column)
|
|
48
|
+
return undefined;
|
|
49
|
+
column.blockEntities ??= {};
|
|
50
|
+
const key = keyOf(pos);
|
|
51
|
+
let entity = column.blockEntities[key];
|
|
52
|
+
if (!entity && create) {
|
|
53
|
+
entity = column.blockEntities[key] = {
|
|
54
|
+
id: str(flat ? `minecraft:${create}` : LEGACY_IDS[create] ?? create),
|
|
55
|
+
x: int(pos.x),
|
|
56
|
+
y: int(pos.y),
|
|
57
|
+
z: int(pos.z),
|
|
58
|
+
Items: { type: 'list', value: { type: 'end', value: [] } }
|
|
59
|
+
};
|
|
60
|
+
world.saveAt?.(pos);
|
|
61
|
+
}
|
|
62
|
+
return entity;
|
|
63
|
+
},
|
|
64
|
+
async removeEntity(world, pos) {
|
|
65
|
+
const column = await world.getColumnAt(pos);
|
|
66
|
+
const key = keyOf(pos);
|
|
67
|
+
const entity = column?.blockEntities?.[key];
|
|
68
|
+
if (entity) {
|
|
69
|
+
delete column.blockEntities[key];
|
|
70
|
+
world.saveAt?.(pos);
|
|
71
|
+
}
|
|
72
|
+
return entity;
|
|
73
|
+
},
|
|
74
|
+
itemToNbt(item, slot) {
|
|
75
|
+
const out = { Slot: { type: 'byte', value: slot }, id: str(`minecraft:${item.name}`) };
|
|
76
|
+
if (withComponents)
|
|
77
|
+
out.count = int(item.count);
|
|
78
|
+
else
|
|
79
|
+
out.Count = { type: 'byte', value: item.count };
|
|
80
|
+
if (!flat)
|
|
81
|
+
out.Damage = { type: 'short', value: item.metadata ?? 0 };
|
|
82
|
+
else if (!withComponents && item.nbt && Object.keys(item.nbt.value ?? {}).length)
|
|
83
|
+
out.tag = item.nbt;
|
|
84
|
+
return out;
|
|
85
|
+
},
|
|
86
|
+
itemFromNbt(nbtItem) {
|
|
87
|
+
const name = skipMc(String(nbtItem?.id?.value ?? ''));
|
|
88
|
+
const def = mcData.itemsByName[name] ?? mcData.blocksByName[name];
|
|
89
|
+
if (!def)
|
|
90
|
+
return null;
|
|
91
|
+
const count = (nbtItem.Count ?? nbtItem.count)?.value ?? 1;
|
|
92
|
+
if (!flat)
|
|
93
|
+
return new ItemClass(def.id, count, nbtItem.Damage?.value ?? 0);
|
|
94
|
+
const tag = nbtItem.tag;
|
|
95
|
+
if (tag && !tag.name)
|
|
96
|
+
tag.name = '';
|
|
97
|
+
return withComponents || !tag ? new ItemClass(def.id, count) : new ItemClass(def.id, count, 0, tag);
|
|
98
|
+
},
|
|
99
|
+
readSlots(entity, size) {
|
|
100
|
+
const slots = new Array(size).fill(null);
|
|
101
|
+
for (const nbtItem of entity?.Items?.value?.value ?? []) {
|
|
102
|
+
const slot = nbtItem.Slot?.value;
|
|
103
|
+
if (typeof slot !== 'number' || slot < 0 || slot >= size)
|
|
104
|
+
continue;
|
|
105
|
+
slots[slot] = store.itemFromNbt(nbtItem);
|
|
106
|
+
}
|
|
107
|
+
return slots;
|
|
108
|
+
},
|
|
109
|
+
writeSlots(entity, slots) {
|
|
110
|
+
const items = slots.flatMap((item, slot) => item ? [store.itemToNbt(item, slot)] : []);
|
|
111
|
+
entity.Items = items.length ? { type: 'list', value: { type: 'compound', value: items } } : { type: 'list', value: { type: 'end', value: [] } };
|
|
112
|
+
},
|
|
113
|
+
stackToItem(s) {
|
|
114
|
+
let name = s.name;
|
|
115
|
+
if (name === 'book' && s.enchantments?.length)
|
|
116
|
+
name = 'enchanted_book';
|
|
117
|
+
const def = itemDef(name);
|
|
118
|
+
if (!def)
|
|
119
|
+
return undefined;
|
|
120
|
+
if (!flat)
|
|
121
|
+
return new ItemClass(def.id, s.count, s.damage ?? 0);
|
|
122
|
+
if (withComponents)
|
|
123
|
+
return new ItemClass(def.id, s.count);
|
|
124
|
+
const value = {};
|
|
125
|
+
if (s.damage !== undefined)
|
|
126
|
+
value.Damage = int(s.damage);
|
|
127
|
+
const known = (s.enchantments ?? []).filter(([n]) => mcData.enchantmentsByName?.[n]);
|
|
128
|
+
if (known.length) {
|
|
129
|
+
value[name === 'enchanted_book' ? 'StoredEnchantments' : 'Enchantments'] = {
|
|
130
|
+
type: 'list',
|
|
131
|
+
value: { type: 'compound', value: known.map(([n, lvl]) => ({ id: str(`minecraft:${n}`), lvl: { type: 'short', value: lvl } })) }
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
if (s.potion)
|
|
135
|
+
value.Potion = str(`minecraft:${s.potion}`);
|
|
136
|
+
if (s.customName)
|
|
137
|
+
value.display = { type: 'compound', value: { Name: str(JSON.stringify({ translate: s.customName })) } };
|
|
138
|
+
return new ItemClass(def.id, s.count, 0, Object.keys(value).length ? { type: 'compound', name: '', value } : undefined);
|
|
139
|
+
},
|
|
140
|
+
async unpackLoot(world, pos, size) {
|
|
141
|
+
const entity = await store.entityAt(world, pos);
|
|
142
|
+
const tableId = entity?.LootTable?.value;
|
|
143
|
+
if (!tableId)
|
|
144
|
+
return false;
|
|
145
|
+
await gameData.ready;
|
|
146
|
+
const table = gameData.lootTable(String(tableId));
|
|
147
|
+
if (!table)
|
|
148
|
+
return false;
|
|
149
|
+
const seed = entity.LootTableSeed ? longToBigint(entity.LootTableSeed.value) : undefined;
|
|
150
|
+
const existing = store.readSlots(entity, size);
|
|
151
|
+
const free = existing.flatMap((it, i) => it ? [] : [i]);
|
|
152
|
+
const slots = existing;
|
|
153
|
+
for (const [slot, stack] of (0, loot_1.fillContainer)(table, size, seed, env, free)) {
|
|
154
|
+
const item = store.stackToItem(stack);
|
|
155
|
+
if (item)
|
|
156
|
+
slots[slot] = item;
|
|
157
|
+
}
|
|
158
|
+
store.writeSlots(entity, slots);
|
|
159
|
+
delete entity.LootTable;
|
|
160
|
+
delete entity.LootTableSeed;
|
|
161
|
+
world.saveAt?.(pos);
|
|
162
|
+
return true;
|
|
163
|
+
},
|
|
164
|
+
async setLootTable(world, pos, blockName, table, seed) {
|
|
165
|
+
const entity = (await store.entityAt(world, pos, blockName));
|
|
166
|
+
entity.LootTable = str(table.includes(':') ? table : `minecraft:${table}`);
|
|
167
|
+
if (seed)
|
|
168
|
+
entity.LootTableSeed = { type: 'long', value: [Number(BigInt.asIntN(32, seed >> 32n)), Number(BigInt.asIntN(32, seed))] };
|
|
169
|
+
else
|
|
170
|
+
delete entity.LootTableSeed;
|
|
171
|
+
world.saveAt?.(pos);
|
|
172
|
+
},
|
|
173
|
+
async itemsOf(world, pos, size) {
|
|
174
|
+
await store.unpackLoot(world, pos, size);
|
|
175
|
+
const entity = await store.entityAt(world, pos);
|
|
176
|
+
return entity ? store.readSlots(entity, size) : new Array(size).fill(null);
|
|
177
|
+
},
|
|
178
|
+
async setSlot(world, pos, blockName, size, slot, item) {
|
|
179
|
+
const entity = (await store.entityAt(world, pos, blockName));
|
|
180
|
+
// edited in the raw list: items of this version the server does not know stay untouched
|
|
181
|
+
const raw = (entity.Items?.value?.value ?? []).filter((n) => n.Slot?.value !== slot);
|
|
182
|
+
if (item)
|
|
183
|
+
raw.push(store.itemToNbt(item, slot));
|
|
184
|
+
raw.sort((a, b) => a.Slot.value - b.Slot.value);
|
|
185
|
+
entity.Items = raw.length ? { type: 'list', value: { type: 'compound', value: raw } } : { type: 'list', value: { type: 'end', value: [] } };
|
|
186
|
+
world.saveAt?.(pos);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
return store;
|
|
190
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Vec3 } from 'vec3';
|
|
2
|
+
import type { Item } from 'prismarine-item';
|
|
3
|
+
import { ContainerStore } from '../containers/store';
|
|
4
|
+
export declare const server: (serv: Server, options: Options) => void;
|
|
5
|
+
export declare const player: (player: Player, serv: Server) => void;
|
|
6
|
+
declare global {
|
|
7
|
+
interface Server {
|
|
8
|
+
/** contents of containers: the store of block entities, loot tables and recipes */
|
|
9
|
+
containers: ContainerStore;
|
|
10
|
+
/** opens the container block at `pos` for the player (chest, barrel, shulker box, hopper, dispenser, dropper, ender chest); false when there is none */
|
|
11
|
+
openContainer: (player: Player, world: any, pos: Vec3) => Promise<boolean>;
|
|
12
|
+
}
|
|
13
|
+
interface Player {
|
|
14
|
+
/** the ender chest of the player (27 slots), saved with the player */
|
|
15
|
+
enderChest: Array<Item | null>;
|
|
16
|
+
}
|
|
17
|
+
interface Options {
|
|
18
|
+
/** zips / jars with `data/<ns>/loot_table` and `recipe` (default: the vanilla client jar of minecraft-worldgen, see GameData) */
|
|
19
|
+
gameDataJars?: string[];
|
|
20
|
+
/** loot tables given directly (`{ 'minecraft:chests/foo': {...} }` or a function) */
|
|
21
|
+
lootTables?: Record<string, any> | ((id: string) => any);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.player = exports.server = void 0;
|
|
7
|
+
const vec3_1 = require("vec3");
|
|
8
|
+
const user_error_1 = __importDefault(require("../user_error"));
|
|
9
|
+
const store_1 = require("../containers/store");
|
|
10
|
+
const HORIZONTAL = { north: [0, -1], south: [0, 1], west: [-1, 0], east: [1, 0] };
|
|
11
|
+
const CLOCKWISE = { north: 'east', east: 'south', south: 'west', west: 'north' };
|
|
12
|
+
const COUNTER_CLOCKWISE = Object.fromEntries(Object.entries(CLOCKWISE).map(([a, b]) => [b, a]));
|
|
13
|
+
const kindOf = (name) => {
|
|
14
|
+
if (name === 'chest' || name === 'trapped_chest')
|
|
15
|
+
return 'chest';
|
|
16
|
+
if (name === 'ender_chest')
|
|
17
|
+
return 'ender_chest';
|
|
18
|
+
if (name === 'barrel' || name === 'hopper' || name === 'dispenser' || name === 'dropper')
|
|
19
|
+
return name;
|
|
20
|
+
if (name === 'shulker_box' || name.endsWith('_shulker_box'))
|
|
21
|
+
return 'shulker_box';
|
|
22
|
+
return undefined;
|
|
23
|
+
};
|
|
24
|
+
const SIZES = { chest: 27, ender_chest: 27, barrel: 27, shulker_box: 27, hopper: 5, dispenser: 9, dropper: 9 };
|
|
25
|
+
const server = (serv, options) => {
|
|
26
|
+
const store = serv.containers = (0, store_1.createContainerStore)(serv, options);
|
|
27
|
+
const mcData = serv.mcData;
|
|
28
|
+
const flat = serv.supportFeature('theFlattening');
|
|
29
|
+
const newWindows = serv.supportFeature('village&pillageInventoryWindows');
|
|
30
|
+
const PBlock = serv.PrismarineBlock;
|
|
31
|
+
/** viewers of every container part: `${world}:${x},${y},${z}` -> windows that show it */
|
|
32
|
+
const viewers = new Map();
|
|
33
|
+
const viewKey = (world, pos) => `${world.worldName ?? 'world'}:${store.keyOf(pos)}`;
|
|
34
|
+
// ----------------------------------------------------------------- what a block is
|
|
35
|
+
const chestPartner = (props, pos) => {
|
|
36
|
+
if (props.type !== 'left' && props.type !== 'right')
|
|
37
|
+
return undefined;
|
|
38
|
+
const dir = props.type === 'left' ? CLOCKWISE[props.facing] : COUNTER_CLOCKWISE[props.facing];
|
|
39
|
+
const [dx, dz] = HORIZONTAL[dir] ?? [0, 0];
|
|
40
|
+
return pos.offset(dx, 0, dz);
|
|
41
|
+
};
|
|
42
|
+
async function describe(world, pos) {
|
|
43
|
+
const block = await world.getBlock(pos);
|
|
44
|
+
const kind = block && kindOf(block.name);
|
|
45
|
+
if (!kind)
|
|
46
|
+
return undefined;
|
|
47
|
+
const partSize = SIZES[kind];
|
|
48
|
+
let parts = [pos.clone()];
|
|
49
|
+
if (kind === 'chest') {
|
|
50
|
+
if (flat) {
|
|
51
|
+
const props = block.getProperties?.() ?? {};
|
|
52
|
+
const partner = chestPartner(props, pos);
|
|
53
|
+
if (partner)
|
|
54
|
+
parts = props.type === 'left' ? [pos.clone(), partner] : [partner, pos.clone()];
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
// before 1.13 there is no chest type: a neighbour chest of the same block makes it double, the one with the lower coordinate is the first half
|
|
58
|
+
for (const [dx, dz] of Object.values(HORIZONTAL)) {
|
|
59
|
+
const n = pos.offset(dx, 0, dz);
|
|
60
|
+
if ((await world.getBlock(n))?.type === block.type) {
|
|
61
|
+
parts = n.x + n.z < pos.x + pos.z || (n.x + n.z === pos.x + pos.z && n.x < pos.x) ? [n, pos.clone()] : [pos.clone(), n];
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const translate = kind === 'chest' ? (parts.length > 1 ? 'container.chestDouble' : 'container.chest')
|
|
68
|
+
: kind === 'ender_chest' ? 'container.enderchest' : kind === 'shulker_box' ? 'container.shulkerBox' : `container.${kind}`;
|
|
69
|
+
return { kind, world, blockName: block.name, parts, partSize, size: partSize * parts.length, title: { translate } };
|
|
70
|
+
}
|
|
71
|
+
// ----------------------------------------------------------------- opening
|
|
72
|
+
const windowTypeOf = (ref) => {
|
|
73
|
+
if (newWindows) {
|
|
74
|
+
if (ref.kind === 'shulker_box')
|
|
75
|
+
return { inventoryType: 'minecraft:shulker_box' };
|
|
76
|
+
if (ref.kind === 'hopper')
|
|
77
|
+
return { inventoryType: 'minecraft:hopper' };
|
|
78
|
+
if (ref.kind === 'dispenser' || ref.kind === 'dropper')
|
|
79
|
+
return { inventoryType: 'minecraft:generic_3x3' };
|
|
80
|
+
return { inventoryType: ref.size > 27 ? 'minecraft:generic_9x6' : 'minecraft:generic_9x3' };
|
|
81
|
+
}
|
|
82
|
+
if (ref.kind === 'shulker_box')
|
|
83
|
+
return { inventoryType: 'minecraft:shulker_box' };
|
|
84
|
+
if (ref.kind === 'hopper' || ref.kind === 'dispenser' || ref.kind === 'dropper')
|
|
85
|
+
return { inventoryType: `minecraft:${ref.kind}` };
|
|
86
|
+
return { inventoryType: 'minecraft:chest', slotCount: ref.size };
|
|
87
|
+
};
|
|
88
|
+
const playSound = (world, pos, sound) => {
|
|
89
|
+
try {
|
|
90
|
+
serv.playSound(sound, world, pos.offset(0.5, 0.5, 0.5), {});
|
|
91
|
+
}
|
|
92
|
+
catch { }
|
|
93
|
+
};
|
|
94
|
+
const soundGroup = (ref) => ref.kind === 'shulker_box' ? 'block.shulker_box' : ref.kind === 'barrel' ? 'block.barrel' : ref.kind === 'ender_chest' ? 'block.ender_chest' : 'block.chest';
|
|
95
|
+
/** lid animation (chests, shulker boxes) / open state (barrels) */
|
|
96
|
+
const setOpen = async (ref, count) => {
|
|
97
|
+
for (const pos of ref.parts) {
|
|
98
|
+
if (ref.kind === 'chest' || ref.kind === 'ender_chest' || ref.kind === 'shulker_box') {
|
|
99
|
+
serv.setBlockAction(ref.world, pos, 1, count);
|
|
100
|
+
}
|
|
101
|
+
else if (ref.kind === 'barrel' && flat) {
|
|
102
|
+
const block = await ref.world.getBlock(pos);
|
|
103
|
+
if (block?.name === 'barrel' && String(block.getProperties().open) !== String(count > 0)) {
|
|
104
|
+
await serv.setBlock(ref.world, pos, PBlock.fromProperties('barrel', { ...block.getProperties(), open: count > 0 }, 0).stateId);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Opens a window with the contents of a container for a player. `load` / `store` abstract where the slots live (block entity or the ender chest of the player).
|
|
111
|
+
*/
|
|
112
|
+
async function showWindow(player, ref, items, writeSlot, watch) {
|
|
113
|
+
const { inventoryType, slotCount } = windowTypeOf(ref);
|
|
114
|
+
const windowSlots = {};
|
|
115
|
+
items.forEach((item, i) => { if (item)
|
|
116
|
+
windowSlots[i] = item; });
|
|
117
|
+
let applyingRemote = false;
|
|
118
|
+
const viewer = { player, window: undefined, refresh: () => { } };
|
|
119
|
+
const open = (key) => { (viewers.get(key) ?? viewers.set(key, new Set()).get(key)).add(viewer); };
|
|
120
|
+
const close = (key) => { viewers.get(key)?.delete(viewer); if (!viewers.get(key)?.size)
|
|
121
|
+
viewers.delete(key); };
|
|
122
|
+
const viewerCount = () => Math.max(...watch.map(k => viewers.get(k)?.size ?? 0), 0);
|
|
123
|
+
const window = player.openCustomWindow({
|
|
124
|
+
inventoryType,
|
|
125
|
+
slotCount,
|
|
126
|
+
title: ref.title,
|
|
127
|
+
windowSlots,
|
|
128
|
+
onSlotClick: (slot, _old, newItem) => {
|
|
129
|
+
if (slot >= ref.size || applyingRemote)
|
|
130
|
+
return;
|
|
131
|
+
writeSlot(slot, newItem);
|
|
132
|
+
// the other players that look into the same container
|
|
133
|
+
for (const k of watch) {
|
|
134
|
+
for (const other of viewers.get(k) ?? [])
|
|
135
|
+
if (other !== viewer)
|
|
136
|
+
other.refresh(slot, newItem);
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
onClose: () => finish()
|
|
140
|
+
});
|
|
141
|
+
let finished = false;
|
|
142
|
+
const finish = () => {
|
|
143
|
+
if (finished)
|
|
144
|
+
return;
|
|
145
|
+
finished = true;
|
|
146
|
+
watch.forEach(close);
|
|
147
|
+
void setOpen(ref, viewerCount());
|
|
148
|
+
playSound(ref.world, ref.parts[0], `${soundGroup(ref)}.close`);
|
|
149
|
+
};
|
|
150
|
+
player.once('disconnected', finish);
|
|
151
|
+
viewer.window = window;
|
|
152
|
+
viewer.refresh = (slot, item) => {
|
|
153
|
+
applyingRemote = true;
|
|
154
|
+
try {
|
|
155
|
+
window.updateSlot(slot, item);
|
|
156
|
+
}
|
|
157
|
+
finally {
|
|
158
|
+
applyingRemote = false;
|
|
159
|
+
}
|
|
160
|
+
player.writePacket('set_slot', { windowId: player.windowId, stateId: 1, slot, item: serv.PrismarineItem.toNotch(item) });
|
|
161
|
+
};
|
|
162
|
+
watch.forEach(open);
|
|
163
|
+
await setOpen(ref, viewerCount());
|
|
164
|
+
playSound(ref.world, ref.parts[0], `${soundGroup(ref)}.open`);
|
|
165
|
+
return window;
|
|
166
|
+
}
|
|
167
|
+
async function openContainer(player, world, pos) {
|
|
168
|
+
const ref = await describe(world, pos);
|
|
169
|
+
if (!ref)
|
|
170
|
+
return false;
|
|
171
|
+
// a block over a chest (solid, or any block for the ender chest) keeps it closed
|
|
172
|
+
if (ref.kind === 'chest' || ref.kind === 'ender_chest') {
|
|
173
|
+
for (const p of ref.parts) {
|
|
174
|
+
const above = await world.getBlock(p.offset(0, 1, 0));
|
|
175
|
+
if (above && above.boundingBox === 'block' && !/glass|slab|stairs|leaves|carpet|trapdoor/.test(above.name))
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
player.windowPos = pos;
|
|
180
|
+
player.windowType = ref.kind;
|
|
181
|
+
if (ref.kind === 'ender_chest') {
|
|
182
|
+
const items = player.enderChest;
|
|
183
|
+
await showWindow(player, ref, items, (slot, item) => { player.enderChest[slot] = item; }, [`ender:${player.uuid}`]);
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
let items = [];
|
|
187
|
+
for (const p of ref.parts) {
|
|
188
|
+
try {
|
|
189
|
+
items = items.concat(await store.itemsOf(world, p, ref.partSize));
|
|
190
|
+
}
|
|
191
|
+
catch (e) {
|
|
192
|
+
serv.warn(`container at ${p}: ${e.message}`);
|
|
193
|
+
items = items.concat(new Array(ref.partSize).fill(null));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
await showWindow(player, ref, items, (slot, item) => {
|
|
197
|
+
const part = ref.parts[Math.floor(slot / ref.partSize)];
|
|
198
|
+
void store.setSlot(world, part, ref.blockName, ref.partSize, slot % ref.partSize, item).catch(e => serv.warn(`container save failed: ${e.message}`));
|
|
199
|
+
}, ref.parts.map(p => viewKey(world, p)));
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
serv.openContainer = openContainer;
|
|
203
|
+
// ----------------------------------------------------------------- breaking
|
|
204
|
+
const dropStacks = (world, pos, items) => {
|
|
205
|
+
const entityId = mcData.entitiesByName[mcData.version['<']('1.11') ? 'Item' : 'item']?.id;
|
|
206
|
+
if (entityId === undefined)
|
|
207
|
+
return;
|
|
208
|
+
for (const item of items) {
|
|
209
|
+
if (!item)
|
|
210
|
+
continue;
|
|
211
|
+
serv.spawnObject(entityId, world, pos.offset(0.5, 0.5, 0.5), {
|
|
212
|
+
velocity: new vec3_1.Vec3(Math.random() * 2 - 1, Math.random() * 2 + 1, Math.random() * 2 - 1),
|
|
213
|
+
itemId: item.type,
|
|
214
|
+
itemDamage: item.metadata,
|
|
215
|
+
itemCount: item.count,
|
|
216
|
+
pickupTime: 500,
|
|
217
|
+
deathTime: 60 * 5 * 1000
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
store.dropStacks = dropStacks;
|
|
222
|
+
/** the container block at `pos` is gone: its items fall out (not in creative), the entity is removed, the other half of a double chest becomes single */
|
|
223
|
+
async function containerBroken(world, pos, blockName, props, drop) {
|
|
224
|
+
const kind = kindOf(blockName);
|
|
225
|
+
if (!kind || kind === 'ender_chest')
|
|
226
|
+
return;
|
|
227
|
+
const size = SIZES[kind];
|
|
228
|
+
const entity = await store.entityAt(world, pos);
|
|
229
|
+
if (entity) {
|
|
230
|
+
if (drop)
|
|
231
|
+
dropStacks(world, pos, await store.itemsOf(world, pos, size));
|
|
232
|
+
await store.removeEntity(world, pos);
|
|
233
|
+
}
|
|
234
|
+
const partner = chestPartner(props, pos);
|
|
235
|
+
if (partner) {
|
|
236
|
+
const other = await world.getBlock(partner);
|
|
237
|
+
if (other?.name === blockName)
|
|
238
|
+
await serv.setBlock(world, partner, PBlock.fromProperties(blockName, { ...other.getProperties(), type: 'single' }, 0).stateId);
|
|
239
|
+
}
|
|
240
|
+
// a closed lid for the viewers that still look into it
|
|
241
|
+
for (const set of [viewers.get(viewKey(world, pos))])
|
|
242
|
+
for (const v of set ?? [])
|
|
243
|
+
v.window.close?.();
|
|
244
|
+
}
|
|
245
|
+
serv.containerBroken = containerBroken;
|
|
246
|
+
// ----------------------------------------------------------------- registration
|
|
247
|
+
serv.once('asap', () => {
|
|
248
|
+
const names = mcData.blocksArray.map(b => b.name).filter(n => kindOf(n));
|
|
249
|
+
for (const name of names) {
|
|
250
|
+
serv.onBlockInteraction(name, async ({ block, player }) => openContainer(player, player.world, block.position));
|
|
251
|
+
}
|
|
252
|
+
// placing: chests face the player and join a chest next to them into a double chest; every container starts empty (no entity of a former block left over)
|
|
253
|
+
if (flat) {
|
|
254
|
+
for (const name of names) {
|
|
255
|
+
const block = mcData.blocksByName[name];
|
|
256
|
+
serv.onItemPlace(name, async ({ player, placedPosition, properties }) => {
|
|
257
|
+
void store.removeEntity(player.world, placedPosition);
|
|
258
|
+
const base = PBlock.fromStateId(block.defaultState, 0).getProperties();
|
|
259
|
+
// the same rules as the default block placement: every property the placement knows (facing, ...) that the block has
|
|
260
|
+
const props = { ...base };
|
|
261
|
+
for (const k of Object.keys(properties))
|
|
262
|
+
if (k in base && k !== 'type' && k !== 'half')
|
|
263
|
+
props[k] = properties[k];
|
|
264
|
+
if (name === 'chest' || name === 'trapped_chest') {
|
|
265
|
+
const facing = properties.facing;
|
|
266
|
+
props.facing = facing;
|
|
267
|
+
props.type = 'single';
|
|
268
|
+
if (!player.crouching) {
|
|
269
|
+
for (const [side, type, partnerType] of [[CLOCKWISE[facing], 'left', 'right'], [COUNTER_CLOCKWISE[facing], 'right', 'left']]) {
|
|
270
|
+
const [dx, dz] = HORIZONTAL[side];
|
|
271
|
+
const npos = placedPosition.offset(dx, 0, dz);
|
|
272
|
+
const n = await player.world.getBlock(npos);
|
|
273
|
+
if (n?.name === name && n.getProperties().facing === facing && n.getProperties().type === 'single') {
|
|
274
|
+
props.type = type;
|
|
275
|
+
await serv.setBlock(player.world, npos, PBlock.fromProperties(name, { ...n.getProperties(), type: partnerType }, 0).stateId);
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
const state = PBlock.fromProperties(block.name, props, 0).stateId;
|
|
282
|
+
return { id: block.id, data: state - block.minStateId };
|
|
283
|
+
}, false);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
// ----------------------------------------------------------------- /loot
|
|
288
|
+
serv.commands.add({
|
|
289
|
+
base: 'loot',
|
|
290
|
+
info: 'Puts a loot table into the container at a position (generated when it is opened)',
|
|
291
|
+
usage: '/loot <x> <y> <z> <loot_table> [seed] e.g. /loot ~ ~-1 ~ chests/simple_dungeon',
|
|
292
|
+
op: true,
|
|
293
|
+
parse(str, ctx) {
|
|
294
|
+
const [x, y, z, table, seed] = str.trim().split(/\s+/);
|
|
295
|
+
if (!table)
|
|
296
|
+
return false;
|
|
297
|
+
const at = (v, c) => serv.posFromString(v, c);
|
|
298
|
+
const p = ctx.player?.position;
|
|
299
|
+
return { pos: new vec3_1.Vec3(Math.floor(+at(x, p?.x ?? 0)), Math.floor(+at(y, p?.y ?? 0)), Math.floor(+at(z, p?.z ?? 0))), table, seed };
|
|
300
|
+
},
|
|
301
|
+
async action({ pos, table, seed }, ctx) {
|
|
302
|
+
const world = ctx.player?.world ?? serv.overworld;
|
|
303
|
+
const block = await world.getBlock(pos);
|
|
304
|
+
const kind = kindOf(block.name);
|
|
305
|
+
if (!kind || kind === 'ender_chest')
|
|
306
|
+
throw new user_error_1.default(`No container at ${pos.x} ${pos.y} ${pos.z} (${block.name})`);
|
|
307
|
+
const id = table.includes(':') ? table : `minecraft:${table}`;
|
|
308
|
+
await store.gameData.ready;
|
|
309
|
+
if (!store.gameData.lootTable(id))
|
|
310
|
+
throw new user_error_1.default(`Unknown loot table ${id}. Loot tables come from the vanilla client jar, datapacks in <world>/datapacks, __MC_GAMEDATA_URL or the lootTables option`);
|
|
311
|
+
await store.setLootTable(world, pos, block.name, id, seed ? BigInt(seed) : undefined);
|
|
312
|
+
return `${block.name} at ${pos.x} ${pos.y} ${pos.z} now holds ${id}`;
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
};
|
|
316
|
+
exports.server = server;
|
|
317
|
+
const player = (player, serv) => {
|
|
318
|
+
const store = () => serv.containers;
|
|
319
|
+
/** the 27 slots of the ender chest of the player (saved in the player data as `EnderItems`) */
|
|
320
|
+
player.enderChest = new Array(27).fill(null);
|
|
321
|
+
player.enderChestToNbt = () => player.enderChest.flatMap((item, slot) => item ? [store().itemToNbt(item, slot)] : []);
|
|
322
|
+
player.loadEnderChest = (nbtItems) => {
|
|
323
|
+
player.enderChest = new Array(27).fill(null);
|
|
324
|
+
for (const n of nbtItems ?? []) {
|
|
325
|
+
const slot = n?.Slot?.value;
|
|
326
|
+
if (typeof slot === 'number' && slot >= 0 && slot < 27)
|
|
327
|
+
player.enderChest[slot] = store().itemFromNbt(n);
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
// items of a broken container fall out
|
|
331
|
+
player.on('dug', ({ position, block }, cancelled) => {
|
|
332
|
+
if (cancelled || !block)
|
|
333
|
+
return;
|
|
334
|
+
const props = block.getProperties?.() ?? {};
|
|
335
|
+
void serv.containerBroken(player.world, position, block.name, props, player.gameMode !== 1).catch((e) => serv.warn(`container break: ${e.message}`));
|
|
336
|
+
});
|
|
337
|
+
};
|
|
338
|
+
exports.player = player;
|