194 words
1 minutes
17 实现对实体的交互
17 实现对实体的交互
EntityInteract事件是PlayerInteractEvent的一个子事件,当玩家对实体右键时候回进行调用.
@EventBusSubscriber
public class UseEntityHandler {
@SubscribeEvent
public static void useEntity(PlayerInteractEvent.EntityInteract event){
InteractionHand hand = event.getHand();
Level level = event.getLevel();
Player player = event.getEntity();
Entity target = event.getTarget();
interact(player,level,hand, target);
}
public static void interact(Player player, Level world, InteractionHand hand, Entity entity) {
if(Config.isQinNa()) { // 我们在这里判断是否是对应的实体,然后消失该实体,并在实体位置掉落物品.
if (entity instanceof Llama && !world.isClientSide()) {//羊驼
world.playSound(null, player.getX(), player.getY(), player.getZ(),
SoundEvents.LLAMA_HURT, SoundSource.NEUTRAL, 0.5f, 0.4f / (world.getRandom().nextFloat() * 0.4f + 0.8f));
entity.spawnAtLocation(ModItems.LLAMA_ITEM);
entity.remove(Entity.RemovalReason.DISCARDED);
}
if (entity instanceof Villager && !world.isClientSide()) {//村民
world.playSound(null, player.getX(), player.getY(), player.getZ(),
SoundEvents.VILLAGER_HURT, SoundSource.NEUTRAL, 0.5f, 0.4f / (world.getRandom().nextFloat() * 0.4f + 0.8f));
entity.spawnAtLocation(ModItems.VILLAGER_ITEM);
entity.remove(Entity.RemovalReason.DISCARDED);
}
if (entity instanceof IronGolem && !world.isClientSide()) {//铁傀儡
world.playSound(null, player.getX(), player.getY(), player.getZ(),
SoundEvents.IRON_GOLEM_HURT, SoundSource.NEUTRAL, 0.5f, 0.4f / (world.getRandom().nextFloat() * 0.4f + 0.8f));
entity.spawnAtLocation(ModItems.IRON_GOLEM_ITEM);
entity.remove(Entity.RemovalReason.DISCARDED);
}
}
}
}