9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-27 10:59:07 +00:00

稍微减少冗余代码

This commit is contained in:
halogly
2025-09-05 15:44:48 +08:00
parent 2ab8ffe7f9
commit 1da1093445
3 changed files with 29 additions and 84 deletions

View File

@@ -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<Entity> 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<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;
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<Entity> 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<AABB> detectionAABBs = List.of(aabb);
List<Object> 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;
}
}
}

View File

@@ -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<org.bukkit.World> world;
@@ -63,49 +54,6 @@ 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();

View File

@@ -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<Entity> getEntities(@Nullable Entity entity, AABB aabb, Predicate<? super Entity> predicate);
default List<Entity> getEntities(@Nullable Entity entity, AABB aabb) {
Predicate<Entity> noSpectator = player -> {
if (player instanceof Player) {
return !((Player) player).isSpectatorMode();
}
return true;
};
return this.getEntities(entity, aabb, noSpectator);
}
String name();
Path directory();