9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-30 04:19:27 +00:00

提取获取实体类型方法

This commit is contained in:
XiaoMoMi
2025-07-26 17:43:38 +08:00
parent 14c30982f7
commit 3b5a2855ab
2 changed files with 11 additions and 7 deletions

View File

@@ -2,6 +2,8 @@ package net.momirealms.craftengine.bukkit.util;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBuiltInRegistries;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.craftengine.core.world.BlockPos;
import org.bukkit.Location;
@@ -35,4 +37,11 @@ public class EntityUtils {
return LegacyEntityUtils.spawnEntity(world, loc, type, function);
}
}
public static Key getEntityType(Entity entity) {
Object nmsEntity = FastNMS.INSTANCE.method$CraftEntity$getHandle(entity);
Object entityType = FastNMS.INSTANCE.method$Entity$getType(nmsEntity);
Object id = FastNMS.INSTANCE.method$Registry$getKey(MBuiltInRegistries.ENTITY_TYPE, entityType);
return KeyUtils.resourceLocationToKey(id);
}
}

View File

@@ -4,8 +4,6 @@ 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.recipe.BukkitRecipeManager;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBuiltInRegistries;
import net.momirealms.craftengine.core.block.BlockKeys;
import net.momirealms.craftengine.core.entity.EntityTypeKeys;
import net.momirealms.craftengine.core.item.Item;
@@ -485,10 +483,7 @@ public class InteractUtils {
}
public static boolean isEntityInteractable(Player player, Entity entity, @Nullable Item<ItemStack> item) {
Object nmsEntity = FastNMS.INSTANCE.method$CraftEntity$getHandle(entity);
Object entityType = FastNMS.INSTANCE.method$Entity$getType(nmsEntity);
Object id = FastNMS.INSTANCE.method$Registry$getKey(MBuiltInRegistries.ENTITY_TYPE, entityType);
TriFunction<Player, Entity, Item<ItemStack>, Boolean> func = ENTITY_INTERACTIONS.get(KeyUtils.resourceLocationToKey(id));
TriFunction<Player, Entity, Item<ItemStack>, Boolean> func = ENTITY_INTERACTIONS.get(EntityUtils.getEntityType(entity));
return func != null && func.apply(player, entity, item);
}
@@ -507,7 +502,7 @@ public class InteractUtils {
}
private static boolean canFeed(Entity entity, Item<ItemStack> item) {
return entity instanceof Animals animals && item.is(Key.of(animals.getType().toString().toLowerCase() + "_food"));
return entity instanceof Animals && item.is(Key.of(EntityUtils.getEntityType(entity).value() + "_food"));
}
private static boolean hasSaddle(Player player, Entity entity) {