前言
测试使用时用的是spigot1.16.5的api。
此文章仅展示一些我平时会写的用法。
0.前置接口
1 2 3
| public interface SphereExecute { void execute(Location center,Location loc); }
|
1 2 3
| public interface EntitiesAtExecute { void execute(Entity target); }
|
1 2 3 4 5 6
| public enum MovementDirection { FORWARD, BACKWARD, LEFT, RIGHT, }
|
1.治愈玩家
根据玩家最大血量治愈百分比血量方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public void healEntity(Entity entity, double percentage) { if (!(entity instanceof LivingEntity)) { return; }
LivingEntity livingEntity = (LivingEntity) entity; double maxHealth = livingEntity.getMaxHealth(); double healAmount = maxHealth * percentage / 100;
double currentHealth = livingEntity.getHealth(); double newHealth = Math.min(currentHealth + healAmount, maxHealth);
livingEntity.setHealth(newHealth); }
|
entity
所需要治愈的实体
percentage
治愈百分比0-100
2.获取周围实体
获取指定位置附加的实体,并进行指定的方法处理
1 2 3 4 5 6 7 8
| public void EntitiesAtLocation(Location location, double radius, EntitiesAtExecute entitiesAtExecute) { for (Entity entity : location.getWorld().getNearbyEntities(location, radius, radius, radius)) { if (entity instanceof LivingEntity) { LivingEntity target = (LivingEntity) entity; entitiesAtExecute.execute(target); } } }
|
location
需要获取的中心位置
radius
中心位置向外的半径
entitiesAtExecute
可自定义的方法
3.对实体施加力
使实体前后左右移动的方法,或者说是施加一个力让玩家弹射出去
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public void movePlayer(Entity player, double distance, double extraForce, MovementDirection direction) { Vector playerDirection = player.getLocation().getDirection().normalize(); playerDirection.setY(0); playerDirection.multiply(distance);
Vector velocity = player.getVelocity(); switch (direction) { case FORWARD: velocity.add(playerDirection.multiply(extraForce)); break; case BACKWARD: playerDirection.multiply(-1); velocity.add(playerDirection.multiply(extraForce)); break; case LEFT: Vector left = new Vector(-playerDirection.getZ(), 0, playerDirection.getX()); playerDirection.add(left); velocity.add(playerDirection.multiply(extraForce)); break; case RIGHT: Vector right = new Vector(playerDirection.getZ(), 0, -playerDirection.getX()); playerDirection.add(right); velocity.add(playerDirection.multiply(extraForce)); break; } player.setVelocity(velocity); }
|
player
所需要对其操作的实体
distance
移动的力
extraForce
额外施加的力
direction
方向
如果想让实体向上的话可以使用这个这个:
1 2 3 4 5
| public void UPlaunchEntity(Entity entity, double power) { Vector velocity = entity.getVelocity(); velocity.setY(power); entity.setVelocity(velocity); }
|
4.指定实体位置,吸收周围实体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public void attractEntities(Entity entity, double attractionRange, double attractionStrength) { Location entityLocation = entity.getLocation();
for (Entity nearbyEntity : entity.getNearbyEntities(attractionRange, attractionRange, attractionRange)) { if (nearbyEntity.equals(entity)) { continue; }
Location nearbyLocation = nearbyEntity.getLocation(); Vector direction = entityLocation.toVector().subtract(nearbyLocation.toVector()).normalize();
Vector attractionForce = direction.multiply(attractionStrength);
nearbyEntity.setVelocity(attractionForce); } }
|
entity
中心实体
attractionRange
吸收半径
attractionStrength
吸收强度