Skip to content

Commit

Permalink
fix #1068 (nbt): CompoundBinaryTag#getBoolean ignores false values wh…
Browse files Browse the repository at this point in the history
…en default value is true
  • Loading branch information
kashike committed Apr 24, 2024
1 parent 9dbba42 commit e0edf0f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit e0edf0f

Please sign in to comment.