From c6a0980f1542d8b56e6af6f55414abce86a91237 Mon Sep 17 00:00:00 2001 From: Alex <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Fri, 16 Apr 2021 11:35:54 +0200 Subject: [PATCH] Cleanup the code a bit more --- src/main/java/com/loohp/limbo/Console.java | 17 ++--- .../java/com/loohp/limbo/consolegui/GUI.java | 7 +- .../loohp/limbo/consolegui/SystemInfo.java | 14 ++-- .../loohp/limbo/file/ServerProperties.java | 8 +- .../java/com/loohp/limbo/metrics/Metrics.java | 21 +----- .../loohp/limbo/utils/CustomStringUtils.java | 4 +- .../com/loohp/limbo/utils/DataTypeIO.java | 2 +- .../java/com/loohp/limbo/utils/YamlOrder.java | 75 +++++++++---------- .../java/com/loohp/limbo/world/World.java | 2 +- 9 files changed, 62 insertions(+), 88 deletions(-) diff --git a/src/main/java/com/loohp/limbo/Console.java b/src/main/java/com/loohp/limbo/Console.java index e7ca653..b398cb3 100644 --- a/src/main/java/com/loohp/limbo/Console.java +++ b/src/main/java/com/loohp/limbo/Console.java @@ -96,14 +96,11 @@ public class Console implements CommandSender { reader.setHandleUserInterrupt(false); terminal = TerminalBuilder.builder().streams(in, out).system(true).jansi(true).build(); - tabReader = LineReaderBuilder.builder().terminal(terminal).completer(new Completer() { - @Override - public void complete(LineReader reader, ParsedLine line, List candidates) { - String[] args = CustomStringUtils.splitStringToArgs(line.line()); - List tab = Limbo.getInstance().getPluginManager().getTabOptions(Limbo.getInstance().getConsole(), args); - for (String each : tab) { - candidates.add(new Candidate(each)); - } + tabReader = LineReaderBuilder.builder().terminal(terminal).completer((reader, line, candidates) -> { + String[] args = CustomStringUtils.splitStringToArgs(line.line()); + List tab = Limbo.getInstance().getPluginManager().getTabOptions(Limbo.getInstance().getConsole(), args); + for (String each : tab) { + candidates.add(new Candidate(each)); } }).build(); tabReader.setAutosuggestion(SuggestionType.NONE); @@ -149,7 +146,7 @@ public class Console implements CommandSender { @Override public void sendMessage(BaseComponent[] component) { - sendMessage(String.join("", Arrays.asList(component).stream().map(each -> each.toLegacyText()).collect(Collectors.toList()))); + sendMessage(Arrays.stream(component).map(BaseComponent::toLegacyText).collect(Collectors.joining(""))); } @Override @@ -159,7 +156,7 @@ public class Console implements CommandSender { ConsoleTextOutput.appendText(ChatColor.stripColor("[" + date + " Info] " + message), true); logs.println(ChatColor.stripColor("[" + date + " Info] " + message)); try { - reader.getOutput().append("[" + date + " Info] " + translateToConsole(message) + "\n"); + reader.getOutput().append("[").append(date).append(" Info] ").append(translateToConsole(message)).append("\n"); reader.getOutput().flush(); } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/com/loohp/limbo/consolegui/GUI.java b/src/main/java/com/loohp/limbo/consolegui/GUI.java index 246e0fc..79b453c 100644 --- a/src/main/java/com/loohp/limbo/consolegui/GUI.java +++ b/src/main/java/com/loohp/limbo/consolegui/GUI.java @@ -191,12 +191,7 @@ public class GUI extends JFrame { GUI frame = new GUI(); frame.setVisible(true); - Thread t1 = new Thread(new Runnable() { - @Override - public void run() { - SystemInfo.printInfo(); - } - }); + Thread t1 = new Thread(SystemInfo::printInfo); t1.start(); loadFinish = true; diff --git a/src/main/java/com/loohp/limbo/consolegui/SystemInfo.java b/src/main/java/com/loohp/limbo/consolegui/SystemInfo.java index e08e0b9..8d2483a 100644 --- a/src/main/java/com/loohp/limbo/consolegui/SystemInfo.java +++ b/src/main/java/com/loohp/limbo/consolegui/SystemInfo.java @@ -20,10 +20,10 @@ public class SystemInfo { long allocatedMemory = runtime.totalMemory(); long freeMemory = runtime.freeMemory(); - sb.append("Free Memory: " + format.format(freeMemory / 1024 / 1024) + " MB\n"); - sb.append("Allocated Memory: " + format.format(allocatedMemory / 1024 / 1024) + " MB\n"); - sb.append("Max Memory: " + format.format(maxMemory / 1024 / 1024) + " MB\n"); - sb.append("Memory Usage: " + format.format((allocatedMemory - freeMemory) / 1024 / 1024) + "/" + format.format(maxMemory / 1024 / 1024) + " MB (" + Math.round((double) (allocatedMemory - freeMemory) / (double) (maxMemory) * 100) + "%)\n"); + sb.append("Free Memory: ").append(format.format(freeMemory / 1024 / 1024)).append(" MB\n"); + sb.append("Allocated Memory: ").append(format.format(allocatedMemory / 1024 / 1024)).append(" MB\n"); + sb.append("Max Memory: ").append(format.format(maxMemory / 1024 / 1024)).append(" MB\n"); + sb.append("Memory Usage: ").append(format.format((allocatedMemory - freeMemory) / 1024 / 1024)).append("/").append(format.format(maxMemory / 1024 / 1024)).append(" MB (").append(Math.round((double) (allocatedMemory - freeMemory) / (double) (maxMemory) * 100)).append("%)\n"); sb.append("\n"); try { @@ -35,9 +35,9 @@ public class SystemInfo { double systemLoad = operatingSystemMXBean.getSystemCpuLoad(); int processors = runtime.availableProcessors(); - sb.append("Available Processors: " + processors + "\n"); - sb.append("Process CPU Load: " + Math.round(processLoad * 100) + "%\n"); - sb.append("System CPU Load: " + Math.round(systemLoad * 100) + "%\n"); + sb.append("Available Processors: ").append(processors).append("\n"); + sb.append("Process CPU Load: ").append(Math.round(processLoad * 100)).append("%\n"); + sb.append("System CPU Load: ").append(Math.round(systemLoad * 100)).append("%\n"); GUI.sysText.setText(sb.toString()); } catch (Exception ignore) { } diff --git a/src/main/java/com/loohp/limbo/file/ServerProperties.java b/src/main/java/com/loohp/limbo/file/ServerProperties.java index da431a8..b8a88a4 100644 --- a/src/main/java/com/loohp/limbo/file/ServerProperties.java +++ b/src/main/java/com/loohp/limbo/file/ServerProperties.java @@ -17,7 +17,7 @@ import java.util.Properties; public class ServerProperties { public static final String COMMENT = "For explaination of what each of the options does, please visit:\nhttps://github.com/LOOHP/Limbo/blob/master/src/main/resources/server.properties"; - Optional favicon; + private BufferedImage favicon; private final File file; private final int maxPlayers; private final int serverPort; @@ -91,7 +91,7 @@ public class ServerProperties { try { BufferedImage image = ImageIO.read(png); if (image.getHeight() == 64 && image.getWidth() == 64) { - favicon = Optional.of(image); + favicon = image; } else { Limbo.getInstance().getConsole().sendMessage("Unable to load server-icon.png! The image is not 64 x 64 in size!"); } @@ -100,7 +100,7 @@ public class ServerProperties { } } else { Limbo.getInstance().getConsole().sendMessage("No server-icon.png found"); - favicon = Optional.empty(); + favicon = null; } Limbo.getInstance().getConsole().sendMessage("Loaded server.properties"); @@ -115,7 +115,7 @@ public class ServerProperties { } public Optional getFavicon() { - return favicon; + return Optional.ofNullable(favicon); } public File getFile() { diff --git a/src/main/java/com/loohp/limbo/metrics/Metrics.java b/src/main/java/com/loohp/limbo/metrics/Metrics.java index 010d6fb..0abaf91 100644 --- a/src/main/java/com/loohp/limbo/metrics/Metrics.java +++ b/src/main/java/com/loohp/limbo/metrics/Metrics.java @@ -93,26 +93,11 @@ public class Metrics { startSubmitting(); } - addCustomChart(new Metrics.SingleLineChart("players", new Callable() { - @Override - public Integer call() throws Exception { - return Limbo.getInstance().getPlayers().size(); - } - })); + addCustomChart(new Metrics.SingleLineChart("players", () -> Limbo.getInstance().getPlayers().size())); - addCustomChart(new Metrics.SimplePie("limbo_version", new Callable() { - @Override - public String call() throws Exception { - return limboVersion; - } - })); + addCustomChart(new Metrics.SimplePie("limbo_version", () -> limboVersion)); - addCustomChart(new Metrics.SimplePie("minecraftVersion", new Callable() { - @Override - public String call() throws Exception { - return Limbo.getInstance().serverImplementationVersion; - } - })); + addCustomChart(new Metrics.SimplePie("minecraftVersion", () -> Limbo.getInstance().serverImplementationVersion)); } /** diff --git a/src/main/java/com/loohp/limbo/utils/CustomStringUtils.java b/src/main/java/com/loohp/limbo/utils/CustomStringUtils.java index 899742e..e10ed64 100644 --- a/src/main/java/com/loohp/limbo/utils/CustomStringUtils.java +++ b/src/main/java/com/loohp/limbo/utils/CustomStringUtils.java @@ -7,7 +7,7 @@ import java.util.List; public class CustomStringUtils { public static boolean arrayContains(String compare, String[] args, boolean IgnoreCase) { - return IgnoreCase ? Arrays.asList(args).stream().anyMatch(each -> each.equalsIgnoreCase(compare)) : Arrays.asList(args).stream().anyMatch(each -> each.equals(compare)); + return IgnoreCase ? Arrays.stream(args).anyMatch(each -> each.equalsIgnoreCase(compare)) : Arrays.asList(args).contains(compare); } public static boolean arrayContains(String compare, String[] args) { @@ -34,7 +34,7 @@ public class CustomStringUtils { } tokens.add(sb.toString()); - return tokens.toArray(new String[tokens.size()]); + return tokens.toArray(new String[0]); } public static int getIndexOfArg(String str, int ordinal) { diff --git a/src/main/java/com/loohp/limbo/utils/DataTypeIO.java b/src/main/java/com/loohp/limbo/utils/DataTypeIO.java index 25ad941..0d232bd 100644 --- a/src/main/java/com/loohp/limbo/utils/DataTypeIO.java +++ b/src/main/java/com/loohp/limbo/utils/DataTypeIO.java @@ -15,7 +15,7 @@ import java.util.UUID; public class DataTypeIO { public static void writeBlockPosition(DataOutputStream out, BlockPosition position) throws IOException { - out.writeLong(((position.getX() & 0x3FFFFFF) << 38) | ((position.getZ() & 0x3FFFFFF) << 12) | (position.getY() & 0xFFF)); + out.writeLong(((long) (position.getX() & 0x3FFFFFF) << 38) | ((long) (position.getZ() & 0x3FFFFFF) << 12) | (position.getY() & 0xFFF)); } public static void writeUUID(DataOutputStream out, UUID uuid) throws IOException { diff --git a/src/main/java/com/loohp/limbo/utils/YamlOrder.java b/src/main/java/com/loohp/limbo/utils/YamlOrder.java index 3eff3ad..4f93241 100644 --- a/src/main/java/com/loohp/limbo/utils/YamlOrder.java +++ b/src/main/java/com/loohp/limbo/utils/YamlOrder.java @@ -16,8 +16,8 @@ import java.util.*; public class YamlOrder extends PropertyUtils { private static final String TRANSIENT = "transient"; - private final Map, Map> propertiesCache = new HashMap, Map>(); - private final Map, Set> readableProperties = new HashMap, Set>(); + private final Map, Map> propertiesCache = new HashMap<>(); + private final Map, Set> readableProperties = new HashMap<>(); private BeanAccess beanAccess = BeanAccess.DEFAULT; private boolean allowReadOnlyProperties = false; private boolean skipMissingProperties = false; @@ -40,47 +40,44 @@ public class YamlOrder extends PropertyUtils { return propertiesCache.get(type); } - Map properties = new LinkedHashMap(); + Map properties = new LinkedHashMap<>(); boolean inaccessableFieldsExist = false; - switch (bAccess) { - case FIELD: - for (Class c = type; c != null; c = c.getSuperclass()) { - for (Field field : c.getDeclaredFields()) { - int modifiers = field.getModifiers(); - if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers) - && !properties.containsKey(field.getName())) { - properties.put(field.getName(), new FieldProperty(field)); - } + if (bAccess == BeanAccess.FIELD) { + for (Class c = type; c != null; c = c.getSuperclass()) { + for (Field field : c.getDeclaredFields()) { + int modifiers = field.getModifiers(); + if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers) + && !properties.containsKey(field.getName())) { + properties.put(field.getName(), new FieldProperty(field)); } } - break; - default: - try { - for (PropertyDescriptor property : Introspector.getBeanInfo(type) - .getPropertyDescriptors()) { - Method readMethod = property.getReadMethod(); - if ((readMethod == null || !readMethod.getName().equals("getClass")) - && !isTransient(property)) { - properties.put(property.getName(), new MethodProperty(property)); - } + } + } else { + try { + for (PropertyDescriptor property : Introspector.getBeanInfo(type) + .getPropertyDescriptors()) { + Method readMethod = property.getReadMethod(); + if ((readMethod == null || !readMethod.getName().equals("getClass")) + && !isTransient(property)) { + properties.put(property.getName(), new MethodProperty(property)); } - } catch (IntrospectionException e) { - throw new YAMLException(e); } + } catch (IntrospectionException e) { + throw new YAMLException(e); + } - for (Class c = type; c != null; c = c.getSuperclass()) { - for (Field field : c.getDeclaredFields()) { - int modifiers = field.getModifiers(); - if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)) { - if (Modifier.isPublic(modifiers)) { - properties.put(field.getName(), new FieldProperty(field)); - } else { - inaccessableFieldsExist = true; - } + for (Class c = type; c != null; c = c.getSuperclass()) { + for (Field field : c.getDeclaredFields()) { + int modifiers = field.getModifiers(); + if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)) { + if (Modifier.isPublic(modifiers)) { + properties.put(field.getName(), new FieldProperty(field)); + } else { + inaccessableFieldsExist = true; } } } - break; + } } if (properties.isEmpty() && inaccessableFieldsExist) { throw new YAMLException("No JavaBean properties found in " + type.getName()); @@ -94,11 +91,11 @@ public class YamlOrder extends PropertyUtils { return Boolean.TRUE.equals(fd.getValue(TRANSIENT)); } - public Set getProperties(Class type) { + public Set getProperties(Class type) { return getProperties(type, beanAccess); } - public Set getProperties(Class type, BeanAccess bAccess) { + public Set getProperties(Class type, BeanAccess bAccess) { if (readableProperties.containsKey(type)) { return readableProperties.get(type); } @@ -107,7 +104,7 @@ public class YamlOrder extends PropertyUtils { return properties; } - protected Set createPropertySet(Class type, BeanAccess bAccess) { + protected Set createPropertySet(Class type, BeanAccess bAccess) { Set properties = new LinkedHashSet<>(); Collection props = getPropertiesMap(type, bAccess).values(); for (Property property : props) { @@ -118,11 +115,11 @@ public class YamlOrder extends PropertyUtils { return properties; } - public Property getProperty(Class type, String name) { + public Property getProperty(Class type, String name) { return getProperty(type, name, beanAccess); } - public Property getProperty(Class type, String name, BeanAccess bAccess) { + public Property getProperty(Class type, String name, BeanAccess bAccess) { Map properties = getPropertiesMap(type, bAccess); Property property = properties.get(name); if (property == null && skipMissingProperties) { diff --git a/src/main/java/com/loohp/limbo/world/World.java b/src/main/java/com/loohp/limbo/world/World.java index 6eec824..4a508a1 100644 --- a/src/main/java/com/loohp/limbo/world/World.java +++ b/src/main/java/com/loohp/limbo/world/World.java @@ -57,7 +57,7 @@ public class World { 1281525273222484040L, 9548107335L}); chunk.setHeightMaps(heightMap); chunk.setBiomes(new int[256]); - chunk.setTileEntities(new ListTag(CompoundTag.class)); + chunk.setTileEntities(new ListTag<>(CompoundTag.class)); } }