9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-28 11:29:17 +00:00

添加物品行为,修正方块交互

This commit is contained in:
halogly
2025-09-04 14:30:07 +08:00
parent f96b12bd45
commit 2ab8ffe7f9
14 changed files with 454 additions and 217 deletions

View File

@@ -4,8 +4,7 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import net.momirealms.craftengine.bukkit.item.behavior.AxeItemBehavior;
import net.momirealms.craftengine.bukkit.item.behavior.FlintAndSteelItemBehavior;
import net.momirealms.craftengine.bukkit.item.behavior.*;
import net.momirealms.craftengine.bukkit.item.factory.BukkitItemFactory;
import net.momirealms.craftengine.bukkit.item.listener.ArmorEventListener;
import net.momirealms.craftengine.bukkit.item.listener.DebugStickListener;
@@ -41,6 +40,9 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
static {
registerVanillaItemExtraBehavior(FlintAndSteelItemBehavior.INSTANCE, ItemKeys.FLINT_AND_STEEL);
registerVanillaItemExtraBehavior(AxeItemBehavior.INSTANCE, ItemKeys.AXES);
registerVanillaItemExtraBehavior(CompassItemBehavior.INSTANCE, ItemKeys.COMPASS);
registerVanillaItemExtraBehavior(EnderEyeItemBehavior.INSTANCE, ItemKeys.ENDER_EYE);
registerVanillaItemExtraBehavior(EndCrystalItemBehavior.INSTANCE, ItemKeys.END_CRYSTAL);
}
private static BukkitItemManager instance;

View File

@@ -108,10 +108,7 @@ public class AxeItemBehavior extends ItemBehavior {
if (!InteractUtils.isInteractable(
bukkitPlayer, BlockStateUtils.fromBlockData(customState.vanillaBlockState().literalObject()),
context.getHitResult(), item
) || (player.isSecondaryUseActive() && !InteractUtils.isIgnoreSneaking(
bukkitPlayer, BlockStateUtils.fromBlockData(customState.vanillaBlockState().literalObject()),
context.getHitResult(), item
))) {
) || player.isSecondaryUseActive()) {
player.swingHand(context.getHand());
}
// shrink item amount

View File

