Skip to content

Commit bf27f76

Browse files
committed
feat: merge upstream (CloudburstMC/NBT@8c4f814)
1 parent 3154d5f commit bf27f76

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

src/main/java/org/cloudburstmc/nbt/NbtList.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ public boolean equals(Object o) {
6969

7070
@Override
7171
public int hashCode() {
72-
if (this.hashCodeGenerated) return this.hashCode;
73-
int result = Objects.hash(super.hashCode(), type);
74-
result = 31 * result + Arrays.deepHashCode(array);
72+
if (this.hashCodeGenerated)
73+
return this.hashCode;
74+
int result = type.hashCode();
75+
for (Object object : array) {
76+
result = 31 * result + NbtUtils.hashCode(object);
77+
}
7578
this.hashCode = result;
7679
this.hashCodeGenerated = true;
7780
return result;

src/main/java/org/cloudburstmc/nbt/NbtMap.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,14 @@ public boolean equals(Object o) {
408408
public int hashCode() {
409409
if (this.hashCodeGenerated)
410410
return this.hashCode;
411-
int h = 0;
412-
for (Entry<String, Object> stringObjectEntry : this.map.entrySet())
413-
h += stringObjectEntry.hashCode();
414-
this.hashCode = h;
411+
int result = 1;
412+
for (Entry<String, Object> stringObjectEntry : this.map.entrySet()) {
413+
result = 31 * result + (stringObjectEntry.getKey().hashCode()
414+
^ NbtUtils.hashCode(stringObjectEntry.getValue()));
415+
}
416+
this.hashCode = result;
415417
this.hashCodeGenerated = true;
416-
return h;
418+
return result;
417419
}
418420

419421
@Override

src/main/java/org/cloudburstmc/nbt/NbtUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,19 @@ public static String printHexBinary(byte[] data) {
183183
return r.toString();
184184
}
185185

186+
public static int hashCode(Object object) {
187+
if (object instanceof byte[]) {
188+
return Arrays.hashCode((byte[]) object);
189+
}
190+
if (object instanceof int[]) {
191+
return Arrays.hashCode((int[]) object);
192+
}
193+
if (object instanceof long[]) {
194+
return Arrays.hashCode((long[]) object);
195+
}
196+
return object.hashCode();
197+
}
198+
186199
/**
187200
* Write each {@link java.lang.reflect.RecordComponent RecordComponent} from record that be marked {@link NBT} to the specified nbtMap
188201
*

src/test/java/org/cloudburstmc/nbt/NbtTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ void networkTest() {
105105
try (NBTInputStream in = NbtUtils.createNetworkReader(bais)) {
106106
Object o = in.readTag();
107107
Assertions.assertEquals(TEST_MAP, o);
108+
Assertions.assertEquals(TEST_MAP.hashCode(), o.hashCode());
108109
} catch (Exception e) {
109110
throw new AssertionError("Error whilst decoding tag", e);
110111
}

0 commit comments

Comments
 (0)