Refactored: Config, SettingsCommand

This commit is contained in:
LabodiDavid 2023-07-13 01:05:09 +02:00
parent 83412fd574
commit 524510ae14
9 changed files with 62 additions and 44 deletions

View File

@ -34,7 +34,7 @@ public final class STPlugin extends JavaPlugin implements CommandExecutor, Liste
private static STPlugin instance;
private final Logger log = Bukkit.getLogger();
public FileConfiguration config = getConfig();
private FileConfiguration config;
public long ServerStartTime;
@ -47,6 +47,16 @@ public final class STPlugin extends JavaPlugin implements CommandExecutor, Liste
}
}
@Override
public FileConfiguration getConfig() {
//TODO Implement defaults everywhere when config is not loaded for some reasons
if (this.config != null) {
return this.config;
}
return super.getConfig();
}
private void registerEvents() {
getServer().getPluginManager().registerEvents(this, this);
@ -94,7 +104,7 @@ public final class STPlugin extends JavaPlugin implements CommandExecutor, Liste
throw new Exception("The server version is not supported! Update to a version between 1.12 - 1.20.1 to run this plugin.");
}
if (this.Reload()) {
if (this.reload()) {
TabHandler tab = new TabHandler();
PluginCommand stCommand = this.getCommand("st");
@ -170,7 +180,7 @@ public final class STPlugin extends JavaPlugin implements CommandExecutor, Liste
return returnText.toString();
}
public boolean Reload(){
public boolean reload(){
File configFile = new File(getDataFolder(), "config.yml");
try {

View File

@ -12,8 +12,8 @@ public class SaveCommand {
*/
public static boolean Run(){
STPlugin plugin = STPlugin.getInstance();
String p = plugin.config.getString("Saving.broadcastMsgProgress").replace("{PREFIX}",plugin.getPrefix());
String d = plugin.config.getString("Saving.broadcastMsgDone").replace("{PREFIX}",plugin.getPrefix());
String p = plugin.getConfig().getString("Saving.broadcastMsgProgress").replace("{PREFIX}",plugin.getPrefix());
String d = plugin.getConfig().getString("Saving.broadcastMsgDone").replace("{PREFIX}",plugin.getPrefix());
Bukkit.broadcast(p,"st.st");
for(World w : Bukkit.getServer().getWorlds()){
w.save();

View File

@ -0,0 +1,23 @@
package hu.ditservices.commands;
import hu.ditservices.STPlugin;
import hu.ditservices.utils.Version;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
public class SettingsCommand {
public static boolean Run(CommandSender sender) {
STPlugin plugin = STPlugin.getInstance();
sender.sendMessage( ChatColor.GREEN+" === Plugin Information === " + "\n"
+ ChatColor.GREEN+"Plugin Version: " + ChatColor.translateAlternateColorCodes('&',"&a[&fSimplify&7Tools&2] &4- &f") + plugin.getDescription().getVersion() + "\n"
+ ChatColor.GREEN+"Server Version: " + Version.ServerVersion.getCurrent().toString() + "\n"
+ ChatColor.GREEN+" -------- Features -------- " + "\n"
+ ChatColor.GREEN+"Tab customization: "+(plugin.getConfig().getBoolean("Tab.enabled") ? ChatColor.GREEN + "Enabled" : ChatColor.RED+"Disabled") + "\n"
+ ChatColor.GREEN+"Custom Advancement Msg: " + (plugin.getConfig().getBoolean("CustomAdvancement.enabled") ? ChatColor.GREEN+"Enabled" : ChatColor.RED+"Disabled") + "\n"
+ ChatColor.GREEN+" ========================== "
);
return true;
}
}

View File

@ -1,9 +1,6 @@
package hu.ditservices.handlers;
import hu.ditservices.commands.PingCommand;
import hu.ditservices.commands.PluginManagerCommand;
import hu.ditservices.commands.SaveCommand;
import hu.ditservices.commands.StatCommand;
import hu.ditservices.commands.*;
import hu.ditservices.utils.TPS;
import hu.ditservices.STPlugin;
import hu.ditservices.utils.Cooldown;
@ -21,13 +18,13 @@ public class CommandHandler implements CommandExecutor {
public CommandHandler(final STPlugin instance) {
this.plugin = instance;
this.noArgMsg = plugin.getPrefix()+ ChatColor.DARK_RED + "To list all SimplifyTools commands use the '/help SIMPLIFYTOOLS' command!";
this.noArgMsg = plugin.getPrefix() + ChatColor.DARK_RED + "To list all SimplifyTools commands use the '/help SIMPLIFYTOOLS' command!";
this.cd = new Cooldown(plugin);
this.config = plugin.config;
this.config = plugin.getConfig();
}
public boolean addToCoolDown(CommandSender sender) {
if (sender instanceof Player){
if (sender instanceof Player) {
Player p = (Player) sender;
this.cd.Add(p);
return true;
@ -45,49 +42,37 @@ public class CommandHandler implements CommandExecutor {
this.addToCoolDown(sender);
}
if (command.getName().equals("st") && args.length==0)
if (command.getName().equals("st") && (args.length == 0 || args[0].contains("help")))
{
sender.sendMessage(plugin.getPrefix()+ChatColor.GREEN+"Version: "+plugin.getDescription().getVersion());
sender.sendMessage(this.noArgMsg);
return true;
}
if (command.getName().equals("st") && args[0].contains("help"))
{
sender.sendMessage(plugin.getPrefix()+ChatColor.GREEN+"Version: "+plugin.getDescription().getVersion());
sender.sendMessage(plugin.getPrefix() + ChatColor.GREEN+"Version: " + plugin.getDescription().getVersion());
sender.sendMessage(this.noArgMsg);
return true;
}
if (command.getName().equals("st") && args[0].contains("settings"))
{
sender.sendMessage(plugin.getPrefix()+ ChatColor.GREEN+" === Plugin Information === ");
sender.sendMessage(plugin.getPrefix()+ChatColor.GREEN+"Plugin Version: "+plugin.getDescription().getVersion());
sender.sendMessage(plugin.getPrefix()+ChatColor.GREEN+"Server Version: "+ Version.ServerVersion.getCurrent().toString());
sender.sendMessage(ChatColor.GREEN+" -------- Features -------- ");
sender.sendMessage(plugin.getPrefix()+ChatColor.GREEN+"Tab customization: "+(config.getBoolean("Tab.enabled") ? ChatColor.GREEN+"Enabled" : ChatColor.RED+"Disabled"));
sender.sendMessage(plugin.getPrefix()+ChatColor.GREEN+"Custom Advancement Msg: "+(config.getBoolean("CustomAdvancement.enabled") ? ChatColor.GREEN+"Enabled" : ChatColor.RED+"Disabled"));
sender.sendMessage(ChatColor.GREEN+" ========================== ");
return true;
return SettingsCommand.Run(sender);
}
if (command.getName().equals("st") && args[0].contains("reload") && sender.hasPermission("st.reload")){
if(plugin.Reload()){
sender.sendMessage(plugin.getPrefix()+ChatColor.GREEN+"Successfully reload!");
sender.sendMessage(plugin.getPrefix()+ChatColor.RED+"Notice: Restart your server if the settings didn't applied.");
if(plugin.reload()){
sender.sendMessage(plugin.getPrefix()+ChatColor.GREEN+"Successfully reload!" + "\n"
+ plugin.getPrefix() + ChatColor.RED + "Notice: Restart your server if the settings didn't applied.");
return true;
}
}
if (command.getName().equals("st") && args[0].contains("tps") && sender.hasPermission("st.tps")){
sender.sendMessage(plugin.getPrefix()+ChatColor.GREEN+"Plugin Calculated TPS: "+TPS.getColor()+String.format("%.2f", TPS.getTPS()));
if (command.getName().equals("st") && args[0].contains("tps") && sender.hasPermission("st.tps")) {
sender.sendMessage(plugin.getPrefix() + ChatColor.GREEN+"Plugin Calculated TPS: "+TPS.getColor()+String.format("%.2f", TPS.getTPS()));
return true;
}
if (command.getName().equalsIgnoreCase("st") && args[0].contains("pmanager")){
if (command.getName().equalsIgnoreCase("st") && args[0].contains("pmanager")) {
if (sender.hasPermission("st.pmanager.unload") || sender.hasPermission("st.pmanager.load") || sender.hasPermission("st.pmanager")) {
if (args.length==1){
sender.sendMessage(plugin.getPrefix()+ChatColor.DARK_RED+"Invalid arguments!");
sender.sendMessage(plugin.getPrefix() + ChatColor.DARK_RED+"Invalid arguments!");
return true;
}
if (config.getBoolean("PluginManager.enabled")) {
@ -100,21 +85,21 @@ public class CommandHandler implements CommandExecutor {
PluginManagerCommand.UnloadPlugin(sender, args);
}
} else {
sender.sendMessage(plugin.getPrefix()+ChatColor.DARK_RED+"Plugin manager commands are disabled in the config!");
sender.sendMessage(plugin.getPrefix() + ChatColor.DARK_RED + "Plugin manager commands are disabled in the config!");
return true;
}
}
}
if (command.getName().equalsIgnoreCase("st") && args[0].contains("save-all") && sender.hasPermission("st.save")){
if (command.getName().equalsIgnoreCase("st") && args[0].contains("save-all") && sender.hasPermission("st.save")) {
return SaveCommand.Run();
}
if (command.getName().equalsIgnoreCase("st") && args[0].contains("ping") && sender.hasPermission("st.ping")){
if (command.getName().equalsIgnoreCase("st") && args[0].contains("ping") && sender.hasPermission("st.ping")) {
return PingCommand.Run(sender);
}
if (command.getName().equalsIgnoreCase("st") && args[0].contains("stats") && sender.hasPermission("st.stats")){
if (command.getName().equalsIgnoreCase("st") && args[0].contains("stats") && sender.hasPermission("st.stats")) {
return StatCommand.Run(sender);
}
}else{

View File

@ -17,7 +17,7 @@ public class ChatEvents implements Listener {
public ChatEvents(STPlugin instance){
this.plugin = instance;
this.config = plugin.config;
this.config = plugin.getConfig();
}
@EventHandler(priority = EventPriority.MONITOR)

View File

@ -15,7 +15,7 @@ public class LogChat implements Listener {
FileConfiguration config;
public LogChat(STPlugin instance){
this.plugin = instance;
this.config = plugin.config;
this.config = plugin.getConfig();
}
@EventHandler(priority = EventPriority.MONITOR)
public void onChat(AsyncPlayerChatEvent event){

View File

@ -16,7 +16,7 @@ public class LogCommand implements Listener {
public LogCommand(STPlugin instance){
this.plugin = instance;
this.config = plugin.config;
this.config = plugin.getConfig();
}
@EventHandler(priority = EventPriority.MONITOR)
public void onCommand(PlayerCommandPreprocessEvent event){

View File

@ -19,7 +19,7 @@ public class LogConnect implements Listener {
FileConfiguration config;
public LogConnect(STPlugin instance){
this.plugin = instance;
this.config = plugin.config;
this.config = plugin.getConfig();
if (config.getBoolean("CustomMsg.enabled")) {
this.JoinMsg = this.config.isSet("CustomMsg.connect") ? this.config.getString("CustomMsg.connect") : "{PREFIX}{NAME} &aconnected to the server.";
this.LeaveMsg = this.config.isSet("CustomMsg.connect") ? this.config.getString("CustomMsg.disconnect") : "{PREFIX}{NAME} left the game.";

View File

@ -19,7 +19,7 @@ public class Cooldown {
public Cooldown(STPlugin instance){
this.plugin = instance;
this.config = plugin.config;
this.config = plugin.getConfig();
if (config.isSet("Cooldown.seconds")){
int cd = this.delay;
if (cd!=config.getInt("Cooldown.seconds")){