mirror of
https://github.com/GTNewHorizons/Mantle.git
synced 2026-11-24 05:44:46 +00:00
Add new fluid texture model
Allows resource packs to control fluid textures, provide the fluid uses TexturedFluidAttributes
This commit is contained in:
1 parent
6f3db08cdb
commit
e77198e67b
3 files changed
+267
No files matched your search
@@ -17,6 +17,7 @@ import slimeknights.mantle.client.book.BookLoader;
|
||||
import slimeknights.mantle.client.model.FallbackModelLoader;
|
||||
import slimeknights.mantle.client.model.RetexturedModel;
|
||||
import slimeknights.mantle.client.model.connected.ConnectedModel;
|
||||
import slimeknights.mantle.client.model.fluid.FluidTextureModel;
|
||||
import slimeknights.mantle.client.model.fluid.FluidsModel;
|
||||
import slimeknights.mantle.client.model.inventory.InventoryModel;
|
||||
import slimeknights.mantle.client.model.util.ModelHelper;
|
||||
@@ -40,6 +41,7 @@ public class ClientEvents {
|
||||
ModelLoaderRegistry.registerLoader(Mantle.getResource("connected"), ConnectedModel.Loader.INSTANCE);
|
||||
ModelLoaderRegistry.registerLoader(Mantle.getResource("fluids"), FluidsModel.Loader.INSTANCE);
|
||||
ModelLoaderRegistry.registerLoader(Mantle.getResource("retextured"), RetexturedModel.Loader.INSTANCE);
|
||||
ModelLoaderRegistry.registerLoader(Mantle.getResource("fluid_texture"), FluidTextureModel.LOADER);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
package slimeknights.mantle.client.model.fluid;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.minecraft.client.renderer.model.IBakedModel;
|
||||
import net.minecraft.client.renderer.model.IModelTransform;
|
||||
import net.minecraft.client.renderer.model.IUnbakedModel;
|
||||
import net.minecraft.client.renderer.model.ItemOverrideList;
|
||||
import net.minecraft.client.renderer.model.ModelBakery;
|
||||
import net.minecraft.client.renderer.model.RenderMaterial;
|
||||
import net.minecraft.client.renderer.model.SimpleBakedModel;
|
||||
import net.minecraft.client.renderer.texture.MissingTextureSprite;
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.fluid.Fluid;
|
||||
import net.minecraft.resources.IResourceManager;
|
||||
import net.minecraft.util.JSONUtils;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.client.model.BakedModelWrapper;
|
||||
import net.minecraftforge.client.model.IModelConfiguration;
|
||||
import net.minecraftforge.client.model.IModelLoader;
|
||||
import net.minecraftforge.client.model.geometry.IModelGeometry;
|
||||
import slimeknights.mantle.client.model.util.ModelHelper;
|
||||
import slimeknights.mantle.registration.ModelFluidAttributes;
|
||||
import slimeknights.mantle.registration.ModelFluidAttributes.IFluidModelProvider;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
/** Fluid model that allows a resource pack to control the textures of a block. Use alongside {@link ModelFluidAttributes} */
|
||||
@RequiredArgsConstructor
|
||||
public class FluidTextureModel implements IModelGeometry<FluidTextureModel> {
|
||||
public static Loader LOADER = new Loader();
|
||||
|
||||
private final int color;
|
||||
|
||||
/** Checks if a texture is missing */
|
||||
private static boolean isMissing(RenderMaterial material) {
|
||||
return MissingTextureSprite.getLocation().equals(material.getTextureLocation());
|
||||
}
|
||||
|
||||
/** Gets the texture, or null if missing */
|
||||
private static void getTexture(IModelConfiguration owner, String name, Collection<RenderMaterial> textures, Set<Pair<String,String>> missingTextureErrors) {
|
||||
RenderMaterial material = owner.resolveTexture(name);
|
||||
if (isMissing(material)) {
|
||||
missingTextureErrors.add(Pair.of(name, owner.getModelName()));
|
||||
}
|
||||
textures.add(material);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<RenderMaterial> getTextures(IModelConfiguration owner, Function<ResourceLocation,IUnbakedModel> modelGetter, Set<Pair<String,String>> missingTextureErrors) {
|
||||
Set<RenderMaterial> textures = new HashSet<>();
|
||||
getTexture(owner, "still", textures, missingTextureErrors);
|
||||
getTexture(owner, "flowing", textures, missingTextureErrors);
|
||||
RenderMaterial overlay = owner.resolveTexture("overlay");
|
||||
if (!isMissing(overlay)) {
|
||||
textures.add(overlay);
|
||||
}
|
||||
return textures;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBakedModel bake(IModelConfiguration owner, ModelBakery bakery, Function<RenderMaterial,TextureAtlasSprite> spriteGetter, IModelTransform modelTransform, ItemOverrideList overrides, ResourceLocation modelLocation) {
|
||||
RenderMaterial still = owner.resolveTexture("still");
|
||||
RenderMaterial flowing = owner.resolveTexture("flowing");
|
||||
RenderMaterial overlay = owner.resolveTexture("overlay");
|
||||
ResourceLocation overlayLocation = isMissing(overlay) ? null : overlay.getTextureLocation();
|
||||
IBakedModel baked = new SimpleBakedModel.Builder(owner, overrides).setTexture(spriteGetter.apply(still)).build();
|
||||
return new BakedModel(baked, still.getTextureLocation(), flowing.getTextureLocation(), overlayLocation, color);
|
||||
}
|
||||
|
||||
/** Data holder class, has no quads */
|
||||
private static class BakedModel extends BakedModelWrapper<IBakedModel> {
|
||||
@Getter
|
||||
private final ResourceLocation still;
|
||||
@Getter
|
||||
private final ResourceLocation flowing;
|
||||
@Getter
|
||||
private final ResourceLocation overlay;
|
||||
@Getter
|
||||
private final int color;
|
||||
public BakedModel(IBakedModel originalModel, ResourceLocation still, ResourceLocation flowing, @Nullable ResourceLocation overlay, int color) {
|
||||
super(originalModel);
|
||||
this.still = still;
|
||||
this.flowing = flowing;
|
||||
this.overlay = overlay;
|
||||
this.color = color;
|
||||
}
|
||||
}
|
||||
|
||||
/** Model loader, also doubles as the fluid model provider */
|
||||
private static class Loader implements IModelLoader<FluidTextureModel>, IFluidModelProvider {
|
||||
private final Map<Fluid,BakedModel> modelCache = new HashMap<>();
|
||||
|
||||
/** Gets a model for a fluid */
|
||||
@Nullable
|
||||
private BakedModel getFluidModel(Fluid fluid) {
|
||||
return ModelHelper.getBakedModel(fluid.getDefaultState().getBlockState(), BakedModel.class);
|
||||
}
|
||||
|
||||
/** Gets a model for a fluid from the cache */
|
||||
@Nullable
|
||||
private BakedModel getCachedModel(Fluid fluid) {
|
||||
return modelCache.computeIfAbsent(fluid, this::getFluidModel);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public ResourceLocation getStillTexture(Fluid fluid) {
|
||||
BakedModel model = getCachedModel(fluid);
|
||||
return model == null ? null : model.getStill();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public ResourceLocation getFlowingTexture(Fluid fluid) {
|
||||
BakedModel model = getCachedModel(fluid);
|
||||
return model == null ? null : model.getFlowing();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public ResourceLocation getOverlayTexture(Fluid fluid) {
|
||||
BakedModel model = getCachedModel(fluid);
|
||||
return model == null ? null : model.getOverlay();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColor(Fluid fluid) {
|
||||
BakedModel model = getCachedModel(fluid);
|
||||
return model == null ? -1 : model.getColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResourceManagerReload(IResourceManager resourceManager) {
|
||||
modelCache.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidTextureModel read(JsonDeserializationContext deserializationContext, JsonObject modelContents) {
|
||||
int color = -1;
|
||||
if (modelContents.has("color")) {
|
||||
String colorString = JSONUtils.getString(modelContents, "color");
|
||||
int length = colorString.length();
|
||||
// prevent some invalid strings, colors should all be 6 or 8 digits
|
||||
if (colorString.charAt(0) == '-' || (length != 6 && length != 8)) {
|
||||
throw new JsonSyntaxException("Invalid color '" + colorString + "'");
|
||||
}
|
||||
try {
|
||||
color = (int)Long.parseLong(colorString, 16);
|
||||
// for 6 length, make fully opaque
|
||||
if (length == 6) {
|
||||
color |= 0xFF000000;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
throw new JsonSyntaxException("Invalid color '" + colorString + "'");
|
||||
}
|
||||
}
|
||||
return new FluidTextureModel(color);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package slimeknights.mantle.registration;
|
||||
|
||||
import net.minecraft.fluid.Fluid;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.fluids.FluidAttributes;
|
||||
import net.minecraftforge.fml.DistExecutor;
|
||||
import slimeknights.mantle.client.model.fluid.FluidTextureModel;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
/** Extension of fluid attributes that moves the fluid textures and color to the resource pack via {@link FluidTextureModel} (registered as loader <code>mantle:fluid_texture</code> for the fluid block ) */
|
||||
public class ModelFluidAttributes extends FluidAttributes {
|
||||
public static final IFluidModelProvider MODEL_PROVIDER = DistExecutor.unsafeRunForDist(() -> () -> FluidTextureModel.LOADER, () -> () -> IFluidModelProvider.EMPTY);
|
||||
|
||||
/** Creates a new builder */
|
||||
public static FluidAttributes.Builder builder() {
|
||||
return new Builder(ModelFluidAttributes::new);
|
||||
}
|
||||
|
||||
private final Fluid fluid;
|
||||
protected ModelFluidAttributes(FluidAttributes.Builder builder, Fluid fluid) {
|
||||
super(builder, fluid);
|
||||
this.fluid = fluid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation getStillTexture() {
|
||||
ResourceLocation texture = MODEL_PROVIDER.getStillTexture(fluid);
|
||||
if (texture == null) {
|
||||
return super.getStillTexture();
|
||||
}
|
||||
return texture;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation getFlowingTexture() {
|
||||
ResourceLocation texture = MODEL_PROVIDER.getFlowingTexture(fluid);
|
||||
if (texture == null) {
|
||||
return super.getFlowingTexture();
|
||||
}
|
||||
return texture;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public ResourceLocation getOverlayTexture() {
|
||||
return MODEL_PROVIDER.getOverlayTexture(fluid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColor() {
|
||||
return MODEL_PROVIDER.getColor(fluid);
|
||||
}
|
||||
|
||||
/** Extension of {@link FluidAttributes.Builder} redirecting fluid textures and colors to the resource pack */
|
||||
public static class Builder extends FluidAttributes.Builder {
|
||||
private static final ResourceLocation FALLBACK_STILL = new ResourceLocation("block/water_still");
|
||||
private static final ResourceLocation FALLBACK_FLOWING = new ResourceLocation("block/water_flow");
|
||||
|
||||
protected Builder(BiFunction<FluidAttributes.Builder,Fluid,FluidAttributes> factory) {
|
||||
super(FALLBACK_STILL, FALLBACK_FLOWING, factory);
|
||||
}
|
||||
}
|
||||
|
||||
/** Interface to proxy the fluid model provider */
|
||||
public interface IFluidModelProvider {
|
||||
/** Serverside instance of the model provider, does nothing */
|
||||
IFluidModelProvider EMPTY = new IFluidModelProvider() {};
|
||||
|
||||
/** Gets the still texture for a fluid */
|
||||
@Nullable
|
||||
default ResourceLocation getStillTexture(Fluid fluid) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Gets the flowing texture for a fluid */
|
||||
@Nullable
|
||||
default ResourceLocation getFlowingTexture(Fluid fluid) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Gets the overlay texture for a fluid */
|
||||
@Nullable
|
||||
default ResourceLocation getOverlayTexture(Fluid fluid) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Gets the color for a fluid */
|
||||
default int getColor(Fluid fluid) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user