Skip to content

Commit

Permalink
2 years old
Browse files Browse the repository at this point in the history
  • Loading branch information
phoshp committed Jun 12, 2024
1 parent ec12df7 commit d3d1dc7
Show file tree
Hide file tree
Showing 21 changed files with 133 additions and 141 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.idea
*.iml
.project
.settings

.gradle
**/build/
Expand All @@ -12,4 +14,4 @@ gradle-app.setting
!gradle-wrapper.jar

# Cache of project
.gradletasknamecache
.gradletasknamecache
32 changes: 28 additions & 4 deletions src/main/java/com/bedrockk/packs/PackHelper.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.bedrockk.packs;

import com.bedrockk.packs.annotation.JsonConverter;
import com.bedrockk.packs.definition.*;
import com.bedrockk.packs.definition.recipe.RecipeDefinition;
import com.bedrockk.packs.json.PackAnnotationIntrospector;
import com.bedrockk.packs.json.PackModule;
import com.bedrockk.packs.json.VersionConverter;
import com.bedrockk.packs.type.SemVersion;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.util.ClassUtil;
import lombok.experimental.UtilityClass;

import java.io.IOException;
Expand All @@ -37,6 +40,7 @@ public static synchronized <T> T deserialize(String json, Class<T> type) throws
} else {
CURRENT_DEFINITION_VERSION = null;
}
MAPPER.setConfig(MAPPER.getDeserializationConfig().withAttribute("no-converter", false));
T value = MAPPER.readValue(json, type);
CURRENT_DEFINITION_VERSION = null;
return value;
Expand Down Expand Up @@ -122,12 +126,32 @@ public static RenderControllerDefinition deserializeRenderController(String json
return deserialize(json, RenderControllerDefinition.class);
}

