Implement new API

This commit is contained in:
fullwall 2016-08-08 17:26:27 +08:00
parent e607244a5f
commit c0a3f0b1de
6 changed files with 38 additions and 29 deletions

View File

@ -6,7 +6,6 @@
<formats> <formats>
<format>jar</format> <format>jar</format>
</formats> </formats>
<includeBaseDirectory>false</includeBaseDirectory> <includeBaseDirectory>false</includeBaseDirectory>
<dependencySets> <dependencySets>

View File

@ -37,6 +37,7 @@ import net.citizensnpcs.api.astar.pathfinder.PathPoint.PathCallback;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
public class CitizensNavigator implements Navigator, Runnable { public class CitizensNavigator implements Navigator, Runnable {
private final NavigatorParameters defaultParams = new NavigatorParameters().baseSpeed(UNINITIALISED_SPEED) private final NavigatorParameters defaultParams = new NavigatorParameters().baseSpeed(UNINITIALISED_SPEED)
@ -152,6 +153,9 @@ public class CitizensNavigator implements Navigator, Runnable {
updatePathfindingRange(); updatePathfindingRange();
boolean finished = executing.update(); boolean finished = executing.update();
if (localParams.lookAtFunction() != null) {
Util.faceLocation(npc.getEntity(), localParams.lookAtFunction().apply(this), true);
}
if (!finished) { if (!finished) {
return; return;
} }

View File

@ -56,16 +56,15 @@ class ProfileFetchThread implements Runnable {
queue.add(request); queue.add(request);
requested.put(name, request); requested.put(name, request);
return; return;
} } else if (request.getResult() == ProfileFetchResult.TOO_MANY_REQUESTS) {
else if (request.getResult() == ProfileFetchResult.TOO_MANY_REQUESTS) {
queue.add(request); queue.add(request);
} }
} }
if (handler != null) { if (handler != null) {
if (request.getResult() == ProfileFetchResult.PENDING || if (request.getResult() == ProfileFetchResult.PENDING
request.getResult() == ProfileFetchResult.TOO_MANY_REQUESTS) { || request.getResult() == ProfileFetchResult.TOO_MANY_REQUESTS) {
addHandler(request, handler); addHandler(request, handler);
} else { } else {
sendResult(handler, request); sendResult(handler, request);

View File

@ -8,6 +8,7 @@ import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.mojang.authlib.Agent; import com.mojang.authlib.Agent;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import com.mojang.authlib.GameProfileRepository; import com.mojang.authlib.GameProfileRepository;
@ -41,8 +42,7 @@ public class ProfileFetcher {
int i = 0; int i = 0;
for (ProfileRequest request : requests) { for (ProfileRequest request : requests) {
playerNames[i] = request.getPlayerName(); playerNames[i++] = request.getPlayerName();
i++;
} }
repo.findProfilesByNames(playerNames, Agent.MINECRAFT, new ProfileLookupCallback() { repo.findProfilesByNames(playerNames, Agent.MINECRAFT, new ProfileLookupCallback() {
@ -50,7 +50,8 @@ public class ProfileFetcher {
public void onProfileLookupFailed(GameProfile profile, Exception e) { public void onProfileLookupFailed(GameProfile profile, Exception e) {
if (Messaging.isDebugging()) { if (Messaging.isDebugging()) {
Messaging.debug( Messaging.debug(
"Profile lookup for player '" + profile.getName() + "' failed: " + getExceptionMsg(e)); "Profile lookup for player '" + profile.getName() + "' failed2: " + getExceptionMsg(e));
Messaging.debug(Throwables.getStackTraceAsString(e));
} }
ProfileRequest request = findRequest(profile.getName(), requests); ProfileRequest request = findRequest(profile.getName(), requests);
@ -76,12 +77,13 @@ public class ProfileFetcher {
if (request == null) if (request == null)
return; return;
try { try {
request.setResult(NMS.fillProfileProperties(profile, true), ProfileFetchResult.SUCCESS); request.setResult(NMS.fillProfileProperties(profile, true), ProfileFetchResult.SUCCESS);
} catch (Exception e) { } catch (Exception e) {
if (Messaging.isDebugging()) { if (Messaging.isDebugging()) {
Messaging.debug( Messaging.debug(
"Profile lookup for player '" + profile.getName() + "' failed: " + getExceptionMsg(e)); "Profile lookup for player '" + profile.getName() + "' failed2: " + getExceptionMsg(e));
Messaging.debug(Throwables.getStackTraceAsString(e));
} }
if (isTooManyRequests(e)) { if (isTooManyRequests(e)) {
@ -124,9 +126,7 @@ public class ProfileFetcher {
} }
private static String getExceptionMsg(Exception e) { private static String getExceptionMsg(Exception e) {
String message = e.getMessage(); return Throwables.getRootCause(e).getMessage();
String cause = e.getCause() != null ? e.getCause().getMessage() : null;
return cause != null ? cause : message;
} }
private static void initThread() { private static void initThread() {

View File

@ -23,17 +23,6 @@ public class Util {
private Util() { private Util() {
} }
public static float clampYaw(float yaw) {
while (yaw < -180.0F) {
yaw += 360.0F;
}
while (yaw >= 180.0F) {
yaw -= 360.0F;
}
return yaw;
}
public static void assumePose(Entity entity, float yaw, float pitch) { public static void assumePose(Entity entity, float yaw, float pitch) {
NMS.look(entity, yaw, pitch); NMS.look(entity, yaw, pitch);
} }
@ -51,14 +40,29 @@ public class Util {
return event; return event;
} }
public static float clampYaw(float yaw) {
while (yaw < -180.0F) {
yaw += 360.0F;
}
while (yaw >= 180.0F) {
yaw -= 360.0F;
}
return yaw;
}
public static void faceEntity(Entity entity, Entity at) { public static void faceEntity(Entity entity, Entity at) {
if (entity.getWorld() != at.getWorld()) if (at == null || entity == null || entity.getWorld() != at.getWorld())
return; return;
faceLocation(entity, at.getLocation(AT_LOCATION)); faceLocation(entity, at.getLocation(AT_LOCATION));
} }
public static void faceLocation(Entity entity, Location to) { public static void faceLocation(Entity entity, Location to) {
if (entity.getWorld() != to.getWorld()) faceLocation(entity, to, false);
}
public static void faceLocation(Entity entity, Location to, boolean headOnly) {
if (to == null || entity.getWorld() != to.getWorld())
return; return;
Location fromLocation = entity.getLocation(FROM_LOCATION); Location fromLocation = entity.getLocation(FROM_LOCATION);
double xDiff, yDiff, zDiff; double xDiff, yDiff, zDiff;
@ -74,7 +78,11 @@ public class Util {
if (zDiff < 0.0) if (zDiff < 0.0)
yaw += Math.abs(180 - yaw) * 2; yaw += Math.abs(180 - yaw) * 2;
NMS.look(entity, (float) yaw - 90, (float) pitch); if (headOnly) {
NMS.setHeadYaw(entity, (float) yaw - 90);
} else {
NMS.look(entity, (float) yaw - 90, (float) pitch);
}
} }
public static Location getEyeLocation(Entity entity) { public static Location getEyeLocation(Entity entity) {

View File

@ -471,8 +471,7 @@ public class NMSImpl implements NMSBridge {
public TargetNavigator getTargetNavigator(org.bukkit.entity.Entity entity, org.bukkit.entity.Entity target, public TargetNavigator getTargetNavigator(org.bukkit.entity.Entity entity, org.bukkit.entity.Entity target,
NavigatorParameters parameters) { NavigatorParameters parameters) {
NavigationAbstract navigation = getNavigation(entity); NavigationAbstract navigation = getNavigation(entity);
return navigation == null ? null return navigation == null ? null : new NavigationFieldWrapper(navigation, target, parameters);
: new NavigationFieldWrapper(navigation, target, parameters);
} }
@Override @Override