Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.20.1' into 1.20.1
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaMode committed Apr 23, 2024
2 parents 55d37a1 + 11dca8f commit 8d36ad3
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.github.fabricators_of_create.porting_lib.models;

import java.util.function.Supplier;

import net.fabricmc.fabric.api.renderer.v1.Renderer;
import net.fabricmc.fabric.api.renderer.v1.RendererAccess;
import net.fabricmc.fabric.api.renderer.v1.material.BlendMode;
import net.fabricmc.fabric.api.renderer.v1.material.MaterialFinder;
import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;
import net.fabricmc.fabric.api.renderer.v1.model.ForwardingBakedModel;
import net.fabricmc.fabric.api.renderer.v1.render.RenderContext;
import net.fabricmc.fabric.api.renderer.v1.render.RenderContext.QuadTransform;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.core.BlockPos;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.state.BlockState;

public class CustomBlendModeModel extends ForwardingBakedModel {
private final QuadTransform transform;


public CustomBlendModeModel(BakedModel owner, BlendMode blendMode) {
this.wrapped = owner;

Renderer renderer = RendererAccess.INSTANCE.getRenderer();
MaterialFinder finder = renderer == null ? null : renderer.materialFinder();
this.transform = finder == null ? quad -> true : quad -> {
RenderMaterial newMaterial = finder.copyFrom(quad.material()).blendMode(blendMode).find();
quad.material(newMaterial);
return true;
};
}

@Override
public boolean isVanillaAdapter() {
return false;
}

@Override
public void emitBlockQuads(BlockAndTintGetter blockView, BlockState state, BlockPos pos, Supplier<RandomSource> randomSupplier, RenderContext context) {
context.pushTransform(this.transform);
super.emitBlockQuads(blockView, state, pos, randomSupplier, context);
context.popTransform();
}

@Override
public void emitItemQuads(ItemStack stack, Supplier<RandomSource> randomSupplier, RenderContext context) {
context.pushTransform(this.transform);
super.emitItemQuads(stack, randomSupplier, context);
context.popTransform();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.github.fabricators_of_create.porting_lib.models.extensions;

import net.fabricmc.fabric.api.renderer.v1.material.BlendMode;

import org.jetbrains.annotations.ApiStatus;

import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;

@ApiStatus.Internal
public interface BlockModelExtensions {
void port_lib$setRenderMaterial(RenderMaterial material);
void port_lib$setBlendMode(BlendMode blendMode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ private BlockModel addModelRenderType(BlockModel model, JsonElement jsonElement,
if (renderer != null) {
String typeName = GsonHelper.getAsString(modelJson, "render_type");
BlendMode blendMode = BlendMode.fromRenderLayer(RenderTypeUtil.get(new ResourceLocation(typeName)));
RenderMaterial material = renderer.materialFinder().blendMode(blendMode).find();
((BlockModelExtensions) model).port_lib$setRenderMaterial(material);
((BlockModelExtensions) model).port_lib$setBlendMode(blendMode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.function.Function;

import io.github.fabricators_of_create.porting_lib.models.CustomBlendModeModel;
import net.fabricmc.fabric.api.renderer.v1.material.BlendMode;
import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;

import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -24,18 +26,29 @@
public class BlockModelMixin implements BlockModelExtensions {
@Unique
private RenderMaterial material;
@Unique
private BlendMode blendMode;

@Override
public void port_lib$setRenderMaterial(RenderMaterial material) {
this.material = material;
}

@Override
public void port_lib$setBlendMode(BlendMode blendMode) {
this.blendMode = blendMode;
}

@ModifyReturnValue(method = "bake(Lnet/minecraft/client/resources/model/ModelBaker;Lnet/minecraft/client/renderer/block/model/BlockModel;Ljava/util/function/Function;Lnet/minecraft/client/resources/model/ModelState;Lnet/minecraft/resources/ResourceLocation;Z)Lnet/minecraft/client/resources/model/BakedModel;", at = @At("RETURN"))
private BakedModel useRenderMaterial(BakedModel model, ModelBaker modelBaker, BlockModel blockModel,
Function<Material, TextureAtlasSprite> function, ModelState modelState,
ResourceLocation resourceLocation, boolean bl) {
if (material != null)
return new RenderMaterialModel(model, material);
return model;
private BakedModel useCustomRendering(BakedModel model, ModelBaker modelBaker, BlockModel blockModel,
Function<Material, TextureAtlasSprite> function, ModelState modelState,
ResourceLocation resourceLocation, boolean bl) {
if (this.material != null) {
return new RenderMaterialModel(model, this.material);
} else if (blendMode != null) {
return new CustomBlendModeModel(model, blendMode);
} else {
return model;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
import io.github.fabricators_of_create.porting_lib.core.PortingLib;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;

public class PortingLibModelsTestmod implements ModInitializer {
public static Item DERPY_HELMET = new Item(new FabricItemSettings().equipmentSlot(stack -> EquipmentSlot.HEAD));
public static final Item DERPY_HELMET = new Item(new FabricItemSettings().equipmentSlot(stack -> EquipmentSlot.HEAD));
public static final Item STONE_2 = new Item(new FabricItemSettings());
public static final Block NOT_GLASS = new Block(FabricBlockSettings.copyOf(Blocks.GLASS));

@Override
public void onInitialize() {
Registry.register(BuiltInRegistries.ITEM, PortingLib.id("derp_helmet"), DERPY_HELMET);
Registry.register(BuiltInRegistries.ITEM, PortingLib.id("stone_2"), STONE_2);
Registry.register(BuiltInRegistries.BLOCK, PortingLib.id("not_glass"), NOT_GLASS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "porting_lib:block/not_glass"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"item.porting_lib.derp_helmet": "Derpy Helmet"
"item.porting_lib.derp_helmet": "Derpy Helmet",
"item.porting_lib.stone_2": "Stone 2",
"block.porting_lib.not_glass": "Not Glass"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"parent": "minecraft:block/cube_all",
"textures": {
"all": "minecraft:block/glass"
},
"render_type": "cutout"
}

0 comments on commit 8d36ad3

Please sign in to comment.