Skip to content

Commit

Permalink
Improve render type handling
Browse files Browse the repository at this point in the history
* Use Forge delegates as map keys
* Don't allow any other render types for a given block if there is an Iris override
  • Loading branch information
embeddedt committed Jan 4, 2024
1 parent 1157255 commit de8f045
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap;
import net.coderbot.iris.Iris;
import net.coderbot.iris.shaderpack.materialmap.BlockEntry;
Expand All @@ -20,6 +22,8 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.IRegistryDelegate;

public class BlockMaterialMapping {
public static Object2IntMap<BlockState> createBlockStateIdMap(Int2ObjectMap<List<BlockEntry>> blockPropertiesMap) {
Expand All @@ -34,15 +38,19 @@ public static Object2IntMap<BlockState> createBlockStateIdMap(Int2ObjectMap<List
return blockStateIds;
}

public static Map<Block, RenderType> createBlockTypeMap(Map<NamespacedId, BlockRenderType> blockPropertiesMap) {
Map<Block, RenderType> blockTypeIds = new Reference2ReferenceOpenHashMap<>();
public static Map<IRegistryDelegate<Block>, RenderType> createBlockTypeMap(Map<NamespacedId, BlockRenderType> blockPropertiesMap) {
Map<IRegistryDelegate<Block>, RenderType> blockTypeIds = new Object2ObjectOpenHashMap<>();

blockPropertiesMap.forEach((id, blockType) -> {
ResourceLocation resourceLocation = new ResourceLocation(id.getNamespace(), id.getName());

Block block = Registry.BLOCK.get(resourceLocation);
Block block = ForgeRegistries.BLOCKS.getValue(resourceLocation);

blockTypeIds.put(block, convertBlockToRenderType(blockType));
if (block == null || block == Blocks.AIR) {
return;
}

blockTypeIds.put(block.delegate, convertBlockToRenderType(blockType));
});

return blockTypeIds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;

import net.minecraftforge.registries.IRegistryDelegate;
import org.jetbrains.annotations.Nullable;

import it.unimi.dsi.fastutil.objects.Object2IntFunction;
Expand All @@ -16,7 +17,7 @@ public class BlockRenderingSettings {

private boolean reloadRequired;
private Object2IntMap<BlockState> blockStateIds;
private Map<Block, RenderType> blockTypeIds;
private Map<IRegistryDelegate<Block>, RenderType> blockTypeIds;
private Object2IntFunction<NamespacedId> entityIds;
private float ambientOcclusionLevel;
private boolean disableDirectionalShading;
Expand Down Expand Up @@ -47,7 +48,7 @@ public Object2IntMap<BlockState> getBlockStateIds() {
}

@Nullable
public Map<Block, RenderType> getBlockTypeIds() {
public Map<IRegistryDelegate<Block>, RenderType> getBlockTypeIds() {
return blockTypeIds;
}

Expand All @@ -66,7 +67,7 @@ public void setBlockStateIds(Object2IntMap<BlockState> blockStateIds) {
this.blockStateIds = blockStateIds;
}

public void setBlockTypeIds(Map<Block, RenderType> blockTypeIds) {
public void setBlockTypeIds(Map<IRegistryDelegate<Block>, RenderType> blockTypeIds) {
if (this.blockTypeIds != null && this.blockTypeIds.equals(blockTypeIds)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;

import net.minecraftforge.registries.IRegistryDelegate;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -17,9 +18,9 @@
public class MixinItemBlockRenderTypes {
@Inject(method = "getChunkRenderType", at = @At("HEAD"), cancellable = true)
private static void iris$setCustomRenderType(BlockState arg, CallbackInfoReturnable<RenderType> cir) {
Map<Block, RenderType> idMap = BlockRenderingSettings.INSTANCE.getBlockTypeIds();
Map<IRegistryDelegate<Block>, RenderType> idMap = BlockRenderingSettings.INSTANCE.getBlockTypeIds();
if (idMap != null) {
RenderType type = idMap.get(arg.getBlock());
RenderType type = idMap.get(arg.getBlock().delegate);
if (type != null) {
cir.setReturnValue(type);
}
Expand All @@ -28,11 +29,12 @@ public class MixinItemBlockRenderTypes {

@Inject(method = "canRenderInLayer(Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/client/renderer/RenderType;)Z", at = @At("HEAD"), cancellable = true)
private static void iris$checkCustomRenderType(BlockState arg, RenderType type, CallbackInfoReturnable<Boolean> cir) {
Map<Block, RenderType> idMap = BlockRenderingSettings.INSTANCE.getBlockTypeIds();
Map<IRegistryDelegate<Block>, RenderType> idMap = BlockRenderingSettings.INSTANCE.getBlockTypeIds();
if (idMap != null) {
RenderType irisType = idMap.get(arg.getBlock());
if (type.equals(irisType)) {
cir.setReturnValue(true);
RenderType irisType = idMap.get(arg.getBlock().delegate);
if (irisType != null) {
// If we have an override registered for this block, only allow rendering precisely that type
cir.setReturnValue(type.equals(irisType));
}
}
}
Expand Down

0 comments on commit de8f045

Please sign in to comment.