performance & ram optimizations

This commit is contained in:
Bea 2020-05-27 00:41:02 +02:00
parent bd5b8be487
commit bb5a90b390
21 changed files with 64 additions and 133 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea/

View File

@ -7,12 +7,13 @@ import net.mindoverflow.hubthat.listeners.PlayerChatListener;
import net.mindoverflow.hubthat.listeners.PlayerJoinListener; import net.mindoverflow.hubthat.listeners.PlayerJoinListener;
import net.mindoverflow.hubthat.listeners.PlayerMoveListener; import net.mindoverflow.hubthat.listeners.PlayerMoveListener;
import net.mindoverflow.hubthat.listeners.PlayerRespawnListener; import net.mindoverflow.hubthat.listeners.PlayerRespawnListener;
import net.mindoverflow.hubthat.utils.ConfigEntries;
import net.mindoverflow.hubthat.utils.Debugger;
import net.mindoverflow.hubthat.utils.TeleportUtils;
import net.mindoverflow.hubthat.utils.files.FileUtils; import net.mindoverflow.hubthat.utils.files.FileUtils;
import net.mindoverflow.hubthat.utils.files.OldConfigConversion; import net.mindoverflow.hubthat.utils.files.OldConfigConversion;
import net.mindoverflow.hubthat.utils.statistics.Metrics; import net.mindoverflow.hubthat.utils.statistics.Metrics;
import net.mindoverflow.hubthat.utils.statistics.UpdateChecker; import net.mindoverflow.hubthat.utils.statistics.UpdateChecker;
import net.mindoverflow.hubthat.utils.*;
import org.bukkit.World;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -23,12 +24,15 @@ import java.util.logging.Logger;
public class HubThat extends JavaPlugin public class HubThat extends JavaPlugin
{ {
// Instantiate a Debugger for this class. // Instantiate a Debugger for this class.
private Debugger debugger = new Debugger(getClass().getName()); private final Debugger debugger = new Debugger(getClass().getName());
// Initializing needed variables. // Initializing needed variables.
public static Logger logger; public static Logger logger;
private PluginManager pluginManager; private PluginManager pluginManager;
public UpdateChecker updateChecker; public UpdateChecker updateChecker;
private static HubThat instance;
public HubThat() { instance = this; }
// Method called when the plugin is being loaded. // Method called when the plugin is being loaded.
@Override @Override
@ -49,24 +53,21 @@ public class HubThat extends JavaPlugin
FileUtils fileUtilsInstance = new FileUtils(this); FileUtils fileUtilsInstance = new FileUtils(this);
HubThatCommand hubThatCommandInstance = new HubThatCommand(this); HubThatCommand hubThatCommandInstance = new HubThatCommand(this);
HubCommand hubCommandInstance = new HubCommand(this); HubCommand hubCommandInstance = new HubCommand(this);
SetHubCommand setHubCommandInstance = new SetHubCommand(this);
SpawnCommand spawnCommandInstance = new SpawnCommand(this); SpawnCommand spawnCommandInstance = new SpawnCommand(this);
SetSpawnCommand setSpawnCommandInstance = new SetSpawnCommand(this); SetSpawnCommand setSpawnCommandInstance = new SetSpawnCommand(this);
WorldListCommand worldListCommandInstance = new WorldListCommand(this); WorldListCommand worldListCommandInstance = new WorldListCommand(this);
WorldTpCommand worldTpCommandInstance = new WorldTpCommand(this); WorldTpCommand worldTpCommandInstance = new WorldTpCommand(this);
UpdateChecker updateCheckerInstance = new UpdateChecker(this);
// We need to instantiate Utils classes because they need to access plugin data and server. // We need to instantiate Utils classes because they need to access plugin data and server.
PermissionUtils permissionUtilsInstance = new PermissionUtils(this);
TeleportUtils teleportUtilsInstance = new TeleportUtils(this); TeleportUtils teleportUtilsInstance = new TeleportUtils(this);
MessageUtils messageUtilsInstance = new MessageUtils(this); UpdateChecker updateCheckerInstance = new UpdateChecker(this);
updateChecker = new UpdateChecker(this); updateChecker = new UpdateChecker(this);
debugger.sendDebugMessage(Level.INFO, "Done instantiating classes!"); debugger.sendDebugMessage(Level.INFO, "Done instantiating classes!");
// Register Listeners // Register Listeners
debugger.sendDebugMessage(Level.INFO, "Registering listeners..."); debugger.sendDebugMessage(Level.INFO, "Registering listeners...");
pluginManager.registerEvents(new PlayerJoinListener(this), this); pluginManager.registerEvents(new PlayerJoinListener(this), this);
pluginManager.registerEvents(new PlayerMoveListener(this), this); pluginManager.registerEvents(new PlayerMoveListener(), this);
pluginManager.registerEvents(new PlayerChatListener(this), this); pluginManager.registerEvents(new PlayerChatListener(this), this);
pluginManager.registerEvents(new PlayerRespawnListener(this), this); pluginManager.registerEvents(new PlayerRespawnListener(this), this);
@ -82,7 +83,7 @@ public class HubThat extends JavaPlugin
getCommand("hub").setExecutor(hubCommandInstance); getCommand("hub").setExecutor(hubCommandInstance);
getCommand("sethub").setExecutor(setHubCommandInstance); getCommand("sethub").setExecutor(new SetHubCommand());
getCommand("spawn").setExecutor(spawnCommandInstance); getCommand("spawn").setExecutor(spawnCommandInstance);
getCommand("spawn").setTabCompleter(new SpawnCompleter()); getCommand("spawn").setTabCompleter(new SpawnCompleter());
@ -111,37 +112,6 @@ public class HubThat extends JavaPlugin
FileUtils.reloadYamls(); FileUtils.reloadYamls();
debugger.sendDebugMessage(Level.INFO, "Done!"); debugger.sendDebugMessage(Level.INFO, "Done!");
// Check for updates, if they are enabled.
if(FileUtils.FileType.CONFIG_YAML.yaml.getBoolean(ConfigEntries.UPDATE_CHECKER_ENABLED.path))
{
debugger.sendDebugMessage(Level.INFO, "Update checking is enabled.");
// Start the update checking delayed job. It will handle checking updates, storing variables and telling the console.
debugger.sendDebugMessage(Level.INFO, "Running task (via Main).");
//UpdateChecker.runTimer();
// Check if the links are valid.
/*debugger.sendDebugMessage(Level.INFO, "Checking if links are valid via Main.");
if(updateCheckerInstance.linksValid())
{
debugger.sendDebugMessage(Level.INFO, "Links are valid.");
// Check if the update is needed (if newest version is different from current version).
// We need to surround it with try/catch because it may throw a IOException,
try
{
debugger.sendDebugMessage(Level.INFO, "Checking updates are needed via Main.");
updateCheckerInstance.checkUpdates();
} catch (IOException e)
{
e.printStackTrace();
}
}
else
{ // If the links are not valid... (eg: server/internet is offline)
// Log it to the console.
logger.log(Level.SEVERE, "There's a problem with the updates server.");
logger.log(Level.SEVERE, "Please check if there are any updates manually.");
}*/
}
debugger.sendDebugMessage(Level.INFO,"Setting up Metrics..."); debugger.sendDebugMessage(Level.INFO,"Setting up Metrics...");
setupMetrics(); setupMetrics();
debugger.sendDebugMessage(Level.INFO,"Done setting up Metrics!"); debugger.sendDebugMessage(Level.INFO,"Done setting up Metrics!");
@ -181,4 +151,8 @@ public class HubThat extends JavaPlugin
} }
} }
public static HubThat getInstance()
{
return instance;
}
} }

