Skip to content

Commit c70367c

Browse files
committed
hyperlib demo
1 parent 33cad91 commit c70367c

File tree

4 files changed

+57
-87
lines changed

4 files changed

+57
-87
lines changed

pom.xml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,23 @@
9696
<id>papermc</id>
9797
<url>https://repo.papermc.io/repository/maven-public/</url>
9898
</repository>
99+
<repository>
100+
<id>jitpack.io</id>
101+
<url>https://jitpack.io</url>
102+
</repository>
99103
</repositories>
100104

101105
<dependencies>
102106
<dependency>
103107
<groupId>io.papermc.paper</groupId>
104108
<artifactId>paper-api</artifactId>
105-
<version>1.21.4-R0.1-SNAPSHOT</version>
109+
<version>1.21.8-R0.1-SNAPSHOT</version>
106110
<scope>provided</scope>
107111
</dependency>
108112
<dependency>
109-
<groupId>org.bstats</groupId>
110-
<artifactId>bstats-bukkit</artifactId>
111-
<version>3.1.0</version>
112-
<scope>compile</scope>
113-
</dependency>
114-
<dependency>
115-
<groupId>lol.hyper</groupId>
116-
<artifactId>github-release-api</artifactId>
117-
<version>1.0.5</version>
113+
<groupId>com.github.hyperdefined</groupId>
114+
<artifactId>hyperlib</artifactId>
115+
<version>1.0.8</version>
118116
<scope>compile</scope>
119117
</dependency>
120118
</dependencies>

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

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,45 @@
2222
import lol.hyper.anarchystats.tools.AbstractCommand;
2323
import lol.hyper.anarchystats.tools.MessageParser;
2424
import lol.hyper.anarchystats.tools.WorldSize;
25-
import lol.hyper.githubreleaseapi.GitHubRelease;
26-
import lol.hyper.githubreleaseapi.GitHubReleaseAPI;
27-
import org.bstats.bukkit.Metrics;
25+
import lol.hyper.hyperlib.HyperLib;
26+
import lol.hyper.hyperlib.bstats.HyperStats;
27+
import lol.hyper.hyperlib.releases.HyperUpdater;
28+
import lol.hyper.hyperlib.utils.TextUtils;
29+
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
2830
import org.bukkit.Bukkit;
2931
import org.bukkit.configuration.file.FileConfiguration;
3032
import org.bukkit.configuration.file.YamlConfiguration;
3133
import org.bukkit.plugin.java.JavaPlugin;
3234

3335
import java.io.File;
34-
import java.io.IOException;
3536
import java.nio.file.Path;
3637
import java.nio.file.Paths;
3738
import java.util.ArrayList;
3839
import java.util.List;
39-
import java.util.logging.Logger;
4040

