Skip to content

Commit

Permalink
feat: updater checker and update notify on join
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyAreaBean committed Jan 9, 2024
1 parent 9e069b5 commit 7097311
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/main/java/cc/happyareabean/sjm/SimpleJoinMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cc.happyareabean.sjm.commands.SJMCommand;
import cc.happyareabean.sjm.config.SJMConfig;
import cc.happyareabean.sjm.listener.PlayerJoinListener;
import cc.happyareabean.sjm.listener.UpdateNotifyListener;
import cc.happyareabean.sjm.utils.AdventureWebEditorAPI;
import cc.happyareabean.sjm.utils.Constants;
import lombok.Getter;
Expand All @@ -11,13 +12,20 @@
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bstats.bukkit.Metrics;
import org.bukkit.plugin.java.JavaPlugin;
import org.inventivetalent.update.spiget.SpigetUpdate;
import org.inventivetalent.update.spiget.UpdateCallback;
import org.inventivetalent.update.spiget.comparator.VersionComparator;
import revxrsal.commands.bukkit.BukkitCommandHandler;

import java.io.File;
import java.net.URI;
import java.util.Arrays;

public class SimpleJoinMessage extends JavaPlugin {

public static String NEXT_VERSION = "";
public static String DOWNLOAD_URL = "https://www.spigotmc.org/resources/103413/";

@Getter private static SimpleJoinMessage instance;
@Getter public static final MiniMessage MINIMESSAGE = MiniMessage.miniMessage();
@Getter public static final LegacyComponentSerializer LEGACY_SERIALIZER = LegacyComponentSerializer.legacy('&');
Expand All @@ -36,6 +44,7 @@ public void onEnable() {

getLogger().info("Registering listener...");
getServer().getPluginManager().registerEvents(new PlayerJoinListener(), this);
getServer().getPluginManager().registerEvents(new UpdateNotifyListener(), this);

getLogger().info("Registering commands...");
commandHandler = BukkitCommandHandler.create(this);
Expand Down Expand Up @@ -63,6 +72,48 @@ public void onEnable() {
new Metrics(this, 15462);

getLogger().info("SimpleJoinMessage version " + getDescription().getVersion() + " has been successfully enabled!");

checkUpdate();
}

public void checkUpdate() {
String version = getDescription().getVersion();
if (version.endsWith("-SNAPSHOT")) {
Arrays.asList(
"******************************************",
"You are currently using development build of SimpleJoinMessage!",
"Please report issues here: https://go.happyareabean.cc/sjm/issues",
"******************************************"
).forEach(s -> getLogger().warning(s));
return;
}

SpigetUpdate updater = new SpigetUpdate(this, 103413);
updater.setVersionComparator(VersionComparator.SEM_VER);
updater.checkForUpdate(new UpdateCallback() {
@Override
public void updateAvailable(String newVersion, String downloadUrl, boolean hasDirectDownload) {
NEXT_VERSION = newVersion;

Arrays.asList(
"******************************************",
"",
"There is a new version of SimpleJoinMessage available!",
"",
"Your Version: " + version,
"New Version: " + NEXT_VERSION,
"",
"Download at " + downloadUrl,
"",
"******************************************"
).forEach(s -> getLogger().warning(s));
}

@Override
public void upToDate() {
getLogger().info(String.format("SimpleJoinMessage is up to date! (%s)", version));
}
});
}

public void checkSupportedPlugin() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cc.happyareabean.sjm.listener;

import cc.happyareabean.sjm.SimpleJoinMessage;
import cc.happyareabean.sjm.utils.Constants;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import revxrsal.commands.annotation.Named;

import static net.kyori.adventure.text.Component.newline;
import static net.kyori.adventure.text.Component.space;
import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.format.NamedTextColor.WHITE;

public class UpdateNotifyListener implements Listener {

@EventHandler
public void onJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
if (!player.hasPermission("sjm.admin")) return;
if (SimpleJoinMessage.NEXT_VERSION.isEmpty()) return;

Audience aPlayer = SimpleJoinMessage.getAdventure().player(player);
aPlayer.sendMessage(Component.textOfChildren(
Constants.PREFIX,
text("New version", WHITE),
space(),
text(SimpleJoinMessage.NEXT_VERSION, NamedTextColor.YELLOW, TextDecoration.BOLD),
space(),
text("is available!", WHITE),
newline(),
Constants.PREFIX,
text("You are currently running"),
space(),
text(SimpleJoinMessage.getInstance().getDescription().getVersion()),
text("."),
newline(),
Constants.PREFIX,
text("Click here to download update!", NamedTextColor.GOLD, TextDecoration.BOLD)
.clickEvent(ClickEvent.openUrl(SimpleJoinMessage.DOWNLOAD_URL))
).color(NamedTextColor.GRAY));

}

}

0 comments on commit 7097311

Please sign in to comment.