From 31bab516036ba00b4cc73221feaf688721d3474b Mon Sep 17 00:00:00 2001 From: cookieso Date: Mon, 6 May 2024 21:43:57 +0200 Subject: [PATCH] fix: Made replace mode more dynamic to copy properties for all blocks --- .../mixin/ReplaceStateUpdaterMixin.java | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/main/java/net/moonleay/gimbal/mixin/ReplaceStateUpdaterMixin.java b/src/main/java/net/moonleay/gimbal/mixin/ReplaceStateUpdaterMixin.java index 7d0772d..255604a 100644 --- a/src/main/java/net/moonleay/gimbal/mixin/ReplaceStateUpdaterMixin.java +++ b/src/main/java/net/moonleay/gimbal/mixin/ReplaceStateUpdaterMixin.java @@ -18,12 +18,11 @@ package net.moonleay.gimbal.mixin; -import net.minecraft.block.Block; import net.minecraft.block.BlockState; -import net.minecraft.block.StairsBlock; import net.minecraft.item.BlockItem; import net.minecraft.item.Item; import net.minecraft.item.ItemPlacementContext; +import net.minecraft.state.property.Property; import net.moonleay.gimbal.editor.ServerEditorManager; import net.moonleay.gimbal.editor.state.mode.Capability; import org.spongepowered.asm.mixin.Mixin; @@ -34,8 +33,6 @@ import org.spongepowered.asm.mixin.injection.Redirect; import java.util.UUID; -import static net.minecraft.block.StairsBlock.*; - @Mixin(BlockItem.class) public abstract class ReplaceStateUpdaterMixin extends Item { @@ -54,21 +51,20 @@ public abstract class ReplaceStateUpdaterMixin extends Item { if (!ServerEditorManager.INSTANCE.shouldPlayer(id, Capability.REPLACE)) return this.place(context, state); BlockState oldState = context.getWorld().getBlockState(context.getBlockPos()); - Block targetBl = oldState.getBlock(); - if (state.getBlock() instanceof StairsBlock && targetBl instanceof StairsBlock targetStair) { - // Block and item is stairs, parse Stairs data - return this.place(context, copyStairState(oldState, instance)); - } - return this.place(context, state); + return this.place(context, getTargetBlockState(oldState, instance.getBlock().getDefaultState())); } - @Unique - public BlockState copyStairState(BlockState targetState, BlockItem item) { - BlockState blockState = item.getBlock().getDefaultState() - .with(FACING, targetState.get(FACING)) - .with(HALF, targetState.get(HALF)) - .with(WATERLOGGED, targetState.get(WATERLOGGED)); - return blockState.with(SHAPE, targetState.get(SHAPE)); + public BlockState getTargetBlockState(BlockState oldState, BlockState newBlock) { + var oldManager = oldState.getBlock().getStateManager(); + var newManager = newBlock.getBlock().getStateManager(); + for (var prop : oldManager.getProperties()) { + var matchingProp = newManager.getProperty(prop.getName()); + if (matchingProp != null) { + //noinspection rawtypes,unchecked + newBlock = newBlock.with((Property) matchingProp, oldState.get(matchingProp)); + } + } + return newBlock; } }