h1z1-server 0.23.5-2 → 0.23.5-3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h1z1-server",
3
- "version": "0.23.5-2",
3
+ "version": "0.23.5-3",
4
4
  "description": "Library for emulating h1z1 servers",
5
5
  "author": "Quentin Gruber <quentingruber@gmail.com> (http://github.com/quentingruber)",
6
6
  "license": "GPL-3.0-only",
@@ -1,235 +1,219 @@
1
- // ======================================================================
2
- //
3
- // GNU GENERAL PUBLIC LICENSE
4
- // Version 3, 29 June 2007
5
- // copyright (C) 2020 - 2021 Quentin Gruber
6
- // copyright (C) 2021 - 2023 H1emu community
7
- //
8
- // https://github.com/QuentinGruber/h1z1-server
9
- // https://www.npmjs.com/package/h1z1-server
10
- //
11
- // Based on https://github.com/psemu/soe-network
12
- // ======================================================================
13
-
14
- import { MAX_UINT32 } from "../../../utils/constants";
15
- import { ContainerErrors } from "../models/enums";
16
- import { ZoneServer2016 } from "../zoneserver";
17
- import { BaseItem } from "./baseItem";
18
- import { LoadoutItem } from "./loadoutItem";
19
- import { ZoneClient2016 } from "./zoneclient";
20
-
21
- // helper functions
22
- function combineItemStack(
23
- server: ZoneServer2016,
24
- client: ZoneClient2016,
25
- oldStackCount: number,
26
- targetContainer: LoadoutContainer,
27
- item: BaseItem,
28
- count: number
29
- ) {
30
- if (oldStackCount == count) {
31
- // if full stack is moved
32
- server.addContainerItem(client.character, item, targetContainer, false);
33
- return;
34
- }
35
- // if only partial stack is moved
36
- server.addContainerItem(
37
- client.character,
38
- server.generateItem(item.itemDefinitionId, count),
39
- targetContainer,
40
- false
41
- );
42
- }
43
-
44
- export class LoadoutContainer extends LoadoutItem {
45
- containerDefinitionId: number;
46
- items: { [itemGuid: string]: BaseItem } = {};
47
- canAcceptItems: boolean = true;
48
- acceptedItems: number[] = [];
49
- readonly isMutable: boolean = true;
50
- constructor(item: LoadoutItem, containerDefinitionId: number) {
51
- super(item, item.slotId, item.loadoutItemOwnerGuid);
52
- this.containerDefinitionId = containerDefinitionId;
53
- }
54
-
55
- /**
56
- * Gets the used bulk of this container.
57
- * @param server The ZoneServer instance.
58
- * @returns Returns the amount of bulk used.
59
- */
60
- getUsedBulk(server: ZoneServer2016): number {
61
- let bulk = 0;
62
- for (const item of Object.values(this.items)) {
63
- bulk +=
64
- server.getItemDefinition(item.itemDefinitionId).BULK * item.stackCount;
65
- }
66
- return bulk;
67
- }
68
-
69
- /**
70
- * Gets the maximum bulk that this container can hold.
71
- * @param server The ZoneServer instance.
72
- */
73
- getMaxBulk(server: ZoneServer2016): number {
74
- return server.getContainerDefinition(this.containerDefinitionId).MAX_BULK;
75
- }
76
-
77
- /**
78
- * Gets the available bulk for this container.
79
- * @param server The ZoneServer instance.
80
- * @returns Returns the amount of bulk available.
81
- */
82
- getAvailableBulk(server: ZoneServer2016): number {
83
- const availableBulk = this.getMaxBulk(server) - this.getUsedBulk(server);
84
- // prevents returning a negative available bulk for containers with 0 max bulk
85
- return availableBulk <= 0 ? 0 : availableBulk;
86
- }
87
-
88
- /**
89
- * Returns a boolean if this container has enough space for a given amount of a certain item.
90
- * @param server The ZoneServer instance.
91
- * @param itemDefinitionId The definiton id of the item to check.
92
- * @param count The amount of the item to check.
93
- */
94
- getHasSpace(
95
- server: ZoneServer2016,
96
- itemDefinitionId: number,
97
- count: number
98
- ): boolean {
99
- if (this.getMaxBulk(server) == 0) return true; // for external containers
100
- return !!(
101
- this.getMaxBulk(server) -
102
- (this.getUsedBulk(server) +
103
- server.getItemDefinition(itemDefinitionId).BULK * count) >=
104
- 0
105
- );
106
- }
107
-
108
- /**
109
- * Gets an item stack in a container that has space for a specified item.
110
- * @param server The ZoneServer instance.
111
- * @param itemDefId The item definition ID of the item stack to check.
112
- * @param count The amount of items to fit into the stack.
113
- * @param slotId Optional: The slotId of a specific item stack to check.
114
- * @returns Returns the itemGuid of the item stack.
115
- */
116
- getAvailableItemStack(
117
- server: ZoneServer2016,
118
- itemDefId: number,
119
- count: number,
120
- slotId: number = 0
121
- ): string {
122
- //
123
- // if slotId is defined, then only an item with the same slotId will be returned
124
- if (server.getItemDefinition(itemDefId).MAX_STACK_SIZE == 1) return "";
125
- for (const item of Object.values(this.items)) {
126
- if (
127
- item.itemDefinitionId == itemDefId &&
128
- server.getItemDefinition(item.itemDefinitionId).MAX_STACK_SIZE >=
129
- item.stackCount + count
130
- ) {
131
- if (!slotId || slotId == item.slotId) {
132
- return item.itemGuid;
133
- }
134
- }
135
- }
136
- return "";
137
- }
138
-
139
- /**
140
- * Gets the maximum slots that this container can hold.
141
- * @param server The ZoneServer instance.
142
- */
143
- getMaxSlots(server: ZoneServer2016) {
144
- return server.getContainerDefinition(this.containerDefinitionId)
145
- .MAXIMUM_SLOTS;
146
- }
147
-
148
- // transfers an item from this container to another
149
- transferItem(
150
- server: ZoneServer2016,
151
- targetContainer: LoadoutContainer,
152
- item: BaseItem,
153
- newSlotId: number,
154
- count?: number
155
- ) {
156
- if (!count) count = item.stackCount;
157
- const oldStackCount = item.stackCount; // saves stack count before it gets altered
158
-
159
- let client;
160
-
161
- const lootableEntity = server.getLootableEntity(this.loadoutItemOwnerGuid);
162
- if (lootableEntity) {
163
- const mountedCharacterId = lootableEntity.mountedCharacter;
164
- if (mountedCharacterId)
165
- client = server.getClientByCharId(mountedCharacterId);
166
- } else {
167
- client = server.getClientByCharId(this.loadoutItemOwnerGuid);
168
- }
169
-
170
- if (!client) return;
171
-
172
- if (!targetContainer.canAcceptItems) {
173
- server.containerError(client, ContainerErrors.DOES_NOT_ACCEPT_ITEMS);
174
- return;
175
- }
176
-
177
- if (
178
- targetContainer.acceptedItems.length &&
179
- !targetContainer.acceptedItems.includes(item.itemDefinitionId)
180
- ) {
181
- server.containerError(client, ContainerErrors.UNACCEPTED_ITEM);
182
- return;
183
- }
184
-
185
- if (!this.isMutable || !targetContainer.isMutable) {
186
- server.containerError(client, ContainerErrors.NOT_MUTABLE);
187
- return;
188
- }
189
-
190
- if (
191
- this.containerGuid != targetContainer.containerGuid &&
192
- !targetContainer.getHasSpace(server, item.itemDefinitionId, count)
193
- ) {
194
- // allows items in the same container but different stacks to be stacked
195
- return;
196
- }
197
- if (!server.removeContainerItem(client.character, item, this, count)) {
198
- server.containerError(client, ContainerErrors.NO_ITEM_IN_SLOT);
199
- return;
200
- }
201
- if (newSlotId == MAX_UINT32) {
202
- combineItemStack(
203
- server,
204
- client,
205
- oldStackCount,
206
- targetContainer,
207
- item,
208
- count
209
- );
210
- } else {
211
- const itemStack = targetContainer.getAvailableItemStack(
212
- server,
213
- item.itemDefinitionId,
214
- count,
215
- newSlotId
216
- );
217
- if (itemStack) {
218
- // add to existing item stack
219
- const item = targetContainer.items[itemStack];
220
- item.stackCount += count;
221
- server.updateContainerItem(client, item, targetContainer);
222
- } else {
223
- // add item to end
224
- combineItemStack(
225
- server,
226
- client,
227
- oldStackCount,
228
- targetContainer,
229
- item,
230
- count
231
- );
232
- }
233
- }
234
- }
235
- }
1
+ // ======================================================================
2
+ //
3
+ // GNU GENERAL PUBLIC LICENSE
4
+ // Version 3, 29 June 2007
5
+ // copyright (C) 2020 - 2021 Quentin Gruber
6
+ // copyright (C) 2021 - 2023 H1emu community
7
+ //
8
+ // https://github.com/QuentinGruber/h1z1-server
9
+ // https://www.npmjs.com/package/h1z1-server
10
+ //
11
+ // Based on https://github.com/psemu/soe-network
12
+ // ======================================================================
13
+
14
+ import { ContainerErrors } from "../models/enums";
15
+ import { ZoneServer2016 } from "../zoneserver";
16
+ import { BaseItem } from "./baseItem";
17
+ import { LoadoutItem } from "./loadoutItem";
18
+ import { ZoneClient2016 } from "./zoneclient";
19
+
20
+ // helper functions
21
+ function combineItemStack(
22
+ server: ZoneServer2016,
23
+ client: ZoneClient2016,
24
+ oldStackCount: number,
25
+ targetContainer: LoadoutContainer,
26
+ item: BaseItem,
27
+ count: number
28
+ ) {
29
+ if (oldStackCount == count) {
30
+ // if full stack is moved
31
+ server.addContainerItem(client.character, item, targetContainer, false);
32
+ return;
33
+ }
34
+ // if only partial stack is moved
35
+ server.addContainerItem(
36
+ client.character,
37
+ server.generateItem(item.itemDefinitionId, count),
38
+ targetContainer,
39
+ false
40
+ );
41
+ }
42
+
43
+ export class LoadoutContainer extends LoadoutItem {
44
+ containerDefinitionId: number;
45
+ items: { [itemGuid: string]: BaseItem } = {};
46
+ canAcceptItems: boolean = true;
47
+ acceptedItems: number[] = [];
48
+ readonly isMutable: boolean = true;
49
+ constructor(item: LoadoutItem, containerDefinitionId: number) {
50
+ super(item, item.slotId, item.loadoutItemOwnerGuid);
51
+ this.containerDefinitionId = containerDefinitionId;
52
+ }
53
+
54
+ /**
55
+ * Gets the used bulk of this container.
56
+ * @param server The ZoneServer instance.
57
+ * @returns Returns the amount of bulk used.
58
+ */
59
+ getUsedBulk(server: ZoneServer2016): number {
60
+ let bulk = 0;
61
+ for (const item of Object.values(this.items)) {
62
+ bulk +=
63
+ server.getItemDefinition(item.itemDefinitionId).BULK * item.stackCount;
64
+ }
65
+ return bulk;
66
+ }
67
+
68
+ /**
69
+ * Gets the maximum bulk that this container can hold.
70
+ * @param server The ZoneServer instance.
71
+ */
72
+ getMaxBulk(server: ZoneServer2016): number {
73
+ return server.getContainerDefinition(this.containerDefinitionId).MAX_BULK;
74
+ }
75
+
76
+ /**
77
+ * Gets the available bulk for this container.
78
+ * @param server The ZoneServer instance.
79
+ * @returns Returns the amount of bulk available.
80
+ */
81
+ getAvailableBulk(server: ZoneServer2016): number {
82
+ const availableBulk = this.getMaxBulk(server) - this.getUsedBulk(server);
83
+ // prevents returning a negative available bulk for containers with 0 max bulk
84
+ return availableBulk <= 0 ? 0 : availableBulk;
85
+ }
86
+
87
+ /**
88
+ * Returns a boolean if this container has enough space for a given amount of a certain item.
89
+ * @param server The ZoneServer instance.
90
+ * @param itemDefinitionId The definiton id of the item to check.
91
+ * @param count The amount of the item to check.
92
+ */
93
+ getHasSpace(
94
+ server: ZoneServer2016,
95
+ itemDefinitionId: number,
96
+ count: number
97
+ ): boolean {
98
+ if (this.getMaxBulk(server) == 0) return true; // for external containers
99
+ return !!(
100
+ this.getMaxBulk(server) -
101
+ (this.getUsedBulk(server) +
102
+ server.getItemDefinition(itemDefinitionId).BULK * count) >=
103
+ 0
104
+ );
105
+ }
106
+
107
+ /**
108
+ * Gets an item stack in a container that has space for a specified item.
109
+ * @param server The ZoneServer instance.
110
+ * @param itemDefId The item definition ID of the item stack to check.
111
+ * @param count The amount of items to fit into the stack.
112
+ * @param slotId Optional: The slotId of a specific item stack to check.
113
+ * @returns Returns the itemGuid of the item stack.
114
+ */
115
+ getAvailableItemStack(
116
+ server: ZoneServer2016,
117
+ itemDefId: number,
118
+ count: number
119
+ ): string {
120
+ //
121
+ // if slotId is defined, then only an item with the same slotId will be returned
122
+ if (server.getItemDefinition(itemDefId).MAX_STACK_SIZE == 1) return "";
123
+ for (const item of Object.values(this.items)) {
124
+ if (
125
+ item.itemDefinitionId == itemDefId &&
126
+ server.getItemDefinition(item.itemDefinitionId).MAX_STACK_SIZE >=
127
+ item.stackCount + count
128
+ ) {
129
+ return item.itemGuid;
130
+ }
131
+ }
132
+ return "";
133
+ }
134
+
135
+ /**
136
+ * Gets the maximum slots that this container can hold.
137
+ * @param server The ZoneServer instance.
138
+ */
139
+ getMaxSlots(server: ZoneServer2016) {
140
+ return server.getContainerDefinition(this.containerDefinitionId)
141
+ .MAXIMUM_SLOTS;
142
+ }
143
+
144
+ // transfers an item from this container to another
145
+ transferItem(
146
+ server: ZoneServer2016,
147
+ targetContainer: LoadoutContainer,
148
+ item: BaseItem,
149
+ newSlotId: number,
150
+ count?: number
151
+ ) {
152
+ if (!count) count = item.stackCount;
153
+ const oldStackCount = item.stackCount; // saves stack count before it gets altered
154
+
155
+ let client;
156
+
157
+ const lootableEntity = server.getLootableEntity(this.loadoutItemOwnerGuid);
158
+ if (lootableEntity) {
159
+ const mountedCharacterId = lootableEntity.mountedCharacter;
160
+ if (mountedCharacterId)
161
+ client = server.getClientByCharId(mountedCharacterId);
162
+ } else {
163
+ client = server.getClientByCharId(this.loadoutItemOwnerGuid);
164
+ }
165
+
166
+ if (!client) return;
167
+
168
+ if (!targetContainer.canAcceptItems) {
169
+ server.containerError(client, ContainerErrors.DOES_NOT_ACCEPT_ITEMS);
170
+ return;
171
+ }
172
+
173
+ if (
174
+ targetContainer.acceptedItems.length &&
175
+ !targetContainer.acceptedItems.includes(item.itemDefinitionId)
176
+ ) {
177
+ server.containerError(client, ContainerErrors.UNACCEPTED_ITEM);
178
+ return;
179
+ }
180
+
181
+ if (!this.isMutable || !targetContainer.isMutable) {
182
+ server.containerError(client, ContainerErrors.NOT_MUTABLE);
183
+ return;
184
+ }
185
+
186
+ if (
187
+ this.containerGuid != targetContainer.containerGuid &&
188
+ !targetContainer.getHasSpace(server, item.itemDefinitionId, count)
189
+ ) {
190
+ // allows items in the same container but different stacks to be stacked
191
+ return;
192
+ }
193
+ if (!server.removeContainerItem(client.character, item, this, count)) {
194
+ server.containerError(client, ContainerErrors.NO_ITEM_IN_SLOT);
195
+ return;
196
+ }
197
+ const itemStack = targetContainer.getAvailableItemStack(
198
+ server,
199
+ item.itemDefinitionId,
200
+ count
201
+ );
202
+ if (itemStack) {
203
+ // add to existing item stack
204
+ const item = targetContainer.items[itemStack];
205
+ item.stackCount += count;
206
+ server.updateContainerItem(client, item, targetContainer);
207
+ } else {
208
+ // add item to end
209
+ combineItemStack(
210
+ server,
211
+ client,
212
+ oldStackCount,
213
+ targetContainer,
214
+ item,
215
+ count
216
+ );
217
+ }
218
+ }
219
+ }