From 93b322116b29f4e3a9d070d263d75a368f43b836 Mon Sep 17 00:00:00 2001 From: Butzlabben Date: Tue, 12 Feb 2019 10:06:54 +0100 Subject: [PATCH] Some code clean up --- .../java/de/butzlabben/inventory/OrcListener.java | 2 +- .../butzlabben/inventory/pages/InventoryPage.java | 13 ------------- .../butzlabben/inventory/pages/PageGUICreator.java | 2 +- .../de/butzlabben/world/GameProfileBuilder.java | 14 +++++++------- .../de/butzlabben/world/WorldCheckerRunnable.java | 8 ++++---- src/main/java/de/butzlabben/world/WorldSystem.java | 1 - .../world/command/WorldSettingsCommands.java | 1 - .../de/butzlabben/world/config/MessageConfig.java | 3 ++- .../butzlabben/world/listener/CommandListener.java | 9 +++------ .../world/wrapper/AsyncCreatorAdapter.java | 3 +-- .../butzlabben/world/wrapper/CreatorAdapter.java | 2 +- src/main/java/org/bstats/bukkit/Metrics.java | 3 ++- 12 files changed, 22 insertions(+), 39 deletions(-) diff --git a/src/main/java/de/butzlabben/inventory/OrcListener.java b/src/main/java/de/butzlabben/inventory/OrcListener.java index 2c1b4a6..a515c3e 100644 --- a/src/main/java/de/butzlabben/inventory/OrcListener.java +++ b/src/main/java/de/butzlabben/inventory/OrcListener.java @@ -48,7 +48,7 @@ public class OrcListener implements Listener { @EventHandler public void on(InventoryCloseEvent e) { - if (e.getInventory() != null && invs.containsKey(e.getPlayer().getUniqueId())) { + if (e.getInventory() != null) { invs.remove(e.getPlayer().getUniqueId()); } } diff --git a/src/main/java/de/butzlabben/inventory/pages/InventoryPage.java b/src/main/java/de/butzlabben/inventory/pages/InventoryPage.java index c590eae..a969fc6 100644 --- a/src/main/java/de/butzlabben/inventory/pages/InventoryPage.java +++ b/src/main/java/de/butzlabben/inventory/pages/InventoryPage.java @@ -2,8 +2,6 @@ package de.butzlabben.inventory.pages; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.Inventory; import de.butzlabben.inventory.OrcInventory; @@ -60,15 +58,4 @@ public class InventoryPage extends OrcInventory { addItem(i, item); i++; } - - @EventHandler - public void on(InventoryClickEvent e) { - if (e.getClickedInventory() != null && e.getClickedInventory().getTitle() != null - && e.getClickedInventory().getTitle().equals(title)) { - e.setCancelled(true); - OrcItem item = items.get(e.getSlot()); - if (item != null) - item.onClick((Player) e.getWhoClicked(), this); - } - } } diff --git a/src/main/java/de/butzlabben/inventory/pages/PageGUICreator.java b/src/main/java/de/butzlabben/inventory/pages/PageGUICreator.java index e859337..f5cd6ed 100644 --- a/src/main/java/de/butzlabben/inventory/pages/PageGUICreator.java +++ b/src/main/java/de/butzlabben/inventory/pages/PageGUICreator.java @@ -23,7 +23,7 @@ public class PageGUICreator { if (items == null || items.size() == 0) return; - int pages = (int) (Math.ceil((double) (items.size() / (double) elementsPerPage) < 1 ? 1 : Math.ceil((double) items.size() / (double) elementsPerPage))); + int pages = (int) (Math.ceil((items.size() / (double) elementsPerPage) < 1 ? 1 : Math.ceil((double) items.size() / (double) elementsPerPage))); invpages = new ArrayList<>(pages); diff --git a/src/main/java/de/butzlabben/world/GameProfileBuilder.java b/src/main/java/de/butzlabben/world/GameProfileBuilder.java index e6f8f2c..1b1ff53 100644 --- a/src/main/java/de/butzlabben/world/GameProfileBuilder.java +++ b/src/main/java/de/butzlabben/world/GameProfileBuilder.java @@ -40,15 +40,15 @@ public class GameProfileBuilder { .registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()).create(); private static HashMap cache = new HashMap<>(); private static long cacheTime = -1L; - private static Object sync = new Object(); + private static final Object sync = new Object(); public static GameProfile fetch(UUID uuid) throws IOException { return fetch(uuid, false); } public static GameProfile fetch(UUID uuid, boolean forceNew) throws IOException { - if ((!forceNew) && (cache.containsKey(uuid)) && (((CachedProfile) cache.get(uuid)).isValid())) { - return ((CachedProfile) cache.get(uuid)).profile; + if ((!forceNew) && (cache.containsKey(uuid)) && (cache.get(uuid).isValid())) { + return cache.get(uuid).profile; } HttpURLConnection connection; @@ -61,12 +61,12 @@ public class GameProfileBuilder { if (connection.getResponseCode() == 200) { String json = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine(); - GameProfile result = (GameProfile) gson.fromJson(json, GameProfile.class); + GameProfile result = gson.fromJson(json, GameProfile.class); cache.put(uuid, new CachedProfile(result)); return result; } if ((!forceNew) && (cache.containsKey(uuid))) { - return ((CachedProfile) cache.get(uuid)).profile; + return cache.get(uuid).profile; } throw new IOException("Could not connect to mojang servers for unknown player: " + uuid.toString()); } @@ -92,7 +92,7 @@ public class GameProfileBuilder { Base64Coder.encodeString(String.format( cape ? "{\"timestamp\":%d,\"profileId\":\"%s\",\"profileName\":\"%s\",\"isPublic\":true,\"textures\":{\"SKIN\":{\"url\":\"%s\"},\"CAPE\":{\"url\":\"%s\"}}}" : "{\"timestamp\":%d,\"profileId\":\"%s\",\"profileName\":\"%s\",\"isPublic\":true,\"textures\":{\"SKIN\":{\"url\":\"%s\"}}}", - args.toArray(new Object[args.size()]))))); + args.toArray(new Object[0]))))); return profile; } @@ -111,7 +111,7 @@ public class GameProfileBuilder { if (object.has("properties")) { for (Map.Entry prop : ((PropertyMap) context.deserialize(object.get("properties"), PropertyMap.class)).entries()) { - profile.getProperties().put((String) prop.getKey(), (Property) prop.getValue()); + profile.getProperties().put(prop.getKey(), prop.getValue()); } } return profile; diff --git a/src/main/java/de/butzlabben/world/WorldCheckerRunnable.java b/src/main/java/de/butzlabben/world/WorldCheckerRunnable.java index bd7ebdd..32963a9 100644 --- a/src/main/java/de/butzlabben/world/WorldCheckerRunnable.java +++ b/src/main/java/de/butzlabben/world/WorldCheckerRunnable.java @@ -28,19 +28,19 @@ public class WorldCheckerRunnable implements Runnable { String ownerofWorld = null; for (OfflinePlayer p : Bukkit.getOfflinePlayers()) { if (p.getUniqueId().toString() - .equals(worldname.substring(worldname.length() - 36, worldname.length()))) + .equals(worldname.substring(worldname.length() - 36))) ownerofWorld = p.getName(); } - String members = ""; + StringBuilder members = new StringBuilder(); for (Player p : world.getPlayers()) { - members += p.getName() + " "; + members.append(p.getName()).append(" "); } for (Player p : Bukkit.getOnlinePlayers()) { if (!p.hasPermission("ws.lag")) continue; p.sendMessage(MessageConfig.getLagDetection().replaceAll("%world", ownerofWorld + " ( ID: " + world.getName().substring(2, worldname.length() - 37) + " )")); - p.sendMessage(MessageConfig.getPlayerList().replaceAll("%players", members)); + p.sendMessage(MessageConfig.getPlayerList().replaceAll("%players", members.toString())); } } diff --git a/src/main/java/de/butzlabben/world/WorldSystem.java b/src/main/java/de/butzlabben/world/WorldSystem.java index 632686d..a82c1bf 100644 --- a/src/main/java/de/butzlabben/world/WorldSystem.java +++ b/src/main/java/de/butzlabben/world/WorldSystem.java @@ -3,7 +3,6 @@ package de.butzlabben.world; import de.butzlabben.autoupdater.AutoUpdater; import de.butzlabben.world.command.*; import de.butzlabben.world.config.*; -import de.butzlabben.world.gui.GuiCommand; import de.butzlabben.world.listener.*; import de.butzlabben.world.wrapper.AsyncCreatorAdapter; import de.butzlabben.world.wrapper.CreatorAdapter; diff --git a/src/main/java/de/butzlabben/world/command/WorldSettingsCommands.java b/src/main/java/de/butzlabben/world/command/WorldSettingsCommands.java index c041c71..c4ee81e 100644 --- a/src/main/java/de/butzlabben/world/command/WorldSettingsCommands.java +++ b/src/main/java/de/butzlabben/world/command/WorldSettingsCommands.java @@ -83,7 +83,6 @@ public class WorldSettingsCommands { } else { p.sendMessage(MessageConfig.getInvalidInput().replaceAll("input", args.getArgument(0))); - return; } } else { if (sw.isLoaded()) diff --git a/src/main/java/de/butzlabben/world/config/MessageConfig.java b/src/main/java/de/butzlabben/world/config/MessageConfig.java index 06690e3..16c5cd4 100644 --- a/src/main/java/de/butzlabben/world/config/MessageConfig.java +++ b/src/main/java/de/butzlabben/world/config/MessageConfig.java @@ -24,7 +24,8 @@ public class MessageConfig { } private static List defaultCmdHelp = new ArrayList<>(20); - { + + static { defaultCmdHelp.add("/ws get §8- §7Will give you a World"); defaultCmdHelp.add("/ws home §8- §7Teleports you on your World"); defaultCmdHelp.add("/ws sethome §8- §7Sets a specific home"); diff --git a/src/main/java/de/butzlabben/world/listener/CommandListener.java b/src/main/java/de/butzlabben/world/listener/CommandListener.java index f31a5dd..a219868 100644 --- a/src/main/java/de/butzlabben/world/listener/CommandListener.java +++ b/src/main/java/de/butzlabben/world/listener/CommandListener.java @@ -81,8 +81,7 @@ public class CommandListener implements Listener { if (!wp.canChangeGamemode() && !wp.isOwnerofWorld()) { p.sendMessage(MessageConfig.getNoPermission()); e.setCancelled(true); - return; - } + } } else if (cmd.startsWith("/tp") || cmd.startsWith("/teleport")) { String[] args = e.getMessage().split(" "); if (args.length == 2) { @@ -106,8 +105,7 @@ public class CommandListener implements Listener { if (!wp.canTeleport()) { p.sendMessage(MessageConfig.getNoPermission()); e.setCancelled(true); - return; - } + } } else if (args.length == 3) { if (!p.hasPermission("ws.tp.other")) { p.sendMessage(MessageConfig.getNoPermission()); @@ -127,8 +125,7 @@ public class CommandListener implements Listener { if (!wp.canTeleport()) { p.sendMessage(MessageConfig.getNoPermission()); e.setCancelled(true); - return; - } + } } } } diff --git a/src/main/java/de/butzlabben/world/wrapper/AsyncCreatorAdapter.java b/src/main/java/de/butzlabben/world/wrapper/AsyncCreatorAdapter.java index 6d3c91c..ef05845 100644 --- a/src/main/java/de/butzlabben/world/wrapper/AsyncCreatorAdapter.java +++ b/src/main/java/de/butzlabben/world/wrapper/AsyncCreatorAdapter.java @@ -39,7 +39,6 @@ public class AsyncCreatorAdapter implements CreatorAdapter { r.run(); } }); - return; - } + } } diff --git a/src/main/java/de/butzlabben/world/wrapper/CreatorAdapter.java b/src/main/java/de/butzlabben/world/wrapper/CreatorAdapter.java index 334e46a..fb40d8b 100644 --- a/src/main/java/de/butzlabben/world/wrapper/CreatorAdapter.java +++ b/src/main/java/de/butzlabben/world/wrapper/CreatorAdapter.java @@ -8,5 +8,5 @@ import org.bukkit.WorldCreator; */ public interface CreatorAdapter { - public void create(WorldCreator creator, SystemWorld world, Runnable sendPlayerMessageCallback); + void create(WorldCreator creator, SystemWorld world, Runnable sendPlayerMessageCallback); } diff --git a/src/main/java/org/bstats/bukkit/Metrics.java b/src/main/java/org/bstats/bukkit/Metrics.java index 8be68dc..f63ae54 100644 --- a/src/main/java/org/bstats/bukkit/Metrics.java +++ b/src/main/java/org/bstats/bukkit/Metrics.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -331,7 +332,7 @@ public class Metrics { } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(outputStream); - gzip.write(str.getBytes("UTF-8")); + gzip.write(str.getBytes(StandardCharsets.UTF_8)); gzip.close(); return outputStream.toByteArray(); }