|
| 1 | +package org.embeddedt.modernfix.common.mixin.perf.deduplicate_wall_shapes; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableList; |
| 4 | +import com.google.common.collect.ImmutableMap; |
| 5 | +import com.mojang.datafixers.util.Pair; |
| 6 | +import net.minecraft.world.level.block.Block; |
| 7 | +import net.minecraft.world.level.block.WallBlock; |
| 8 | +import net.minecraft.world.level.block.state.BlockState; |
| 9 | +import net.minecraft.world.level.block.state.StateDefinition; |
| 10 | +import net.minecraft.world.level.block.state.properties.Property; |
| 11 | +import net.minecraft.world.phys.shapes.VoxelShape; |
| 12 | +import org.spongepowered.asm.mixin.Mixin; |
| 13 | +import org.spongepowered.asm.mixin.injection.At; |
| 14 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 15 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 16 | + |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +/** |
| 21 | + * Most wall blocks use the default set of vanilla properties, and the default sizes for their shapes. This means |
| 22 | + * there is no need to reconstruct a separate VoxelShape instance for each wall, we can just repurpose the |
| 23 | + * same shape instances. To do this we can cache a mapping between a state (represented only as its prop->value map) |
| 24 | + * and the desired shape, and generate the BlockState->VoxelShape map from this for each block. |
| 25 | + */ |
| 26 | +@Mixin(WallBlock.class) |
| 27 | +public abstract class WallBlockMixin extends Block { |
| 28 | + private static Map<ImmutableList<Float>, Pair<Map<ImmutableMap<Property<?>, Comparable<?>>, VoxelShape>, StateDefinition<Block, BlockState>>> CACHE_BY_SHAPE_VALS = new HashMap<>(); |
| 29 | + |
| 30 | + public WallBlockMixin(Properties properties) { |
| 31 | + super(properties); |
| 32 | + } |
| 33 | + |
| 34 | + @Inject(method = "makeShapes", at = @At("HEAD"), cancellable = true) |
| 35 | + private synchronized void useCachedShapeMap(float f1, float f2, float f3, float f4, float f5, float f6, CallbackInfoReturnable<Map<BlockState, VoxelShape>> cir) { |
| 36 | + ImmutableList<Float> key = ImmutableList.of(f1, f2, f3, f4, f5, f6); |
| 37 | + Pair<Map<ImmutableMap<Property<?>, Comparable<?>>, VoxelShape>, StateDefinition<Block, BlockState>> cache = CACHE_BY_SHAPE_VALS.get(key); |
| 38 | + // require the properties to be identical |
| 39 | + if(cache == null || !cache.getSecond().getProperties().equals(this.stateDefinition.getProperties())) |
| 40 | + return; |
| 41 | + ImmutableMap.Builder<BlockState, VoxelShape> builder = ImmutableMap.builder(); |
| 42 | + for(BlockState state : this.stateDefinition.getPossibleStates()) { |
| 43 | + builder.put(state, cache.getFirst().get(state.getValues())); |
| 44 | + } |
| 45 | + cir.setReturnValue(builder.build()); |
| 46 | + } |
| 47 | + |
| 48 | + @Inject(method = "makeShapes", at = @At("RETURN")) |
| 49 | + private synchronized void storeCachedShapesByProperty(float f1, float f2, float f3, float f4, float f5, float f6, CallbackInfoReturnable<Map<BlockState, VoxelShape>> cir) { |
| 50 | + ImmutableList<Float> key = ImmutableList.of(f1, f2, f3, f4, f5, f6); |
| 51 | + if(!CACHE_BY_SHAPE_VALS.containsKey(key)) { |
| 52 | + Map<ImmutableMap<Property<?>, Comparable<?>>, VoxelShape> cacheByProperties = new HashMap<>(); |
| 53 | + Map<BlockState, VoxelShape> shapeMap = cir.getReturnValue(); |
| 54 | + for(Map.Entry<BlockState, VoxelShape> entry : shapeMap.entrySet()) { |
| 55 | + cacheByProperties.put(entry.getKey().getValues(), entry.getValue()); |
| 56 | + } |
| 57 | + CACHE_BY_SHAPE_VALS.put(key, Pair.of(cacheByProperties, this.stateDefinition)); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments