Bukkit插件开发教程 - Entity(实体)

2018-04-10  本文已影响0人  Zoyn_

实体 - Entity

学习目标

了解Entity的定义

实体是Entity包括在Minecraft中所有动态的、移动中的对象
所有的实体都有以下性质:

了解部分实体的生成

在BukkitAPI中,我们可以通过调用World类下的几个spawn方法进行生成

spawn(Location location, java.lang.Class<T> clazz)
在指定的位置根据给定的类生成一个实体.
spawnEntity(Location loc, EntityType type)
在指定的位置创建一个实体.
spawnArrow(Location location, Vector direction, float speed, float spread)
在指定的位置创建一个箭的实体.
spawnFallingBlock(Location location, MaterialData data)
在指定的位置创建一个指定Material的坠落方块

在上方的代码中第2和第3不做相关解释

范例: 使用spawn方法生成一头牛

// 假设现在的需求是在玩家身旁生成一头牛
World world = player.getWorld();
world.spawn(player.getLocation(), Cow.class);

范例: 使用spawnEntity方法生成一头牛

// 假设现在的需求是在玩家身旁生成一头牛
World world = player.getWorld();
world.spawnEntity(player.getLocation(), EntityType.COW);

在上方的范例中,第二种方法较为常用

在有些时候我们会发现有些实体的头上会有一些名字,这是怎么做到的呢?
范例: 使用spawnEntity方法生成一头牛, 并为其命名

// 假设现在的需求是在玩家身旁生成一头牛
World world = player.getWorld();
// 因为spawnEntity方法会返回该实体的实例对象, 而Cow类继承了Entity所以需要强转
Cow cow = (Cow) world.spawnEntity(player.getLocation(), EntityType.COW);
// 设置牛的自定义名的可见性
cow.setCustomNameVisible(true);
// 设置牛的自定义名
cow.setCustomName("§6一头奶牛");
上一篇下一篇

猜你喜欢

热点阅读