Init
This commit is contained in:
6
src/main/java/systems/brn/gotifymc/ConfigObject.java
Normal file
6
src/main/java/systems/brn/gotifymc/ConfigObject.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package systems.brn.gotifymc;
|
||||
|
||||
public class ConfigObject {
|
||||
public String instanceURL = "https://gotify.example.com/message";
|
||||
public String token = "YOUR-TOKEN-HERE";
|
||||
}
|
46
src/main/java/systems/brn/gotifymc/GotifyAPI.java
Normal file
46
src/main/java/systems/brn/gotifymc/GotifyAPI.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package systems.brn.gotifymc;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.Objects;
|
||||
|
||||
import static systems.brn.gotifymc.GotifyMC.*;
|
||||
|
||||
public class GotifyAPI {
|
||||
public static void sendNotification(String title, String message, int priority) {
|
||||
if (!Objects.equals(configObject.token, "YOUR-TOKEN-HERE")) {
|
||||
try {
|
||||
URL instanceURL = URI.create(configObject.instanceURL).toURL();
|
||||
HttpURLConnection connection = getHttpURLConnection(instanceURL);
|
||||
connection.connect();
|
||||
GotifyMessage gotifyMessage = new GotifyMessage(title, message, priority);
|
||||
mapper.writeValue(connection.getOutputStream(), gotifyMessage);
|
||||
// Get the response code
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode != 200) {
|
||||
modLogger.warn("Failed to send notification. Response code: {}", responseCode);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
modLogger.error("Failed to create instance URL", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static @NotNull HttpURLConnection getHttpURLConnection(URL instanceURL) throws IOException {
|
||||
HttpURLConnection connection = (HttpURLConnection) instanceURL.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
|
||||
connection.setRequestProperty("Content-Type", "application/json; utf-8");
|
||||
connection.setRequestProperty("Accept", "application/json");
|
||||
|
||||
connection.setRequestProperty("X-Gotify-Key", configObject.token);
|
||||
connection.setDoOutput(true);
|
||||
connection.setDoInput(true);
|
||||
connection.setUseCaches(false);
|
||||
return connection;
|
||||
}
|
||||
}
|
53
src/main/java/systems/brn/gotifymc/GotifyMC.java
Normal file
53
src/main/java/systems/brn/gotifymc/GotifyMC.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package systems.brn.gotifymc;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class GotifyMC implements ModInitializer {
|
||||
|
||||
public static final String MOD_ID = "gotifymc";
|
||||
public static final File CONFIG_FILE = new File("config/gotify.json");
|
||||
public static final Logger modLogger = LoggerFactory.getLogger(MOD_ID);
|
||||
public static final ObjectMapper mapper = new ObjectMapper();
|
||||
public static ConfigObject configObject = new ConfigObject();
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
if (CONFIG_FILE.exists()) {
|
||||
try {
|
||||
configObject = mapper.readValue(CONFIG_FILE, ConfigObject.class);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ServerLifecycleEvents.SERVER_STARTING.register(NotificationEvent::serverStarting);
|
||||
ServerLifecycleEvents.SERVER_STARTED.register(NotificationEvent::serverStarted);
|
||||
ServerLifecycleEvents.SERVER_STOPPING.register(NotificationEvent::serverStopping);
|
||||
ServerLifecycleEvents.SERVER_STOPPED.register(NotificationEvent::serverStopped);
|
||||
|
||||
ServerPlayConnectionEvents.JOIN.register(NotificationEvent::connect);
|
||||
|
||||
ServerPlayerEvents.AFTER_RESPAWN.register(NotificationEvent::afterRespawn);
|
||||
|
||||
ServerMessageEvents.CHAT_MESSAGE.register(NotificationEvent::chatMessage);
|
||||
ServerMessageEvents.GAME_MESSAGE.register(NotificationEvent::gameMessage);
|
||||
|
||||
} else {
|
||||
try {
|
||||
mapper.writeValue(CONFIG_FILE, configObject);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
13
src/main/java/systems/brn/gotifymc/GotifyMessage.java
Normal file
13
src/main/java/systems/brn/gotifymc/GotifyMessage.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package systems.brn.gotifymc;
|
||||
|
||||
public class GotifyMessage {
|
||||
public final String title;
|
||||
public final String message;
|
||||
public final int priority;
|
||||
|
||||
public GotifyMessage(String title, String message, int priority) {
|
||||
this.title = title;
|
||||
this.message = message;
|
||||
this.priority = priority;
|
||||
}
|
||||
}
|
54
src/main/java/systems/brn/gotifymc/NotificationEvent.java
Normal file
54
src/main/java/systems/brn/gotifymc/NotificationEvent.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package systems.brn.gotifymc;
|
||||
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketSender;
|
||||
import net.minecraft.network.message.MessageType;
|
||||
import net.minecraft.network.message.SignedMessage;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.network.ServerPlayNetworkHandler;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.stat.Stats;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
import static systems.brn.gotifymc.GotifyAPI.sendNotification;
|
||||
|
||||
public class NotificationEvent {
|
||||
public static void serverStarting(MinecraftServer server) {
|
||||
sendNotification("Startup", "The server is starting", 5);
|
||||
}
|
||||
|
||||
public static void serverStarted(MinecraftServer server) {
|
||||
sendNotification("Started", "The server is started", 5);
|
||||
}
|
||||
|
||||
public static void serverStopping(MinecraftServer server) {
|
||||
sendNotification("Stopping", "The server is stopping", 5);
|
||||
}
|
||||
|
||||
public static void serverStopped(MinecraftServer server) {
|
||||
sendNotification("Stopped", "The server is stopped", 5);
|
||||
}
|
||||
|
||||
public static void connect(ServerPlayNetworkHandler serverPlayNetworkHandler, PacketSender packetSender, MinecraftServer server) {
|
||||
if (packetSender instanceof ServerPlayerEntity player) {
|
||||
if (player.getStatHandler().getStat(Stats.CUSTOM, Stats.LEAVE_GAME) == 0) {
|
||||
sendNotification("NEW player connecting", "A %s is connecting from %s".formatted(player.getDisplayName(), player.getIp()), 6);
|
||||
} else {
|
||||
sendNotification("Player connecting", "A %s is connecting from %s".formatted(player.getDisplayName(), player.getIp()), 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void afterRespawn(ServerPlayerEntity oldPlayer, ServerPlayerEntity newPlayer, boolean alive) {
|
||||
sendNotification("Respawning", "%s died".formatted(oldPlayer.getDisplayName()), 2);
|
||||
}
|
||||
|
||||
public static void chatMessage(SignedMessage signedMessage, ServerPlayerEntity player, MessageType.Parameters parameters) {
|
||||
sendNotification("Chat message", "<%s> %s".formatted(player.getDisplayName(), signedMessage.getContent().getString()), 3);
|
||||
}
|
||||
|
||||
public static void gameMessage(MinecraftServer server, Text text, boolean b) {
|
||||
if (!b) {
|
||||
sendNotification("Server message", text.getString(), 3);
|
||||
}
|
||||
}
|
||||
}
|
25
src/main/resources/fabric.mod.json
Normal file
25
src/main/resources/fabric.mod.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"id": "gotifymc",
|
||||
"version": "${version}",
|
||||
"name": "GotifyMC",
|
||||
"description": "",
|
||||
"authors": [],
|
||||
"contact": {},
|
||||
"license": "MIT",
|
||||
"icon": "assets/gotifymc/icon.png",
|
||||
"environment": "server",
|
||||
"entrypoints": {
|
||||
"main": [
|
||||
"systems.brn.gotifymc.GotifyMC"
|
||||
]
|
||||
},
|
||||
"mixins": [
|
||||
"gotifymc.mixins.json"
|
||||
],
|
||||
"depends": {
|
||||
"fabricloader": ">=${loader_version}",
|
||||
"fabric": "*",
|
||||
"minecraft": "${minecraft_version}"
|
||||
}
|
||||
}
|
13
src/main/resources/gotifymc.mixins.json
Normal file
13
src/main/resources/gotifymc.mixins.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"required": true,
|
||||
"minVersion": "0.8",
|
||||
"package": "systems.brn.gotifymc.mixin",
|
||||
"compatibilityLevel": "JAVA_21",
|
||||
"mixins": [
|
||||
],
|
||||
"client": [
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user