Raxen/src/main/java/me/unurled/raxen/utils/Utils.java
unurled b24ddfba27 gq to me
change of domain name
2022-06-09 23:27:04 +02:00

369 lines
12 KiB
Java

package me.unurled.raxen.utils;
import me.unurled.raxen.Raxen;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.nio.file.*;
public class Utils {
/**
* convert a string with mini-message formatting to a colored Component
* @param stringToComponent a string
* @return a component
*/
public static @NotNull Component colorComp(String stringToComponent) {
MiniMessage minimessage = MiniMessage.builder()
.tags(TagResolver.builder()
.resolver(StandardTags.color())
.resolver(StandardTags.decorations())
.resolver(StandardTags.reset())
.build()
)
.build();
return minimessage.deserialize(stringToComponent);
}
/**
* convert a string with mini-message formatting to a colored TextComponent
* @param stringToComponent a string
* @return a component
*/
public static @NotNull TextComponent colorTextComp(String stringToComponent) {
MiniMessage minimessage = MiniMessage.builder()
.tags(TagResolver.builder()
.resolver(StandardTags.color())
.resolver(StandardTags.decorations())
.resolver(StandardTags.reset())
.build()
)
.build();
return (TextComponent) minimessage.deserialize(stringToComponent);
}
/**
* change the mini-message colorized format to the legacy format, with §
* @param string a string
* @return a colored string
*/
public static @NotNull String colorString(String string) {
MiniMessage minimessage = MiniMessage.builder()
.tags(TagResolver.builder()
.resolver(StandardTags.color())
.resolver(StandardTags.decorations())
.resolver(StandardTags.reset())
.build()
)
.build();
return LegacyComponentSerializer.legacyAmpersand().serialize(minimessage.deserialize(string));
}
/**
* change the mini-message colorized format to the legacy format, with §
* @param string a string
* @return a String
*/
public static String coloredString(String string) {
MiniMessage minimessage = MiniMessage.builder()
.tags(TagResolver.builder()
.resolver(StandardTags.color())
.resolver(StandardTags.decorations())
.resolver(StandardTags.reset())
.build()
)
.build();
return ChatColor.translateAlternateColorCodes('&', LegacyComponentSerializer.legacyAmpersand()
.serialize(minimessage.deserialize(string)));
}
/**
* transform a text-component to a string
* @param comp a string
* @return a String
*/
public static String textCompToString(TextComponent comp) {
MiniMessage minimessage = MiniMessage.builder()
.tags(TagResolver.builder()
.resolver(StandardTags.color())
.resolver(StandardTags.decorations())
.resolver(StandardTags.reset())
.build()
)
.build();
return ChatColor.translateAlternateColorCodes('&', LegacyComponentSerializer.legacyAmpersand()
.serialize(comp));
}
/**
* Strips all color from a string.
* @param string the string that want's to be decolored.
* @return a decolored string
*/
public static String decolor(String string) {
return MiniMessage.miniMessage().stripTags(string);
}
/**
* debug output to console if debug is set to true in config
* @param main Raxen instance
* @param strings output to console
*/
public static void debug(Raxen main, String... strings) {
FileConfiguration config = main.getConfig();
if(config.getBoolean("debug")) {
for(String string : strings) {
main.getLogger().info(string);
}
}
}
/**
* debug output to console if debug is set to true in config
* @param strings output to console
*/
public static void debug(String... strings) {
Raxen main = (Raxen) Bukkit.getPluginManager().getPlugin("Raxen");
assert main != null;
FileConfiguration config = main.getConfig();
if(config.getBoolean("debug")) {
for(String string : strings) {
main.getLogger().info(string);
}
}
}
/**
* debug output to console if debug is set to true in config
* @param config main file config
* @param strings ouput to console
*/
public static void debug(FileConfiguration config, String... strings) {
Raxen main = (Raxen) Bukkit.getPluginManager().getPlugin("Raxen");
assert main != null;
if(config.getBoolean("debug")) {
for(String string : strings) {
main.getLogger().info(string);
}
}
}
/**
* Log the strings to the console
* @param main a main instance running
* @param strings string that want to be outputted.
*/
public static void log(Raxen main, String... strings) {
for(String string : strings) {
main.getLogger().info(string);
}
}
/**
* Log the strings to the console
* @param strings string that want to be outputted.
*/
public static void log(String... strings) {
Raxen main = (Raxen) Bukkit.getPluginManager().getPlugin("Raxen");
assert main != null;
for(String string : strings) {
main.getLogger().info(string);
}
}
/**
* Log the TextComponent to the console
* @param comp TextComponent that want to be outputted.
*/
public static void log(TextComponent... comp) {
Raxen main = (Raxen) Bukkit.getPluginManager().getPlugin("Raxen");
assert main != null;
for(TextComponent string : comp) {
main.getServer().sendMessage(string);
}
}
/**
* Warns strings to the console using the logger
* @param strings to warn message
*/
public static void warn(String... strings) {
Raxen main = (Raxen) Bukkit.getPluginManager().getPlugin("Raxen");
assert main != null;
for(String string : strings) {
main.getLogger().warning(string);
}
}
/**
* Warns strings to the console using the logger
* @param strings to warn message
* @param main a running instance
*/
public static void warn(Raxen main, String... strings) {
for(String string : strings) {
main.getLogger().warning(string);
}
}
/**
* Error strings to the console using the logger
* @param strings to error message
*/
public static void error(String... strings) {
Raxen main = (Raxen) Bukkit.getPluginManager().getPlugin("Raxen");
assert main != null;
for(String string : strings) {
main.getLogger().severe(string);
}
}
/**
* Error strings to the console using the logger
* @param strings to error message
* @param main a main instance running
*/
public static void error(Raxen main, String... strings) {
for(String string : strings) {
main.getLogger().severe(string);
}
}
/**
* message strings to player.
* @param player an online player
* @param strings strings that will be sent.
*/
public static void msgPlayer(Player player, String... strings) {
for(String string : strings) {
player.sendMessage(colorComp(string));
}
}
/**
* message textComponent to player.
* @param player an online player
* @param comp textComponent that will be sent.
*/
public static void msgPlayer(Player player, TextComponent comp) {
player.sendMessage(colorComp(PlainTextComponentSerializer.plainText().serialize(comp)));
}
/**
* message the command sender with textComponent.
* @param player the command sender
* @param textComponent message
*/
public static void msgSender(CommandSender player, TextComponent textComponent) {
player.sendMessage(colorComp(PlainTextComponentSerializer.plainText().serialize(textComponent)));
}
/**
* message the command sender with strings.
* @param player the command sender
* @param strings message
*/
public static void msgSender(CommandSender player, String... strings) {
for(String string : strings) {
player.sendMessage(colorComp(string));
}
}
/**
* fill an inventory with grey glass pane.
* @param inv an inventory
* @return the inventory filled
*/
public static Inventory fillGreyPane(Inventory inv) {
int in = -1;
for (ItemStack it : inv) {
in++;
if (it == null || it.getType() == Material.AIR) {
inv.setItem(in, Items.greyPane());
}
}
return inv;
}
/**
* output message when player have no perms
* @return a component
*/
public static Component noPerms() {
return colorComp("<red>You don't have the permission to use this feature.");
}
/**
* error message output to player
* @return a component
*/
public static Component error() {
return colorComp("<red>An Error has occurred. Please retry or contact an Admin.");
}
public static void errorConsoleSender(CommandSender sender) {
sender.sendMessage(colorComp("<red>Can't use this command as the console.</red>"));
}
/**
* Copy folder from sourceDirectoryLocation to destinationDirectoryLocation
* @param sourceDirectoryLocation the source directory
* @param destinationDirectoryLocation the copy location
* @throws IOException exception with coping data
*/
public static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) throws IOException {
Files.walk(Paths.get(sourceDirectoryLocation))
.forEach(source -> {
Path destination = Paths.get(destinationDirectoryLocation, source.toString()
.substring(sourceDirectoryLocation.length()));
try {
Files.copy(source, destination);
} catch (IOException e) {
e.printStackTrace();
}
});
}
/**
* remove path directory
* @param path removed folder
* @throws IOException error with handling data
*/
public static void removeDirectory(Path path) throws IOException {
if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
try (DirectoryStream<Path> entries = Files.newDirectoryStream(path)) {
for (Path entry : entries) {
removeDirectory(entry);
}
}
}
Files.delete(path);
}
public static boolean isInt(String strNum) {
if (strNum == null) {
return false;
}
try {
double d = Integer.parseInt(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
}