Skip to content

Commit

Permalink
temporarily return to older syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
ZZZank committed Aug 9, 2024
1 parent 708abd5 commit ed278dc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@

public class CompoundTagWrapper implements NBTSerializable, MapLike<String, Object>, JsonSerializable, ChangeListener<Tag> {
public static Object unwrap(@Nullable Tag t, @Nullable ChangeListener<Tag> l) {
return switch (t) {
case null -> null;
case EndTag end -> null;
case StringTag str -> t.getAsString();
case NumericTag numeric -> numeric.getAsNumber();
case CompoundTag compound -> new CompoundTagWrapper(compound).withListener(l);
case CollectionTag<?> collection -> new CollectionTagWrapper<>(collection).withListener(l);
default -> t;
};
if (t == null) {
return null;
} else if (t instanceof EndTag) {
return null;
} else if (t instanceof StringTag) {
return t.getAsString();
} else if (t instanceof NumericTag numeric) {
return numeric.getAsNumber();
} else if (t instanceof CompoundTag compound) {
return new CompoundTagWrapper(compound).withListener(l);
} else if (t instanceof CollectionTag<?> collection) {
return new CollectionTagWrapper<>(collection).withListener(l);
}
return t;
}

public static Tag wrap(@Nullable Object o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,20 @@ static JsonElement copy(@Nullable JsonElement element) {
}

static JsonElement of(@Nullable Object o) {
return switch (o) {
case JsonSerializable serializable -> serializable.toJson();
case JsonElement element -> element;
case CharSequence charSequence -> new JsonPrimitive(o.toString());
case Boolean b -> new JsonPrimitive(b);
case Number number -> new JsonPrimitive(number);
case Character c -> new JsonPrimitive(c);
case null, default -> JsonNull.INSTANCE;
};
if (o instanceof JsonSerializable serializable) {
return serializable.toJson();
} else if (o instanceof JsonElement element) {
return element;
} else if (o instanceof CharSequence) {
return new JsonPrimitive(o.toString());
} else if (o instanceof Boolean b) {
return new JsonPrimitive(b);
} else if (o instanceof Number number) {
return new JsonPrimitive(number);
} else if (o instanceof Character c) {
return new JsonPrimitive(c);
}
return JsonNull.INSTANCE;
}

@Nullable
Expand Down
20 changes: 12 additions & 8 deletions common/src/main/java/dev/latvian/mods/rhino/mod/util/NBTUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ else if (o instanceof CharSequence || o instanceof Character) {
} else if (o instanceof Boolean b) {
return ByteTag.valueOf(b);
} else if (o instanceof Number number) {
return switch (number) {
case Byte b -> ByteTag.valueOf(b);
case Short i -> ShortTag.valueOf(i);
case Integer i -> IntTag.valueOf(i);
case Long l -> LongTag.valueOf(l);
case Float v -> FloatTag.valueOf(v);
default -> DoubleTag.valueOf(number.doubleValue());
};
if (number instanceof Byte b) {
return ByteTag.valueOf(b);
} else if (number instanceof Short i) {
return ShortTag.valueOf(i);
} else if (number instanceof Integer i) {
return IntTag.valueOf(i);
} else if (number instanceof Long l) {
return LongTag.valueOf(l);
} else if (number instanceof Float v) {
return FloatTag.valueOf(v);
}
return DoubleTag.valueOf(number.doubleValue());
}
//native json
else if (o instanceof JsonPrimitive json) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,38 @@ static AABB ofVec(Vec3 start, Vec3 end) {
}

static AABB wrap(Object o) {
return switch (o) {
case AABB aabb -> aabb;
case BlockPos blockPos -> ofBlock(blockPos);
case BoundingBox box -> AABB.of(box);
case double[] d -> switch (d.length) {
case 3 -> ofSize(d[0], d[1], d[2]);
case 6 -> of(d[0], d[1], d[2], d[3], d[4], d[5]);
default -> EMPTY;
};
case List<?> l -> switch (l.size()) {
case 3 ->
l.get(0) instanceof Number n0 && l.get(1) instanceof Number n1 && l.get(2) instanceof Number n2
? ofSize(n0.doubleValue(), n1.doubleValue(), n2.doubleValue())
: EMPTY;
case 6 ->
l.get(0) instanceof Number n0 && l.get(1) instanceof Number n1 && l.get(2) instanceof Number n2
&& l.get(3) instanceof Number n3 && l.get(4) instanceof Number n4
&& l.get(5) instanceof Number n5 ? of(
n0.doubleValue(),
n1.doubleValue(),
n2.doubleValue(),
n3.doubleValue(),
n4.doubleValue(),
n5.doubleValue()
) : EMPTY;
default -> EMPTY;
};
case null, default -> EMPTY;
};
if (o instanceof AABB aabb) {
return aabb;
} else if (o instanceof BlockPos blockPos) {
return ofBlock(blockPos);
} else if (o instanceof BoundingBox box) {
return AABB.of(box);
} else if (o instanceof double[] d) {
return switch (d.length) {
case 3 -> ofSize(d[0], d[1], d[2]);
case 6 -> of(d[0], d[1], d[2], d[3], d[4], d[5]);
default -> EMPTY;
};
} else if (o instanceof List<?> l) {
return switch (l.size()) {
case 3 ->
l.get(0) instanceof Number n0 && l.get(1) instanceof Number n1 && l.get(2) instanceof Number n2
? ofSize(n0.doubleValue(), n1.doubleValue(), n2.doubleValue())
: EMPTY;
case 6 ->
l.get(0) instanceof Number n0 && l.get(1) instanceof Number n1 && l.get(2) instanceof Number n2
&& l.get(3) instanceof Number n3 && l.get(4) instanceof Number n4
&& l.get(5) instanceof Number n5 ? of(
n0.doubleValue(),
n1.doubleValue(),
n2.doubleValue(),
n3.doubleValue(),
n4.doubleValue(),
n5.doubleValue()
) : EMPTY;
default -> EMPTY;
};
}
return EMPTY;
}
}

0 comments on commit ed278dc

Please sign in to comment.