Skip to content

Commit

Permalink
Implement new API
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Aug 8, 2016
1 parent e607244 commit c0a3f0b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 29 deletions.
1 change: 0 additions & 1 deletion dist/src/main/assembly/all-jar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<formats>
<format>jar</format>
</formats>

<includeBaseDirectory>false</includeBaseDirectory>

<dependencySets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;

public class CitizensNavigator implements Navigator, Runnable {
private final NavigatorParameters defaultParams = new NavigatorParameters().baseSpeed(UNINITIALISED_SPEED)
Expand Down Expand Up @@ -152,6 +153,9 @@ public void run() {

updatePathfindingRange();
boolean finished = executing.update();
if (localParams.lookAtFunction() != null) {
Util.faceLocation(npc.getEntity(), localParams.lookAtFunction().apply(this), true);
}
if (!finished) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,15 @@ void fetch(String name, @Nullable ProfileFetchHandler handler) {
queue.add(request);
requested.put(name, request);
return;
}
else if (request.getResult() == ProfileFetchResult.TOO_MANY_REQUESTS) {
} else if (request.getResult() == ProfileFetchResult.TOO_MANY_REQUESTS) {
queue.add(request);
}
}

if (handler != null) {

if (request.getResult() == ProfileFetchResult.PENDING ||
request.getResult() == ProfileFetchResult.TOO_MANY_REQUESTS) {
if (request.getResult() == ProfileFetchResult.PENDING
|| request.getResult() == ProfileFetchResult.TOO_MANY_REQUESTS) {
addHandler(request, handler);
} else {
sendResult(handler, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.scheduler.BukkitTask;

import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.mojang.authlib.Agent;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.GameProfileRepository;
Expand Down Expand Up @@ -41,16 +42,16 @@ void fetchRequests(final Collection<ProfileRequest> requests) {

int i = 0;
for (ProfileRequest request : requests) {
playerNames[i] = request.getPlayerName();
i++;
playerNames[i++] = request.getPlayerName();
}

repo.findProfilesByNames(playerNames, Agent.MINECRAFT, new ProfileLookupCallback() {
@Override
public void onProfileLookupFailed(GameProfile profile, Exception e) {
if (Messaging.isDebugging()) {
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);
Expand All @@ -76,12 +77,13 @@ public void onProfileLookupSucceeded(final GameProfile profile) {
if (request == null)
return;

try {
try {
request.setResult(NMS.fillProfileProperties(profile, true), ProfileFetchResult.SUCCESS);
} catch (Exception e) {
if (Messaging.isDebugging()) {
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)) {
Expand Down Expand Up @@ -124,9 +126,7 @@ private static ProfileRequest findRequest(String name, Collection<ProfileRequest
}

private static String getExceptionMsg(Exception e) {
String message = e.getMessage();
String cause = e.getCause() != null ? e.getCause().getMessage() : null;
return cause != null ? cause : message;
return Throwables.getRootCause(e).getMessage();
}

private static void initThread() {
Expand Down
36 changes: 22 additions & 14 deletions main/src/main/java/net/citizensnpcs/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ public class 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) {
NMS.look(entity, yaw, pitch);
}
Expand All @@ -51,14 +40,29 @@ public static NPCPushEvent callPushEvent(NPC npc, Vector vector) {
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) {
if (entity.getWorld() != at.getWorld())
if (at == null || entity == null || entity.getWorld() != at.getWorld())
return;
faceLocation(entity, at.getLocation(AT_LOCATION));
}

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;
Location fromLocation = entity.getLocation(FROM_LOCATION);
double xDiff, yDiff, zDiff;
Expand All @@ -74,7 +78,11 @@ public static void faceLocation(Entity entity, Location to) {
if (zDiff < 0.0)
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,7 @@ public boolean update() {
public TargetNavigator getTargetNavigator(org.bukkit.entity.Entity entity, org.bukkit.entity.Entity target,
NavigatorParameters parameters) {
NavigationAbstract navigation = getNavigation(entity);
return navigation == null ? null
: new NavigationFieldWrapper(navigation, target, parameters);
return navigation == null ? null : new NavigationFieldWrapper(navigation, target, parameters);
}

@Override
Expand Down

0 comments on commit c0a3f0b

Please sign in to comment.