add custom module config class

fix npe in tab listener
add getter for indefinite bans
fix npe in plex cmd
remove spring boot and add jetty
This commit is contained in:
Taah 2022-04-01 20:58:03 -07:00
parent 099680d068
commit 1f2eed78de
6 changed files with 189 additions and 23 deletions

View File

@ -37,7 +37,9 @@ dependencies {
library "org.apache.maven.resolver:maven-resolver-connector-basic:1.7.3"
library "org.apache.maven.resolver:maven-resolver-transport-http:1.7.3"
library "org.apache.maven:maven-resolver-provider:3.8.5"
library "org.springframework.boot:spring-boot-starter-web:2.6.6"
library "org.eclipse.jetty:jetty-server:11.0.8"
library "org.eclipse.jetty:jetty-servlet:11.0.8"
library "org.eclipse.jetty:jetty-proxy:11.0.8"
compileOnly "io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT"
implementation "org.bstats:bstats-base:3.0.0"
implementation "org.bstats:bstats-bukkit:3.0.0"

View File

@ -51,7 +51,7 @@ public class PlexCMD extends PlexCommand
plugin.getRankManager().importDefaultRanks();
send(sender, "Imported ranks");
send(sender, "Plex successfully reloaded.");
plugin.setSystem(plugin.config.getString("commands.permissions"));
plugin.setSystem(plugin.config.getString("system"));
return null;
}
else if (args[0].equalsIgnoreCase("redis"))

View File

