Skip to content

Commit

Permalink
If an error happens on place, untransform
Browse files Browse the repository at this point in the history
  • Loading branch information
ewoudje committed Jan 30, 2024
1 parent 42ce6f5 commit 5e053f1
Showing 1 changed file with 14 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,50 +1,34 @@
package org.valkyrienskies.mod.mixin.feature.block_placement_orientation;

import net.minecraft.world.InteractionResult;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import org.valkyrienskies.mod.common.PlayerUtil;

@Mixin(BlockItem.class)
public abstract class MixinBlockItem {

@Inject(
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/item/BlockItem;getPlacementState(Lnet/minecraft/world/item/context/BlockPlaceContext;)Lnet/minecraft/world/level/block/state/BlockState;"
),
method = "place",
locals = LocalCapture.CAPTURE_FAILHARD
)
private void transformPlayerWhenPlacing(final BlockPlaceContext ignore,
final CallbackInfoReturnable<InteractionResult> cir, final BlockPlaceContext context) {
if (context == null || context.getPlayer() == null) {
return;
}

PlayerUtil.transformPlayerTemporarily(context.getPlayer(), context.getLevel(), context.getClickedPos());
}

@Inject(
@WrapOperation(
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/item/BlockItem;getPlacementState(Lnet/minecraft/world/item/context/BlockPlaceContext;)Lnet/minecraft/world/level/block/state/BlockState;",
shift = Shift.AFTER
target = "Lnet/minecraft/world/item/BlockItem;getPlacementState(Lnet/minecraft/world/item/context/BlockPlaceContext;)Lnet/minecraft/world/level/block/state/BlockState;"
),
method = "place"
)
private void untransformPlayerAfterPlacing(final BlockPlaceContext context,
final CallbackInfoReturnable<InteractionResult> cir) {
if (context.getPlayer() == null) {
return;
private BlockState transformPlayerWhenPlacing(BlockItem _instance, BlockPlaceContext _ctx, Operation<BlockState> original, final BlockPlaceContext ctx) {
if (ctx == null || ctx.getPlayer() == null) {
return null;
}

PlayerUtil.untransformPlayer(context.getPlayer());
return PlayerUtil.transformPlayerTemporarily(
ctx.getPlayer(),
ctx.getLevel(),
ctx.getClickedPos(),
original::call
);
}
}

0 comments on commit 5e053f1

Please sign in to comment.