Skip to content

Commit

Permalink
Merge pull request #222 from SoSeDiK/bool
Browse files Browse the repository at this point in the history
Add support for boolean in getOrDefault/getOrNull
  • Loading branch information
tr7zw authored May 6, 2023
2 parents c22cbaa + 4a0e1b5 commit 5759b68
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,8 @@ public NBTCompoundList getCompoundList(String name) {
* Returns the stored value if exists, or provided value otherwise.
* <p>
* Supported types:
* {@code byte/Byte, short/Short, int/Integer, long/Long, float/Float, double/Double, byte[], int[]},
* {@link String}, {@link UUID}
* {@code Boolean, Byte, Short, Integer, Long, Float, Double, byte[], int[]},
* {@link String}, {@link UUID}, and {@link Enum}
*
* @param key key
* @param defaultValue default non-null value
Expand All @@ -873,6 +873,8 @@ public <T> T getOrDefault(String key, T defaultValue) {
return defaultValue;

Class<?> clazz = defaultValue.getClass();
if (clazz == Boolean.class)
return (T) getBoolean(key);
if (clazz == Byte.class)
return (T) getByte(key);
if (clazz == Short.class)
Expand Down Expand Up @@ -908,13 +910,13 @@ public <T> T getOrDefault(String key, T defaultValue) {
* Returns the stored value if exists, or null.
* <p>
* Supported types:
* {@code Byte, Short, Integer, Long, Float, Double, byte[], int[]},
* {@link String}, {@link UUID}
* {@code Boolean, Byte, Short, Integer, Long, Float, Double, byte[], int[]},
* {@link String}, {@link UUID}, and {@link Enum}
*
* @param key key
* @param type data type
* @param <T> value type
* @return Stored or provided value
* @return Stored value or null
*/
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
Expand All @@ -924,6 +926,8 @@ public <T> T getOrNull(String key, Class<?> type) {
if (!hasTag(key))
return null;

if (type == Boolean.class)
return (T) getBoolean(key);
if (type == Byte.class)
return (T) getByte(key);
if (type == Short.class)
Expand Down

0 comments on commit 5759b68

Please sign in to comment.