4141
public final class AnarchyStats extends JavaPlugin {
4242

4343
public String worldSize;
4444
public final File configFile = new File(this.getDataFolder(), "config.yml");
45-
public final Logger logger = this.getLogger();
45+
public final ComponentLogger logger = this.getComponentLogger();
4646
public final List<Path> worldPaths = new ArrayList<>();
4747
public final int CONFIG_VERSION = 2;
4848
public FileConfiguration config;
4949
public CommandReload commandReload;
5050
public MessageParser messageParser;
51+
public HyperLib hyperLib;
52+
public TextUtils textUtils;
5153

5254
@Override
5355
public void onEnable() {
56+
hyperLib = new HyperLib(this);
57+
hyperLib.setup();
58+
59+
textUtils = new TextUtils(hyperLib);
60+
61+
HyperStats stats = new HyperStats(hyperLib, 6877);
62+
stats.setup();
63+
5464
messageParser = new MessageParser(this);
5565
commandReload = new CommandReload(this);
5666
if (!configFile.exists()) {
@@ -65,13 +75,15 @@ public void onEnable() {
6575
this.getCommand("anarchystats").setExecutor(commandReload);
6676
Bukkit.getAsyncScheduler().runNow(this, scheduledTask -> updateWorldSize());
6777

68-
new Metrics(this, 6877);
69-
70-
Bukkit.getAsyncScheduler().runNow(this, scheduledTask -> checkForUpdates());
78+
HyperUpdater updater = new HyperUpdater(hyperLib);
79+
updater.setGitHub("hyperdefined", "AnarchyStats");
80+
updater.setModrinth("Z04RmeT7");
81+
updater.setHangar("AnarchyStats", "paper");
82+
updater.check();
7183
}
7284

7385
public void updateWorldSize() {
74-
worldSize = WorldSize.readableFileSize(WorldSize.getWorldSize(worldPaths));
86+
worldSize = WorldSize.readableFileSize(WorldSize.getWorldSize(worldPaths, logger));
7587
}
7688

7789
public void loadConfig() {
@@ -82,38 +94,15 @@ public void loadConfig() {
8294
for (String worldFolder : config.getStringList("worlds-to-use")) {
8395
Path currentPath = Paths.get(Paths.get(".").toAbsolutePath().normalize() + File.separator + worldFolder);
8496
if (!currentPath.toFile().exists()) {
85-
logger.warning("World folder \"" + worldFolder + "\" does not exist! Excluding from size calculation.");
97+
logger.warn("World folder \"{}\" does not exist! Excluding from size calculation.", worldFolder);
8698
} else {
87-
logger.info("Adding " + worldFolder);
99+
logger.info("Adding {}", worldFolder);
88100
worldPaths.add(currentPath);
89101
}
90102
}
91103

92104
if (config.getInt("config-version") != CONFIG_VERSION) {
93-
logger.warning("You configuration is out of date! Some features may not work!");
94-
}
95-
}
96-
97-
public void checkForUpdates() {
98-
GitHubReleaseAPI api;
99-
try {
100-
api = new GitHubReleaseAPI("AnarchyStats", "hyperdefined");
101-
} catch (IOException e) {
102-
logger.warning("Unable to check updates!");
103-
e.printStackTrace();
104-
return;
105-
}
106-
GitHubRelease current = api.getReleaseByTag(this.getPluginMeta().getVersion());
107-
GitHubRelease latest = api.getLatestVersion();
108-
if (current == null) {
109-
logger.warning("You are running a version that does not exist on GitHub. If you are in a dev environment, you can ignore this. Otherwise, this is a bug!");
110-
return;
111-
}
112-
int buildsBehind = api.getBuildsBehind(current);
113-
if (buildsBehind == 0) {
114-
logger.info("You are running the latest version.");
115-
} else {
116-
logger.warning("A new version is available (" + latest.getTagVersion() + ")! You are running version " + current.getTagVersion() + ". You are " + buildsBehind + " version(s) behind.");
105+
logger.warn("You configuration is out of date! Some features may not work!");
117106
}
118107
}
119108
}

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

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import lol.hyper.anarchystats.AnarchyStats;
2121
import net.kyori.adventure.text.Component;
22-
import net.kyori.adventure.text.minimessage.MiniMessage;
2322
import org.bukkit.Bukkit;
2423

2524
import java.text.DateFormat;
@@ -42,6 +41,7 @@ public MessageParser(AnarchyStats anarchyStats) {
4241

4342
/**
4443
* Builds the /info command from the config.
44+
*
4545
* @return A full component with the command.
4646
*/
4747
public Component infoCommand() {
@@ -53,12 +53,12 @@ public Component infoCommand() {
5353
try {
5454
originalDate = originalFormat.parse(configDate);
5555
} catch (ParseException e) {
56-
anarchyStats.logger.severe("The date in the config is invalid.");
56+
anarchyStats.logger.warn("The date in the config is invalid.");
5757
e.printStackTrace();
5858
}
5959
String finalDate;
6060
if (configFormat == null) {
61-
anarchyStats.logger.severe("date-format is invalid! Trying to use default formatting for date instead.");
61+
anarchyStats.logger.warn("date-format is invalid! Trying to use default formatting for date instead.");
6262
finalDate = originalFormat.format(originalDate);
6363
} else {
6464
finalFormat = new SimpleDateFormat(configFormat, Locale.ENGLISH);
@@ -67,40 +67,22 @@ public Component infoCommand() {
6767

6868
List<String> rawMessages = anarchyStats.config.getStringList("command-message");
6969
if (rawMessages.isEmpty()) {
70-
anarchyStats.logger.warning("'command-message' is empty on the configuration!");
70+
anarchyStats.logger.warn("'command-message' is empty on the configuration!");
7171
return null;
7272
}
7373

74-
// start with an empty component
75-
Component infoCommand = Component.empty();
7674
for (int i = 0; i < rawMessages.size(); i++) {
7775
String line = rawMessages.get(i);
78-
if (line.contains("{{STARTDATE}}")) {
79-
line = line.replace("{{STARTDATE}}", finalDate);
80-
}
8176

82-
if (line.contains("{{DAYS}}")) {
83-
line = line.replace("{{DAYS}}", Long.toString(getDays()));
84-
}
77+
line = line.replace("{{STARTDATE}}", finalDate);
78+
line = line.replace("{{DAYS}}", Long.toString(getDays()));
79+
line = line.replace("{{WORLDSIZE}}", anarchyStats.worldSize);
80+
line = line.replace("{{TOTALJOINS}}", Integer.toString(Bukkit.getOfflinePlayers().length));
8581

86-
if (line.contains("{{WORLDSIZE}}")) {
87-
line = line.replace("{{WORLDSIZE}}", anarchyStats.worldSize);
88-
}
89-
90-
if (line.contains("{{TOTALJOINS}}")) {
91-
line = line.replace("{{TOTALJOINS}}", Integer.toString(Bukkit.getOfflinePlayers().length));
92-
}
93-
94-
// append a new line + the component
95-
// don't add a new line if it's the first one
96-
// creates a gap
97-
if (i == 0) {
98-
infoCommand = MiniMessage.miniMessage().deserialize(line);
99-
} else {
100-
infoCommand = infoCommand.append(Component.newline()).append(MiniMessage.miniMessage().deserialize(line));
101-
}
82+
rawMessages.set(i, line);
10283
}
103-
return infoCommand;
84+
85+
return anarchyStats.textUtils.formatMultiLine(rawMessages);
10486
}
10587

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

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
package lol.hyper.anarchystats.tools;
1919

20-
import org.bukkit.Bukkit;
20+
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
21+
import org.jetbrains.annotations.NotNull;
2122

2223
import java.io.IOException;
2324
import java.nio.file.FileVisitResult;
@@ -38,34 +39,34 @@ public class WorldSize {
3839
* <p>
3940
* https://stackoverflow.com/a/19877372
4041
*/
41-
public static long getWorldSize(List<Path> paths) {
42+
public static long getWorldSize(List<Path> paths, ComponentLogger logger) {
4243

4344
final AtomicLong size = new AtomicLong(0);
4445

4546
for (Path p : paths) {
4647
try {
47-
Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
48+
Files.walkFileTree(p, new SimpleFileVisitor<>() {
4849
@Override
49-
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
50+
public @NotNull FileVisitResult visitFile(Path file, @NotNull BasicFileAttributes attributes) {
5051

51-
size.addAndGet(attrs.size());
52+
size.addAndGet(attributes.size());
5253
return FileVisitResult.CONTINUE;
5354
}
5455

5556
@Override
56-
public FileVisitResult visitFileFailed(Path file, IOException exc) {
57+
public @NotNull FileVisitResult visitFileFailed(Path file, @NotNull IOException exception) {
5758

58-
Bukkit.getLogger().warning("File " + file + " doesn't exist. (" + exc + ")");
59+
logger.warn("File {} doesn't exist. ({})", file, exception.toString());
5960
// Skip folders that can't be traversed
6061
return FileVisitResult.CONTINUE;
6162
}
6263

6364
@Override
64-
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
65+
public @NotNull FileVisitResult postVisitDirectory(Path dir, IOException exception) {
6566

66-
if (exc != null)
67-
Bukkit.getLogger().warning("Had trouble traversing: " + dir + " (" + exc + ")");
68-
// Ignore errors traversing a folder
67+
if (exception != null) {
68+
logger.warn("Had trouble traversing: {} ({})", dir, exception.toString());
69+
}
6970
return FileVisitResult.CONTINUE;
7071
}
7172
});
@@ -82,7 +83,7 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
8283
*/
8384
public static String readableFileSize(long size) {
8485
if (size <= 0) return "0";
85-
final String[] units = new String[] {"B", "kB", "MB", "GB", "TB"};
86+
final String[] units = new String[]{"B", "kB", "MB", "GB", "TB"};
8687
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
8788
return new DecimalFormat("#,##0.##").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
8889
}

0 commit comments

Comments
 (0)