keeblarcraft 1.0 plugin release

This commit is contained in:
Jkibbels 2024-11-14 22:03:26 -05:00
commit c1f326f49f
11 changed files with 462 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -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

108
pom.xml Normal file
View File

@ -0,0 +1,108 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tech.techyjessy</groupId>
<artifactId>keeblarcraft</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>keeblarcraft</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.21.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>plugin.yml</include>
</includes>
</resource>
</resources>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -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!");
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<String> playerList = new ArrayList<String>();
Iterator<OfflinePlayer> whitelistIterator = sender.getServer().getWhitelistedPlayers().iterator();
while (whitelistIterator.hasNext()) {
OfflinePlayer p = whitelistIterator.next();
playerList.add(p.getName());
}
player.sendMessage("WHITELIST -> " + playerList);
}
}
return true;
}
}

View File

@ -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<String> errors = new ArrayList<String>();
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;
}
}

View File

@ -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> T GetJsonObjectFromFile(Class<T> 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");
}
}
}

View File

@ -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<String> names = new ArrayList<String>();
}
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<String> ListNames() {
return internalFormat.names;
}
}

View File

@ -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 <player_name>
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

View File

@ -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 );
}
}