mirror of
https://github.com/kaboomserver/extras.git
synced 2025-08-05 12:03:08 +00:00
Better command handling in console
This commit is contained in:
parent
d3902cbc8b
commit
6b22761698
14 changed files with 203 additions and 299 deletions
|
@ -10,6 +10,7 @@ import org.bukkit.ChatColor;
|
|||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
|
@ -28,57 +29,61 @@ class CommandUsername implements CommandExecutor {
|
|||
}
|
||||
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) {
|
||||
final Player player = (Player) sender;
|
||||
|
||||
if (args.length == 0) {
|
||||
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
|
||||
if (sender instanceof ConsoleCommandSender) {
|
||||
sender.sendMessage("Command has to be run by a player");
|
||||
} else {
|
||||
final String[] name = args;
|
||||
|
||||
new BukkitRunnable() {
|
||||
public void run() {
|
||||
try {
|
||||
String texture = "";
|
||||
String signature = "";
|
||||
|
||||
final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", name));
|
||||
final String nameShort = nameColor.substring(0, Math.min(16, nameColor.length()));
|
||||
|
||||
final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + nameShort);
|
||||
final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection();
|
||||
skinConnection.setConnectTimeout(0);
|
||||
skinConnection.setDefaultUseCaches(false);
|
||||
skinConnection.setUseCaches(false);
|
||||
|
||||
if (skinConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
|
||||
final InputStreamReader skinStream = new InputStreamReader(skinConnection.getInputStream());
|
||||
final JsonObject response = new JsonParser().parse(skinStream).getAsJsonObject();
|
||||
final JsonObject rawSkin = response.getAsJsonObject("textures").getAsJsonObject("raw");
|
||||
texture = rawSkin.get("value").getAsString();
|
||||
signature = rawSkin.get("signature").getAsString();
|
||||
skinStream.close();
|
||||
}
|
||||
|
||||
skinConnection.disconnect();
|
||||
|
||||
final PlayerProfile profile = player.getPlayerProfile();
|
||||
profile.setName(nameShort);
|
||||
|
||||
if (!("".equals(texture)) &&
|
||||
!("".equals(signature))) {
|
||||
profile.setProperty(new ProfileProperty("textures", texture, signature));
|
||||
}
|
||||
|
||||
player.sendMessage("Successfully set your username to \"" + nameShort + "\"");
|
||||
new BukkitRunnable() {
|
||||
public void run() {
|
||||
player.setPlayerProfile(profile);
|
||||
final Player player = (Player) sender;
|
||||
|
||||
if (args.length == 0) {
|
||||
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
|
||||
} else {
|
||||
final String[] name = args;
|
||||
|
||||
new BukkitRunnable() {
|
||||
public void run() {
|
||||
try {
|
||||
String texture = "";
|
||||
String signature = "";
|
||||
|
||||
final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", name));
|
||||
final String nameShort = nameColor.substring(0, Math.min(16, nameColor.length()));
|
||||
|
||||
final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + nameShort);
|
||||
final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection();
|
||||
skinConnection.setConnectTimeout(0);
|
||||
skinConnection.setDefaultUseCaches(false);
|
||||
skinConnection.setUseCaches(false);
|
||||
|
||||
if (skinConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
|
||||
final InputStreamReader skinStream = new InputStreamReader(skinConnection.getInputStream());
|
||||
final JsonObject response = new JsonParser().parse(skinStream).getAsJsonObject();
|
||||
final JsonObject rawSkin = response.getAsJsonObject("textures").getAsJsonObject("raw");
|
||||
texture = rawSkin.get("value").getAsString();
|
||||
signature = rawSkin.get("signature").getAsString();
|
||||
skinStream.close();
|
||||
}
|
||||
}.runTask(main);
|
||||
} catch (Exception exception) {
|
||||
|
||||
skinConnection.disconnect();
|
||||
|
||||
final PlayerProfile profile = player.getPlayerProfile();
|
||||
profile.setName(nameShort);
|
||||
|
||||
if (!("".equals(texture)) &&
|
||||
!("".equals(signature))) {
|
||||
profile.setProperty(new ProfileProperty("textures", texture, signature));
|
||||
}
|
||||
|
||||
player.sendMessage("Successfully set your username to \"" + nameShort + "\"");
|
||||
new BukkitRunnable() {
|
||||
public void run() {
|
||||
player.setPlayerProfile(profile);
|
||||
}
|
||||
}.runTask(main);
|
||||
} catch (Exception exception) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}.runTaskAsynchronously(main);
|
||||
}.runTaskAsynchronously(main);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue