Skip to content

Commit

Permalink
Remove most of the model injection code (#165)
Browse files Browse the repository at this point in the history
* new raw ore textures i got made, feel free to reorder/remove as you please

* fix-a-mixin (removed most of the ModelBakeryMixin code, and moved it to the respective renderer classes as suppliers)
also fixed create fluid tank rendering on forge

* revert to the old raw ore block for dull, this one is too dark

---------

Co-authored-by: KilaBash <[email protected]>
  • Loading branch information
screret and Yefancy authored Jul 15, 2023
1 parent eb849b1 commit f1efd37
Show file tree
Hide file tree
Showing 26 changed files with 99 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1084,17 +1084,6 @@ private void verifyInfo(MaterialProperties p, boolean averageRGB) {
}
}

// Verify FluidTexture
if (p.hasProperty(PropertyKey.FLUID)) {
var fluid = p.getProperty(PropertyKey.FLUID);
if (fluid.getStillTexture() == null) {
fluid.setStillTexture(MaterialIconType.fluid.getBlockTexturePath(iconSet, true));
}
if (fluid.getFlowTexture() == null) {
fluid.setFlowTexture(fluid.getStillTexture());
}
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.fabricmc.api.Environment;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.client.color.item.ItemColor;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -49,4 +50,13 @@ public IRenderer getRenderer(ItemStack stack) {
return getBlock().getRenderer(getBlock().defaultBlockState());
}

@Override
public Component getDescription() {
return this.getBlock().getName();
}

@Override
public Component getName(ItemStack stack) {
return getDescription();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ public static MaterialBlockRenderer getOrCreate(MaterialIconType type, MaterialI
}

protected MaterialBlockRenderer(MaterialIconType type, MaterialIconSet iconSet) {
super(GTCEu.id("block/tinted_cube_all"), Map.of("all", type.getBlockTexturePath(iconSet, true)));
}

public void setBlockTexture(ResourceLocation newBlockTexture) {
setTextureOverride(Map.of("all", newBlockTexture));
super(GTCEu.id("block/tinted_cube_all"), () -> Map.of("all", type.getBlockTexturePath(iconSet, true)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,19 @@
*/
public class OreBlockRenderer extends BlockStateRenderer {
private final Supplier<BlockState> stone;
private Supplier<ResourceLocation> overlaySupplier;
private ResourceLocation overlay;
private final boolean emissive;

public OreBlockRenderer(Supplier<BlockState> stone, ResourceLocation overlay, boolean emissive) {
public OreBlockRenderer(Supplier<BlockState> stone, Supplier<ResourceLocation> overlaySupplier, boolean emissive) {
this.stone = stone;
this.overlay = overlay;
this.overlaySupplier = overlaySupplier;
this.emissive = emissive;
if (LDLib.isClient()) {
registerEvent();
}
}

public void setOverlayTexture(ResourceLocation newOverlay) {
this.overlay = newOverlay;
}

@Override
public BlockInfo getBlockInfo() {
return new BlockInfo(stone.get());
Expand Down Expand Up @@ -95,6 +92,9 @@ public List<BakedQuad> renderModel(BlockAndTintGetter level, BlockPos pos, Block
public void onPrepareTextureAtlas(ResourceLocation atlasName, Consumer<ResourceLocation> register) {
super.onPrepareTextureAtlas(atlasName, register);
if (atlasName.equals(TextureAtlas.LOCATION_BLOCKS)) {
if (overlaySupplier != null) {
overlay = overlaySupplier.get();
}
register.accept(overlay);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Collections;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
* @author KilaBash
Expand All @@ -30,6 +31,8 @@ public class TextureOverrideRenderer extends CTMModelRenderer {

@Nonnull
protected Map<String, ResourceLocation> override;
@Nullable
protected Supplier<Map<String, ResourceLocation>> overrideSupplier;

public TextureOverrideRenderer(ResourceLocation model, @Nonnull Map<String, ResourceLocation> override) {
super(model);
Expand All @@ -39,6 +42,15 @@ public TextureOverrideRenderer(ResourceLocation model, @Nonnull Map<String, Reso
}
}

public TextureOverrideRenderer(ResourceLocation model, @Nonnull Supplier<Map<String, ResourceLocation>> overrideSupplier) {
super(model);
this.override = Collections.emptyMap();
this.overrideSupplier = overrideSupplier;
if (LDLib.isClient()) {
registerEvent();
}
}

public TextureOverrideRenderer(ResourceLocation model) {
super(model);
this.override = Collections.emptyMap();
Expand Down Expand Up @@ -83,6 +95,7 @@ public BakedModel getRotatedModel(Direction frontFacing) {
public void onPrepareTextureAtlas(ResourceLocation atlasName, Consumer<ResourceLocation> register) {
super.onPrepareTextureAtlas(atlasName, register);
if (atlasName.equals(TextureAtlas.LOCATION_BLOCKS)) { // prepare for override.
if (overrideSupplier != null) override = overrideSupplier.get();
for (Object value : override.values()) {
register.accept(new ResourceLocation(value.toString()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import com.google.common.collect.Table;
import com.google.common.collect.Tables;
import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.data.chemical.material.info.MaterialIconSet;
import com.gregtechceu.gtceu.api.data.chemical.material.info.MaterialIconType;
import com.lowdragmc.lowdraglib.client.model.ModelFactory;
import com.lowdragmc.lowdraglib.client.renderer.impl.IModelRenderer;
import lombok.Setter;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.renderer.block.model.BlockModel;
Expand All @@ -16,9 +14,9 @@
import net.minecraft.resources.ResourceLocation;

import javax.annotation.Nullable;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
* @author KilaBash
Expand All @@ -28,16 +26,14 @@
public class TagPrefixItemRenderer extends IModelRenderer {
private static final Table<MaterialIconType, MaterialIconSet, TagPrefixItemRenderer> MODELS = Tables.newCustomTable(new HashMap<>(), HashMap::new);

@Nullable
private ResourceLocation modelLocation;
@Nullable
private Supplier<ResourceLocation> modelLocationSupplier;

private TagPrefixItemRenderer(MaterialIconType type, MaterialIconSet iconSet) {
super(type.getItemModelPath(iconSet, true));
this.modelLocation = type.getItemModelPath(iconSet, true);
}

public void setModelLocation(ResourceLocation newModelLocation) {
this.modelLocation = newModelLocation;
this.itemModel = null;
super(null);
this.modelLocationSupplier = () -> type.getItemModelPath(iconSet, true);
}

public static TagPrefixItemRenderer getOrCreate(MaterialIconType type, MaterialIconSet iconSet) {
Expand Down Expand Up @@ -85,6 +81,9 @@ public BakedModel getRotatedModel(Direction frontFacing) {
@Override
@Environment(EnvType.CLIENT)
public void onAdditionalModel(Consumer<ResourceLocation> registry) {
// no-op, handled in ModelBakeryMixin.java
if (modelLocationSupplier != null) {
modelLocation = modelLocationSupplier.get();
}
registry.accept(modelLocation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public static void generateMaterialBlocks() {
var entry = REGISTRATE.block("%s%s_ore".formatted(oreTag != TagPrefix.ore ? FormattingUtil.toLowerCaseUnder(oreTag.name) + "_" : "", material.getName()),
oreType.material(),
properties -> new MaterialBlock(properties, oreTag, material, Platform.isClient() ? new OreBlockRenderer(oreType.stoneType(),
Objects.requireNonNull(oreTag.materialIconType()).getBlockTexturePath(material.getMaterialIconSet(), true),
() -> Objects.requireNonNull(oreTag.materialIconType()).getBlockTexturePath(material.getMaterialIconSet(), true),
oreProperty.isEmissive()) : null))
.initialProperties(() -> oreType.stoneType().get().getBlock())
.properties(properties -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,117 +1,41 @@
package com.gregtechceu.gtceu.core.mixins;

import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.block.MaterialBlock;
import com.google.common.collect.Sets;
import com.gregtechceu.gtceu.api.data.chemical.material.Material;
import com.gregtechceu.gtceu.api.data.chemical.material.info.MaterialFlags;
import com.gregtechceu.gtceu.api.data.chemical.material.info.MaterialIconSet;
import com.gregtechceu.gtceu.api.data.chemical.material.info.MaterialIconType;
import com.gregtechceu.gtceu.api.data.chemical.material.properties.FluidProperty;
import com.gregtechceu.gtceu.api.data.chemical.material.properties.PropertyKey;
import com.gregtechceu.gtceu.api.data.tag.TagPrefix;
import com.gregtechceu.gtceu.api.registry.GTRegistries;
import com.gregtechceu.gtceu.client.renderer.block.MaterialBlockRenderer;
import com.gregtechceu.gtceu.client.renderer.block.OreBlockRenderer;
import com.gregtechceu.gtceu.client.renderer.item.TagPrefixItemRenderer;
import com.gregtechceu.gtceu.common.data.GTBlocks;
import com.gregtechceu.gtceu.core.MixinHelpers;
import com.tterrag.registrate.util.entry.BlockEntry;
import net.minecraft.client.renderer.texture.TextureAtlas;
import com.mojang.datafixers.util.Pair;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.profiling.ProfilerFiller;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.Map;
import java.util.Set;
import java.util.LinkedHashSet;

@Mixin(value = ModelBakery.class, priority = 999)
public abstract class ModelBakeryMixin {

@Shadow
public abstract UnbakedModel getModel(ResourceLocation modelLocation);

@Shadow @Final
private Map<ResourceLocation, UnbakedModel> unbakedCache;

@Shadow @Final private Map<ResourceLocation, UnbakedModel> topLevelModels;

@Shadow @Final private ResourceManager resourceManager;

@Shadow @Final private static Set<net.minecraft.client.resources.model.Material> UNREFERENCED_TEXTURES;

/**
* register additional models as what forge does
*/
@Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/profiling/ProfilerFiller;popPush(Ljava/lang/String;)V", ordinal = 4))
private void gtceu$injectModelBakery(ProfilerFiller profiler, String name) { // Have to use a redirect here cuz it's to constructor and mixin doesn't like that much
@Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/Sets;newLinkedHashSet()Ljava/util/LinkedHashSet;", ordinal = 0))
private LinkedHashSet<Pair<String, String>> gtceu$injectModelBakery() { // Have to use a redirect here cuz it's to constructor and mixin doesn't like that much
for (Material material : GTRegistries.MATERIALS.values()) {
MaterialIconSet iconSet = material.getMaterialIconSet();

{
if (material.hasProperty(PropertyKey.ORE) && !material.hasProperty(PropertyKey.GEM)) {
MaterialBlockRenderer.getOrCreate(MaterialIconType.rawOreBlock, iconSet).setBlockTexture(gtceu$generateBlockTexture(iconSet, MaterialIconType.rawOreBlock));
}
if (material.hasProperty(PropertyKey.INGOT) || material.hasProperty(PropertyKey.GEM) || material.hasFlag(MaterialFlags.FORCE_GENERATE_BLOCK)) {
MaterialBlockRenderer.getOrCreate(MaterialIconType.block, iconSet).setBlockTexture(gtceu$generateBlockTexture(iconSet, MaterialIconType.block));
}
if (material.hasProperty(PropertyKey.DUST) && material.hasFlag(MaterialFlags.GENERATE_FRAME)) {
MaterialBlockRenderer.getOrCreate(MaterialIconType.frameGt, iconSet).setBlockTexture(gtceu$generateBlockTexture(iconSet, MaterialIconType.frameGt));
if (material.hasProperty(PropertyKey.FLUID)) {
FluidProperty fluid = material.getProperty(PropertyKey.FLUID);
if (fluid.getStillTexture() == null) {
ResourceLocation foundTexture = MaterialIconType.fluid.getBlockTexturePath(iconSet, false);
fluid.setStillTexture(foundTexture);
}
if (material.hasProperty(PropertyKey.FLUID)) {
FluidProperty prop = material.getProperty(PropertyKey.FLUID);
prop.setStillTexture(gtceu$generateBlockTexture(iconSet, MaterialIconType.fluid));
prop.setFlowTexture(prop.getStillTexture());
MaterialBlockRenderer.getOrCreate(MaterialIconType.fluid, iconSet).setBlockTexture(prop.getStillTexture());
MixinHelpers.addFluidTexture(material, prop);
}
}

prefixLoop:
for (TagPrefix tagPrefix : TagPrefix.values()) {
MaterialIconType type = tagPrefix.materialIconType();

if (material.hasProperty(PropertyKey.ORE)) {
BlockEntry<? extends MaterialBlock> blockEntry = GTBlocks.MATERIAL_BLOCKS.get(tagPrefix, material);
if (blockEntry != null && blockEntry.isPresent()) {
MaterialBlock block = blockEntry.get();
if (block.getRenderer(block.defaultBlockState()) instanceof OreBlockRenderer oreRenderer) {
oreRenderer.setOverlayTexture(gtceu$generateBlockTexture(iconSet, type));
}
continue prefixLoop;
}
}

if (tagPrefix.doGenerateItem(material)) {
ResourceLocation model = GTCEu.id(String.format("item/material_sets/%s/%s", iconSet.name, type.name()));
ResourceLocation foundModel = type.getItemModelPath(iconSet, false);

UnbakedModel unbakedmodel = this.getModel(foundModel);
this.unbakedCache.put(model, unbakedmodel);
this.topLevelModels.put(model, unbakedmodel);
TagPrefixItemRenderer.getOrCreate(type, iconSet).setModelLocation(model);
if (fluid.getFlowTexture() == null) {
fluid.setFlowTexture(fluid.getStillTexture());
}
MixinHelpers.addFluidTexture(material, fluid);
}
}
profiler.popPush(name);
}

@Unique
private ResourceLocation gtceu$generateBlockTexture(MaterialIconSet iconSet, MaterialIconType type) {
ResourceLocation texture = GTCEu.id(String.format("block/material_sets/%s/%s", iconSet.name, type.name()));
ResourceLocation foundTexture = type.getBlockTexturePath(iconSet, false);

ResourceLocation path = GTCEu.id(String.format("textures/block/material_sets/%s/%s.png", iconSet.name, type.name()));
foundTexture = this.resourceManager.getResource(path).isPresent() ? texture : foundTexture;

UNREFERENCED_TEXTURES.add(new net.minecraft.client.resources.model.Material(TextureAtlas.LOCATION_BLOCKS, foundTexture));
return foundTexture;
return Sets.newLinkedHashSet();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "gtceu:item/material_sets/diamond/raw_ore"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "gtceu:item/material_sets/fine/raw_ore"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "gtceu:item/material_sets/lignite/raw_ore"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "gtceu:item/material_sets/shiny/raw_ore"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ protected T createEntry() {
protected void registerDefaultRenderer(T flowing) {
FluidRenderHandlerRegistry.INSTANCE.register(getSource(), flowing, new SimpleFluidRenderHandler(stillTexture, flowingTexture, color));
ClientSpriteRegistryCallback.event(InventoryMenu.BLOCK_ATLAS).register((atlas, registry) -> {
registry.register(stillTexture);
registry.register(flowingTexture);
if (stillTexture != null) registry.register(stillTexture);
if (flowingTexture != null) registry.register(flowingTexture);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.gregtechceu.gtceu.common.data.GTFluids;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandlerRegistry;
import net.fabricmc.fabric.api.client.render.fluid.v1.SimpleFluidRenderHandler;
import net.fabricmc.fabric.api.event.client.ClientSpriteRegistryCallback;
import net.minecraft.world.inventory.InventoryMenu;

public class MixinHelpersImpl {

Expand All @@ -13,6 +15,10 @@ public static void addFluidTexture(Material material, FluidProperty prop) {
var fluids = GTFluids.MATERIAL_FLUID_FLOWING.get(prop);
if (fluids != null) {
FluidRenderHandlerRegistry.INSTANCE.register(fluids.get().getFirst(), fluids.get().getSecond(), new SimpleFluidRenderHandler(prop.getStillTexture(), prop.getFlowTexture(), material.getMaterialRGB()));
ClientSpriteRegistryCallback.event(InventoryMenu.BLOCK_ATLAS).register((atlas, registry) -> {
registry.register(prop.getStillTexture());
registry.register(prop.getFlowTexture());
});
}
}
}
Loading

0 comments on commit f1efd37

Please sign in to comment.