|
23 | 23 | package dev.galacticraft.machinelib.client.api.model;
|
24 | 24 |
|
25 | 25 | import com.google.common.base.Preconditions;
|
26 |
| -import com.google.gson.JsonObject; |
27 |
| -import dev.galacticraft.machinelib.api.machine.MachineRenderData; |
28 |
| -import dev.galacticraft.machinelib.api.util.BlockFace; |
| 26 | +import com.mojang.serialization.Codec; |
| 27 | +import dev.galacticraft.machinelib.client.api.model.sprite.MachineTextureBase; |
| 28 | +import dev.galacticraft.machinelib.client.api.model.sprite.SingleTextureProvider; |
| 29 | +import dev.galacticraft.machinelib.client.api.model.sprite.TextureProvider; |
29 | 30 | import dev.galacticraft.machinelib.client.impl.model.MachineBakedModel;
|
30 |
| -import net.minecraft.client.renderer.texture.TextureAtlasSprite; |
31 |
| -import net.minecraft.client.resources.model.Material; |
| 31 | +import dev.galacticraft.machinelib.client.impl.model.MachineModelRegistryImpl; |
32 | 32 | import net.minecraft.resources.ResourceLocation;
|
33 |
| -import org.jetbrains.annotations.Contract; |
34 | 33 | import org.jetbrains.annotations.NotNull;
|
35 | 34 | import org.jetbrains.annotations.Nullable;
|
36 | 35 |
|
37 |
| -import java.util.HashMap; |
38 |
| -import java.util.Map; |
39 |
| -import java.util.function.Function; |
40 |
| - |
41 | 36 | /**
|
42 | 37 | * A registry for {@link MachineBakedModel} sprite providers.
|
43 | 38 | */
|
44 |
| -public interface MachineModelRegistry { |
45 |
| - String MARKER = "machinelib:generate"; |
46 |
| - Map<ResourceLocation, SpriteProviderFactory> FACTORIES = new HashMap<>(); |
| 39 | +public final class MachineModelRegistry { |
| 40 | + public static final String MARKER = "machinelib:generate"; |
47 | 41 |
|
48 | 42 | /**
|
49 | 43 | * Registers a sprite provider for a block.
|
50 | 44 | *
|
51 |
| - * @param id The id to register the provider for. |
52 |
| - * @param factory The provider to register. |
| 45 | + * @param id the id to register the provider for |
| 46 | + * @param codec the provider to register |
53 | 47 | */
|
54 |
| - static void register(@NotNull ResourceLocation id, @NotNull SpriteProviderFactory factory) { |
| 48 | + public static void register(@NotNull ResourceLocation id, @NotNull Codec<? extends TextureProvider<?>> codec) { |
55 | 49 | Preconditions.checkNotNull(id);
|
56 |
| - Preconditions.checkNotNull(factory); |
| 50 | + Preconditions.checkNotNull(codec); |
57 | 51 |
|
58 |
| - FACTORIES.put(id, factory); |
| 52 | + MachineModelRegistryImpl.FACTORIES.put(id, codec); |
| 53 | + } |
| 54 | + |
| 55 | + public static void registerBase(@NotNull ResourceLocation id, @NotNull MachineTextureBase bundle) { |
| 56 | + Preconditions.checkNotNull(id); |
| 57 | + Preconditions.checkNotNull(bundle); |
| 58 | + |
| 59 | + MachineModelRegistryImpl.TEXTURE_BASES.put(id, bundle); |
59 | 60 | }
|
60 | 61 |
|
61 | 62 | /**
|
62 | 63 | * {@return the registered provider, or null if none is registered}
|
63 | 64 | *
|
64 |
| - * @param providerId The provider id to get the provider for. |
| 65 | + * @param providerId the provider id to get the provider for |
65 | 66 | */
|
66 |
| - static @Nullable SpriteProviderFactory getProviderFactory(@NotNull ResourceLocation providerId) { |
67 |
| - return FACTORIES.get(providerId); |
68 |
| - } |
69 |
| - |
70 |
| - static @NotNull SpriteProviderFactory getProviderFactoryOrDefault(@NotNull ResourceLocation providerId) { |
71 |
| - return FACTORIES.getOrDefault(providerId, SpriteProviderFactory.DEFAULT); |
| 67 | + public static @Nullable Codec<? extends TextureProvider<?>> getProviderFactory(@NotNull ResourceLocation providerId) { |
| 68 | + return MachineModelRegistryImpl.FACTORIES.get(providerId); |
72 | 69 | }
|
73 | 70 |
|
74 |
| - @FunctionalInterface |
75 |
| - interface SpriteProviderFactory { |
76 |
| - SpriteProviderFactory DEFAULT = new SpriteProviderFactory() { |
77 |
| - @Contract(value = "_, _ -> new", pure = true) |
78 |
| - @Override |
79 |
| - public @NotNull SpriteProvider create(@NotNull JsonObject json, @NotNull Function<Material, TextureAtlasSprite> atlas) { |
80 |
| - return new SpriteProvider() { |
81 |
| - private final TextureAtlasSprite machineSide = atlas.apply(MachineBakedModel.MACHINE_SIDE); |
82 |
| - private final TextureAtlasSprite machine = atlas.apply(MachineBakedModel.MACHINE); |
83 |
| - |
84 |
| - @Override |
85 |
| - public @NotNull TextureAtlasSprite getSpritesForState(@Nullable MachineRenderData renderData, @NotNull BlockFace face) { |
86 |
| - if (face.side()) return this.machineSide; |
87 |
| - return this.machine; |
88 |
| - } |
89 |
| - }; |
90 |
| - } |
91 |
| - }; |
92 |
| - |
93 |
| - SpriteProvider create(@NotNull JsonObject json, @NotNull Function<Material, TextureAtlasSprite> atlas); |
94 |
| - } |
95 |
| - |
96 |
| - @FunctionalInterface |
97 |
| - interface SpriteProvider { |
98 |
| - /** |
99 |
| - * @param face The face that is being textured. |
100 |
| - * @return The appropriate sprite to render for the given face. |
101 |
| - */ |
102 |
| - @Contract(pure = true) |
103 |
| - @NotNull |
104 |
| - TextureAtlasSprite getSpritesForState(@Nullable MachineRenderData data, @NotNull BlockFace face); |
| 71 | + /** |
| 72 | + * {@return the registered provider, or {@link SingleTextureProvider#MISSING_CODEC} if none is registered} |
| 73 | + * |
| 74 | + * @param providerId the provider id to get the provider for |
| 75 | + */ |
| 76 | + public static @NotNull Codec<? extends TextureProvider<?>> getProviderFactoryOrDefault(@NotNull ResourceLocation providerId) { |
| 77 | + return MachineModelRegistryImpl.FACTORIES.getOrDefault(providerId, SingleTextureProvider.CODEC); |
105 | 78 | }
|
106 | 79 | }
|
0 commit comments