Optimize username command

This commit is contained in:
mathiascode 2020-02-16 05:53:06 +02:00
parent 177d2ce452
commit 4169747681
4 changed files with 59 additions and 35 deletions

View file

@ -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.");
}

View file

@ -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,37 +10,67 @@ 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) {
sender.sendMessage("Command has to be run by a player");
} else {
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) {
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()));
} else if (!busyNames.contains(name)) {
busyNames.add(name);
UUID offlineUUID = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8));
final PlayerProfile profile = player.getPlayerProfile();
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
if (name.equals(onlinePlayer.getName())) {
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));
}
}
final boolean shouldChangeUsername = true;
final boolean shouldSendMessage = true;
SkinDownloader skinDownloader = new SkinDownloader();
skinDownloader.applySkin(player, name, shouldChangeUsername, shouldSendMessage);
}.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;

View file

@ -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,15 +53,12 @@ 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;
}
return;
}
new BukkitRunnable() {
@ -73,12 +66,7 @@ public final class SkinDownloader {
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());

View file

@ -55,6 +55,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());