diff --git a/build.gradle b/build.gradle index 51830e7..e283a17 100644 --- a/build.gradle +++ b/build.gradle @@ -25,18 +25,10 @@ repositories { maven { url 'https://repo.papermc.io/repository/maven-public/' } - maven { - url 'https://nexus.bencodez.com/repository/maven-public/' - } - maven { - url 'https://nexuslite.gcnt.net/repos/other/' - } } dependencies { compileOnly 'io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT' - - implementation 'com.bencodez:advancedcore:3.7.3' } processResources { diff --git a/src/main/java/net/simplyvanilla/simplyvotifier/SimplyVotifier.java b/src/main/java/net/simplyvanilla/simplyvotifier/SimplyVotifier.java index ee3183d..0bd6bfe 100644 --- a/src/main/java/net/simplyvanilla/simplyvotifier/SimplyVotifier.java +++ b/src/main/java/net/simplyvanilla/simplyvotifier/SimplyVotifier.java @@ -18,22 +18,17 @@ package net.simplyvanilla.simplyvotifier; -import com.bencodez.advancedcore.AdvancedCorePlugin; -import com.bencodez.advancedcore.folialib.FoliaLib; +import java.io.File; +import java.security.KeyPair; import lombok.Getter; import lombok.Setter; -import net.simplyvanilla.simplyvotifier.config.Config; import net.simplyvanilla.simplyvotifier.crypto.RSAIO; import net.simplyvanilla.simplyvotifier.crypto.RSAKeygen; import net.simplyvanilla.simplyvotifier.model.Vote; import net.simplyvanilla.simplyvotifier.model.VotifierEvent; import net.simplyvanilla.simplyvotifier.net.VoteReceiver; import org.bukkit.Bukkit; - -import java.io.File; -import java.net.InetSocketAddress; -import java.net.ServerSocket; -import java.security.KeyPair; +import org.bukkit.plugin.java.JavaPlugin; /** * The main Votifier plugin class. @@ -41,194 +36,125 @@ * @author Blake Beaupain * @author Kramer Campbell */ -public class SimplyVotifier extends AdvancedCorePlugin { +public class SimplyVotifier extends JavaPlugin { - /** - * The Votifier instance. - */ - @Getter - private static SimplyVotifier instance; + /** The Votifier instance. */ + @Getter private static SimplyVotifier instance; - @Getter - private FoliaLib foliaLib; + /** The vote receiver. */ + @Getter private VoteReceiver voteReceiver; - public Config config; + /** The RSA key pair. */ + @Getter @Setter private KeyPair keyPair; - /** - * The vote receiver. - */ - @Getter - private VoteReceiver voteReceiver; + @Override + public void onEnable() { + this.saveDefaultConfig(); + this.reloadConfig(); + File rsaDirectory = new File(this.getDataFolder() + "/rsa"); - /** - * The RSA key pair. + /* + * Create RSA directory and keys if it does not exist; otherwise, read keys. */ - @Getter - @Setter - private KeyPair keyPair; - - - @Override - public void onPostLoad() { - this.foliaLib = new FoliaLib(this); - - File rsaDirectory = new File(getDataFolder() + "/rsa"); - - /* - * Create RSA directory and keys if it does not exist; otherwise, read keys. - */ - try { - if (!rsaDirectory.exists()) { - rsaDirectory.mkdir(); - keyPair = RSAKeygen.generate(2048); - RSAIO.save(rsaDirectory, keyPair); - } else { - keyPair = RSAIO.load(rsaDirectory); - } - } catch (Exception ex) { - getLogger().severe("Error reading configuration file or RSA keys"); - gracefulExit(); - return; - } - - loadVoteReceiver(); + try { + if (!rsaDirectory.exists()) { + rsaDirectory.mkdir(); + this.keyPair = RSAKeygen.generate(2048); + RSAIO.save(rsaDirectory, this.keyPair); + } else { + this.keyPair = RSAIO.load(rsaDirectory); + } + } catch (Exception ex) { + this.getLogger().severe("Error reading configuration file or RSA keys"); + return; } - private void loadVoteReceiver() { - try { - voteReceiver = new VoteReceiver(config.getHost(), config.getPort()) { - - @Override - public void logWarning(String warn) { - getLogger().warning(warn); - } - - @Override - public void logSevere(String msg) { - getLogger().severe(msg); - } - - @Override - public void log(String msg) { - getLogger().info(msg); - } - - @Override - public String getVersion() { - return getDescription().getVersion(); - } - - @Override - public KeyPair getKeyPair() { - return instance.getKeyPair(); - } - - @Override - public void debug(Exception e) { - instance.debug(e); - } - - @Override - public void debug(String debug) { - instance.debug(debug); - } - - @Override - public void callEvent(Vote vote) { - foliaLib.getImpl().runAsync(new Runnable() { - public void run() { - Bukkit.getServer().getPluginManager().callEvent(new VotifierEvent(vote)); - } - }); - } - }; - voteReceiver.start(); - - getLogger().info("Votifier enabled."); - } catch (Exception ex) { - gracefulExit(); - return; - } - } + this.loadVoteReceiver(); + } - @Override - public void onDisable() { - // Interrupt the vote receiver. - if (voteReceiver != null) { - voteReceiver.shutdown(); - } - getLogger().info("Votifier disabled."); - } + private void loadVoteReceiver() { + try { + this.voteReceiver = + new VoteReceiver(this.getConfig().getString("host"), this.getConfig().getInt("port")) { - private void gracefulExit() { - getLogger().severe("Votifier did not initialize properly!"); - } + @Override + public void logWarning(String warn) { + SimplyVotifier.this.getLogger().warning(warn); + } - @Override - public void onPreLoad() { - instance = this; + @Override + public void logSevere(String msg) { + SimplyVotifier.this.getLogger().severe(msg); + } - config = new Config(this); - config.setup(); + @Override + public void log(String msg) { + SimplyVotifier.this.getLogger().info(msg); + } - if (config.isJustCreated()) { - int openPort = 8192; - try { - ServerSocket s = new ServerSocket(); - s.bind(new InetSocketAddress("0.0.0.0", 0)); - openPort = s.getLocalPort(); - s.close(); - } catch (Exception e) { + @Override + public String getVersion() { + return SimplyVotifier.this.getDescription().getVersion(); + } + @Override + public KeyPair getKeyPair() { + return instance.getKeyPair(); } - try { - // First time run - do some initialization. - getLogger().info("Configuring Votifier for the first time..."); - config.getData().set("port", openPort); - config.saveData(); - - /* - * Remind hosted server admins to be sure they have the right port number. - */ - getLogger().info("------------------------------------------------------------------------------"); - getLogger().info("Assigning Votifier to listen on an open port " + openPort - + ". If you are hosting server on a"); - getLogger().info("shared server please check with your hosting provider to verify that this port"); - getLogger().info("is available for your use. Chances are that your hosting provider will assign"); - getLogger().info("a different port, which you need to specify in config.yml"); - getLogger().info("------------------------------------------------------------------------------"); - - } catch (Exception ex) { - getLogger().severe("Error creating configuration file"); - debug(ex); + + @Override + public void debug(Exception e) { + SimplyVotifier.this.getSLF4JLogger().error("Error in Votifier", e); } - } - config.loadValues(); - updateAdvancedCoreHook(); - } + @Override + public void debug(String debug) { + SimplyVotifier.this.getSLF4JLogger().info(debug); + } - @Override - public void onUnLoad() { + @Override + public void callEvent(Vote vote) { + if (isFolia()) { + Bukkit.getAsyncScheduler() + .runNow( + SimplyVotifier.this, + scheduledTask -> { + Bukkit.getServer().getPluginManager().callEvent(new VotifierEvent(vote)); + }); + } else { + Bukkit.getScheduler() + .runTask( + SimplyVotifier.this, + () -> + Bukkit.getServer() + .getPluginManager() + .callEvent(new VotifierEvent(vote))); + } + } + }; + this.voteReceiver.start(); + this.getLogger().info("Votifier enabled."); + } catch (Exception e) { + this.getSLF4JLogger().error("Error in Votifier", e); } + } - @Override - public void reload() { - config.reloadData(); - updateAdvancedCoreHook(); - voteReceiver.shutdown(); - loadVoteReceiver(); + @Override + public void onDisable() { + // Interrupt the vote receiver. + if (this.voteReceiver != null) { + this.voteReceiver.shutdown(); } - - @SuppressWarnings("deprecation") - public void updateAdvancedCoreHook() { - setConfigData(config.getData()); - setLoadRewards(false); - setLoadServerData(false); - setLoadUserData(false); - setLoadGeyserAPI(false); - setLoadLuckPerms(false); + this.getLogger().info("Votifier disabled."); + } + + public static boolean isFolia() { + try { + Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); + return true; + } catch (ClassNotFoundException e) { + return false; } - + } } diff --git a/src/main/java/net/simplyvanilla/simplyvotifier/config/Config.java b/src/main/java/net/simplyvanilla/simplyvotifier/config/Config.java deleted file mode 100644 index 6476708..0000000 --- a/src/main/java/net/simplyvanilla/simplyvotifier/config/Config.java +++ /dev/null @@ -1,39 +0,0 @@ -package net.simplyvanilla.simplyvotifier.config; - -import com.bencodez.advancedcore.api.yml.YMLFile; -import com.bencodez.advancedcore.api.yml.annotation.AnnotationHandler; -import com.bencodez.advancedcore.api.yml.annotation.ConfigDataBoolean; -import com.bencodez.advancedcore.api.yml.annotation.ConfigDataInt; -import com.bencodez.advancedcore.api.yml.annotation.ConfigDataString; -import lombok.Getter; -import lombok.Setter; -import net.simplyvanilla.simplyvotifier.SimplyVotifier; - -import java.io.File; - -@Setter -@Getter -public class Config extends YMLFile { - - public Config(SimplyVotifier plugin) { - super(plugin, new File(SimplyVotifier.getInstance().getDataFolder(), "config.yml")); - } - - public void loadValues() { - new AnnotationHandler().load(getData(), this); - } - - @Override - public void onFileCreation() { - SimplyVotifier.getInstance().saveResource("config.yml", true); - } - - @ConfigDataString(path = "host") - private String host = "0.0.0.0"; - - @ConfigDataInt(path = "port") - private int port = 8192; - - @ConfigDataBoolean(path = "debug") - private boolean debug = false; -} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 2e22435..ffb7d19 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,7 +1,2 @@ -# Debug levels: -# NONE -# INFO -# EXTRA -DebugLevel: NONE host: 0.0.0.0 port: 8192