Add --url parameter to /npc skin

This commit is contained in:
fullwall 2020-03-20 17:29:23 +08:00
parent 1ddc78a543
commit 5f6026f50b
3 changed files with 72 additions and 3 deletions

View File

@ -1,5 +1,12 @@
package net.citizensnpcs.commands;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -31,6 +38,8 @@ import org.bukkit.entity.Rabbit;
import org.bukkit.entity.Villager.Profession;
import org.bukkit.entity.Zombie;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
@ -1627,7 +1636,7 @@ public class NPCCommands {
@Command(
aliases = { "npc" },
usage = "skin (-c -l(atest)) [name] (or -t [uuid/name] [data] [signature])",
usage = "skin (-c -l(atest)) [name] (or --url [url] or -t [uuid/name] [data] [signature])",
desc = "Sets an NPC's skin name. Use -l to set the skin to always update to the latest",
modifiers = { "skin" },
min = 1,
@ -1639,6 +1648,62 @@ public class NPCCommands {
String skinName = npc.getName();
if (args.hasFlag('c')) {
npc.data().remove(NPC.PLAYER_SKIN_UUID_METADATA);
} else if (args.hasValueFlag("url")) {
final String url = args.getFlag("url");
Bukkit.getScheduler().runTaskAsynchronously(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
DataOutputStream out = null;
BufferedReader reader = null;
try {
URL target = new URL("https://api.mineskin.org/generate/url");
HttpURLConnection con = (HttpURLConnection) target.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setConnectTimeout(1000);
con.setReadTimeout(10000);
out = new DataOutputStream(con.getOutputStream());
out.writeBytes("url=" + URLEncoder.encode(url, "UTF-8"));
out.close();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
JSONObject output = (JSONObject) new JSONParser().parse(reader);
JSONObject data = (JSONObject) output.get("data");
String uuid = (String) data.get("uuid");
JSONObject texture = (JSONObject) data.get("texture");
String textureEncoded = (String) texture.get("value");
String signature = (String) texture.get("signature");
con.disconnect();
Bukkit.getScheduler().runTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
((SkinnableEntity) npc.getEntity()).setSkinPersistent(uuid, signature, textureEncoded);
Messaging.sendTr(sender, Messages.SKIN_URL_SET, npc.getName(), url);
}
});
} catch (Throwable t) {
Bukkit.getScheduler().runTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
Messaging.sendErrorTr(sender, Messages.ERROR_SETTING_SKIN_URL, url);
}
});
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}
});
return;
} else if (args.hasFlag('t')) {
if (args.argsLength() != 4)
throw new CommandException(Messages.SKIN_REQUIRED);

View File

@ -89,6 +89,7 @@ public class Messages {
public static final String ERROR_LOADING_ECONOMY = "citizens.economy.error-loading";
public static final String ERROR_SETTING_ENTITY_PERSISTENT = "citizens.nms-errors.error-setting-persistent";
public static final String ERROR_SETTING_LOOKCLOSE_RANGE = "citizens.commands.npc.lookclose.error-random-range";
public static final String ERROR_SETTING_SKIN_URL = "citizens.commands.npc.skin.error-setting-url";
public static final String ERROR_SPAWNING_CUSTOM_ENTITY = "citizens.nms-errors.spawning-custom-entity";
public static final String ERROR_STOPPING_NETWORK_THREADS = "citizens.nms-errors.stopping-network-threads";
public static final String ERROR_UPDATING_NAVIGATION_WORLD = "citizens.nms-errors.updating-navigation-world";
@ -274,9 +275,12 @@ public class Messages {
public static final String SKIN_LAYERS_SET = "citizens.commands.npc.skin.layers-set";
public static final String SKIN_REQUIRED = "citizens.commands.npc.skin.missing-skin";
public static final String SKIN_SET = "citizens.commands.npc.skin.set";
public static final String SKIN_URL_SET = "citizens.commands.npc.skin.skin-url-set";
public static final String SKIPPING_BROKEN_TRAIT = "citizens.notifications.skipping-broken-trait";
public static final String SKIPPING_INVALID_ANCHOR = "citizens.notifications.skipping-invalid-anchor";
public static final String SKIPPING_INVALID_POSE = "citizens.notifications.skipping-invalid-pose";
public static final String SNOWMAN_DERP_SET = "citizens.commands.npc.snowman.derp-set";
public static final String SNOWMAN_DERP_STOPPED = "citizens.commands.npc.snowman.derp-stopped";
public static final String SOUND_INFO = "citizens.commands.npc.sound.info";
public static final String SOUND_SET = "citizens.commands.npc.sound.set";
public static final String SPAWN_NUMERIC_ID_ONLY = "citizens.commands.npc.spawn.numeric-id-only";
@ -370,6 +374,4 @@ public class Messages {
public static final String ZOMBIE_VILLAGER_PROFESSION_SET = "citizens.commands.npc.zombiemod.villager-profession-set";
public static final String ZOMBIE_VILLAGER_SET = "citizens.commands.npc.zombiemod.villager-set";
public static final String ZOMBIE_VILLAGER_UNSET = "citizens.commands.npc.zombiemod.villager-unset";
public static final String SNOWMAN_DERP_SET = "citizens.commands.npc.snowman.derp-set";
public static final String SNOWMAN_DERP_STOPPED = "citizens.commands.npc.snowman.derp-stopped";
}

View File

@ -172,6 +172,8 @@ citizens.commands.npc.script.current-scripts=[[{0}]]''s current scripts are [[{1
citizens.commands.npc.shulker.peek-set=[[{0}]]''s peek amount set to [[{1}]].
citizens.commands.npc.shulker.color-set=[[{0}]]''s color set to [[{1}]].
citizens.commands.npc.shulker.invalid-color=Invalid shulker color given. Valid colors are: [[{0}]].
citizens.commands.npc.skin.error-setting-url=Error downloading skin texture from [[{0}]]. Are you sure the URL is valid?
citizens.commands.npc.skin.skin-url-set=Downloaded [[{0}]]''s skin from [[{1}]].
citizens.commands.npc.skin.set=[[{0}]]''s skin name set to [[{1}]].
citizens.commands.npc.skin.missing-skin=A skin name is required.
citizens.commands.npc.skin.cleared=[[{0}]]''s skin name was cleared.