Skip to content

Commit 5c454e1

Browse files
committed
1.4 update
1 parent b383ffd commit 5c454e1

File tree

7 files changed

+85
-31
lines changed

7 files changed

+85
-31
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>lol.hyper</groupId>
88
<artifactId>anarchystats</artifactId>
9-
<version>1.3</version>
9+
<version>1.4</version>
1010
<packaging>jar</packaging>
1111

1212
<name>AnarchyStats</name>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ public final class AnarchyStats extends JavaPlugin {
1717
public static String worldSize;
1818
public final File configFile = new File(this.getDataFolder(), "config.yml");
1919
public FileConfiguration config;
20-
public Logger logger = this.getLogger();
20+
public final Logger logger = this.getLogger();
2121

2222
public CommandInfo commandInfo;
2323
public CommandReload commandReload;
24+
public MessageParser messageParser;
2425

2526
@Override
2627
public void onEnable() {
27-
commandInfo = new CommandInfo(this);
28+
messageParser = new MessageParser(this);
29+
commandInfo = new CommandInfo(this, messageParser);
2830
commandReload = new CommandReload(this);
2931
if (!configFile.exists()) {
3032
this.saveResource("config.yml", true);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package lol.hyper.anarchystats;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.ChatColor;
5+
6+
import java.time.LocalDate;
7+
import java.time.format.DateTimeFormatter;
8+
import java.time.temporal.ChronoUnit;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
public class MessageParser {
13+
14+
private final AnarchyStats anarchyStats;
15+
16+
public MessageParser(AnarchyStats anarchyStats) {
17+
this.anarchyStats = anarchyStats;
18+
}
19+
20+
public List<String> getCommandMessage() {
21+
String date = anarchyStats.config.getString("date");
22+
List<String> rawMessages = anarchyStats.config.getStringList("command-message");
23+
List<String> formattedMessage = new ArrayList<>();
24+
for (String x : rawMessages) {
25+
if (x.contains("{{STARTDATE}}")) {
26+
x = x.replace("{{STARTDATE}}", date);
27+
}
28+
29+
if (x.contains("{{DAYS}}")) {
30+
x = x.replace("{{DAYS}}", Long.toString(getDays()));
31+
}
32+
33+
if (x.contains("{{WORLDSIZE}}")) {
34+
x = x.replace("{{WORLDSIZE}}", AnarchyStats.worldSize);
35+
}
36+
37+
if (x.contains("{{TOTALJOINS}}")) {
38+
x = x.replace("{{TOTALJOINS}}", Integer.toString(Bukkit.getOfflinePlayers().length));
39+
}
40+
41+
formattedMessage.add(ChatColor.translateAlternateColorCodes('&', x));
42+
}
43+
44+
return formattedMessage;
45+
}
46+
47+
// Calculates the days between today and day 1.
48+
public long getDays() {
49+
LocalDate now = LocalDate.now();
50+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/dd/yyyy");
51+
String date = anarchyStats.config.getString("date");
52+
LocalDate firstDay = LocalDate.parse(date, formatter);
53+
return ChronoUnit.DAYS.between(firstDay, now);
54+
}
55+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public class WorldSize {
1616

1717
/**
1818
* Attempts to calculate the size of a file or directory.
19-
*
20-
* <p>
2119
* Since the operation is non-atomic, the returned value may be inaccurate.
2220
* However, this method is quick and does its best.
21+
*
22+
* https://stackoverflow.com/a/19877372
2323
*/
2424
public static long getWorldSize(Path path) {
2525

@@ -58,6 +58,9 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
5858
return size.get();
5959
}
6060

61+
/**
62+
* https://stackoverflow.com/a/5599842
63+
*/
6164
public static String readableFileSize(long size) {
6265
if (size <= 0) return "0";
6366
final String[] units = new String[]{"B", "kB", "MB", "GB", "TB"};
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,29 @@
11
package lol.hyper.anarchystats.commands;
22

33
import lol.hyper.anarchystats.AnarchyStats;
4+
import lol.hyper.anarchystats.MessageParser;
45
import lol.hyper.anarchystats.WorldSize;
56
import org.bukkit.Bukkit;
6-
import org.bukkit.ChatColor;
77
import org.bukkit.command.Command;
88
import org.bukkit.command.CommandExecutor;
99
import org.bukkit.command.CommandSender;
1010

11-
import java.time.LocalDate;
12-
import java.time.format.DateTimeFormatter;
13-
import java.time.temporal.ChronoUnit;
14-
1511
public class CommandInfo implements CommandExecutor {
1612

1713
private final AnarchyStats anarchyStats;
14+
private final MessageParser messageParser;
1815

19-
public CommandInfo(AnarchyStats anarchyStats) {
16+
public CommandInfo(AnarchyStats anarchyStats, MessageParser messageParser) {
2017
this.anarchyStats = anarchyStats;
18+
this.messageParser = messageParser;
2119
}
2220

2321
@Override
2422
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
25-
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/d/yyyy");
26-
String date = anarchyStats.config.getString("date");
27-
LocalDate firstDay = LocalDate.parse(date, formatter);
2823
Bukkit.getScheduler().runTaskAsynchronously(anarchyStats, () -> anarchyStats.updateWorldSize(WorldSize.world, WorldSize.nether, WorldSize.end));
29-
sender.sendMessage(ChatColor.GOLD + "--------------------------------------------");
30-
sender.sendMessage(ChatColor.DARK_AQUA + anarchyStats.config.getString("server-name") + " was created on " + date + ". That was " + getDays() + " days ago.");
31-
sender.sendMessage(ChatColor.DARK_AQUA + "The world is " + AnarchyStats.worldSize + ".");
32-
sender.sendMessage(ChatColor.DARK_AQUA + "A total of " + Bukkit.getOfflinePlayers().length + " players have joined.");
33-
sender.sendMessage(ChatColor.GOLD + "--------------------------------------------");
24+
for (String x : messageParser.getCommandMessage()) {
25+
sender.sendMessage(x);
26+
}
3427
return true;
3528
}
36-
// Calculates the days between today and day 1.
37-
public long getDays() {
38-
LocalDate now = LocalDate.now();
39-
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/d/yyyy");
40-
String date = anarchyStats.config.getString("date");
41-
LocalDate firstDay = LocalDate.parse(date, formatter);
42-
return ChronoUnit.DAYS.between(firstDay, now);
43-
}
4429
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.bukkit.ChatColor;
55
import org.bukkit.command.Command;
66
import org.bukkit.command.CommandSender;
7-
import org.bukkit.command.ConsoleCommandSender;
87
import org.bukkit.command.TabExecutor;
98

109
import java.util.Collections;
@@ -20,7 +19,7 @@ public CommandReload (AnarchyStats anarchyStats) {
2019

2120
@Override
2221
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
23-
if (args.length == 0 || sender instanceof ConsoleCommandSender) {
22+
if (args.length == 0) {
2423
sender.sendMessage(ChatColor.GREEN + "AnarchyStats version " + anarchyStats.getDescription().getVersion() + ". Created by hyperdefined.");
2524
return true;
2625
} else if (args.length == 1) {

src/main/resources/config.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,15 @@
22
# Format: MM/DD/YYYY. You must put zeros for single digits. Example: 06, 02, 01, etc.
33
date: 05/27/2019 # This date is May 27th, 2019.
44

5-
# Change this to match your server name. Displays on the command.
6-
server-name: "ServerName"
5+
6+
# This is where you can change what message is on the command.
7+
# {{STARTDATE}} - Displays the start date for the server. Uses the date above.
8+
# {{DAYS}} - Displays how many days since the start date for the server.
9+
# {{WORLDSIZE}} - Displays the world size.
10+
# {{TOTALJOINS}} - Displays the total unique players that have joined.
11+
command-message:
12+
- "&6--------------------------------------------"
13+
- "&3ServerName was created on {{STARTDATE}}. That was {{DAYS}} days ago."
14+
- "&3The world is {{WORLDSIZE}}."
15+
- "&3A total of {{TOTALJOINS}} players have joined."
16+
- "&6--------------------------------------------"

0 commit comments

Comments
 (0)