From 1da10934451a2f08105e68127727cfeac8e59676 Mon Sep 17 00:00:00 2001 From: halogly Date: Fri, 5 Sep 2025 15:44:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A8=8D=E5=BE=AE=E5=87=8F=E5=B0=91=E5=86=97?= =?UTF-8?q?=E4=BD=99=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../item/behavior/EndCrystalItemBehavior.java | 44 ++++++++++------ .../craftengine/bukkit/world/BukkitWorld.java | 52 ------------------- .../craftengine/core/world/World.java | 17 ------ 3 files changed, 29 insertions(+), 84 deletions(-) diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/EndCrystalItemBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/EndCrystalItemBehavior.java index 7b959ccf3..050b334f3 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/EndCrystalItemBehavior.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/EndCrystalItemBehavior.java @@ -1,5 +1,6 @@ package net.momirealms.craftengine.bukkit.item.behavior; +import net.momirealms.craftengine.bukkit.entity.BukkitEntity; import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBlocks; import net.momirealms.craftengine.bukkit.util.BlockStateUtils; @@ -14,9 +15,13 @@ 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.GameMode; import org.bukkit.block.data.BlockData; +import org.bukkit.entity.Player; +import org.bukkit.util.BoundingBox; import java.nio.file.Path; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -43,26 +48,35 @@ public class EndCrystalItemBehavior extends ItemBehavior { 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 entities = world.getEntities(null, aabb); - if (!entities.isEmpty()) { - return InteractionResult.PASS; - } + Object level = world.serverWorld(); + org.bukkit.World bukkitWorld = (org.bukkit.World) world.platformWorld(); - List aabbs = List.of(aabb); - List 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; + AABB aabb = new AABB(x, y, z, x + 1.0, y + 2.0, z + 1.0); + BoundingBox bukkitAABB = new BoundingBox(aabb.minX, aabb.minY, aabb.minZ, aabb.maxX, aabb.maxY, aabb.maxZ); + // 忽略旁观者 + List entities = new ArrayList<>(); + for (org.bukkit.entity.Entity bukkitEntity : bukkitWorld.getNearbyEntities(bukkitAABB)) { + if (bukkitEntity instanceof Player player && player.getGameMode().equals(GameMode.SPECTATOR)) { + continue; + } + Entity entity = new BukkitEntity(bukkitEntity); + entities.add(entity); + } + // 检测家具实体 + List detectionAABBs = List.of(aabb); + List nmsAABBs = detectionAABBs.stream() + .map(aabbs -> FastNMS.INSTANCE.constructor$AABB(aabbs.minX, aabbs.minY, aabbs.minZ, aabbs.maxX, aabbs.maxY, aabbs.maxZ)) + .toList(); + boolean hasEntities = FastNMS.INSTANCE.checkEntityCollision(level, nmsAABBs, x + 0.5, y, z + 0.5); + + if (!entities.isEmpty() || !hasEntities) { + return InteractionResult.PASS; + } else { + return InteractionResult.SUCCESS; } - return InteractionResult.SUCCESS; } } } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitWorld.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitWorld.java index f7c2f3d9e..bfb04f654 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitWorld.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitWorld.java @@ -1,36 +1,27 @@ 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 world; @@ -63,49 +54,6 @@ public class BukkitWorld implements World { return new BukkitExistingBlock(platformWorld().getBlockAt(x, y, z)); } - @Override - public List getEntities(@Nullable Entity entity, AABB aabb, Predicate predicate) { - List 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(); diff --git a/core/src/main/java/net/momirealms/craftengine/core/world/World.java b/core/src/main/java/net/momirealms/craftengine/core/world/World.java index 6dbd9cce0..a7d960454 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/world/World.java +++ b/core/src/main/java/net/momirealms/craftengine/core/world/World.java @@ -1,22 +1,17 @@ package net.momirealms.craftengine.core.world; import net.momirealms.craftengine.core.block.BlockStateWrapper; -import net.momirealms.craftengine.core.entity.Entity; -import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.item.Item; import net.momirealms.craftengine.core.plugin.context.Context; import net.momirealms.craftengine.core.sound.SoundData; import net.momirealms.craftengine.core.sound.SoundSource; import net.momirealms.craftengine.core.util.Key; -import net.momirealms.craftengine.core.world.collision.AABB; import net.momirealms.craftengine.core.world.particle.ParticleData; import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; import java.nio.file.Path; -import java.util.List; import java.util.UUID; -import java.util.function.Predicate; public interface World { @@ -34,18 +29,6 @@ public interface World { void setBlockAt(int x, int y, int z, BlockStateWrapper blockState, int flags); - List getEntities(@Nullable Entity entity, AABB aabb, Predicate predicate); - - default List getEntities(@Nullable Entity entity, AABB aabb) { - Predicate noSpectator = player -> { - if (player instanceof Player) { - return !((Player) player).isSpectatorMode(); - } - return true; - }; - return this.getEntities(entity, aabb, noSpectator); - } - String name(); Path directory();