master upload
This commit is contained in:
41
src/main/java/com/minster586/ntfyplugin/CommandMonitor.java
Normal file
41
src/main/java/com/minster586/ntfyplugin/CommandMonitor.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package com.minster586.ntfyplugin;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CommandMonitor implements Listener {
|
||||
|
||||
private final FileConfiguration config;
|
||||
private final MessageManager messageManager;
|
||||
private final NTFYNotifier notifier;
|
||||
|
||||
public CommandMonitor(Main plugin) {
|
||||
this.config = plugin.getConfig();
|
||||
this.messageManager = new MessageManager(plugin);
|
||||
this.notifier = new NTFYNotifier(plugin);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
|
||||
if (!config.getBoolean("notifications.commands.enabled", true)) return;
|
||||
|
||||
String commandLine = event.getMessage().toLowerCase().trim();
|
||||
CommandSender sender = event.getPlayer();
|
||||
|
||||
List<String> watched = config.getStringList("notifications.commands.watch");
|
||||
for (String keyword : watched) {
|
||||
if (commandLine.startsWith("/" + keyword.toLowerCase())) {
|
||||
String title = messageManager.getMessage(keyword + ".title");
|
||||
String message = messageManager.formatMessage(keyword + ".message", (sender instanceof Player) ? (Player) sender : null);
|
||||
notifier.sendNotification(title, message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
41
src/main/java/com/minster586/ntfyplugin/Main.java
Normal file
41
src/main/java/com/minster586/ntfyplugin/Main.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package com.minster586.ntfyplugin;
|
||||
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class Main extends JavaPlugin {
|
||||
|
||||
private static Main instance;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
instance = this;
|
||||
|
||||
// Save default config.yml (Bukkit handles this automatically)
|
||||
saveDefaultConfig();
|
||||
|
||||
// Save msg.yml if it doesn't exist
|
||||
File msgFile = new File(getDataFolder(), "msg.yml");
|
||||
if (!msgFile.exists()) {
|
||||
saveResource("msg.yml", false);
|
||||
}
|
||||
|
||||
// Register listeners
|
||||
PluginManager pm = getServer().getPluginManager();
|
||||
pm.registerEvents(new PlayerEventListener(this), this);
|
||||
pm.registerEvents(new CommandMonitor(this), this);
|
||||
|
||||
getLogger().info("NTFYNotifier has been enabled.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
getLogger().info("NTFYNotifier has been disabled.");
|
||||
}
|
||||
|
||||
public static Main getInstance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
35
src/main/java/com/minster586/ntfyplugin/MessageManager.java
Normal file
35
src/main/java/com/minster586/ntfyplugin/MessageManager.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.minster586.ntfyplugin;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class MessageManager {
|
||||
|
||||
private final Main plugin;
|
||||
private final YamlConfiguration messages;
|
||||
|
||||
public MessageManager(Main plugin) {
|
||||
this.plugin = plugin;
|
||||
File msgFile = new File(plugin.getDataFolder(), "msg.yml");
|
||||
this.messages = YamlConfiguration.loadConfiguration(msgFile);
|
||||
}
|
||||
|
||||
public String getMessage(String path) {
|
||||
return messages.getString("messages." + path, "");
|
||||
}
|
||||
|
||||
public String formatMessage(String path, Player player) {
|
||||
String message = getMessage(path);
|
||||
|
||||
if (player != null) {
|
||||
message = message.replace("%player%", player.getName());
|
||||
}
|
||||
|
||||
String serverName = plugin.getConfig().getString("server-name", "Minecraft Server");
|
||||
message = message.replace("%server%", serverName);
|
||||
|
||||
return message;
|
||||
}
|
||||
}
|
56
src/main/java/com/minster586/ntfyplugin/NTFYNotifier.java
Normal file
56
src/main/java/com/minster586/ntfyplugin/NTFYNotifier.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package com.minster586.ntfyplugin;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
public class NTFYNotifier {
|
||||
|
||||
private final FileConfiguration config;
|
||||
|
||||
public NTFYNotifier(Main plugin) {
|
||||
this.config = plugin.getConfig();
|
||||
}
|
||||
|
||||
public void sendNotification(String title, String message) {
|
||||
try {
|
||||
String server = config.getString("ntfy.server", "https://ntfy.sh");
|
||||
String topic = config.getString("ntfy.topic", "minecraft-events");
|
||||
|
||||
String urlString = server.endsWith("/") ? server + topic : server + "/" + topic;
|
||||
URL url = new URL(urlString);
|
||||
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
|
||||
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setDoOutput(true);
|
||||
conn.setRequestProperty("Title", title);
|
||||
conn.setRequestProperty("Content-Type", "text/plain");
|
||||
|
||||
// Optional Basic Auth
|
||||
if (config.getBoolean("ntfy.auth.enabled", false)) {
|
||||
String username = config.getString("ntfy.auth.username");
|
||||
String password = config.getString("ntfy.auth.password");
|
||||
if (username != null && password != null) {
|
||||
String auth = username + ":" + password;
|
||||
String encoded = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
|
||||
conn.setRequestProperty("Authorization", "Basic " + encoded);
|
||||
}
|
||||
}
|
||||
|
||||
// Send body
|
||||
try (OutputStream os = conn.getOutputStream()) {
|
||||
byte[] input = message.getBytes(StandardCharsets.UTF_8);
|
||||
os.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
// Close connection to complete POST
|
||||
conn.getInputStream().close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
package com.minster586.ntfyplugin;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
public class PlayerEventListener implements Listener {
|
||||
|
||||
private final Main plugin;
|
||||
private final FileConfiguration config;
|
||||
private final MessageManager messageManager;
|
||||
private final NTFYNotifier notifier;
|
||||
|
||||
public PlayerEventListener(Main plugin) {
|
||||
this.plugin = plugin;
|
||||
this.config = plugin.getConfig();
|
||||
this.messageManager = new MessageManager(plugin);
|
||||
this.notifier = new NTFYNotifier(plugin);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
if (config.getBoolean("notifications.join", true)) {
|
||||
Player player = event.getPlayer();
|
||||
String title = messageManager.getMessage("join.title");
|
||||
String message = messageManager.formatMessage("join.message", player);
|
||||
notifier.sendNotification(title, message);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
if (config.getBoolean("notifications.quit", true)) {
|
||||
Player player = event.getPlayer();
|
||||
String title = messageManager.getMessage("quit.title");
|
||||
String message = messageManager.formatMessage("quit.message", player);
|
||||
notifier.sendNotification(title, message);
|
||||
}
|
||||
}
|
||||
}
|
22
src/main/resources/config.yml
Normal file
22
src/main/resources/config.yml
Normal file
@@ -0,0 +1,22 @@
|
||||
ntfy:
|
||||
server: "https://ntfy.sh" # Default public ntfy.sh server
|
||||
topic: "minecraft-events"
|
||||
auth:
|
||||
enabled: false
|
||||
username: "user"
|
||||
password: "pass"
|
||||
|
||||
server-name: "NTFYCraft" # Custom variable used in message placeholders
|
||||
|
||||
notifications:
|
||||
join: true
|
||||
quit: true
|
||||
commands:
|
||||
enabled: true
|
||||
watch:
|
||||
- "stop"
|
||||
- "ban"
|
||||
- "kick"
|
||||
- "mute"
|
||||
- "warn"
|
||||
- "op"
|
25
src/main/resources/msg.yml
Normal file
25
src/main/resources/msg.yml
Normal file
@@ -0,0 +1,25 @@
|
||||
messages:
|
||||
join:
|
||||
title: "Player Joined"
|
||||
message: "%player% has joined the server."
|
||||
quit:
|
||||
title: "Player Left"
|
||||
message: "%player% has left the server."
|
||||
stop:
|
||||
title: "Server Stop"
|
||||
message: "%player% issued /stop"
|
||||
ban:
|
||||
title: "Player Banned"
|
||||
message: "%player% used /ban"
|
||||
kick:
|
||||
title: "Player Kicked"
|
||||
message: "%player% used /kick"
|
||||
mute:
|
||||
title: "Player Muted"
|
||||
message: "%player% used /mute"
|
||||
warn:
|
||||
title: "Player Warned"
|
||||
message: "%player% used /warn"
|
||||
op:
|
||||
title: "Player Opped"
|
||||
message: "%player% used /op"
|
8
src/main/resources/plugin.yml
Normal file
8
src/main/resources/plugin.yml
Normal file
@@ -0,0 +1,8 @@
|
||||
name: NTFYNotifier
|
||||
version: 1.0.0
|
||||
main: com.minster586.ntfyplugin.Main
|
||||
api-version: 1.14
|
||||
author: minster586
|
||||
description: Sends customizable join, quit, and command notifications to an ntfy.sh server.
|
||||
commands: {}
|
||||
permissions: {}
|
Reference in New Issue
Block a user