From 164f6b5524ce363f6d5ca81e5be9a4832ca35c0a Mon Sep 17 00:00:00 2001 From: mathiascode Date: Sat, 21 Dec 2019 15:39:31 +0200 Subject: [PATCH] Fix style errors in command code --- .gitignore | 2 + pom.xml | 12 ++++- .../extras/commands/CommandClearChat.java | 8 ++-- .../extras/commands/CommandConsole.java | 4 +- .../commands/CommandDestroyEntities.java | 4 +- .../extras/commands/CommandEnchantAll.java | 39 +++------------- .../extras/commands/CommandJumpscare.java | 17 ++++--- .../kaboom/extras/commands/CommandPrefix.java | 4 +- .../extras/commands/CommandPumpkin.java | 8 ++-- .../extras/commands/CommandServerInfo.java | 46 +++++++++---------- .../kaboom/extras/commands/CommandSkin.java | 11 ++--- .../kaboom/extras/commands/CommandSpawn.java | 17 +++---- .../kaboom/extras/commands/CommandSpidey.java | 14 +++--- .../extras/commands/CommandTellraw.java | 4 +- .../extras/commands/CommandUnloadChunks.java | 4 +- .../extras/commands/CommandUsername.java | 11 ++--- .../kaboom/extras/helpers/SkinDownloader.java | 15 +++--- .../modules/player/PlayerConnection.java | 7 ++- suppressions.xml | 1 - 19 files changed, 108 insertions(+), 120 deletions(-) diff --git a/.gitignore b/.gitignore index 3f88fb7..f845591 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ +.idea/ .settings/ bin/ target/ .checkstyle .classpath .project +*.iml diff --git a/pom.xml b/pom.xml index 7000a28..7f863aa 100644 --- a/pom.xml +++ b/pom.xml @@ -5,8 +5,8 @@ master - 1.7 - 1.7 + 1.8 + 1.8 true UTF-8 @@ -49,6 +49,14 @@ + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + diff --git a/src/main/java/pw/kaboom/extras/commands/CommandClearChat.java b/src/main/java/pw/kaboom/extras/commands/CommandClearChat.java index ca24e79..d176d16 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandClearChat.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandClearChat.java @@ -9,10 +9,12 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -public class CommandClearChat implements CommandExecutor { - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { +public final class CommandClearChat implements CommandExecutor { + public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { + final int maxMessages = 100; + for (Player onlinePlayer : Bukkit.getOnlinePlayers()) { - for (int i = 0; i < 100; ++i) { + for (int i = 0; i < maxMessages; ++i) { onlinePlayer.sendMessage(""); } onlinePlayer.sendMessage(ChatColor.DARK_GREEN + "The chat has been cleared"); diff --git a/src/main/java/pw/kaboom/extras/commands/CommandConsole.java b/src/main/java/pw/kaboom/extras/commands/CommandConsole.java index 400a446..3453a2a 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandConsole.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandConsole.java @@ -7,8 +7,8 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; -public class CommandConsole implements CommandExecutor { - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { +public final class CommandConsole implements CommandExecutor { + public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { if (args.length == 0) { sender.sendMessage(ChatColor.RED + "Usage: /" + label + " "); } else { diff --git a/src/main/java/pw/kaboom/extras/commands/CommandDestroyEntities.java b/src/main/java/pw/kaboom/extras/commands/CommandDestroyEntities.java index 360b249..a745e07 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandDestroyEntities.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandDestroyEntities.java @@ -10,8 +10,8 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; -public class CommandDestroyEntities implements CommandExecutor { - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { +public final class CommandDestroyEntities implements CommandExecutor { + public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { int entityCount = 0; int worldCount = 0; diff --git a/src/main/java/pw/kaboom/extras/commands/CommandEnchantAll.java b/src/main/java/pw/kaboom/extras/commands/CommandEnchantAll.java index 512f4fe..0e59b71 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandEnchantAll.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandEnchantAll.java @@ -13,47 +13,20 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -public class CommandEnchantAll implements CommandExecutor { - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { +public final class CommandEnchantAll implements CommandExecutor { + 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 ItemStack item = player.getInventory().getItemInMainHand(); - + if (item.getType() == Material.AIR) { player.sendMessage("Please hold an item in your hand to enchant it"); } else { - item.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 32767); - item.addUnsafeEnchantment(Enchantment.ARROW_FIRE, 32767); - item.addUnsafeEnchantment(Enchantment.ARROW_INFINITE, 32767); - item.addUnsafeEnchantment(Enchantment.ARROW_KNOCKBACK, 32767); - item.addUnsafeEnchantment(Enchantment.BINDING_CURSE, 32767); - item.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 32767); - item.addUnsafeEnchantment(Enchantment.DAMAGE_ARTHROPODS, 32767); - item.addUnsafeEnchantment(Enchantment.DAMAGE_UNDEAD, 32767); - item.addUnsafeEnchantment(Enchantment.DEPTH_STRIDER, 32767); - item.addUnsafeEnchantment(Enchantment.DIG_SPEED, 32767); - item.addUnsafeEnchantment(Enchantment.DURABILITY, 32767); - item.addUnsafeEnchantment(Enchantment.FIRE_ASPECT, 32767); - item.addUnsafeEnchantment(Enchantment.FROST_WALKER, 32767); - item.addUnsafeEnchantment(Enchantment.KNOCKBACK, 32767); - item.addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 32767); - item.addUnsafeEnchantment(Enchantment.LOOT_BONUS_MOBS, 32767); - item.addUnsafeEnchantment(Enchantment.LUCK, 32767); - item.addUnsafeEnchantment(Enchantment.LURE, 32767); - item.addUnsafeEnchantment(Enchantment.MENDING, 32767); - item.addUnsafeEnchantment(Enchantment.OXYGEN, 32767); - item.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 32767); - item.addUnsafeEnchantment(Enchantment.PROTECTION_EXPLOSIONS, 32767); - item.addUnsafeEnchantment(Enchantment.PROTECTION_FALL, 32767); - item.addUnsafeEnchantment(Enchantment.PROTECTION_FIRE, 32767); - item.addUnsafeEnchantment(Enchantment.PROTECTION_PROJECTILE, 32767); - item.addUnsafeEnchantment(Enchantment.SILK_TOUCH, 32767); - item.addUnsafeEnchantment(Enchantment.SWEEPING_EDGE, 32767); - item.addUnsafeEnchantment(Enchantment.THORNS, 32767); - item.addUnsafeEnchantment(Enchantment.VANISHING_CURSE, 32767); - item.addUnsafeEnchantment(Enchantment.WATER_WORKER, 32767); + for (Enchantment enchantment : Enchantment.values()) { + item.addUnsafeEnchantment(enchantment, Short.MAX_VALUE); + } player.sendMessage("I killed Tim."); } } diff --git a/src/main/java/pw/kaboom/extras/commands/CommandJumpscare.java b/src/main/java/pw/kaboom/extras/commands/CommandJumpscare.java index 397efbd..404c061 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandJumpscare.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandJumpscare.java @@ -11,15 +11,18 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -public class CommandJumpscare implements CommandExecutor { - private void createJumpscare(Player player) { - player.spawnParticle(Particle.MOB_APPEARANCE, player.getLocation(), 4); - for (int i = 0; i < 10; i++) { +public final class CommandJumpscare implements CommandExecutor { + private void createJumpscare(final Player player) { + final int count = 4; + player.spawnParticle(Particle.MOB_APPEARANCE, player.getLocation(), count); + + final int maxIterator = 10; + for (int i = 0; i <= maxIterator; i++) { player.playSound(player.getLocation(), Sound.ENTITY_ENDERMAN_SCREAM, 1, 0); } } - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { if (args.length == 0) { sender.sendMessage(ChatColor.RED + "Usage: /" + label + " "); } else { @@ -34,9 +37,9 @@ public class CommandJumpscare implements CommandExecutor { createJumpscare(target); sender.sendMessage("Successfully created jumpscare for player \"" + target.getName() + "\""); } else { - sender.sendMessage("Player \"" + target.getName() + "\" not found"); + sender.sendMessage("Player \"" + args[0] + "\" not found"); } - } + } } return true; } diff --git a/src/main/java/pw/kaboom/extras/commands/CommandPrefix.java b/src/main/java/pw/kaboom/extras/commands/CommandPrefix.java index 3827b13..1f29f44 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandPrefix.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandPrefix.java @@ -18,8 +18,8 @@ import org.bukkit.plugin.java.JavaPlugin; import pw.kaboom.extras.Main; -public class CommandPrefix implements CommandExecutor { - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { +public final class CommandPrefix implements CommandExecutor { + public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) { if (sender instanceof ConsoleCommandSender) { sender.sendMessage("Command has to be run by a player"); } else { diff --git a/src/main/java/pw/kaboom/extras/commands/CommandPumpkin.java b/src/main/java/pw/kaboom/extras/commands/CommandPumpkin.java index a92246e..00db337 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandPumpkin.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandPumpkin.java @@ -12,12 +12,12 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -public class CommandPumpkin implements CommandExecutor { - private void placePumpkin(Player player) { +public final class CommandPumpkin implements CommandExecutor { + private void placePumpkin(final Player player) { player.getInventory().setHelmet(new ItemStack(Material.CARVED_PUMPKIN)); } - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { if (args.length == 0) { sender.sendMessage(ChatColor.RED + "Usage: /" + label + " "); } else { @@ -34,7 +34,7 @@ public class CommandPumpkin implements CommandExecutor { } else { sender.sendMessage("Player \"" + target.getName() + "\" not found"); } - } + } } return true; } diff --git a/src/main/java/pw/kaboom/extras/commands/CommandServerInfo.java b/src/main/java/pw/kaboom/extras/commands/CommandServerInfo.java index 2f7f8ca..5ca2e32 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandServerInfo.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandServerInfo.java @@ -12,15 +12,15 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; -public class CommandServerInfo implements CommandExecutor { - private void sendInfoMessage(CommandSender target, String description, String value) { +public final class CommandServerInfo implements CommandExecutor { + private void sendInfoMessage(final CommandSender target, final String description, final String value) { target.sendMessage( - ChatColor.GRAY + description + ": " + - ChatColor.WHITE + value + ChatColor.GRAY + description + ": " + + ChatColor.WHITE + value ); } - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { try { sendInfoMessage(sender, "Hostname", InetAddress.getLocalHost().getHostName() @@ -28,7 +28,7 @@ public class CommandServerInfo implements CommandExecutor { sendInfoMessage(sender, "IP address", InetAddress.getLocalHost().getHostAddress() ); - } catch (Exception exception) { + } catch (Exception ignored) { } sendInfoMessage(sender, "OS name", @@ -44,21 +44,21 @@ public class CommandServerInfo implements CommandExecutor { ManagementFactory.getRuntimeMXBean().getVmName() ); sendInfoMessage(sender, "Java version", - ManagementFactory.getRuntimeMXBean().getSpecVersion() + - " " + - ManagementFactory.getRuntimeMXBean().getVmVersion() + ManagementFactory.getRuntimeMXBean().getSpecVersion() + + " " + + ManagementFactory.getRuntimeMXBean().getVmVersion() ); try { - String[] shCommand = { + final String[] shCommand = { "/bin/sh", "-c", "cat /proc/cpuinfo | grep 'model name' | cut -f 2 -d ':' | awk '{$1=$1}1' | head -1" }; - Process process = Runtime.getRuntime().exec(shCommand); - InputStreamReader isr = new InputStreamReader(process.getInputStream()); - BufferedReader br = new BufferedReader(isr); + final Process process = Runtime.getRuntime().exec(shCommand); + final InputStreamReader isr = new InputStreamReader(process.getInputStream()); + final BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { @@ -68,7 +68,7 @@ public class CommandServerInfo implements CommandExecutor { } br.close(); - } catch (Exception exception) { + } catch (Exception ignored) { } sendInfoMessage(sender, "CPU cores", @@ -78,13 +78,13 @@ public class CommandServerInfo implements CommandExecutor { String.valueOf(ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage()) ); - long heapUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); - long nonHeapUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getUsed(); - long memoryMax = ( - ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax() + - ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getMax() + final long heapUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); + final long nonHeapUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getUsed(); + final long memoryMax = ( + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax() + + ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getMax() ); - long memoryUsage = (heapUsage + nonHeapUsage); + final long memoryUsage = (heapUsage + nonHeapUsage); sendInfoMessage(sender, "Available memory", (memoryMax / 1024 / 1024) + " MB" @@ -98,9 +98,9 @@ public class CommandServerInfo implements CommandExecutor { sendInfoMessage(sender, "Total memory usage", (memoryUsage / 1024 / 1024) + " MB" ); - - long minutes = (ManagementFactory.getRuntimeMXBean().getUptime() / 1000) / 60; - long seconds = (ManagementFactory.getRuntimeMXBean().getUptime() / 1000) % 60; + + final long minutes = (ManagementFactory.getRuntimeMXBean().getUptime() / 1000) / 60; + final long seconds = (ManagementFactory.getRuntimeMXBean().getUptime() / 1000) % 60; sendInfoMessage(sender, "Server uptime", minutes + " minute(s) " + diff --git a/src/main/java/pw/kaboom/extras/commands/CommandSkin.java b/src/main/java/pw/kaboom/extras/commands/CommandSkin.java index 718a5f0..1e36af7 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandSkin.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandSkin.java @@ -1,31 +1,30 @@ package pw.kaboom.extras.commands; 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; import pw.kaboom.extras.Main; import pw.kaboom.extras.helpers.SkinDownloader; -public class CommandSkin implements CommandExecutor { - public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) { +public final class CommandSkin implements CommandExecutor { + @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; - + if (args.length == 0) { player.sendMessage(ChatColor.RED + "Usage: /" + label + " "); } else if (!Main.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); } else { diff --git a/src/main/java/pw/kaboom/extras/commands/CommandSpawn.java b/src/main/java/pw/kaboom/extras/commands/CommandSpawn.java index 7edeeac..557b70c 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandSpawn.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandSpawn.java @@ -14,26 +14,27 @@ import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; -public class CommandSpawn implements CommandExecutor { - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { +public final class CommandSpawn implements CommandExecutor { + 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 World world = Bukkit.getWorld("world"); final Location spawnLocation = world.getSpawnLocation(); - - for (double y = spawnLocation.getY(); y <= 256; y++) { + final int maxWorldHeight = 256; + + for (double y = spawnLocation.getY(); y <= maxWorldHeight; y++) { final Location yLocation = new Location(world, spawnLocation.getX(), y, spawnLocation.getZ()); final Block coordBlock = world.getBlockAt(yLocation); - - if (!coordBlock.getType().isSolid() && - !coordBlock.getRelative(BlockFace.UP).getType().isSolid()) { + + if (!coordBlock.getType().isSolid() + && !coordBlock.getRelative(BlockFace.UP).getType().isSolid()) { player.teleportAsync(yLocation); break; } } - + player.sendMessage("Successfully moved to spawn"); } return true; diff --git a/src/main/java/pw/kaboom/extras/commands/CommandSpidey.java b/src/main/java/pw/kaboom/extras/commands/CommandSpidey.java index 93a5ac2..02223ff 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandSpidey.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandSpidey.java @@ -13,8 +13,8 @@ import org.bukkit.entity.Player; import org.bukkit.util.BlockIterator; import org.bukkit.util.Vector; -public class CommandSpidey implements CommandExecutor { - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { +public final class CommandSpidey implements CommandExecutor { + 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 { @@ -24,7 +24,7 @@ public class CommandSpidey implements CommandExecutor { final Vector direction = player.getEyeLocation().getDirection(); final int yOffset = 0; final int distance = 50; - + final BlockIterator blockIterator = new BlockIterator( world, start, @@ -32,10 +32,10 @@ public class CommandSpidey implements CommandExecutor { yOffset, distance ); - - while (blockIterator.hasNext() && - (blockIterator.next().getType() == Material.AIR || - blockIterator.next().getType() == Material.CAVE_AIR)) { + + while (blockIterator.hasNext() + && (blockIterator.next().getType() == Material.AIR + || blockIterator.next().getType() == Material.CAVE_AIR)) { blockIterator.next().setType(Material.COBWEB); } } diff --git a/src/main/java/pw/kaboom/extras/commands/CommandTellraw.java b/src/main/java/pw/kaboom/extras/commands/CommandTellraw.java index 47c8bcd..f3dff49 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandTellraw.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandTellraw.java @@ -9,8 +9,8 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -public class CommandTellraw implements CommandExecutor { - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { +public final class CommandTellraw implements CommandExecutor { + public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { if (args.length == 0) { sender.sendMessage(ChatColor.RED + "Usage: /" + label + " "); } else { diff --git a/src/main/java/pw/kaboom/extras/commands/CommandUnloadChunks.java b/src/main/java/pw/kaboom/extras/commands/CommandUnloadChunks.java index 4cc9742..044395b 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandUnloadChunks.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandUnloadChunks.java @@ -8,8 +8,8 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; -public class CommandUnloadChunks implements CommandExecutor { - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { +public final class CommandUnloadChunks implements CommandExecutor { + public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { int chunkCount = 0; for (World world : Bukkit.getServer().getWorlds()) { diff --git a/src/main/java/pw/kaboom/extras/commands/CommandUsername.java b/src/main/java/pw/kaboom/extras/commands/CommandUsername.java index b430cf2..e690d4d 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandUsername.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandUsername.java @@ -1,24 +1,23 @@ package pw.kaboom.extras.commands; 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; import pw.kaboom.extras.Main; import pw.kaboom.extras.helpers.SkinDownloader; -public class CommandUsername implements CommandExecutor { - public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) { +public final class CommandUsername implements CommandExecutor { + @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; - + if (args.length == 0) { player.sendMessage(ChatColor.RED + "Usage: /" + label + " "); } else if (!Main.skinInProgress.contains(player.getUniqueId())) { @@ -26,7 +25,7 @@ public class CommandUsername implements CommandExecutor { final String name = nameColor.substring(0, Math.min(16, nameColor.length())); final boolean shouldChangeUsername = true; final boolean shouldSendMessage = true; - + SkinDownloader skinDownloader = new SkinDownloader(); skinDownloader.applySkin(player, name, shouldChangeUsername, shouldSendMessage); } else { diff --git a/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java b/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java index cac4b0e..5541a7d 100644 --- a/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java +++ b/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java @@ -17,14 +17,14 @@ import com.google.gson.JsonParser; import pw.kaboom.extras.Main; -public class SkinDownloader { +public final class SkinDownloader { private HttpsURLConnection skinConnection; private InputStreamReader skinStream; private String texture; private String signature; - public void applySkin(Player player, String name, boolean shouldChangeName, boolean shouldSendMessage) { + public void applySkin(final Player player, final String name, final boolean shouldChangeName, final boolean shouldSendMessage) { new BukkitRunnable() { @Override public void run() { @@ -66,16 +66,15 @@ public class SkinDownloader { @Override public void run() { try { - if (player.isOnline()) { - player.setPlayerProfile(profile); + player.setPlayerProfile(profile); - if (shouldChangeName && shouldSendMessage) { - player.sendMessage("Successfully set your username to \"" + name + "\""); - } + if (shouldChangeName && shouldSendMessage) { + player.sendMessage("Successfully set your username to \"" + name + "\""); } } catch (Exception exception) { // Do nothing } + Main.skinInProgress.remove(player.getUniqueId()); } }.runTask(JavaPlugin.getPlugin(Main.class)); @@ -83,7 +82,7 @@ public class SkinDownloader { }.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class)); } - private void fetchSkinData(String playerName) throws IOException { + private void fetchSkinData(final String playerName) throws IOException { final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + playerName); skinConnection = (HttpsURLConnection) skinUrl.openConnection(); skinConnection.setConnectTimeout(0); diff --git a/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java b/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java index 4509b65..8881e3a 100644 --- a/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java +++ b/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java @@ -1,5 +1,7 @@ package pw.kaboom.extras.modules.player; +import java.io.IOException; + import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.block.BlockState; @@ -19,7 +21,7 @@ import pw.kaboom.extras.helpers.SkinDownloader; public class PlayerConnection implements Listener { @EventHandler - void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent event) { + void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent event) throws IOException { if (event.getName().length() > 16) { event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "Your username can't be longer than 16 characters"); return; @@ -77,11 +79,12 @@ public class PlayerConnection implements Listener { player.setOp(true); } + final String name = player.getName(); final boolean shouldChangeUsername = false; final boolean shouldSendMessage = false; SkinDownloader skinDownloader = new SkinDownloader(); - skinDownloader.applySkin(player, player.getName(), shouldChangeUsername, shouldSendMessage); + skinDownloader.applySkin(player, name, shouldChangeUsername, shouldSendMessage); } @SuppressWarnings("deprecation") diff --git a/suppressions.xml b/suppressions.xml index 3b75716..c74f19a 100644 --- a/suppressions.xml +++ b/suppressions.xml @@ -5,7 +5,6 @@ "http://www.puppycrawl.com/dtds/suppressions_1_0.dtd"> -