Initial commit

This commit is contained in:
mangorifo
2023-03-28 19:24:11 +08:00
commit a6af8fdae2
46 changed files with 2228 additions and 0 deletions
+187
View File
@@ -0,0 +1,187 @@
package cf.sobrooms;
import cf.sobrooms.commands.InfoCommand;
import cf.sobrooms.commands.PingCommand;
import cf.sobrooms.commands.SendMessageCommand;
import cf.sobrooms.commands.SetLoggingModeCommand;
import cf.sobrooms.events.enchantment.EnchantItem;
import cf.sobrooms.events.enchantment.PrepareItemEnchant;
import cf.sobrooms.events.player.*;
import cf.sobrooms.events.server.ServerStart;
import cf.sobrooms.events.weather.LightningStrike;
import cf.sobrooms.events.weather.ThunderChange;
import cf.sobrooms.events.weather.WeatherChange;
import cf.sobrooms.events.world.WorldInit;
import cf.sobrooms.events.world.WorldLoad;
import cf.sobrooms.events.world.WorldSave;
import cf.sobrooms.events.world.WorldUnload;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.*;
import java.util.Objects;
public class Main extends JavaPlugin {
public static File isLoggingConfig;
public static File notifyWebhookConfig;
public static File pubMessageWebhookConfig;
public static String serverAddress = "mc-srv2-singapore.rrryfoo.cf";
private static boolean webhooksAreValidUrls;
public static void main(String... args) {
System.out.printf("Initializing using server address: %s...%n", serverAddress);
}
@Override
public void onEnable() {
String loggingDir = "./NoS";
File directory = new File(loggingDir);
if (!directory.exists()) {
if (directory.mkdir()) {
System.out.println("Config dir created: " + loggingDir);
} else {
System.out.println("Failed to create config directory: " + loggingDir);
}
}
isLoggingConfig = new File("./NoS/willLogCommands.bool");
notifyWebhookConfig = new File("./NoS/notifyWebhook.json");
pubMessageWebhookConfig = new File("./NoS/pubMessageWebhook.json");
try {
if (isLoggingConfig.createNewFile() || notifyWebhookConfig.createNewFile() || pubMessageWebhookConfig.createNewFile()) {
System.out.println("Config created: " + isLoggingConfig.getName());
FileWriter notifyWebhookConfigWriter = new FileWriter(notifyWebhookConfig);
FileWriter pubMessageWebhookConfigWriter = new FileWriter(pubMessageWebhookConfig);
notifyWebhookConfigWriter.write("{\"url\": \"default-ns\"}");
pubMessageWebhookConfigWriter.write("{\"url\": \"default-ns\"}");
pubMessageWebhookConfigWriter.close();
notifyWebhookConfigWriter.close();
} else {
System.out.println("Created config even while it already exists, skipping step...");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
// post
Bukkit.getConsoleSender().sendMessage("Note: This Spigot/Bukkit plugin is highly verbose, so it will log a lot of events...");
// register commands
try {
Objects.requireNonNull(this.getCommand("info")).setExecutor(new InfoCommand());
Objects.requireNonNull(this.getCommand("send-message")).setExecutor(new SendMessageCommand());
Objects.requireNonNull(this.getCommand("set-logging")).setExecutor(new SetLoggingModeCommand());
Objects.requireNonNull(this.getCommand("ping-server")).setExecutor(new PingCommand());
} catch (NullPointerException e) {
throw new RuntimeException(e);
}
// register events
ServerStart serverStart = new ServerStart();
getServer().getPluginManager().registerEvents(serverStart, this);
if (getLoggingConfig().equals("true") && webhooksAreValidUrls)
registerEvents();
if (webhooksAreValidUrls) {
PlayerJoin playerJoin = new PlayerJoin();
PlayerQuit playerQuit = new PlayerQuit();
getServer().getPluginManager().registerEvents(playerJoin, this);
getServer().getPluginManager().registerEvents(playerQuit, this);
}
}
public static String getPublicMessageWebhook() {
try {
BufferedReader brfmsg = new BufferedReader(new FileReader(pubMessageWebhookConfig));
JsonParser jsonReader = new JsonParser();
JsonElement MsgParse = jsonReader.parse(brfmsg.readLine());
String lineMsg = MsgParse.getAsJsonObject().get("url").getAsString();
// validate url being url
// but this won't validate the url being an actual webhook
// i can try but lazy :sob:
if (lineMsg.equals("default-ns")) {
System.out.printf("Please set the webhook URL in %s and restart the server", pubMessageWebhookConfig.getAbsolutePath());
webhooksAreValidUrls = false;
return lineMsg;
} else if (lineMsg.startsWith("https://") || lineMsg.startsWith("http://")) {
return lineMsg;
} else {
System.out.printf("Please set the webhook URL in %s and restart the server", pubMessageWebhookConfig.getAbsolutePath());
webhooksAreValidUrls = false;
return lineMsg;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String getNotifyWebhook() {
try {
BufferedReader brfntfy = new BufferedReader(new FileReader(notifyWebhookConfig));
JsonParser jsonReader = new JsonParser();
JsonElement NtfyParse = jsonReader.parse(brfntfy.readLine());
String lineNtfy = NtfyParse.getAsJsonObject().get("url").getAsString();
// validate url being url
// but this won't validate the url being an actual webhook
// i can try but lazy :sob:
if (lineNtfy.equals("default-ns")) {
System.out.printf("Please set the webhook URL in %s and restart the server", notifyWebhookConfig.getAbsolutePath());
webhooksAreValidUrls = false;
return lineNtfy;
} else if (lineNtfy.startsWith("https://") || lineNtfy.startsWith("http://")) {
return lineNtfy;
} else {
System.out.printf("Please set the webhook URL in %s and restart the server", notifyWebhookConfig.getAbsolutePath());
webhooksAreValidUrls = false;
return lineNtfy;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String getLoggingConfig() {
try {
File file = new File("./NoS/willLogCommands.bool");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
if (line.equals("true") || line.equals("false"))
return line;
else
return "Content is not a boolean";
}
reader.close();
} catch (IOException e) {
System.out.println("An error occurred while reading the configuration file.");
e.printStackTrace();
return "err";
}
return "err";
}
public void registerEvents() {
PlayerChat playerChat = new PlayerChat();
PlayerCommandPreprocess playerCommandPreprocess = new PlayerCommandPreprocess();
EnchantItem enchantItemEvent = new EnchantItem();
PrepareItemEnchant prepareItemEnchant = new PrepareItemEnchant();
PlayerBedEnter playerBedEnter = new PlayerBedEnter();
PlayerBedLeave playerBedLeave = new PlayerBedLeave();
LightningStrike lightningStrike = new LightningStrike();
ThunderChange thunderChange = new ThunderChange();
WeatherChange weatherChange = new WeatherChange();
WorldInit worldInit = new WorldInit();
WorldLoad worldLoad = new WorldLoad();
WorldSave worldSave = new WorldSave();
WorldUnload worldUnload = new WorldUnload();
getServer().getPluginManager().registerEvents(playerChat, this);
getServer().getPluginManager().registerEvents(playerCommandPreprocess, this);
getServer().getPluginManager().registerEvents(enchantItemEvent, this);
getServer().getPluginManager().registerEvents(prepareItemEnchant, this);
getServer().getPluginManager().registerEvents(playerBedEnter, this);
getServer().getPluginManager().registerEvents(playerBedLeave, this);
getServer().getPluginManager().registerEvents(lightningStrike, this);
getServer().getPluginManager().registerEvents(thunderChange, this);
getServer().getPluginManager().registerEvents(weatherChange, this);
getServer().getPluginManager().registerEvents(worldInit, this);
getServer().getPluginManager().registerEvents(worldLoad, this);
getServer().getPluginManager().registerEvents(worldSave, this);
getServer().getPluginManager().registerEvents(worldUnload, this);
}
}