Skip to content

Commit e9f547f

Browse files
committed
Add ArrayUtil
1 parent b7f388b commit e9f547f

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/rewriter/EntityPacketRewriter1_21.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.viaversion.viaversion.protocols.v1_20_5to1_21.data.Paintings1_20_5;
3838
import com.viaversion.viaversion.protocols.v1_20_5to1_21.storage.EfficiencyAttributeStorage;
3939
import com.viaversion.viaversion.rewriter.EntityRewriter;
40+
import com.viaversion.viaversion.util.ArrayUtil;
4041
import com.viaversion.viaversion.util.Key;
4142

4243
public final class EntityPacketRewriter1_21 extends EntityRewriter<ClientboundPacket1_20_5, Protocol1_20_5To1_21> {
@@ -60,7 +61,7 @@ public void registerPackets() {
6061
campfireDamageType.putString("scaling", "when_caused_by_living_non_player");
6162
campfireDamageType.putString("message_id", "inFire");
6263
campfireDamageType.putFloat("exhaustion", 0.1F);
63-
wrapper.set(Types.REGISTRY_ENTRY_ARRAY, 0, addRegistryEntries(entries, new RegistryEntry("minecraft:campfire", campfireDamageType)));
64+
wrapper.set(Types.REGISTRY_ENTRY_ARRAY, 0, ArrayUtil.add(entries, new RegistryEntry("minecraft:campfire", campfireDamageType)));
6465
} else {
6566
handleRegistryData1_20_5(wrapper.user(), type, entries);
6667
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
3+
* Copyright (C) 2016-2024 ViaVersion and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.viaversion.viaversion.util;
19+
20+
import java.util.Arrays;
21+
22+
/**
23+
* For type safety and effort reasons, buffer types use arrays instead of lists.
24+
* <p>
25+
* This class includes simple methods to work with these arrays in case they need to be modified
26+
* (obviously being more expensive due to the required array copies for every modification).
27+
*/
28+
public final class ArrayUtil {
29+
30+
public static <T> T[] add(final T[] array, final T element) {
31+
final int length = array.length;
32+
final T[] newArray = Arrays.copyOf(array, length + 1);
33+
newArray[length] = element;
34+
return newArray;
35+
}
36+
37+
@SafeVarargs
38+
public static <T> T[] add(final T[] array, final T... elements) {
39+
final int length = array.length;
40+
final T[] newArray = Arrays.copyOf(array, length + elements.length);
41+
System.arraycopy(elements, 0, newArray, length, elements.length);
42+
return newArray;
43+
}
44+
45+
public static <T> T[] remove(final T[] array, final int index) {
46+
final T[] newArray = Arrays.copyOf(array, array.length - 1);
47+
System.arraycopy(array, index + 1, newArray, index, newArray.length - index);
48+
return newArray;
49+
}
50+
}

0 commit comments

Comments
 (0)