From 954a3bd27b94de3998cf4ea79570dc976d0ab249 Mon Sep 17 00:00:00 2001 From: fullwall Date: Wed, 17 Dec 2014 19:48:47 +0800 Subject: [PATCH] Add some settings to control NPC skin retries --- src/main/java/net/citizensnpcs/Settings.java | 2 + .../npc/entity/HumanController.java | 42 +++++++++++-------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/citizensnpcs/Settings.java b/src/main/java/net/citizensnpcs/Settings.java index c43400db2..6cab44ad5 100644 --- a/src/main/java/net/citizensnpcs/Settings.java +++ b/src/main/java/net/citizensnpcs/Settings.java @@ -91,12 +91,14 @@ public class Settings { KEEP_CHUNKS_LOADED("npc.chunks.always-keep-loaded", false), LOCALE("general.translation.locale", ""), MAX_NPC_LIMIT_CHECKS("npc.limits.max-permission-checks", 100), + MAX_NPC_SKIN_RETRIES("npc.skins.max-retries", -1), MAX_SPEED("npc.limits.max-speed", 100), MAX_TEXT_RANGE("npc.chat.options.max-text-range", 500), MESSAGE_COLOUR("general.color-scheme.message", ""), NEW_PATHFINDER_OPENS_DOORS("npc.pathfinding.new-finder-open-doors", false), NPC_ATTACK_DISTANCE("npc.pathfinding.attack-range", 1.75 * 1.75), NPC_COST("economy.npc.cost", 100D), + NPC_SKIN_RETRY_DELAY("npc.skins.retry-delay", 120), PACKET_UPDATE_DELAY("npc.packets.update-delay", 30), QUICK_SELECT("npc.selection.quick-select", false), REMOVE_PLAYERS_FROM_PLAYER_LIST("npc.player.remove-from-list", true), diff --git a/src/main/java/net/citizensnpcs/npc/entity/HumanController.java b/src/main/java/net/citizensnpcs/npc/entity/HumanController.java index 4674813ef..9b19d258c 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/HumanController.java +++ b/src/main/java/net/citizensnpcs/npc/entity/HumanController.java @@ -173,7 +173,7 @@ public class HumanController extends AbstractEntityController { if (cached != null) { if (Messaging.isDebugging()) { Messaging - .debug("Using cached skin texture for NPC " + npc.getName() + " UUID " + npc.getUniqueId()); + .debug("Using cached skin texture for NPC " + npc.getName() + " UUID " + npc.getUniqueId()); } skinProfile = new GameProfile(UUID.fromString(realUUID), ""); skinProfile.getProperties().put("textures", cached); @@ -185,7 +185,7 @@ public class HumanController extends AbstractEntityController { } catch (Exception e) { if ((e.getMessage() != null && e.getMessage().contains("too many requests")) || (e.getCause() != null && e.getCause().getMessage() != null && e.getCause().getMessage() - .contains("too many requests"))) { + .contains("too many requests"))) { SKIN_THREAD.delay(); SKIN_THREAD.addRunnable(this); } @@ -219,6 +219,7 @@ public class HumanController extends AbstractEntityController { public static class SkinThread implements Runnable { private volatile int delay = 0; + private volatile int retryTimes = 0; private final BlockingDeque tasks = new LinkedBlockingDeque(); public void addRunnable(Runnable r) { @@ -232,13 +233,18 @@ public class HumanController extends AbstractEntityController { } public void delay() { - delay = 120; // need to wait a minute before Mojang accepts API - // calls again + delay = Setting.NPC_SKIN_RETRY_DELAY.asInt(); + // need to wait before Mojang accepts API calls again + retryTimes++; + if (Setting.MAX_NPC_SKIN_RETRIES.asInt() >= 0 && retryTimes > Setting.MAX_NPC_SKIN_RETRIES.asInt()) { + tasks.clear(); + retryTimes = 0; + } } @Override public void run() { - if (delay != 0) { + if (delay > 0) { delay--; return; } @@ -275,21 +281,21 @@ public class HumanController extends AbstractEntityController { .getGameProfileRepository(); repo.findProfilesByNames(new String[] { ChatColor.stripColor(reportedUUID) }, Agent.MINECRAFT, new ProfileLookupCallback() { - @Override - public void onProfileLookupFailed(GameProfile arg0, Exception arg1) { - } + @Override + public void onProfileLookupFailed(GameProfile arg0, Exception arg1) { + } - @Override - public void onProfileLookupSucceeded(final GameProfile profile) { - UUID_CACHE.put(reportedUUID, profile.getId().toString()); - if (Messaging.isDebugging()) { - Messaging.debug("Fetched UUID " + profile.getId() + " for NPC " + npc.getName() + @Override + public void onProfileLookupSucceeded(final GameProfile profile) { + UUID_CACHE.put(reportedUUID, profile.getId().toString()); + if (Messaging.isDebugging()) { + Messaging.debug("Fetched UUID " + profile.getId() + " for NPC " + npc.getName() + " UUID " + npc.getUniqueId()); - } - npc.data().setPersistent(CACHED_SKIN_UUID_METADATA, profile.getId().toString()); - npc.data().setPersistent(CACHED_SKIN_UUID_NAME_METADATA, profile.getName()); - } - }); + } + npc.data().setPersistent(CACHED_SKIN_UUID_METADATA, profile.getId().toString()); + npc.data().setPersistent(CACHED_SKIN_UUID_NAME_METADATA, profile.getName()); + } + }); return npc.data().get(CACHED_SKIN_UUID_METADATA, reportedUUID); } }