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; package net.moonleay.gimbal.mixin;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.StairsBlock;
import net.minecraft.item.BlockItem; import net.minecraft.item.BlockItem;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemPlacementContext;
import net.minecraft.state.property.Property;
import net.moonleay.gimbal.editor.ServerEditorManager; import net.moonleay.gimbal.editor.ServerEditorManager;
import net.moonleay.gimbal.editor.state.mode.Capability; import net.moonleay.gimbal.editor.state.mode.Capability;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
@ -34,8 +33,6 @@ import org.spongepowered.asm.mixin.injection.Redirect;
import java.util.UUID; import java.util.UUID;
import static net.minecraft.block.StairsBlock.*;
@Mixin(BlockItem.class) @Mixin(BlockItem.class)
public abstract class ReplaceStateUpdaterMixin extends Item { public abstract class ReplaceStateUpdaterMixin extends Item {
@ -54,21 +51,20 @@ public abstract class ReplaceStateUpdaterMixin extends Item {
if (!ServerEditorManager.INSTANCE.shouldPlayer(id, Capability.REPLACE)) if (!ServerEditorManager.INSTANCE.shouldPlayer(id, Capability.REPLACE))
return this.place(context, state); return this.place(context, state);
BlockState oldState = context.getWorld().getBlockState(context.getBlockPos()); BlockState oldState = context.getWorld().getBlockState(context.getBlockPos());
Block targetBl = oldState.getBlock(); return this.place(context, getTargetBlockState(oldState, instance.getBlock().getDefaultState()));
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);
} }
@Unique @Unique
public BlockState copyStairState(BlockState targetState, BlockItem item) { public BlockState getTargetBlockState(BlockState oldState, BlockState newBlock) {
BlockState blockState = item.getBlock().getDefaultState() var oldManager = oldState.getBlock().getStateManager();
.with(FACING, targetState.get(FACING)) var newManager = newBlock.getBlock().getStateManager();
.with(HALF, targetState.get(HALF)) for (var prop : oldManager.getProperties()) {
.with(WATERLOGGED, targetState.get(WATERLOGGED)); var matchingProp = newManager.getProperty(prop.getName());
return blockState.with(SHAPE, targetState.get(SHAPE)); if (matchingProp != null) {
//noinspection rawtypes,unchecked
newBlock = newBlock.with((Property) matchingProp, oldState.get(matchingProp));
}
}
return newBlock;
} }
} }