Skip to content

Commit

Permalink
Use Forge delegates as map keys instead of blocks themselves
Browse files Browse the repository at this point in the history
Theoretically fixes issues around registry replacement
  • Loading branch information
embeddedt committed Jan 4, 2024
1 parent c730f72 commit 1b2097a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
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.Reference2ReferenceOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.coderbot.iris.Iris;
import net.coderbot.iris.shaderpack.materialmap.BlockEntry;
import net.coderbot.iris.shaderpack.materialmap.BlockRenderType;
import net.coderbot.iris.shaderpack.materialmap.NamespacedId;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -18,10 +19,12 @@
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraftforge.client.ChunkRenderTypeSet;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class BlockMaterialMapping {
public static Object2IntMap<BlockState> createBlockStateIdMap(Int2ObjectMap<List<BlockEntry>> blockPropertiesMap) {
Expand All @@ -36,13 +39,13 @@ public static Object2IntMap<BlockState> createBlockStateIdMap(Int2ObjectMap<List
return blockStateIds;
}

public static Map<Block, ChunkRenderTypeSet> createBlockTypeMap(Map<NamespacedId, BlockRenderType> blockPropertiesMap) {
Map<Block, ChunkRenderTypeSet> blockTypeIds = new Reference2ReferenceOpenHashMap<>();
public static Map<Holder.Reference<Block>, ChunkRenderTypeSet> createBlockTypeMap(Map<NamespacedId, BlockRenderType> blockPropertiesMap) {
Map<Holder.Reference<Block>, ChunkRenderTypeSet> blockTypeIds = new Object2ObjectOpenHashMap<>();

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

Block block = BuiltInRegistries.BLOCK.get(resourceLocation);
Holder.Reference<Block> block = ForgeRegistries.BLOCKS.getDelegateOrThrow(resourceLocation);

blockTypeIds.put(block, ChunkRenderTypeSet.of(convertBlockToRenderType(blockType)));
});
Expand All @@ -68,10 +71,15 @@ private static void addBlockStates(BlockEntry entry, Object2IntMap<BlockState> i
NamespacedId id = entry.getId();
ResourceLocation resourceLocation = new ResourceLocation(id.getNamespace(), id.getName());

Block block = BuiltInRegistries.BLOCK.get(resourceLocation);
Optional<Holder.Reference<Block>> delegateOpt = ForgeRegistries.BLOCKS.getDelegate(resourceLocation);

// If the block doesn't exist, by default the registry will return AIR. That probably isn't what we want.
// TODO: Assuming that Registry.BLOCK.getDefaultId() == "minecraft:air" here
if (delegateOpt.isEmpty() || !delegateOpt.get().isBound()) {
return;
}

Block block = delegateOpt.get().get();

if (block == Blocks.AIR) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import net.coderbot.iris.shaderpack.materialmap.NamespacedId;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.Holder;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.client.ChunkRenderTypeSet;
Expand All @@ -16,7 +17,7 @@ public class BlockRenderingSettings {

private boolean reloadRequired;
private Object2IntMap<BlockState> blockStateIds;
private Map<Block, ChunkRenderTypeSet> blockTypeIds;
private Map<Holder.Reference<Block>, ChunkRenderTypeSet> blockTypeIds;
private Object2IntFunction<NamespacedId> entityIds;
private Object2IntFunction<NamespacedId> itemIds;
private float ambientOcclusionLevel;
Expand Down Expand Up @@ -54,7 +55,7 @@ public Object2IntMap<BlockState> getBlockStateIds() {
}

@Nullable
public Map<Block, ChunkRenderTypeSet> getBlockTypeIds() {
public Map<Holder.Reference<Block>, ChunkRenderTypeSet> getBlockTypeIds() {
return blockTypeIds;
}

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

public void setBlockTypeIds(Map<Block, ChunkRenderTypeSet> blockTypeIds) {
public void setBlockTypeIds(Map<Holder.Reference<Block>, ChunkRenderTypeSet> blockTypeIds) {
if (this.blockTypeIds != null && this.blockTypeIds.equals(blockTypeIds)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import net.coderbot.iris.block_rendering.BlockRenderingSettings;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.Holder;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.client.ChunkRenderTypeSet;
import net.minecraftforge.registries.ForgeRegistries;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -17,9 +19,9 @@
public class MixinItemBlockRenderTypes {
@Inject(method = "getRenderLayers", at = @At("HEAD"), cancellable = true, remap = false)
private static void iris$setCustomRenderType(BlockState arg, CallbackInfoReturnable<ChunkRenderTypeSet> cir) {
Map<Block, ChunkRenderTypeSet> idMap = BlockRenderingSettings.INSTANCE.getBlockTypeIds();
Map<Holder.Reference<Block>, ChunkRenderTypeSet> idMap = BlockRenderingSettings.INSTANCE.getBlockTypeIds();
if (idMap != null) {
ChunkRenderTypeSet type = idMap.get(arg.getBlock());
ChunkRenderTypeSet type = idMap.get(ForgeRegistries.BLOCKS.getDelegateOrThrow(arg.getBlock()));
if (type != null) {
cir.setReturnValue(type);
}
Expand Down

0 comments on commit 1b2097a

Please sign in to comment.