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