Refactor some public static variables

This commit is contained in:
Esophose 2019-01-26 00:43:36 -07:00
parent f306062bfa
commit 00127ca017
4 changed files with 33 additions and 20 deletions

View file

@ -13,7 +13,7 @@ public class DataUpdater {
* If they don't, create them
*/
protected static void tryCreateTables() {
PlayerParticles.getDBConnector().connect((connection) -> {
PlayerParticles.getPlugin().getDBConnector().connect((connection) -> {
try (Statement createStatement = connection.createStatement()) {
createStatement.addBatch("CREATE TABLE IF NOT EXISTS pp_settings (player_uuid VARCHAR(36), particles_hidden TINYINT)");
createStatement.addBatch("CREATE TABLE IF NOT EXISTS pp_particle (uuid VARCHAR(36), group_uuid VARCHAR(36), id SMALLINT, effect VARCHAR(100), style VARCHAR(100), item_material VARCHAR(100), block_material VARCHAR(100), note SMALLINT, r SMALLINT, g SMALLINT, b SMALLINT, PRIMARY KEY(uuid))");
@ -50,7 +50,7 @@ public class DataUpdater {
* Updates the data from versions older than v5.2
*/
private static void updateFrom_legacy_to_current() {
PlayerParticles.getDBConnector().connect((connection) -> {
PlayerParticles.getPlugin().getDBConnector().connect((connection) -> {
try (Statement statement = connection.createStatement()) {
statement.addBatch("DROP TABLE IF EXISTS pp_users");
statement.addBatch("DROP TABLE IF EXISTS pp_fixed");
@ -68,7 +68,7 @@ public class DataUpdater {
* Note: v5.3 was never officially released
*/
private static void updateFrom_5_3_to_current() {
PlayerParticles.getDBConnector().connect((connection) -> {
PlayerParticles.getPlugin().getDBConnector().connect((connection) -> {
// Create player settings table
try (Statement statement = connection.createStatement()) {
String updateQuery = "CREATE TABLE IF NOT EXISTS pp_settings (player_uuid VARCHAR(36), particles_hidden TINYINT)";

View file

@ -41,29 +41,32 @@ import com.esophose.playerparticles.updater.Updater;
public class PlayerParticles extends JavaPlugin {
private static Plugin pluginInstance;
/**
* The running instance of PlayerParticles on the server
*/
private static PlayerParticles pluginInstance;
/**
* The version a new update has, will be null if the config has it disabled
* or if there is no new version
*/
public static String updateVersion = null;
public String updateVersion = null;
/**
* The database connection manager
*/
private static DatabaseConnector databaseConnector = null;
private DatabaseConnector databaseConnector = null;
/**
* The task that spawns the particles
*/
private static BukkitTask particleTask = null;
private BukkitTask particleTask = null;
/**
* Executes essential tasks for starting up the plugin
*/
public void onEnable() {
pluginInstance = Bukkit.getServer().getPluginManager().getPlugin("PlayerParticles");
pluginInstance = (PlayerParticles)Bukkit.getServer().getPluginManager().getPlugin("PlayerParticles");
this.registerCommands();
@ -174,7 +177,7 @@ public class PlayerParticles extends JavaPlugin {
*
* @return The PlayerParticles plugin instance
*/
public static Plugin getPlugin() {
public static PlayerParticles getPlugin() {
return pluginInstance;
}
@ -183,10 +186,20 @@ public class PlayerParticles extends JavaPlugin {
*
* @return The DatabaseConnector
*/
public static DatabaseConnector getDBConnector() {
public DatabaseConnector getDBConnector() {
return databaseConnector;
}
/**
* Gets the latest available version of the plugin
* Will be null if no update is available
*
* @return The latest version available for the plugin
*/
public String getUpdateVersion() {
return this.updateVersion;
}
/**
* Determines if we should use MySQL or SQLite based on the useMySql parameter and if we were able to connect successfully
*

View file

@ -62,7 +62,7 @@ public class DataManager {
List<ParticleGroup> groups = new ArrayList<ParticleGroup>();
List<FixedParticleEffect> fixedParticles = new ArrayList<FixedParticleEffect>();
PlayerParticles.getDBConnector().connect((connection) -> {
PlayerParticles.getPlugin().getDBConnector().connect((connection) -> {
// Load settings
boolean particlesHidden = false;
String settingsQuery = "SELECT particles_hidden FROM pp_settings WHERE player_uuid = ?";
@ -191,7 +191,7 @@ public class DataManager {
*/
public static void loadFixedEffects() {
async(() -> {
PlayerParticles.getDBConnector().connect((connection) -> {
PlayerParticles.getPlugin().getDBConnector().connect((connection) -> {
String query = "SELECT DISTINCT owner_uuid FROM pp_fixed";
try (PreparedStatement statement = connection.prepareStatement(query)) {
ResultSet result = statement.executeQuery();
@ -214,7 +214,7 @@ public class DataManager {
*/
public static void updateSettingParticlesHidden(UUID playerUUID, boolean particlesHidden) {
async(() -> {
PlayerParticles.getDBConnector().connect((connection) -> {
PlayerParticles.getPlugin().getDBConnector().connect((connection) -> {
String updateQuery = "UPDATE pp_settings SET particles_hidden = ? WHERE player_uuid = ?";
try (PreparedStatement updateStatement = connection.prepareStatement(updateQuery)) {
updateStatement.setBoolean(1, particlesHidden);
@ -238,7 +238,7 @@ public class DataManager {
*/
public static void saveParticleGroup(UUID playerUUID, ParticleGroup group) {
async(() -> {
PlayerParticles.getDBConnector().connect((connection) -> {
PlayerParticles.getPlugin().getDBConnector().connect((connection) -> {
String groupUUID = null;
String groupUUIDQuery = "SELECT uuid FROM pp_group WHERE owner_uuid = ? AND name = ?";
@ -310,7 +310,7 @@ public class DataManager {
*/
public static void removeParticleGroup(UUID playerUUID, ParticleGroup group) {
async(() -> {
PlayerParticles.getDBConnector().connect((connection) -> {
PlayerParticles.getPlugin().getDBConnector().connect((connection) -> {
String groupQuery = "SELECT * FROM pp_group WHERE owner_uuid = ? AND name = ?";
String particleDeleteQuery = "DELETE FROM pp_particle WHERE group_uuid = ?";
String groupDeleteQuery = "DELETE FROM pp_group WHERE uuid = ?";
@ -356,7 +356,7 @@ public class DataManager {
*/
public static void saveFixedEffect(FixedParticleEffect fixedEffect) {
async(() -> {
PlayerParticles.getDBConnector().connect((connection) -> {
PlayerParticles.getPlugin().getDBConnector().connect((connection) -> {
String particleUUID = UUID.randomUUID().toString();
String particleQuery = "INSERT INTO pp_particle (uuid, group_uuid, id, effect, style, item_material, block_material, note, r, g, b) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
@ -402,7 +402,7 @@ public class DataManager {
*/
public static void updateFixedEffect(FixedParticleEffect fixedEffect) {
async(() -> {
PlayerParticles.getDBConnector().connect((connection) -> {
PlayerParticles.getPlugin().getDBConnector().connect((connection) -> {
// Update fixed effect
String fixedEffectQuery = "UPDATE pp_fixed SET xPos = ?, yPos = ?, zPos = ? WHERE owner_uuid = ? AND id = ?";
try (PreparedStatement statement = connection.prepareStatement(fixedEffectQuery)) {
@ -450,7 +450,7 @@ public class DataManager {
*/
public static void removeFixedEffect(UUID playerUUID, int id) {
async(() -> {
PlayerParticles.getDBConnector().connect((connection) -> {
PlayerParticles.getPlugin().getDBConnector().connect((connection) -> {
String particleUUID = null;
String particleUUIDQuery = "SELECT particle_uuid FROM pp_fixed WHERE owner_uuid = ? AND id = ?";

View file

@ -17,8 +17,8 @@ public class PluginUpdateListener implements Listener {
*/
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
if (e.getPlayer().isOp() && PlayerParticles.updateVersion != null) {
LangManager.sendCommandSenderMessage(e.getPlayer(), Lang.UPDATE_AVAILABLE, PlayerParticles.updateVersion, PlayerParticles.getPlugin().getDescription().getVersion());
if (e.getPlayer().isOp() && PlayerParticles.getPlugin().getUpdateVersion() != null) {
LangManager.sendCommandSenderMessage(e.getPlayer(), Lang.UPDATE_AVAILABLE, PlayerParticles.getPlugin().getUpdateVersion(), PlayerParticles.getPlugin().getDescription().getVersion());
}
}