Merge pull request 'fix: Dynamic Property Copying in Replace Mode' (#2) from Cookieso/Gimbal:master into master

Reviewed-on: https://codeberg.org/moonleay/Gimbal/pulls/2
This commit is contained in:
moonleay 2024-05-06 23:11:57 +00:00
commit 60e638a3d8

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, getTargetBlockState(oldState, instance.getBlock().getDefaultState()));
}
return this.place(context, state);
}
@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;
}
}