Skip to content

Commit

Permalink
Add a few suggestions from issue Minestom#7
Browse files Browse the repository at this point in the history
  • Loading branch information
jglrxavpok committed Aug 19, 2021
1 parent 3b385c1 commit 057fa28
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
13 changes: 12 additions & 1 deletion common/src/main/kotlin/org/jglrxavpok/hephaistos/nbt/NBTByte.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ package org.jglrxavpok.hephaistos.nbt
import java.io.DataInputStream
import java.io.DataOutputStream

class NBTByte internal constructor(value: Byte) : NBTNumber<Byte>(value) {
class NBTByte constructor(value: Byte) : NBTNumber<Byte>(value) {

override val ID = NBTType.TAG_Byte

// help Java compiler to find the correct type (boxed vs primitive types)
fun getValue(): Byte = value

/**
* Returns true iif the value is not 0
*/
fun asBoolean(): Boolean = value != 0.toByte()

override fun writeContents(destination: DataOutputStream) {
destination.writeByte(value.toInt())
}
Expand All @@ -22,5 +27,11 @@ class NBTByte internal constructor(value: Byte) : NBTNumber<Byte>(value) {
override fun readContents(source: DataInputStream): NBTByte {
return NBTByte(source.readByte())
}

@JvmStatic
val ONE = NBTByte(1)

@JvmStatic
val ZERO = NBTByte(0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class NBTCompound(val tags: Map<String, NBT> = mapOf()): NBT, NBTCompoundGetters

@Contract(pure = true)
internal fun entry(key: String, value: NBT) = CompoundEntry(key, value)

@JvmStatic
val EMPTY = NBTCompound()
}

data class CompoundEntry(val key: String, val value: NBT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ interface NBTCompoundGetters: Map<String, NBT> {
*/
fun getByte(key: String) = (get(key) as? NBTByte)?.value

/**
* Returns the value associated to the given key, if any. Returns 'null' otherwise.
* Also returns 'null' if the tag is not of the correct type (eg getByte on a NBTCompound will yield 'null')
*
* Uses `NBTByte#asBoolean()` to determine the truthness
*/
fun getBoolean(key: String) = (get(key) as? NBTByte)?.asBoolean()

/**
* Returns the value associated to the given key, if any. Returns 'null' otherwise.
* Also returns 'null' if the tag is not of the correct type (eg getByte on a NBTCompound will yield 'null')
Expand Down
17 changes: 16 additions & 1 deletion common/src/test/java/nbt/Misc.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.jglrxavpok.hephaistos.nbt.*;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

public class Misc {

Expand All @@ -18,4 +18,19 @@ public void snbtArrays() {
array = NBT.LongArray(1, 2, 3);
assertEquals("[L;1L,2L,3L]", array.toSNBT());
}

@Test
public void truthness() {
NBTByte shouldBeFalse = NBT.Byte(0);
assertFalse(shouldBeFalse.asBoolean());

NBTByte shouldBeTrue = NBT.Byte(1);
assertTrue(shouldBeTrue.asBoolean());

NBTByte shouldBeTrue2 = NBT.Byte(-1);
assertTrue(shouldBeTrue2.asBoolean());

NBTByte shouldBeTrueToo = NBT.Byte(42);
assertTrue(shouldBeTrueToo.asBoolean());
}
}

0 comments on commit 057fa28

Please sign in to comment.