tweaks724/src/main/java/eu/m724/tweaks/updater/UpdaterCommands.java
2024-12-31 13:34:17 +01:00

81 lines
3.2 KiB
Java

/*
* Copyright (C) 2024 Minecon724
* Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
* in the project root for the full license text.
*/
package eu.m724.tweaks.updater;
import eu.m724.tweaks.Language;
import eu.m724.tweaks.updater.cache.VersionedResource;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.hover.content.Text;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.time.format.DateTimeFormatter;
public class UpdaterCommands implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (UpdateChecker.lastChecked == null) {
sender.sendMessage(Language.getString("updatesNotChecked"));
return true;
}
String lastChecked = UpdateChecker.lastChecked.format(DateTimeFormatter.ofPattern("HH:mm"));
int n = UpdateChecker.availableUpdates.size();
if (n > 0) {
sender.spigot().sendMessage(
Language.getComponent("updateAvailableNotice", ChatColor.GRAY, n)
);
int i = 0;
for (VersionedResource v : UpdateChecker.availableUpdates) {
sender.spigot().sendMessage(
new ComponentBuilder(++i + ". ").color(ChatColor.GRAY).build(), resourceToBaseComponent(v)
);
}
} else {
sender.spigot().sendMessage(Language.getComponent("updatesNoUpdates", ChatColor.GREEN, lastChecked));
}
return true;
}
private BaseComponent resourceToBaseComponent(VersionedResource v) {
Color nameColor = Color.decode("#" + Integer.toHexString(v.resource().name().hashCode()).substring(0, 6));
BaseComponent component = new ComponentBuilder(v.resource().name()).color(ChatColor.of(nameColor))
.append(" (").color(ChatColor.DARK_GRAY)
.append(v.running().label()).color(ChatColor.GRAY)
.append(" -> ").color(ChatColor.DARK_GRAY)
.append(v.latest().label()).color(ChatColor.of(nameColor.brighter()))
.append(")").color(ChatColor.DARK_GRAY)
.build();
component = new ComponentBuilder(component)
.event(
new ClickEvent(
ClickEvent.Action.OPEN_URL,
"https://www.spigotmc.org/resources/%d/update?update=%d".formatted(v.resource().resourceId(), v.latest().updateId())
)
)
.event(
new HoverEvent(
HoverEvent.Action.SHOW_TEXT,
new Text(Language.getString("updatesClickToOpen", v.latest().description().title()))
)
)
.build();
return component;
}
}