Skip to content

Commit

Permalink
Add server start/stop/loading messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Pequla committed Sep 17, 2022
1 parent 41dac02 commit 9933779
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 55 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.pequla</groupId>
<artifactId>little-hooks</artifactId>
<version>1.0</version>
<version>1.1</version>
<packaging>jar</packaging>

<name>LittleHooks</name>
Expand Down Expand Up @@ -75,7 +75,7 @@
<dependency>
<groupId>com.pequla</groupId>
<artifactId>little-link</artifactId>
<version>1.4</version>
<version>1.6</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
77 changes: 24 additions & 53 deletions src/main/java/com/pequla/link/GameHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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<String> 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());
}
}
}
63 changes: 63 additions & 0 deletions src/main/java/com/pequla/link/LittleHooks.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -27,12 +37,65 @@ public void onEnable() {
return;
}

// Starting message
sendMessage("Server starting");

// Registering the event handler
manager.registerEvents(new GameHandler(this, plugin), this);
}

@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<String> 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()
);
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/pequla/link/PluginUtils.java
Original file line number Diff line number Diff line change
@@ -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("-", "");
}
}

0 comments on commit 9933779

Please sign in to comment.