Skip to content

Commit

Permalink
50% complete
Browse files Browse the repository at this point in the history
  • Loading branch information
muriplz committed Jan 17, 2024
1 parent 7ca0a02 commit fe6c401
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 68 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ loom {
splitEnvironmentSourceSets()

mods {
"modid" {
"votifier" {
sourceSet sourceSets.main
sourceSet sourceSets.client
}
Expand Down
89 changes: 22 additions & 67 deletions src/main/java/com/kryeit/votifier/Votifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@
package com.kryeit.votifier;

import java.io.*;
import java.nio.file.Path;
import java.security.KeyPair;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.*;

import com.kryeit.votifier.config.ConfigReader;
import net.fabricmc.api.DedicatedServerModInitializer;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import com.kryeit.votifier.crypto.RSAIO;
import com.kryeit.votifier.crypto.RSAKeygen;
import com.kryeit.votifier.model.ListenerLoader;
import com.kryeit.votifier.model.VoteListener;
import com.kryeit.votifier.net.VoteReceiver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The main Votifier plugin class.
Expand All @@ -43,7 +44,7 @@
public class Votifier implements DedicatedServerModInitializer {

/** The logger instance. */
private static final Logger LOG = Logger.getLogger("Votifier");
public static final Logger LOGGER = LoggerFactory.getLogger(Votifier.class);

/** Log entry prefix */
private static final String logPrefix = "[Votifier] ";
Expand All @@ -66,30 +67,23 @@ public class Votifier implements DedicatedServerModInitializer {
/** Debug mode flag */
private boolean debug;

/**
* Attach custom log filter to logger.
*/
static {
LOG.setFilter(new LogFilter(logPrefix));
}

@Override
public void onInitializeServer() {
Votifier.instance = this;

// Set the plugin version.
version = getDescription().getVersion();
version = "1.0";

// Handle configuration.
if (!getDataFolder().exists()) {
getDataFolder().mkdir();
try {
LOGGER.info("Reading config file...");
ConfigReader.readFile(Path.of("config/votifier"));
} catch (IOException e) {
throw new RuntimeException(e);
}
File config = new File(getDataFolder() + "/config.yml");
YamlConfiguration cfg = YamlConfiguration.loadConfiguration(config);
File rsaDirectory = new File(getDataFolder() + "/rsa");
File rsaDirectory = new File("mods/votifier/rsa");
// Replace to remove a bug with Windows paths - SmilingDevil
String listenerDirectory = getDataFolder().toString()
.replace("\\", "/") + "/listeners";
String listenerDirectory = "mods/votifier/listeners";

/*
* Use IP address from server.properties as a default for
Expand All @@ -101,44 +95,6 @@ public void onInitializeServer() {
if (hostAddr == null || hostAddr.length() == 0)
hostAddr = "0.0.0.0";

/*
* Create configuration file if it does not exists; otherwise, load it
*/
if (!config.exists()) {
try {
// First time run - do some initialization.
LOG.info("Configuring Votifier for the first time...");

// Initialize the configuration file.
config.createNewFile();

cfg.set("host", hostAddr);
cfg.set("port", 8192);
cfg.set("debug", false);

/*
* Remind hosted server admins to be sure they have the right
* port number.
*/
LOG.info("------------------------------------------------------------------------------");
LOG.info("Assigning Votifier to listen on port 8192. If you are hosting Craftbukkit on a");
LOG.info("shared server please check with your hosting provider to verify that this port");
LOG.info("is available for your use. Chances are that your hosting provider will assign");
LOG.info("a different port, which you need to specify in config.yml");
LOG.info("------------------------------------------------------------------------------");

cfg.set("listener_folder", listenerDirectory);
cfg.save(config);
} catch (Exception ex) {
LOG.log(Level.SEVERE, "Error creating configuration file", ex);
gracefulExit();
return;
}
} else {
// Load configuration.
cfg = YamlConfiguration.loadConfiguration(config);
}

/*
* Create RSA directory and keys if it does not exist; otherwise, read
* keys.
Expand All @@ -153,28 +109,27 @@ public void onInitializeServer() {
keyPair = RSAIO.load(rsaDirectory);
}
} catch (Exception ex) {
LOG.log(Level.SEVERE,
"Error reading configuration file or RSA keys", ex);
LOGGER.warn("Error reading configuration file or RSA keys", ex);
gracefulExit();
return;
}

// Load the vote listeners.
listenerDirectory = cfg.getString("listener_folder");
listenerDirectory = ConfigReader.LISTENER_FOLDER;
listeners.addAll(ListenerLoader.load(listenerDirectory));

// Initialize the receiver.
String host = cfg.getString("host", hostAddr);
int port = cfg.getInt("port", 8192);
debug = cfg.getBoolean("debug", false);
String host = ConfigReader.HOST;
int port = ConfigReader.PORT;
debug = ConfigReader.DEBUG;
if (debug)
LOG.info("DEBUG mode enabled!");
LOGGER.info("DEBUG mode enabled!");

try {
voteReceiver = new VoteReceiver(this, host, port);
voteReceiver.start();

LOG.info("Votifier enabled.");
LOGGER.info("Votifier enabled.");
} catch (Exception ex) {
gracefulExit();
return;
Expand All @@ -188,11 +143,11 @@ public void registerDisableEvent() {
if (voteReceiver != null) {
voteReceiver.shutdown();
}
LOG.info("Votifier disabled.");
LOGGER.info("Votifier disabled.");
}

private void gracefulExit() {
LOG.log(Level.SEVERE, "Votifier did not initialize properly!");
LOGGER.warn("Votifier did not initialize properly!");
}

/**
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/kryeit/votifier/config/ConfigReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.kryeit.votifier.config;


import com.kryeit.votifier.utils.JSONObject;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

public class ConfigReader {

public static String HOST;
public static int PORT;
public static boolean DEBUG;
public static String LISTENER_FOLDER;

private ConfigReader() {

}

public static void readFile(Path path) throws IOException {
String config = readOrCopyFile(path.resolve("config.json"), "/config.json");
JSONObject configObject = new JSONObject(config);
HOST = configObject.getString("host");
PORT = Integer.parseInt(configObject.getString("port"));
DEBUG = configObject.getBoolean("debug");
LISTENER_FOLDER = configObject.getString("listener-folder");
}

public static String readOrCopyFile(Path path, String exampleFile) throws IOException {
File file = path.toFile();
if (!file.exists()) {
InputStream stream = ConfigReader.class.getResourceAsStream(exampleFile);
if (stream == null) throw new NullPointerException("Cannot load example file");

//noinspection ResultOfMethodCallIgnored
file.getParentFile().mkdirs();
Files.copy(stream, path);
}
return Files.readString(path);
}
}
Loading

0 comments on commit fe6c401

Please sign in to comment.