9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2026-01-03 22:26:16 +00:00

refactor(bukkit): 重构方块放置逻辑以便支持无玩家对象的上下文

This commit is contained in:
jhqwqmc
2025-06-24 05:27:16 +08:00
parent cac7c57341
commit bf41c572bf
10 changed files with 127 additions and 67 deletions

View File

@@ -8,6 +8,8 @@ import net.momirealms.craftengine.core.world.BlockHitResult;
import net.momirealms.craftengine.core.world.BlockPos;
import net.momirealms.craftengine.core.world.World;
import javax.annotation.Nullable;
public class BlockPlaceContext extends UseOnContext {
private final BlockPos relativePos;
protected boolean replaceClicked;
@@ -16,7 +18,7 @@ public class BlockPlaceContext extends UseOnContext {
this(context.getLevel(), context.getPlayer(), context.getHand(), context.getItem(), context.getHitResult());
}
public BlockPlaceContext(World world, Player player, InteractionHand hand, Item<?> stack, BlockHitResult hit) {
public BlockPlaceContext(World world, @Nullable Player player, InteractionHand hand, Item<?> stack, BlockHitResult hit) {
super(world, player, hand, stack, hit);
this.relativePos = hit.getBlockPos().relative(hit.getDirection());
this.replaceClicked = true;

View File

@@ -0,0 +1,18 @@
package net.momirealms.craftengine.core.item.context;
import net.momirealms.craftengine.core.entity.player.InteractionHand;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.util.Direction;
import net.momirealms.craftengine.core.world.BlockHitResult;
import net.momirealms.craftengine.core.world.World;
public class PlaceBlockBlockPlaceContext extends BlockPlaceContext {
public PlaceBlockBlockPlaceContext(World world, InteractionHand hand, Item<?> stack, BlockHitResult hit) {
super(world, null, hand, stack, hit);
}
@Override
public Direction getNearestLookingDirection() {
return this.getHitResult().getDirection();
}
}

View File

@@ -8,8 +8,10 @@ import net.momirealms.craftengine.core.world.BlockHitResult;
import net.momirealms.craftengine.core.world.BlockPos;
import net.momirealms.craftengine.core.world.Vec3d;
import net.momirealms.craftengine.core.world.World;
import org.jetbrains.annotations.Nullable;
public class UseOnContext {
@Nullable
private final Player player;
private final InteractionHand hand;
private final BlockHitResult hitResult;
@@ -24,7 +26,7 @@ public class UseOnContext {
this(player.world(), player, hand, stack, hit);
}
public UseOnContext(World world, Player player, InteractionHand hand, Item<?> stack, BlockHitResult hit) {
public UseOnContext(World world, @Nullable Player player, InteractionHand hand, Item<?> stack, BlockHitResult hit) {
this.player = player;
this.hand = hand;
this.hitResult = hit;
@@ -56,6 +58,7 @@ public class UseOnContext {
return this.itemStack;
}
@Nullable
public Player getPlayer() {
return this.player;
}
@@ -69,11 +72,11 @@ public class UseOnContext {
}
public Direction getHorizontalDirection() {
return this.player.getDirection();
return this.player == null ? Direction.NORTH : this.player.getDirection();
}
public boolean isSecondaryUseActive() {
return this.player.isSecondaryUseActive();
return this.player != null && this.player.isSecondaryUseActive();
}
public float getRotation() {