Skip to content

Commit

Permalink
Moved formatDate to DateFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
RoinujNosde committed Oct 27, 2023
1 parent 32f41d3 commit 42f9bd1
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager;
import net.sacredlabyrinth.phaed.simpleclans.utils.ChatUtils;
import net.sacredlabyrinth.phaed.simpleclans.utils.CurrencyFormat;
import net.sacredlabyrinth.phaed.simpleclans.utils.DateFormat;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
Expand Down Expand Up @@ -464,7 +465,7 @@ public long getFounded() {
*/
@Placeholder("founded")
public String getFoundedString() {
return ChatUtils.formatDate(founded);
return DateFormat.formatDateTime(founded);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.sacredlabyrinth.phaed.simpleclans.hooks.papi.Placeholder;
import net.sacredlabyrinth.phaed.simpleclans.managers.ProtectionManager.Action;
import net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager;
import net.sacredlabyrinth.phaed.simpleclans.utils.ChatUtils;
import net.sacredlabyrinth.phaed.simpleclans.utils.DateFormat;
import net.sacredlabyrinth.phaed.simpleclans.utils.VanishUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -488,7 +488,7 @@ public String getJoinDateString() {
if (joinDate == 0) {
return "";
}
return ChatUtils.formatDate(joinDate);
return DateFormat.formatDateTime(joinDate);
}

/**
Expand All @@ -512,7 +512,7 @@ public String getLastSeenString(@Nullable CommandSender sender) {
if (!VanishUtils.isVanished(sender, this)) {
return lang("online", sender);
}
return ChatUtils.formatDate(lastSeen);
return DateFormat.formatDateTime(lastSeen);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.*;
import java.util.logging.Level;

Expand Down Expand Up @@ -313,7 +312,7 @@ public enum ConfigField {
USERNAME_REGEX("settings.username-regex", "^\\**[a-zA-Z0-9_$]{1,16}$"),
TAG_REGEX("settings.tag-regex", ""),
ACCEPT_OTHER_ALPHABETS_LETTERS("settings.accept-other-alphabets-letters-on-tag", false),
DATE_PATTERN("settings.date-time-format-pattern", "HH:mm - dd/MM/yyyy"),
DATE_TIME_PATTERN("settings.date-time-pattern", "HH:mm - dd/MM/yyyy"),
/*
================
> Tag Settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.*;
import net.md_5.bungee.api.chat.ComponentBuilder.FormatRetention;
import net.sacredlabyrinth.phaed.simpleclans.SimpleClans;
import net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -22,7 +18,6 @@

public class ChatUtils {

private static final SimpleClans plugin = SimpleClans.getInstance();
public static boolean HEX_COLOR_SUPPORT;

private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("(%([A-Za-z]+)%)");
Expand Down Expand Up @@ -52,7 +47,7 @@ public static String parseColors(@NotNull String text) {
// https://www.spigotmc.org/threads/hex-color-code-translate.449748/#post-3867795
if (HEX_COLOR_SUPPORT) {
Matcher matcher = HEX_COLOR_PATTERN.matcher(text);
StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
while (matcher.find()) {
matcher.appendReplacement(buffer, ChatColor.of("#" + matcher.group(1)).toString());
}
Expand All @@ -64,7 +59,7 @@ public static String parseColors(@NotNull String text) {
public static String stripColors(String text) {
Pattern pattern = HEX_COLOR_SUPPORT ? HEX_STRIP_COLOR_PATTERN : STRIP_COLOR_PATTERN;
Matcher matcher = pattern.matcher(text);
StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
while (matcher.find()) {
matcher.appendReplacement(buffer, "");
}
Expand Down Expand Up @@ -168,9 +163,4 @@ public static void applyLastColorToFollowingLines(@NotNull List<String> lines) {
}
}

@Deprecated
public static String formatDate(long time) { //TODO move to DateFormat class
return new SimpleDateFormat(plugin.getSettingsManager().getString(SettingsManager.ConfigField.DATE_PATTERN)).format(new Date(time));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.sacredlabyrinth.phaed.simpleclans.utils;

import net.sacredlabyrinth.phaed.simpleclans.SimpleClans;

import java.text.SimpleDateFormat;
import java.util.Date;

import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.DATE_TIME_PATTERN;

public class DateFormat {

private static final SimpleClans plugin = SimpleClans.getInstance();
private static SimpleDateFormat format;

static {
String pattern = plugin.getSettingsManager().getString(DATE_TIME_PATTERN);
try {
format = new SimpleDateFormat(pattern);
} catch (IllegalArgumentException ex) {
plugin.getLogger().warning("%s is not a valid pattern!".formatted(pattern));
format = new SimpleDateFormat("HH:mm - dd/MM/yyyy");
}
}

public static String formatDateTime(long date) {
return format.format(new Date(date));
}

}
2 changes: 1 addition & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ settings:
past-clans-limit: 10
username-regex: '(\.|\*){0,1}[a-zA-Z0-9_$]{1,16}'
tag-regex: ''
date-time-format-pattern: 'HH:mm - dd/MM/yyyy'
date-time-pattern: 'HH:mm - dd/MM/yyyy'
war-and-protection:
war-enabled: false
land-sharing: true
Expand Down

0 comments on commit 42f9bd1

Please sign in to comment.