From e0edf0fb33a9f8f7eb210b5fb36e9ca8e8be2219 Mon Sep 17 00:00:00 2001 From: Riley Park Date: Wed, 24 Apr 2024 11:37:37 -0700 Subject: [PATCH] fix #1068 (nbt): CompoundBinaryTag#getBoolean ignores false values when default value is true --- .../adventure/nbt/CompoundBinaryTag.java | 8 +++- .../adventure/nbt/CompoundBinaryTagTest.java | 45 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 nbt/src/test/java/net/kyori/adventure/nbt/CompoundBinaryTagTest.java diff --git a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java index 46147a842..843b2748e 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/CompoundBinaryTag.java @@ -125,8 +125,12 @@ default boolean getBoolean(final @NotNull String key) { * @since 4.0.0 */ default boolean getBoolean(final @NotNull String key, final boolean defaultValue) { - // != 0 might look weird, but it is what vanilla does - return this.getByte(key) != 0 || defaultValue; + final BinaryTag tag = this.get(key); + if (tag instanceof ByteBinaryTag) { + // != 0 might look weird, but it is what vanilla does + return ((ByteBinaryTag) tag).value() != 0; + } + return defaultValue; } /** diff --git a/nbt/src/test/java/net/kyori/adventure/nbt/CompoundBinaryTagTest.java b/nbt/src/test/java/net/kyori/adventure/nbt/CompoundBinaryTagTest.java new file mode 100644 index 000000000..10e82ae7a --- /dev/null +++ b/nbt/src/test/java/net/kyori/adventure/nbt/CompoundBinaryTagTest.java @@ -0,0 +1,45 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2024 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.nbt; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class CompoundBinaryTagTest { + @DisplayName("https://github.com/KyoriPowered/adventure/issues/1068") + @Test + void test1068() { + final CompoundBinaryTag tagWithValue = CompoundBinaryTag.builder() + .putBoolean("test", false) + .build(); + final CompoundBinaryTag tagWithoutValue = CompoundBinaryTag.builder() + .build(); + + assertFalse(tagWithValue.getBoolean("test", true)); + assertTrue(tagWithoutValue.getBoolean("test", true)); + } +}