Skip to content

Commit

Permalink
Better skin reloading (thanks to @Pyrofab)
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Jul 28, 2020
1 parent 1496c3a commit 0b6e51b
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 204 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ repositories {
url = 'https://dl.bintray.com/ladysnake/libs'
}
maven {
//url 'https://masa.dy.fi/maven'
url 'https://jitpack.io'
}
maven {
url 'https://masa.dy.fi/maven'
}
}

sourceCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -33,7 +35,7 @@ dependencies {
// Cardinal Components
modImplementation "io.github.onyxstudios.Cardinal-Components-API:${project.cca_module}:${project.cca_version}"
modImplementation "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-base:${project.cca_version}"
modImplementation "com.github.gnembon:fabric-carpet:1.16-SNAPSHOT"
modImplementation "carpet:fabric-carpet:1.16-${project.carpet_core_version}"
// Includes Cardinal Components API as a Jar-in-Jar dependency (optional)
//include "io.github.OnyxStudios:Cardinal-Components-API:${project.cca_module}:${project.cca_version}"
}
Expand Down
15 changes: 9 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
org.gradle.jvmargs=-Xmx1G

# Fabric Properties
minecraft_version=1.16
yarn_mappings=1.16+build.1
loader_version=0.8.8+build.202
minecraft_version=1.16.1
yarn_mappings=1.16.1+build.21
loader_version=0.9.0+build.204

#Fabric api
fabric_version=0.13.1+build.370-1.16
fabric_version=0.16.0+build.384-1.16.1

# Mod Properties
mod_version = 1.0.0
mod_version = 1.1.0
maven_group = org.samo_lego
archives_base_name = fabrictailor

# Dependencies
cca_module = cardinal-components-entity
cca_version = 2.4.0-nightly.1.16-pre5
cca_version = 2.4.2

# Carpet for debugging
carpet_core_version = 1.4.0+v200623
165 changes: 1 addition & 164 deletions src/main/java/org/samo_lego/fabrictailor/Command/SetskinCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,185 +4,22 @@
import net.minecraft.entity.Entity;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.greedyString;
import static net.minecraft.command.arguments.MessageArgumentType.getMessage;
import static net.minecraft.command.arguments.MessageArgumentType.message;
import static org.samo_lego.fabrictailor.FabricTailor.setPlayerSkin;

public class SetskinCommand {

public static void register(CommandDispatcher<ServerCommandSource> dispatcher, boolean b) {
dispatcher.register(CommandManager.literal("setskin")
.then(CommandManager.literal("URL")
.then(CommandManager.argument("skin URL", message())
.executes(ctx -> fetchSkinByUrl((ServerPlayerEntity) ctx.getSource().getEntityOrThrow(), getMessage(ctx, "skin URL").getString()))
)
.executes(ctx -> {
Entity player = ctx.getSource().getEntityOrThrow();
player.sendSystemMessage(
new LiteralText(
"§6You have to provide URL of the skin you want."
),
player.getUuid()
);
return 1;
})
)
.then(CommandManager.literal("player")
.then(CommandManager.argument("playername", greedyString())
.executes(ctx -> fetchSkinByName((ServerPlayerEntity) ctx.getSource().getEntityOrThrow(), getString(ctx, "playername"), true))
)
.executes(ctx -> {
Entity player = ctx.getSource().getEntityOrThrow();
player.sendSystemMessage(
new LiteralText(
"§6You have to provide player's name."
),
player.getUuid()
);
return 1;
})
)
.then(CommandManager.argument("clear", greedyString())
.executes(ctx -> setPlayerSkin((ServerPlayerEntity) ctx.getSource().getEntityOrThrow(), "", "") ? 1 : 0)
)
.executes(ctx -> {
Entity player = ctx.getSource().getEntityOrThrow();
player.sendSystemMessage(
new LiteralText(
6You have to provide URL or player's name of the skin you want."
6/setskin is deprecated. Please use /skin"
),
player.getUuid()
);
return 1;
})
);
}

// Skin setting by URL
public static int fetchSkinByUrl(ServerPlayerEntity player, String skinUrl) {
player.sendSystemMessage(
new LiteralText(
"§eTrying to set your skin ... Please wait."
),
player.getUuid()
);
new Thread(() -> {
try {
// Code from https://stackoverflow.com/questions/45809234/change-player-skin-with-nms-in-minecraft-bukkit-spigot
HttpsURLConnection connection = (HttpsURLConnection) new URL(String.format("https://api.mineskin.org/generate/url?url=%s", skinUrl)).openConnection();
connection.setRequestMethod("POST");
if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
String reply = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();
if(reply.contains("error")) {
player.sendSystemMessage(
new LiteralText(
"§cAn error occurred when trying to fetch skin."
),
player.getUuid()
);
return;
}
String value = reply.split("\"value\":\"")[1].split("\"")[0];
String signature = reply.split("\"signature\":\"")[1].split("\"")[0];

if(setPlayerSkin(player, value, signature)) {
player.sendSystemMessage(
new LiteralText(
"§aYour skin was set successfully."
),
player.getUuid()
);
}
else {
player.sendSystemMessage(
new LiteralText(
"§aA problem occurred when trying to set the skin."
),
player.getUuid()
);
}
}
else
player.sendSystemMessage(
new LiteralText(
"§cAn error occurred when trying to fetch skin."
),
player.getUuid()
);
} catch (IOException e) {
player.sendSystemMessage(
new LiteralText(
"§cMalformed url!"
),
player.getUuid()
);
}
}).start();
return 0;
}

// Skin setting by playername
public static int fetchSkinByName(ServerPlayerEntity player, String playername, boolean giveFeedback) {
if(giveFeedback)
player.sendSystemMessage(
new LiteralText(
"§eTrying to set your skin ... Please wait."
),
player.getUuid()
);
new Thread(() -> {
// If user has no skin data
// Getting skin data from ely.by api, since it can be used with usernames
// it also includes mojang skins
try {
HttpURLConnection connection = (HttpURLConnection) new URL("http://skinsystem.ely.by/textures/signed/" + playername + ".png?proxy=true").openConnection();
if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
String reply = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();
if (reply.contains("error") && giveFeedback) {
player.sendSystemMessage(
new LiteralText(
"§cAn error occurred when trying to fetch skin."
),
player.getUuid()
);
return;
}

String value = reply.split("\"value\":\"")[1].split("\"")[0];
String signature = reply.split("\"signature\":\"")[1].split("\"")[0];

if(setPlayerSkin(player, value, signature) && giveFeedback) {
player.sendSystemMessage(
new LiteralText(
"§aYour skin was set successfully."
),
player.getUuid()
);
}

}
} catch (IOException e) {
if(giveFeedback)
player.sendSystemMessage(
new LiteralText(
"§cThis player doesn't seem to have any skins saved."
),
player.getUuid()
);
}
}).start();
return 0;
}
}
Loading

0 comments on commit 0b6e51b

Please sign in to comment.