2021-01-03 07:21:15 +00:00
|
|
|
package dev.plex.util;
|
2020-10-27 18:14:34 +00:00
|
|
|
|
2022-03-19 03:33:23 +00:00
|
|
|
import com.google.common.collect.ImmutableSet;
|
|
|
|
import com.google.common.reflect.ClassPath;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.Plex;
|
2022-01-27 21:38:44 +00:00
|
|
|
import dev.plex.PlexBase;
|
2022-04-11 17:56:26 +00:00
|
|
|
import dev.plex.cache.DataUtils;
|
2022-04-13 02:08:12 +00:00
|
|
|
import dev.plex.cache.PlayerCache;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.config.Config;
|
2022-04-11 17:56:26 +00:00
|
|
|
import dev.plex.permission.Permission;
|
|
|
|
import dev.plex.player.PlexPlayer;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.storage.StorageType;
|
2022-04-13 02:22:17 +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.Connection;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.time.Instant;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.time.Month;
|
|
|
|
import java.time.ZoneId;
|
2022-04-10 23:30:21 +00:00
|
|
|
import java.time.format.DateTimeFormatter;
|
2022-04-13 02:22:17 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.TimeZone;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
import java.util.stream.Collectors;
|
2022-04-05 20:40:42 +00:00
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
2022-04-10 01:53:27 +00:00
|
|
|
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
2022-04-13 02:22:17 +00:00
|
|
|
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
|
2022-04-10 03:24:44 +00:00
|
|
|
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
2022-04-05 20:40:42 +00:00
|
|
|
import org.apache.commons.lang.math.NumberUtils;
|
2022-04-13 02:22:17 +00:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.GameRule;
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.Particle;
|
|
|
|
import org.bukkit.World;
|
2022-04-05 20:40:42 +00:00
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.PluginCommandYamlParser;
|
|
|
|
import org.bukkit.entity.Player;
|
2022-04-11 17:56:26 +00:00
|
|
|
import org.bukkit.permissions.PermissionAttachment;
|
2022-04-05 20:40:42 +00:00
|
|
|
import org.bukkit.plugin.Plugin;
|
2022-04-11 17:56:26 +00:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2022-04-05 20:40:42 +00:00
|
|
|
import org.json.simple.JSONObject;
|
|
|
|
import org.json.simple.parser.JSONParser;
|
|
|
|
import org.json.simple.parser.ParseException;
|
2022-04-04 08:36:50 +00:00
|
|
|
|
2022-04-17 05:27:04 +00:00
|
|
|
public class PlexUtils implements PlexBase
|
2020-10-27 18:14:34 +00:00
|
|
|
{
|
2021-06-20 08:02:07 +00:00
|
|
|
private static final Random RANDOM;
|
2022-04-19 20:16:07 +00:00
|
|
|
public static final List<String> timeUnits = new ArrayList<>()
|
2022-02-26 06:24:11 +00:00
|
|
|
{{
|
|
|
|
add("y");
|
|
|
|
add("mo");
|
|
|
|
add("w");
|
|
|
|
add("d");
|
|
|
|
add("h");
|
|
|
|
add("m");
|
|
|
|
add("s");
|
|
|
|
}};
|
2022-02-26 06:26:42 +00:00
|
|
|
public static Map<String, ChatColor> CHAT_COLOR_NAMES;
|
|
|
|
public static List<ChatColor> CHAT_COLOR_POOL;
|
|
|
|
public static List<String> DEVELOPERS =
|
|
|
|
Arrays.asList("78408086-1991-4c33-a571-d8fa325465b2", // Telesphoreo
|
2022-04-07 14:27:57 +00:00
|
|
|
"f5cd54c4-3a24-4213-9a56-c06c49594dff", // Taahh
|
2022-04-08 07:40:42 +00:00
|
|
|
"ca83b658-c03b-4106-9edc-72f70a80656d", // ayunami2000
|
|
|
|
"2e06e049-24c8-42e4-8bcf-d35372af31e6" //Fleek
|
2022-02-26 06:26:42 +00:00
|
|
|
);
|
2022-04-10 23:30:21 +00:00
|
|
|
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mm:ss a z");
|
|
|
|
private static final Set<String> TIMEZONES = Set.of(TimeZone.getAvailableIDs());
|
|
|
|
private static String TIMEZONE = Plex.get().config.getString("server.timezone");
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-11 17:56:26 +00:00
|
|
|
public static void setupPermissions(@NotNull Player player)
|
|
|
|
{
|
|
|
|
PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId());
|
|
|
|
PermissionAttachment attachment = player.addAttachment(Plex.get());
|
|
|
|
plexPlayer.getPermissions().forEach(permission -> attachment.setPermission(permission.getPermission(), permission.isAllowed()));
|
|
|
|
plexPlayer.setPermissionAttachment(attachment);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void addPermission(PlexPlayer player, Permission permission)
|
|
|
|
{
|
|
|
|
Plex.get().getSqlPermissions().addPermission(addToArrayList(player.getPermissions(), permission));
|
|
|
|
Player p = Bukkit.getPlayer(player.getUuid());
|
|
|
|
if (p == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
player.getPermissionAttachment().setPermission(permission.getPermission(), permission.isAllowed());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void addPermission(PlexPlayer player, String permission)
|
|
|
|
{
|
|
|
|
addPermission(player, new Permission(player.getUuid(), permission));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void removePermission(PlexPlayer player, String permission)
|
|
|
|
{
|
|
|
|
Plex.get().getSqlPermissions().removePermission(player.getUuid(), permission);
|
|
|
|
player.getPermissions().removeIf(permission1 -> permission1.getPermission().equalsIgnoreCase(permission));
|
|
|
|
Player p = Bukkit.getPlayer(player.getUuid());
|
|
|
|
if (p == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
player.getPermissionAttachment().unsetPermission(permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updatePermission(PlexPlayer player, String permission, boolean newValue)
|
|
|
|
{
|
2022-04-13 02:08:12 +00:00
|
|
|
player.getPermissions().stream().filter(permission1 -> permission.equalsIgnoreCase(permission)).findFirst().ifPresent(permission1 ->
|
|
|
|
{
|
2022-04-11 17:56:26 +00:00
|
|
|
Plex.get().getSqlPermissions().updatePermission(permission1, newValue);
|
|
|
|
});
|
|
|
|
player.getPermissions().removeIf(permission1 -> permission1.getPermission().equalsIgnoreCase(permission));
|
|
|
|
Player p = Bukkit.getPlayer(player.getUuid());
|
|
|
|
if (p == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
player.getPermissionAttachment().unsetPermission(permission);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-04-07 23:23:38 +00:00
|
|
|
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();
|
2022-04-07 16:55:12 +00:00
|
|
|
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-04-07 23:23:38 +00:00
|
|
|
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).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);
|
|
|
|
}
|
|
|
|
|
2020-11-06 03:50:16 +00:00
|
|
|
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()
|
|
|
|
{
|
2022-04-07 07:37:31 +00:00
|
|
|
if (Plex.get().getSqlConnection().getDataSource() != null)
|
2020-10-27 18:14:34 +00:00
|
|
|
{
|
2022-04-07 07:37:31 +00:00
|
|
|
try (Connection con = Plex.get().getSqlConnection().getCon())
|
2020-10-27 18:14:34 +00:00
|
|
|
{
|
2022-04-07 07:37:31 +00:00
|
|
|
if (Plex.get().getStorageType() == StorageType.MARIADB)
|
|
|
|
{
|
|
|
|
PlexLog.log("Successfully enabled MySQL!");
|
2022-04-07 23:23:38 +00:00
|
|
|
}
|
|
|
|
else if (Plex.get().getStorageType() == StorageType.SQLITE)
|
2022-04-07 07:37:31 +00:00
|
|
|
{
|
|
|
|
PlexLog.log("Successfully enabled SQLite!");
|
|
|
|
}
|
2022-04-07 23:23:38 +00:00
|
|
|
}
|
|
|
|
catch (SQLException e)
|
2020-10-27 18:14:34 +00:00
|
|
|
{
|
2022-04-07 07:37:31 +00:00
|
|
|
if (Plex.get().getMongoConnection().getDatastore() != null)
|
|
|
|
{
|
|
|
|
PlexLog.log("Successfully enabled MongoDB!");
|
|
|
|
}
|
2020-10-27 18:14:34 +00:00
|
|
|
}
|
2022-04-07 23:23:38 +00:00
|
|
|
}
|
|
|
|
else
|
2022-04-04 08:36:50 +00:00
|
|
|
{
|
|
|
|
if (Plex.get().getMongoConnection().getDatastore() != null)
|
2020-10-28 03:49:56 +00:00
|
|
|
{
|
2022-04-04 08:36:50 +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-27 18:14:34 +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)
|
|
|
|
{
|
|
|
|
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-11-06 03:50:16 +00:00
|
|
|
public static String colorize(final String string)
|
|
|
|
{
|
|
|
|
return ChatColor.translateAlternateColorCodes('&', string);
|
|
|
|
}
|
|
|
|
|
2022-04-10 03:24:44 +00:00
|
|
|
private static final MiniMessage safeMessage = MiniMessage.builder().tags(TagResolver.builder().resolvers(
|
|
|
|
StandardTags.color(),
|
|
|
|
StandardTags.decorations(),
|
|
|
|
StandardTags.gradient(),
|
|
|
|
StandardTags.rainbow(),
|
|
|
|
StandardTags.reset()
|
|
|
|
).build()).build();
|
2022-04-10 01:53:27 +00:00
|
|
|
|
2022-04-10 22:27:25 +00:00
|
|
|
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");
|
|
|
|
}
|
2022-04-10 05:10:35 +00:00
|
|
|
LocalDateTime date = LocalDateTime.now();
|
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
|
|
|
{
|
2022-04-10 17:44:36 +00:00
|
|
|
Component component = MiniMessage.miniMessage().deserialize(input); // removes existing tags
|
2022-04-10 05:10:35 +00:00
|
|
|
return MiniMessage.miniMessage().deserialize("<rainbow>" + PlainTextComponentSerializer.plainText().serialize(component));
|
|
|
|
}
|
|
|
|
return MiniMessage.miniMessage().deserialize(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-04-10 05:10:35 +00:00
|
|
|
return MiniMessage.miniMessage().deserialize(messageString(entry, objects));
|
2022-02-25 07:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String messageString(String entry, Object... objects)
|
|
|
|
{
|
2022-02-25 09:54:11 +00:00
|
|
|
String f = plugin.messages.getString(entry);
|
2022-02-25 07:09:55 +00:00
|
|
|
if (f == null)
|
|
|
|
{
|
|
|
|
throw new NullPointerException();
|
|
|
|
}
|
2022-04-04 04:07:48 +00:00
|
|
|
for (int i = 0; i < objects.length; i++)
|
|
|
|
{
|
2022-04-07 23:23:38 +00:00
|
|
|
f = f.replace("{" + i + "}", String.valueOf(objects[i]));
|
2022-02-25 07:09:55 +00:00
|
|
|
}
|
|
|
|
return f;
|
2020-11-02 00:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-26 06:24:11 +00:00
|
|
|
private static long a(String parse)
|
|
|
|
{
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
2022-04-19 20:16:07 +00:00
|
|
|
timeUnits.forEach(obj ->
|
2022-02-26 06:24:11 +00:00
|
|
|
{
|
|
|
|
if (parse.endsWith(obj))
|
|
|
|
{
|
|
|
|
sb.append(parse.split(obj)[0]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return Long.parseLong(sb.toString());
|
|
|
|
}
|
|
|
|
|
2022-04-19 20:16:07 +00:00
|
|
|
public static int parseInteger(String s) throws NumberFormatException
|
2022-02-26 06:24:11 +00:00
|
|
|
{
|
2022-04-19 20:16:07 +00:00
|
|
|
if (!NumberUtils.isNumber(s))
|
2022-02-26 06:24:11 +00:00
|
|
|
{
|
2022-04-19 20:16:07 +00:00
|
|
|
throw new NumberFormatException(messageString("unableToParseNumber", s));
|
|
|
|
}
|
|
|
|
return Integer.parseInt(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static LocalDateTime createDate(String arg)
|
|
|
|
{
|
|
|
|
LocalDateTime time = LocalDateTime.now();
|
|
|
|
for (String unit : PlexUtils.timeUnits)
|
|
|
|
{
|
|
|
|
if (arg.endsWith(unit))
|
2022-02-26 06:24:11 +00:00
|
|
|
{
|
2022-04-19 20:16:07 +00:00
|
|
|
int duration = parseInteger(arg.replace(unit, ""));
|
|
|
|
switch (unit)
|
2022-02-26 06:24:11 +00:00
|
|
|
{
|
2022-04-19 20:16:07 +00:00
|
|
|
case "y" -> time = time.plusYears(duration);
|
|
|
|
case "mo" -> time = time.plusMonths(duration);
|
|
|
|
case "w" -> time = time.plusWeeks(duration);
|
|
|
|
case "d" -> time = time.plusDays(duration);
|
|
|
|
case "h" -> time = time.plusHours(duration);
|
|
|
|
case "m" -> time = time.plusMinutes(duration);
|
|
|
|
case "s" -> time = time.plusSeconds(duration);
|
2022-02-26 06:24:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-19 20:16:07 +00:00
|
|
|
return time;
|
2022-02-26 06:24:11 +00:00
|
|
|
}
|
|
|
|
|
2022-04-10 23:30:21 +00:00
|
|
|
public static String useTimezone(LocalDateTime date)
|
|
|
|
{
|
|
|
|
// Use UTC if the timezone is null or not set correctly
|
|
|
|
if (TIMEZONE == null || !TIMEZONES.contains(TIMEZONE))
|
|
|
|
{
|
|
|
|
TIMEZONE = "Etc/UTC";
|
|
|
|
}
|
|
|
|
return DATE_FORMAT.withZone(ZoneId.of(TIMEZONE)).format(date);
|
|
|
|
}
|
|
|
|
|
2020-11-02 00:06:08 +00:00
|
|
|
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;
|
2022-04-07 23:23:38 +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;
|
2022-04-07 23:23:38 +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
|
|
|
|
2022-03-21 01:05:50 +00:00
|
|
|
public static <T> void commitGlobalGameRules(World world)
|
|
|
|
{
|
|
|
|
for (String s : Plex.get().config.getStringList("global_gamerules"))
|
|
|
|
{
|
|
|
|
readGameRules(world, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T> void commitSpecificGameRules(World world)
|
2022-02-25 09:54:11 +00:00
|
|
|
{
|
|
|
|
for (String s : Plex.get().config.getStringList("worlds." + world.getName().toLowerCase(Locale.ROOT) + ".gameRules"))
|
|
|
|
{
|
2022-03-21 01:05:50 +00:00
|
|
|
readGameRules(world, s);
|
2022-02-25 09:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 01:05:50 +00:00
|
|
|
private static <T> void readGameRules(World world, String s)
|
2022-02-25 09:54:11 +00:00
|
|
|
{
|
2022-03-21 01:05:50 +00:00
|
|
|
String gameRule = s.split(";")[0];
|
2022-04-07 23:23:38 +00:00
|
|
|
T value = (T)s.split(";")[1];
|
|
|
|
GameRule<T> rule = (GameRule<T>)GameRule.getByName(gameRule);
|
2022-03-21 01:05:50 +00:00
|
|
|
if (rule != null && check(value).getClass().equals(rule.getType()))
|
|
|
|
{
|
|
|
|
world.setGameRule(rule, value);
|
|
|
|
PlexLog.debug("Setting game rule " + gameRule + " for world " + world.getName() + " with value " + value);
|
2022-04-07 23:23:38 +00:00
|
|
|
}
|
|
|
|
else
|
2022-03-21 01:05:50 +00:00
|
|
|
{
|
|
|
|
PlexLog.error(String.format("Failed to set game rule %s for world %s with value %s!", gameRule, world.getName().toLowerCase(Locale.ROOT), value));
|
|
|
|
}
|
|
|
|
}
|
2022-02-25 09:54:11 +00:00
|
|
|
|
2022-03-21 01:05:50 +00:00
|
|
|
public static <T> Object check(T value)
|
|
|
|
{
|
2022-02-25 09:54:11 +00:00
|
|
|
if (value.toString().equalsIgnoreCase("true") || value.toString().equalsIgnoreCase("false"))
|
|
|
|
{
|
|
|
|
return Boolean.parseBoolean(value.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NumberUtils.isNumber(value.toString()))
|
|
|
|
{
|
|
|
|
return Integer.parseInt(value.toString());
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
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-04-10 22:27:25 +00:00
|
|
|
Bukkit.broadcast(MiniMessage.miniMessage().deserialize(s));
|
2022-01-27 09:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-04-13 02:08:12 +00:00
|
|
|
public static void broadcastToAdmins(Component component)
|
|
|
|
{
|
|
|
|
Bukkit.getOnlinePlayers().stream().filter(pl -> PlayerCache.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-02-25 03:56:28 +00:00
|
|
|
public static Object simpleGET(String url)
|
2020-11-05 13:40:48 +00:00
|
|
|
{
|
2022-02-25 03:56:28 +00:00
|
|
|
try
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2022-02-25 03:56:28 +00:00
|
|
|
URL u = new URL(url);
|
2022-04-07 23:23:38 +00:00
|
|
|
HttpURLConnection connection = (HttpURLConnection)u.openConnection();
|
2022-02-25 03:56:28 +00:00
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
String line;
|
|
|
|
StringBuilder content = new StringBuilder();
|
|
|
|
while ((line = in.readLine()) != null)
|
|
|
|
{
|
|
|
|
content.append(line);
|
|
|
|
}
|
|
|
|
in.close();
|
|
|
|
connection.disconnect();
|
|
|
|
return new JSONParser().parse(content.toString());
|
2022-04-07 23:23:38 +00:00
|
|
|
}
|
|
|
|
catch (IOException | ParseException ex)
|
2022-02-25 03:56:28 +00:00
|
|
|
{
|
|
|
|
return null;
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2020-11-05 13:40:48 +00:00
|
|
|
}
|
2020-11-05 21:17:14 +00:00
|
|
|
|
|
|
|
public static UUID getFromName(String name)
|
|
|
|
{
|
|
|
|
JSONObject profile;
|
2022-04-07 23:23:38 +00:00
|
|
|
profile = (JSONObject)simpleGET("https://api.ashcon.app/mojang/v2/user/" + name);
|
2022-02-25 03:56:28 +00:00
|
|
|
if (profile == null)
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2022-02-25 03:56:28 +00:00
|
|
|
PlexLog.error("Profile from Ashcon API returned null!");
|
2020-11-05 21:17:14 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-04-07 23:23: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
|
|
|
|
2022-03-19 03:33:23 +00:00
|
|
|
@SuppressWarnings("UnstableApiUsage")
|
|
|
|
public static Set<Class<?>> getClassesFrom(String packageName)
|
|
|
|
{
|
|
|
|
Set<Class<?>> classes = new HashSet<>();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ClassPath path = ClassPath.from(Plex.class.getClassLoader());
|
|
|
|
ImmutableSet<ClassPath.ClassInfo> infoSet = path.getTopLevelClasses(packageName);
|
|
|
|
infoSet.forEach(info ->
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Class<?> clazz = Class.forName(info.getName());
|
|
|
|
classes.add(clazz);
|
2022-04-07 23:23:38 +00:00
|
|
|
}
|
|
|
|
catch (ClassNotFoundException ex)
|
2022-03-19 03:33:23 +00:00
|
|
|
{
|
|
|
|
PlexLog.error("Unable to find class " + info.getName() + " in " + packageName);
|
|
|
|
}
|
|
|
|
});
|
2022-04-07 23:23:38 +00:00
|
|
|
}
|
|
|
|
catch (IOException ex)
|
2022-03-19 03:33:23 +00:00
|
|
|
{
|
|
|
|
PlexLog.error("Something went wrong while fetching classes from " + packageName);
|
|
|
|
throw new RuntimeException(ex);
|
|
|
|
}
|
|
|
|
return Collections.unmodifiableSet(classes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static <T> Set<Class<? extends T>> getClassesBySubType(String packageName, Class<T> subType)
|
|
|
|
{
|
|
|
|
Set<Class<?>> loadedClasses = getClassesFrom(packageName);
|
|
|
|
Set<Class<? extends T>> classes = new HashSet<>();
|
|
|
|
loadedClasses.forEach(clazz ->
|
|
|
|
{
|
|
|
|
if (clazz.getSuperclass() == subType || Arrays.asList(clazz.getInterfaces()).contains(subType))
|
|
|
|
{
|
2022-04-07 23:23:38 +00:00
|
|
|
classes.add((Class<? extends T>)clazz);
|
2022-03-19 03:33:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return Collections.unmodifiableSet(classes);
|
|
|
|
}
|
|
|
|
|
2022-04-08 03:47:32 +00:00
|
|
|
public static boolean randomBoolean()
|
|
|
|
{
|
|
|
|
return ThreadLocalRandom.current().nextBoolean();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|