From aacc7fdeb48cee4172e4bcc6fcc9cc63f9ffe86e Mon Sep 17 00:00:00 2001 From: filoghost Date: Tue, 4 May 2021 16:10:18 +0200 Subject: [PATCH] Small refactoring --- .../holographicdisplays/core/Utils.java | 44 ------------------- .../commands/subs/AddlineCommand.java | 4 +- .../commands/subs/CreateCommand.java | 4 +- .../commands/subs/InsertlineCommand.java | 4 +- .../commands/subs/SetlineCommand.java | 4 +- .../object/base/BaseHologramComponent.java | 10 +++-- .../internal/DefaultPlaceholders.java | 10 ++++- pom.xml | 2 +- 8 files changed, 24 insertions(+), 58 deletions(-) diff --git a/core/src/main/java/me/filoghost/holographicdisplays/core/Utils.java b/core/src/main/java/me/filoghost/holographicdisplays/core/Utils.java index c532706d..9be5f85d 100644 --- a/core/src/main/java/me/filoghost/holographicdisplays/core/Utils.java +++ b/core/src/main/java/me/filoghost/holographicdisplays/core/Utils.java @@ -5,57 +5,13 @@ */ package me.filoghost.holographicdisplays.core; -import me.filoghost.fcommons.Preconditions; import me.filoghost.fcommons.Strings; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - public class Utils { - - /** - * Converts a generic array to a list of Strings using the method toString(). - */ - public static List toStringList(Object... array) { - List result = new ArrayList<>(array.length); - for (Object obj : array) { - result.add(Objects.toString(obj)); - } - return result; - } - - public static int floor(double num) { - int floor = (int) num; - return floor == num ? floor : floor - (int) (Double.doubleToRawLongBits(num) >>> 63); - } - public static double square(double num) { return num * num; } - - - public static String join(String[] elements, String separator, int startIndex, int endIndex) { - Preconditions.checkArgument(startIndex >= 0 && startIndex < elements.length, "startIndex out of bounds"); - Preconditions.checkArgument(endIndex >= 0 && endIndex <= elements.length, "endIndex out of bounds"); - Preconditions.checkArgument(startIndex <= endIndex, "startIndex lower than endIndex"); - - StringBuilder result = new StringBuilder(); - - while (startIndex < endIndex) { - if (result.length() != 0) { - result.append(separator); - } - - if (elements[startIndex] != null) { - result.append(elements[startIndex]); - } - startIndex++; - } - - return result.toString(); - } public static String formatExceptionMessage(Throwable t) { return formatExceptionMessage(t.getMessage()); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AddlineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AddlineCommand.java index bc948d6f..d211a7b3 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AddlineCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AddlineCommand.java @@ -5,12 +5,12 @@ */ package me.filoghost.holographicdisplays.commands.subs; +import me.filoghost.fcommons.Strings; import me.filoghost.fcommons.command.sub.SubCommandContext; import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.holographicdisplays.Colors; import me.filoghost.holographicdisplays.commands.HologramCommandManager; import me.filoghost.holographicdisplays.commands.HologramCommandValidate; -import me.filoghost.holographicdisplays.core.Utils; import me.filoghost.holographicdisplays.disk.ConfigManager; import me.filoghost.holographicdisplays.event.InternalHologramEditEvent; import me.filoghost.holographicdisplays.object.internal.InternalHologram; @@ -39,7 +39,7 @@ public class AddlineCommand extends LineEditingCommand implements QuickEditComma @Override public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { InternalHologram hologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[0]); - String serializedLine = Utils.join(args, " ", 1, args.length); + String serializedLine = Strings.joinFrom(" ", args, 1); InternalHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine); hologram.addLine(line); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CreateCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CreateCommand.java index f220c57d..21c8f913 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CreateCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CreateCommand.java @@ -5,13 +5,13 @@ */ package me.filoghost.holographicdisplays.commands.subs; +import me.filoghost.fcommons.Strings; import me.filoghost.fcommons.command.sub.SubCommandContext; import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.fcommons.command.validation.CommandValidate; import me.filoghost.holographicdisplays.Colors; import me.filoghost.holographicdisplays.commands.HologramCommandValidate; import me.filoghost.holographicdisplays.commands.HologramSubCommand; -import me.filoghost.holographicdisplays.core.Utils; import me.filoghost.holographicdisplays.disk.ConfigManager; import me.filoghost.holographicdisplays.object.internal.InternalHologram; import me.filoghost.holographicdisplays.object.internal.InternalHologramLine; @@ -62,7 +62,7 @@ public class CreateCommand extends HologramSubCommand { InternalHologramLine line; if (args.length > 1) { - String text = Utils.join(args, " ", 1, args.length); + String text = Strings.joinFrom(" ", args, 1); CommandValidate.check(!text.equalsIgnoreCase("{empty}"), "The first line should not be empty."); line = HologramCommandValidate.parseHologramLine(hologram, text); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InsertlineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InsertlineCommand.java index 6d89d16b..84d38d03 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InsertlineCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InsertlineCommand.java @@ -5,6 +5,7 @@ */ package me.filoghost.holographicdisplays.commands.subs; +import me.filoghost.fcommons.Strings; import me.filoghost.fcommons.command.sub.SubCommandContext; import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.fcommons.command.validation.CommandValidate; @@ -12,7 +13,6 @@ import me.filoghost.holographicdisplays.Colors; import me.filoghost.holographicdisplays.commands.HologramCommandManager; import me.filoghost.holographicdisplays.commands.HologramCommandValidate; import me.filoghost.holographicdisplays.commands.Messages; -import me.filoghost.holographicdisplays.core.Utils; import me.filoghost.holographicdisplays.disk.ConfigManager; import me.filoghost.holographicdisplays.event.InternalHologramEditEvent; import me.filoghost.holographicdisplays.object.internal.InternalHologram; @@ -45,7 +45,7 @@ public class InsertlineCommand extends LineEditingCommand implements QuickEditCo public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { InternalHologram hologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[0]); int insertAfterIndex = CommandValidate.parseInteger(args[1]); - String serializedLine = Utils.join(args, " ", 2, args.length); + String serializedLine = Strings.joinFrom(" ", args, 2); int oldLinesAmount = hologram.getLinesAmount(); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/SetlineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/SetlineCommand.java index 183d453a..2d9301d0 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/SetlineCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/SetlineCommand.java @@ -5,13 +5,13 @@ */ package me.filoghost.holographicdisplays.commands.subs; +import me.filoghost.fcommons.Strings; import me.filoghost.fcommons.command.sub.SubCommandContext; import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.fcommons.command.validation.CommandValidate; import me.filoghost.holographicdisplays.Colors; import me.filoghost.holographicdisplays.commands.HologramCommandManager; import me.filoghost.holographicdisplays.commands.HologramCommandValidate; -import me.filoghost.holographicdisplays.core.Utils; import me.filoghost.holographicdisplays.disk.ConfigManager; import me.filoghost.holographicdisplays.event.InternalHologramEditEvent; import me.filoghost.holographicdisplays.object.internal.InternalHologram; @@ -40,7 +40,7 @@ public class SetlineCommand extends LineEditingCommand implements QuickEditComma @Override public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { InternalHologram hologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[0]); - String serializedLine = Utils.join(args, " ", 2, args.length); + String serializedLine = Strings.joinFrom(" ", args, 2); int lineNumber = CommandValidate.parseInteger(args[1]); CommandValidate.check(lineNumber >= 1 && lineNumber <= hologram.getLinesAmount(), "The line number must be between 1 and " + hologram.getLinesAmount() + "."); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/base/BaseHologramComponent.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/base/BaseHologramComponent.java index f0d02644..9cc94d45 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/object/base/BaseHologramComponent.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/base/BaseHologramComponent.java @@ -6,7 +6,6 @@ package me.filoghost.holographicdisplays.object.base; import me.filoghost.fcommons.Preconditions; -import me.filoghost.holographicdisplays.core.Utils; import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.World; @@ -43,8 +42,13 @@ public abstract class BaseHologramComponent { this.x = x; this.y = y; this.z = z; - this.chunkX = Utils.floor(x) >> 4; - this.chunkZ = Utils.floor(z) >> 4; + this.chunkX = floor(x) >> 4; + this.chunkZ = floor(z) >> 4; + } + + private static int floor(double num) { + int floor = (int) num; + return floor == num ? floor : floor - (int) (Double.doubleToRawLongBits(num) >>> 63); } public World getWorld() { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/internal/DefaultPlaceholders.java b/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/internal/DefaultPlaceholders.java index 2a98d5de..ffa50f83 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/internal/DefaultPlaceholders.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/internal/DefaultPlaceholders.java @@ -5,16 +5,18 @@ */ package me.filoghost.holographicdisplays.placeholder.internal; +import me.filoghost.fcommons.collection.CollectionUtils; import me.filoghost.holographicdisplays.HolographicDisplays; import me.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerTracker; import me.filoghost.holographicdisplays.bridge.bungeecord.ServerInfo; -import me.filoghost.holographicdisplays.core.Utils; import me.filoghost.holographicdisplays.disk.Configuration; import me.filoghost.holographicdisplays.placeholder.registry.PlaceholderRegistry; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import java.time.Instant; +import java.util.Arrays; +import java.util.List; public class DefaultPlaceholders { @@ -26,7 +28,7 @@ public class DefaultPlaceholders { placeholderRegistry.unregisterAll(plugin); // TODO restore "&u" - placeholderRegistry.register(plugin, "rainbow", new AnimationPlaceholder(4, Utils.toStringList( + placeholderRegistry.register(plugin, "rainbow", new AnimationPlaceholder(4, toStringList( ChatColor.RED, ChatColor.GOLD, ChatColor.YELLOW, @@ -100,4 +102,8 @@ public class DefaultPlaceholders { }); } + private static List toStringList(ChatColor... colors) { + return CollectionUtils.toArrayList(Arrays.asList(colors), ChatColor::toString); + } + } diff --git a/pom.xml b/pom.xml index 09ae1628..e5c1020e 100644 --- a/pom.xml +++ b/pom.xml @@ -215,7 +215,7 @@ me.filoghost.fcommons fcommons - 1.2.7 + 1.2.8-SNAPSHOT