First build

This commit is contained in:
minster586
2025-06-19 18:48:53 -04:00
parent 7001852de5
commit 8016810631
10 changed files with 341 additions and 2 deletions

View File

@@ -0,0 +1,23 @@
package com.smartcraft.notifier;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerAdvancementDoneEvent;
public class AdvancementListener implements Listener {
@EventHandler
public void onAdvancement(PlayerAdvancementDoneEvent event) {
String advancementKey = event.getAdvancement().getKey().getKey();
String playerName = event.getPlayer().getName();
// Filter out root advancements or hidden criteria
if (advancementKey.contains("root") || advancementKey.contains("recipes")) return;
SmartCraftNotifier.instance.getGotifyClient().sendMessage(
"📜 Advancement Unlocked!",
playerName + " has earned: " + advancementKey,
5
);
}
}

View File

@@ -0,0 +1,50 @@
package com.smartcraft.notifier;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
public class BungeeMessageListener implements PluginMessageListener {
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (!channel.equals("BungeeCord")) return;
try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(message))) {
String subChannel = in.readUTF();
if (subChannel.equals("PlayerJoin")) {
String joinedPlayer = in.readUTF();
SmartCraftNotifier.instance.getGotifyClient().sendMessage(
"👤 Player Joined",
joinedPlayer + " connected to the network.",
4
);
} else if (subChannel.equals("PlayerQuit")) {
String leftPlayer = in.readUTF();
SmartCraftNotifier.instance.getGotifyClient().sendMessage(
"🚪 Player Left",
leftPlayer + " disconnected from the network.",
4
);
} else if (subChannel.equals("ServerSwitch")) {
String playerName = in.readUTF();
String fromServer = in.readUTF();
String toServer = in.readUTF();
SmartCraftNotifier.instance.getGotifyClient().sendMessage(
"🔁 Server Switch",
playerName + " switched from " + fromServer + " to " + toServer,
3
);
}
} catch (IOException e) {
Bukkit.getLogger().warning("[SmartCraftNotifier] Failed to parse BungeeCord message: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,75 @@
package com.smartcraft.notifier;
import okhttp3.*;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import java.io.IOException;
public class GotifyClient {
private final String serverUrl;
private final String appToken;
private final boolean enabled;
private final OkHttpClient http;
public GotifyClient(FileConfiguration config) {
this.enabled = config.getBoolean("gotify.enabled", false);
this.serverUrl = config.getString("gotify.serverUrl", "").trim();
this.appToken = config.getString("gotify.appToken", "").trim();
this.http = new OkHttpClient();
}
public boolean isReady() {
return enabled && !serverUrl.isEmpty() && !appToken.isEmpty();
}
public void sendMessage(String title, String message, int priority) {
if (!isReady()) return;
HttpUrl url = HttpUrl.parse(serverUrl + "/message");
if (url == null) return;
RequestBody body = new FormBody.Builder()
.add("title", title)
.add("message", message)
.add("priority", String.valueOf(priority))
.build();
Request request = new Request.Builder()
.url(url)
.addHeader("X-Gotify-Key", appToken)
.post(body)
.build();
http.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Bukkit.getLogger().warning("[SmartCraftNotifier] Failed to send Gotify message: " + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) {
response.close();
}
});
}
public boolean pingServer() {
if (!isReady()) return false;
HttpUrl url = HttpUrl.parse(serverUrl + "/application");
if (url == null) return false;
Request request = new Request.Builder()
.url(url)
.addHeader("X-Gotify-Key", appToken)
.build();
try (Response response = http.newCall(request).execute()) {
return response.isSuccessful();
} catch (IOException e) {
return false;
}
}
}

View File

@@ -0,0 +1,59 @@
package com.smartcraft.notifier;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.messaging.Messenger;
public class SmartCraftNotifier extends JavaPlugin {
public static SmartCraftNotifier instance;
private GotifyClient gotifyClient;
@Override
public void onEnable() {
instance = this;
saveDefaultConfig();
FileConfiguration config = getConfig();
this.gotifyClient = new GotifyClient(config);
if (!gotifyClient.isReady()) {
getLogger().warning("Gotify is not properly configured. Notifications will not be sent.");
} else {
getLogger().info("Gotify is ready to send messages.");
}
// Event listeners
if (config.getBoolean("events.logAdvancements", true)) {
getServer().getPluginManager().registerEvents(new AdvancementListener(), this);
}
// BungeeCord plugin messaging setup
if (config.getBoolean("bungeecord", false)) {
Messenger messenger = getServer().getMessenger();
messenger.registerOutgoingPluginChannel(this, "BungeeCord");
messenger.registerIncomingPluginChannel(this, "BungeeCord", new BungeeMessageListener());
getLogger().info("BungeeCord messaging enabled.");
}
// Register command
getCommand("gotifystatus").setExecutor((sender, command, label, args) -> {
boolean result = gotifyClient.pingServer();
sender.sendMessage("Gotify status: " + (result ? "✅ Online" : "❌ Unreachable"));
return true;
});
getLogger().info("SmartCraft Notifier has been enabled.");
}
@Override
public void onDisable() {
getLogger().info("SmartCraft Notifier has been disabled.");
}
public GotifyClient getGotifyClient() {
return gotifyClient;
}
}

View File

@@ -0,0 +1,12 @@
bungeecord: false
gotify:
enabled: true
serverUrl: "https://your.gotify.server"
appToken: "YOUR_SECRET_TOKEN"
events:
logJoins: true
logLeaves: true
logServerSwitch: true
logAdvancements: true

View File

@@ -0,0 +1,14 @@
name: SmartCraftNotifier
version: 1.0.0
main: com.smartcraft.notifier.SmartCraftNotifier
api-version: 1.20
description: Sends player events to Gotify and supports optional BungeeCord messaging.
commands:
gotifystatus:
description: Check if Gotify is reachable.
usage: /gotifystatus
permission: smartcraftnotifier.status
permissions:
smartcraftnotifier.status:
description: Allows use of /gotifystatus command
default: op