mirror of
https://github.com/Shiewk/SModeration.git
synced 2026-04-29 06:34:17 +02:00
Move SModeration for Paper to root project
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package de.shiewk.smoderation.paper.util;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
/**
|
||||
* Utility class for byte-based saving of integers, longs and UUIDs
|
||||
*/
|
||||
public abstract class ByteUtil {
|
||||
private ByteUtil(){}
|
||||
|
||||
public static byte[] longToBytes(long v){
|
||||
ByteBuffer buffer = ByteBuffer.allocate(8);
|
||||
buffer.putLong(v);
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
public static long bytesToLong(byte[] i){
|
||||
if (i.length != 8){
|
||||
throw new IllegalArgumentException("length must be 8");
|
||||
}
|
||||
ByteBuffer buffer = ByteBuffer.allocate(8);
|
||||
buffer.put(0, i);
|
||||
return buffer.getLong(0);
|
||||
}
|
||||
|
||||
public static byte[] uuidToBytes(UUID uuid){
|
||||
byte[] l = longToBytes(uuid.getLeastSignificantBits());
|
||||
byte[] m = longToBytes(uuid.getMostSignificantBits());
|
||||
return new byte[]{
|
||||
m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7],
|
||||
l[0], l[1], l[2], l[3], l[4], l[5], l[6], l[7]
|
||||
};
|
||||
}
|
||||
|
||||
public static UUID bytesToUuid(byte[] i){
|
||||
if (i.length != 16){
|
||||
throw new IllegalArgumentException("length must be 16, was " + i.length);
|
||||
}
|
||||
long l = bytesToLong(new byte[]{ i[8], i[9], i[10], i[11], i[12], i[13], i[14], i[15] });
|
||||
long m = bytesToLong(new byte[]{ i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7] });
|
||||
return new UUID(m, l);
|
||||
}
|
||||
|
||||
public static int bytesToInt(byte[] bytes) {
|
||||
if (bytes.length != 4){
|
||||
throw new IllegalArgumentException("length must be 4");
|
||||
}
|
||||
ByteBuffer buffer = ByteBuffer.allocate(4);
|
||||
buffer.put(0, bytes);
|
||||
return buffer.getInt(0);
|
||||
}
|
||||
|
||||
public static byte[] intToBytes(int value) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(4);
|
||||
buffer.putInt(value);
|
||||
return buffer.array();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package de.shiewk.smoderation.paper.util;
|
||||
|
||||
import de.shiewk.smoderation.paper.SModerationPaper;
|
||||
import de.shiewk.smoderation.paper.punishments.Punishment;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public abstract class PlayerUtil {
|
||||
private PlayerUtil(){}
|
||||
|
||||
public static final UUID UUID_CONSOLE = new UUID(0, 0);
|
||||
|
||||
public static @NotNull String offlinePlayerName(UUID uuid){
|
||||
if (uuid.equals(UUID_CONSOLE)){
|
||||
return "CONSOLE";
|
||||
}
|
||||
OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);
|
||||
return player.getName() == null ? "Unknown Player " + uuid : player.getName();
|
||||
}
|
||||
|
||||
public static @Nullable UUID offlinePlayerUUIDByName(String name){
|
||||
final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayerIfCached(name); // getOfflinePlayerIfCached(String) is safer (I have experience with getOfflinePlayer(String) returning wrong UUIDs)
|
||||
if (offlinePlayer != null) {
|
||||
return offlinePlayer.getUniqueId();
|
||||
} else {
|
||||
// try to find uuid by searching through punishments
|
||||
final Punishment punishment = SModerationPaper.container.find(p -> offlinePlayerName(p.to).equalsIgnoreCase(name));
|
||||
if (punishment != null) {
|
||||
return punishment.to;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static @Nullable CommandSender senderByUUID(@NotNull UUID uid){
|
||||
if (uid.equals(UUID_CONSOLE)){
|
||||
return Bukkit.getConsoleSender();
|
||||
} else {
|
||||
return Bukkit.getPlayer(uid);
|
||||
}
|
||||
}
|
||||
|
||||
public static @Nullable Player findOnlinePlayer(String name){
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
if (onlinePlayer.getName().equalsIgnoreCase(name)){
|
||||
return onlinePlayer;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<String> listPlayerNames(){
|
||||
return listPlayerNames(pl -> true);
|
||||
}
|
||||
|
||||
public static List<String> listPlayerNames(final Predicate<Player> predicate) {
|
||||
final ArrayList<String> names = new ArrayList<>();
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
if (predicate.test(onlinePlayer)){
|
||||
names.add(onlinePlayer.getName());
|
||||
}
|
||||
}
|
||||
return List.copyOf(names);
|
||||
}
|
||||
|
||||
public static List<String> listPlayerNames(String search) {
|
||||
return StringUtil.copyPartialMatches(search, listPlayerNames(), new ArrayList<>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
package de.shiewk.smoderation.paper.util;
|
||||
|
||||
import org.jetbrains.annotations.Range;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public abstract class TimeUtil {
|
||||
private TimeUtil(){}
|
||||
|
||||
public static String formatTimeLong(long millis){
|
||||
long seconds = millis / 1000;
|
||||
millis -= seconds * 1000;
|
||||
|
||||
long minutes = seconds / 60;
|
||||
seconds -= minutes * 60;
|
||||
|
||||
long hours = minutes / 60;
|
||||
minutes -= hours * 60;
|
||||
|
||||
long days = hours / 24;
|
||||
hours -= days * 24;
|
||||
|
||||
long years = days / 365;
|
||||
days -= years * 365;
|
||||
|
||||
long months = days / 30;
|
||||
days -= months * 30;
|
||||
|
||||
long weeks = days / 7;
|
||||
days -= weeks * 7;
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
if (years > 0){
|
||||
if (!builder.isEmpty()){
|
||||
builder.append(" ");
|
||||
}
|
||||
builder.append("%s years".formatted(years));
|
||||
}
|
||||
|
||||
if (months > 0){
|
||||
if (!builder.isEmpty()){
|
||||
builder.append(" ");
|
||||
}
|
||||
builder.append("%s months".formatted(months));
|
||||
}
|
||||
|
||||
if (weeks > 0){
|
||||
if (!builder.isEmpty()){
|
||||
builder.append(" ");
|
||||
}
|
||||
builder.append("%s weeks".formatted(weeks));
|
||||
}
|
||||
|
||||
if (days > 0){
|
||||
if (!builder.isEmpty()){
|
||||
builder.append(" ");
|
||||
}
|
||||
builder.append("%s days".formatted(days));
|
||||
}
|
||||
|
||||
if (hours > 0){
|
||||
if (!builder.isEmpty()){
|
||||
builder.append(" ");
|
||||
}
|
||||
builder.append("%s hours".formatted(hours));
|
||||
}
|
||||
|
||||
if (minutes > 0){
|
||||
if (!builder.isEmpty()){
|
||||
builder.append(" ");
|
||||
}
|
||||
builder.append("%s minutes".formatted(minutes));
|
||||
}
|
||||
|
||||
if (seconds > 0){
|
||||
if (!builder.isEmpty()){
|
||||
builder.append(" ");
|
||||
}
|
||||
builder.append("%s seconds".formatted(seconds));
|
||||
}
|
||||
|
||||
if (builder.isEmpty()){
|
||||
builder.append("%s ms".formatted(millis));
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static long parseDurationMillisSafely(String in){
|
||||
try {
|
||||
return parseDurationMillis(in);
|
||||
} catch (Throwable e){
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static long parseDurationMillis(String in){
|
||||
if (in.endsWith("ms")){
|
||||
return Long.parseLong(in.substring(0, in.length()-2));
|
||||
} else if (in.endsWith("s")){
|
||||
return Long.parseLong(in.substring(0, in.length()-1)) * 1000L;
|
||||
} else if (in.endsWith("min")){
|
||||
return Long.parseLong(in.substring(0, in.length()-3)) * 60000L;
|
||||
} else if (in.endsWith("h")){
|
||||
return Long.parseLong(in.substring(0, in.length()-1)) * 3600000L;
|
||||
} else if (in.endsWith("d")){
|
||||
return Long.parseLong(in.substring(0, in.length()-1)) * 86400000L;
|
||||
} else if (in.endsWith("w")){
|
||||
return Long.parseLong(in.substring(0, in.length()-1)) * 604800000L;
|
||||
} else if (in.endsWith("mo")){
|
||||
return Long.parseLong(in.substring(0, in.length()-2)) * 2592000000L;
|
||||
} else if (in.endsWith("y")){
|
||||
return Long.parseLong(in.substring(0, in.length()-1)) * 31536000000L;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static String calendarTimestamp(long time){
|
||||
Date date = new Date(time);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
TimeZone zone = calendar.getTimeZone();
|
||||
int second = calendar.get(Calendar.SECOND);
|
||||
int minute = calendar.get(Calendar.MINUTE);
|
||||
int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
||||
int year = calendar.get(Calendar.YEAR);
|
||||
|
||||
String day = numberWithSuffix(calendar.get(Calendar.DAY_OF_MONTH));
|
||||
String month = monthName(calendar.get(Calendar.MONTH));
|
||||
return "%s %s %s, %s:%s:%s %s".formatted(
|
||||
month,
|
||||
day,
|
||||
year,
|
||||
hour < 10 ? "0" + hour : hour,
|
||||
minute < 10 ? "0" + minute : minute,
|
||||
second < 10 ? "0" + second : second,
|
||||
zone.getDisplayName(false, TimeZone.SHORT)
|
||||
);
|
||||
}
|
||||
|
||||
private static String numberWithSuffix(int i){
|
||||
return i + numberSuffix(i);
|
||||
}
|
||||
|
||||
private static String numberSuffix(int i){
|
||||
if ((i % 10) == 1 && i != 11){
|
||||
return "st";
|
||||
} else if ((i % 10) == 2 && i != 12){
|
||||
return "nd";
|
||||
} else if ((i % 10) == 3 && i != 13){
|
||||
return "rd";
|
||||
}
|
||||
return "th";
|
||||
}
|
||||
|
||||
public static String monthName(@Range(from = 0, to = 11) int m){
|
||||
switch (m){
|
||||
case 0 -> {
|
||||
return "January";
|
||||
}
|
||||
case 1 -> {
|
||||
return "February";
|
||||
}
|
||||
case 2 -> {
|
||||
return "March";
|
||||
}
|
||||
case 3 -> {
|
||||
return "April";
|
||||
}
|
||||
case 4 -> {
|
||||
return "May";
|
||||
}
|
||||
case 5 -> {
|
||||
return "June";
|
||||
}
|
||||
case 6 -> {
|
||||
return "July";
|
||||
}
|
||||
case 7 -> {
|
||||
return "August";
|
||||
}
|
||||
case 8 -> {
|
||||
return "September";
|
||||
}
|
||||
case 9 -> {
|
||||
return "October";
|
||||
}
|
||||
case 10 -> {
|
||||
return "November";
|
||||
}
|
||||
case 11 -> {
|
||||
return "December";
|
||||
}
|
||||
}
|
||||
return "Unknown Month";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user