@@ -12,6 +12,9 @@ public class BukkitItemBehaviors extends ItemBehaviors {
public static final Key COMPOSTABLE_ITEM = Key.from("craftengine:compostable_item");
public static final Key AXE_ITEM = Key.from("craftengine:axe_item");
public static final Key DOUBLE_HIGH_BLOCK_ITEM = Key.from("craftengine:double_high_block_item");
public static final Key COMPASS_ITEM = Key.from("craftengine:compass_item");
public static final Key ENDER_EYE_ITEM = Key.from("craftengine:ender_eye_item");
public static final Key END_CRYSTAL_ITEM = Key.from("craftengine:end_crystal_item");
public static void init() {
register(EMPTY, EmptyItemBehavior.FACTORY);
@@ -22,5 +25,8 @@ public class BukkitItemBehaviors extends ItemBehaviors {
register(COMPOSTABLE_ITEM, CompostableItemBehavior.FACTORY);
register(AXE_ITEM, AxeItemBehavior.FACTORY);
register(DOUBLE_HIGH_BLOCK_ITEM, DoubleHighBlockItemBehavior.FACTORY);
register(COMPASS_ITEM, CompassItemBehavior.FACTORY);
register(ENDER_EYE_ITEM, EnderEyeItemBehavior.FACTORY);
register(END_CRYSTAL_ITEM, EndCrystalItemBehavior.FACTORY);
}
}

View File

@@ -0,0 +1,40 @@
package net.momirealms.craftengine.bukkit.item.behavior;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBlocks;
import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
import net.momirealms.craftengine.bukkit.world.BukkitExistingBlock;
import net.momirealms.craftengine.core.entity.player.InteractionResult;
import net.momirealms.craftengine.core.item.behavior.ItemBehavior;
import net.momirealms.craftengine.core.item.behavior.ItemBehaviorFactory;
import net.momirealms.craftengine.core.item.context.UseOnContext;
import net.momirealms.craftengine.core.pack.Pack;
import net.momirealms.craftengine.core.util.Key;
import org.bukkit.block.data.BlockData;
import java.nio.file.Path;
import java.util.Map;
public class CompassItemBehavior extends ItemBehavior {
public static final CompassItemBehavior INSTANCE = new CompassItemBehavior();
public static final CompassItemBehavior.Factory FACTORY = new CompassItemBehavior.Factory();
@Override
public InteractionResult useOnBlock(UseOnContext context) {
BukkitExistingBlock block = (BukkitExistingBlock) context.getLevel().getBlockAt(context.getClickedPos());
BlockData blockData = block.block().getBlockData();
Object blockOwner = BlockStateUtils.getBlockOwner(BlockStateUtils.blockDataToBlockState(blockData));
if (blockOwner != MBlocks.LODESTONE) {
return InteractionResult.PASS;
} else {
return InteractionResult.SUCCESS;
}
}
public static class Factory implements ItemBehaviorFactory {
@Override
public ItemBehavior create(Pack pack, Path path, Key id, Map<String, Object> arguments) {
return INSTANCE;
}
}
}

View File

@@ -0,0 +1,76 @@
package net.momirealms.craftengine.bukkit.item.behavior;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBlocks;
import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
import net.momirealms.craftengine.bukkit.world.BukkitExistingBlock;
import net.momirealms.craftengine.core.entity.Entity;
import net.momirealms.craftengine.core.entity.player.InteractionResult;
import net.momirealms.craftengine.core.item.behavior.ItemBehavior;
import net.momirealms.craftengine.core.item.behavior.ItemBehaviorFactory;
import net.momirealms.craftengine.core.item.context.UseOnContext;
import net.momirealms.craftengine.core.pack.Pack;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.world.BlockPos;
import net.momirealms.craftengine.core.world.World;
import net.momirealms.craftengine.core.world.collision.AABB;
import org.bukkit.block.data.BlockData;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
public class EndCrystalItemBehavior extends ItemBehavior {
public static final EndCrystalItemBehavior INSTANCE = new EndCrystalItemBehavior();
public static final EndCrystalItemBehavior.Factory FACTORY = new EndCrystalItemBehavior.Factory();
@Override
public InteractionResult useOnBlock(UseOnContext context) {
World world = context.getLevel();
BlockPos blockPos = context.getClickedPos();
BukkitExistingBlock block = (BukkitExistingBlock) world.getBlockAt(blockPos);
BlockData blockData = block.block().getBlockData();
Object blockOwner = BlockStateUtils.getBlockOwner(BlockStateUtils.blockDataToBlockState(blockData));
if (blockOwner != MBlocks.OBSIDIAN && blockOwner != MBlocks.BEDROCK) {
return InteractionResult.PASS;
} else {
BlockPos abovePos = blockPos.above();
BukkitExistingBlock aboveBlock = (BukkitExistingBlock) world.getBlockAt(abovePos);
BlockData aboveBlockData = aboveBlock.block().getBlockData();
Object aboveBlockOwner = BlockStateUtils.getBlockOwner(BlockStateUtils.blockDataToBlockState(aboveBlockData));
if (aboveBlockOwner != MBlocks.AIR) {
return InteractionResult.PASS;
} else {
double x = abovePos.x();
double y = abovePos.y();
double z = abovePos.z();
AABB aabb = new AABB(x, y, z, x + 1.0, y + 2.0, z + 1.0);
List<Entity> entities = world.getEntities(null, aabb);
if (!entities.isEmpty()) {
return InteractionResult.PASS;
}
List<AABB> aabbs = List.of(aabb);
List<Object> nmsAABB = aabbs.stream()
.map(AABB -> FastNMS.INSTANCE.constructor$AABB(
AABB.minX, AABB.minY, AABB.minZ,
AABB.maxX, AABB.maxY, AABB.maxZ
)).toList();
if (!FastNMS.INSTANCE.checkEntityCollision(context.getLevel().serverWorld(), nmsAABB, x, y, z)) {
return InteractionResult.PASS;
}
return InteractionResult.SUCCESS;
}
}
}
public static class Factory implements ItemBehaviorFactory {
@Override
public ItemBehavior create(Pack pack, Path path, Key id, Map<String, Object> arguments) {
return INSTANCE;
}
}
}

View File

@@ -0,0 +1,41 @@
package net.momirealms.craftengine.bukkit.item.behavior;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBlocks;
import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
import net.momirealms.craftengine.bukkit.world.BukkitExistingBlock;
import net.momirealms.craftengine.core.entity.player.InteractionResult;
import net.momirealms.craftengine.core.item.behavior.ItemBehavior;
import net.momirealms.craftengine.core.item.behavior.ItemBehaviorFactory;
import net.momirealms.craftengine.core.item.context.UseOnContext;
import net.momirealms.craftengine.core.pack.Pack;
import net.momirealms.craftengine.core.util.Key;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.EndPortalFrame;
import java.nio.file.Path;
import java.util.Map;
public class EnderEyeItemBehavior extends ItemBehavior {
public static final EnderEyeItemBehavior INSTANCE = new EnderEyeItemBehavior();
public static final EnderEyeItemBehavior.Factory FACTORY = new EnderEyeItemBehavior.Factory();
@Override
public InteractionResult useOnBlock(UseOnContext context) {
BukkitExistingBlock block = (BukkitExistingBlock) context.getLevel().getBlockAt(context.getClickedPos());
BlockData blockData = block.block().getBlockData();
Object blockOwner = BlockStateUtils.getBlockOwner(BlockStateUtils.blockDataToBlockState(blockData));
if (blockOwner != MBlocks.END_PORTAL_FRAME || (blockData instanceof EndPortalFrame endPortalFrame && endPortalFrame.hasEye())) {
return InteractionResult.PASS;
} else {
return InteractionResult.SUCCESS;
}
}
public static class Factory implements ItemBehaviorFactory {
@Override
public ItemBehavior create(Pack pack, Path path, Key id, Map<String, Object> arguments) {
return INSTANCE;
}
}
}

View File

@@ -257,7 +257,7 @@ public class ItemEventListener implements Listener {
// so we should check and resend sounds on BlockPlaceEvent
BlockData craftBlockData = BlockStateUtils.fromBlockData(immutableBlockState.vanillaBlockState().literalObject());
if (InteractUtils.isInteractable(player, craftBlockData, hitResult, itemInHand)) {
if (!serverPlayer.isSecondaryUseActive() || InteractUtils.isIgnoreSneaking(player, craftBlockData, hitResult, itemInHand)) {
if (!serverPlayer.isSecondaryUseActive()) {
serverPlayer.setResendSound();
}
} else {
@@ -276,7 +276,7 @@ public class ItemEventListener implements Listener {
return;
} else {
// todo 实际上这里的处理并不正确,因为判断玩家是否能够放置那个方块需要更加细节的判断。比如玩家无法对着树叶放置火把,但是交互事件依然触发,此情况下不可丢弃自定义行为。
if ((serverPlayer.isSecondaryUseActive() && InteractUtils.isIgnoreSneaking(player, blockData, hitResult, itemInHand)) || !InteractUtils.isInteractable(player, blockData, hitResult, itemInHand)) {
if (serverPlayer.isSecondaryUseActive() || !InteractUtils.isInteractable(player, blockData, hitResult, itemInHand)) {
event.setCancelled(true);
}
}
@@ -290,10 +290,9 @@ public class ItemEventListener implements Listener {
if (optionalItemBehaviors.isPresent()) {
// 检测是否可交互应当只判断原版方块,因为自定义方块早就判断过了,如果可交互不可能到这一步
boolean interactable = immutableBlockState == null && InteractUtils.isInteractable(player, blockData, hitResult, itemInHand);
boolean isIgnoreSneaking = InteractUtils.isIgnoreSneaking(player, blockData, hitResult, itemInHand);
// 如果方块可交互但是玩家没shift那么原版的方块交互优先取消自定义物品的behavior
// todo 如果我的物品行为允许某些交互呢?是否值得进一步处理?
if ((!serverPlayer.isSecondaryUseActive() || isIgnoreSneaking) && interactable) {
if (!serverPlayer.isSecondaryUseActive() && interactable) {
return;
}
UseOnContext useOnContext = new UseOnContext(serverPlayer, hand, itemInHand, hitResult);
@@ -316,7 +315,7 @@ public class ItemEventListener implements Listener {
// 执行物品右键事件
if (hasCustomItem) {
// 要求服务端侧这个方块不可交互,或玩家处于潜行状态
if ((serverPlayer.isSecondaryUseActive() && !InteractUtils.isIgnoreSneaking(player, blockData, hitResult, itemInHand)) || !InteractUtils.isInteractable(player, blockData, hitResult, itemInHand)) {
if (serverPlayer.isSecondaryUseActive() || !InteractUtils.isInteractable(player, blockData, hitResult, itemInHand)) {
Cancellable dummy = Cancellable.dummy();
PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
.withParameter(DirectContextParameters.BLOCK, new BukkitExistingBlock(block))

View File

@@ -10,6 +10,8 @@ public final class MBlocks {
public static final Object AIR$defaultState;
public static final Object STONE;
public static final Object STONE$defaultState;
public static final Object BEDROCK;
public static final Object OBSIDIAN;
public static final Object FIRE;
public static final Object SOUL_FIRE;
public static final Object ICE;
@@ -22,6 +24,8 @@ public final class MBlocks {
public static final Object WATER$defaultState;
public static final Object TNT;
public static final Object TNT$defaultState;
public static final Object LODESTONE;
public static final Object END_PORTAL_FRAME;
private static Object getById(String id) {
Object rl = FastNMS.INSTANCE.method$ResourceLocation$fromNamespaceAndPath("minecraft", id);
@@ -35,6 +39,8 @@ public final class MBlocks {
SOUL_FIRE = getById("soul_fire");
STONE = getById("stone");
STONE$defaultState = FastNMS.INSTANCE.method$Block$defaultState(STONE);
BEDROCK = getById("bedrock");
OBSIDIAN = getById("obsidian");
ICE = getById("ice");
SHORT_GRASS = getById(VersionHelper.isOrAbove1_20_3() ? "short_grass" : "grass");
SHORT_GRASS$defaultState = FastNMS.INSTANCE.method$Block$defaultState(SHORT_GRASS);
@@ -45,5 +51,7 @@ public final class MBlocks {
WATER$defaultState = FastNMS.INSTANCE.method$Block$defaultState(WATER);
TNT = getById("tnt");
TNT$defaultState = FastNMS.INSTANCE.method$Block$defaultState(TNT);
LODESTONE = getById("lodestone");
END_PORTAL_FRAME = getById("end_portal_frame");
}
}

View File

@@ -3,7 +3,10 @@ package net.momirealms.craftengine.bukkit.util;
import io.papermc.paper.entity.Shearable;
import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
import net.momirealms.craftengine.bukkit.item.behavior.BlockItemBehavior;
import net.momirealms.craftengine.bukkit.item.behavior.EnderEyeItemBehavior;
import net.momirealms.craftengine.bukkit.item.behavior.FlintAndSteelItemBehavior;
import net.momirealms.craftengine.bukkit.item.recipe.BukkitRecipeManager;
import net.momirealms.craftengine.bukkit.world.BukkitExistingBlock;
import net.momirealms.craftengine.bukkit.world.BukkitWorld;
import net.momirealms.craftengine.core.block.BlockKeys;
import net.momirealms.craftengine.core.entity.EntityTypeKeys;
@@ -16,16 +19,17 @@ import net.momirealms.craftengine.core.item.recipe.UniqueIdItem;
import net.momirealms.craftengine.core.item.recipe.input.SingleItemInput;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.util.Direction;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.QuadFunction;
import net.momirealms.craftengine.core.util.TriFunction;
import net.momirealms.craftengine.core.util.*;
import net.momirealms.craftengine.core.world.BlockHitResult;
import net.momirealms.craftengine.core.world.BlockPos;
import net.momirealms.craftengine.core.world.ExistingBlock;
import org.bukkit.*;
import org.bukkit.DyeColor;
import org.bukkit.GameMode;
import org.bukkit.Registry;
import org.bukkit.World;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Levelled;
import org.bukkit.block.data.Lightable;
@@ -42,7 +46,6 @@ import java.util.Optional;
public final class InteractUtils {
private static final Map<Key, QuadFunction<Player, Item<ItemStack>, BlockData, BlockHitResult, Boolean>> INTERACTIONS = new HashMap<>();
private static final Map<Key, QuadFunction<Player, Item<ItemStack>, BlockData, BlockHitResult, Boolean>> SNEAK_BYPASS = new HashMap<>();
private static final Map<Key, QuadFunction<Player, Item<ItemStack>, BlockData, BlockHitResult, Boolean>> WILL_CONSUME = new HashMap<>();
private static final Map<Key, TriFunction<Player, Entity, @Nullable Item<ItemStack>, Boolean>> ENTITY_INTERACTIONS = new HashMap<>();
@@ -61,63 +64,6 @@ public final class InteractUtils {
Key id = item.vanillaId();
return ItemKeys.BUCKET.equals(id);
});
// 自然方块
registerInteraction(BlockKeys.OBSIDIAN, (player, item, blockState, result) -> {
Key id = item.vanillaId();
if (ItemKeys.END_CRYSTAL.equals(id)) {
World world = player.getWorld();
BukkitWorld bukkitWorld = new BukkitWorld(world);
BlockPos blockPos = result.getBlockPos();
BlockPos abovePos = blockPos.relative(Direction.UP);
ExistingBlock aboveBlock = bukkitWorld.getBlockAt(abovePos);
if (!aboveBlock.id().equals(BlockKeys.AIR)) {
return false;
} else {
return true;
// TODO 需要判定区域内无任何实体
// double x = aboveBlock.x();
// double y = aboveBlock.y();
// double z = aboveBlock.z();
// Object serverWorld = bukkitWorld.serverWorld();
// AABB aabb = new AABB(x, y, z, x + 1.0, y + 2.0, z + 1.0);
// Object nmsAABB = FastNMS.INSTANCE.constructor$AABB(
// aabb.minX, aabb.minY, aabb.minZ,
// aabb.maxX, aabb.maxY, aabb.maxZ
// );
// boolean hasCollision = FastNMS.INSTANCE.checkEntityCollision(serverWorld, List.of(nmsAABB), x + 0.5, y + 1.0, z + 0.5);
// return !hasCollision;
}
}
return false;
});
registerInteraction(BlockKeys.BEDROCK, (player, item, blockState, result) -> {
Key id = item.vanillaId();
if (ItemKeys.END_CRYSTAL.equals(id)) {
World world = player.getWorld();
BukkitWorld bukkitWorld = new BukkitWorld(world);
BlockPos blockPos = result.getBlockPos();
BlockPos abovePos = blockPos.relative(Direction.UP);
ExistingBlock aboveBlock = bukkitWorld.getBlockAt(abovePos);
if (!aboveBlock.id().equals(BlockKeys.AIR)) {
return false;
} else {
return true;
// TODO 需要判定区域内无任何实体
// double x = aboveBlock.x();
// double y = aboveBlock.y();
// double z = aboveBlock.z();
// Object serverWorld = bukkitWorld.serverWorld();
// AABB aabb = new AABB(x, y, z, x + 1.0, y + 2.0, z + 1.0);
// Object nmsAABB = FastNMS.INSTANCE.constructor$AABB(
// aabb.minX, aabb.minY, aabb.minZ,
// aabb.maxX, aabb.maxY, aabb.maxZ
// );
// boolean hasCollision = FastNMS.INSTANCE.checkEntityCollision(serverWorld, List.of(nmsAABB), x + 0.5, y + 1.0, z + 0.5);
// return !hasCollision;
}
}
return false;
});
// 功能方块
registerInteraction(BlockKeys.CRAFTING_TABLE, (player, item, blockState, result) -> true);
registerInteraction(BlockKeys.STONECUTTER, (player, item, blockState, result) -> true);
@@ -190,10 +136,6 @@ public final class InteractUtils {
return false;
});
registerInteraction(BlockKeys.BEACON, (player, item, blockState, result) -> true);
registerInteraction(BlockKeys.LODESTONE, (player, item, blockState, result) -> {
Key id = item.vanillaId();
return ItemKeys.COMPASS.equals(id);
});
registerInteraction(BlockKeys.BEE_NEST, (player, item, blockState, result) -> {
if (blockState instanceof Beehive beehive && beehive.getHoneyLevel() == beehive.getMaximumHoneyLevel()) {
Key id = item.vanillaId();
@@ -226,9 +168,11 @@ public final class InteractUtils {
});
registerInteraction(BlockKeys.DRAGON_EGG, (player, item, blockState, result) -> true);
registerInteraction(BlockKeys.END_PORTAL_FRAME, (player, item, blockState, result) -> {
if (blockState instanceof EndPortalFrame endPortalFrame && !endPortalFrame.hasEye()) {
Key id = item.vanillaId();
return ItemKeys.ENDER_EYE.equals(id);
Optional<List<ItemBehavior>> behaviors = item.getItemBehavior();
if (behaviors.isPresent()) {
for (ItemBehavior behavior : behaviors.get()) {
if (behavior instanceof EnderEyeItemBehavior) return true;
}
}
return false;
});
@@ -242,7 +186,49 @@ public final class InteractUtils {
return id.asString().endsWith("_spawn_egg");
});
// 红石方块
registerInteraction(BlockKeys.REDSTONE_WIRE, (player, item, blockState, result) -> true);
registerInteraction(BlockKeys.REDSTONE_WIRE, (player, item, blockState, result) -> {
if (blockState instanceof RedstoneWire redstoneWire) {
boolean isCross = redstoneWire.getFace(BlockFace.EAST).equals(RedstoneWire.Connection.SIDE)
&& redstoneWire.getFace(BlockFace.NORTH).equals(RedstoneWire.Connection.SIDE)
&& redstoneWire.getFace(BlockFace.SOUTH).equals(RedstoneWire.Connection.SIDE)
&& redstoneWire.getFace(BlockFace.WEST).equals(RedstoneWire.Connection.SIDE);
boolean isDot = redstoneWire.getFace(BlockFace.EAST).equals(RedstoneWire.Connection.NONE)
&& redstoneWire.getFace(BlockFace.NORTH).equals(RedstoneWire.Connection.NONE)
&& redstoneWire.getFace(BlockFace.SOUTH).equals(RedstoneWire.Connection.NONE)
&& redstoneWire.getFace(BlockFace.WEST).equals(RedstoneWire.Connection.NONE);
if (isCross || isDot) {
BlockPos blockPos = result.getBlockPos();
BukkitWorld bukkitWorld = new BukkitWorld(player.getWorld());
World world = bukkitWorld.platformWorld();
Direction[] directions = {Direction.EAST, Direction.WEST, Direction.SOUTH, Direction.NORTH};
for (Direction direction : directions) {
BlockPos neighborPos = blockPos.relative(direction);
Block neighborBlock = world.getBlockAt(neighborPos.x(), neighborPos.y(), neighborPos.z());
Key neighborBlockKey = new BukkitExistingBlock(neighborBlock).id();
BlockData neighborBlockData = neighborBlock.getBlockData();
boolean canConnection = ArrayUtils.contains(BlockKeys.REDSTONE_CONNECTION, neighborBlockKey)
|| ArrayUtils.contains(BlockKeys.PRESSURE_PLATES, neighborBlockKey)
|| ArrayUtils.contains(BlockKeys.BUTTONS, neighborBlockKey);
if (canConnection) {
return switch (neighborBlockData) {
case Repeater repeater -> {
Direction neighborDirection = DirectionUtils.toDirection(repeater.getFacing());
yield !(neighborDirection == direction || neighborDirection == direction.opposite());
}
case Observer observer -> {
Direction neighborDirection = DirectionUtils.toDirection(observer.getFacing());
yield !(neighborDirection == direction);
}
default -> false;
};
}
}
return true;
}
}
return false;
});
registerInteraction(BlockKeys.REPEATER, (player, item, blockState, result) -> true);
registerInteraction(BlockKeys.COMPARATOR, (player, item, blockState, result) -> true);
registerInteraction(BlockKeys.LEVER, (player, item, blockState, result) -> true);
@@ -252,8 +238,13 @@ public final class InteractUtils {
registerInteraction(BlockKeys.CRAFTER, (player, item, blockState, result) -> true);
registerInteraction(BlockKeys.HOPPER, (player, item, blockState, result) -> true);
registerInteraction(BlockKeys.TNT, (player, item, blockState, result) -> {
Key id = item.vanillaId();
return ItemKeys.FLINT_AND_STEEL.equals(id);
Optional<List<ItemBehavior>> behaviors = item.getItemBehavior();
if (behaviors.isPresent()) {
for (ItemBehavior behavior : behaviors.get()) {
if (behavior instanceof FlintAndSteelItemBehavior) return true;
}
}
return false;
});
registerInteraction(BlockKeys.REDSTONE_ORE, (player, item, blockState, result) -> {
Optional<List<ItemBehavior>> behaviors = item.getItemBehavior();
@@ -727,21 +718,6 @@ public final class InteractUtils {
registerInteraction(BlockKeys.WARPED_WALL_HANGING_SIGN, (player, item, blockState, result) -> true);
}
// 忽略潜行
static {
registerSneakBypass(BlockKeys.LODESTONE, (player, item, blockState, result) -> {
Key id = item.vanillaId();
return ItemKeys.COMPASS.equals(id);
});
registerSneakBypass(BlockKeys.END_PORTAL_FRAME, (player, item, blockState, result) -> {
if (blockState instanceof EndPortalFrame endPortalFrame && !endPortalFrame.hasEye()) {
Key id = item.vanillaId();
return ItemKeys.ENDER_EYE.equals(id);
}
return false;
});
}
// 消耗
static {
registerWillConsume(BlockKeys.CACTUS, (player, item, blockState, result) -> {
@@ -794,7 +770,7 @@ public final class InteractUtils {
registerEntityInteraction(EntityTypeKeys.SHEEP, (player, entity, item) -> {
Key id = item.vanillaId();
if (entity instanceof Sheep sheep && sheep.readyToBeSheared() && id.in(ItemKeys.DYES)) {
if (entity instanceof Sheep sheep && sheep.readyToBeSheared() && ArrayUtils.contains(ItemKeys.DYES, item)) {
DyeColor sheepColor = sheep.getColor();
if (sheepColor != null) {
String color = sheepColor.name().toLowerCase();
@@ -820,12 +796,17 @@ public final class InteractUtils {
});
registerEntityInteraction(EntityTypeKeys.CREEPER, (player, entity, item) -> {
Key id = item.vanillaId();
return canBeFeed(entity, item) || ItemKeys.FLINT_AND_STEEL.equals(id);
Optional<List<ItemBehavior>> behaviors = item.getItemBehavior();
if (behaviors.isPresent()) {
for (ItemBehavior behavior : behaviors.get()) {
if (behavior instanceof FlintAndSteelItemBehavior) return true;
}
}
return false;
});
registerEntityInteraction(EntityTypeKeys.PIGLIN, (player, entity, item) -> {
Key id = item.vanillaId();
return canBeFeed(entity, item) || ItemKeys.GOLD_INGOT.equals(id);
return ItemKeys.GOLD_INGOT.equals(id);
});
registerEntityInteraction(EntityTypeKeys.ARMADILLO, (player, entity, item) -> {
Key id = item.vanillaId();
@@ -954,13 +935,6 @@ public final class InteractUtils {
}
}
private static void registerSneakBypass(Key key, QuadFunction<org.bukkit.entity.Player, Item<ItemStack>, BlockData, BlockHitResult, Boolean> function) {
var previous = SNEAK_BYPASS.put(key, function);
if (previous != null) {
CraftEngine.instance().logger().warn("Duplicated interaction check: " + key);
}
}
private static void registerWillConsume(Key key, QuadFunction<org.bukkit.entity.Player, Item<ItemStack>, BlockData, BlockHitResult, Boolean> function) {
var previous = WILL_CONSUME.put(key, function);
if (previous != null) {
@@ -983,14 +957,6 @@ public final class InteractUtils {
return false;
}
public static boolean isIgnoreSneaking(Player player, BlockData state, BlockHitResult hit, @Nullable Item<ItemStack> item) {
Key blockType = BlockStateUtils.getBlockOwnerIdFromData(state);
if (SNEAK_BYPASS.containsKey(blockType)) {
return SNEAK_BYPASS.get(blockType).apply(player, item, state, hit);
}
return false;
}
public static boolean willConsume(Player player, BlockData state, BlockHitResult hit, @Nullable Item<ItemStack> item) {
if (item == null) return false;
Key blockType = BlockStateUtils.getBlockOwnerIdFromData(state);

View File

@@ -1,27 +1,36 @@
package net.momirealms.craftengine.bukkit.world;
import com.google.common.collect.Lists;
import net.momirealms.craftengine.bukkit.entity.BukkitEntity;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
import net.momirealms.craftengine.bukkit.util.*;
import net.momirealms.craftengine.core.block.BlockStateWrapper;
import net.momirealms.craftengine.core.entity.Entity;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.plugin.context.Context;
import net.momirealms.craftengine.core.sound.SoundSource;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.craftengine.core.world.*;
import net.momirealms.craftengine.core.world.collision.AABB;
import net.momirealms.craftengine.core.world.particle.ParticleData;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.SoundCategory;
import org.bukkit.entity.EnderDragonPart;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.BoundingBox;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.nio.file.Path;
import java.util.List;
import java.util.UUID;
import java.util.function.Predicate;
public class BukkitWorld implements World {
private final WeakReference<org.bukkit.World> world;
@@ -54,6 +63,49 @@ public class BukkitWorld implements World {
return new BukkitExistingBlock(platformWorld().getBlockAt(x, y, z));
}
@Override
public List<Entity> getEntities(@Nullable Entity entity, AABB aabb, Predicate<? super Entity> predicate) {
List<Entity> entities = Lists.newArrayList();
Object nmsAABB = FastNMS.INSTANCE.constructor$AABB(
aabb.minX, aabb.minY, aabb.minZ,
aabb.maxX, aabb.maxY, aabb.maxZ
);
BoundingBox bukkitAABB = new BoundingBox(
aabb.minX, aabb.minY, aabb.minZ,
aabb.maxX, aabb.maxY, aabb.maxZ
);
Object level = serverWorld();
int entityCount = FastNMS.INSTANCE.method$EntityGetter$getEntitiesOfClass(level, nmsAABB, CoreReflections.clazz$Entity);
if (entityCount == 0) {
return entities;
}
for (org.bukkit.entity.Entity bukkitEntity : platformWorld().getNearbyEntities(bukkitAABB)) {
if (bukkitEntity instanceof org.bukkit.entity.EnderDragonPart) {
continue;
}
Entity craftEngineEntity = new BukkitEntity(bukkitEntity);
if (!craftEngineEntity.equals(entity) && predicate.test(craftEngineEntity)) {
entities.add(craftEngineEntity);
}
}
for (EnderDragonPart dragonPart : platformWorld().getEntitiesByClass(EnderDragonPart.class)) {
org.bukkit.util.BoundingBox partBoundingBox = dragonPart.getBoundingBox();
boolean intersects = aabb.maxX >= partBoundingBox.getMinX() &&
aabb.minX <= partBoundingBox.getMaxX() &&
aabb.maxY >= partBoundingBox.getMinY() &&
aabb.minY <= partBoundingBox.getMaxY() &&
aabb.maxZ >= partBoundingBox.getMinZ() &&
aabb.minZ <= partBoundingBox.getMaxZ();
if (!intersects) continue;
Entity craftEnginePart = new BukkitEntity(dragonPart);
Entity craftEngineParent = new BukkitEntity(dragonPart.getParent());
if (!craftEnginePart.equals(entity) && !craftEngineParent.equals(entity) && predicate.test(craftEnginePart)) {
entities.add(craftEnginePart);
}
}
return entities;
}
@Override
public String name() {
return platformWorld().getName();