fix: Made replace mode more dynamic to copy properties for all blocks

This commit is contained in:
cookieso 2024-05-06 21:43:57 +02:00
parent 6002af93a5
commit 31bab51603

View file

@ -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;
}
}