diff --git a/pom.xml b/pom.xml index 4546602..f88f4eb 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.pequla little-hooks - 1.0 + 1.1 jar LittleHooks @@ -75,7 +75,7 @@ com.pequla little-link - 1.4 + 1.6 provided diff --git a/src/main/java/com/pequla/link/GameHandler.java b/src/main/java/com/pequla/link/GameHandler.java index 0940c8d..c8cbb14 100644 --- a/src/main/java/com/pequla/link/GameHandler.java +++ b/src/main/java/com/pequla/link/GameHandler.java @@ -1,8 +1,6 @@ package com.pequla.link; import com.pequla.link.model.*; -import com.pequla.link.service.DataService; -import org.bukkit.ChatColor; import org.bukkit.Server; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -11,11 +9,9 @@ import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.server.ServerLoadEvent; +import org.bukkit.event.world.WorldLoadEvent; -import java.net.URI; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.List; @@ -29,19 +25,35 @@ public GameHandler(LittleHooks plugin, LittleLink core) { this.core = core; } + @EventHandler + public void onServerLoadEvent(ServerLoadEvent event) { + new Thread(() -> { + if (event.getType() == ServerLoadEvent.LoadType.STARTUP) { + plugin.sendMessage("Server loaded"); + } + }).start(); + } + + @EventHandler + public void onWorldLoadEvent(WorldLoadEvent event) { + if (plugin.getServer().getWorlds().get(0).equals(event.getWorld())) { + plugin.sendMessage("Loading the world"); + } + } + @EventHandler public void handlePlayerJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); DataModel data = core.getPlayerData().get(player.getUniqueId()); Server server = plugin.getServer(); - sendMessage(player, EmbedModel.builder() + plugin.sendMessage(player, EmbedModel.builder() .color(plugin.getConfig().getInt("color.join")) .author(EmbedAuthor.builder() .name(data.getNickname()) .icon_url(data.getAvatar()) .build()) - .description(bold(event.getJoinMessage())) + .description(PluginUtils.bold(event.getJoinMessage())) .fields(List.of(EmbedField.builder() .name("Online:") .value(server.getOnlinePlayers().size() + "/" + server.getMaxPlayers()) @@ -60,13 +72,13 @@ public void handlePlayerQuit(PlayerQuitEvent event) { DataModel data = core.getPlayerData().get(player.getUniqueId()); Server server = plugin.getServer(); - sendMessage(player, EmbedModel.builder() + plugin.sendMessage(player, EmbedModel.builder() .color(plugin.getConfig().getInt("color.leave")) .author(EmbedAuthor.builder() .name(data.getNickname()) .icon_url(data.getAvatar()) .build()) - .description(bold(event.getQuitMessage())) + .description(PluginUtils.bold(event.getQuitMessage())) .fields(List.of(EmbedField.builder() .name("Online:") .value((server.getOnlinePlayers().size() - 1) + "/" + server.getMaxPlayers()) @@ -84,58 +96,17 @@ public void handlePlayerDeath(PlayerDeathEvent event) { Player player = event.getEntity(); DataModel data = core.getPlayerData().get(player.getUniqueId()); - sendMessage(player, EmbedModel.builder() + plugin.sendMessage(player, EmbedModel.builder() .color(plugin.getConfig().getInt("color.death")) .author(EmbedAuthor.builder() .name(data.getNickname()) .icon_url(data.getAvatar()) .build()) - .description(bold(event.getDeathMessage())) + .description(PluginUtils.bold(event.getDeathMessage())) .footer(EmbedFooter.builder() .text(data.getId()) .build()) .timestamp(Instant.now().toString()) .build()); } - - private String getMinecraftAvatarUrl(Player player) { - return "https://visage.surgeplay.com/face/" + player.getUniqueId().toString().replace("-", ""); - } - - private String bold(String str) { - return "**" + ChatColor.stripColor(str) + "**"; - } - - private void sendMessage(Player player, EmbedModel model) { - try { - String url = plugin.getConfig().getString("webhook-url"); - if (url == null) throw new RuntimeException("Webhook URL not found"); - - DataService service = DataService.getInstance(); - String json = service.getMapper().writeValueAsString(MessageModel.builder() - .username(player.getName()) - .avatar_url(getMinecraftAvatarUrl(player)) - .embeds(List.of(model)) - .build()); - - new Thread(()->{ - try { - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(url)) - .header("Content-Type", "application/json") - .POST(HttpRequest.BodyPublishers.ofString(json, StandardCharsets.UTF_8)) - .build(); - - HttpResponse rsp = service.getClient().send(request, HttpResponse.BodyHandlers.ofString()); - if (rsp.statusCode() != 204) - throw new RuntimeException("Server responded with HTTP " + rsp.statusCode()); - } catch (Exception e) { - plugin.getLogger().severe("Webhook could not be sent! " + e.getMessage()); - } - - }).start(); - } catch (Exception e) { - plugin.getLogger().severe("Webhook could not be sent! " + e.getMessage()); - } - } } diff --git a/src/main/java/com/pequla/link/LittleHooks.java b/src/main/java/com/pequla/link/LittleHooks.java index 553adad..6f51048 100644 --- a/src/main/java/com/pequla/link/LittleHooks.java +++ b/src/main/java/com/pequla/link/LittleHooks.java @@ -1,8 +1,18 @@ package com.pequla.link; +import com.pequla.link.model.EmbedModel; +import com.pequla.link.model.MessageModel; +import com.pequla.link.service.DataService; +import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; +import java.net.URI; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.util.List; import java.util.Objects; public final class LittleHooks extends JavaPlugin { @@ -27,6 +37,9 @@ public void onEnable() { return; } + // Starting message + sendMessage("Server starting"); + // Registering the event handler manager.registerEvents(new GameHandler(this, plugin), this); } @@ -34,5 +47,55 @@ public void onEnable() { @Override public void onDisable() { // Plugin shutdown logic + sendMessage("Server stopped"); + } + + public void sendMessage(MessageModel model) { + try { + String url = getConfig().getString("webhook-url"); + if (url == null) throw new RuntimeException("Webhook URL not found"); + + DataService service = DataService.getInstance(); + String json = service.getMapper().writeValueAsString(model); + + new Thread(() -> { + try { + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(url)) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(json, StandardCharsets.UTF_8)) + .build(); + + HttpResponse rsp = service.getClient().send(request, HttpResponse.BodyHandlers.ofString()); + if (rsp.statusCode() != 204) + throw new RuntimeException("Server responded with HTTP " + rsp.statusCode()); + } catch (Exception e) { + getLogger().severe("Webhook could not be sent! " + e.getMessage()); + } + + }).start(); + } catch (Exception e) { + getLogger().severe("Webhook could not be sent! " + e.getMessage()); + } + } + + public void sendMessage(Player player, EmbedModel model) { + sendMessage(MessageModel.builder() + .username(player.getName()) + .avatar_url("https://visage.surgeplay.com/face/" + PluginUtils.cleanUUID(player.getUniqueId())) + .embeds(List.of(model)) + .build() + ); + } + + public void sendMessage(String content) { + sendMessage(MessageModel.builder() + .embeds(List.of(EmbedModel.builder() + .color(getConfig().getInt("color.system")) + .description(PluginUtils.bold(content)) + .timestamp(Instant.now().toString()) + .build())) + .build() + ); } } diff --git a/src/main/java/com/pequla/link/PluginUtils.java b/src/main/java/com/pequla/link/PluginUtils.java new file mode 100644 index 0000000..235ee6d --- /dev/null +++ b/src/main/java/com/pequla/link/PluginUtils.java @@ -0,0 +1,16 @@ +package com.pequla.link; + +import org.bukkit.ChatColor; + +import java.util.UUID; + +public class PluginUtils { + + public static String bold(String input) { + return "**" + ChatColor.stripColor(input) + "**"; + } + + public static String cleanUUID(UUID uuid) { + return uuid.toString().replace("-", ""); + } +}