Skip to content

Commit

Permalink
Add a command and API endpoint to see the game date and time (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
zefir-git authored May 25, 2024
2 parents b7c778e + 8458e92 commit eb0f7ac
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/pro/cloudnode/smp/smpcore/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.TimeZone;
import java.util.stream.Collectors;

public class Messages extends BaseConfig {
Expand Down Expand Up @@ -170,6 +172,16 @@ public Messages() {
.toLocalDateTime()), Placeholder.component("last-seen-relative", SMPCore.relativeTime(lastSeen)));
}

public @NotNull Component time(final @NotNull Date date) {
final @NotNull Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.setTime(date);
final int day = calendar.get(Calendar.DAY_OF_MONTH);
return MiniMessage.miniMessage()
.deserialize(Objects.requireNonNull(config.getString("time")), Formatter.date("date", calendar
.toInstant().atZone(ZoneOffset.UTC)
.toLocalDateTime()), Placeholder.unparsed("day", String.valueOf(day)), Placeholder.unparsed("day", String.valueOf(day)), Formatter.choice("day-format", day));
}

// errors

public @NotNull Component errorNoPermission() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/pro/cloudnode/smp/smpcore/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public final class Permission {
* Allow using `/seen` on staff
*/
public static @NotNull String SEEN_STAFF = "smpcore.seen.staff";

/**
* Allow seeing the game time and date
*/
public static @NotNull String TIME = "smpcore.time";
}
6 changes: 6 additions & 0 deletions src/main/java/pro/cloudnode/smp/smpcore/Rest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public Rest(final int port) {
ctx.header("Access-Control-Max-Age", "3600");
});

javalin.get("/", ctx -> {
final @NotNull JsonObject obj = new JsonObject();
obj.addProperty("time", SMPCore.gameTime().getTime());
ctx.json(obj);
});

javalin.get("/members", ctx -> {
final @Nullable String filter = ctx.queryParam("filter");
final @Nullable String limitString = ctx.queryParam("limit");
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/pro/cloudnode/smp/smpcore/SMPCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import net.kyori.adventure.text.Component;
import org.bukkit.World;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -11,6 +12,7 @@
import pro.cloudnode.smp.smpcore.command.Command;
import pro.cloudnode.smp.smpcore.command.MainCommand;
import pro.cloudnode.smp.smpcore.command.SeenCommand;
import pro.cloudnode.smp.smpcore.command.TimeCommand;
import pro.cloudnode.smp.smpcore.command.UnbanCommand;
import pro.cloudnode.smp.smpcore.listener.NationTeamUpdaterListener;

Expand Down Expand Up @@ -73,6 +75,7 @@ public void onEnable() {
put("ban", new BanCommand());
put("unban", new UnbanCommand());
put("seen", new SeenCommand());
put("time", new TimeCommand());
}};
commands.put("alts", new AltsCommand(commands.get("smpcore")));
for (final @NotNull Map.Entry<@NotNull String, @NotNull Command> entry : commands.entrySet())
Expand Down Expand Up @@ -192,4 +195,13 @@ public static boolean ifDisallowedCharacters(final @NotNull String source, final
public static @NotNull Component relativeTime(final @NotNull Date date) {
return relativeTime(date, new Date());
}

private static @NotNull World overworld() {
return Objects.requireNonNull(getInstance().getServer().getWorlds().stream()
.filter(w -> w.getEnvironment() == World.Environment.NORMAL).findFirst().orElse(null));
}

public static @NotNull Date gameTime() {
return new Date(overworld().getGameTime() * 72);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public boolean run(@NotNull CommandSender sender, @NotNull String label, @NotNul
return switch (args[0]) {
case "reload" -> reload(sender);
case "alt" -> alt(sender, argsSubset, label + " " + args[0]);
case "time", "date" -> time(sender, argsSubset, label + " " + args[0]);
default -> sendMessage(sender, MiniMessage.miniMessage()
.deserialize("<red>(!) Unrecognised command <gray><command>", Placeholder.unparsed("command", args[0])));
};
Expand All @@ -42,6 +43,7 @@ public boolean run(@NotNull CommandSender sender, @NotNull String label, @NotNul
if (args.length == 1) {
if (sender.hasPermission(Permission.RELOAD)) suggestions.add("reload");
if (sender.hasPermission(Permission.ALT)) suggestions.add("alt");
if (sender.hasPermission(Permission.TIME)) suggestions.addAll(List.of("time", "date"));
}
else if (args.length > 1) switch (args[0]) {
case "alt" -> {
Expand Down Expand Up @@ -234,4 +236,10 @@ else switch (originalArgs[0]) {
}
}
}

public static boolean time(final @NotNull CommandSender sender, final @NotNull String @NotNull [] args, final @NotNull String label) {
if (!sender.hasPermission(Permission.TIME))
return sendMessage(sender, SMPCore.messages().errorNoPermission());
return sendMessage(sender, SMPCore.messages().time(SMPCore.gameTime()));
}
}
19 changes: 19 additions & 0 deletions src/main/java/pro/cloudnode/smp/smpcore/command/TimeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pro.cloudnode.smp.smpcore.command;

import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public final class TimeCommand extends Command {

@Override
public boolean run(@NotNull CommandSender sender, @NotNull String label, @NotNull String @NotNull [] args) {
return MainCommand.time(sender, args, label);
}

@Override
public @NotNull List<@NotNull String> tab(@NotNull CommandSender sender, @NotNull String label, @NotNull String @NotNull [] args) {
return List.of();
}
}
2 changes: 2 additions & 0 deletions src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ seen:
inactive: <aqua>(!) Member <white><player></white> is <red>inactive</red> and last seen on <white><last-seen:'dd MMM yyyy HH:mm'> UTC</white> <gray>(<last-seen-relative>)</aqua>
non-member: <aqua>(!) Player <white><player></white> was last seen on <white><last-seen:'dd MMM yyyy HH:mm'> UTC</white> <gray>(<last-seen-relative>)</aqua>

time: <green>(!) Today is <white><date:'EEEE, MMMM'> <day><day-format:'1#st|2#nd|3#rd|3<th|21#st|22#nd|23#rd|23<th|31#st'> <date:'yyyy'></white> and the time is <white><date:'HH:mm'> UTC</white>.</green>

error:
no-permission: <red>(!) You don't have permission to use this command.</red>
player-not-banned: <red>(!) Player <gray><player></gray> is not banned and is not a member.</red>
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ commands:
description: Check when a player was last online
usage: /<command> <player>
aliases: [ lastseen ]
time:
permission: smpcore.time
description: See the current game time and date
usage: /<command>
aliases: [ date, today ]

0 comments on commit eb0f7ac

Please sign in to comment.