9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-24 17:39:30 +00:00

fix(bukkit): 可以躲避下碰撞实体

This commit is contained in:
jhqwqmc
2025-03-31 20:24:51 +08:00
parent 225da1ca97
commit aaccfc7c48
2 changed files with 35 additions and 0 deletions

View File

@@ -1,8 +1,10 @@
package net.momirealms.craftengine.bukkit.entity.furniture;
import net.momirealms.craftengine.bukkit.nms.CollisionEntity;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
import net.momirealms.craftengine.bukkit.util.EntityUtils;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.core.entity.furniture.*;
import net.momirealms.craftengine.core.loot.LootTable;
import net.momirealms.craftengine.core.pack.Pack;
@@ -388,6 +390,7 @@ public class BukkitFurnitureManager implements FurnitureManager {
int z = location.getBlockZ();
if (!world.getBlockAt(x, y - 1, z).getType().isSolid()) return false;
if (!world.getBlockAt(x, y, z).isPassable()) return false;
if (isEntityBlocking(location)) return false;
return world.getBlockAt(x, y + 1, z).isPassable();
}
@@ -409,4 +412,18 @@ public class BukkitFurnitureManager implements FurnitureManager {
}
return null;
}
private boolean isEntityBlocking(Location location) {
World world = location.getWorld();
if (world == null) return true;
try {
Collection<Entity> nearbyEntities = world.getNearbyEntities(location, 0.5, 2, 0.5);
for (Entity bukkitEntity : nearbyEntities) {
if (bukkitEntity instanceof Player) continue;
Object nmsEntity = Reflections.method$CraftEntity$getHandle.invoke(bukkitEntity);
return (boolean) Reflections.method$Entity$canBeCollidedWith.invoke(nmsEntity);
}
} catch (Exception ignored) {}
return false;
}
}

View File

@@ -5898,4 +5898,22 @@ public class Reflections {
clazz$FriendlyByteBuf, clazz$FriendlyByteBuf, int[].class
)
);
public static final Method method$Entity$canBeCollidedWith = requireNonNull(
VersionHelper.isVersionNewerThan1_20_5()
? ReflectionUtils.getMethod(clazz$Entity, boolean.class, new String[]{"canBeCollidedWith"})
: VersionHelper.isVersionNewerThan1_20_3()
? ReflectionUtils.getMethod(clazz$Entity, boolean.class, new String[]{"bz"})
: VersionHelper.isVersionNewerThan1_20_2()
? ReflectionUtils.getMethod(clazz$Entity, boolean.class, new String[]{"bx"})
: VersionHelper.isVersionNewerThan1_20()
? ReflectionUtils.getMethod(clazz$Entity, boolean.class, new String[]{"bu"})
: ReflectionUtils.getMethod(clazz$Entity, boolean.class, new String[]{"canBeCollidedWith", "bu", "bx", "bz"})
);
public static final Method method$CraftEntity$getHandle = requireNonNull(
ReflectionUtils.getMethod(
clazz$CraftEntity, clazz$Entity, 0
)
);
}