Move stuff of RegistryTrigger over to StringSerializableTrigger to make future expansion possible

This commit is contained in:
grimmauld 2021-02-19 15:08:50 +01:00
parent 589572df6c
commit d0357df863
4 changed files with 117 additions and 83 deletions

View file

@ -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

View file

@ -28,8 +28,8 @@
"trigger": "create:bracket_apply",
"conditions": {
"accepted_entries": [
"create:large_cogwheel",
"create:cogwheel"
"create:cogwheel",
"create:large_cogwheel"
]
}
},

View file

@ -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<T extends IForgeRegistryEntry<T>> extends CriterionTriggerBase<RegistryTrigger.Instance<T>> {
private final Class<T> registryType;
public class RegistryTrigger<T extends IForgeRegistryEntry<T>> extends StringSerializableTrigger<T> {
private final IForgeRegistry<T> registry;
public RegistryTrigger(String id, Class<T> registryType) {
super(id);
this.registryType = registryType;
}
@SafeVarargs
public final Instance<T> 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<ServerPlayerEntity, T> trigger = this::trigger;
return player -> trigger.accept(player, entry);
this.registry = RegistryManager.ACTIVE.getRegistry(registryType);
}
@Nullable
@Override
public Instance<T> deserializeInstance(JsonObject json, JsonDeserializationContext context) {
if (json.has("accepted_entries")) {
JsonArray elements = json.getAsJsonArray("accepted_entries");
IForgeRegistry<T> 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<T extends IForgeRegistryEntry<T>> extends CriterionTriggerBase.Instance {
@Nullable
private final Set<T> entries;
public Instance(ResourceLocation id, @Nullable Set<T> registryEntries) {
super(id);
entries = registryEntries;
}
@Override
protected boolean test(@Nullable List<Supplier<Object>> 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();
}
}

View file

@ -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<T> extends CriterionTriggerBase<StringSerializableTrigger.Instance<T>> {
public StringSerializableTrigger(String id) {
super(id);
}
@SafeVarargs
public final Instance<T> 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<ServerPlayerEntity, T> trigger = this::trigger;
return player -> trigger.accept(player, entry);
}
@Override
public Instance<T> 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<T> extends CriterionTriggerBase.Instance {
@Nullable
private final Set<T> entries;
private final StringSerializableTrigger<T> trigger;
public Instance(StringSerializableTrigger<T> trigger, @Nullable Set<T> entries) {
super(trigger.getId());
this.trigger = trigger;
this.entries = entries;
}
@Override
protected boolean test(@Nullable List<Supplier<Object>> 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;
}
}
}