Skip to content

Commit

Permalink
Fix Block Place with complex blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ssblur committed Sep 8, 2024
1 parent c74d8fd commit 3132c5e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ssblur.scriptor.mixin;

import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(BlockPlaceContext.class)
public interface BlockPlaceContextAccessor {
@Invoker("<init>")
static BlockPlaceContext createBlockPlaceContext(Level level, @Nullable Player player, InteractionHand interactionHand, ItemStack itemStack, BlockHitResult blockHitResult) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import com.ssblur.scriptor.api.word.Action;
import com.ssblur.scriptor.api.word.Descriptor;
import com.ssblur.scriptor.color.CustomColors;
import com.ssblur.scriptor.events.network.client.ParticleNetwork;
import com.ssblur.scriptor.helpers.ItemTargetableHelper;
import com.ssblur.scriptor.helpers.targetable.Targetable;
import com.ssblur.scriptor.mixin.BlockPlaceContextAccessor;
import com.ssblur.scriptor.registry.colorable.ColorableBlockRegistry;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;

public class PlaceBlockAction extends Action {
@Override
Expand All @@ -32,9 +36,19 @@ public void apply(Targetable caster, Targetable targetable, Descriptor[] descrip
);

if(itemFocus.getItem() instanceof BlockItem blockItem) {
itemFocus.shrink(1);
var block = blockItem.getBlock();
level.setBlock(pos, block.defaultBlockState(), 11);
var status = blockItem.place(
BlockPlaceContextAccessor.createBlockPlaceContext(
level,
null,
InteractionHand.MAIN_HAND,
itemFocus,
new BlockHitResult(targetable.getTargetPos(), targetable.getFacing(), targetable.getTargetBlockPos(), false)
)
);
if(status.consumesAction())
itemFocus.shrink(1);
else
ParticleNetwork.fizzle(level, targetable.getTargetBlockPos());
} else {
ColorableBlockRegistry.DYE_COLORABLE_BLOCKS.MAGIC_BLOCK.setColor(color, level, pos);
}
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/scriptor.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"package": "com.ssblur.scriptor.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"BlockPlaceContextAccessor",
"BlockRenderDispatcherAccessor",
"EditWritableSpellbookMixin",
"GiveCommandCommunityMixin",
Expand Down

0 comments on commit 3132c5e

Please sign in to comment.