Skip to content

Commit

Permalink
Fix PlayerTag.skin and skin_blob mechanisms (on paper) (#2373)
Browse files Browse the repository at this point in the history
* Fix `PlayerTag.skin` mechanism (on paper)

* Fix setting player's skin back to their own one

* Only use for 1.19+

* Fix `PlayerTag.skin_blob` as well
  • Loading branch information
tal5 committed Sep 12, 2022
1 parent a5e0005 commit 1894f81
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
Expand Up @@ -5,7 +5,10 @@
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.paper.PaperModule;
import com.denizenscript.denizen.utilities.AdvancedTextImpl;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.destroystokyo.paper.profile.PlayerProfile;
import com.destroystokyo.paper.profile.ProfileProperty;
import io.papermc.paper.entity.RelativeTeleportFlag;
import io.papermc.paper.potion.PotionMix;
import net.kyori.adventure.text.Component;
Expand All @@ -29,9 +32,7 @@
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.potion.PotionBrewer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.*;

public class PaperAdvancedTextImpl extends AdvancedTextImpl {

Expand Down Expand Up @@ -203,4 +204,54 @@ public String getDeathMessage(PlayerDeathEvent event) {
public void setDeathMessage(PlayerDeathEvent event, String message) {
event.deathMessage(PaperModule.parseFormattedText(message, ChatColor.WHITE));
}

public Set<UUID> modifiedTextures = new HashSet<>();

@Override
public void setSkin(Player player, String name) {
if (NMSHandler.getVersion().isAtMost(NMSVersion.v1_18)) {
NMSHandler.instance.getProfileEditor().setPlayerSkin(player, name);
return;
}
// Note: this API is present on all supported versions, but currently used for 1.19+ only
PlayerProfile skinProfile = Bukkit.createProfile(name);
boolean isOwnName = CoreUtilities.equalsIgnoreCase(player.getName(), name);
if (isOwnName && modifiedTextures.contains(player.getUniqueId())) {
skinProfile.removeProperty("textures");
}
Bukkit.getScheduler().runTaskAsynchronously(Denizen.instance, () -> {
if (!skinProfile.complete()) {
return;
}
DenizenCore.runOnMainThread(() -> {
for (ProfileProperty property : skinProfile.getProperties()) {
if (property.getName().equals("textures")) {
PlayerProfile playerProfile = player.getPlayerProfile();
playerProfile.setProperty(property);
player.setPlayerProfile(playerProfile);
if (isOwnName) {
modifiedTextures.remove(player.getUniqueId());
}
else {
modifiedTextures.add(player.getUniqueId());
}
return;
}
}
});
});
}

@Override
public void setSkinBlob(Player player, String blob) {
if (NMSHandler.getVersion().isAtMost(NMSVersion.v1_18)) {
NMSHandler.instance.getProfileEditor().setPlayerSkinBlob(player, blob);
return;
}
// Note: this API is present on all supported versions, but currently used for 1.19+ only
List<String> split = CoreUtilities.split(blob, ';');
PlayerProfile playerProfile = player.getPlayerProfile();
playerProfile.setProperty(new ProfileProperty("textures", split.get(0), split.size() > 1 ? split.get(1) : null));
player.setPlayerProfile(playerProfile);
}
}
Expand Up @@ -3788,11 +3788,10 @@ else if (getPlayerEntity().getGameMode() == GameMode.SPECTATOR) {
if (mechanism.matches("skin") && mechanism.hasValue()) {
String name = mechanism.getValue().asString();
if (name.length() > 16) {
Debug.echoError("Must specify a name with no more than 16 characters.");
}
else {
NMSHandler.instance.getProfileEditor().setPlayerSkin(getPlayerEntity(), mechanism.getValue().asString());
mechanism.echoError("Must specify a name with no more than 16 characters.");
return;
}
AdvancedTextImpl.instance.setSkin(getPlayerEntity(), name);
}

// <--[mechanism]
Expand All @@ -3807,7 +3806,7 @@ else if (getPlayerEntity().getGameMode() == GameMode.SPECTATOR) {
// <PlayerTag.skin_blob>
// -->
if (mechanism.matches("skin_blob") && mechanism.hasValue()) {
NMSHandler.instance.getProfileEditor().setPlayerSkinBlob(getPlayerEntity(), mechanism.getValue().asString());
AdvancedTextImpl.instance.setSkinBlob(getPlayerEntity(), mechanism.getValue().asString());
}

// <--[mechanism]
Expand Down
Expand Up @@ -126,4 +126,12 @@ public String getDeathMessage(PlayerDeathEvent event) {
public void setDeathMessage(PlayerDeathEvent event, String message) {
event.setDeathMessage(message);
}

public void setSkin(Player player, String name) {
NMSHandler.instance.getProfileEditor().setPlayerSkin(player, name);
}

public void setSkinBlob(Player player, String blob) {
NMSHandler.instance.getProfileEditor().setPlayerSkinBlob(player, blob);
}
}

0 comments on commit 1894f81

Please sign in to comment.