6161
6262import java .io .*;
6363import java .lang .management .ManagementFactory ;
64- import java .lang .reflect .Field ;
65- import java .lang .reflect .InvocationTargetException ;
6664import java .nio .charset .StandardCharsets ;
6765import java .nio .file .Files ;
6866import java .text .DecimalFormat ;
@@ -84,24 +82,18 @@ public class Util {
8482 private static final List <BlockFace > VERTICAL_FACING = Collections .unmodifiableList (Arrays .asList (BlockFace .NORTH , BlockFace .EAST , BlockFace .SOUTH , BlockFace .WEST ));
8583 private static final List <String > DEBUG_LOGS = new ArrayList <>();
8684 private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock ();
87- @ Getter
88- private static final Map <String , String > CURRENCY_SYMBOL_MAPPING = new HashMap <>();
85+
8986 private static final ThreadLocal <MineDown > MINEDOWN = ThreadLocal .withInitial (() -> new MineDown ("" ));
9087 private static int bypassedCustomStackSize = -1 ;
9188 private static Yaml yaml = null ;
9289 private static Boolean devMode = null ;
9390 @ Setter
9491 private static QuickShop plugin ;
95- private static Object serverInstance ;
96- private static Field tpsField ;
9792 @ Getter
9893 private static boolean disableDebugLogger = false ;
9994 @ Getter
10095 @ Nullable
10196 private static DyeColor dyeColor = null ;
102- @ Nullable
103- private static Class <?> cachedNMSClass = null ;
104- private volatile static String nmsVersion ;
10597
10698 /**
10799 * Convert strArray to String. E.g "Foo, Bar"
@@ -353,73 +345,7 @@ public static void debugLog(@NotNull String... logs) {
353345 LOCK .writeLock ().unlock ();
354346 }
355347
356- /**
357- * Formats the given number according to how vault would like it. E.g. $50 or 5 dollars.
358- *
359- * @param n price
360- * @param shop shop
361- * @return The formatted string.
362- */
363- @ NotNull
364- public static String format (double n , @ Nullable Shop shop ) {
365- if (shop == null ) {
366- return "Error: Shop null" ;
367- }
368- return format (n , plugin .getConfig ().getBoolean ("shop.disable-vault-format" , false ), shop .getLocation ().getWorld (), shop );
369- }
370-
371- @ NotNull
372- public static String format (double n , boolean internalFormat , @ NotNull World world , @ Nullable Shop shop ) {
373- if (shop != null ) {
374- return format (n , internalFormat , world , shop .getCurrency ());
375- } else {
376- return format (n , internalFormat , world , (Shop ) null );
377- }
378- }
379-
380- @ NotNull
381- public static String format (double n , boolean internalFormat , @ NotNull World world , @ Nullable String currency ) {
382- if (internalFormat ) {
383- return getInternalFormat (n , currency );
384- }
385-
386- if (plugin == null ) {
387- Util .debugLog ("Called format before Plugin booted up, forcing fixing." );
388- plugin = QuickShop .getInstance ();
389- }
390- if (plugin .getEconomy () == null ) {
391- Util .debugLog ("Called format before Economy booted up, using built-in formatter." );
392- return getInternalFormat (n , currency );
393- }
394- try {
395- String formatted = plugin .getEconomy ().format (n , world , currency );
396- if (StringUtils .isEmpty (formatted )) {
397- Util .debugLog (
398- "Use alternate-currency-symbol to formatting, Cause economy plugin returned null" );
399- return getInternalFormat (n , currency );
400- } else {
401- return formatted ;
402- }
403- } catch (NumberFormatException e ) {
404- Util .debugLog ("format" , e .getMessage ());
405- Util .debugLog (
406- "format" , "Use alternate-currency-symbol to formatting, Cause NumberFormatException" );
407- return getInternalFormat (n , currency );
408- }
409- }
410348
411- private static String getInternalFormat (double amount , @ Nullable String currency ) {
412- if (StringUtils .isEmpty (currency )) {
413- Util .debugLog ("Format: Currency is null" );
414- String formatted = plugin .getConfig ().getBoolean ("use-decimal-format" , false ) ? MsgUtil .decimalFormat (amount ) : Double .toString (amount );
415- return plugin .getConfig ().getBoolean ("shop.currency-symbol-on-right" , false ) ? formatted + plugin .getConfig ().getString ("shop.alternate-currency-symbol" , "$" ) : plugin .getConfig ().getString ("shop.alternate-currency-symbol" , "$" ) + formatted ;
416- } else {
417- Util .debugLog ("Format: Currency is: [" + currency + "]" );
418- String formatted = plugin .getConfig ().getBoolean ("use-decimal-format" , false ) ? MsgUtil .decimalFormat (amount ) : Double .toString (amount );
419- String symbol = CURRENCY_SYMBOL_MAPPING .getOrDefault (currency , currency );
420- return plugin .getConfig ().getBoolean ("shop.currency-symbol-on-right" , false ) ? formatted + symbol : symbol + formatted ;
421- }
422- }
423349
424350 /**
425351 * return the right side for given blockFace
@@ -625,7 +551,6 @@ public static void initialize() {
625551 SHOPABLES .clear ();
626552 RESTRICTED_PRICES .clear ();
627553 CUSTOM_STACKSIZE .clear ();
628- CURRENCY_SYMBOL_MAPPING .clear ();
629554 devMode = plugin .getConfig ().getBoolean ("dev-mode" );
630555
631556 for (String s : plugin .getConfig ().getStringList ("shop-blocks" )) {
@@ -688,14 +613,7 @@ public static void initialize() {
688613 dyeColor = DyeColor .valueOf (plugin .getConfig ().getString ("shop.sign-dye-color" ));
689614 } catch (Exception ignored ) {
690615 }
691- List <String > symbols = plugin .getConfig ().getStringList ("shop.alternate-currency-symbol-list" );
692- symbols .forEach (entry -> {
693- String [] splits = entry .split (";" , 2 );
694- if (splits .length < 2 ) {
695- plugin .getLogger ().warning ("Invalid entry in alternate-currency-symbol-list: " + entry );
696- }
697- CURRENCY_SYMBOL_MAPPING .put (splits [0 ], splits [1 ]);
698- });
616+
699617 InteractUtil .init (plugin .getConfig ());
700618 }
701619
@@ -1171,14 +1089,6 @@ public static String getClassPrefix() {
11711089 return "[" + className + "-" + methodName + "] " ;
11721090 }
11731091
1174- @ NotNull
1175- public static String getNMSVersion () {
1176- if (nmsVersion == null ) {
1177- String name = Bukkit .getServer ().getClass ().getPackage ().getName ();
1178- nmsVersion = name .substring (name .lastIndexOf ('.' ) + 1 );
1179- }
1180- return nmsVersion ;
1181- }
11821092
11831093 /**
11841094 * Get the sign material using by plugin. With compatiabily process.
@@ -1194,37 +1104,6 @@ public static Material getSignMaterial() {
11941104 return Material .OAK_WALL_SIGN ;
11951105 }
11961106
1197- /**
1198- * Get MinecraftServer's TPS
1199- *
1200- * @return TPS (e.g 19.92)
1201- */
1202- @ NotNull
1203- public static Double getTPS () {
1204- if (serverInstance == null || tpsField == null ) {
1205- try {
1206- serverInstance = getNMSClass ("MinecraftServer" ).getMethod ("getServer" ).invoke (null );
1207- tpsField = serverInstance .getClass ().getField ("recentTps" );
1208- } catch (NoSuchFieldException
1209- | SecurityException
1210- | IllegalAccessException
1211- | IllegalArgumentException
1212- | InvocationTargetException
1213- | NoSuchMethodException e ) {
1214- plugin .getLogger ().log (Level .WARNING , "Failed to getting server TPS, please report to QuickShop." , e );
1215- serverInstance = null ;
1216- tpsField = null ;
1217- Util .debugLog ("Failed to get TPS " + e .getMessage ());
1218- return 20.0 ;
1219- }
1220- }
1221- try {
1222- double [] tps = ((double []) tpsField .get (serverInstance ));
1223- return tps [0 ];
1224- } catch (IllegalAccessException ignored ) {
1225- return 20.0 ;
1226- }
1227- }
12281107
12291108 @ SneakyThrows
12301109 public static void makeExportBackup (@ Nullable String backupName ) {
@@ -1253,23 +1132,6 @@ public static void makeExportBackup(@Nullable String backupName) {
12531132 });
12541133 }
12551134
1256- @ NotNull
1257- public static Class <?> getNMSClass (@ Nullable String className ) {
1258- if (cachedNMSClass != null ) {
1259- return cachedNMSClass ;
1260- }
1261- if (className == null ) {
1262- className = "MinecraftServer" ;
1263- }
1264- String name = Bukkit .getServer ().getClass ().getPackage ().getName ();
1265- String version = name .substring (name .lastIndexOf ('.' ) + 1 );
1266- try {
1267- cachedNMSClass = Class .forName ("net.minecraft.server." + version + "." + className );
1268- return cachedNMSClass ;
1269- } catch (ClassNotFoundException e ) {
1270- throw new RuntimeException (e );
1271- }
1272- }
12731135
12741136 /**
12751137 * Check QuickShop is running on dev edition or not.
0 commit comments