From c1f326f49fc76c30cd5cf0a14deb94e4568a8324 Mon Sep 17 00:00:00 2001 From: Jkibbels Date: Thu, 14 Nov 2024 22:03:26 -0500 Subject: [PATCH] keeblarcraft 1.0 plugin release --- .gitignore | 1 + pom.xml | 108 ++++++++++++++++++ src/main/java/tech/techyjessy/App.java | 36 ++++++ .../Commands/CustomWhitelistCommand.java | 37 ++++++ .../techyjessy/Commands/ListRegistrars.java | 25 ++++ .../techyjessy/Commands/ListWhitelisted.java | 34 ++++++ .../Commands/RegisterWhitelister.java | 41 +++++++ .../techyjessy/Configuration/ConfigMgr.java | 70 ++++++++++++ .../Configuration/WhitelisterFormat.java | 74 ++++++++++++ src/main/resources/plugin.yml | 16 +++ src/test/java/tech/techyjessy/AppTest.java | 20 ++++ 11 files changed, 462 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/tech/techyjessy/App.java create mode 100644 src/main/java/tech/techyjessy/Commands/CustomWhitelistCommand.java create mode 100644 src/main/java/tech/techyjessy/Commands/ListRegistrars.java create mode 100644 src/main/java/tech/techyjessy/Commands/ListWhitelisted.java create mode 100644 src/main/java/tech/techyjessy/Commands/RegisterWhitelister.java create mode 100644 src/main/java/tech/techyjessy/Configuration/ConfigMgr.java create mode 100644 src/main/java/tech/techyjessy/Configuration/WhitelisterFormat.java create mode 100644 src/main/resources/plugin.yml create mode 100644 src/test/java/tech/techyjessy/AppTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2d2e88 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/classes target/generated-sources target/generated-test-sources target/keeblarcraft-1.0.jar target/maven-archiver target/maven-status target/surefire-reports target/test-classes diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..79102b8 --- /dev/null +++ b/pom.xml @@ -0,0 +1,108 @@ + + + + 4.0.0 + + tech.techyjessy + keeblarcraft + 1.0 + jar + + keeblarcraft + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + + + + junit + junit + 4.11 + test + + + + org.spigotmc + spigot-api + 1.21.1-R0.1-SNAPSHOT + provided + + + + commons-io + commons-io + 2.4 + + + + + + + ${project.basedir}/src/main/java + + + ${project.basedir}/src/main/resources + + plugin.yml + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + diff --git a/src/main/java/tech/techyjessy/App.java b/src/main/java/tech/techyjessy/App.java new file mode 100644 index 0000000..1bbda84 --- /dev/null +++ b/src/main/java/tech/techyjessy/App.java @@ -0,0 +1,36 @@ +package tech.techyjessy; + +import java.io.File; + +import org.bukkit.plugin.java.JavaPlugin; + +import tech.techyjessy.Commands.CustomWhitelistCommand; +import tech.techyjessy.Commands.ListRegistrars; +import tech.techyjessy.Commands.ListWhitelisted; +import tech.techyjessy.Commands.RegisterWhitelister; +import tech.techyjessy.Configuration.ConfigMgr; +import tech.techyjessy.Configuration.WhitelisterFormat; + +public class App extends JavaPlugin { + public static ConfigMgr config = ConfigMgr.getInstance(); + public static WhitelisterFormat wFormat = WhitelisterFormat.getInstance(); + + @Override + public void onEnable() { + getLogger().info("Hello, SpigotMC!"); + + // initialize crucial plugin stuff + config.SetConfDir(getDataFolder()); + wFormat.Init(); + + // command initialization + this.getCommand("register-whitelister").setExecutor(new RegisterWhitelister()); + this.getCommand("whitelist-list").setExecutor(new ListWhitelisted()); + this.getCommand("whitelist-add").setExecutor(new CustomWhitelistCommand()); + this.getCommand("whitelister-list").setExecutor(new ListRegistrars()); + } + @Override + public void onDisable() { + getLogger().info("See you again, SpigotMC!"); + } +} diff --git a/src/main/java/tech/techyjessy/Commands/CustomWhitelistCommand.java b/src/main/java/tech/techyjessy/Commands/CustomWhitelistCommand.java new file mode 100644 index 0000000..feb57f0 --- /dev/null +++ b/src/main/java/tech/techyjessy/Commands/CustomWhitelistCommand.java @@ -0,0 +1,37 @@ +package tech.techyjessy.Commands; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.OfflinePlayer; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import tech.techyjessy.Configuration.WhitelisterFormat; + +public class CustomWhitelistCommand implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + + // Player must be registrar or opped to use this command + if (WhitelisterFormat.getInstance().IsRegistrar(player.getName()) || player.isOp()) { + for (String name : args) { + // Attempt to construct a player object for each name to add + try { + sender.getServer().getOfflinePlayer(name).setWhitelisted(true); + player.sendMessage("Added " + name + " to the whitelist"); + } catch (Exception e) { + player.sendMessage("Could not add " + name + " to the whitelist. Make sure that's their correct name and have logged in the last 30 days!"); + } + } + } else { + player.sendMessage("You cannot whitelist people! If you want to become a whitelist registrar please contact a server administrator!"); + } + } + return true; + } +} diff --git a/src/main/java/tech/techyjessy/Commands/ListRegistrars.java b/src/main/java/tech/techyjessy/Commands/ListRegistrars.java new file mode 100644 index 0000000..7ec8bf9 --- /dev/null +++ b/src/main/java/tech/techyjessy/Commands/ListRegistrars.java @@ -0,0 +1,25 @@ +package tech.techyjessy.Commands; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import tech.techyjessy.Configuration.WhitelisterFormat; + +public class ListRegistrars implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + + for (String registrar : WhitelisterFormat.getInstance().ListNames()) { + player.sendMessage("REGISTRAR -> " + registrar); + } + } + return true; + } +} diff --git a/src/main/java/tech/techyjessy/Commands/ListWhitelisted.java b/src/main/java/tech/techyjessy/Commands/ListWhitelisted.java new file mode 100644 index 0000000..c8bcc1e --- /dev/null +++ b/src/main/java/tech/techyjessy/Commands/ListWhitelisted.java @@ -0,0 +1,34 @@ +package tech.techyjessy.Commands; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.bukkit.OfflinePlayer; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import tech.techyjessy.Configuration.WhitelisterFormat; + +public class ListWhitelisted implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + + if (WhitelisterFormat.getInstance().IsRegistrar(sender.getName()) || player.isOp()) { + List playerList = new ArrayList(); + Iterator whitelistIterator = sender.getServer().getWhitelistedPlayers().iterator(); + while (whitelistIterator.hasNext()) { + OfflinePlayer p = whitelistIterator.next(); + playerList.add(p.getName()); + } + + player.sendMessage("WHITELIST -> " + playerList); + } + } + return true; + } +} diff --git a/src/main/java/tech/techyjessy/Commands/RegisterWhitelister.java b/src/main/java/tech/techyjessy/Commands/RegisterWhitelister.java new file mode 100644 index 0000000..04e9977 --- /dev/null +++ b/src/main/java/tech/techyjessy/Commands/RegisterWhitelister.java @@ -0,0 +1,41 @@ +package tech.techyjessy.Commands; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import tech.techyjessy.Configuration.ConfigMgr; +import tech.techyjessy.Configuration.WhitelisterFormat; + +public class RegisterWhitelister implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender instanceof Player) { + List errors = new ArrayList(); + Player player = (Player) sender; + if (player.isOp()) { + // We will assume the player wants multiple people whitelisted if there are multiple arguments + WhitelisterFormat wFormat = WhitelisterFormat.getInstance(); + for (String name : args) { + String err = wFormat.AddName(name.toLowerCase()); + + // Add any error messages to display to player later + if (err != "success") { + errors.add(err); + } + } + } else { + player.sendMessage("You don't have permission for this command!"); + } + for (String err : errors) { + player.sendMessage(err); + } + } + return true; + } +} diff --git a/src/main/java/tech/techyjessy/Configuration/ConfigMgr.java b/src/main/java/tech/techyjessy/Configuration/ConfigMgr.java new file mode 100644 index 0000000..7b8e053 --- /dev/null +++ b/src/main/java/tech/techyjessy/Configuration/ConfigMgr.java @@ -0,0 +1,70 @@ +package tech.techyjessy.Configuration; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; + +import org.apache.commons.io.FileUtils; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonIOException; +import com.google.gson.JsonSyntaxException; + +public class ConfigMgr { + private static ConfigMgr static_inst; + private File config; + + public static ConfigMgr getInstance() { + if (static_inst == null) { + static_inst = new ConfigMgr(); + } + + return static_inst; + } + + public void SetConfDir(File file) { + config = new File(file, "keeblarcraft.json"); + if (!config.exists()) { + config.getParentFile().mkdirs(); + } + } + + public T GetJsonObjectFromFile(Class classToConvertTo) { + Gson gson = new Gson(); + String ret = ""; + + // hot fix: Not sure how to return "false" for invalid conversion when I'm forced to convert or just catch... Look into a better + // return value in the future - but for now throw JsonSyntaxException no matter what exception is caught + try { + ret = FileUtils.readFileToString(config, "UTF-8"); + } catch (Exception e) { + System.out.println("Getting json object from file had errors. This is unsupported in this version of Keeblarcraft"); + e.printStackTrace(); + } + + return gson.fromJson(ret, classToConvertTo); + } + + // WriteToJsonFile + // + // Will write to or append to a json file. It will search if the key exists first & update the field; + // or add a new entry. It should be noted that json objects *can* be buried inside each other. It is + // considered best (and only) practice to call the "GetJsonStringFromFile" function first from this + // class and simply iterate to what you would need and then update the entire entry alongside the + // top-level key. + // + // NOTE: THIS DOES NOT SAFE UPDATE THE KEY OBJECT. PRE-EXISTING DATA WILL BE DELETED FOREVER + public void WriteToJsonFile(Object data) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + try { + FileWriter writer = new FileWriter(config); + gson.toJson(data, writer); + writer.flush(); + writer.close(); + } catch (JsonIOException | IOException e) { + System.out.println("Writing to json had errors. This is unhandled in this version of Keeblarcraft"); + } + } +} diff --git a/src/main/java/tech/techyjessy/Configuration/WhitelisterFormat.java b/src/main/java/tech/techyjessy/Configuration/WhitelisterFormat.java new file mode 100644 index 0000000..86d49a5 --- /dev/null +++ b/src/main/java/tech/techyjessy/Configuration/WhitelisterFormat.java @@ -0,0 +1,74 @@ +package tech.techyjessy.Configuration; + +import java.util.ArrayList; +import java.util.List; + +import tech.techyjessy.App; + +public class WhitelisterFormat { + private static WhitelisterFormat static_inst; + + public static WhitelisterFormat getInstance() { + if (static_inst == null) { + static_inst = new WhitelisterFormat(); + } + + return static_inst; + } + + class WhitelisterJsonFormat { + List names = new ArrayList(); + } + + WhitelisterJsonFormat internalFormat = new WhitelisterJsonFormat(); + + public void Init() { + ReadConfig(); + } + + public void ReadConfig() { + internalFormat = App.config.GetJsonObjectFromFile(WhitelisterJsonFormat.class); + } + + public void WriteConfig() { + App.config.WriteToJsonFile(internalFormat); + } + + public String AddName(String newName) { + String ret = ""; + if (!internalFormat.names.contains(newName)) { + internalFormat.names.add(newName); + WriteConfig(); + ret = "success"; + } else { + ret = newName + " is already on the whitelister registrar list!"; + } + + return ret; + } + + public String DelName(String newName) { + String ret = ""; + if (internalFormat.names.contains(newName)) { + internalFormat.names.remove(newName); + WriteConfig(); + ret = "success"; + } else { + ret = newName + " is not on the whitelister registrar list!"; + } + + return ret; + } + + public Boolean IsRegistrar(String name) { + if (internalFormat.names.contains(name)) { + return true; + } else { + return false; + } + } + + public List ListNames() { + return internalFormat.names; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..c3be38f --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,16 @@ +main: tech.techyjessy.App +name: Keeblarcraft +version: 1.0 +commands: + register-whitelister: + description: Registers someone to be able to whitelist others on the server + usage: /register-whitelister + whitelist-list: + description: Gets the whitelist on the server via the Keeblarcraft plugin. This lets registrars see the whitelist too + usage: /whitelist-list + whitelist-add: + description: Add a player to the server whitelist as an opped player or as registrar + usage: /whitelist-add {name1} {name2} {...} + whitelister-list: + description: Command allows everyone on the server to see who can whitelist! This allows players to contact these people to whitelist others in need + usage: /whitelister-list \ No newline at end of file diff --git a/src/test/java/tech/techyjessy/AppTest.java b/src/test/java/tech/techyjessy/AppTest.java new file mode 100644 index 0000000..932fe88 --- /dev/null +++ b/src/test/java/tech/techyjessy/AppTest.java @@ -0,0 +1,20 @@ +package tech.techyjessy; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class AppTest +{ + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() + { + assertTrue( true ); + } +}