public static String serialize(Object object) throws JsonProcessingException {
return MAPPER.writeValueAsString(object);
public static <T> String serialize(T object) throws JsonProcessingException {
return serialize(object, null, false);
}

public static String serializeAsPretty(Object object) throws JsonProcessingException {
return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(object);
public static <T> String serializePretty(T object) throws JsonProcessingException {
return serialize(object, null, true);
}

public static <T> String serialize(T object, SemVersion version, boolean pretty) throws JsonProcessingException {
var converter = object.getClass().getAnnotation(JsonConverter.class);
Object input = object;
if (converter != null && converter.past() != VersionConverter.None.class) {
var inst = ClassUtil.createInstance(converter.past(), true);
if (inst != null) {
var node = toJsonNode(object);
if (version == null) {
if (node.has("format_version") && node.get("format_version").isTextual()) {
version = SemVersion.of(node.get("format_version").asText());
} else {
version = FORMAT_VERSION;
}
}
input = inst.convert(toJsonNode(input), version);
}
}
return pretty ? MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(object) : MAPPER.writeValueAsString(object);
}

public static <T> T convert(Object value, Class<T> type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bedrockk.packs.annotation;

import com.bedrockk.packs.json.VersionConverter;
import com.fasterxml.jackson.databind.util.Converter;

import java.lang.annotation.ElementType;
Expand All @@ -11,7 +12,7 @@
@Target({ElementType.TYPE, ElementType.FIELD})
public @interface JsonConverter {

Class<? extends Converter> current() default Converter.None.class;
Class<? extends VersionConverter> current() default VersionConverter.None.class;

Class<? extends Converter> past() default Converter.None.class;
Class<? extends VersionConverter> past() default VersionConverter.None.class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,11 @@ public static class ReplaceRule implements PackNode {
private List<BlockReference> mayReplace;
}

public static class Converter extends VersionConverter<OreFeatureDefinition> {
public static class Converter implements VersionConverter {

@Override
public boolean isValid(SemVersion version) {
return version.isLower(FormatVersions.V1_16_220);
}

@Override
public JsonNode apply(JsonNode value) {
if (value instanceof ObjectNode node && value.has("may_replace")) {
public JsonNode toCurrent(JsonNode value, SemVersion version) {
if (version.isLower(FormatVersions.V1_16_220) && value instanceof ObjectNode node && value.has("may_replace")) {
var array = PackHelper.MAPPER.createArrayNode();
var rule = PackHelper.MAPPER.createObjectNode();
rule.set("places_block", node.remove("places_block"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,11 @@ public class EntityPlacerDefinition implements ItemComponentNode {
@Singular("canDispenseOn")
private List<String> dispenseOn;

public static class Converter extends VersionConverter<EntityPlacerDefinition> {
public static class Converter implements VersionConverter {

@Override
public boolean isValid(SemVersion version) {
return version.isLower(FormatVersions.V1_16_100);
}

@Override
public JsonNode apply(JsonNode value) {
if (value instanceof ObjectNode node) {
public JsonNode toCurrent(JsonNode value, SemVersion version) {
if (version.isLower(FormatVersions.V1_16_100) && value instanceof ObjectNode node) {
if (node.has("allowedBlocks")) {
node.set("useOn", node.remove("allowedBlocks"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,6 @@ public static class RenderOffset implements PackNode {
private ImmutableVec3 scale;
}

public static class Converter extends VersionConverter<RenderOffsetsDefinition> {

@Override
public boolean isValid(SemVersion version) {
return version.isLower(FormatVersions.V1_16_200);
}

@Override
public JsonNode apply(JsonNode value) {
// TODO
return value.isTextual() ? PackHelper.MAPPER.createObjectNode() : value;
}
}

public enum HandType {
MAIN_HAND,
OFF_HAND;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public UseDurationDefinition(Integer value) {
super(value);
}

public static class Converter extends VersionConverter<UseDurationDefinition> {
public static class Converter implements VersionConverter {
@Override
public JsonNode apply(JsonNode value) {
public JsonNode toCurrent(JsonNode value, SemVersion version) {
return value.isDouble() ? PackHelper.toJsonNode((int) value.asDouble() * 20) : value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.bedrockk.packs.utils.FormatVersions;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.util.StdConverter;
import lombok.*;
import lombok.extern.jackson.Jacksonized;

Expand All @@ -33,16 +32,11 @@ public static class Pattern implements PackNode {
private String color;
}

public static class Converter extends VersionConverter<SetBannerDetailsFunction> {
public static class Converter implements VersionConverter {

@Override
public boolean isValid(SemVersion version) {
return version.isLower(FormatVersions.V1_16_100);
}

@Override
public JsonNode apply(JsonNode value) {
if (value instanceof ObjectNode node && value.has("type")) {
public JsonNode toCurrent(JsonNode value, SemVersion version) {
if (version.isLower(FormatVersions.V1_16_100) && value instanceof ObjectNode node && value.has("type")) {
var type = value.get("type");
if (type.isObject() && type.has("min") && type.has("max")) {
node.set("type", PackHelper.toJsonNode("default"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@ public BlockDestroyTimeDescription(Integer value) {
super(value);
}

public static class Converter extends VersionConverter<BlockDestroyTimeDescription> {
public static class Converter implements VersionConverter {

@Override
public boolean isValid(SemVersion version) {
return version.isLower(FormatVersions.V1_16_0);
}

@Override
public JsonNode apply(JsonNode value) {
return value.isDouble() ? PackHelper.toJsonNode((int) value.get("value").asDouble() * 20) : value;
public JsonNode toCurrent(JsonNode value, SemVersion version) {
return version.isLower(FormatVersions.V1_16_0) && value.isDouble() ? PackHelper.toJsonNode((int) value.get("value").asDouble() * 20) : value;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bedrockk.packs.description.block;

import com.bedrockk.packs.PackHelper;
import com.bedrockk.packs.annotation.JsonConverter;
import com.bedrockk.packs.description.BlockDescription;
import com.bedrockk.packs.json.VersionConverter;
Expand All @@ -15,16 +14,11 @@ public BlockExplodeableDescription(Double value) {
super(value);
}

public static class Converter extends VersionConverter<BlockExplodeableDescription> {
public static class Converter implements VersionConverter {

@Override
public boolean isValid(SemVersion version) {
return version.isLower(FormatVersions.V1_16_0);
}

@Override
public JsonNode apply(JsonNode value) {
return value.isDouble() ? value.get("value") : value;
public JsonNode toCurrent(JsonNode value, SemVersion version) {
return version.isLower(FormatVersions.V1_16_0) && value.isDouble() ? value.get("value") : value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ public BlockFrictionDescription(Double value) {
super(value);
}

public static class Converter extends VersionConverter<BlockFrictionDescription> {
public static class Converter implements VersionConverter {

@Override
public boolean isValid(SemVersion version) {
return version.isLower(FormatVersions.V1_16_0);
}

@Override
public JsonNode apply(JsonNode value) {
return value.isDouble() ? value.get("value") : value;
public JsonNode toCurrent(JsonNode value, SemVersion version) {
return version.isLower(FormatVersions.V1_16_0) && value.isDouble() ? value.get("value") : value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ public BlockLightAbsorptionDescription(Double value) {
super(value);
}

public static class Converter extends VersionConverter<BlockLightAbsorptionDescription> {
public static class Converter implements VersionConverter {

@Override
public boolean isValid(SemVersion version) {
return version.isLower(FormatVersions.V1_16_0);
}

@Override
public JsonNode apply(JsonNode value) {
return value.isDouble() ? value.get("value") : value;
public JsonNode toCurrent(JsonNode value, SemVersion version) {
return version.isLower(FormatVersions.V1_16_0) && value.isDouble() ? value.get("value") : value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ public BlockLightEmissionDescription(Double value) {
super(value);
}

public static class Converter extends VersionConverter<BlockLightEmissionDescription> {
public static class Converter implements VersionConverter {

@Override
public boolean isValid(SemVersion version) {
return version.isLower(FormatVersions.V1_16_0);
}

@Override
public JsonNode apply(JsonNode value) {
return value.isDouble() ? value.get("value") : value;
public JsonNode toCurrent(JsonNode value, SemVersion version) {
return version.isLower(FormatVersions.V1_16_0) && value.isDouble() ? value.get("value") : value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@ public BlockLootDescription(Path value) {
super(value);
}

public static class Converter extends VersionConverter<BlockLootDescription> {
public static class Converter implements VersionConverter {

@Override
public boolean isValid(SemVersion version) {
return version.isLower(FormatVersions.V1_16_0);
}

@Override
public JsonNode apply(JsonNode value) {
return value.isDouble() ? value.get("value") : value;
public JsonNode toCurrent(JsonNode value, SemVersion version) {
return version.isLower(FormatVersions.V1_16_0) && value.isDouble() ? value.get("value") : value;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bedrockk.packs.description.block;

import com.bedrockk.packs.PackHelper;
import com.bedrockk.packs.annotation.JsonConverter;
import com.bedrockk.packs.description.BlockDescription;
import com.bedrockk.packs.json.VersionConverter;
Expand All @@ -12,20 +13,29 @@
@JsonConverter(current = BlockMapColorDescription.Converter.class)
public class BlockMapColorDescription extends SingleValueNode<Integer> implements BlockDescription {
@JsonCreator
public BlockMapColorDescription(Integer value) {
public BlockMapColorDescription(int value) {
super(value);
}

public static class Converter extends VersionConverter<BlockMapColorDescription> {
@JsonCreator
public static BlockMapColorDescription of(JsonNode node) {
return new BlockMapColorDescription(node.asInt());
}

public static class Converter implements VersionConverter {

@Override
public boolean isValid(SemVersion version) {
return version.isLower(FormatVersions.V1_16_0);
public JsonNode toCurrent(JsonNode node, SemVersion version) {
return version.isLower(FormatVersions.V1_16_0) && node.isObject() ? node.get("color") : node;
}

@Override
public JsonNode apply(JsonNode node) {
return node.isObject() ? node.get("color") : node;
public JsonNode toSpecific(JsonNode node, SemVersion version) {
if (version.isLower(FormatVersions.V1_16_0) && node.isNumber()) {
var wrap = PackHelper.MAPPER.createObjectNode();
wrap.set("value", node);
return wrap;
}
return node;
}
}
}
Loading

0 comments on commit d3d1dc7

Please sign in to comment.