浏览代码

Refactoring message color conversion

RedstoneFuture 1 年之前
父节点
当前提交
3530f9e706

+ 44 - 2
missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/Messages.java

@@ -21,10 +21,14 @@ package de.butzlabben.missilewars.configuration;
 import de.butzlabben.missilewars.MissileWars;
 import de.butzlabben.missilewars.util.SetupUtil;
 import lombok.Getter;
+import me.clip.placeholderapi.PlaceholderAPI;
 import org.bukkit.ChatColor;
 import org.bukkit.configuration.file.YamlConfiguration;
+import org.bukkit.entity.Player;
 
 import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
 
 
 /**
@@ -73,8 +77,15 @@ public class Messages {
         return getConfigMessage(MessageEnum.PREFIX);
     }
 
+    /**
+     * This method returns the desired message from the 'messages.yml'. 
+     * The legacy color code with '&' is used.
+     * 
+     * @param msg the target message registered in the 'MessageEnum'
+     * @return (String) the converted message
+     */
     private static String getConfigMessage(MessageEnum msg) {
-        return ChatColor.translateAlternateColorCodes('&', cfg.getString(msg.getPath(),
+        return getConvertedMsg(cfg.getString(msg.getPath(),
                 "&cError while reading from messages.yml: '" + msg.getPath() + "'"));
     }
 
@@ -196,5 +207,36 @@ public class Messages {
         }
 
     }
-
+    
+    /**
+     * This method returns the desired message. Legacy 
+     * color-codes with '&' will be converted to the 
+     * final text message.
+     * 
+     * @param message the target message
+     * @return (String) the converted message
+     */
+    public static String getConvertedMsg(String message) {
+        return ChatColor.translateAlternateColorCodes('&', message);
+    }
+    
+    /**
+     * This method returns the desired message list. 
+     * Legacy color-codes with '&' will be converted 
+     * to the final text message.
+     * 
+     * @param messageList the target message list
+     * @return (String) the converted message list
+     */
+    public static List<String> getConvertedMsgList(List<String> messageList) {
+        List<String> convertedMsgList = new ArrayList<>();
+        for (String message : messageList) {
+            convertedMsgList.add(getConvertedMsg(message));
+        }
+        return convertedMsgList;
+    }
+    
+    public static String getPapiMessage(String message, Player player) {
+        return ChatColor.translateAlternateColorCodes('&', PlaceholderAPI.setPlaceholders(player, message));
+    }
 }

+ 3 - 5
missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/misc/ScoreboardManager.java

@@ -88,7 +88,7 @@ public class ScoreboardManager {
         }
         obj = board.registerNewObjective("Info", "dummy");
         obj.setDisplaySlot(DisplaySlot.SIDEBAR);
-        obj.setDisplayName(ChatColor.translateAlternateColorCodes('&', SCOREBOARD_TITLE));
+        obj.setDisplayName(SCOREBOARD_TITLE);
 
         // check if the team lists are used
         for (String cleanLine : SCOREBOARD_ENTRIES) {
@@ -255,10 +255,8 @@ public class ScoreboardManager {
             // Show the remaining duration of the last game:
             time = Integer.toString(game.getRemainingGameDuration() / 60);
         }
-
-
-        text = ChatColor.translateAlternateColorCodes('&', text);
-
+        
+        
         text = text.replace("%team1%", team1.getFullname());
         text = text.replace("%team2%", team2.getFullname());