From b2b03f3e40ad5949618bbdaa296d9a3f8bb032ae Mon Sep 17 00:00:00 2001 From: filoghost Date: Sun, 24 Jan 2021 10:45:58 +0100 Subject: [PATCH] Small refactoring --- .../holographicdisplays/common/Utils.java | 17 +++++++++-------- .../placeholder/AnimationsRegister.java | 2 +- .../placeholder/CyclicPlaceholderReplacer.java | 10 ++++++---- .../placeholder/PlaceholdersRegister.java | 4 ++-- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/common/src/main/java/me/filoghost/holographicdisplays/common/Utils.java b/common/src/main/java/me/filoghost/holographicdisplays/common/Utils.java index 8149e2c8..9b49f969 100644 --- a/common/src/main/java/me/filoghost/holographicdisplays/common/Utils.java +++ b/common/src/main/java/me/filoghost/holographicdisplays/common/Utils.java @@ -7,19 +7,20 @@ package me.filoghost.holographicdisplays.common; import me.filoghost.fcommons.Preconditions; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + public class Utils { /** - * Converts a generic array to an array of Strings using the method toString(). - * @param array the array to convert - * @return the new generated array of Strings + * Converts a generic array to a list of Strings using the method toString(). */ - public static String[] arrayToStrings(Object... array) { - String[] result = new String[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = array[i] != null ? array[i].toString() : null; + public static List toStringList(Object... array) { + List result = new ArrayList<>(array.length); + for (Object obj : array) { + result.add(Objects.toString(obj)); } - return result; } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/AnimationsRegister.java b/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/AnimationsRegister.java index 4100f2a2..e1c6eef6 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/AnimationsRegister.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/AnimationsRegister.java @@ -77,7 +77,7 @@ public class AnimationsRegister { lines.set(i, StringConverter.toReadableFormat(lines.get(i))); } - animations.put(file.getFileName().toString(), new Placeholder(HolographicDisplays.getInstance(), file.getFileName().toString(), speed, new CyclicPlaceholderReplacer(lines.toArray(new String[0])))); + animations.put(file.getFileName().toString(), new Placeholder(HolographicDisplays.getInstance(), file.getFileName().toString(), speed, new CyclicPlaceholderReplacer(lines))); DebugLogger.info("Successfully loaded animation '" + file.getFileName() + "', speed = " + speed + "."); } catch (Exception e) { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/CyclicPlaceholderReplacer.java b/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/CyclicPlaceholderReplacer.java index 085b7f52..99656175 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/CyclicPlaceholderReplacer.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/CyclicPlaceholderReplacer.java @@ -7,22 +7,24 @@ package me.filoghost.holographicdisplays.placeholder; import me.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; +import java.util.List; + public class CyclicPlaceholderReplacer implements PlaceholderReplacer { - String[] frames; + List frames; private int index; - public CyclicPlaceholderReplacer(String[] frames) { + public CyclicPlaceholderReplacer(List frames) { this.frames = frames; index = 0; } @Override public String update() { - String result = frames[index]; + String result = frames.get(index); index++; - if (index >= frames.length) { + if (index >= frames.size()) { index = 0; } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/PlaceholdersRegister.java b/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/PlaceholdersRegister.java index 8c4c6382..857af34c 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/PlaceholdersRegister.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/PlaceholdersRegister.java @@ -6,8 +6,8 @@ package me.filoghost.holographicdisplays.placeholder; import me.filoghost.holographicdisplays.HolographicDisplays; -import me.filoghost.holographicdisplays.disk.Configuration; import me.filoghost.holographicdisplays.common.Utils; +import me.filoghost.holographicdisplays.disk.Configuration; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.plugin.Plugin; @@ -40,7 +40,7 @@ public class PlaceholdersRegister { return Configuration.timeFormat.format(new Date()); })); - register(new Placeholder(HolographicDisplays.getInstance(), "&u", 0.2, new CyclicPlaceholderReplacer(Utils.arrayToStrings( + register(new Placeholder(HolographicDisplays.getInstance(), "&u", 0.2, new CyclicPlaceholderReplacer(Utils.toStringList( ChatColor.RED, ChatColor.GOLD, ChatColor.YELLOW,