mirror of
https://github.com/plexusorg/Plex.git
synced 2024-12-22 17:17:37 +00:00
Organize the util classes
This commit is contained in:
parent
98c487c639
commit
7d2a25668a
@ -14,6 +14,7 @@ import dev.plex.punishment.PunishmentType;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.PlexLog;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.WebUtils;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@ -38,7 +39,7 @@ public class BanCMD extends PlexCommand
|
||||
return usage();
|
||||
}
|
||||
|
||||
UUID targetUUID = PlexUtils.getFromName(args[0]);
|
||||
UUID targetUUID = WebUtils.getFromName(args[0]);
|
||||
|
||||
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
|
||||
{
|
||||
|
@ -6,6 +6,7 @@ import dev.plex.command.annotation.CommandParameters;
|
||||
import dev.plex.command.annotation.CommandPermissions;
|
||||
import dev.plex.command.annotation.System;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.GameRuleUtil;
|
||||
import dev.plex.util.PlexLog;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import java.util.Arrays;
|
||||
@ -47,7 +48,7 @@ public class DebugCMD extends PlexCommand
|
||||
{
|
||||
for (World world : Bukkit.getWorlds())
|
||||
{
|
||||
PlexUtils.commitGlobalGameRules(world);
|
||||
GameRuleUtil.commitGlobalGameRules(world);
|
||||
PlexLog.log("Set global gamerules for world: " + world.getName());
|
||||
}
|
||||
for (String world : plugin.config.getConfigurationSection("worlds").getKeys(false))
|
||||
@ -55,7 +56,7 @@ public class DebugCMD extends PlexCommand
|
||||
World bukkitWorld = Bukkit.getWorld(world);
|
||||
if (bukkitWorld != null)
|
||||
{
|
||||
PlexUtils.commitSpecificGameRules(bukkitWorld);
|
||||
GameRuleUtil.commitSpecificGameRules(bukkitWorld);
|
||||
PlexLog.log("Set specific gamerules for world: " + world.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import dev.plex.punishment.Punishment;
|
||||
import dev.plex.punishment.PunishmentType;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.WebUtils;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@ -33,7 +34,7 @@ public class KickCMD extends PlexCommand
|
||||
return usage();
|
||||
}
|
||||
|
||||
UUID targetUUID = PlexUtils.getFromName(args[0]);
|
||||
UUID targetUUID = WebUtils.getFromName(args[0]);
|
||||
String reason = "No reason provided";
|
||||
|
||||
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
|
||||
|
@ -12,19 +12,18 @@ import dev.plex.punishment.Punishment;
|
||||
import dev.plex.punishment.PunishmentType;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.TimeUtils;
|
||||
import dev.plex.util.WebUtils;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@CommandParameters(name = "tempban", usage = "/<command> <player> <time> [reason]", description = "Temporarily ban a player")
|
||||
@CommandPermissions(level = Rank.ADMIN, permission = "plex.tempban", source = RequiredCommandSource.ANY)
|
||||
|
||||
@ -38,7 +37,7 @@ public class TempbanCMD extends PlexCommand
|
||||
return usage();
|
||||
}
|
||||
|
||||
UUID targetUUID = PlexUtils.getFromName(args[0]);
|
||||
UUID targetUUID = WebUtils.getFromName(args[0]);
|
||||
String reason;
|
||||
|
||||
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
|
||||
@ -67,17 +66,18 @@ public class TempbanCMD extends PlexCommand
|
||||
return messageComponent("playerBanned");
|
||||
}
|
||||
Punishment punishment = new Punishment(targetUUID, getUUID(sender));
|
||||
punishment.setType(PunishmentType.TEMPBAN);
|
||||
punishment.setType(PunishmentType.BAN);
|
||||
if (args.length > 2)
|
||||
{
|
||||
reason = StringUtils.join(args, " ", 2, args.length);
|
||||
punishment.setReason(reason);
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
punishment.setReason("No reason provided.");
|
||||
}
|
||||
punishment.setPunishedUsername(plexPlayer.getName());
|
||||
punishment.setEndDate(PlexUtils.createDate(args[1]));
|
||||
punishment.setEndDate(TimeUtils.createDate(args[1]));
|
||||
punishment.setCustomTime(false);
|
||||
punishment.setActive(!isAdmin(plexPlayer));
|
||||
if (player != null)
|
||||
|
@ -11,6 +11,7 @@ import dev.plex.command.source.RequiredCommandSource;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.WebUtils;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@ -34,7 +35,7 @@ public class UnbanCMD extends PlexCommand
|
||||
|
||||
if (args.length == 1)
|
||||
{
|
||||
UUID targetUUID = PlexUtils.getFromName(args[0]);
|
||||
UUID targetUUID = WebUtils.getFromName(args[0]);
|
||||
|
||||
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
|
||||
{
|
||||
|
@ -5,7 +5,7 @@ import dev.plex.PlexBase;
|
||||
import dev.plex.command.PlexCommand;
|
||||
import dev.plex.command.annotation.System;
|
||||
import dev.plex.util.PlexLog;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.ReflectionsUtil;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@ -14,7 +14,7 @@ public class CommandHandler implements PlexBase
|
||||
{
|
||||
public CommandHandler()
|
||||
{
|
||||
Set<Class<? extends PlexCommand>> commandSet = PlexUtils.getClassesBySubType("dev.plex.command.impl", PlexCommand.class);
|
||||
Set<Class<? extends PlexCommand>> commandSet = ReflectionsUtil.getClassesBySubType("dev.plex.command.impl", PlexCommand.class);
|
||||
List<PlexCommand> commands = Lists.newArrayList();
|
||||
|
||||
commandSet.forEach(clazz ->
|
||||
|
@ -6,6 +6,7 @@ import dev.plex.listener.PlexListener;
|
||||
import dev.plex.listener.annotation.Toggleable;
|
||||
import dev.plex.util.PlexLog;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.ReflectionsUtil;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@ -14,7 +15,7 @@ public class ListenerHandler implements PlexBase
|
||||
{
|
||||
public ListenerHandler()
|
||||
{
|
||||
Set<Class<? extends PlexListener>> listenerSet = PlexUtils.getClassesBySubType("dev.plex.listener.impl", PlexListener.class);
|
||||
Set<Class<? extends PlexListener>> listenerSet = ReflectionsUtil.getClassesBySubType("dev.plex.listener.impl", PlexListener.class);
|
||||
List<PlexListener> listeners = Lists.newArrayList();
|
||||
|
||||
listenerSet.forEach(clazz ->
|
||||
|
@ -5,6 +5,7 @@ import dev.plex.cache.PlayerCache;
|
||||
import dev.plex.listener.PlexListener;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.storage.StorageType;
|
||||
import dev.plex.util.PermissionsUtil;
|
||||
import dev.plex.util.PlexLog;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import java.util.Arrays;
|
||||
@ -80,7 +81,7 @@ public class PlayerListener<T> extends PlexListener
|
||||
PlexUtils.broadcast(PlexUtils.mmDeserialize("<aqua>" + player.getName() + " is " + loginMessage));
|
||||
}
|
||||
|
||||
PlexUtils.setupPermissions(player);
|
||||
PermissionsUtil.setupPermissions(player);
|
||||
|
||||
if (plugin.getStorageType() != StorageType.MONGODB)
|
||||
{
|
||||
|
@ -47,7 +47,6 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class LibraryLoader
|
||||
{
|
||||
|
||||
private final Logger logger;
|
||||
private final RepositorySystem repository;
|
||||
private final DefaultRepositorySystemSession session;
|
||||
|
@ -1,6 +1,7 @@
|
||||
package dev.plex.services.impl;
|
||||
|
||||
import dev.plex.services.AbstractService;
|
||||
import dev.plex.util.GameRuleUtil;
|
||||
import dev.plex.util.PlexLog;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import java.util.Locale;
|
||||
@ -19,7 +20,7 @@ public class GameRuleService extends AbstractService
|
||||
{
|
||||
for (World world : Bukkit.getWorlds())
|
||||
{
|
||||
PlexUtils.commitGlobalGameRules(world);
|
||||
GameRuleUtil.commitGlobalGameRules(world);
|
||||
PlexLog.log("Set global gamerules for world: " + world.getName());
|
||||
}
|
||||
for (String world : plugin.config.getConfigurationSection("worlds").getKeys(false))
|
||||
@ -27,7 +28,7 @@ public class GameRuleService extends AbstractService
|
||||
World bukkitWorld = Bukkit.getWorld(world);
|
||||
if (bukkitWorld != null)
|
||||
{
|
||||
PlexUtils.commitSpecificGameRules(bukkitWorld);
|
||||
GameRuleUtil.commitSpecificGameRules(bukkitWorld);
|
||||
PlexLog.log("Set specific gamerules for world: " + world.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
}
|
||||
|
56
src/main/java/dev/plex/util/GameRuleUtil.java
Normal file
56
src/main/java/dev/plex/util/GameRuleUtil.java
Normal file
@ -0,0 +1,56 @@
|
||||
package dev.plex.util;
|
||||
|
||||
import dev.plex.Plex;
|
||||
import java.util.Locale;
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
import org.bukkit.GameRule;
|
||||
import org.bukkit.World;
|
||||
|
||||
public class GameRuleUtil
|
||||
{
|
||||
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)
|
||||
{
|
||||
for (String s : Plex.get().config.getStringList("worlds." + world.getName().toLowerCase(Locale.ROOT) + ".gameRules"))
|
||||
{
|
||||
readGameRules(world, s);
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> void readGameRules(World world, String s)
|
||||
{
|
||||
String gameRule = s.split(";")[0];
|
||||
T value = (T)s.split(";")[1];
|
||||
GameRule<T> rule = (GameRule<T>)GameRule.getByName(gameRule);
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
PlexLog.error(String.format("Failed to set game rule %s for world %s with value %s!", gameRule, world.getName().toLowerCase(Locale.ROOT), value));
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> Object check(T value)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
65
src/main/java/dev/plex/util/PermissionsUtil.java
Normal file
65
src/main/java/dev/plex/util/PermissionsUtil.java
Normal file
@ -0,0 +1,65 @@
|
||||
package dev.plex.util;
|
||||
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.cache.DataUtils;
|
||||
import dev.plex.permission.Permission;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.PermissionAttachment;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PermissionsUtil
|
||||
{
|
||||
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(PlexUtils.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)
|
||||
{
|
||||
player.getPermissions().stream().filter(permission1 -> permission.equalsIgnoreCase(permission)).findFirst().ifPresent(permission1 ->
|
||||
{
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
@ -1,40 +1,25 @@
|
||||
package dev.plex.util;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.reflect.ClassPath;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.PlexBase;
|
||||
import dev.plex.cache.DataUtils;
|
||||
import dev.plex.cache.PlayerCache;
|
||||
import dev.plex.config.Config;
|
||||
import dev.plex.permission.Permission;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.storage.StorageType;
|
||||
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;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
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;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@ -42,37 +27,20 @@ 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.plain.PlainTextComponentSerializer;
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
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;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.PluginCommandYamlParser;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.PermissionAttachment;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
public class PlexUtils implements PlexBase
|
||||
{
|
||||
private static final Random RANDOM;
|
||||
public static final List<String> timeUnits = new ArrayList<>()
|
||||
{{
|
||||
add("y");
|
||||
add("mo");
|
||||
add("w");
|
||||
add("d");
|
||||
add("h");
|
||||
add("m");
|
||||
add("s");
|
||||
}};
|
||||
public static Map<String, ChatColor> CHAT_COLOR_NAMES;
|
||||
public static List<ChatColor> CHAT_COLOR_POOL;
|
||||
public static List<String> DEVELOPERS =
|
||||
@ -96,58 +64,6 @@ public class PlexUtils implements PlexBase
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
player.getPermissions().stream().filter(permission1 -> permission.equalsIgnoreCase(permission)).findFirst().ifPresent(permission1 ->
|
||||
{
|
||||
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);
|
||||
@ -286,53 +202,6 @@ public class PlexUtils implements PlexBase
|
||||
return f;
|
||||
}
|
||||
|
||||
private static long a(String parse)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
timeUnits.forEach(obj ->
|
||||
{
|
||||
if (parse.endsWith(obj))
|
||||
{
|
||||
sb.append(parse.split(obj)[0]);
|
||||
}
|
||||
});
|
||||
|
||||
return Long.parseLong(sb.toString());
|
||||
}
|
||||
|
||||
public static int parseInteger(String s) throws NumberFormatException
|
||||
{
|
||||
if (!NumberUtils.isNumber(s))
|
||||
{
|
||||
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))
|
||||
{
|
||||
int duration = parseInteger(arg.replace(unit, ""));
|
||||
switch (unit)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
return time;
|
||||
}
|
||||
|
||||
public static String useTimezone(LocalDateTime date)
|
||||
{
|
||||
// Use UTC if the timezone is null or not set correctly
|
||||
@ -343,89 +212,6 @@ public class PlexUtils implements PlexBase
|
||||
return DATE_FORMAT.withZone(ZoneId.of(TIMEZONE)).format(date);
|
||||
}
|
||||
|
||||
public static ChatColor getChatColorFromConfig(Config config, ChatColor def, String path)
|
||||
{
|
||||
ChatColor color;
|
||||
if (config.getString(path) == null)
|
||||
{
|
||||
color = def;
|
||||
}
|
||||
else if (ChatColor.getByChar(config.getString(path)) == null)
|
||||
{
|
||||
color = def;
|
||||
}
|
||||
else
|
||||
{
|
||||
color = ChatColor.getByChar(config.getString(path));
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
public static void setBlocks(Location c1, Location c2, Material material)
|
||||
{
|
||||
if (!c1.getWorld().getName().equals(c1.getWorld().getName()))
|
||||
{
|
||||
return;
|
||||
}
|
||||
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());
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
for (String s : Plex.get().config.getStringList("worlds." + world.getName().toLowerCase(Locale.ROOT) + ".gameRules"))
|
||||
{
|
||||
readGameRules(world, s);
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> void readGameRules(World world, String s)
|
||||
{
|
||||
String gameRule = s.split(";")[0];
|
||||
T value = (T)s.split(";")[1];
|
||||
GameRule<T> rule = (GameRule<T>)GameRule.getByName(gameRule);
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
PlexLog.error(String.format("Failed to set game rule %s for world %s with value %s!", gameRule, world.getName().toLowerCase(Locale.ROOT), value));
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> Object check(T value)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public static List<String> getPlayerNameList()
|
||||
{
|
||||
@ -450,87 +236,6 @@ public class PlexUtils implements PlexBase
|
||||
});
|
||||
}
|
||||
|
||||
public static Object simpleGET(String url)
|
||||
{
|
||||
try
|
||||
{
|
||||
URL u = new URL(url);
|
||||
HttpURLConnection connection = (HttpURLConnection)u.openConnection();
|
||||
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());
|
||||
}
|
||||
catch (IOException | ParseException ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static UUID getFromName(String name)
|
||||
{
|
||||
JSONObject profile;
|
||||
profile = (JSONObject)simpleGET("https://api.ashcon.app/mojang/v2/user/" + name);
|
||||
if (profile == null)
|
||||
{
|
||||
PlexLog.error("Profile from Ashcon API returned null!");
|
||||
return null;
|
||||
}
|
||||
String uuidString = (String)profile.get("uuid");
|
||||
return UUID.fromString(uuidString);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
catch (ClassNotFoundException ex)
|
||||
{
|
||||
PlexLog.error("Unable to find class " + info.getName() + " in " + packageName);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
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))
|
||||
{
|
||||
classes.add((Class<? extends T>)clazz);
|
||||
}
|
||||
});
|
||||
return Collections.unmodifiableSet(classes);
|
||||
}
|
||||
|
||||
public static boolean randomBoolean()
|
||||
{
|
||||
return ThreadLocalRandom.current().nextBoolean();
|
||||
@ -550,24 +255,4 @@ public class PlexUtils implements PlexBase
|
||||
{
|
||||
return ThreadLocalRandom.current().nextInt(start, limit);
|
||||
}
|
||||
|
||||
public static long getDateNow()
|
||||
{
|
||||
return new Date().getTime();
|
||||
}
|
||||
|
||||
public static Date getDateFromLong(long epoch)
|
||||
{
|
||||
return new Date(epoch);
|
||||
}
|
||||
|
||||
public static long hoursToSeconds(long hours)
|
||||
{
|
||||
return hours * 3600;
|
||||
}
|
||||
|
||||
public static long minutesToSeconds(long minutes)
|
||||
{
|
||||
return minutes * 60;
|
||||
}
|
||||
}
|
||||
|
57
src/main/java/dev/plex/util/ReflectionsUtil.java
Normal file
57
src/main/java/dev/plex/util/ReflectionsUtil.java
Normal file
@ -0,0 +1,57 @@
|
||||
package dev.plex.util;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.reflect.ClassPath;
|
||||
import dev.plex.Plex;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class ReflectionsUtil
|
||||
{
|
||||
@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);
|
||||
}
|
||||
catch (ClassNotFoundException ex)
|
||||
{
|
||||
PlexLog.error("Unable to find class " + info.getName() + " in " + packageName);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
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))
|
||||
{
|
||||
classes.add((Class<? extends T>)clazz);
|
||||
}
|
||||
});
|
||||
return Collections.unmodifiableSet(classes);
|
||||
}
|
||||
}
|
73
src/main/java/dev/plex/util/TimeUtils.java
Normal file
73
src/main/java/dev/plex/util/TimeUtils.java
Normal file
@ -0,0 +1,73 @@
|
||||
package dev.plex.util;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
|
||||
public class TimeUtils
|
||||
{
|
||||
private static final List<String> timeUnits = new ArrayList<>()
|
||||
{{
|
||||
add("s");
|
||||
add("m");
|
||||
add("h");
|
||||
add("d");
|
||||
add("w");
|
||||
add("mo");
|
||||
add("y");
|
||||
}};
|
||||
|
||||
private static int parseInteger(String s) throws NumberFormatException
|
||||
{
|
||||
if (!NumberUtils.isNumber(s))
|
||||
{
|
||||
throw new NumberFormatException();
|
||||
}
|
||||
return Integer.parseInt(s);
|
||||
}
|
||||
|
||||
public static LocalDateTime createDate(String arg)
|
||||
{
|
||||
LocalDateTime time = LocalDateTime.now();
|
||||
for (String unit : timeUnits)
|
||||
{
|
||||
if (arg.endsWith(unit))
|
||||
{
|
||||
int duration = parseInteger(arg.replace(unit, ""));
|
||||
switch (unit)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
return time;
|
||||
}
|
||||
|
||||
public static long getDateNow()
|
||||
{
|
||||
return new Date().getTime();
|
||||
}
|
||||
|
||||
public static Date getDateFromLong(long epoch)
|
||||
{
|
||||
return new Date(epoch);
|
||||
}
|
||||
|
||||
public static long hoursToSeconds(long hours)
|
||||
{
|
||||
return hours * 3600;
|
||||
}
|
||||
|
||||
public static long minutesToSeconds(long minutes)
|
||||
{
|
||||
return minutes * 60;
|
||||
}
|
||||
}
|
51
src/main/java/dev/plex/util/WebUtils.java
Normal file
51
src/main/java/dev/plex/util/WebUtils.java
Normal file
@ -0,0 +1,51 @@
|
||||
package dev.plex.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.UUID;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
public class WebUtils
|
||||
{
|
||||
public static Object simpleGET(String url)
|
||||
{
|
||||
try
|
||||
{
|
||||
URL u = new URL(url);
|
||||
HttpURLConnection connection = (HttpURLConnection)u.openConnection();
|
||||
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());
|
||||
}
|
||||
catch (IOException | ParseException ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static UUID getFromName(String name)
|
||||
{
|
||||
JSONObject profile;
|
||||
profile = (JSONObject)simpleGET("https://api.ashcon.app/mojang/v2/user/" + name);
|
||||
if (profile == null)
|
||||
{
|
||||
PlexLog.error("Profile from Ashcon API returned null!");
|
||||
return null;
|
||||
}
|
||||
String uuidString = (String)profile.get("uuid");
|
||||
return UUID.fromString(uuidString);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user