Skip to content

Commit e0edf0f

Browse files
committed
fix #1068 (nbt): CompoundBinaryTag#getBoolean ignores false values when default value is true
1 parent 9dbba42 commit e0edf0f

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,12 @@ default boolean getBoolean(final @NotNull String key) {
125125
* @since 4.0.0
126126
*/
127127
default boolean getBoolean(final @NotNull String key, final boolean defaultValue) {
128-
// != 0 might look weird, but it is what vanilla does
129-
return this.getByte(key) != 0 || defaultValue;
128+
final BinaryTag tag = this.get(key);
129+
if (tag instanceof ByteBinaryTag) {
130+
// != 0 might look weird, but it is what vanilla does
131+
return ((ByteBinaryTag) tag).value() != 0;
132+
}
133+
return defaultValue;
130134
}
131135

132136
/**
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of adventure, licensed under the MIT License.
3+
*
4+
* Copyright (c) 2017-2024 KyoriPowered
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package net.kyori.adventure.nbt;
25+
26+
import org.junit.jupiter.api.DisplayName;
27+
import org.junit.jupiter.api.Test;
28+
29+
import static org.junit.jupiter.api.Assertions.assertFalse;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
32+
class CompoundBinaryTagTest {
33+
@DisplayName("https://github.com/KyoriPowered/adventure/issues/1068")
34+
@Test
35+
void test1068() {
36+
final CompoundBinaryTag tagWithValue = CompoundBinaryTag.builder()
37+
.putBoolean("test", false)
38+
.build();
39+
final CompoundBinaryTag tagWithoutValue = CompoundBinaryTag.builder()
40+
.build();
41+
42+
assertFalse(tagWithValue.getBoolean("test", true));
43+
assertTrue(tagWithoutValue.getBoolean("test", true));
44+
}
45+
}

0 commit comments

Comments
 (0)