Small refactoring

This commit is contained in:
filoghost 2021-05-04 16:10:18 +02:00
parent 177598ae78
commit aacc7fdeb4
8 changed files with 24 additions and 58 deletions

View File

@ -5,58 +5,14 @@
*/
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<String> toStringList(Object... array) {
List<String> 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());
}

View File

@ -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);

View File

@ -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);

View File

@ -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();

View File

@ -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() + ".");

View File

@ -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() {

View File

@ -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<String> toStringList(ChatColor... colors) {
return CollectionUtils.toArrayList(Arrays.asList(colors), ChatColor::toString);
}
}

View File

@ -215,7 +215,7 @@
<dependency>
<groupId>me.filoghost.fcommons</groupId>
<artifactId>fcommons</artifactId>
<version>1.2.7</version>
<version>1.2.8-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>