mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-02-23 07:41:29 +01:00
More version refactoring
This commit is contained in:
parent
547cdb132d
commit
986a94674a
@ -1,7 +1,7 @@
|
|||||||
package net.citizensnpcs.npc.ai;
|
package net.citizensnpcs.npc.ai;
|
||||||
|
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.craftbukkit.v1_10_R1.entity.CraftLivingEntity;
|
import org.bukkit.entity.Entity;
|
||||||
|
|
||||||
import net.citizensnpcs.api.ai.NavigatorParameters;
|
import net.citizensnpcs.api.ai.NavigatorParameters;
|
||||||
import net.citizensnpcs.api.ai.TargetType;
|
import net.citizensnpcs.api.ai.TargetType;
|
||||||
@ -10,11 +10,10 @@ import net.citizensnpcs.api.npc.NPC;
|
|||||||
import net.citizensnpcs.api.util.Messaging;
|
import net.citizensnpcs.api.util.Messaging;
|
||||||
import net.citizensnpcs.util.NMS;
|
import net.citizensnpcs.util.NMS;
|
||||||
import net.minecraft.server.v1_10_R1.EntityHorse;
|
import net.minecraft.server.v1_10_R1.EntityHorse;
|
||||||
import net.minecraft.server.v1_10_R1.EntityLiving;
|
|
||||||
import net.minecraft.server.v1_10_R1.NavigationAbstract;
|
import net.minecraft.server.v1_10_R1.NavigationAbstract;
|
||||||
|
|
||||||
public class MCNavigationStrategy extends AbstractPathStrategy {
|
public class MCNavigationStrategy extends AbstractPathStrategy {
|
||||||
private final EntityLiving handle;
|
private final Entity handle;
|
||||||
private float lastSpeed;
|
private float lastSpeed;
|
||||||
private final NavigationAbstract navigation;
|
private final NavigationAbstract navigation;
|
||||||
private final NavigatorParameters parameters;
|
private final NavigatorParameters parameters;
|
||||||
@ -25,26 +24,27 @@ public class MCNavigationStrategy extends AbstractPathStrategy {
|
|||||||
this.target = dest;
|
this.target = dest;
|
||||||
this.parameters = params;
|
this.parameters = params;
|
||||||
this.lastSpeed = parameters.speed();
|
this.lastSpeed = parameters.speed();
|
||||||
handle = ((CraftLivingEntity) npc.getEntity()).getHandle();
|
handle = npc.getEntity();
|
||||||
handle.onGround = true;
|
net.minecraft.server.v1_10_R1.Entity raw = NMS.getHandle(handle);
|
||||||
|
raw.onGround = true;
|
||||||
// not sure of a better way around this - if onGround is false, then
|
// not sure of a better way around this - if onGround is false, then
|
||||||
// navigation won't execute, and calling entity.move doesn't
|
// navigation won't execute, and calling entity.move doesn't
|
||||||
// entirely fix the problem.
|
// entirely fix the problem.
|
||||||
navigation = NMS.getNavigation(handle);
|
navigation = NMS.getNavigation(npc.getEntity());
|
||||||
float oldWidth = handle.width;
|
float oldWidth = raw.width;
|
||||||
if (handle instanceof EntityHorse) {
|
if (raw instanceof EntityHorse) {
|
||||||
handle.width = Math.min(0.99f, oldWidth);
|
raw.width = Math.min(0.99f, oldWidth);
|
||||||
}
|
}
|
||||||
navigation.a(dest.getX(), dest.getY(), dest.getZ(), parameters.speed());
|
navigation.a(dest.getX(), dest.getY(), dest.getZ(), parameters.speed());
|
||||||
handle.width = oldWidth; // minecraft requires that an entity fit onto both blocks if width >= 1f, but we'd
|
raw.width = oldWidth; // minecraft requires that an entity fit onto both blocks if width >= 1f, but we'd
|
||||||
// prefer to make it just fit on 1 so hack around it a bit.
|
// prefer to make it just fit on 1 so hack around it a bit.
|
||||||
if (NMS.isNavigationFinished(navigation)) {
|
if (NMS.isNavigationFinished(navigation)) {
|
||||||
setCancelReason(CancelReason.STUCK);
|
setCancelReason(CancelReason.STUCK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private double distanceSquared() {
|
private double distanceSquared() {
|
||||||
return handle.getBukkitEntity().getLocation(HANDLE_LOCATION).distanceSquared(target);
|
return handle.getLocation(HANDLE_LOCATION).distanceSquared(target);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package net.citizensnpcs.npc.ai;
|
package net.citizensnpcs.npc.ai;
|
||||||
|
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity;
|
import org.bukkit.entity.Entity;
|
||||||
import org.bukkit.entity.LivingEntity;
|
import org.bukkit.entity.LivingEntity;
|
||||||
|
|
||||||
import net.citizensnpcs.api.ai.AttackStrategy;
|
import net.citizensnpcs.api.ai.AttackStrategy;
|
||||||
@ -10,11 +10,8 @@ import net.citizensnpcs.api.ai.NavigatorParameters;
|
|||||||
import net.citizensnpcs.api.ai.TargetType;
|
import net.citizensnpcs.api.ai.TargetType;
|
||||||
import net.citizensnpcs.api.ai.event.CancelReason;
|
import net.citizensnpcs.api.ai.event.CancelReason;
|
||||||
import net.citizensnpcs.api.npc.NPC;
|
import net.citizensnpcs.api.npc.NPC;
|
||||||
|
import net.citizensnpcs.util.BoundingBox;
|
||||||
import net.citizensnpcs.util.NMS;
|
import net.citizensnpcs.util.NMS;
|
||||||
import net.citizensnpcs.util.PlayerAnimation;
|
|
||||||
import net.minecraft.server.v1_10_R1.Entity;
|
|
||||||
import net.minecraft.server.v1_10_R1.EntityLiving;
|
|
||||||
import net.minecraft.server.v1_10_R1.EntityPlayer;
|
|
||||||
import net.minecraft.server.v1_10_R1.NavigationAbstract;
|
import net.minecraft.server.v1_10_R1.NavigationAbstract;
|
||||||
|
|
||||||
public class MCTargetStrategy implements PathStrategy, EntityTarget {
|
public class MCTargetStrategy implements PathStrategy, EntityTarget {
|
||||||
@ -31,18 +28,17 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
|
|||||||
public MCTargetStrategy(NPC npc, org.bukkit.entity.Entity target, boolean aggro, NavigatorParameters params) {
|
public MCTargetStrategy(NPC npc, org.bukkit.entity.Entity target, boolean aggro, NavigatorParameters params) {
|
||||||
this.npc = npc;
|
this.npc = npc;
|
||||||
this.parameters = params;
|
this.parameters = params;
|
||||||
this.handle = ((CraftEntity) npc.getEntity()).getHandle();
|
this.handle = npc.getEntity();
|
||||||
this.target = ((CraftEntity) target).getHandle();
|
this.target = target;
|
||||||
NavigationAbstract nav = NMS.getNavigation(this.handle);
|
NavigationAbstract nav = NMS.getNavigation(npc.getEntity());
|
||||||
this.targetNavigator = nav != null && !params.useNewPathfinder() ? new NavigationFieldWrapper(nav)
|
this.targetNavigator = nav != null && !params.useNewPathfinder() ? new NavigationFieldWrapper(nav)
|
||||||
: new AStarTargeter();
|
: new AStarTargeter();
|
||||||
this.aggro = aggro;
|
this.aggro = aggro;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean canAttack() {
|
private boolean canAttack() {
|
||||||
return attackTicks == 0
|
BoundingBox handleBB = NMS.getBoundingBox(handle), targetBB = NMS.getBoundingBox(target);
|
||||||
&& (handle.getBoundingBox().e > target.getBoundingBox().b
|
return attackTicks == 0 && (handleBB.maxY > targetBB.minY && handleBB.minY < targetBB.maxY)
|
||||||
&& handle.getBoundingBox().b < target.getBoundingBox().e)
|
|
||||||
&& closeEnough(distanceSquared()) && hasLineOfSight();
|
&& closeEnough(distanceSquared()) && hasLineOfSight();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,8 +52,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private double distanceSquared() {
|
private double distanceSquared() {
|
||||||
return handle.getBukkitEntity().getLocation(HANDLE_LOCATION)
|
return handle.getLocation(HANDLE_LOCATION).distanceSquared(target.getLocation(TARGET_LOCATION));
|
||||||
.distanceSquared(target.getBukkitEntity().getLocation(TARGET_LOCATION));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -67,7 +62,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public org.bukkit.entity.Entity getTarget() {
|
public org.bukkit.entity.Entity getTarget() {
|
||||||
return target.getBukkitEntity();
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -81,7 +76,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasLineOfSight() {
|
private boolean hasLineOfSight() {
|
||||||
return ((LivingEntity) handle.getBukkitEntity()).hasLineOfSight(target.getBukkitEntity());
|
return ((LivingEntity) handle).hasLineOfSight(target);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -101,11 +96,11 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean update() {
|
public boolean update() {
|
||||||
if (target == null || !target.getBukkitEntity().isValid()) {
|
if (target == null || !target.isValid()) {
|
||||||
cancelReason = CancelReason.TARGET_DIED;
|
cancelReason = CancelReason.TARGET_DIED;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (target.world != handle.world) {
|
if (target.getWorld() != handle.getWorld()) {
|
||||||
cancelReason = CancelReason.TARGET_MOVED_WORLD;
|
cancelReason = CancelReason.TARGET_MOVED_WORLD;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -123,11 +118,9 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
|
|||||||
NMS.look(handle, target);
|
NMS.look(handle, target);
|
||||||
if (aggro && canAttack()) {
|
if (aggro && canAttack()) {
|
||||||
AttackStrategy strategy = parameters.attackStrategy();
|
AttackStrategy strategy = parameters.attackStrategy();
|
||||||
if (strategy != null
|
if (strategy != null && strategy.handle((LivingEntity) handle, (LivingEntity) getTarget())) {
|
||||||
&& strategy.handle((LivingEntity) handle.getBukkitEntity(), (LivingEntity) getTarget())) {
|
|
||||||
} else if (strategy != parameters.defaultAttackStrategy()) {
|
} else if (strategy != parameters.defaultAttackStrategy()) {
|
||||||
parameters.defaultAttackStrategy().handle((LivingEntity) handle.getBukkitEntity(),
|
parameters.defaultAttackStrategy().handle((LivingEntity) handle, (LivingEntity) getTarget());
|
||||||
(LivingEntity) getTarget());
|
|
||||||
}
|
}
|
||||||
attackTicks = parameters.attackDelayTicks();
|
attackTicks = parameters.attackDelayTicks();
|
||||||
}
|
}
|
||||||
@ -162,7 +155,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setStrategy() {
|
private void setStrategy() {
|
||||||
Location location = parameters.entityTargetLocationMapper().apply(target.getBukkitEntity());
|
Location location = parameters.entityTargetLocationMapper().apply(target);
|
||||||
if (location == null) {
|
if (location == null) {
|
||||||
throw new IllegalStateException("mapper should not return null");
|
throw new IllegalStateException("mapper should not return null");
|
||||||
}
|
}
|
||||||
@ -190,18 +183,14 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setPath() {
|
public void setPath() {
|
||||||
Location location = parameters.entityTargetLocationMapper().apply(target.getBukkitEntity());
|
Location location = parameters.entityTargetLocationMapper().apply(target);
|
||||||
if (location == null) {
|
if (location == null) {
|
||||||
throw new IllegalStateException("mapper should not return null");
|
throw new IllegalStateException("mapper should not return null");
|
||||||
}
|
}
|
||||||
double oldX = target.locX, oldY = target.locY, oldZ = target.locZ;
|
Location oldLoc = target.getLocation(HANDLE_LOCATION);
|
||||||
target.locX = location.getX();
|
target.teleport(location);
|
||||||
target.locY = location.getY();
|
NMS.setNavigationTarget(handle, target, parameters.speed());
|
||||||
target.locZ = location.getZ();
|
target.teleport(oldLoc);
|
||||||
navigation.a(target, parameters.speed());
|
|
||||||
target.locX = oldX;
|
|
||||||
target.locY = oldY;
|
|
||||||
target.locZ = oldZ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -226,15 +215,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
|
|||||||
static final AttackStrategy DEFAULT_ATTACK_STRATEGY = new AttackStrategy() {
|
static final AttackStrategy DEFAULT_ATTACK_STRATEGY = new AttackStrategy() {
|
||||||
@Override
|
@Override
|
||||||
public boolean handle(LivingEntity attacker, LivingEntity bukkitTarget) {
|
public boolean handle(LivingEntity attacker, LivingEntity bukkitTarget) {
|
||||||
EntityLiving handle = NMS.getHandle(attacker);
|
NMS.attack(attacker, bukkitTarget);
|
||||||
EntityLiving target = NMS.getHandle(bukkitTarget);
|
|
||||||
if (handle instanceof EntityPlayer) {
|
|
||||||
EntityPlayer humanHandle = (EntityPlayer) handle;
|
|
||||||
humanHandle.attack(target);
|
|
||||||
PlayerAnimation.ARM_SWING.play(humanHandle.getBukkitEntity());
|
|
||||||
} else {
|
|
||||||
NMS.attack(handle, target);
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
19
src/main/java/net/citizensnpcs/util/BoundingBox.java
Normal file
19
src/main/java/net/citizensnpcs/util/BoundingBox.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package net.citizensnpcs.util;
|
||||||
|
|
||||||
|
public class BoundingBox {
|
||||||
|
public final double maxX;
|
||||||
|
public final double maxY;
|
||||||
|
public final double maxZ;
|
||||||
|
public final double minX;
|
||||||
|
public final double minY;
|
||||||
|
public final double minZ;
|
||||||
|
|
||||||
|
public BoundingBox(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
|
||||||
|
this.minX = minX;
|
||||||
|
this.minY = minY;
|
||||||
|
this.minZ = minZ;
|
||||||
|
this.maxX = maxX;
|
||||||
|
this.maxY = maxY;
|
||||||
|
this.maxZ = maxZ;
|
||||||
|
}
|
||||||
|
}
|
@ -142,6 +142,12 @@ public class NMS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void attack(EntityLiving handle, Entity target) {
|
public static void attack(EntityLiving handle, Entity target) {
|
||||||
|
if (handle instanceof EntityPlayer) {
|
||||||
|
EntityPlayer humanHandle = (EntityPlayer) handle;
|
||||||
|
humanHandle.attack(target);
|
||||||
|
PlayerAnimation.ARM_SWING.play(humanHandle.getBukkitEntity());
|
||||||
|
return;
|
||||||
|
}
|
||||||
AttributeInstance attackDamage = handle.getAttributeInstance(GenericAttributes.ATTACK_DAMAGE);
|
AttributeInstance attackDamage = handle.getAttributeInstance(GenericAttributes.ATTACK_DAMAGE);
|
||||||
float f = (float) (attackDamage == null ? 1 : attackDamage.getValue());
|
float f = (float) (attackDamage == null ? 1 : attackDamage.getValue());
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@ -169,6 +175,10 @@ public class NMS {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void attack(LivingEntity attacker, LivingEntity bukkitTarget) {
|
||||||
|
attack(NMS.getHandle(attacker), NMS.getHandle(bukkitTarget));
|
||||||
|
}
|
||||||
|
|
||||||
public static void changeWorlds(org.bukkit.entity.Entity entity, org.bukkit.World world) {
|
public static void changeWorlds(org.bukkit.entity.Entity entity, org.bukkit.World world) {
|
||||||
getHandle(entity).world = ((CraftWorld) world).getHandle();
|
getHandle(entity).world = ((CraftWorld) world).getHandle();
|
||||||
}
|
}
|
||||||
@ -410,6 +420,11 @@ public class NMS {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static BoundingBox getBoundingBox(org.bukkit.entity.Entity handle) {
|
||||||
|
AxisAlignedBB bb = NMS.getHandle(handle).getBoundingBox();
|
||||||
|
return new BoundingBox(bb.a, bb.b, bb.c, bb.d, bb.e, bb.f);
|
||||||
|
}
|
||||||
|
|
||||||
public static org.bukkit.entity.Entity getBukkitVehicle(org.bukkit.entity.Entity entity) {
|
public static org.bukkit.entity.Entity getBukkitVehicle(org.bukkit.entity.Entity entity) {
|
||||||
Entity vehicle = getVehicle(entity);
|
Entity vehicle = getVehicle(entity);
|
||||||
return vehicle == null ? null : vehicle.getBukkitEntity();
|
return vehicle == null ? null : vehicle.getBukkitEntity();
|
||||||
@ -466,7 +481,8 @@ public class NMS {
|
|||||||
return handle.bg;
|
return handle.bg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NavigationAbstract getNavigation(Entity handle) {
|
public static NavigationAbstract getNavigation(org.bukkit.entity.Entity entity) {
|
||||||
|
Entity handle = NMS.getHandle(entity);
|
||||||
return handle instanceof EntityInsentient ? ((EntityInsentient) handle).getNavigation()
|
return handle instanceof EntityInsentient ? ((EntityInsentient) handle).getNavigation()
|
||||||
: handle instanceof EntityHumanNPC ? ((EntityHumanNPC) handle).getNavigation() : null;
|
: handle instanceof EntityHumanNPC ? ((EntityHumanNPC) handle).getNavigation() : null;
|
||||||
}
|
}
|
||||||
@ -601,14 +617,6 @@ public class NMS {
|
|||||||
((CraftServer) Bukkit.getServer()).enablePlugins(PluginLoadOrder.POSTWORLD);
|
((CraftServer) Bukkit.getServer()).enablePlugins(PluginLoadOrder.POSTWORLD);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void look(Entity handle, Entity target) {
|
|
||||||
if (handle instanceof EntityInsentient) {
|
|
||||||
((EntityInsentient) handle).getControllerLook().a(target, 10.0F, ((EntityInsentient) handle).N());
|
|
||||||
} else if (handle instanceof EntityHumanNPC) {
|
|
||||||
((EntityHumanNPC) handle).setTargetLook(target, 10F, 40F);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void look(org.bukkit.entity.Entity entity, float yaw, float pitch) {
|
public static void look(org.bukkit.entity.Entity entity, float yaw, float pitch) {
|
||||||
Entity handle = getHandle(entity);
|
Entity handle = getHandle(entity);
|
||||||
if (handle == null)
|
if (handle == null)
|
||||||
@ -619,6 +627,15 @@ public class NMS {
|
|||||||
handle.pitch = pitch;
|
handle.pitch = pitch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void look(org.bukkit.entity.Entity bhandle, org.bukkit.entity.Entity btarget) {
|
||||||
|
Entity handle = NMS.getHandle(bhandle), target = NMS.getHandle(btarget);
|
||||||
|
if (handle instanceof EntityInsentient) {
|
||||||
|
((EntityInsentient) handle).getControllerLook().a(target, 10.0F, ((EntityInsentient) handle).N());
|
||||||
|
} else if (handle instanceof EntityHumanNPC) {
|
||||||
|
((EntityHumanNPC) handle).setTargetLook(target, 10F, 40F);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public static void minecartItemLogic(EntityMinecartAbstract minecart) {
|
public static void minecartItemLogic(EntityMinecartAbstract minecart) {
|
||||||
NPC npc = ((NPCHolder) minecart).getNPC();
|
NPC npc = ((NPCHolder) minecart).getNPC();
|
||||||
@ -837,6 +854,11 @@ public class NMS {
|
|||||||
handle.getAttributeInstance(GenericAttributes.c).setValue(d);
|
handle.getAttributeInstance(GenericAttributes.c).setValue(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setNavigationTarget(org.bukkit.entity.Entity handle, org.bukkit.entity.Entity target,
|
||||||
|
float speed) {
|
||||||
|
NMS.getNavigation(handle).a(NMS.getHandle(target), speed);
|
||||||
|
}
|
||||||
|
|
||||||
public static void setProfile(SkullMeta meta, GameProfile profile) {
|
public static void setProfile(SkullMeta meta, GameProfile profile) {
|
||||||
if (SKULL_PROFILE_FIELD == null) {
|
if (SKULL_PROFILE_FIELD == null) {
|
||||||
try {
|
try {
|
||||||
|
Loading…
Reference in New Issue
Block a user