Skip to content

Commit 92d6bf0

Browse files
committed
minimessage support
1 parent 27f2176 commit 92d6bf0

File tree

7 files changed

+107
-41
lines changed

7 files changed

+107
-41
lines changed

pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
<dependency>
9999
<groupId>org.spigotmc</groupId>
100100
<artifactId>spigot-api</artifactId>
101-
<version>1.17-R0.1-SNAPSHOT</version>
101+
<version>1.18.2-R0.1-SNAPSHOT</version>
102102
<scope>provided</scope>
103103
</dependency>
104104
<dependency>
@@ -113,5 +113,17 @@
113113
<version>1.0.1</version>
114114
<scope>provided</scope>
115115
</dependency>
116+
<dependency>
117+
<groupId>net.kyori</groupId>
118+
<artifactId>adventure-text-minimessage</artifactId>
119+
<version>4.11.0</version>
120+
<scope>provided</scope>
121+
</dependency>
122+
<dependency>
123+
<groupId>net.kyori</groupId>
124+
<artifactId>adventure-platform-bukkit</artifactId>
125+
<version>4.1.0</version>
126+
<scope>provided</scope>
127+
</dependency>
116128
</dependencies>
117129
</project>

src/main/java/lol/hyper/anarchystats/AnarchyStats.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import lol.hyper.anarchystats.tools.WorldSize;
2525
import lol.hyper.githubreleaseapi.GitHubRelease;
2626
import lol.hyper.githubreleaseapi.GitHubReleaseAPI;
27+
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
28+
import net.kyori.adventure.text.minimessage.MiniMessage;
2729
import org.bstats.bukkit.Metrics;
2830
import org.bukkit.Bukkit;
2931
import org.bukkit.configuration.file.FileConfiguration;
@@ -43,13 +45,16 @@ public final class AnarchyStats extends JavaPlugin {
4345
public final File configFile = new File(this.getDataFolder(), "config.yml");
4446
public final Logger logger = this.getLogger();
4547
public final ArrayList<Path> worldPaths = new ArrayList<>();
46-
public final int CONFIG_VERSION = 1;
48+
public final int CONFIG_VERSION = 2;
49+
public final MiniMessage miniMessage = MiniMessage.miniMessage();
50+
private BukkitAudiences adventure;
4751
public FileConfiguration config;
4852
public CommandReload commandReload;
4953
public MessageParser messageParser;
5054

5155
@Override
5256
public void onEnable() {
57+
this.adventure = BukkitAudiences.create(this);
5358
messageParser = new MessageParser(this);
5459
commandReload = new CommandReload(this);
5560
if (!configFile.exists()) {
@@ -80,7 +85,7 @@ public void loadConfig() {
8085
}
8186
for (String x : config.getStringList("worlds-to-use")) {
8287
Path currentPath =
83-
Paths.get(Paths.get(".").toAbsolutePath().normalize().toString() + File.separator + x);
88+
Paths.get(Paths.get(".").toAbsolutePath().normalize() + File.separator + x);
8489
if (!currentPath.toFile().exists()) {
8590
logger.warning("World folder \"" + x + "\" does not exist! Excluding from size calculation.");
8691
} else {
@@ -115,4 +120,12 @@ public void checkForUpdates() {
115120
logger.warning("A new version is available (" + latest.getTagVersion() + ")! You are running version " + current.getTagVersion() + ". You are " + buildsBehind + " version(s) behind.");
116121
}
117122
}
123+
124+
public BukkitAudiences getAdventure() {
125+
if (this.adventure == null) {
126+
throw new IllegalStateException(
127+
"Tried to access Adventure when the plugin was disabled!");
128+
}
129+
return this.adventure;
130+
}
118131
}

src/main/java/lol/hyper/anarchystats/commands/CommandInfo.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,41 @@
1919

2020
import lol.hyper.anarchystats.AnarchyStats;
2121
import lol.hyper.anarchystats.tools.AbstractCommand;
22+
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
23+
import net.kyori.adventure.text.Component;
24+
import net.kyori.adventure.text.format.NamedTextColor;
2225
import org.bukkit.Bukkit;
23-
import org.bukkit.ChatColor;
2426
import org.bukkit.command.Command;
2527
import org.bukkit.command.CommandSender;
2628
import org.jetbrains.annotations.NotNull;
2729

2830
public class CommandInfo extends AbstractCommand {
2931

3032
private final AnarchyStats anarchyStats;
33+
private final BukkitAudiences audiences;
3134

3235
public CommandInfo(String command, AnarchyStats anarchyStats) {
3336
super(command);
3437
this.anarchyStats = anarchyStats;
38+
this.audiences = anarchyStats.getAdventure();
3539
}
3640

3741
@Override
3842
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
3943
if (anarchyStats.config.getBoolean("use-permission-node")) {
40-
if (!sender.hasPermission(anarchyStats.config.getString("permission-node"))) {
41-
sender.sendMessage(ChatColor.RED + "You don't have permission for this command.");
44+
String permission = anarchyStats.config.getString("permission-node");
45+
if (permission == null) {
46+
audiences.sender(sender).sendMessage(Component.text("Permission node is not set for this command! Please see 'permission-node' under AnarchyStats' config!").color(NamedTextColor.RED));
47+
return true;
48+
}
49+
if (!sender.hasPermission(permission)) {
50+
audiences.sender(sender).sendMessage(Component.text("You do not have permission for this command.").color(NamedTextColor.RED));
4251
return true;
4352
}
4453
}
4554
Bukkit.getScheduler().runTaskAsynchronously(anarchyStats, anarchyStats::updateWorldSize);
46-
for (String x : anarchyStats.messageParser.getCommandMessage()) {
47-
sender.sendMessage(x);
48-
}
55+
Component infoCommand = anarchyStats.messageParser.infoCommand();
56+
audiences.sender(sender).sendMessage(infoCommand);
4957
return true;
5058
}
5159
}

src/main/java/lol/hyper/anarchystats/commands/CommandReload.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
package lol.hyper.anarchystats.commands;
1919

2020
import lol.hyper.anarchystats.AnarchyStats;
21-
import org.bukkit.ChatColor;
21+
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
22+
import net.kyori.adventure.text.Component;
23+
import net.kyori.adventure.text.format.NamedTextColor;
2224
import org.bukkit.command.Command;
2325
import org.bukkit.command.CommandSender;
2426
import org.bukkit.command.TabExecutor;
@@ -30,24 +32,25 @@
3032
public class CommandReload implements TabExecutor {
3133

3234
private final AnarchyStats anarchyStats;
35+
private final BukkitAudiences audiences;
3336

3437
public CommandReload(AnarchyStats anarchyStats) {
3538
this.anarchyStats = anarchyStats;
39+
this.audiences = anarchyStats.getAdventure();
3640
}
3741

3842
@Override
3943
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
4044
if (args.length == 0) {
41-
sender.sendMessage(ChatColor.GREEN + "AnarchyStats version "
42-
+ anarchyStats.getDescription().getVersion() + ". Created by hyperdefined.");
45+
audiences.sender(sender).sendMessage(Component.text("AnarchyStats version " + anarchyStats.getDescription().getVersion() + ". Created by hyperdefined.").color(NamedTextColor.GREEN));
4346
return true;
4447
} else if (args.length == 1) {
4548
if (args[0].equalsIgnoreCase("reload")) {
4649
if (sender.hasPermission("anarchystats.reload")) {
4750
anarchyStats.loadConfig();
48-
sender.sendMessage(ChatColor.GREEN + "Config reloaded!");
51+
audiences.sender(sender).sendMessage(Component.text("Config reloaded!").color(NamedTextColor.GREEN));
4952
} else {
50-
sender.sendMessage(ChatColor.RED + "You do not have permission for this command.");
53+
audiences.sender(sender).sendMessage(Component.text("You do not have permission for this command.").color(NamedTextColor.RED));
5154
}
5255
}
5356
}

src/main/java/lol/hyper/anarchystats/tools/MessageParser.java

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,62 +18,90 @@
1818
package lol.hyper.anarchystats.tools;
1919

2020
import lol.hyper.anarchystats.AnarchyStats;
21+
import net.kyori.adventure.text.Component;
22+
import net.kyori.adventure.text.minimessage.MiniMessage;
2123
import org.bukkit.Bukkit;
22-
import org.bukkit.ChatColor;
2324

2425
import java.text.DateFormat;
2526
import java.text.ParseException;
2627
import java.text.SimpleDateFormat;
2728
import java.time.LocalDate;
2829
import java.time.format.DateTimeFormatter;
2930
import java.time.temporal.ChronoUnit;
30-
import java.util.ArrayList;
3131
import java.util.Date;
3232
import java.util.List;
3333
import java.util.Locale;
3434

3535
public class MessageParser {
3636

3737
private final AnarchyStats anarchyStats;
38+
private final MiniMessage miniMessage;
3839

3940
public MessageParser(AnarchyStats anarchyStats) {
4041
this.anarchyStats = anarchyStats;
42+
this.miniMessage = anarchyStats.miniMessage;
4143
}
4244

43-
public List<String> getCommandMessage() {
44-
String date = anarchyStats.config.getString("date");
45+
46+
/**
47+
* Builds the /info command from the config.
48+
* @return A full component with the command.
49+
*/
50+
public Component infoCommand() {
51+
String configDate = anarchyStats.config.getString("date");
4552
DateFormat originalFormat = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
46-
DateFormat newFormat = new SimpleDateFormat(anarchyStats.config.getString("date-format"), Locale.ENGLISH);
53+
String configFormat = anarchyStats.config.getString("date-format");
54+
DateFormat finalFormat;
4755
Date originalDate = null;
4856
try {
49-
originalDate = originalFormat.parse(date);
57+
originalDate = originalFormat.parse(configDate);
5058
} catch (ParseException e) {
5159
e.printStackTrace();
5260
}
53-
String newDate = newFormat.format(originalDate);
61+
String finalDate;
62+
if (configFormat == null) {
63+
anarchyStats.logger.severe("date-format is invalid! Trying to use default formatting for date instead.");
64+
finalDate = originalFormat.format(originalDate);
65+
} else {
66+
finalFormat = new SimpleDateFormat(configFormat, Locale.ENGLISH);
67+
finalDate = finalFormat.format(originalDate);
68+
}
69+
5470
List<String> rawMessages = anarchyStats.config.getStringList("command-message");
55-
List<String> formattedMessage = new ArrayList<>();
56-
for (String x : rawMessages) {
57-
if (x.contains("{{STARTDATE}}")) {
58-
x = x.replace("{{STARTDATE}}", newDate);
71+
if (rawMessages.isEmpty()) {
72+
return null;
73+
}
74+
75+
// start with an empty component
76+
Component infoCommand = Component.empty();
77+
for (int i = 0; i < rawMessages.size(); i++) {
78+
String line = rawMessages.get(i);
79+
if (line.contains("{{STARTDATE}}")) {
80+
line = line.replace("{{STARTDATE}}", finalDate);
5981
}
6082

61-
if (x.contains("{{DAYS}}")) {
62-
x = x.replace("{{DAYS}}", Long.toString(getDays()));
83+
if (line.contains("{{DAYS}}")) {
84+
line = line.replace("{{DAYS}}", Long.toString(getDays()));
6385
}
6486

65-
if (x.contains("{{WORLDSIZE}}")) {
66-
x = x.replace("{{WORLDSIZE}}", AnarchyStats.worldSize);
87+
if (line.contains("{{WORLDSIZE}}")) {
88+
line = line.replace("{{WORLDSIZE}}", AnarchyStats.worldSize);
6789
}
6890

69-
if (x.contains("{{TOTALJOINS}}")) {
70-
x = x.replace("{{TOTALJOINS}}", Integer.toString(Bukkit.getOfflinePlayers().length));
91+
if (line.contains("{{TOTALJOINS}}")) {
92+
line = line.replace("{{TOTALJOINS}}", Integer.toString(Bukkit.getOfflinePlayers().length));
7193
}
7294

73-
formattedMessage.add(ChatColor.translateAlternateColorCodes('&', x));
95+
// append a new line + the component
96+
// don't add a new line if it's the first one
97+
// creates a gap
98+
if (i == 0) {
99+
infoCommand = miniMessage.deserialize(line);
100+
} else {
101+
infoCommand = infoCommand.append(Component.newline()).append(miniMessage.deserialize(line));
102+
}
74103
}
75-
76-
return formattedMessage;
104+
return infoCommand;
77105
}
78106

79107
// Calculates the days between today and day 1.

src/main/resources/config.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ worlds-to-use:
2626
# {{WORLDSIZE}} - Displays the world size.
2727
# {{TOTALJOINS}} - Displays the total unique players that have joined.
2828
command-message:
29-
- "&6--------------------------------------------"
30-
- "&3ServerName was created on {{STARTDATE}}. That was {{DAYS}} days ago."
31-
- "&3The world is {{WORLDSIZE}}."
32-
- "&3A total of {{TOTALJOINS}} players have joined."
33-
- "&6--------------------------------------------"
29+
- "<gold>--------------------------------------------</gold>"
30+
- "<dark_aqua>ServerName was created on {{STARTDATE}}. That was {{DAYS}} days ago.</dark_aqua>"
31+
- "<dark_aqua>The world is {{WORLDSIZE}}.</dark_aqua>"
32+
- "<dark_aqua>A total of {{TOTALJOINS}} players have joined.</dark_aqua>"
33+
- "<gold>--------------------------------------------</gold>"
3434

3535
# Require the command to have permissions to use. Setting this value to "true" will require the player to have the permission below to use the command.
3636
# You can change the permission node to whatever you want.
3737
use-permission-node: false
3838
permission-node: "anarchystats.info"
3939

40-
config-version: 1
40+
config-version: 2

src/main/resources/plugin.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ commands:
1414
anarchystats:
1515
usage: /anarchystats
1616
libraries:
17-
- lol.hyper:github-release-api:1.0.1
17+
- lol.hyper:github-release-api:1.0.1
18+
- net.kyori:adventure-text-minimessage:4.11.0
19+
- net.kyori:adventure-platform-bukkit:4.1.0

0 commit comments

Comments
 (0)