Plex/server/src/main/java/dev/plex/util/PlexUtils.java

275 lines
9.8 KiB
Java
Raw Normal View History

2021-01-03 07:21:15 +00:00
package dev.plex.util;
import com.google.common.base.CharMatcher;
2022-08-03 00:03:04 +00:00
import com.google.common.collect.Lists;
2021-01-03 07:21:15 +00:00
import dev.plex.Plex;
import dev.plex.PlexBase;
2022-08-03 00:03:04 +00:00
import dev.plex.listener.impl.ChatListener;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;
2021-01-03 07:21:15 +00:00
import dev.plex.storage.StorageType;
2022-05-10 05:08:45 +00:00
import java.sql.Connection;
import java.sql.SQLException;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;
2022-08-03 00:03:04 +00:00
import java.util.UUID;
2022-05-10 05:08:45 +00:00
import java.util.stream.Collectors;
2022-04-05 20:40:42 +00:00
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
2022-04-05 20:40:42 +00:00
import net.kyori.adventure.text.minimessage.MiniMessage;
2022-04-10 01:53:27 +00:00
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
2022-04-13 02:22:17 +00:00
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Particle;
2022-04-05 20:40:42 +00:00
import org.bukkit.command.Command;
import org.bukkit.command.PluginCommandYamlParser;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
2022-04-17 05:27:04 +00:00
public class PlexUtils implements PlexBase
{
private static final MiniMessage MINI_MESSAGE = MiniMessage.miniMessage();
2022-02-26 06:26:42 +00:00
public static List<String> DEVELOPERS =
Arrays.asList("78408086-1991-4c33-a571-d8fa325465b2", // Telesphoreo
2022-05-13 03:45:17 +00:00
"f5cd54c4-3a24-4213-9a56-c06c49594dff", // Taahh
2022-04-20 02:16:19 +00:00
"53b1512e-3481-4702-9f4f-63cb9c8be6a1", // supernt
2022-04-08 07:40:42 +00:00
"ca83b658-c03b-4106-9edc-72f70a80656d", // ayunami2000
2022-05-04 06:13:43 +00:00
"2e06e049-24c8-42e4-8bcf-d35372af31e6", // Fleek
"a52f1f08-a398-400a-bca4-2b74b81feae6" // Allink
2022-02-26 06:26:42 +00:00
);
public static <T> T addToArrayList(List<T> list, T object)
{
list.add(object);
return object;
}
2022-04-07 14:59:04 +00:00
public static void disabledEffect(Player player, Location location)
{
Particle.CLOUD.builder().location(location).receivers(player).extra(0).offset(0.5, 0.5, 0.5).count(5).spawn();
Particle.FLAME.builder().location(location).receivers(player).extra(0).offset(0.5, 0.5, 0.5).count(3).spawn();
Particle.SOUL_FIRE_FLAME.builder().location(location).receivers(player).offset(0.5, 0.5, 0.5).extra(0).count(2).spawn();
player.playSound(location, org.bukkit.Sound.BLOCK_FIRE_EXTINGUISH, 0.5f, 0.5f);
2022-04-07 14:59:04 +00:00
}
2022-04-07 17:12:38 +00:00
public static void disabledEffectMultiple(Player[] players, Location location)
{
2022-08-02 12:08:52 +00:00
if (players.length < 1)
{
return;
}
Particle.CLOUD.builder().location(location).receivers(players).extra(0).offset(0.5, 0.5, 0.5).count(5).spawn();
Particle.FLAME.builder().location(location).receivers(players).extra(0).offset(0.5, 0.5, 0.5).count(3).spawn();
Particle.SOUL_FIRE_FLAME.builder().location(location).receivers(players).offset(0.5, 0.5, 0.5).extra(0).count(2)
2022-08-02 12:08:52 +00:00
.spawn();
2022-04-07 17:12:38 +00:00
// note that the sound is played to everyone who is close enough to hear it
players[0].getWorld().playSound(location, org.bukkit.Sound.BLOCK_FIRE_EXTINGUISH, 0.5f, 0.5f);
}
public static void testConnections()
{
2022-04-07 07:37:31 +00:00
if (Plex.get().getSqlConnection().getDataSource() != null)
{
2023-03-08 11:45:47 +00:00
try (Connection ignored = Plex.get().getSqlConnection().getCon())
{
2022-04-07 07:37:31 +00:00
if (Plex.get().getStorageType() == StorageType.MARIADB)
{
PlexLog.log("Successfully enabled MySQL!");
2022-05-10 05:08:45 +00:00
}
else if (Plex.get().getStorageType() == StorageType.SQLITE)
2022-04-07 07:37:31 +00:00
{
PlexLog.log("Successfully enabled SQLite!");
}
2022-05-10 05:08:45 +00:00
}
catch (SQLException e)
{
2022-04-07 07:37:31 +00:00
if (Plex.get().getMongoConnection().getDatastore() != null)
{
PlexLog.log("Successfully enabled MongoDB!");
}
}
2022-05-10 05:08:45 +00:00
}
else
{
if (Plex.get().getMongoConnection().getDatastore() != null)
2020-10-28 03:49:56 +00:00
{
PlexLog.log("Successfully enabled MongoDB!");
2020-10-28 03:49:56 +00:00
}
2022-02-26 06:24:11 +00:00
}
}
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)
{
2023-03-08 11:45:47 +00:00
List<String> cmdAliases = pluginCmd.getAliases().size() > 0 ? pluginCmd.getAliases().stream().map(String::toLowerCase).toList() : null;
2020-10-28 20:47:00 +00:00
if (pluginCmd.getName().equalsIgnoreCase(cmd) || (cmdAliases != null && cmdAliases.contains(cmd.toLowerCase())))
{
return true;
}
}
return false;
}
public static String mmStripColor(String input)
{
return PlainTextComponentSerializer.plainText().serialize(mmDeserialize(input));
}
2022-04-10 01:53:27 +00:00
public static Component mmDeserialize(String input)
{
2022-04-10 17:44:36 +00:00
boolean aprilFools = true; // true by default
if (plugin.config.contains("april_fools"))
{
aprilFools = plugin.config.getBoolean("april_fools");
}
ZonedDateTime date = ZonedDateTime.now(ZoneId.systemDefault());
2022-04-10 17:45:52 +00:00
if (aprilFools && date.getMonth() == Month.APRIL && date.getDayOfMonth() == 1)
2022-04-10 05:10:35 +00:00
{
Component component = MINI_MESSAGE.deserialize(input); // removes existing tags
return MINI_MESSAGE.deserialize("<rainbow>" + PlainTextComponentSerializer.plainText().serialize(component));
2022-04-10 05:10:35 +00:00
}
return MINI_MESSAGE.deserialize(input);
2022-04-10 05:10:35 +00:00
}
public static Component mmCustomDeserialize(String input, TagResolver... resolvers)
{
return MiniMessage.builder().tags(TagResolver.builder().resolvers(resolvers).build()).build().deserialize(input);
2022-04-10 01:53:27 +00:00
}
2022-02-25 07:09:55 +00:00
public static Component messageComponent(String entry, Object... objects)
{
return MINI_MESSAGE.deserialize(messageString(entry, objects));
2022-02-25 07:09:55 +00:00
}
public static Component messageComponent(String entry, Component... objects)
{
Component component = MINI_MESSAGE.deserialize(messageString(entry));
for (int i = 0; i < objects.length; i++)
{
int finalI = i;
component = component.replaceText(builder -> builder.matchLiteral("{" + finalI + "}").replacement(objects[finalI]).build());
}
return component;
}
2022-02-25 07:09:55 +00:00
public static String messageString(String entry, Object... objects)
{
String f = plugin.messages.getString(entry);
2022-02-25 07:09:55 +00:00
if (f == null)
{
throw new NullPointerException();
}
for (int i = 0; i < objects.length; i++)
{
f = f.replace("{" + i + "}", String.valueOf(objects[i]));
2022-02-25 07:09:55 +00:00
}
return f;
}
public static String getTextFromComponent(Component component)
{
try
{
2022-11-26 10:24:12 +00:00
return ((TextComponent)component).content();
2022-05-10 05:08:45 +00:00
}
catch (Exception e)
{
PlexLog.warn("Unable to get text of component", e.getLocalizedMessage());
return "";
}
}
public static String getTextFromComponents(Component... components)
{
try
{
StringBuilder builder = new StringBuilder();
for (Component component : components)
{
builder.append(getTextFromComponent(component));
}
return builder.toString();
2022-05-10 05:08:45 +00:00
}
catch (Exception e)
{
PlexLog.warn("Unable to get text of components", e.getLocalizedMessage());
return "";
}
}
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-02 20:50:51 +00:00
public static void broadcast(String s)
{
Bukkit.broadcast(MINI_MESSAGE.deserialize(s));
}
public static void broadcast(Component component)
{
Bukkit.broadcast(component);
2020-11-02 20:50:51 +00:00
}
2022-04-13 02:08:12 +00:00
public static void broadcastToAdmins(Component component)
{
2022-05-18 10:31:15 +00:00
Bukkit.getOnlinePlayers().stream().filter(pl -> plugin.getPlayerCache().getPlexPlayer(pl.getUniqueId()).isAdminActive()).forEach(pl ->
2022-04-17 05:27:04 +00:00
{
pl.sendMessage(component);
});
2022-04-13 02:08:12 +00:00
}
2022-08-03 00:03:04 +00:00
public static List<UUID> adminChat(String senderName, String message, UUID... ignore)
{
List<UUID> sent = Lists.newArrayList();
for (Player player : Bukkit.getOnlinePlayers())
{
if (Arrays.stream(ignore).anyMatch(uuid -> player.getUniqueId().equals(uuid)))
{
continue;
}
if (plugin.getSystem().equalsIgnoreCase("ranks"))
{
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(player.getUniqueId());
if (plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN) && plexPlayer.isAdminActive())
{
player.sendMessage(messageComponent("adminChatFormat", senderName, message).replaceText(ChatListener.URL_REPLACEMENT_CONFIG));
sent.add(player.getUniqueId());
}
}
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{
if (player.hasPermission("plex.adminchat"))
{
player.sendMessage(PlexUtils.messageComponent("adminChatFormat", senderName, message).replaceText(ChatListener.URL_REPLACEMENT_CONFIG));
sent.add(player.getUniqueId());
}
}
}
return sent;
}
public static String cleanString(String input)
{
return CharMatcher.ascii().retainFrom(input);
}
}