From b894665d65a5c6448ae138bc895179d43ae52244 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 29 Aug 2022 14:47:25 +0200 Subject: [PATCH] Implement the replace flag for state maps --- .../JsonReversableBlockStateConverter.java | 2 +- .../unicopia/block/state/StateMapLoader.java | 55 ++++++++++++++++++- .../unicopia/block/state/StatePredicate.java | 6 +- .../data/unicopia/state_maps/ice.json | 10 +++- 4 files changed, 64 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/block/state/JsonReversableBlockStateConverter.java b/src/main/java/com/minelittlepony/unicopia/block/state/JsonReversableBlockStateConverter.java index 86f3ecf9..84afbb6e 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/state/JsonReversableBlockStateConverter.java +++ b/src/main/java/com/minelittlepony/unicopia/block/state/JsonReversableBlockStateConverter.java @@ -21,7 +21,7 @@ public class JsonReversableBlockStateConverter implements ReversableBlockStateCo public JsonReversableBlockStateConverter(JsonElement json) { this(new ArrayList<>(), null); - JsonHelper.getArray(json.getAsJsonObject(), "entries").forEach(entry -> { + json.getAsJsonArray().forEach(entry -> { entries.add(Entry.of(entry.getAsJsonObject(), true)); }); } diff --git a/src/main/java/com/minelittlepony/unicopia/block/state/StateMapLoader.java b/src/main/java/com/minelittlepony/unicopia/block/state/StateMapLoader.java index d891d546..e47cfc4d 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/state/StateMapLoader.java +++ b/src/main/java/com/minelittlepony/unicopia/block/state/StateMapLoader.java @@ -1,20 +1,24 @@ package com.minelittlepony.unicopia.block.state; +import java.io.*; import java.util.*; import java.util.stream.Collectors; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; -import com.google.gson.JsonElement; +import com.google.common.collect.Maps; +import com.google.gson.*; import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.util.Resources; +import com.mojang.logging.LogUtils; import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener; import net.minecraft.block.BlockState; -import net.minecraft.resource.JsonDataLoader; -import net.minecraft.resource.ResourceManager; +import net.minecraft.resource.*; import net.minecraft.util.Identifier; +import net.minecraft.util.JsonHelper; import net.minecraft.util.profiler.Profiler; import net.minecraft.world.World; @@ -23,6 +27,11 @@ public class StateMapLoader extends JsonDataLoader implements IdentifiableResour public static final StateMapLoader INSTANCE = new StateMapLoader(); + private static final Logger LOGGER = LogUtils.getLogger(); + private static final String FILE_SUFFIX = ".json"; + private static final int FILE_SUFFIX_LENGTH = ".json".length(); + private static final String DATA_TYPE = "state_maps"; + private Map converters = new HashMap<>(); public StateMapLoader() { @@ -34,6 +43,46 @@ public class StateMapLoader extends JsonDataLoader implements IdentifiableResour return ID; } + @Override + protected Map prepare(ResourceManager resourceManager, Profiler profiler) { + Map map = Maps.newHashMap(); + int i = DATA_TYPE.length() + 1; + + resourceManager.findAllResources(DATA_TYPE, id -> id.getPath().endsWith(FILE_SUFFIX)).entrySet().stream().forEach(entry -> { + Identifier resId = entry.getKey(); + String path = resId.getPath(); + Identifier id = new Identifier(resId.getNamespace(), path.substring(i, path.length() - FILE_SUFFIX_LENGTH)); + + JsonArray entries = new JsonArray(); + for (var resource : entry.getValue()) { + try (BufferedReader reader = resource.getReader()) { + JsonObject json = JsonHelper.deserialize(Resources.GSON, reader, JsonObject.class); + + if (json != null) { + if (json.has("entries")) { + + JsonArray incoming = JsonHelper.getArray(json, "entries"); + if (json.has("replace") && json.get("replace").getAsBoolean()) { + entries = incoming; + } else { + entries.addAll(incoming); + } + } + + continue; + } + + LOGGER.error("Couldn't load data file {} from {} as it's null or empty", id, resId); + } catch (JsonParseException | IOException | IllegalArgumentException e) { + LOGGER.error("Couldn't parse data file {} from {}", id, resId, e); + } + } + + map.put(id, entries); + }); + return map; + } + @Override protected void apply(Map data, ResourceManager manager, Profiler profiler) { converters = data.entrySet().stream().collect(Collectors.toMap( diff --git a/src/main/java/com/minelittlepony/unicopia/block/state/StatePredicate.java b/src/main/java/com/minelittlepony/unicopia/block/state/StatePredicate.java index 24f080fb..76f5b923 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/state/StatePredicate.java +++ b/src/main/java/com/minelittlepony/unicopia/block/state/StatePredicate.java @@ -110,7 +110,7 @@ public abstract class StatePredicate implements Predicate { } } - static boolean isPlant(BlockState s) { + public static boolean isPlant(BlockState s) { return s.getBlock() instanceof PlantBlock; } @@ -119,11 +119,11 @@ public abstract class StatePredicate implements Predicate { } public static Predicate ofState(String state) { - Identifier id = new Identifier(state.split("{")[0]); + Identifier id = new Identifier(state.split("\\{")[0]); List properties = Optional.of(state) .filter(s -> s.contains("{")) .stream() - .flatMap(s -> Stream.of(s.split("{")[1].split("}")[0].split(","))) + .flatMap(s -> Stream.of(s.split("\\{")[1].split("\\}")[0].split(","))) .map(PropertyOp::of) .filter(Optional::isPresent) .map(Optional::get) diff --git a/src/main/resources/data/unicopia/state_maps/ice.json b/src/main/resources/data/unicopia/state_maps/ice.json index 04ec142c..4f72deb6 100644 --- a/src/main/resources/data/unicopia/state_maps/ice.json +++ b/src/main/resources/data/unicopia/state_maps/ice.json @@ -1,9 +1,15 @@ { - "parent": "unicopia:snow_piled", "replace": false, "entries": [ { - "match": { "state": "minecraft:water" }, + "match": { "state": "minecraft:snow{layers<7}" }, + "apply": { + "action": "unicopia:cycle_property", + "property": "layers" + } + }, + { + "match": { "tag": "minecraft:water" }, "apply": { "action": "unicopia:set_state", "state": "minecraft:frosted_ice"