Skip to content

Commit

Permalink
Force skin updates on /npc skin -p
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed May 20, 2016
1 parent b48cfd5 commit fe72700
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/citizensnpcs/commands/NPCCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ public void skin(final CommandContext args, final CommandSender sender, final NP

SkinnableEntity skinnable = NMS.getSkinnable(npc.getEntity());
if (skinnable != null) {
skinnable.setSkinName(skinName);
skinnable.setSkinName(skinName, args.hasFlag('p'));
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,15 @@ public void setSkinFlags(byte flags) {

@Override
public void setSkinName(String name) {
setSkinName(name, false);
}

@Override
public void setSkinName(String name, boolean forceUpdate) {
Preconditions.checkNotNull(name);

npc.data().setPersistent(NPC.PLAYER_SKIN_UUID_METADATA, name.toLowerCase());
skinTracker.notifySkinChange();
skinTracker.notifySkinChange(forceUpdate);
}

public void setTargetLook(Entity target, float yawOffset, float renderOffset) {
Expand Down Expand Up @@ -455,8 +460,14 @@ public void setSkinFlags(byte flags) {
public void setSkinName(String name) {
((SkinnableEntity) this.entity).setSkinName(name);
}

@Override
public void setSkinName(String skinName, boolean forceUpdate) {
((SkinnableEntity) this.entity).setSkinName(skinName, forceUpdate);
}
}

private static final float EPSILON = 0.005F;

private static final Location LOADED_LOCATION = new Location(null, 0, 0, 0);
}
36 changes: 17 additions & 19 deletions src/main/java/net/citizensnpcs/npc/profile/ProfileFetcher.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.citizensnpcs.npc.profile;

import java.util.Collection;

import javax.annotation.Nullable;

import org.bukkit.Bukkit;
Expand Down Expand Up @@ -45,10 +46,8 @@ void fetchRequests(final Collection<ProfileRequest> requests) {
}

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));
Expand All @@ -69,7 +68,6 @@ public void onProfileLookupFailed(GameProfile profile, Exception e) {

@Override
public void onProfileLookupSucceeded(final GameProfile profile) {

if (Messaging.isDebugging()) {
Messaging.debug("Fetched profile " + profile.getId() + " for player " + profile.getName());
}
Expand All @@ -81,7 +79,6 @@ public void onProfileLookupSucceeded(final GameProfile profile) {
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));
Expand Down Expand Up @@ -114,20 +111,14 @@ public static void fetch(String name, @Nullable ProfileFetchHandler handler) {
PROFILE_THREAD.fetch(name, handler);
}

/**
* Clear all queued and cached requests.
*/
public static void reset() {
initThread();
}

@Nullable
private static ProfileRequest findRequest(String name, Collection<ProfileRequest> requests) {
name = name.toLowerCase();

for (ProfileRequest request : requests) {
if (request.getPlayerName().equals(name))
if (request.getPlayerName().equals(name)) {
return request;
}
}
return null;
}
Expand All @@ -138,6 +129,15 @@ private static String getExceptionMsg(Exception e) {
return cause != null ? cause : message;
}

private static void initThread() {
if (THREAD_TASK != null) {
THREAD_TASK.cancel();
}

PROFILE_THREAD = new ProfileFetchThread();
THREAD_TASK = Bukkit.getScheduler().runTaskTimerAsynchronously(CitizensAPI.getPlugin(), PROFILE_THREAD, 21, 20);
}

private static boolean isProfileNotFound(Exception e) {
String message = e.getMessage();
String cause = e.getCause() != null ? e.getCause().getMessage() : null;
Expand All @@ -155,13 +155,11 @@ private static boolean isTooManyRequests(Exception e) {
|| (cause != null && cause.contains("too many requests"));
}

private static void initThread() {
if (THREAD_TASK != null)
THREAD_TASK.cancel();

PROFILE_THREAD = new ProfileFetchThread();
THREAD_TASK = Bukkit.getScheduler().runTaskTimerAsynchronously(
CitizensAPI.getPlugin(), PROFILE_THREAD, 21, 20);
/**
* Clear all queued and cached requests.
*/
public static void reset() {
initThread();
}

private static ProfileFetchThread PROFILE_THREAD;
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/net/citizensnpcs/npc/skin/Skin.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class Skin {
*
* @param skinName
* The name of the player the skin belongs to.
* @param forceUpdate
*/
Skin(String skinName) {
this.skinName = skinName.toLowerCase();
Expand Down Expand Up @@ -205,6 +206,7 @@ public boolean isValid() {
}

private void setData(@Nullable GameProfile profile) {
System.out.println("SET DATA");
if (profile == null) {
isValid = false;
return;
Expand Down Expand Up @@ -251,10 +253,26 @@ public static void clearCache() {
* The skinnable entity.
*/
public static Skin get(SkinnableEntity entity) {
return get(entity, false);
}

/**
* Get a skin for a skinnable entity.
*
* <p>
* If a Skin instance does not exist, a new one is created and the skin data is automatically fetched.
* </p>
*
* @param entity
* The skinnable entity.
* @param forceUpdate
* if the skin should be checked via the cache
*/
public static Skin get(SkinnableEntity entity, boolean forceUpdate) {
Preconditions.checkNotNull(entity);

String skinName = entity.getSkinName().toLowerCase();
return get(skinName);
return get(skinName, forceUpdate);
}

/**
Expand All @@ -267,7 +285,7 @@ public static Skin get(SkinnableEntity entity) {
* @param skinName
* The name of the skin.
*/
public static Skin get(String skinName) {
public static Skin get(String skinName, boolean forceUpdate) {
Preconditions.checkNotNull(skinName);

skinName = skinName.toLowerCase();
Expand All @@ -279,6 +297,8 @@ public static Skin get(String skinName) {

if (skin == null) {
skin = new Skin(skinName);
} else if (forceUpdate) {
skin.fetch();
}

return skin;
Expand Down
41 changes: 20 additions & 21 deletions src/main/java/net/citizensnpcs/npc/skin/SkinPacketTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,11 @@ void notifyRemovePacketSent(UUID playerId) {
/**
* Notify that the NPC skin has been changed.
*/
public void notifySkinChange() {
this.skin = Skin.get(entity);
public void notifySkinChange(boolean forceUpdate) {
this.skin = Skin.get(entity, forceUpdate);
skin.applyAndRespawn(entity);
}

/**
* Invoke when the NPC entity is spawned.
*/
public void onSpawnNPC() {
isRemoved = false;
new BukkitRunnable() {
@Override
public void run() {
if (!entity.getNPC().isSpawned())
return;

double viewDistance = Settings.Setting.NPC_SKIN_VIEW_DISTANCE.asDouble();
updateNearbyViewers(viewDistance);
}
}.runTaskLater(CitizensAPI.getPlugin(), 20);
}

/**
* Invoke when the NPC entity is removed.
*
Expand All @@ -139,6 +122,23 @@ public void onRemoveNPC() {
}
}

/**
* Invoke when the NPC entity is spawned.
*/
public void onSpawnNPC() {
isRemoved = false;
new BukkitRunnable() {
@Override
public void run() {
if (!entity.getNPC().isSpawned())
return;

double viewDistance = Settings.Setting.NPC_SKIN_VIEW_DISTANCE.asDouble();
updateNearbyViewers(viewDistance);
}
}.runTaskLater(CitizensAPI.getPlugin(), 20);
}

private void scheduleRemovePacket(final PlayerEntry entry) {
if (isRemoved)
return;
Expand All @@ -162,8 +162,7 @@ private void scheduleRemovePacket(PlayerEntry entry, int count) {
}

private boolean shouldRemoveFromTabList() {
return entity.getNPC().data().get("removefromtablist",
Settings.Setting.DISABLE_TABLIST.asBoolean());
return entity.getNPC().data().get("removefromtablist", Settings.Setting.DISABLE_TABLIST.asBoolean());
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/net/citizensnpcs/npc/skin/SkinnableEntity.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package net.citizensnpcs.npc.skin;

import org.bukkit.entity.Player;

import com.mojang.authlib.GameProfile;

import net.citizensnpcs.npc.ai.NPCHolder;
import org.bukkit.entity.Player;

/**
* Interface for player entities that are skinnable.
Expand Down Expand Up @@ -42,8 +44,7 @@ public interface SkinnableEntity extends NPCHolder {
void setSkinFlags(byte flags);

/**
* Set the name of the player whose skin the NPC
* uses.
* Set the name of the player whose skin the NPC uses.
*
* <p>
* Setting the skin name automatically updates and respawn the NPC.
Expand All @@ -53,4 +54,6 @@ public interface SkinnableEntity extends NPCHolder {
* The skin name.
*/
void setSkinName(String name);

void setSkinName(String skinName, boolean forceUpdate);
}

0 comments on commit fe72700

Please sign in to comment.