2021-01-03 07:21:15 +00:00
|
|
|
package dev.plex.util;
|
2020-10-27 18:14:34 +00:00
|
|
|
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.Plex;
|
2022-01-27 21:38:44 +00:00
|
|
|
import dev.plex.PlexBase;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.config.Config;
|
|
|
|
import dev.plex.storage.StorageType;
|
2022-01-27 21:23:01 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
import java.util.stream.Collectors;
|
2022-01-27 09:00:50 +00:00
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
2022-01-27 21:23:01 +00:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.World;
|
2022-01-04 03:04:39 +00:00
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.PluginCommandYamlParser;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
import org.json.simple.JSONObject;
|
|
|
|
import org.json.simple.parser.JSONParser;
|
|
|
|
import org.json.simple.parser.ParseException;
|
|
|
|
|
2022-01-27 21:38:44 +00:00
|
|
|
public class PlexUtils extends PlexBase
|
2020-10-27 18:14:34 +00:00
|
|
|
{
|
2020-11-06 03:50:16 +00:00
|
|
|
public static Map<String, ChatColor> CHAT_COLOR_NAMES;
|
|
|
|
public static List<ChatColor> CHAT_COLOR_POOL;
|
2022-02-04 04:49:05 +00:00
|
|
|
public static List<String> DEVELOPERS =
|
|
|
|
Arrays.asList("78408086-1991-4c33-a571-d8fa325465b2", // Telesphoreo
|
|
|
|
"f5cd54c4-3a24-4213-9a56-c06c49594dff" // Taahh
|
|
|
|
);
|
2021-06-20 08:02:07 +00:00
|
|
|
private static final Random RANDOM;
|
2020-11-06 03:50:16 +00:00
|
|
|
|
|
|
|
static
|
|
|
|
{
|
|
|
|
RANDOM = new Random();
|
|
|
|
CHAT_COLOR_NAMES = new HashMap<>();
|
|
|
|
CHAT_COLOR_POOL = Arrays.asList(ChatColor.DARK_RED, ChatColor.RED, ChatColor.GOLD, ChatColor.YELLOW, ChatColor.GREEN, ChatColor.DARK_GREEN, ChatColor.AQUA, ChatColor.DARK_AQUA, ChatColor.BLUE, ChatColor.DARK_BLUE, ChatColor.DARK_PURPLE, ChatColor.LIGHT_PURPLE);
|
|
|
|
for (final ChatColor chatColor : CHAT_COLOR_POOL)
|
|
|
|
{
|
|
|
|
CHAT_COLOR_NAMES.put(chatColor.name().toLowerCase().replace("_", ""), chatColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ChatColor randomChatColor()
|
|
|
|
{
|
|
|
|
return CHAT_COLOR_POOL.get(RANDOM.nextInt(CHAT_COLOR_POOL.size()));
|
|
|
|
}
|
|
|
|
|
2020-10-27 18:14:34 +00:00
|
|
|
public static void testConnections()
|
|
|
|
{
|
|
|
|
if (Plex.get().getSqlConnection().getCon() != null)
|
|
|
|
{
|
2021-01-03 07:21:15 +00:00
|
|
|
if (Plex.get().getStorageType() == StorageType.MARIADB)
|
2020-10-27 18:14:34 +00:00
|
|
|
{
|
|
|
|
PlexLog.log("Successfully enabled MySQL!");
|
2020-10-28 03:49:56 +00:00
|
|
|
}
|
|
|
|
else if (Plex.get().getStorageType() == StorageType.SQLITE)
|
2020-10-27 18:14:34 +00:00
|
|
|
{
|
|
|
|
PlexLog.log("Successfully enabled SQLite!");
|
|
|
|
}
|
2020-10-28 03:49:56 +00:00
|
|
|
try
|
|
|
|
{
|
2020-10-27 18:14:34 +00:00
|
|
|
Plex.get().getSqlConnection().getCon().close();
|
|
|
|
}
|
2020-10-28 03:49:56 +00:00
|
|
|
catch (SQLException ignored)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Plex.get().getMongoConnection().getDatastore() != null)
|
2020-10-27 18:14:34 +00:00
|
|
|
{
|
|
|
|
PlexLog.log("Successfully enabled MongoDB!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 20:47:00 +00:00
|
|
|
public static boolean isPluginCMD(String cmd, String pluginName)
|
|
|
|
{
|
|
|
|
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginName);
|
|
|
|
if (plugin == null)
|
|
|
|
{
|
|
|
|
PlexLog.error(pluginName + " can not be found on the server! Make sure it is spelt correctly!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
List<Command> cmds = PluginCommandYamlParser.parse(plugin);
|
|
|
|
for (Command pluginCmd : cmds)
|
|
|
|
{
|
|
|
|
List<String> cmdAliases = pluginCmd.getAliases().size() > 0 ? pluginCmd.getAliases().stream().map(String::toLowerCase).collect(Collectors.toList()) : null;
|
|
|
|
if (pluginCmd.getName().equalsIgnoreCase(cmd) || (cmdAliases != null && cmdAliases.contains(cmd.toLowerCase())))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-31 15:09:13 +00:00
|
|
|
public static String color(String s)
|
|
|
|
{
|
|
|
|
return ChatColor.translateAlternateColorCodes('&', s);
|
|
|
|
}
|
|
|
|
|
2020-11-06 03:50:16 +00:00
|
|
|
public static String colorize(final String string)
|
|
|
|
{
|
|
|
|
return ChatColor.translateAlternateColorCodes('&', string);
|
|
|
|
}
|
|
|
|
|
2020-11-02 00:06:08 +00:00
|
|
|
// if you think the name of this method is dumb feel free to change it i just thought it'd be cool
|
|
|
|
public static String tl(String s, Object... objects)
|
|
|
|
{
|
2020-11-02 20:50:51 +00:00
|
|
|
if (s.equals("baseColor") || s.equals("errorColor") || s.equals("broadcastColor"))
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2020-11-02 20:50:51 +00:00
|
|
|
return getChatColorFromConfig(plugin.messages, ChatColor.WHITE, s).toString();
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2020-11-02 00:06:08 +00:00
|
|
|
String f = plugin.messages.getString(s);
|
|
|
|
if (f == null)
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2020-11-02 00:06:08 +00:00
|
|
|
return ChatColor.RED + "No message";
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2020-11-02 00:06:08 +00:00
|
|
|
for (Object object : objects)
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2020-11-05 13:40:48 +00:00
|
|
|
f = f.replaceFirst("<v>", String.valueOf(object));
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2020-11-02 00:06:08 +00:00
|
|
|
ChatColor base = getChatColorFromConfig(plugin.messages, ChatColor.GRAY, "baseColor");
|
2020-11-02 20:50:51 +00:00
|
|
|
ChatColor broadcast = getChatColorFromConfig(plugin.messages, ChatColor.AQUA, "broadcastColor");
|
2020-11-02 00:06:08 +00:00
|
|
|
ChatColor error = getChatColorFromConfig(plugin.messages, ChatColor.RED, "errorColor");
|
|
|
|
f = f.replaceAll("<r>", base.toString());
|
2020-11-02 20:50:51 +00:00
|
|
|
f = f.replaceAll("<b>", broadcast.toString());
|
2020-11-02 00:06:08 +00:00
|
|
|
f = f.replaceAll("<e>", error.toString());
|
2020-11-02 20:50:51 +00:00
|
|
|
f = color(f);
|
2020-11-02 00:06:08 +00:00
|
|
|
return base + f;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ChatColor getChatColorFromConfig(Config config, ChatColor def, String path)
|
|
|
|
{
|
|
|
|
ChatColor color;
|
|
|
|
if (config.getString(path) == null)
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2020-11-02 00:06:08 +00:00
|
|
|
color = def;
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2020-11-02 00:06:08 +00:00
|
|
|
else if (ChatColor.getByChar(config.getString(path)) == null)
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2020-11-02 00:06:08 +00:00
|
|
|
color = def;
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2020-11-02 00:06:08 +00:00
|
|
|
else
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2020-11-02 00:06:08 +00:00
|
|
|
color = ChatColor.getByChar(config.getString(path));
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2020-11-02 00:06:08 +00:00
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setBlocks(Location c1, Location c2, Material material)
|
|
|
|
{
|
|
|
|
if (!c1.getWorld().getName().equals(c1.getWorld().getName()))
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2020-11-02 00:06:08 +00:00
|
|
|
return;
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2022-01-30 01:53:22 +00:00
|
|
|
int sy = Math.min(c1.getBlockY(), c2.getBlockY()), ey = Math.max(c1.getBlockY(), c2.getBlockY()), sx = Math.min(c1.getBlockX(), c2.getBlockX()), ex = Math.max(c1.getBlockX(), c2.getBlockX()), sz = Math.min(c1.getBlockZ(), c2.getBlockZ()), ez = Math.max(c1.getBlockZ(), c2.getBlockZ());
|
2020-11-02 00:06:08 +00:00
|
|
|
World world = c1.getWorld();
|
|
|
|
for (int y = sy; y <= ey; y++)
|
|
|
|
{
|
|
|
|
for (int x = sx; x <= ex; x++)
|
|
|
|
{
|
|
|
|
for (int z = sz; z <= ez; z++)
|
|
|
|
{
|
|
|
|
world.getBlockAt(x, y, z).setType(material);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-02 20:50:51 +00:00
|
|
|
|
2020-11-03 00:19:26 +00:00
|
|
|
public static List<String> getPlayerNameList()
|
|
|
|
{
|
2022-01-30 01:53:22 +00:00
|
|
|
return Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList());
|
2020-11-03 00:19:26 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:50:51 +00:00
|
|
|
public static void broadcast(String s)
|
|
|
|
{
|
2022-01-27 09:00:50 +00:00
|
|
|
Bukkit.broadcast(LegacyComponentSerializer.legacyAmpersand().deserialize(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void broadcast(Component component)
|
|
|
|
{
|
|
|
|
Bukkit.broadcast(component);
|
2020-11-02 20:50:51 +00:00
|
|
|
}
|
2020-11-05 13:40:48 +00:00
|
|
|
|
|
|
|
public static Object simpleGET(String url) throws IOException, ParseException
|
|
|
|
{
|
|
|
|
URL u = new URL(url);
|
2020-11-06 01:29:38 +00:00
|
|
|
HttpURLConnection connection = (HttpURLConnection)u.openConnection();
|
2020-11-05 13:40:48 +00:00
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
String line;
|
|
|
|
StringBuilder content = new StringBuilder();
|
|
|
|
while ((line = in.readLine()) != null)
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2020-11-05 13:40:48 +00:00
|
|
|
content.append(line);
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2020-11-05 13:40:48 +00:00
|
|
|
in.close();
|
|
|
|
connection.disconnect();
|
|
|
|
return new JSONParser().parse(content.toString());
|
|
|
|
}
|
2020-11-05 21:17:14 +00:00
|
|
|
|
|
|
|
public static UUID getFromName(String name)
|
|
|
|
{
|
|
|
|
JSONObject profile;
|
2020-11-06 01:29:38 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
profile = (JSONObject)PlexUtils.simpleGET("https://api.ashcon.app/mojang/v2/user/" + name);
|
|
|
|
}
|
|
|
|
catch (IOException | ParseException e)
|
|
|
|
{
|
2020-11-05 21:17:14 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
2020-11-06 01:29:38 +00:00
|
|
|
String uuidString = (String)profile.get("uuid");
|
2020-11-05 21:17:14 +00:00
|
|
|
return UUID.fromString(uuidString);
|
|
|
|
}
|
2020-11-06 04:39:27 +00:00
|
|
|
|
|
|
|
public static int randomNum()
|
|
|
|
{
|
|
|
|
return ThreadLocalRandom.current().nextInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int randomNum(int limit)
|
|
|
|
{
|
|
|
|
return ThreadLocalRandom.current().nextInt(limit);
|
|
|
|
}
|
2022-01-30 01:53:22 +00:00
|
|
|
|
2020-11-06 04:39:27 +00:00
|
|
|
public static int randomNum(int start, int limit)
|
|
|
|
{
|
|
|
|
return ThreadLocalRandom.current().nextInt(start, limit);
|
|
|
|
}
|
2020-11-10 02:47:03 +00:00
|
|
|
|
|
|
|
public static long getDateNow()
|
|
|
|
{
|
|
|
|
return new Date().getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Date getDateFromLong(long epoch)
|
|
|
|
{
|
|
|
|
return new Date(epoch);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long hoursToSeconds(long hours)
|
|
|
|
{
|
2022-01-30 20:56:08 +00:00
|
|
|
return hours * 3600;
|
2020-11-10 02:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static long minutesToSeconds(long minutes)
|
|
|
|
{
|
|
|
|
return minutes * 60;
|
|
|
|
}
|
2020-10-27 18:14:34 +00:00
|
|
|
}
|