@ -0,0 +1,145 @@
package dev.plex.config;
import dev.plex.module.PlexModule;
import dev.plex.util.PlexLog;
import org.apache.logging.log4j.Level;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
/**
* Creates a custom Config object
*/
public class ModuleConfig extends YamlConfiguration {
/**
* The plugin instance
*/
private PlexModule module;
/**
* The File instance
*/
private File file;
/**
* The file name
*/
private String name;
/**
* Whether new entries were added to the file automatically
*/
private boolean added = false;
/**
* Creates a config object
*
* @param module The module instance
* @param name The file name
*/
public ModuleConfig(PlexModule module, String name) {
this.module = module;
this.file = new File(module.getDataFolder(), name);
this.name = name;
if (!file.exists()) {
saveDefault();
}
}
public void load() {
this.load(true);
}
/**
* Loads the configuration file
*/
public void load(boolean loadFromFile) {
try {
if (loadFromFile) {
YamlConfiguration externalYamlConfig = YamlConfiguration.loadConfiguration(file);
InputStreamReader internalConfigFileStream = new InputStreamReader(module.getResource(name), StandardCharsets.UTF_8);
YamlConfiguration internalYamlConfig = YamlConfiguration.loadConfiguration(internalConfigFileStream);
// Gets all the keys inside the internal file and iterates through all of it's key pairs
for (String string : internalYamlConfig.getKeys(true)) {
// Checks if the external file contains the key already.
if (!externalYamlConfig.contains(string)) {
// If it doesn't contain the key, we set the key based off what was found inside the plugin jar
externalYamlConfig.setComments(string, internalYamlConfig.getComments(string));
externalYamlConfig.set(string, internalYamlConfig.get(string));
PlexLog.log("Setting key: " + string + " in " + this.name + " to the default value(s) since it does not exist!");
added = true;
}
}
if (added) {
externalYamlConfig.save(file);
PlexLog.log("Saving new file...");
added = false;
}
}
super.load(file);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Saves the configuration file
*/
public void save() {
try {
super.save(file);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Moves the configuration file from the plugin's resources folder to the data folder (plugins/Plex/)
*/
private void saveDefault() {
try {
Files.copy(module.getClass().getResourceAsStream("/" + name), this.file.toPath());
} catch (IOException e) {
e.printStackTrace();
}
/*if (name == null || name.equals("")) {
throw new IllegalArgumentException("ResourcePath cannot be null or empty");
}
name = name.replace('\\', '/');
InputStream in = module.getResource("/" + name);
if (in == null) {
throw new IllegalArgumentException("The embedded resource '" + name + "'");
}
File outFile = new File(module.getDataFolder(), name);
int lastIndex = name.lastIndexOf('/');
File outDir = new File(module.getDataFolder(), name.substring(0, lastIndex >= 0 ? lastIndex : 0));
if (!outDir.exists()) {
outDir.mkdirs();
}
try {
if (!outFile.exists()) {
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
} else {
module.getLogger().log(org.apache.logging.log4j.Level.INFO, "Could not save " + outFile.getName() + " to " + outFile + " because " + outFile.getName() + " already exists.");
}
} catch (IOException ex) {
module.getLogger().log(Level.ERROR, "Could not save " + outFile.getName() + " to " + outFile, ex);
}*/
}
}

View File

@ -27,6 +27,7 @@ public class TabListener extends PlexListener
{
PlexPlayer plexPlayer = event.getPlexPlayer();
Player player = event.getPlexPlayer().getPlayer();
if (player == null) return;
player.playerListName(Component.text(player.getName()).color(plugin.getRankManager().getColor(plexPlayer)));
}
@ -35,6 +36,7 @@ public class TabListener extends PlexListener
{
PlexPlayer plexPlayer = event.getPlexPlayer();
Player player = event.getPlexPlayer().getPlayer();
if (player == null) return;
player.playerListName(Component.text(player.getName()).color(plugin.getRankManager().getColor(plexPlayer)));
}
@ -43,6 +45,7 @@ public class TabListener extends PlexListener
{
PlexPlayer plexPlayer = event.getPlexPlayer();
Player player = event.getPlexPlayer().getPlayer();
if (player == null) return;
player.playerListName(Component.text(player.getName()).color(plugin.getRankManager().getColor(plexPlayer)));
}
}

View File

@ -4,19 +4,25 @@ import com.google.common.collect.Lists;
import dev.plex.Plex;
import dev.plex.command.PlexCommand;
import dev.plex.listener.PlexListener;
import java.io.File;
import java.util.List;
import java.util.Locale;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.apache.logging.log4j.Logger;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Locale;
@Getter
@Setter(AccessLevel.MODULE)
public abstract class PlexModule
{
public abstract class PlexModule {
@Getter(AccessLevel.MODULE)
private final List<PlexCommand> commands = Lists.newArrayList();
@ -28,41 +34,49 @@ public abstract class PlexModule
private File dataFolder;
private Logger logger;
public void load()
{
public void load() {
}
public void enable()
{
public void enable() {
}
public void disable()
{
public void disable() {
}
public void registerListener(PlexListener listener)
{
public void registerListener(PlexListener listener) {
listeners.add(listener);
}
public void unregisterListener(PlexListener listener)
{
public void unregisterListener(PlexListener listener) {
listeners.remove(listener);
HandlerList.unregisterAll(listener);
}
public void registerCommand(PlexCommand command)
{
public void registerCommand(PlexCommand command) {
commands.add(command);
}
public void unregisterCommand(PlexCommand command)
{
public void unregisterCommand(PlexCommand command) {
commands.remove(command);
}
public PlexCommand getCommand(String name)
{
public PlexCommand getCommand(String name) {
return commands.stream().filter(plexCommand -> plexCommand.getName().equalsIgnoreCase(name) || plexCommand.getAliases().stream().map(String::toLowerCase).toList().contains(name.toLowerCase(Locale.ROOT))).findFirst().orElse(null);
}
@Nullable
public InputStream getResource(@NotNull String filename) {
try {
URL url = this.getClass().getClassLoader().getResource(filename);
if (url == null) {
return null;
}
URLConnection connection = url.openConnection();
connection.setUseCaches(false);
return connection.getInputStream();
} catch (IOException ex) {
return null;
}
}
}

View File

@ -23,6 +23,7 @@ import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.Getter;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable;
@ -32,6 +33,7 @@ import redis.clients.jedis.Jedis;
public class PunishmentManager extends PlexBase
{
@Getter
private final List<IndefiniteBan> indefiniteBans = Lists.newArrayList();
public void mergeIndefiniteBans()