|
1 | 1 | package tk.fungy.wsformc;
|
2 | 2 |
|
3 | 3 | import fi.iki.elonen.NanoHTTPD;
|
| 4 | +import me.clip.placeholderapi.PlaceholderAPI; |
| 5 | +import org.bukkit.Bukkit; |
4 | 6 |
|
5 | 7 | import java.io.*;
|
6 | 8 | import java.nio.file.NoSuchFileException;
|
7 | 9 | import java.sql.Time;
|
8 | 10 | import java.text.SimpleDateFormat;
|
9 | 11 | import java.util.Date;
|
10 | 12 | import java.util.Map;
|
| 13 | +import java.util.regex.Matcher; |
| 14 | +import java.util.regex.Pattern; |
11 | 15 |
|
12 | 16 | import static tk.fungy.wsformc.FileManager.logsFolder;
|
13 | 17 |
|
@@ -136,12 +140,43 @@ public Response serve(IHTTPSession session) {
|
136 | 140 | throw new RuntimeException(e);
|
137 | 141 | }
|
138 | 142 | if (Method.GET.equals(method) && "/".equals(uri)) file = new File(Main.instance.getDataFolder() + "/web/" + "index.html".toLowerCase());
|
139 |
| - return newChunkedResponse(Response.Status.OK, mimeType, |
140 |
| - new FileInputStream(file)); |
| 143 | + |
| 144 | + if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { |
| 145 | + StringBuilder fileContent = new StringBuilder(); |
| 146 | + try (BufferedReader reader = new BufferedReader(new FileReader(Main.instance.getDataFolder() + "/web/" + uri.toLowerCase()))) { |
| 147 | + String line; |
| 148 | + while ((line = reader.readLine()) != null) { |
| 149 | + fileContent.append(line).append("\n"); |
| 150 | + } |
| 151 | + } catch (IOException e) { |
| 152 | + return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, "text/plain", "404 File Not Found - " + uri); |
| 153 | + } |
| 154 | + |
| 155 | + String placeholderRegex = "%(.*?)%"; |
| 156 | + |
| 157 | + Pattern pattern = Pattern.compile(placeholderRegex); |
| 158 | + Matcher matcher = pattern.matcher(fileContent); |
| 159 | + StringBuffer modifiedContentBuffer = new StringBuffer(); |
| 160 | + while (matcher.find()) { |
| 161 | + String placeholder = matcher.group(1); |
| 162 | + String resolvedValue = PlaceholderAPI.setPlaceholders(null, "%" + placeholder + "%"); |
| 163 | + matcher.appendReplacement(modifiedContentBuffer, Matcher.quoteReplacement(resolvedValue)); |
| 164 | + } |
| 165 | + |
| 166 | + matcher.appendTail(modifiedContentBuffer); |
| 167 | + |
| 168 | + String modifiedContent = modifiedContentBuffer.toString(); |
| 169 | + Response response = newFixedLengthResponse(Response.Status.OK, mimeType, modifiedContent); |
| 170 | + return response; |
| 171 | + } else { |
| 172 | + return newChunkedResponse(Response.Status.OK, mimeType, new FileInputStream(file)); |
| 173 | + } |
| 174 | + |
141 | 175 | } catch (FileNotFoundException e) {
|
142 | 176 | return newFixedLengthResponse(Response.Status.NOT_FOUND, "text/plain",
|
143 | 177 | "404 File Not Found - " + uri);
|
| 178 | + } catch (RuntimeException e) { |
| 179 | + throw new RuntimeException(e); |
144 | 180 | }
|
145 |
| - |
146 | 181 | }
|
147 | 182 | }
|
0 commit comments