Skip to content

Commit

Permalink
Fixes #30
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Jun 14, 2021
1 parent 4b78124 commit 3de04d3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
55 changes: 30 additions & 25 deletions src/main/java/org/samo_lego/fabrictailor/command/SkinCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@

public class SkinCommand {
public static LiteralCommandNode<ServerCommandSource> skinNode;
private static final boolean TATERZENS_LOADED;
private static final String SET_SKIN_ATTEMPT = "Trying to set the skin ... Please wait.";
protected static final boolean TATERZENS_LOADED;
protected static final String SET_SKIN_ATTEMPT = "Trying to set the skin ... Please wait.";

public static void register(CommandDispatcher<ServerCommandSource> dispatcher, boolean dedicated) {
skinNode = dispatcher.register(literal("skin")
Expand Down Expand Up @@ -128,7 +128,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher, b
);
}

private static int clearSkin(ServerCommandSource source) throws CommandSyntaxException {
protected static int clearSkin(ServerCommandSource source) throws CommandSyntaxException {
ServerPlayerEntity player = source.getPlayer();

if(((TailoredPlayer) player).setSkin("", "", true)) {
Expand All @@ -155,7 +155,7 @@ private static int clearSkin(ServerCommandSource source) throws CommandSyntaxExc
* @param useSlim whether slim format should be used
* @return 1 if image is valid, otherwise 0
*/
private static int setSkinFromFile(ServerCommandSource src, String skinFilePath, boolean useSlim) {
protected static int setSkinFromFile(ServerCommandSource src, String skinFilePath, boolean useSlim) {
if(src.getMinecraftServer().isDedicated()) {
src.sendFeedback(new LiteralText(
"FabricTailor mod is running in server environment.\n" +
Expand Down Expand Up @@ -240,16 +240,19 @@ public static int fetchSkinByName(ServerCommandSource src, String playername, bo
// If user has no skin data

// Try to get Mojang skin first
GameProfile profile = new GameProfile(null, playername);
GameProfile profile = new GameProfile(player.getUuid(), playername);
SkullBlockEntity.loadProperties(profile, gameProfile -> {
PropertyMap propertyMap = gameProfile.getProperties();
if(propertyMap.containsKey("textures")) {

// We check the uuid as well as there is a weird
// edge case when skin for your own self doesn't get fetched (#30)
if(propertyMap.containsKey("textures") && gameProfile.getId() != player.getUuid()) {
Property textures = propertyMap.get("textures").iterator().next();
String value = textures.getValue();
String signature = textures.getSignature();
if(
TATERZENS_LOADED && TaterzensCompatibility.setTaterzenSkin(player, value, signature) ||
(((TailoredPlayer) player).setSkin(value, signature, true) && giveFeedback)
(((TailoredPlayer) player).setSkin(value, signature, true) && giveFeedback)
) {
player.sendMessage(
new LiteralText(
Expand All @@ -258,23 +261,25 @@ public static int fetchSkinByName(ServerCommandSource src, String playername, bo
false
);
}
} else {
// Getting skin data from ely.by api, since it can be used with usernames
// it also includes mojang skins
String reply = null;
try {
reply = urlRequest(new URL(String.format("http://skinsystem.ely.by/textures/signed/%s.png?proxy=true", playername)), null);
} catch(IOException e) {
if(giveFeedback)
src.sendError(
new LiteralText(
"This player doesn't seem to have any skins saved."
).formatted(Formatting.RED)
);

}
setSkinFromReply(reply, player, giveFeedback);
return;
}
// Getting skin data from ely.by api, since it can be used with usernames
// it also includes mojang skins
String reply = null;
try {
reply = urlRequest(new URL(String.format("http://skinsystem.ely.by/textures/signed/%s.png?proxy=true", playername)), null);
} catch(IOException e) {
if(giveFeedback)
src.sendError(
new LiteralText(
"This player doesn't seem to have any skins saved."
).formatted(Formatting.RED)
);

}
setSkinFromReply(reply, player, giveFeedback);

});
});
return 0;
Expand All @@ -288,7 +293,7 @@ public static int fetchSkinByName(ServerCommandSource src, String playername, bo
* @param player player to send message to.
* @param giveFeedback whether feedback should be sent to player.
*/
private static void setSkinFromReply(String reply, ServerPlayerEntity player, boolean giveFeedback) {
protected static void setSkinFromReply(String reply, ServerPlayerEntity player, boolean giveFeedback) {
if(reply == null || (reply.contains("error") && giveFeedback)) {
if(giveFeedback)
player.sendMessage(
Expand Down Expand Up @@ -325,7 +330,7 @@ private static void setSkinFromReply(String reply, ServerPlayerEntity player, bo
* @return reply from website as string
* @throws IOException IOException is thrown when connection fails for some reason.
*/
private static String urlRequest(URL url, File image) throws IOException {
protected static String urlRequest(URL url, File image) throws IOException {
URLConnection connection = url.openConnection();

String reply = null;
Expand Down Expand Up @@ -388,7 +393,7 @@ private static String urlRequest(URL url, File image) throws IOException {
* @return API reply as String
* @throws IOException exception when something went wrong
*/
private static String getContent(URLConnection connection) throws IOException {
protected static String getContent(URLConnection connection) throws IOException {
try (
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@

@Mixin(PlayerManager.class)
public abstract class MixinPlayerManager {
@Inject(method = "onPlayerConnect(Lnet/minecraft/network/ClientConnection;Lnet/minecraft/server/network/ServerPlayerEntity;)V", at = @At("HEAD"))
@Inject(
method = "onPlayerConnect(Lnet/minecraft/network/ClientConnection;Lnet/minecraft/server/network/ServerPlayerEntity;)V",
at = @At(
target = "Lnet/minecraft/server/PlayerManager;loadPlayerData(Lnet/minecraft/server/network/ServerPlayerEntity;)Lnet/minecraft/nbt/NbtCompound;",
value = "INVOKE_ASSIGN",
shift = At.Shift.AFTER
)
)
private void onPlayerConnect(ClientConnection clientConnection, ServerPlayerEntity player, CallbackInfo ci) throws CommandSyntaxException {
String value = ((TailoredPlayer) player).getSkinValue();
String signature = ((TailoredPlayer) player).getSkinSignature();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ public String getSkinSignature() {
private void writeCustomDataToNbt(NbtCompound tag, CallbackInfo ci) {
if(this.getSkinValue() != null && this.getSkinSignature() != null) {
NbtCompound skinDataTag = new NbtCompound();
skinDataTag.putString("value", this.skinValue);
skinDataTag.putString("signature", this.skinSignature);
skinDataTag.putString("value", this.getSkinValue());
skinDataTag.putString("signature", this.getSkinSignature());

tag.put("fabrictailor:skin_data", skinDataTag);
}
Expand Down

0 comments on commit 3de04d3

Please sign in to comment.