Skip to content

Commit c02e48d

Browse files
committed
Add gift box helper methods
1 parent e7ce46e commit c02e48d

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/createPrefab.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Prefab } from './Prefab';
22
import { createString } from './createString';
33
import { PrefabData } from './decoders';
4-
import { PhysicalMaterialPartHash } from '.';
4+
import { PhysicalMaterialPartHash } from './PhysicalMaterialPartHash';
5+
import * as components from './components';
6+
import { reasonableGifts } from './utils';
57

68
const VALID_MATERIALS = Object.values(PhysicalMaterialPartHash)
79
.filter(key => typeof key === 'string')
@@ -32,6 +34,8 @@ type PrefabManager<S> = {
3234
setIntegrity: (integrity: number) => PrefabManager<S>;
3335
setServings: (servings: number) => PrefabManager<S>;
3436
setOnFire: (isLit?: boolean) => PrefabManager<S>;
37+
setGiftBoxLabel: (label: string) => PrefabManager<S>;
38+
addGift: <P extends Prefab, C extends keyof P['slots']>(gift: PrefabManager<C>) => PrefabManager<S>;
3539
useSlot: <P extends Prefab, C extends keyof P['slots']>(slot: S, childPrefab: PrefabManager<C>) => PrefabManager<S>;
3640
toString: () => string;
3741
print: () => void;
@@ -202,6 +206,48 @@ export const createPrefab = <P extends Prefab, S extends keyof P['slots']>(prefa
202206
return this;
203207
},
204208

209+
setGiftBoxLabel(label) {
210+
this.data.components = {
211+
...this.data.components,
212+
SentGift: {
213+
...this.data.components!.SentGift,
214+
senderName: label
215+
}
216+
};
217+
218+
return this;
219+
},
220+
221+
addGift(giftPrefab) {
222+
if (!reasonableGifts.includes(giftPrefab.data.prefabObject.hash)) {
223+
throw new Error('No gifts for naughty people.');
224+
}
225+
226+
const string = createString(giftPrefab.data);
227+
228+
const [dataString, versionsString] = string.split('|');
229+
const [hash, messageSizeInBytes, ...data] = dataString.split(',').map(Number);
230+
const [_, ...chunkVersioning] = versionsString.split(',').map(Number);
231+
232+
this.data.components = {
233+
...this.data.components,
234+
SentGift: {
235+
...this.data.components!.SentGift,
236+
gifts: [
237+
...((this.data.components!.SentGift as components.SentGift | undefined)?.gifts ?? []),
238+
{
239+
data,
240+
messageSizeInBytes,
241+
hash,
242+
chunkVersioning
243+
}
244+
]
245+
}
246+
};
247+
248+
return this;
249+
},
250+
205251
useSlot(slotName, childPrefab) {
206252
if (typeof slotName === 'undefined' || typeof childPrefab === 'undefined') {
207253
throw new Error(`useSlot(slot, prefab) called with invalid arguments.`);

0 commit comments

Comments
 (0)