-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(registry): metatag datagen provider
- Loading branch information
1 parent
e283669
commit 9015731
Showing
4 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...egistry/metatag/data/MetatagResource.java → ...egistry/metatag/data/MetatagResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...src/main/java/dev/spiritstudios/specter/api/registry/metatag/datagen/MetatagProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package dev.spiritstudios.specter.api.registry.metatag.datagen; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.mojang.datafixers.util.Pair; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.DynamicOps; | ||
import com.mojang.serialization.JsonOps; | ||
import dev.spiritstudios.specter.api.registry.metatag.Metatag; | ||
import dev.spiritstudios.specter.api.registry.metatag.data.MetatagResource; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; | ||
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput; | ||
import net.minecraft.data.DataOutput; | ||
import net.minecraft.data.DataProvider; | ||
import net.minecraft.data.DataWriter; | ||
import net.minecraft.registry.RegistryOps; | ||
import net.minecraft.registry.RegistryWrapper; | ||
import net.minecraft.util.Identifier; | ||
|
||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Consumer; | ||
|
||
public abstract class MetatagProvider<R> implements DataProvider { | ||
protected final CompletableFuture<RegistryWrapper.WrapperLookup> registriesFuture; | ||
protected final FabricDataOutput dataOutput; | ||
protected final DataOutput.OutputType outputType; | ||
|
||
protected MetatagProvider(FabricDataOutput dataOutput, CompletableFuture<RegistryWrapper.WrapperLookup> registriesFuture, DataOutput.OutputType outputType) { | ||
this.dataOutput = dataOutput; | ||
this.registriesFuture = registriesFuture; | ||
this.outputType = outputType; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<?> run(DataWriter writer) { | ||
return this.registriesFuture.thenCompose(lookup -> { | ||
DataOutput.PathResolver pathResolver = dataOutput.getResolver(outputType, "metatags"); | ||
DynamicOps<JsonElement> ops = RegistryOps.of(JsonOps.INSTANCE, lookup); | ||
|
||
List<MetatagBuilder<R, ?>> metatags = new ArrayList<>(); | ||
configure(metatags::add, lookup); | ||
|
||
return CompletableFuture.allOf(metatags.stream() | ||
.map(builder -> generate(builder, pathResolver, writer, ops)) | ||
.toArray(CompletableFuture[]::new)); | ||
}); | ||
} | ||
|
||
private <V> CompletableFuture<?> generate(MetatagBuilder<R, V> builder, DataOutput.PathResolver pathResolver, DataWriter writer, DynamicOps<JsonElement> ops) { | ||
return CompletableFuture.supplyAsync(() -> { | ||
Codec<MetatagResource<V>> resourceCodec = MetatagResource.resourceCodecOf(builder.metatag); | ||
return resourceCodec.encodeStart(ops, builder.build()) | ||
.getOrThrow(error -> { | ||
throw new IllegalStateException("Failed to encode metatag resource: " + error); | ||
}); | ||
}).thenComposeAsync(encoded -> { | ||
Identifier registryId = builder.metatag.getRegistry().getKey().getValue(); | ||
Path metatagPath = pathResolver.resolveJson(builder.metatag.getId().withPrefixedPath(registryId.getNamespace() + "/" + registryId.getPath() + "/")); | ||
|
||
return DataProvider.writeToPath(writer, encoded, metatagPath); | ||
}); | ||
} | ||
|
||
protected abstract void configure(Consumer<MetatagBuilder<R, ?>> provider, RegistryWrapper.WrapperLookup lookup); | ||
|
||
protected final <V> MetatagBuilder<R, V> create(Metatag<R, V> metatag) { | ||
return new MetatagBuilder<>(metatag, false); | ||
} | ||
|
||
protected final <V> MetatagBuilder<R, V> create(Metatag<R, V> metatag, boolean replace) { | ||
return new MetatagBuilder<>(metatag, replace); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "Metatags"; | ||
} | ||
|
||
protected static final class MetatagBuilder<R, V> { | ||
private final Metatag<R, V> metatag; | ||
private final Map<R, V> values = new Object2ObjectOpenHashMap<>(); | ||
private final boolean replace; | ||
|
||
private MetatagBuilder(Metatag<R, V> metatag, boolean replace) { | ||
this.metatag = metatag; | ||
this.replace = replace; | ||
} | ||
|
||
public MetatagBuilder<R, V> put(R value, V metatagValue) { | ||
this.values.put(value, metatagValue); | ||
return this; | ||
} | ||
|
||
private MetatagResource<V> build() { | ||
return new MetatagResource<>( | ||
replace, | ||
values.entrySet().stream() | ||
.map(entry -> Pair.of(metatag.getRegistry().getId(entry.getKey()), entry.getValue())) | ||
.filter(pair -> Objects.nonNull(pair.getFirst())) | ||
.toList() | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters