mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2026-01-06 15:52:03 +00:00
refactor(item): 重构使用物品上下文以支持玩家为null的情况
This commit is contained in:
@@ -4,7 +4,9 @@ import net.momirealms.craftengine.core.entity.player.InteractionHand;
|
||||
import net.momirealms.craftengine.core.entity.player.InteractionResult;
|
||||
import net.momirealms.craftengine.core.entity.player.Player;
|
||||
import net.momirealms.craftengine.core.item.context.UseOnContext;
|
||||
import net.momirealms.craftengine.core.world.BlockPos;
|
||||
import net.momirealms.craftengine.core.world.World;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public abstract class ItemBehavior {
|
||||
|
||||
@@ -12,7 +14,7 @@ public abstract class ItemBehavior {
|
||||
return InteractionResult.PASS;
|
||||
}
|
||||
|
||||
public InteractionResult use(World world, Player player, InteractionHand hand) {
|
||||
public InteractionResult use(World world, @Nullable Player player, InteractionHand hand, @Nullable UseOnContext context) {
|
||||
return InteractionResult.PASS;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import net.momirealms.craftengine.core.util.Direction;
|
||||
import net.momirealms.craftengine.core.world.BlockHitResult;
|
||||
import net.momirealms.craftengine.core.world.BlockPos;
|
||||
import net.momirealms.craftengine.core.world.World;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class BlockPlaceContext extends UseOnContext {
|
||||
private final BlockPos relativePos;
|
||||
@@ -16,7 +17,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;
|
||||
|
||||
@@ -9,8 +9,10 @@ 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.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class UseOnContext {
|
||||
@Nullable
|
||||
private final Player player;
|
||||
private final InteractionHand hand;
|
||||
private final BlockHitResult hitResult;
|
||||
@@ -25,7 +27,7 @@ public class UseOnContext {
|
||||
this(player.world(), player, hand, stack, hit);
|
||||
}
|
||||
|
||||
public UseOnContext(@NotNull World world, Player player, InteractionHand hand, @NotNull Item<?> stack, BlockHitResult hit) {
|
||||
public UseOnContext(@NotNull World world, @Nullable Player player, InteractionHand hand, @NotNull Item<?> stack, BlockHitResult hit) {
|
||||
this.player = player;
|
||||
this.hand = hand;
|
||||
this.hitResult = hit;
|
||||
@@ -58,6 +60,7 @@ public class UseOnContext {
|
||||
return this.itemStack;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Player getPlayer() {
|
||||
return this.player;
|
||||
}
|
||||
@@ -71,11 +74,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() {
|
||||
|
||||
Reference in New Issue
Block a user