From d0357df8631e35f3f8ca35db53f6d857bd1e8340 Mon Sep 17 00:00:00 2001 From: grimmauld Date: Fri, 19 Feb 2021 15:08:50 +0100 Subject: [PATCH] Move stuff of RegistryTrigger over to StringSerializableTrigger to make future expansion possible --- src/generated/resources/.cache/cache | 2 +- .../data/create/advancements/aesthetics.json | 4 +- .../advancement/RegistryTrigger.java | 91 ++-------------- .../StringSerializableTrigger.java | 103 ++++++++++++++++++ 4 files changed, 117 insertions(+), 83 deletions(-) create mode 100644 src/main/java/com/simibubi/create/foundation/advancement/StringSerializableTrigger.java diff --git a/src/generated/resources/.cache/cache b/src/generated/resources/.cache/cache index ecde36955..e1d242c23 100644 --- a/src/generated/resources/.cache/cache +++ b/src/generated/resources/.cache/cache @@ -1586,7 +1586,7 @@ d080b1b25e5bc8baf5aee68691b08c7f12ece3b0 assets/create/models/item/windmill_bear 9f9455ccb5fc9e3cbfce73862b46078346a522a5 assets/create/models/item/zinc_nugget.json b1689617190c05ef34bd18456b0c7ae09bb3210f assets/create/models/item/zinc_ore.json e76041b7ae829fdd7dc0524f6ca4d2f89fca51bb assets/create/sounds.json -5d0cc4c0255dc241e61c173b31ddca70c88d08e4 data/create/advancements/aesthetics.json +0f1b4b980afba9bf2caf583b88e261bba8b10313 data/create/advancements/aesthetics.json 187921fa131b06721bfaf63f2623a28c141aae9a data/create/advancements/andesite_alloy.json 0ea2db7173b5be28b289ea7c9a6a0cf5805c60c7 data/create/advancements/andesite_casing.json 356f4855a2a6c65be3fb51d7d1aabf2ca6034d42 data/create/advancements/arm_blaze_burner.json diff --git a/src/generated/resources/data/create/advancements/aesthetics.json b/src/generated/resources/data/create/advancements/aesthetics.json index 59a86f429..d723cbe38 100644 --- a/src/generated/resources/data/create/advancements/aesthetics.json +++ b/src/generated/resources/data/create/advancements/aesthetics.json @@ -28,8 +28,8 @@ "trigger": "create:bracket_apply", "conditions": { "accepted_entries": [ - "create:large_cogwheel", - "create:cogwheel" + "create:cogwheel", + "create:large_cogwheel" ] } }, diff --git a/src/main/java/com/simibubi/create/foundation/advancement/RegistryTrigger.java b/src/main/java/com/simibubi/create/foundation/advancement/RegistryTrigger.java index 468820b3c..0a70e87b0 100644 --- a/src/main/java/com/simibubi/create/foundation/advancement/RegistryTrigger.java +++ b/src/main/java/com/simibubi/create/foundation/advancement/RegistryTrigger.java @@ -1,9 +1,6 @@ package com.simibubi.create.foundation.advancement; -import com.google.common.collect.Sets; -import com.google.gson.*; import mcp.MethodsReturnNonnullByDefault; -import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.util.ResourceLocation; import net.minecraftforge.registries.IForgeRegistry; import net.minecraftforge.registries.IForgeRegistryEntry; @@ -11,94 +8,28 @@ import net.minecraftforge.registries.RegistryManager; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; -import java.util.Collections; -import java.util.List; -import java.util.Set; -import java.util.function.BiConsumer; -import java.util.function.Supplier; -import java.util.stream.Collectors; -import java.util.stream.StreamSupport; @ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault -public class RegistryTrigger> extends CriterionTriggerBase> { - private final Class registryType; +public class RegistryTrigger> extends StringSerializableTrigger { + private final IForgeRegistry registry; public RegistryTrigger(String id, Class registryType) { super(id); - this.registryType = registryType; - } - - @SafeVarargs - public final Instance forEntries(@Nullable T... entries) { - return new Instance<>(getId(), entries == null ? null : Sets.newHashSet(entries)); - } - - public void trigger(ServerPlayerEntity player, T registryEntry) { - trigger(player, Collections.singletonList(() -> registryEntry)); - } - - public ITriggerable constructTriggerFor(T entry) { - BiConsumer trigger = this::trigger; - return player -> trigger.accept(player, entry); + this.registry = RegistryManager.ACTIVE.getRegistry(registryType); } + @Nullable @Override - public Instance deserializeInstance(JsonObject json, JsonDeserializationContext context) { - if (json.has("accepted_entries")) { - JsonArray elements = json.getAsJsonArray("accepted_entries"); - IForgeRegistry registry = RegistryManager.ACTIVE.getRegistry(registryType); + protected T getValue(String key) { - return new Instance<>(getId(), - StreamSupport.stream(elements.spliterator(), false).map(JsonElement::getAsString).map(ResourceLocation::new) - .map(rl -> { - T entry = registry.getValue(rl); - if (entry == null) - throw new JsonSyntaxException("Unknown registry entry '" + rl + "'"); - return entry; - }).collect(Collectors.toSet())); - } - - return forEntries((T) null); + return registry.getValue(new ResourceLocation(key)); } - public static class Instance> extends CriterionTriggerBase.Instance { - - @Nullable - private final Set entries; - - public Instance(ResourceLocation id, @Nullable Set registryEntries) { - super(id); - entries = registryEntries; - } - - @Override - protected boolean test(@Nullable List> suppliers) { - if (entries == null || suppliers == null || suppliers.isEmpty()) - return false; - return entries.contains(suppliers.get(0).get()); - } - - @Override - public JsonElement serialize() { - JsonObject jsonobject = new JsonObject(); - JsonArray elements = new JsonArray(); - - if (entries == null) { - jsonobject.add("accepted_entries", elements); - return jsonobject; - } - - for (T entry : entries) { - if (entry == null) - continue; - ResourceLocation key = RegistryManager.ACTIVE.getRegistry(entry.getRegistryType()).getKey(entry); - if (key != null) - elements.add(key.toString()); - } - - jsonobject.add("accepted_entries", elements); - return jsonobject; - } + @Nullable + @Override + protected String getKey(T value) { + ResourceLocation key = registry.getKey(value); + return key == null ? null : key.toString(); } } diff --git a/src/main/java/com/simibubi/create/foundation/advancement/StringSerializableTrigger.java b/src/main/java/com/simibubi/create/foundation/advancement/StringSerializableTrigger.java new file mode 100644 index 000000000..21e823a63 --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/advancement/StringSerializableTrigger.java @@ -0,0 +1,103 @@ +package com.simibubi.create.foundation.advancement; + +import com.google.common.collect.Sets; +import com.google.gson.*; +import mcp.MethodsReturnNonnullByDefault; +import net.minecraft.entity.player.ServerPlayerEntity; + +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +@MethodsReturnNonnullByDefault +@ParametersAreNonnullByDefault +public abstract class StringSerializableTrigger extends CriterionTriggerBase> { + public StringSerializableTrigger(String id) { + super(id); + } + + @SafeVarargs + public final Instance forEntries(@Nullable T... entries) { + return new Instance<>(this, entries == null ? null : Sets.newHashSet(entries)); + } + + public void trigger(ServerPlayerEntity player, T registryEntry) { + trigger(player, Collections.singletonList(() -> registryEntry)); + } + + public ITriggerable constructTriggerFor(T entry) { + BiConsumer trigger = this::trigger; + return player -> trigger.accept(player, entry); + } + + @Override + public Instance deserializeInstance(JsonObject json, JsonDeserializationContext context) { + if (json.has("accepted_entries")) { + JsonArray elements = json.getAsJsonArray("accepted_entries"); + return new Instance<>(this, + StreamSupport.stream(elements.spliterator(), false).map(JsonElement::getAsString) + .map(rl -> { + T entry = getValue(rl); + if (entry == null) + throw new JsonSyntaxException("Unknown entry '" + rl + "'"); + return entry; + }).collect(Collectors.toSet())); + } + + return forEntries((T) null); + } + + @Nullable + abstract protected T getValue(String key); + + @Nullable + abstract protected String getKey(T value); + + public static class Instance extends CriterionTriggerBase.Instance { + + @Nullable + private final Set entries; + private final StringSerializableTrigger trigger; + + public Instance(StringSerializableTrigger trigger, @Nullable Set entries) { + super(trigger.getId()); + this.trigger = trigger; + this.entries = entries; + } + + @Override + protected boolean test(@Nullable List> suppliers) { + if (entries == null || suppliers == null || suppliers.isEmpty()) + return false; + return entries.contains(suppliers.get(0).get()); + } + + @Override + public JsonElement serialize() { + JsonObject jsonobject = new JsonObject(); + JsonArray elements = new JsonArray(); + + if (entries == null) { + jsonobject.add("accepted_entries", elements); + return jsonobject; + } + + for (T entry : entries) { + if (entry == null) + continue; + String key = trigger.getKey(entry); + if (key != null) + elements.add(key); + } + + jsonobject.add("accepted_entries", elements); + return jsonobject; + } + } +}