|
| 1 | +package org.embeddedt.modernfix.common.mixin.perf.deduplicate_wall_shapes; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableMap; |
| 4 | +import net.minecraft.world.level.block.Block; |
| 5 | +import net.minecraft.world.level.block.WallBlock; |
| 6 | +import net.minecraft.world.level.block.state.BlockState; |
| 7 | +import net.minecraft.world.level.block.state.StateDefinition; |
| 8 | +import net.minecraft.world.level.block.state.properties.Property; |
| 9 | +import net.minecraft.world.phys.shapes.VoxelShape; |
| 10 | +import org.spongepowered.asm.mixin.Mixin; |
| 11 | +import org.spongepowered.asm.mixin.injection.At; |
| 12 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 13 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +@Mixin(WallBlock.class) |
| 20 | +public abstract class WallBlockMixin extends Block { |
| 21 | + private static Map<ImmutableMap<Property<?>, Comparable<?>>, VoxelShape> CACHE_BY_PROPERTIES = new HashMap<>(); |
| 22 | + private static StateDefinition<Block, BlockState> CACHED_DEFINITION = null; |
| 23 | + private static float[] CACHED_FLOATS = null; |
| 24 | + |
| 25 | + public WallBlockMixin(Properties properties) { |
| 26 | + super(properties); |
| 27 | + } |
| 28 | + |
| 29 | + @Inject(method = "makeShapes", at = @At("HEAD"), cancellable = true) |
| 30 | + private synchronized void useCachedShapeMap(float f1, float f2, float f3, float f4, float f5, float f6, CallbackInfoReturnable<Map<BlockState, VoxelShape>> cir) { |
| 31 | + if(CACHED_DEFINITION != null) { |
| 32 | + // check if this state container's properties exactly match the one we used for the cache |
| 33 | + if(CACHED_DEFINITION.getProperties().equals(this.stateDefinition.getProperties()) && Arrays.equals(CACHED_FLOATS, new float[] { f1, f2, f3, f4, f5, f6 })) { |
| 34 | + ImmutableMap.Builder<BlockState, VoxelShape> builder = ImmutableMap.builder(); |
| 35 | + for(BlockState state : this.stateDefinition.getPossibleStates()) { |
| 36 | + builder.put(state, CACHE_BY_PROPERTIES.get(state.getValues())); |
| 37 | + } |
| 38 | + cir.setReturnValue(builder.build()); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Inject(method = "makeShapes", at = @At("RETURN")) |
| 44 | + private synchronized void storeCachedShapesByProperty(float f1, float f2, float f3, float f4, float f5, float f6, CallbackInfoReturnable<Map<BlockState, VoxelShape>> cir) { |
| 45 | + if(CACHE_BY_PROPERTIES.size() == 0) { |
| 46 | + Map<BlockState, VoxelShape> shapeMap = cir.getReturnValue(); |
| 47 | + for(Map.Entry<BlockState, VoxelShape> entry : shapeMap.entrySet()) { |
| 48 | + CACHE_BY_PROPERTIES.put(entry.getKey().getValues(), entry.getValue()); |
| 49 | + } |
| 50 | + CACHED_FLOATS = new float[] { f1, f2, f3, f4, f5, f6 }; |
| 51 | + CACHED_DEFINITION = this.stateDefinition; |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments