Skip to content

Commit

Permalink
Allow item hover event count to be specified as any number
Browse files Browse the repository at this point in the history
Fixes compatibility with vanilla codec, as vanilla allows any number to be decoded within DFU
  • Loading branch information
booky10 committed Jul 23, 2024
1 parent ef2d24c commit cc03d30
Showing 1 changed file with 17 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public AdventureNBTSerializer(boolean downsampleColor) {
} else {
NBTReader child = hoverEvent.child("contents");
Key itemId = child.readUTF("id", Key::key);
Integer count = child.readInt("count", Function.identity());
Integer count = child.readNumber("count", Number::intValue);
int nonNullCount = count == null ? 1 : count;

BinaryTagHolder tag = child.readUTF("tag", BinaryTagHolder::binaryTagHolder);
Expand Down Expand Up @@ -598,36 +598,24 @@ public <R> R readShort(String key, Function<Short, R> function) {
return withTag(key, tag -> function.apply(requireType(tag, NBTType.SHORT).getAsShort()));
}

public void useInt(String key, Consumer<Integer> consumer) {
useTag(key, tag -> consumer.accept(requireType(tag, NBTType.INT).getAsInt()));
}

public <R> R readInt(String key, Function<Integer, R> function) {
return withTag(key, tag -> function.apply(requireType(tag, NBTType.INT).getAsInt()));
}

public void useLong(String key, Consumer<Long> consumer) {
useTag(key, tag -> consumer.accept(requireType(tag, NBTType.LONG).getAsLong()));
}

public <R> R readLong(String key, Function<Long, R> function) {
return withTag(key, tag -> function.apply(requireType(tag, NBTType.LONG).getAsLong()));
}

public void useFloat(String key, Consumer<Float> consumer) {
useTag(key, tag -> consumer.accept(requireType(tag, NBTType.FLOAT).getAsFloat()));
}

public <R> R readFloat(String key, Function<Float, R> function) {
return withTag(key, tag -> function.apply(requireType(tag, NBTType.FLOAT).getAsFloat()));
}

public void useDouble(String key, Consumer<Double> consumer) {
useTag(key, tag -> consumer.accept(requireType(tag, NBTType.DOUBLE).getAsDouble()));
public void useNumber(String key, Consumer<Number> consumer) {
useTag(key, tag -> {
if (tag instanceof NBTNumber) {
consumer.accept(((NBTNumber) tag).getAsNumber());
} else {
throw new IllegalArgumentException("Expected number but got " + tag.getType());
}
});
}

public <R> R readDouble(String key, Function<Double, R> function) {
return withTag(key, tag -> function.apply(requireType(tag, NBTType.DOUBLE).getAsDouble()));
public <R> R readNumber(String key, Function<Number, R> function) {
return withTag(key, tag -> {
if (tag instanceof NBTNumber) {
return function.apply(((NBTNumber) tag).getAsNumber());
} else {
throw new IllegalArgumentException("Expected number but got " + tag.getType());
}
});
}

public void useUTF(String key, Consumer<String> consumer) {
Expand Down

0 comments on commit cc03d30

Please sign in to comment.