mirror of
https://github.com/kaboomserver/extras.git
synced 2025-02-14 05:01:19 +00:00
Optimize username command
This commit is contained in:
parent
177d2ce452
commit
4169747681
4 changed files with 59 additions and 35 deletions
|
@ -21,11 +21,10 @@ public final class CommandSkin implements CommandExecutor {
|
||||||
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
|
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
|
||||||
} else if (!SkinDownloader.skinInProgress.contains(player.getUniqueId())) {
|
} else if (!SkinDownloader.skinInProgress.contains(player.getUniqueId())) {
|
||||||
final String name = args[0];
|
final String name = args[0];
|
||||||
final boolean shouldChangeUsername = false;
|
|
||||||
final boolean shouldSendMessage = true;
|
final boolean shouldSendMessage = true;
|
||||||
|
|
||||||
SkinDownloader skinDownloader = new SkinDownloader();
|
SkinDownloader skinDownloader = new SkinDownloader();
|
||||||
skinDownloader.applySkin(player, name, shouldChangeUsername, shouldSendMessage);
|
skinDownloader.applySkin(player, name, shouldSendMessage);
|
||||||
} else {
|
} else {
|
||||||
player.sendMessage("You are already applying a skin. Please wait a few seconds.");
|
player.sendMessage("You are already applying a skin. Please wait a few seconds.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package pw.kaboom.extras.commands;
|
package pw.kaboom.extras.commands;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
|
@ -7,10 +10,19 @@ import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.ConsoleCommandSender;
|
import org.bukkit.command.ConsoleCommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
|
import com.destroystokyo.paper.profile.PlayerProfile;
|
||||||
|
import com.destroystokyo.paper.profile.ProfileProperty;
|
||||||
|
import com.google.common.base.Charsets;
|
||||||
|
|
||||||
|
import pw.kaboom.extras.Main;
|
||||||
import pw.kaboom.extras.helpers.SkinDownloader;
|
import pw.kaboom.extras.helpers.SkinDownloader;
|
||||||
|
|
||||||
public final class CommandUsername implements CommandExecutor {
|
public final class CommandUsername implements CommandExecutor {
|
||||||
|
public static HashSet<String> busyNames = new HashSet<String>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
|
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
|
||||||
if (sender instanceof ConsoleCommandSender) {
|
if (sender instanceof ConsoleCommandSender) {
|
||||||
|
@ -18,26 +30,47 @@ public final class CommandUsername implements CommandExecutor {
|
||||||
} else {
|
} else {
|
||||||
final Player player = (Player) sender;
|
final Player player = (Player) sender;
|
||||||
|
|
||||||
|
final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", args));
|
||||||
|
final String name = nameColor.substring(0, Math.min(16, nameColor.length()));
|
||||||
|
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
|
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
|
||||||
} else if (!SkinDownloader.skinInProgress.contains(player.getUniqueId())) {
|
} else if (!busyNames.contains(name)) {
|
||||||
final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", args));
|
busyNames.add(name);
|
||||||
final String name = nameColor.substring(0, Math.min(16, nameColor.length()));
|
|
||||||
|
|
||||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
UUID offlineUUID = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8));
|
||||||
if (name.equals(onlinePlayer.getName())) {
|
final PlayerProfile profile = player.getPlayerProfile();
|
||||||
player.sendMessage("A player with that username is already logged in");
|
|
||||||
return true;
|
profile.setId(offlineUUID);
|
||||||
|
profile.setName(name);
|
||||||
|
|
||||||
|
player.setPlayerProfile(profile);
|
||||||
|
player.setOp(true);
|
||||||
|
busyNames.remove(name);
|
||||||
|
|
||||||
|
/*new BukkitRunnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
UUID offlineUUID = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8));
|
||||||
|
final PlayerProfile profile = player.getPlayerProfile();
|
||||||
|
//profile.clearProperties();
|
||||||
|
profile.setId(offlineUUID);
|
||||||
|
profile.setName(name);
|
||||||
|
|
||||||
|
new BukkitRunnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
player.setPlayerProfile(profile);
|
||||||
|
player.setOp(true);
|
||||||
|
busyNames.remove(name);
|
||||||
|
}
|
||||||
|
}.runTask(JavaPlugin.getPlugin(Main.class));
|
||||||
}
|
}
|
||||||
}
|
}.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class));*/
|
||||||
|
|
||||||
final boolean shouldChangeUsername = true;
|
player.sendMessage("Successfully set your username to \"" + name + "\"");
|
||||||
final boolean shouldSendMessage = true;
|
|
||||||
|
|
||||||
SkinDownloader skinDownloader = new SkinDownloader();
|
|
||||||
skinDownloader.applySkin(player, name, shouldChangeUsername, shouldSendMessage);
|
|
||||||
} else {
|
} else {
|
||||||
player.sendMessage("Your username is already being changed. Please wait a few seconds.");
|
player.sendMessage("A player with that username is already logged in");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -15,6 +15,7 @@ import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
import com.destroystokyo.paper.profile.PlayerProfile;
|
import com.destroystokyo.paper.profile.PlayerProfile;
|
||||||
import com.destroystokyo.paper.profile.ProfileProperty;
|
import com.destroystokyo.paper.profile.ProfileProperty;
|
||||||
|
import com.google.common.base.Charsets;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
|
|
||||||
|
@ -30,7 +31,7 @@ public final class SkinDownloader {
|
||||||
private String texture;
|
private String texture;
|
||||||
private String signature;
|
private String signature;
|
||||||
|
|
||||||
public void applySkin(final Player player, final String name, final boolean shouldChangeName, final boolean shouldSendMessage) {
|
public void applySkin(final Player player, final String name, final boolean shouldSendMessage) {
|
||||||
new BukkitRunnable() {
|
new BukkitRunnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
@ -38,16 +39,11 @@ public final class SkinDownloader {
|
||||||
|
|
||||||
final PlayerProfile profile = player.getPlayerProfile();
|
final PlayerProfile profile = player.getPlayerProfile();
|
||||||
|
|
||||||
if (shouldChangeName && shouldSendMessage) {
|
|
||||||
profile.setName(name);
|
|
||||||
player.sendMessage("Changing your username. Please wait...");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fetchSkinData(name);
|
fetchSkinData(name);
|
||||||
profile.setProperty(new ProfileProperty("textures", texture, signature));
|
profile.setProperty(new ProfileProperty("textures", texture, signature));
|
||||||
|
|
||||||
if (!shouldChangeName && shouldSendMessage) {
|
if (shouldSendMessage) {
|
||||||
player.sendMessage("Successfully set your skin to " + name + "'s");
|
player.sendMessage("Successfully set your skin to " + name + "'s");
|
||||||
}
|
}
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
|
@ -57,15 +53,12 @@ public final class SkinDownloader {
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!shouldChangeName && shouldSendMessage) {
|
if (shouldSendMessage) {
|
||||||
player.sendMessage("A player with that username doesn't exist");
|
player.sendMessage("A player with that username doesn't exist");
|
||||||
}
|
}
|
||||||
|
|
||||||
skinInProgress.remove(player.getUniqueId());
|
skinInProgress.remove(player.getUniqueId());
|
||||||
|
return;
|
||||||
if (!shouldChangeName) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
new BukkitRunnable() {
|
new BukkitRunnable() {
|
||||||
|
@ -73,12 +66,7 @@ public final class SkinDownloader {
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
player.setPlayerProfile(profile);
|
player.setPlayerProfile(profile);
|
||||||
|
} catch (Exception ignored) {
|
||||||
if (shouldChangeName && shouldSendMessage) {
|
|
||||||
player.sendMessage("Successfully set your username to \"" + name + "\"");
|
|
||||||
}
|
|
||||||
} catch (Exception exception) {
|
|
||||||
// Do nothing
|
|
||||||
}
|
}
|
||||||
|
|
||||||
skinInProgress.remove(player.getUniqueId());
|
skinInProgress.remove(player.getUniqueId());
|
||||||
|
|
|
@ -56,6 +56,10 @@ public final class PlayerConnection implements Listener {
|
||||||
try {
|
try {
|
||||||
final PlayerProfile profile = event.getPlayerProfile();
|
final PlayerProfile profile = event.getPlayerProfile();
|
||||||
|
|
||||||
|
UUID offlineUUID = UUID.nameUUIDFromBytes(("OfflinePlayer:" + event.getName()).getBytes(Charsets.UTF_8));
|
||||||
|
|
||||||
|
profile.setId(offlineUUID);
|
||||||
|
|
||||||
SkinDownloader skinDownloader = new SkinDownloader();
|
SkinDownloader skinDownloader = new SkinDownloader();
|
||||||
skinDownloader.fillJoinProfile(profile, event.getName(), event.getUniqueId());
|
skinDownloader.fillJoinProfile(profile, event.getName(), event.getUniqueId());
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
|
|
Loading…
Reference in a new issue