Skip to content

Commit

Permalink
Add placeholder support for skins
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Dec 28, 2020
1 parent cc5c132 commit 21163e4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions main/src/main/java/net/citizensnpcs/Settings.java
Expand Up @@ -137,6 +137,7 @@ public void loadFromKey(DataKey root) {
NPC_SKIN_USE_LATEST("npc.skins.use-latest-by-default", false),
NPC_SKIN_VIEW_DISTANCE("npc.skins.view-distance", 100D),
PACKET_UPDATE_DELAY("npc.packets.update-delay", 30),
PLACEHOLDER_SKIN_UPDATE_FREQUENCY("npc.skins.placeholder-update-frequency-ticks", 5 * 60 * 20),
REMOVE_PLAYERS_FROM_PLAYER_LIST("npc.player.remove-from-list", true),
SAVE_TASK_DELAY("storage.save-task.delay", 20 * 60 * 60),
SELECTION_ITEM("npc.selection.item", "stick"),
Expand Down
43 changes: 40 additions & 3 deletions main/src/main/java/net/citizensnpcs/trait/SkinTrait.java
Expand Up @@ -7,25 +7,40 @@
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Placeholders;
import net.citizensnpcs.npc.skin.SkinnableEntity;
import net.md_5.bungee.api.ChatColor;

@TraitName("skintrait")
public class SkinTrait extends Trait {
@Persist
private boolean fetchDefaultSkin = true;
private String filledPlaceholder;
@Persist
private String signature;
@Persist
private String skinName;
@Persist
private String textureRaw;
private int timer;
@Persist
private boolean updateSkins = Setting.NPC_SKIN_USE_LATEST.asBoolean();

public SkinTrait() {
super("skintrait");
}

private void checkPlaceholder(boolean update) {
String filled = ChatColor.stripColor(Placeholders.replace(skinName, null, npc).toLowerCase());
if (!filled.equalsIgnoreCase(skinName)) {
filledPlaceholder = filled;
if (update) {
onSkinChange(true);
}
}
}

/**
* Clears skin texture and name.
*/
Expand Down Expand Up @@ -53,7 +68,7 @@ public String getSignature() {
* @return The skin name if set, or null (i.e. using the NPC's name)
*/
public String getSkinName() {
return skinName;
return filledPlaceholder != null && skinName != null ? filledPlaceholder : skinName;
}

/**
Expand All @@ -63,6 +78,11 @@ public String getTexture() {
return textureRaw;
}

@Override
public void load(DataKey key) {
checkPlaceholder(false);
}

@SuppressWarnings("deprecation")
private void migrate() {
boolean update = false;
Expand Down Expand Up @@ -100,6 +120,12 @@ private void onSkinChange(boolean forceUpdate) {
@Override
public void run() {
migrate();
if (timer-- > 0)
return;
timer = Setting.PLACEHOLDER_SKIN_UPDATE_FREQUENCY.asInt();
if (filledPlaceholder == null)
return;
checkPlaceholder(true);
}

/**
Expand Down Expand Up @@ -137,10 +163,21 @@ public void setSkinName(String name) {
*/
public void setSkinName(String name, boolean forceUpdate) {
Preconditions.checkNotNull(name);
this.skinName = name.toLowerCase();
setSkinNameInternal(name);
onSkinChange(forceUpdate);
}

private void setSkinNameInternal(String name) {
skinName = ChatColor.stripColor(name.toLowerCase());
checkPlaceholder(false);
String filled = ChatColor.stripColor(Placeholders.replace(skinName, null, npc).toLowerCase());
if (!filled.equalsIgnoreCase(skinName)) {
filledPlaceholder = filled;
} else {
filledPlaceholder = null;
}
}

/**
* Sets the skin data directly, respawning the NPC if spawned
*
Expand All @@ -156,7 +193,7 @@ public void setSkinPersistent(String skinName, String signature, String data) {
Preconditions.checkNotNull(signature);
Preconditions.checkNotNull(data);

this.skinName = skinName.toLowerCase();
setSkinNameInternal(skinName);
this.signature = signature;
this.textureRaw = data;
this.updateSkins = false;
Expand Down

0 comments on commit 21163e4

Please sign in to comment.