Skip to content

Commit bf3592b

Browse files
committed
PlaceholderAPI on website
This placeholder can be used as statistics. YOU CANNOT YOU THEY AS PLAYER!!
1 parent 17f017a commit bf3592b

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
<id>sonatype</id>
7070
<url>https://oss.sonatype.org/content/groups/public/</url>
7171
</repository>
72+
<repository>
73+
<id>placeholderapi</id>
74+
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
75+
</repository>
7276
</repositories>
7377

7478
<dependencies>
@@ -78,6 +82,12 @@
7882
<version>3.0.0</version>
7983
<scope>compile</scope>
8084
</dependency>
85+
<dependency>
86+
<groupId>me.clip</groupId>
87+
<artifactId>placeholderapi</artifactId>
88+
<version>2.11.2</version>
89+
<scope>provided</scope>
90+
</dependency>
8191
<dependency>
8292
<groupId>org.nanohttpd</groupId>
8393
<artifactId>nanohttpd</artifactId>

src/main/java/tk/fungy/wsformc/Main.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tk.fungy.wsformc;
22

33
import org.bstats.bukkit.Metrics;
4+
import org.bukkit.Bukkit;
45
import org.bukkit.plugin.java.JavaPlugin;
56

67
import java.net.UnknownHostException;
@@ -42,14 +43,15 @@ public void onEnable() {
4243
}
4344
}
4445
FileManager.setStringInConfig("Version", getDescription().getVersion());
45-
if (!(new FileManager().getBooleanFromConfig("Version"))) {
46-
new FileManager().setBooleanInConfig("Updates", true);
47-
}
4846

4947
int pluginId = 17696;
5048
Metrics metrics = new Metrics(Main.instance, pluginId);
5149

5250
Updater.startUpdater();
51+
52+
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") == null) {
53+
getLogger().warning("Could not find PlaceholderAPI! This plugin is required for WebServer placeholders.");
54+
}
5355
}
5456

5557
@Override

src/main/java/tk/fungy/wsformc/WebServer.java

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package tk.fungy.wsformc;
22

33
import fi.iki.elonen.NanoHTTPD;
4+
import me.clip.placeholderapi.PlaceholderAPI;
5+
import org.bukkit.Bukkit;
46

57
import java.io.*;
68
import java.nio.file.NoSuchFileException;
79
import java.sql.Time;
810
import java.text.SimpleDateFormat;
911
import java.util.Date;
1012
import java.util.Map;
13+
import java.util.regex.Matcher;
14+
import java.util.regex.Pattern;
1115

1216
import static tk.fungy.wsformc.FileManager.logsFolder;
1317

@@ -136,12 +140,43 @@ public Response serve(IHTTPSession session) {
136140
throw new RuntimeException(e);
137141
}
138142
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+
141175
} catch (FileNotFoundException e) {
142176
return newFixedLengthResponse(Response.Status.NOT_FOUND, "text/plain",
143177
"404 File Not Found - " + uri);
178+
} catch (RuntimeException e) {
179+
throw new RuntimeException(e);
144180
}
145-
146181
}
147182
}

src/main/resources/plugin.yml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ main: tk.fungy.wsformc.Main
44
api-version: 1.19
55
authors: [ FungY911 ]
66
description: Host your website in your Minecraft Server!
7+
softdepend: [ PlaceholderAPI ]
78
commands:
89
wsm:
910
usage: /wsm

0 commit comments

Comments
 (0)