View File

@ -17,10 +17,10 @@ public class HubCommand implements CommandExecutor
// Initialize the debugger so I can debug the plugin. // Initialize the debugger so I can debug the plugin.
private static Debugger debugger = new Debugger(HubCommand.class.getName()); private static final Debugger debugger = new Debugger(HubCommand.class.getName());
// Initialize the plugin variable so we can access all of the plugin's data. // Initialize the plugin variable so we can access all of the plugin's data.
private static HubThat plugin; private final HubThat plugin;
// Constructor to actually give "plugin" a value. // Constructor to actually give "plugin" a value.
public HubCommand(HubThat givenPlugin) { plugin = givenPlugin; } public HubCommand(HubThat givenPlugin) { plugin = givenPlugin; }

View File

@ -5,11 +5,9 @@ import net.mindoverflow.hubthat.commands.hubthatcommands.HelpCommand;
import net.mindoverflow.hubthat.commands.hubthatcommands.ReloadCommand; import net.mindoverflow.hubthat.commands.hubthatcommands.ReloadCommand;
import net.mindoverflow.hubthat.utils.Debugger; import net.mindoverflow.hubthat.utils.Debugger;
import net.mindoverflow.hubthat.utils.MessageUtils; import net.mindoverflow.hubthat.utils.MessageUtils;
import net.mindoverflow.hubthat.utils.LocalizedMessages;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import java.util.logging.Level; import java.util.logging.Level;
@ -17,10 +15,10 @@ public class HubThatCommand implements CommandExecutor
{ {
// Initialize the plugin variable so we can access all of the plugin's data. // Initialize the plugin variable so we can access all of the plugin's data.
private HubThat plugin; private final HubThat plugin;
// Initialize the debugger so I can debug the plugin. // Initialize the debugger so I can debug the plugin.
private Debugger debugger = new Debugger(getClass().getName()); private final Debugger debugger = new Debugger(getClass().getName());
// Constructor to actually give "plugin" a value. // Constructor to actually give "plugin" a value.
public HubThatCommand(HubThat givenPlugin) public HubThatCommand(HubThat givenPlugin)

View File

@ -1,6 +1,5 @@
package net.mindoverflow.hubthat.commands; package net.mindoverflow.hubthat.commands;
import net.mindoverflow.hubthat.HubThat;
import net.mindoverflow.hubthat.utils.*; import net.mindoverflow.hubthat.utils.*;
import net.mindoverflow.hubthat.utils.files.FileUtils; import net.mindoverflow.hubthat.utils.files.FileUtils;
import org.bukkit.Location; import org.bukkit.Location;
@ -16,15 +15,7 @@ public class SetHubCommand implements CommandExecutor
{ {
// Initialize the debugger so I can debug the plugin. // Initialize the debugger so I can debug the plugin.
private Debugger debugger = new Debugger(getClass().getName()); private final Debugger debugger = new Debugger(getClass().getName());
// Initialize the plugin variable so we can access all of the plugin's data.
private HubThat plugin;
// Constructor to actually give "plugin" a value.
public SetHubCommand(HubThat givenPlugin) { plugin = givenPlugin; }
@Override @Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args)

View File

@ -18,11 +18,11 @@ public class SetSpawnCommand implements CommandExecutor
{ {
// Initialize the debugger so I can debug the plugin. // Initialize the debugger so I can debug the plugin.
private Debugger debugger = new Debugger(getClass().getName()); private final Debugger debugger = new Debugger(getClass().getName());
// Initialize the plugin variable so we can access all of the plugin's data. // Initialize the plugin variable so we can access all of the plugin's data.
private HubThat plugin; private final HubThat plugin;
// Constructor to actually give "plugin" a value. // Constructor to actually give "plugin" a value.

View File

@ -17,11 +17,11 @@ public class SpawnCommand implements CommandExecutor
{ {
// Initialize the debugger so I can debug the plugin. // Initialize the debugger so I can debug the plugin.
private static Debugger debugger = new Debugger(SpawnCommand.class.getName()); private static final Debugger debugger = new Debugger(SpawnCommand.class.getName());
// Initialize the plugin variable so we can access all of the plugin's data. // Initialize the plugin variable so we can access all of the plugin's data.
private static HubThat plugin; private final HubThat plugin;
// Constructor to actually give "plugin" a value. // Constructor to actually give "plugin" a value.
public SpawnCommand(HubThat givenPlugin) { plugin = givenPlugin; } public SpawnCommand(HubThat givenPlugin) { plugin = givenPlugin; }

View File

@ -15,11 +15,11 @@ public class WorldListCommand implements CommandExecutor
// Initialize the debugger so I can debug the plugin. // Initialize the debugger so I can debug the plugin.
private Debugger debugger = new Debugger(getClass().getName()); private final Debugger debugger = new Debugger(getClass().getName());
// Initialize the plugin variable so we can access all of the plugin's data. // Initialize the plugin variable so we can access all of the plugin's data.
private HubThat plugin; private final HubThat plugin;
// Constructor to actually give "plugin" a value. // Constructor to actually give "plugin" a value.
public WorldListCommand(HubThat givenPlugin) { plugin = givenPlugin; } public WorldListCommand(HubThat givenPlugin) { plugin = givenPlugin; }

View File

@ -16,11 +16,11 @@ public class WorldTpCommand implements CommandExecutor
{ {
// Initialize the debugger so I can debug the plugin. // Initialize the debugger so I can debug the plugin.
private Debugger debugger = new Debugger(getClass().getName()); private final Debugger debugger = new Debugger(getClass().getName());
// Initialize the plugin variable so we can access all of the plugin's data. // Initialize the plugin variable so we can access all of the plugin's data.
private HubThat plugin; private final HubThat plugin;
// Constructor to actually give "plugin" a value. // Constructor to actually give "plugin" a value.
public WorldTpCommand(HubThat givenPlugin) { plugin = givenPlugin; } public WorldTpCommand(HubThat givenPlugin) { plugin = givenPlugin; }

View File

@ -1,9 +1,10 @@
package net.mindoverflow.hubthat.commands.hubthatcommands; package net.mindoverflow.hubthat.commands.hubthatcommands;
import net.mindoverflow.hubthat.utils.*; import net.mindoverflow.hubthat.utils.LocalizedMessages;
import org.bukkit.Location; import net.mindoverflow.hubthat.utils.MessageUtils;
import net.mindoverflow.hubthat.utils.PermissionUtils;
import net.mindoverflow.hubthat.utils.Permissions;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
public class HelpCommand public class HelpCommand

View File

@ -9,7 +9,7 @@ import java.util.logging.Level;
public class ReloadCommand public class ReloadCommand
{ {
private static Debugger debugger = new Debugger(ReloadCommand.class.getName()); private static final Debugger debugger = new Debugger(ReloadCommand.class.getName());
public static void reloadCommand(CommandSender commandSender, HubThat plugin) public static void reloadCommand(CommandSender commandSender, HubThat plugin)

View File

@ -13,9 +13,9 @@ public class PlayerChatListener implements Listener
{ {
// Instantiate a Debugger for this class. // Instantiate a Debugger for this class.
private Debugger debugger = new Debugger(getClass().getName()); private final Debugger debugger = new Debugger(getClass().getName());
private HubThat plugin; private final HubThat plugin;
public PlayerChatListener(HubThat givenPlugin) public PlayerChatListener(HubThat givenPlugin)
{ {
plugin = givenPlugin; plugin = givenPlugin;

View File

@ -2,7 +2,9 @@ package net.mindoverflow.hubthat.listeners;
import net.mindoverflow.hubthat.HubThat; import net.mindoverflow.hubthat.HubThat;
import net.mindoverflow.hubthat.commands.HubCommand; import net.mindoverflow.hubthat.commands.HubCommand;
import net.mindoverflow.hubthat.utils.*; import net.mindoverflow.hubthat.utils.ConfigEntries;
import net.mindoverflow.hubthat.utils.Debugger;
import net.mindoverflow.hubthat.utils.MessageUtils;
import net.mindoverflow.hubthat.utils.files.FileUtils; import net.mindoverflow.hubthat.utils.files.FileUtils;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@ -16,9 +18,9 @@ import java.util.logging.Level;
public class PlayerJoinListener implements Listener public class PlayerJoinListener implements Listener
{ {
// Instantiate a Debugger for this class. // Instantiate a Debugger for this class.
private Debugger debugger = new Debugger(getClass().getName()); private final Debugger debugger = new Debugger(getClass().getName());
private HubThat plugin; private final HubThat plugin;
public PlayerJoinListener(HubThat givenPlugin) public PlayerJoinListener(HubThat givenPlugin)
{ {
plugin = givenPlugin; plugin = givenPlugin;

View File

@ -1,7 +1,9 @@
package net.mindoverflow.hubthat.listeners; package net.mindoverflow.hubthat.listeners;
import net.mindoverflow.hubthat.HubThat; import net.mindoverflow.hubthat.utils.CommonValues;
import net.mindoverflow.hubthat.utils.*; import net.mindoverflow.hubthat.utils.ConfigEntries;
import net.mindoverflow.hubthat.utils.LocalizedMessages;
import net.mindoverflow.hubthat.utils.MessageUtils;
import net.mindoverflow.hubthat.utils.files.FileUtils; import net.mindoverflow.hubthat.utils.files.FileUtils;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -10,13 +12,6 @@ import org.bukkit.event.player.PlayerMoveEvent;
public class PlayerMoveListener implements Listener public class PlayerMoveListener implements Listener
{ {
private HubThat plugin;
public PlayerMoveListener(HubThat givenPlugin)
{
plugin = givenPlugin;
}
@EventHandler @EventHandler
public void onPlayerMove(PlayerMoveEvent event) public void onPlayerMove(PlayerMoveEvent event)
{ {

View File

@ -16,7 +16,7 @@ import org.bukkit.event.player.PlayerRespawnEvent;
public class PlayerRespawnListener implements Listener public class PlayerRespawnListener implements Listener
{ {
private HubThat plugin; private final HubThat plugin;
public PlayerRespawnListener(HubThat givenPlugin) public PlayerRespawnListener(HubThat givenPlugin)
{ {
plugin = givenPlugin; plugin = givenPlugin;

View File

@ -1,7 +1,6 @@
package net.mindoverflow.hubthat.utils; package net.mindoverflow.hubthat.utils;
import net.mindoverflow.hubthat.HubThat;
import net.mindoverflow.hubthat.utils.files.FileUtils; import net.mindoverflow.hubthat.utils.files.FileUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -12,14 +11,8 @@ import java.util.logging.Level;
public class MessageUtils public class MessageUtils
{ {
// Initialize the Debugger instance. // Initialize the Debugger instance.
private static Debugger debugger = new Debugger(MessageUtils.class.getName()); private static final Debugger debugger = new Debugger(MessageUtils.class.getName());
private static HubThat plugin;
public MessageUtils(HubThat plugin)
{
MessageUtils.plugin = plugin;
}
// Method to automatically load and send a localized message to the CommandSender. // Method to automatically load and send a localized message to the CommandSender.
public static void sendLocalizedMessage(CommandSender sender, LocalizedMessages messageEnum) public static void sendLocalizedMessage(CommandSender sender, LocalizedMessages messageEnum)
{ {

View File

@ -1,18 +1,10 @@
package net.mindoverflow.hubthat.utils; package net.mindoverflow.hubthat.utils;
import net.mindoverflow.hubthat.HubThat;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
public class PermissionUtils public class PermissionUtils
{ {
// Initialize the Debugger instance.
private static Debugger debugger = new Debugger(PermissionUtils.class.getName());
private static HubThat plugin;
public PermissionUtils(HubThat givenPlugin) { plugin = givenPlugin; }
// Method to get the permission string from the Permissions enum. // Method to get the permission string from the Permissions enum.
public static boolean playerHasPermission(CommandSender user, Permissions permission) public static boolean playerHasPermission(CommandSender user, Permissions permission)
{ {

View File

@ -17,7 +17,7 @@ public class TeleportUtils
plugin = givenPlugin; plugin = givenPlugin;
} }
// Initialize the debugger so I can debug the plugin. // Initialize the debugger so I can debug the plugin.
private static Debugger debugger = new Debugger(TeleportUtils.class.getName()); private static final Debugger debugger = new Debugger(TeleportUtils.class.getName());
// Method to teleport a player, given the location coordinates, the world name and the player's name. // Method to teleport a player, given the location coordinates, the world name and the player's name.
public static void teleportPlayer(double x, double y, double z, double yaw, double pitch, String worldName, String playerName) public static void teleportPlayer(double x, double y, double z, double yaw, double pitch, String worldName, String playerName)

View File

@ -16,7 +16,7 @@ public class FileUtils
{ {
// Instantiate a Debugger for this class. // Instantiate a Debugger for this class.
private static Debugger debugger = new Debugger(FileUtils.class.getName()); private static final Debugger debugger = new Debugger(FileUtils.class.getName());
// Necessary variables. // Necessary variables.
private static HubThat plugin; private static HubThat plugin;

View File

@ -1,29 +1,8 @@
package net.mindoverflow.hubthat.utils.statistics; package net.mindoverflow.hubthat.utils.statistics;
import java.io.BufferedReader; import com.google.gson.JsonArray;
import java.io.ByteArrayOutputStream; import com.google.gson.JsonObject;
import java.io.DataOutputStream; import com.google.gson.JsonParser;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.zip.GZIPOutputStream;
import javax.net.ssl.HttpsURLConnection;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -31,9 +10,16 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.ServicePriority;
import com.google.gson.JsonArray; import javax.net.ssl.HttpsURLConnection;
import com.google.gson.JsonObject; import java.io.*;
import com.google.gson.JsonParser; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.zip.GZIPOutputStream;
/** /**
* bStats collects some data for plugin authors. * bStats collects some data for plugin authors.

View File

@ -24,9 +24,7 @@ public class UpdateChecker implements Runnable
public static BukkitTask task; public static BukkitTask task;
Debugger debugger = new Debugger(getClass().getName()); private final Debugger debugger = new Debugger(getClass().getName());
public String newVersion, updateLink; public String newVersion, updateLink;
public ArrayList<String> updateText = new ArrayList<>(), warningMessage = new ArrayList<>(); public ArrayList<String> updateText = new ArrayList<>(), warningMessage = new ArrayList<>();
@ -35,7 +33,7 @@ public class UpdateChecker implements Runnable
public Boolean isServerUnreachable = true; public Boolean isServerUnreachable = true;
public String errorCode; public String errorCode;
private HubThat plugin; private final HubThat plugin;
public UpdateChecker(HubThat givenPlugin) public UpdateChecker(HubThat givenPlugin)
{ {
plugin = givenPlugin; plugin = givenPlugin;