diff --git a/package.json b/package.json index 9045af8..8533915 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ }, "devDependencies": { "@types/jest": "^26.0.24", + "@types/node": "^16.9.1", "husky": "^7.0.1", "jest": "^27.0.6", "lint-staged": "^11.1.1", diff --git a/src/PrefabSlot.ts b/src/PrefabSlot.ts new file mode 100644 index 0000000..9ac883a --- /dev/null +++ b/src/PrefabSlot.ts @@ -0,0 +1,560 @@ +export const PrefabSlot = { + Guard: { + Insert_SwordType_Craft: 39368, + Slot_SwordType: 39370 + }, + Guard_Fancy: { + Insert_SwordType_Deco: 39368, + Slot_SwordType: 39370 + }, + Guard_Handle: { + Insert_Mid: 39368, + Slot_End: 39370 + }, + Guard_Hemisphere: { + Insert_SwordType_Deco: 39368, + Slot_SwordType: 39370 + }, + Guard_Pointy_Ends: { + Insert_SwordType_Craft: 39368, + Slot_Multi: 31108 + }, + Guard_Round_Ends: { + Insert_Mid: 39368, + Slot_End: 39370 + }, + Guard_Straight_Ends: { + Insert_SwordType_Craft: 39368, + Slot_Multi: 30024 + }, + Hebios_Guard: { + Slot_SwordType: 39370 + }, + Large_Guard_Rectangle: { + Insert_Large_SwordType_Craft: 51894, + Slot_Large_SwordType: 51896 + }, + Large_Guard_TShape: { + Insert_Large_SwordType_Craft: 23506, + Slot_Large_SwordType: 23508 + }, + Large_Guard_Wedge: { + Insert_Large_SwordType_Craft: 38964, + Slot_Large_SwordType: 38966 + }, + Phantom_Guard: { + Insert_SwordType_Craft: 39368, + Slot_Multi: 39370 + }, + Pole_Hugger_Edgy: { + Insert_Deco: 112 + }, + Pole_Hugger_Pointy_Ends: { + Insert_Deco: 112 + }, + Pole_Hugger_Short: { + Insert_Deco: 112 + }, + Pole_Hugger_Tall: { + Insert_Deco: 112 + }, + Pommel: { + Insert_PommelType: 112 + }, + Pommel_Circle: { + Insert_PommelType: 112 + }, + Pommel_Cone: { + Insert_Deco: 112 + }, + Pommel_Diamond: { + Insert_PommelType: 112 + }, + Pommel_Large_Square: { + Insert_PommelType: 42136 + }, + Pommel_Squashed: { + Insert_Deco: 112 + }, + Axe_Head: { + Insert_Pole: 7796 + }, + Axe_Head_Curve: { + Insert_AxeType: 7796 + }, + Axe_Head_Felling: { + Insert_AxeType: 7796 + }, + Axe_Head_GreatCurve: { + Insert_HammerType_End_Cap: 7796 + }, + Axe_Head_LShape: { + Insert_AxeType: 7796 + }, + Great_Sword_Blade: { + Insert_Large_SwordType: 7796 + }, + Large_Longsword_Blade: { + Insert_Large_SwordType: 31944 + }, + Rapier_Blade: { + Insert_Straight_SwordType: 7796 + }, + Scythe_Blade: { + Insert_AxeType: 7796 + }, + Short_Sword_Blade: { + Insert_Straight_SwordType: 7796 + }, + Thick_Great_Sword_Blade: { + Insert_Large_SwordType: 7796 + }, + Chisel: { + Insert_SwordType: 25524 + }, + Dagger_Blade_Curved: { + Insert_Straight_Short_SwordType: 7796 + }, + Dagger_Blade_Edgy: { + Insert_Straight_Short_SwordType: 7796 + }, + Dagger_Blade_Round: { + Insert_Straight_Short_SwordType: 7796 + }, + Dagger_Blade_Wide: { + Insert_Straight_Short_SwordType: 7796 + }, + Metal_Hammer_Head: { + Insert_HammerType_End_Cap: 52192, + Slot_Deco_1: 48318, + Slot_Deco_2: 37872 + }, + WarHammer: { + Insert_HammerType_End_Cap: 52192 + }, + Shotel_Blade: { + Insert_SwordType: 7796 + }, + Crystal_Shard_Blue: { + Insert_Deco: 112 + }, + Explosive_Spike: { + Insert_Deco: 112 + }, + Healing_Pod: { + Insert_Deco: 112 + }, + Small_Bone_Spike: { + Insert_Deco: 112 + }, + Spike_Fancy: { + Insert_Deco: 112 + }, + Spike_Short: { + Insert_Deco: 112 + }, + Spike_Tall: { + Insert_Deco: 112 + }, + Craft_Piece_Side_1Way: { + Insert_AxeType_Craft: 39368, + Slot_AxeType: 7170 + }, + Craft_Piece_Side_2Way: { + Insert_AxeType_Craft: 39368, + Slot_AxeType_1: 15316, + Slot_AxeType_2: 15318 + }, + Craft_Piece_Side_4Way: { + Insert_AxeType_Craft: 15432, + Slot_AxeType_1: 40654, + Slot_AxeType_2: 19782, + Slot_AxeType_3: 18636, + Slot_AxeType_4: 17932 + }, + Craft_Piece_Side_Flat_1Way: { + Insert_Edgetype_Craft: 15580, + Slot_EdgeType_1: 39368, + Slot_EdgeType_2: 38338 + }, + Craft_Piece_SideFlat_2Way: { + Insert_Edgetype_Craft: 57508, + Slot_EdgeType_1: 57506, + Slot_EdgeType_2: 39368, + Slot_EdgeType_3: 51674, + Slot_EdgeType_4: 51672 + }, + Edge_Blade_Long_Curve: { + Insert_Edgetype: 7796 + }, + Edge_Curved_Blade: { + Insert_Edgetype: 7796 + }, + Edge_Short_Curve_Blade: { + Insert_Edgetype: 7796 + }, + Edge_Straight_Blade: { + Insert_Edgetype: 7796 + }, + Curled_Wooden_Handle: { + Slot_Multi: 34272, + Slot_Multi_1: 34270, + Slot_Multi_2: 2108 + }, + Handle_Bow: { + Slot_Grass_1: 11474, + Slot_Grass_2: 29036 + }, + Handle_Fist: { + Slot_Deco_1: 22992, + Slot_Deco_2: 54114, + Slot_Deco_3: 22982, + Slot_Multi: 22990, + Slot_Deco_4: 23002 + }, + Handle_Large_Branch: { + Slot_Primitive_AxeType: 55026, + Slot_Multi: 20330 + }, + Handle_Large_Cool: { + Slot_Multi_1: 29320, + Slot_Multi_2: 43166, + Slot_EdgeType_Craft_1: 43172, + Slot_EdgeType_Craft_2: 64610 + }, + Handle_Long_Straight: { + Slot_Multi_1: 63218, + Slot_Multi_2: 63178, + Slot_Straight_Short_Sword_Type_1: 63270, + Slot_Straight_Short_Sword_Type_2: 63318, + Slot_EdgeType_Craft_1: 53692, + Slot_EdgeType_Craft_2: 53492, + Slot_Multi_3: 53060, + Slot_Multi_4: 53074 + }, + Handle_Medium_Branch: { + Slot_Multi_1: 29458, + Slot_Multi_2: 52880, + Slot_EdgeType_Craft: 45470, + Slot_Multi_3: 15462, + Slot_PommelType: 55836 + }, + Handle_Medium_Cool: { + Slot_EdgeType_Craft_1: 57058, + Slot_EdgeType_Craft_2: 59436, + Slot_Multi_1: 3558, + Slot_Multi_2: 59224 + }, + Handle_Medium_Curved: { + Slot_Multi: 7860, + Slot_EdgeType_Craft: 51182 + }, + Handle_Medium_Ridged: { + Slot_Pole_1: 64914, + Slot_Pole_2: 19996 + }, + Handle_Medium_Standard: { + Slot_Pole_1: 20070, + Slot_Pole_2: 64914 + }, + Handle_Medium_Straight: { + Slot_Multi_1: 63218, + Slot_Multi_2: 63178, + Slot_EdgeType_Craft_1: 53692, + Slot_EdgeType_Craft_2: 53492, + Slot_Multi_3: 53060, + Slot_Multi_4: 53074, + Slot_Straight_Short_Sword_Type_1: 63270, + Slot_Straight_Short_Sword_Type_2: 63318 + }, + Handle_Round_Fist: { + Slot_Deco_1: 57746, + Slot_Deco_2: 50738, + Slot_Deco_3: 38156, + Slot_Deco_4: 31482, + Slot_Multi: 54114 + }, + Handle_Short: { + Slot_PommelType_1: 30776, + Slot_Large_SwordType_Craft_1: 54356, + Slot_Multi_1: 6136, + Slot_PommelType_2: 20654, + Slot_Large_SwordType_Craft_2: 6134, + Slot_Multi_2: 6138 + }, + Handle_Short_C_Curve: { + Slot_PommelType_1: 5686, + Slot_Large_SwordType_Craft_1: 5714, + Slot_Multi_1: 5626, + Slot_PommelType_2: 5700, + Slot_Large_SwordType_Craft_2: 5628, + Slot_Multi_2: 5624 + }, + Handle_Short_Cool: { + Slot_Large_SwordType_Craft: 33946, + Slot_Multi: 48480, + Slot_EdgeType_Craft: 43890 + }, + Handle_Short_Pointy_End: { + Slot_Large_SwordType_Craft: 9736, + Slot_Multi: 18472, + Slot_EdgeType_Craft: 14502 + }, + Handle_Short_S_Curve: { + Slot_Mid_1: 64914, + Slot_Mid_2: 20238 + }, + Handle_Short_Taper: { + Slot_Mid_1: 64914, + Slot_Mid_2: 20296 + }, + Handle_Spear: { + Slot_Pole: 64914, + Slot_Deco: 4866 + }, + Handle_Tonfa: { + Slot_HammerType_End_Cap: 47874, + Slot_Shield: 37414, + Slot_Multi: 64782, + Slot_EdgeType_Craft: 52822 + }, + Hebios_Handle_Katana: { + Slot_EdgeType_Craft: 53492, + Slot_Straight_Short_Sword_Type: 63318 + }, + Hebios_Handle_Kunai: { + Slot_Multi: 6136 + }, + Hebios_Handle_Naginata: { + Slot_Multi_1: 63218, + Slot_Straight_Short_Sword_Type: 63270, + Slot_EdgeType_Craft: 53692, + Slot_Multi_2: 53060 + }, + Hebios_Handle_Wakizashi: { + Slot_Straight_Short_Sword_Type: 63318 + }, + Rod_Long: { + Slot_EdgeType_Craft: 31944, + Slot_AxeType: 37426, + Slot_Multi: 53940 + }, + Rod_Medium: { + Slot_Multi_1: 31156, + Slot_Multi_2: 39008, + Slot_EdgeType_Craft: 35298, + Slot_AxeType: 15522, + Slot_Deco: 37438 + }, + Rod_Slim_40cm: { + Slot_Multi: 44690 + }, + Hammer_Head: { + Insert_HammerType_End_Cap: 52192 + }, + Hammer_Head_Small: { + Insert_HammerType_End_Cap: 18784 + }, + Spriggull_Feather_Green: { + Insert_Feather: 112 + }, + Spriggull_Feather_Purple: { + Insert_Feather: 112 + }, + Turabada_Arm: { + Insert_HammerType_End_Cap: 52192 + }, + Wooden_Net: { + Insert_HammerType_End_Cap: 7796 + }, + Wooden_Stake: { + Slot_Multi: 11474 + }, + DebugPlayerStone: { + Insert_Primitive_SwordType: 7796 + }, + Arrow_Shaft_Wooden: { + Slot_Feather: 38282, + Slot_Deco: 33560, + Slot_Grass: 13582 + }, + Bow_Blade: { + Insert_Bow_Blade: 7796 + }, + Metal_Bow: { + Slot_Deco_1: 65334, + Slot_Deco_2: 61122, + Slot_Bow_Blade_1: 252, + Slot_Bow_Blade_2: 10188 + }, + Shield_Core_Bent_Middle: { + Slot_Shield_Part_1: 47874, + Slot_Shield_Part_2: 8898, + Insert_Shield_Handle: 112 + }, + Shield_Core_Circle_Middle: { + Slot_Shield_Part_1: 47874, + Slot_Shield_Part_2: 8898, + Insert_Shield_Handle: 112 + }, + Shield_Core_Handle: { + Slot_Shield_Handle: 47874 + }, + Shield_Core_Holed_Middle: { + Slot_Shield_Part_1: 47874, + Slot_Shield_Part_2: 8898, + Insert_Shield_Handle: 112 + }, + Shield_Part_Half_Circle: { + Insert_Shield_Part: 112, + Slot_Deco_1: 25870, + Slot_Deco_2: 25868, + Slot_Deco_3: 55242, + Slot_Deco_4: 55244, + Slot_Deco_5: 53720, + Slot_Deco_6: 53718, + Slot_Deco_7: 52564, + Slot_Deco_8: 52566, + Slot_Deco_9: 51508, + Slot_Deco_10: 51506 + }, + Shield_Part_Half_Circle_Hole: { + Insert_Shield_Part: 112, + Slot_Deco_1: 25870, + Slot_Deco_2: 25868, + Slot_Deco_3: 52564, + Slot_Deco_4: 52566, + Slot_Deco_5: 55242, + Slot_Deco_6: 55244, + Slot_Deco_7: 51508, + Slot_Deco_8: 51506 + }, + Shield_Part_Half_Hole: { + Insert_Shield_Part: 112, + Slot_Deco_1: 20018, + Slot_Deco_2: 854, + Slot_Deco_3: 63256, + Slot_Deco_4: 56752, + Slot_Deco_5: 52138, + Slot_Deco_6: 46580 + }, + Shield_Part_Half_Point_01: { + Insert_Shield_Part: 112, + Slot_Deco_1: 26366, + Slot_Deco_2: 26340, + Slot_Deco_3: 26368, + Slot_Deco_4: 26396, + Slot_Deco_5: 20420 + }, + Small_Shield: { + Insert_Shield: 64244 + }, + Spriggull_Fletching_Blue: { + Insert_Feather: 51282 + }, + Spriggull_Fletching_Red: { + Insert_Feather: 112 + }, + Bag: { + Slot_Bag_Pin_Collectible_Attach: 31454, + Slot_Bag_Attach_1: 55164, + Slot_Bag_Tool_Attachment_1: 36292, + Slot_Bag_Attach_2: 30776, + Slot_Bag_Tool_Attachment_2: 36386 + }, + Barrel_Bag: { + Slot_Bag_Attach_1: 55164, + Slot_Bag_Attach_2: 30776 + }, + Fixed_Light_Bag_Attachment: { + Insert_Bag_Tool_Attachment: 7796 + }, + Forage_Basket_Bag: { + Slot_Bag_Attach_1: 55164, + Slot_Bag_Attach_2: 30776 + }, + Forage_Side_Pouch_Attachment: { + Insert_Bag_Attach: 7796 + }, + Hoarder_Bag: { + Slot_Bag_Attach_1: 55164, + Slot_Bag_Attach_2: 30776, + Slot_Bag_Pin_Collectible_Attach: 59540, + Slot_Bag_Attach_3: 10614, + Slot_Bag_Tool_Attachment_1: 1020, + Slot_Bag_Attach_4: 10616, + Slot_Bag_Tool_Attachment_2: 918 + }, + Ore_Bag: { + Slot_Bag_Attach_1: 55164, + Slot_Bag_Attach_2: 30776, + Slot_Bag_Attach_3: 62396, + Slot_Bag_Tool_Attachment_1: 36292, + Slot_Bag_Tool_Attachment_2: 36386 + }, + Ore_Side_Pouch_Attachment: { + Insert_Bag_Attach: 7796 + }, + Potion_Hoops_Side_Tool_Attachment: { + Insert_Bag_Attach: 7796 + }, + Standard_Side_Pouch_Attachment: { + Insert_Bag_Attach: 7796 + }, + Standard_Side_Tool_Attachment: { + Insert_Bag_Attach: 7796 + }, + Timber_Bag: { + Slot_Bag_Attach_1: 55164, + Slot_Bag_Attach_2: 30776 + }, + Timber_Side_Pouch_Attachment: { + Insert_Bag_Attach: 7796 + }, + Flint: { + Insert_Primitive_SwordType: 7796 + }, + Grass_Clump: { + Insert_Grass: 7796 + }, + Pickaxe_Head: { + Insert_HammerType_End_Cap: 27166 + }, + Stone: { + Insert_Primitive_SwordType: 7796 + }, + Stick: { + Slot_Multi: 64914, + Slot_Primitive_SwordType: 30294 + }, + Hebios_Katana_Blade_Part_Full: { + Insert_Straight_SwordType: 7796 + }, + Hebios_Naginata_Blade_Part_Full: { + Insert_Straight_SwordType: 7796 + }, + Hebios_Sai_Blade_Part_Full: { + Insert_Straight_Short_SwordType: 7796 + }, + Hebios_Wakizashi_Blade_Part_Full: { + Insert_Straight_SwordType: 7796 + }, + Metal_Hebios_Katana_Blade: { + Insert_Straight_SwordType: 7796 + }, + Metal_Hebios_Naginata_Blade: { + Insert_Straight_SwordType: 7796 + }, + Metal_Hebios_Sai_Blade: { + Insert_Straight_Short_SwordType: 7796 + }, + Metal_Hebios_Wakizashi_Blade: { + Insert_Straight_Short_SwordType: 7796 + }, + Rusty_Shield: { + Insert_Shield: 64244 + }, + Spade_Head: { + Insert_AxeType: 7796 + } +}; diff --git a/src/createPrefab.ts b/src/createPrefab.ts new file mode 100644 index 0000000..9f38b1a --- /dev/null +++ b/src/createPrefab.ts @@ -0,0 +1,200 @@ +import { createString } from './createString'; +import { Prefab } from './decoders'; +import { PrefabHash } from './PrefabHash'; +import { PrefabSlot } from './PrefabSlot'; + +export type PrefabFactory = { + prefab: Prefab; + slots: { [slotName: string]: number }; + setPosition: (x: number, y: number, z: number) => PrefabFactory; + setRotation: (x: number, y: number, z: number, w: number) => PrefabFactory; + setKinematic: (isKinematic?: boolean) => PrefabFactory; + setServerSleeping: (isServerSleeping?: boolean) => PrefabFactory; + setVelocity: (x: number, y: number, z: number) => PrefabFactory; + setAngularVelocity: (x: number, y: number, z: number) => PrefabFactory; + setMaterial: (materialHash: number) => PrefabFactory; + setIntegrity: (integrity: number) => PrefabFactory; + setOnFire: (isLit?: boolean) => PrefabFactory; + useSlot: (slotHash: number, childPrefab: PrefabFactory) => PrefabFactory; + toString: () => string; + print: () => void; +}; + +const availableSlots = (prefabObjectHash: number) => { + const prefabName = PrefabHash[prefabObjectHash]; + const slots = PrefabSlot[prefabName as keyof typeof PrefabSlot] ?? {}; + const filteredEntries = Object.entries(slots).filter(([key]) => key.startsWith('Slot_')); + + return Object.fromEntries(filteredEntries); +}; + +export const createPrefab = (prefabObjectHash: number): PrefabFactory => ({ + prefab: { + prefabObject: { hash: prefabObjectHash }, + components: {}, + embeddedEntities: {}, + childPrefabs: [] + }, + + slots: availableSlots(prefabObjectHash), + + setPosition(x, y, z) { + if (typeof x === 'undefined' || typeof y === 'undefined' || typeof z === 'undefined') { + throw new Error(`setPosition called with invalid arguments.\n\nUsage: .setPosition(x, y, z)`); + } + + const position = { x, y, z }; + + this.prefab.prefabObject.position = position; + this.prefab.components!.NetworkRigidbody = { + ...this.prefab.components!.NetworkRigidbody, + position + }; + + return this; + }, + + setRotation(x, y, z, w) { + if (typeof x === 'undefined' || typeof y === 'undefined' || typeof z === 'undefined' || typeof w === 'undefined') { + throw new Error(`setRotation called with invalid arguments.\n\nUsage: .setRotation(x, y, z, w)`); + } + + const rotation = { x, y, z, w }; + + this.prefab.prefabObject.rotation = rotation; + this.prefab.components!.NetworkRigidbody = { + ...this.prefab.components!.NetworkRigidbody, + rotation + }; + + return this; + }, + + setKinematic(isKinematic = true) { + this.prefab.components!.NetworkRigidbody = { + ...this.prefab.components!.NetworkRigidbody, + isKinematic + }; + + return this; + }, + + setServerSleeping(isServerSleeping = true) { + this.prefab.components!.NetworkRigidbody = { + ...this.prefab.components!.NetworkRigidbody, + isServerSleeping + }; + + return this; + }, + + setVelocity(x, y, z) { + if (typeof x === 'undefined' || typeof y === 'undefined' || typeof z === 'undefined') { + throw new Error(`setVelocity called with invalid arguments.\n\nUsage: .setVelocity(x, y, z)`); + } + + const velocity = { x, y, z }; + + this.prefab.components!.NetworkRigidbody = { + ...this.prefab.components!.NetworkRigidbody, + velocity + }; + + return this; + }, + + setAngularVelocity(x, y, z) { + if (typeof x === 'undefined' || typeof y === 'undefined' || typeof z === 'undefined') { + throw new Error(`setAngularVelocity called with invalid arguments.\n\nUsage: .setAngularVelocity(x, y, z)`); + } + + const angularVelocity = { x, y, z }; + + this.prefab.components!.NetworkRigidbody = { + ...this.prefab.components!.NetworkRigidbody, + angularVelocity + }; + + return this; + }, + + setMaterial(materialHash) { + if (typeof materialHash === 'undefined') { + throw new Error(`setMaterial called with invalid arguments.\n\nUsage: .setMaterial(materialHash)`); + } + + this.prefab.components = { + ...this.prefab.components, + PhysicalMaterialPart: { materialHash } + }; + + return this; + }, + + setIntegrity(integrity) { + if (typeof integrity === 'undefined') { + throw new Error(`setIntegrity called with invalid arguments.\n\nUsage: .setIntegrity(integrity)`); + } + + this.prefab.components = { + ...this.prefab.components, + DurabilityModule: { integrity } + }; + + return this; + }, + + setOnFire(isLit = true) { + this.prefab.embeddedEntities!.Fire = { + ...this.prefab.embeddedEntities!.Fire, + isAlive: isLit, + components: { + ...this.prefab.embeddedEntities!.Fire!.components, + HeatSourceBase: { + ...this.prefab.embeddedEntities!.Fire!.components!.HeatSourceBase, + isLit + } + } + }; + + return this; + }, + + useSlot(slotHash, childPrefab) { + if (typeof slotHash === 'undefined' || typeof childPrefab === 'undefined') { + throw new Error(`useSlot called with invalid arguments.\n\nUsage: .useSlot(slotHash, prefab)`); + } + + const validHashes = Object.values(this.slots); + + if (!validHashes.includes(slotHash)) { + const prefabName = PrefabHash[prefabObjectHash]; + + throw new Error( + `useSlot called with invalid slot hash.\n\n${prefabName} has ${ + validHashes.length ? `these valid slots: ${JSON.stringify(this.slots, null, 2)}` : 'no available slots.' + }` + ); + } + + this.prefab.childPrefabs = [ + ...this.prefab.childPrefabs!.filter(({ parentHash }) => parentHash !== slotHash), + { + parentHash: slotHash, + prefab: { ...childPrefab.prefab } + } + ]; + + return this; + }, + + toString() { + return createString(this.prefab); + }, + + print() { + const string = this.toString(); + + return console.log(string); + } +}); diff --git a/src/index.ts b/src/index.ts index 94df89d..7e656f0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,6 +35,7 @@ export type { ChildPrefab } from './decoders'; export type { Component, KnownComponent, UnknownComponent } from './components'; +export type { PrefabFactory } from './createPrefab'; /** * HASHES @@ -45,6 +46,7 @@ export { PhysicalMaterialPartHash } from './PhysicalMaterialPartHash'; export { PopulationDefinitionHash } from './PopulationDefinitionHash'; export { PrefabEmbeddedEntityHash } from './PrefabEmbeddedEntityHash'; export { PrefabHash } from './PrefabHash'; +export { PrefabSlot } from './PrefabSlot'; export { PresetHash } from './PresetHash'; export { SpeciesHash } from './SpeciesHash'; @@ -56,5 +58,6 @@ export * from './utils'; /** * ATT STRING TRANSCODER */ +export { createPrefab } from './createPrefab'; export { createString } from './createString'; export { decodeString } from './decodeString'; diff --git a/tests/createPrefab.test.ts b/tests/createPrefab.test.ts new file mode 100644 index 0000000..050687b --- /dev/null +++ b/tests/createPrefab.test.ts @@ -0,0 +1,61 @@ +import { createPrefab, PrefabHash, PrefabFactory, NetworkRigidbody } from '../build'; + +let prefabFactory: PrefabFactory; + +describe('createPrefab', () => { + beforeEach(() => { + prefabFactory = createPrefab(PrefabHash.Handle_Short); + }); + + it('creates a prefab factory', () => { + expect(prefabFactory).toHaveProperty(['prefab', 'prefabObject', 'hash'], PrefabHash.Handle_Short); + }); + + it('sets new position', () => { + expect(prefabFactory.prefab.prefabObject.position).toBe(undefined); + + const x = 0, + y = 69, + z = 420, + position = { x, y, z }; + + prefabFactory.setPosition(x, y, z); + + expect(prefabFactory.prefab.prefabObject.position).toStrictEqual(position); + expect((prefabFactory.prefab.components!.NetworkRigidbody as NetworkRigidbody).position).toStrictEqual(position); + }); + + it('sets new rotation', () => { + expect(prefabFactory.prefab.prefabObject.rotation).toBe(undefined); + + const x = 0, + y = 0.69, + z = 0.42, + w = 0.123, + rotation = { x, y, z, w }; + + prefabFactory.setRotation(x, y, z, w); + + expect(prefabFactory.prefab.prefabObject.rotation).toStrictEqual(rotation); + expect((prefabFactory.prefab.components!.NetworkRigidbody as NetworkRigidbody).rotation).toStrictEqual(rotation); + }); + + it('uses a slot', () => { + expect(prefabFactory.prefab.childPrefabs).toStrictEqual([]); + + const slotHash = 6134; + const guardFactory = createPrefab(PrefabHash.Guard); + prefabFactory.useSlot(slotHash, guardFactory); + + expect(prefabFactory.prefab.childPrefabs).toStrictEqual([ + { + parentHash: slotHash, + prefab: guardFactory.prefab + } + ]); + }); + + it('outputs a string', () => { + expect(prefabFactory.toString()).toEqual('42230,48,42230,0,0,0,0,0,0,1065353216,1065353216,0,0,0,|0,'); + }); +}); diff --git a/tests/createString.test.ts b/tests/createString.test.ts index 337fc1a..f6b8b44 100644 --- a/tests/createString.test.ts +++ b/tests/createString.test.ts @@ -3,13 +3,13 @@ import { handle, litGrassClump } from './data/unencodedPrefabObjects'; import { grassClumpOnFire, shortHandleWithLargeGuardAndLargeBlade } from './data/encodedPrefabObjects'; describe('createString', () => { - it('should encode a prefab object', () => { + it('encodes a prefab object', () => { const encodedPrefab = createString(handle); expect(encodedPrefab).toStrictEqual(shortHandleWithLargeGuardAndLargeBlade); }); - it('should create a version string', () => { + it('creates a version string', () => { const encodedPrefab = createString(litGrassClump); expect(encodedPrefab).toStrictEqual(grassClumpOnFire); diff --git a/tests/decodeString.test.ts b/tests/decodeString.test.ts index 59cdcd8..975e2ed 100644 --- a/tests/decodeString.test.ts +++ b/tests/decodeString.test.ts @@ -3,7 +3,7 @@ import { encodedEmptyFlask } from './data/encodedStrings'; import { decodedEmptyFlask } from './data/decodedStrings'; describe('decodeString', () => { - it('should decode a string of UInts', () => { + it('decodes a string of UInts', () => { const decodedString = decodeString(encodedEmptyFlask); expect(decodedString).toStrictEqual(decodedEmptyFlask);