More version refactoring

This commit is contained in:
fullwall 2016-07-13 20:43:54 +08:00
parent 547cdb132d
commit 986a94674a
4 changed files with 83 additions and 61 deletions

View File

@ -1,7 +1,7 @@
package net.citizensnpcs.npc.ai;
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.TargetType;
@ -10,11 +10,10 @@ import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_10_R1.EntityHorse;
import net.minecraft.server.v1_10_R1.EntityLiving;
import net.minecraft.server.v1_10_R1.NavigationAbstract;
public class MCNavigationStrategy extends AbstractPathStrategy {
private final EntityLiving handle;
private final Entity handle;
private float lastSpeed;
private final NavigationAbstract navigation;
private final NavigatorParameters parameters;
@ -25,26 +24,27 @@ public class MCNavigationStrategy extends AbstractPathStrategy {
this.target = dest;
this.parameters = params;
this.lastSpeed = parameters.speed();
handle = ((CraftLivingEntity) npc.getEntity()).getHandle();
handle.onGround = true;
handle = npc.getEntity();
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
// navigation won't execute, and calling entity.move doesn't
// entirely fix the problem.
navigation = NMS.getNavigation(handle);
float oldWidth = handle.width;
if (handle instanceof EntityHorse) {
handle.width = Math.min(0.99f, oldWidth);
navigation = NMS.getNavigation(npc.getEntity());
float oldWidth = raw.width;
if (raw instanceof EntityHorse) {
raw.width = Math.min(0.99f, oldWidth);
}
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
// prefer to make it just fit on 1 so hack around it a bit.
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.
if (NMS.isNavigationFinished(navigation)) {
setCancelReason(CancelReason.STUCK);
}
}
private double distanceSquared() {
return handle.getBukkitEntity().getLocation(HANDLE_LOCATION).distanceSquared(target);
return handle.getLocation(HANDLE_LOCATION).distanceSquared(target);
}
@Override

View File

@ -1,7 +1,7 @@
package net.citizensnpcs.npc.ai;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
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.event.CancelReason;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.util.BoundingBox;
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;
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) {
this.npc = npc;
this.parameters = params;
this.handle = ((CraftEntity) npc.getEntity()).getHandle();
this.target = ((CraftEntity) target).getHandle();
NavigationAbstract nav = NMS.getNavigation(this.handle);
this.handle = npc.getEntity();
this.target = target;
NavigationAbstract nav = NMS.getNavigation(npc.getEntity());
this.targetNavigator = nav != null && !params.useNewPathfinder() ? new NavigationFieldWrapper(nav)
: new AStarTargeter();
this.aggro = aggro;
}
private boolean canAttack() {
return attackTicks == 0
&& (handle.getBoundingBox().e > target.getBoundingBox().b
&& handle.getBoundingBox().b < target.getBoundingBox().e)
BoundingBox handleBB = NMS.getBoundingBox(handle), targetBB = NMS.getBoundingBox(target);
return attackTicks == 0 && (handleBB.maxY > targetBB.minY && handleBB.minY < targetBB.maxY)
&& closeEnough(distanceSquared()) && hasLineOfSight();
}
@ -56,8 +52,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
}
private double distanceSquared() {
return handle.getBukkitEntity().getLocation(HANDLE_LOCATION)
.distanceSquared(target.getBukkitEntity().getLocation(TARGET_LOCATION));
return handle.getLocation(HANDLE_LOCATION).distanceSquared(target.getLocation(TARGET_LOCATION));
}
@Override
@ -67,7 +62,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
@Override
public org.bukkit.entity.Entity getTarget() {
return target.getBukkitEntity();
return target;
}
@Override
@ -81,7 +76,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
}
private boolean hasLineOfSight() {
return ((LivingEntity) handle.getBukkitEntity()).hasLineOfSight(target.getBukkitEntity());
return ((LivingEntity) handle).hasLineOfSight(target);
}
@Override
@ -101,11 +96,11 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
@Override
public boolean update() {
if (target == null || !target.getBukkitEntity().isValid()) {
if (target == null || !target.isValid()) {
cancelReason = CancelReason.TARGET_DIED;
return true;
}
if (target.world != handle.world) {
if (target.getWorld() != handle.getWorld()) {
cancelReason = CancelReason.TARGET_MOVED_WORLD;
return true;
}
@ -123,11 +118,9 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
NMS.look(handle, target);
if (aggro && canAttack()) {
AttackStrategy strategy = parameters.attackStrategy();
if (strategy != null
&& strategy.handle((LivingEntity) handle.getBukkitEntity(), (LivingEntity) getTarget())) {
if (strategy != null && strategy.handle((LivingEntity) handle, (LivingEntity) getTarget())) {
} else if (strategy != parameters.defaultAttackStrategy()) {
parameters.defaultAttackStrategy().handle((LivingEntity) handle.getBukkitEntity(),
(LivingEntity) getTarget());
parameters.defaultAttackStrategy().handle((LivingEntity) handle, (LivingEntity) getTarget());
}
attackTicks = parameters.attackDelayTicks();
}
@ -162,7 +155,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
}
private void setStrategy() {
Location location = parameters.entityTargetLocationMapper().apply(target.getBukkitEntity());
Location location = parameters.entityTargetLocationMapper().apply(target);
if (location == null) {
throw new IllegalStateException("mapper should not return null");
}
@ -190,18 +183,14 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
@Override
public void setPath() {
Location location = parameters.entityTargetLocationMapper().apply(target.getBukkitEntity());
Location location = parameters.entityTargetLocationMapper().apply(target);
if (location == null) {
throw new IllegalStateException("mapper should not return null");
}
double oldX = target.locX, oldY = target.locY, oldZ = target.locZ;
target.locX = location.getX();
target.locY = location.getY();
target.locZ = location.getZ();
navigation.a(target, parameters.speed());
target.locX = oldX;
target.locY = oldY;
target.locZ = oldZ;
Location oldLoc = target.getLocation(HANDLE_LOCATION);
target.teleport(location);
NMS.setNavigationTarget(handle, target, parameters.speed());
target.teleport(oldLoc);
}
@Override
@ -226,15 +215,7 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
static final AttackStrategy DEFAULT_ATTACK_STRATEGY = new AttackStrategy() {
@Override
public boolean handle(LivingEntity attacker, LivingEntity bukkitTarget) {
EntityLiving handle = NMS.getHandle(attacker);
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);
}
NMS.attack(attacker, bukkitTarget);
return false;
}
};

View 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;
}
}

View File

@ -142,6 +142,12 @@ public class NMS {
}
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);
float f = (float) (attackDamage == null ? 1 : attackDamage.getValue());
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) {
getHandle(entity).world = ((CraftWorld) world).getHandle();
}
@ -410,6 +420,11 @@ public class NMS {
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) {
Entity vehicle = getVehicle(entity);
return vehicle == null ? null : vehicle.getBukkitEntity();
@ -466,7 +481,8 @@ public class NMS {
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()
: handle instanceof EntityHumanNPC ? ((EntityHumanNPC) handle).getNavigation() : null;
}
@ -601,14 +617,6 @@ public class NMS {
((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) {
Entity handle = getHandle(entity);
if (handle == null)
@ -619,6 +627,15 @@ public class NMS {
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")
public static void minecartItemLogic(EntityMinecartAbstract minecart) {
NPC npc = ((NPCHolder) minecart).getNPC();
@ -837,6 +854,11 @@ public class NMS {
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) {
if (SKULL_PROFILE_FIELD == null) {
try {