Compare commits

..

2 Commits

Author SHA1 Message Date
524510ae14 Refactored: Config, SettingsCommand 2023-07-13 01:05:09 +02:00
83412fd574 Refactoring 2023-07-13 00:09:18 +02:00
15 changed files with 295 additions and 242 deletions

View File

@ -6,7 +6,7 @@ import hu.ditservices.handlers.TabHandler;
import hu.ditservices.utils.*;
import hu.ditservices.utils.Math;
import org.bstats.bukkit.Metrics;
import hu.ditservices.commands.DitCmd;
import hu.ditservices.handlers.CommandHandler;
import hu.ditservices.listeners.ChatEvents;
import hu.ditservices.listeners.LogChat;
import hu.ditservices.listeners.LogCommand;
@ -34,52 +34,52 @@ 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;
@Override
public void onEnable() {
instance = this;
try {
this.Init();
} catch (Exception e) {
this.log.warning("[SimplifyTools] - INITIALIZATION ERROR: "+e.getMessage());
this.log.warning("[SimplifyTools] - Plugin disabled!");
this.setEnabled(false);
return;
}
if (this.initPlugin()) {
this.log.info(ChatColor.stripColor(this.getPrefix())+"Started running.");
}
private void Init() throws Exception {
this.ServerStartTime = ManagementFactory.getRuntimeMXBean().getStartTime();
if (Version.ServerVersion.isCurrentLower(Version.ServerVersion.v1_12_R1)){
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()){
@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();
}
TabHandler tab = new TabHandler();
PluginCommand ditCmd = this.getCommand("st");
ditCmd.setExecutor(new DitCmd(this));
ditCmd.setTabCompleter(new DITTabCompleter());
private void registerEvents() {
getServer().getPluginManager().registerEvents(this, this);
getServer().getPluginManager().registerEvents(new LogChat(this), this);
getServer().getPluginManager().registerEvents(new LogCommand(this), this);
getServer().getPluginManager().registerEvents(new LogConnect(this), this);
if (this.config.isSet("CustomAdvancement.enabled") && this.config.getBoolean("CustomAdvancement.enabled")){
if (this.config.isSet("CustomAdvancement.enabled") && this.config.getBoolean("CustomAdvancement.enabled")) {
getServer().getPluginManager().registerEvents(new ChatEvents(this), this);
this.log.info(ChatColor.stripColor(this.getPrefix()) + "Custom Advancement Messages enabled!");
}
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new TPS(), 0, 1);
if (this.config.getBoolean("Saving.enabled") && this.config.getInt("Saving.interval")>0){
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new SaveHandler(), 0L, Math.convert(Math.Convert.SECONDS,Math.Convert.TICKS,instance.config.getInt("Saving.interval")));
}
private void scheduleTasks() {
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new TPS(), 0, 1);
if (this.config.getBoolean("Saving.enabled") && this.config.getInt("Saving.interval") > 0) {
long intervalTicks = Math.convert(Math.Convert.SECONDS, Math.Convert.TICKS, instance.config.getInt("Saving.interval"));
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new SaveHandler(), 0L, intervalTicks);
}
}
private void initUpdateChecker() {
new UpdateChecker(this, UpdateCheckSource.GITHUB_RELEASE_TAG, "LabodiDavid/SimplifyTools")
.setDownloadLink("https://github.com/LabodiDavid/SimplifyTools/releases")
.setDonationLink("https://paypal.me/labodidavid")
@ -89,53 +89,98 @@ public final class STPlugin extends JavaPlugin implements CommandExecutor, Liste
.setUserAgent(new UserAgentBuilder().addPluginNameAndVersion())
.checkEveryXHours(24)
.checkNow();
}
private void initMetrics() {
Metrics metrics = new Metrics(this, 15108);
}
private boolean initPlugin() {
try {
this.ServerStartTime = ManagementFactory.getRuntimeMXBean().getStartTime();
if (Version.ServerVersion.isCurrentLower(Version.ServerVersion.v1_12_R1) ||
Version.ServerVersion.isCurrentHigher(Version.ServerVersion.v1_20_R1))
{
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()) {
TabHandler tab = new TabHandler();
PluginCommand stCommand = this.getCommand("st");
stCommand.setExecutor(new CommandHandler(this));
stCommand.setTabCompleter(new DITTabCompleter());
registerEvents();
scheduleTasks();
initUpdateChecker();
initMetrics();
return true;
} else {
return false;
}
} catch (Exception e) {
this.log.warning("[SimplifyTools] - INITIALIZATION ERROR: " + e.getMessage());
this.log.warning("[SimplifyTools] - Plugin disabled!");
this.setEnabled(false);
return false;
}
}
/**
* Gets the plugin's prefix.
*
* @return String
*/
public String getPrefix(){
if (this.config.isSet("Prefix") && !Objects.requireNonNull(this.config.getString("Prefix")).isEmpty()){
if (this.config.isSet("Prefix") && !Objects.requireNonNull(this.config.getString("Prefix")).isEmpty()) {
return ChatColor.translateAlternateColorCodes('&', this.config.getString("Prefix"));
}else{
return ChatColor.translateAlternateColorCodes('&',"&a[&fSimplify&7Tools&2] &4- &f");
}
}
/**
* Returns the main plugin instance.
*
* @return STPlugin
*/
public static STPlugin getInstance(){
return instance;
}
public static String getUptime(){
int uptime = (int)(System.currentTimeMillis() - instance.ServerStartTime)/1000;
String returnText = "";
/**
* Gets the server's uptime in a human readable format.
*
* @return String
*/
public static String getUptime() {
long uptime = (System.currentTimeMillis() - instance.ServerStartTime) / 1000;
StringBuilder returnText = new StringBuilder();
// Get remaining seconds from total minutes.
int seconds = uptime % 60;
// Convert to minutes, get remaining minutes from total hours.
int minutes = (uptime / 60) % 60;
// Convert to hours, get remaining hours from total days.
int hours = (uptime / 3600) % 24;
// Convert to days.
int days = uptime / 86400;
int days = (int) (uptime / 86400);
int hours = (int) ((uptime % 86400) / 3600);
int minutes = (int) ((uptime % 3600) / 60);
int seconds = (int) (uptime % 60);
if (days>1){
returnText = returnText+ days+" days ";
if (days > 1) {
returnText.append(days).append(" days ");
}
if (hours>1){
returnText = returnText+ hours+ " hours ";
if (hours > 1) {
returnText.append(hours).append(" hours ");
}
if (minutes>1){
returnText = returnText+ minutes+ " min ";
if (minutes > 1) {
returnText.append(minutes).append(" min ");
}
if (seconds>1){
returnText = returnText+ seconds+ "s ";
if (seconds > 1) {
returnText.append(seconds).append("s ");
}
return returnText;
return returnText.toString();
}
public boolean Reload(){
public boolean reload(){
File configFile = new File(getDataFolder(), "config.yml");
try {
@ -153,6 +198,6 @@ public final class STPlugin extends JavaPlugin implements CommandExecutor, Liste
}
@Override
public void onDisable() {
this.log.info(ChatColor.stripColor(this.getPrefix()) + "Started running.");
this.log.info(ChatColor.stripColor(this.getPrefix()) + "Stopped running.");
}
}

View File

@ -1,128 +0,0 @@
package hu.ditservices.commands;
import hu.ditservices.utils.TPS;
import hu.ditservices.STPlugin;
import hu.ditservices.utils.Cooldown;
import hu.ditservices.utils.Version;
import org.bukkit.ChatColor;
import org.bukkit.command.*;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
public class DitCmd implements CommandExecutor {
private final String noArgMsg;
private final STPlugin plugin;
private final Cooldown cd;
private final FileConfiguration config;
public DitCmd(final STPlugin instance){
this.plugin = instance;
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;
}
public boolean addToCoolDown(CommandSender sender){
if (sender instanceof Player){
Player p = (Player) sender;
this.cd.Add(p);
return true;
}
return false;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equals("st")){
if (cd.Check(sender)){
if (command.getName().equals("st") && args.length==0)
{
this.addToCoolDown(sender);
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(this.noArgMsg);
return true;
}
if (command.getName().equals("st") && args[0].contains("settings"))
{
this.addToCoolDown(sender);
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;
}
if (command.getName().equals("st") && args[0].contains("reload") && sender.hasPermission("st.reload")){
if(plugin.Reload()){
this.addToCoolDown(sender);
sender.sendMessage(plugin.getPrefix()+ChatColor.GREEN+"Successfully reload!");
sender.sendMessage(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")){
this.addToCoolDown(sender);
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 (sender.hasPermission("st.pmanager.unload") || sender.hasPermission("st.pmanager.load") || sender.hasPermission("st.pmanager")) {
this.addToCoolDown(sender);
if (args.length==1){
sender.sendMessage(plugin.getPrefix()+ChatColor.DARK_RED+"Invalid arguments!");
return true;
}
if (config.getBoolean("PluginManager.enabled")) {
if (args[1].equalsIgnoreCase("load")) {
//PluginCmd.handleLoad(sender,args);
PluginCmd.LoadPlugin(sender,args);
}
if (args[1].equalsIgnoreCase("unload")) {
//PluginCmd.handleUnload(sender,args);
PluginCmd.UnloadPlugin(sender, args);
}
} else {
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")){
this.addToCoolDown(sender);
return SaveCmd.Run();
}
if (command.getName().equalsIgnoreCase("st") && args[0].contains("ping") && sender.hasPermission("st.ping")){
this.addToCoolDown(sender);
return PingCmd.Run(sender);
}
if (command.getName().equalsIgnoreCase("st") && args[0].contains("stats") && sender.hasPermission("st.stats")){
this.addToCoolDown(sender);
return StatCmd.Run(sender);
}
}else{
cd.CDText(sender);
}
}
return true;
}
}

View File

@ -6,7 +6,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
public class PingCmd {
public class PingCommand {
public static boolean Run(CommandSender sender) {
STPlugin plugin = STPlugin.getInstance();
if (sender instanceof Player) {

View File

@ -7,7 +7,7 @@ import hu.ditservices.STPlugin;
import org.bukkit.command.CommandSender;
public class PluginCmd {
public class PluginManagerCommand {
public static boolean LoadPlugin(CommandSender sender, String[] args){
Plugin plugin = Bukkit.getPluginManager().getPlugin(args[2]);
if (plugin == null) {

View File

@ -4,7 +4,7 @@ import hu.ditservices.STPlugin;
import org.bukkit.Bukkit;
import org.bukkit.World;
public class SaveCmd {
public class SaveCommand {
/**
* Saves all worlds and players data.
*
@ -12,8 +12,8 @@ public class SaveCmd {
*/
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

@ -6,7 +6,7 @@ import org.bukkit.Statistic;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class StatCmd {
public class StatCommand {
public static boolean Run(CommandSender sender){
Player player = (Player) sender;

View File

@ -0,0 +1,113 @@
package hu.ditservices.handlers;
import hu.ditservices.commands.*;
import hu.ditservices.utils.TPS;
import hu.ditservices.STPlugin;
import hu.ditservices.utils.Cooldown;
import hu.ditservices.utils.Version;
import org.bukkit.ChatColor;
import org.bukkit.command.*;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
public class CommandHandler implements CommandExecutor {
private final String noArgMsg;
private final STPlugin plugin;
private final Cooldown cd;
private final FileConfiguration config;
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.cd = new Cooldown(plugin);
this.config = plugin.getConfig();
}
public boolean addToCoolDown(CommandSender sender) {
if (sender instanceof Player) {
Player p = (Player) sender;
this.cd.Add(p);
return true;
}
return false;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equals("st")){
if (cd.Check(sender)){
if (command.getName().equals("st")) {
this.addToCoolDown(sender);
}
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("settings"))
{
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!" + "\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()));
return true;
}
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!");
return true;
}
if (config.getBoolean("PluginManager.enabled")) {
if (args[1].equalsIgnoreCase("load")) {
//PluginCmd.handleLoad(sender,args);
PluginManagerCommand.LoadPlugin(sender,args);
}
if (args[1].equalsIgnoreCase("unload")) {
//PluginCmd.handleUnload(sender,args);
PluginManagerCommand.UnloadPlugin(sender, args);
}
} else {
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")) {
return SaveCommand.Run();
}
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")) {
return StatCommand.Run(sender);
}
}else{
cd.CDText(sender);
}
}
return true;
}
}

View File

@ -1,10 +1,10 @@
package hu.ditservices.handlers;
import hu.ditservices.commands.SaveCmd;
import hu.ditservices.commands.SaveCommand;
public class SaveHandler implements Runnable{
@Override
public void run() {
SaveCmd.Run();
SaveCommand.Run();
}
}

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,10 +19,10 @@ public class LogConnect implements Listener {
FileConfiguration config;
public LogConnect(STPlugin instance){
this.plugin = instance;
this.config = plugin.config;
if (config.getBoolean("CustomMsg.enabled")){
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} leaved the game.";
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")){