Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/GlowstoneMC/Glowstone into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mastercoms committed Jul 20, 2018
2 parents 15c2217 + 8108bfa commit ff90a7f
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 12 deletions.
14 changes: 11 additions & 3 deletions src/main/java/net/glowstone/util/mojangson/Mojangson.java
Expand Up @@ -89,9 +89,17 @@ public static Tag parseTag(String mojangson) throws MojangsonParseException {
return parseDouble(
mojangson);
} catch (MojangsonParseException e2) {
// Couldn't find anything matching it, assuming it is a String.
return parseString(
mojangson);
switch (mojangson) {
case "true":
return new ByteTag((byte)1);

case "false":
return new ByteTag((byte)0);

default:
// Couldn't find anything matching it, assuming it is a String.
return parseString(mojangson);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/glowstone/util/nbt/ByteTag.java
Expand Up @@ -3,7 +3,7 @@
/**
* The {@code TAG_Byte} tag.
*/
public class ByteTag extends Tag<Byte> {
public class ByteTag extends NumericTag<Byte> {

/**
* The value.
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/net/glowstone/util/nbt/CompoundTag.java
Expand Up @@ -143,6 +143,16 @@ public void mergeInto(CompoundTag other, boolean overwrite) {
// Simple gets


/**
* Returns the value of a numeric subtag.
*
* @param key the key to look up
* @return the numeric tag value
*/
public Number getNumber(String key) {
return get(key, NumericTag.class);
}

/**
* Returns the value of a {@code byte} subtag.
*
Expand Down Expand Up @@ -188,7 +198,7 @@ public int getInt(@NonNls String key) {

@Override
public boolean getBoolean(@NonNls String key) {
return getByte(key) != 0;
return getNumber(key).byteValue() != 0;
}


Expand All @@ -200,7 +210,7 @@ public boolean getBoolean(@NonNls String key) {
* @return the tag value as a boolean, or defaultValue if it's not a byte
*/
public boolean getBoolean(@NonNls String key, boolean defaultValue) {
return isByte(key) ? getBoolean(key) : defaultValue;
return isNumeric(key) ? getBoolean(key) : defaultValue;
}

/**
Expand Down Expand Up @@ -725,6 +735,16 @@ public List<CompoundTag> getCompoundList(@NonNls String key) {
////////////////////////////////////////////////////////////////////////////
// Simple is

/**
* Test whether the subtag with the given key is of a numeric type.
*
* @param key the key to look up
* @return true if the subtag exists and is numeric; false otherwise
*/
public boolean isNumeric(@NonNls String key) {
return is(key, NumericTag.class);
}

/**
* Test whether the subtag with the given key is of {@code byte} type.
*
Expand Down Expand Up @@ -981,7 +1001,7 @@ private <T extends Tag<?>> boolean is(@NonNls String key, Class<T> clazz) {
return false;
}
Tag tag = value.get(key);
return tag != null && clazz == tag.getClass();
return tag != null && clazz.isAssignableFrom(tag.getClass());
}

void put(String key, Tag tag) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/glowstone/util/nbt/DoubleTag.java
Expand Up @@ -3,7 +3,7 @@
/**
* The {@code TAG_Double} tag.
*/
public final class DoubleTag extends Tag<Double> {
public final class DoubleTag extends NumericTag<Double> {

/**
* The value.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/glowstone/util/nbt/FloatTag.java
Expand Up @@ -3,7 +3,7 @@
/**
* The {@code TAG_Float} tag.
*/
public final class FloatTag extends Tag<Float> {
public final class FloatTag extends NumericTag<Float> {

/**
* The value.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/glowstone/util/nbt/IntTag.java
Expand Up @@ -3,7 +3,7 @@
/**
* The {@code TAG_Int} tag.
*/
public final class IntTag extends Tag<Integer> {
public final class IntTag extends NumericTag<Integer> {

/**
* The value.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/glowstone/util/nbt/LongTag.java
Expand Up @@ -3,7 +3,7 @@
/**
* The {@code TAG_Long} tag.
*/
public final class LongTag extends Tag<Long> {
public final class LongTag extends NumericTag<Long> {

/**
* The value.
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/glowstone/util/nbt/NumericTag.java
@@ -0,0 +1,7 @@
package net.glowstone.util.nbt;

public abstract class NumericTag<T extends Number> extends Tag<T> {
public NumericTag(TagType type) {
super(type);
}
}
2 changes: 1 addition & 1 deletion src/main/java/net/glowstone/util/nbt/ShortTag.java
Expand Up @@ -3,7 +3,7 @@
/**
* The {@code TAG_Short} tag.
*/
public final class ShortTag extends Tag<Short> {
public final class ShortTag extends NumericTag<Short> {

/**
* The value.
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/net/glowstone/util/mojangson/MojangsonParseTest.java
@@ -1,5 +1,6 @@
package net.glowstone.util.mojangson;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.Arrays;
Expand Down Expand Up @@ -45,4 +46,30 @@ public void canParseType(TagType key, String json) {
+ e.getMessage());
}
}

public static Collection<Arguments> getBooleanCases() {
return Arrays.asList(
Arguments.of("{value:true}", true),
Arguments.of("{value:false}", false),
Arguments.of("{value:1b}", true),
Arguments.of("{value:0b}", false),
Arguments.of("{value:1s}", true),
Arguments.of("{value:0s}", false),
Arguments.of("{value:1l}", true),
Arguments.of("{value:0l}", false),
Arguments.of("{value:1.0f}", true),
Arguments.of("{value:0.0f}", false),
Arguments.of("{value:1.0d}", true),
Arguments.of("{value:0.0d}", false)
);
}

@MethodSource("getBooleanCases")
@ParameterizedTest
public void canParseBoolean(String json, boolean expected) throws MojangsonParseException {
CompoundTag compound = Mojangson.parseCompound(json);
boolean bool = compound.getBoolean("value");

assertEquals(expected, bool);
}
}

0 comments on commit ff90a7f

Please sign in to comment.