Get rid of more ChatColor usage

This commit is contained in:
Telesphoreo 2022-04-19 15:31:34 -05:00
parent ee18ae324d
commit 81200f73d1
7 changed files with 57 additions and 91 deletions

View File

@ -10,6 +10,7 @@ import dev.plex.util.AshconInfo;
import dev.plex.util.MojangUtils;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import dev.plex.util.TimeUtils;
import java.util.Arrays;
import java.util.List;
import net.kyori.adventure.text.Component;
@ -47,7 +48,7 @@ public class NameHistoryCMD extends PlexCommand
historyList.add(
messageComponent("nameHistoryBody",
history.getUsername(),
PlexUtils.useTimezone(history.getLocalDateTime())));
TimeUtils.useTimezone(history.getLocalDateTime())));
}
else
{

View File

@ -9,6 +9,7 @@ import dev.plex.punishment.extra.Note;
import dev.plex.rank.enums.Rank;
import dev.plex.storage.StorageType;
import dev.plex.util.PlexUtils;
import dev.plex.util.TimeUtils;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
@ -168,7 +169,7 @@ public class NotesCMD extends PlexCommand
AtomicReference<Component> noteList = new AtomicReference<>(Component.text("Player notes for: " + plexPlayer.getName()).color(NamedTextColor.GREEN));
for (Note note : notes)
{
Component noteLine = mmString("<gold><!italic>" + note.getId() + " - Written by: " + DataUtils.getPlayer(note.getWrittenBy()).getName() + " on " + PlexUtils.useTimezone(note.getTimestamp()));
Component noteLine = mmString("<gold><!italic>" + note.getId() + " - Written by: " + DataUtils.getPlayer(note.getWrittenBy()).getName() + " on " + TimeUtils.useTimezone(note.getTimestamp()));
noteLine = noteLine.append(mmString("<newline><yellow># " + note.getNote()));
noteList.set(noteList.get().append(Component.newline()));
noteList.set(noteList.get().append(noteLine));

View File

@ -2,7 +2,7 @@ package dev.plex.listener.impl;
import com.destroystokyo.paper.event.server.PaperServerListPingEvent;
import dev.plex.listener.PlexListener;
import dev.plex.util.PlexUtils;
import dev.plex.util.RandomUtil;
import java.util.List;
import java.util.stream.Collectors;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
@ -23,7 +23,7 @@ public class ServerListener extends PlexListener
final StringBuilder motd = new StringBuilder();
for (final String word : baseMotd.split(" "))
{
motd.append(PlexUtils.randomChatColor()).append(word).append(" ");
motd.append(RandomUtil.getRandomColor()).append(word).append(" ");
}
event.motd(LegacyComponentSerializer.legacyAmpersand().deserialize(motd.toString().trim()));
}

View File

@ -5,6 +5,7 @@ import dev.morphia.annotations.Entity;
import dev.plex.Plex;
import dev.plex.util.MojangUtils;
import dev.plex.util.PlexUtils;
import dev.plex.util.TimeUtils;
import dev.plex.util.adapter.LocalDateTimeDeserializer;
import dev.plex.util.adapter.LocalDateTimeSerializer;
import java.time.LocalDateTime;
@ -43,9 +44,7 @@ public class Punishment
public static Component generateBanMessage(Punishment punishment)
{
return PlexUtils.messageComponent("banMessage", banUrl, punishment.getReason(),
PlexUtils.useTimezone(punishment.getEndDate()),
punishment.getPunisher() == null ? "CONSOLE" : MojangUtils.getInfo(punishment.getPunisher().toString()).getUsername());
return PlexUtils.messageComponent("banMessage", banUrl, punishment.getReason(), TimeUtils.useTimezone(punishment.getEndDate()), punishment.getPunisher() == null ? "CONSOLE" : MojangUtils.getInfo(punishment.getPunisher().toString()).getUsername());
}
public static Component generateIndefBanMessage(String type)

View File

@ -2,24 +2,17 @@ package dev.plex.util;
import dev.plex.Plex;
import dev.plex.PlexBase;
import dev.plex.cache.DataUtils;
import dev.plex.cache.PlayerCache;
import dev.plex.permission.Permission;
import dev.plex.player.PlexPlayer;
import dev.plex.storage.StorageType;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import net.kyori.adventure.text.Component;
@ -34,35 +27,16 @@ import org.bukkit.Particle;
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;
public class PlexUtils implements PlexBase
{
private static final Random RANDOM;
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
"f5cd54c4-3a24-4213-9a56-c06c49594dff", // Taahh
"ca83b658-c03b-4106-9edc-72f70a80656d", // ayunami2000
"2e06e049-24c8-42e4-8bcf-d35372af31e6" //Fleek
);
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");
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 <T> T addToArrayList(List<T> list, T object)
{
@ -87,11 +61,6 @@ public class PlexUtils implements PlexBase
players[0].getWorld().playSound(location, org.bukkit.Sound.BLOCK_FIRE_EXTINGUISH, 0.5f, 0.5f);
}
public static ChatColor randomChatColor()
{
return CHAT_COLOR_POOL.get(RANDOM.nextInt(CHAT_COLOR_POOL.size()));
}
public static void testConnections()
{
if (Plex.get().getSqlConnection().getDataSource() != null)
@ -144,11 +113,6 @@ public class PlexUtils implements PlexBase
return false;
}
public static String colorize(final String string)
{
return ChatColor.translateAlternateColorCodes('&', string);
}
private static final MiniMessage safeMessage = MiniMessage.builder().tags(TagResolver.builder().resolvers(
StandardTags.color(),
StandardTags.decorations(),
@ -202,17 +166,6 @@ public class PlexUtils implements PlexBase
return f;
}
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);
}
public static List<String> getPlayerNameList()
{
return Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList());
@ -235,24 +188,4 @@ public class PlexUtils implements PlexBase
pl.sendMessage(component);
});
}
public static boolean randomBoolean()
{
return ThreadLocalRandom.current().nextBoolean();
}
public static int randomNum()
{
return ThreadLocalRandom.current().nextInt();
}
public static int randomNum(int limit)
{
return ThreadLocalRandom.current().nextInt(limit);
}
public static int randomNum(int start, int limit)
{
return ThreadLocalRandom.current().nextInt(start, limit);
}
}

View File

@ -0,0 +1,34 @@
package dev.plex.util;
import java.util.concurrent.ThreadLocalRandom;
import net.kyori.adventure.text.format.NamedTextColor;
public class RandomUtil
{
public static NamedTextColor getRandomColor()
{
NamedTextColor[] colors = NamedTextColor.NAMES.values().toArray(NamedTextColor[]::new);
return colors[randomNum(colors.length)];
}
public static boolean randomBoolean()
{
return ThreadLocalRandom.current().nextBoolean();
}
public static int randomNum()
{
return ThreadLocalRandom.current().nextInt();
}
public static int randomNum(int limit)
{
return ThreadLocalRandom.current().nextInt(limit);
}
public static int randomNum(int start, int limit)
{
return ThreadLocalRandom.current().nextInt(start, limit);
}
}

View File

@ -1,13 +1,21 @@
package dev.plex.util;
import dev.plex.Plex;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
import org.apache.commons.lang.math.NumberUtils;
public class TimeUtils
{
private static String TIMEZONE = Plex.get().config.getString("server.timezone");
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 final List<String> timeUnits = new ArrayList<>()
{{
add("s");
@ -51,23 +59,13 @@ public class TimeUtils
return time;
}
public static long getDateNow()
public static String useTimezone(LocalDateTime date)
{
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;
// 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);
}
}