Database calls now async

All database calls (excluding the ones on plugin onEnable) are now run
asynchronously. Fix a small bug with the GUI and opening a previously
stored inventory.
This commit is contained in:
Esophose 2018-03-03 14:49:31 -07:00
parent 900d6785be
commit 95a400a42c
3 changed files with 346 additions and 278 deletions

View file

@ -332,10 +332,8 @@ public class PlayerParticlesGui extends BukkitRunnable implements Listener {
if (playerGuiInventories.containsKey(p.getUniqueId())) { if (playerGuiInventories.containsKey(p.getUniqueId())) {
GuiInventory guiInventory = playerGuiInventories.get(p.getUniqueId()); GuiInventory guiInventory = playerGuiInventories.get(p.getUniqueId());
guiInventory.setGuiState(state); guiInventory.setGuiState(state);
if (player.getOpenInventory().getTopInventory() != guiInventory.getInventory()) { if (!player.getOpenInventory().getTopInventory().equals(guiInventory.getInventory())) {
Inventory ppInventory = Bukkit.createInventory(null, INVENTORY_SIZE, "PlayerParticles"); player.openInventory(guiInventory.getInventory());
player.openInventory(ppInventory);
guiInventory.setInventory(ppInventory);
} }
} else { } else {
Inventory ppInventory = Bukkit.createInventory(null, INVENTORY_SIZE, "PlayerParticles"); Inventory ppInventory = Bukkit.createInventory(null, INVENTORY_SIZE, "PlayerParticles");

View file

@ -24,6 +24,7 @@ import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.scheduler.BukkitRunnable;
import com.esophose.playerparticles.PPlayer; import com.esophose.playerparticles.PPlayer;
import com.esophose.playerparticles.PlayerParticles; import com.esophose.playerparticles.PlayerParticles;
@ -46,7 +47,7 @@ public class ConfigManager {
/** /**
* The file the data is located in for the instance * The file the data is located in for the instance
*/ */
private File file; private File playerDataYamlFile;
/** /**
* The configuration used to edit the .yaml file * The configuration used to edit the .yaml file
*/ */
@ -81,17 +82,17 @@ public class ConfigManager {
if (!PlayerParticles.useMySQL) { // Don't bother creating the playerData.yml file if we aren't going to use it if (!PlayerParticles.useMySQL) { // Don't bother creating the playerData.yml file if we aren't going to use it
if (!PlayerParticles.getPlugin().getDataFolder().exists()) PlayerParticles.getPlugin().getDataFolder().mkdir(); if (!PlayerParticles.getPlugin().getDataFolder().exists()) PlayerParticles.getPlugin().getDataFolder().mkdir();
file = new File(PlayerParticles.getPlugin().getDataFolder(), fileName + ".yml"); playerDataYamlFile = new File(PlayerParticles.getPlugin().getDataFolder(), fileName + ".yml");
if (!file.exists()) { if (!playerDataYamlFile.exists()) {
try { try {
file.createNewFile(); playerDataYamlFile.createNewFile();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
playerDataYaml = YamlConfiguration.loadConfiguration(file); playerDataYaml = YamlConfiguration.loadConfiguration(playerDataYamlFile);
} }
} }
@ -100,7 +101,7 @@ public class ConfigManager {
*/ */
private void save() { private void save() {
try { try {
playerDataYaml.save(file); playerDataYaml.save(playerDataYamlFile);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -180,37 +181,38 @@ public class ConfigManager {
callback.execute(pplayer); callback.execute(pplayer);
} }
} else { } else {
String id = playerUUID.toString(); // @formatter:off async(() -> {
PlayerParticles.mySQL.connect((connection) -> { String id = playerUUID.toString(); // @formatter:off
String query = "SELECT * FROM pp_users u " + PlayerParticles.mySQL.connect((connection) -> {
"JOIN pp_data_item i ON u.player_uuid = i.uuid " + String query = "SELECT * FROM pp_users u " +
"JOIN pp_data_block b ON u.player_uuid = b.uuid " + "JOIN pp_data_item i ON u.player_uuid = i.uuid " +
"JOIN pp_data_color c ON u.player_uuid = c.uuid " + "JOIN pp_data_block b ON u.player_uuid = b.uuid " +
"JOIN pp_data_note n ON u.player_uuid = n.uuid " + "JOIN pp_data_color c ON u.player_uuid = c.uuid " +
"WHERE u.player_uuid = '" + id + "'"; "JOIN pp_data_note n ON u.player_uuid = n.uuid " +
"WHERE u.player_uuid = '" + id + "'";
try (Statement statement = connection.createStatement(); try (Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery(query)) { ResultSet res = statement.executeQuery(query)) {
if (res.next()) { if (res.next()) {
ParticleEffect particleEffect = ParticleEffect.fromName(res.getString("u.effect")); ParticleEffect particleEffect = ParticleEffect.fromName(res.getString("u.effect"));
ParticleStyle particleStyle = ParticleStyleManager.styleFromString(res.getString("u.style")); ParticleStyle particleStyle = ParticleStyleManager.styleFromString(res.getString("u.style"));
ItemData particleItemData = new ItemData(Material.matchMaterial(res.getString("i.material")), res.getByte("i.data")); ItemData particleItemData = new ItemData(Material.matchMaterial(res.getString("i.material")), res.getByte("i.data"));
BlockData particleBlockData = new BlockData(Material.matchMaterial(res.getString("b.material")), res.getByte("b.data")); BlockData particleBlockData = new BlockData(Material.matchMaterial(res.getString("b.material")), res.getByte("b.data"));
OrdinaryColor particleColorData = new OrdinaryColor(res.getInt("c.r"), res.getInt("c.g"), res.getInt("c.b")); OrdinaryColor particleColorData = new OrdinaryColor(res.getInt("c.r"), res.getInt("c.g"), res.getInt("c.b"));
NoteColor particleNoteColorData = new NoteColor(res.getByte("n.note")); NoteColor particleNoteColorData = new NoteColor(res.getByte("n.note"));
callback.execute(new PPlayer(playerUUID, particleEffect, particleStyle, particleItemData, particleBlockData, particleColorData, particleNoteColorData)); sync(() -> callback.execute(new PPlayer(playerUUID, particleEffect, particleStyle, particleItemData, particleBlockData, particleColorData, particleNoteColorData)));
return; return;
}
if (createIfNotFound) { // Didn't find an existing PPlayer, create and return a new one
PPlayer pplayer = PPlayer.getNewPPlayer(playerUUID);
saveNewPPlayer(pplayer);
sync(() -> callback.execute(pplayer));
}
} }
});
if (createIfNotFound) {
// Didn't find an existing PPlayer, create and return a new one
PPlayer pplayer = PPlayer.getNewPPlayer(playerUUID);
saveNewPPlayer(pplayer);
callback.execute(pplayer);
}
}
}); });
} }
} }
@ -254,41 +256,43 @@ public class ConfigManager {
save(); save();
} else { } else {
PlayerParticles.mySQL.connect((connection) -> { async(() -> {
try (Statement statement = connection.createStatement(); PlayerParticles.mySQL.connect((connection) -> {
ResultSet res = statement.executeQuery("SELECT * FROM pp_users WHERE player_uuid = '" + pplayer.getUniqueId() + "'")) { try (Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery("SELECT * FROM pp_users WHERE player_uuid = '" + pplayer.getUniqueId() + "'")) {
if (!res.next()) { if (!res.next()) {
PlayerParticles.mySQL.updateSQL("INSERT INTO pp_users (player_uuid, effect, style) VALUES (" + PlayerParticles.mySQL.updateSQL("INSERT INTO pp_users (player_uuid, effect, style) VALUES (" +
"'" + pplayer.getUniqueId().toString() + "', " + "'" + pplayer.getUniqueId().toString() + "', " +
"'" + pplayer.getParticleEffect().getName() + "', " + "'" + pplayer.getParticleEffect().getName() + "', " +
"'" + pplayer.getParticleStyle().getName() + "'" + "'" + pplayer.getParticleStyle().getName() + "'" +
"); " + "); " +
"INSERT INTO pp_data_item (uuid, material, data) VALUES (" + "INSERT INTO pp_data_item (uuid, material, data) VALUES (" +
"'" + pplayer.getUniqueId().toString() + "', " + "'" + pplayer.getUniqueId().toString() + "', " +
"'" + pplayer.getItemData().getMaterial().name() + "', " + "'" + pplayer.getItemData().getMaterial().name() + "', " +
pplayer.getItemData().getData() + pplayer.getItemData().getData() +
"); " + "); " +
"INSERT INTO pp_data_block (uuid, material, data) VALUES (" + "INSERT INTO pp_data_block (uuid, material, data) VALUES (" +
"'" + pplayer.getUniqueId().toString() + "', " + "'" + pplayer.getUniqueId().toString() + "', " +
"'" + pplayer.getBlockData().getMaterial().name() + "', " + "'" + pplayer.getBlockData().getMaterial().name() + "', " +
pplayer.getBlockData().getData() + pplayer.getBlockData().getData() +
"); " + "); " +
"INSERT INTO pp_data_color (uuid, r, g, b) VALUES (" + "INSERT INTO pp_data_color (uuid, r, g, b) VALUES (" +
"'" + pplayer.getUniqueId().toString() + "', " + "'" + pplayer.getUniqueId().toString() + "', " +
pplayer.getColorData().getRed() + ", " + pplayer.getColorData().getRed() + ", " +
pplayer.getColorData().getGreen() + ", " + pplayer.getColorData().getGreen() + ", " +
pplayer.getColorData().getBlue() + pplayer.getColorData().getBlue() +
"); " + "); " +
"INSERT INTO pp_data_note (uuid, note) VALUES (" + "INSERT INTO pp_data_note (uuid, note) VALUES (" +
"'" + pplayer.getUniqueId().toString() + "', " + "'" + pplayer.getUniqueId().toString() + "', " +
(byte) (pplayer.getNoteColorData().getValueX() * 24) + (byte) (pplayer.getNoteColorData().getValueX() * 24) +
");" ");"
); );
} else { } else {
throw new RuntimeException("The user " + pplayer.getUniqueId() + " is already in the database. They can not be added."); throw new RuntimeException("The user " + pplayer.getUniqueId() + " is already in the database. They can not be added.");
}
} }
} });
}); });
} }
@ -338,11 +342,13 @@ public class ConfigManager {
section.set("name", particleEffect.getName()); section.set("name", particleEffect.getName());
save(); save();
} else { } else {
try { async(() -> {
PlayerParticles.mySQL.updateSQL("UPDATE pp_users SET effect = '" + particleEffect.getName() + "' WHERE player_uuid = '" + playerUUID + "';"); try {
} catch (SQLException e) { PlayerParticles.mySQL.updateSQL("UPDATE pp_users SET effect = '" + particleEffect.getName() + "' WHERE player_uuid = '" + playerUUID + "';");
e.printStackTrace(); } catch (SQLException e) {
} e.printStackTrace();
}
});
} }
getPPlayer(playerUUID, (pplayer) -> { getPPlayer(playerUUID, (pplayer) -> {
@ -362,11 +368,13 @@ public class ConfigManager {
section.set("name", particleStyle.getName()); section.set("name", particleStyle.getName());
save(); save();
} else { } else {
try { async(() -> {
PlayerParticles.mySQL.updateSQL("UPDATE pp_users SET style = '" + particleStyle.getName() + "' WHERE player_uuid = '" + playerUUID + "';"); try {
} catch (SQLException e) { PlayerParticles.mySQL.updateSQL("UPDATE pp_users SET style = '" + particleStyle.getName() + "' WHERE player_uuid = '" + playerUUID + "';");
e.printStackTrace(); } catch (SQLException e) {
} e.printStackTrace();
}
});
} }
getPPlayer(playerUUID, (pplayer) -> { getPPlayer(playerUUID, (pplayer) -> {
@ -387,11 +395,13 @@ public class ConfigManager {
section.set("data", particleItemData.getData()); section.set("data", particleItemData.getData());
save(); save();
} else { } else {
try { async(() -> {
PlayerParticles.mySQL.updateSQL("UPDATE pp_data_item SET material = '" + particleItemData.getMaterial().name() + "', data = '" + particleItemData.getData() + "' WHERE uuid = '" + playerUUID + "';"); try {
} catch (SQLException e) { PlayerParticles.mySQL.updateSQL("UPDATE pp_data_item SET material = '" + particleItemData.getMaterial().name() + "', data = '" + particleItemData.getData() + "' WHERE uuid = '" + playerUUID + "';");
e.printStackTrace(); } catch (SQLException e) {
} e.printStackTrace();
}
});
} }
getPPlayer(playerUUID, (pplayer) -> { getPPlayer(playerUUID, (pplayer) -> {
@ -412,11 +422,13 @@ public class ConfigManager {
section.set("data", particleBlockData.getData()); section.set("data", particleBlockData.getData());
save(); save();
} else { } else {
try { async(() -> {
PlayerParticles.mySQL.updateSQL("UPDATE pp_data_block SET material = '" + particleBlockData.getMaterial().name() + "', data = '" + particleBlockData.getData() + "' WHERE uuid = '" + playerUUID + "';"); try {
} catch (SQLException e) { PlayerParticles.mySQL.updateSQL("UPDATE pp_data_block SET material = '" + particleBlockData.getMaterial().name() + "', data = '" + particleBlockData.getData() + "' WHERE uuid = '" + playerUUID + "';");
e.printStackTrace(); } catch (SQLException e) {
} e.printStackTrace();
}
});
} }
getPPlayer(playerUUID, (pplayer) -> { getPPlayer(playerUUID, (pplayer) -> {
@ -438,11 +450,13 @@ public class ConfigManager {
section.set("b", particleColorData.getBlue()); section.set("b", particleColorData.getBlue());
save(); save();
} else { } else {
try { async(() -> {
PlayerParticles.mySQL.updateSQL("UPDATE pp_data_color SET r = " + particleColorData.getRed() + ", g = " + particleColorData.getGreen() + ", b = " + particleColorData.getBlue() + " WHERE uuid = '" + playerUUID + "';"); try {
} catch (SQLException e) { PlayerParticles.mySQL.updateSQL("UPDATE pp_data_color SET r = " + particleColorData.getRed() + ", g = " + particleColorData.getGreen() + ", b = " + particleColorData.getBlue() + " WHERE uuid = '" + playerUUID + "';");
e.printStackTrace(); } catch (SQLException e) {
} e.printStackTrace();
}
});
} }
getPPlayer(playerUUID, (pplayer) -> { getPPlayer(playerUUID, (pplayer) -> {
@ -462,11 +476,13 @@ public class ConfigManager {
section.set("note", (byte) (particleNoteColorData.getValueX() * 24)); section.set("note", (byte) (particleNoteColorData.getValueX() * 24));
save(); save();
} else { } else {
try { async(() -> {
PlayerParticles.mySQL.updateSQL("UPDATE pp_data_note SET note = " + (byte) (particleNoteColorData.getValueX() * 24) + " WHERE uuid = '" + playerUUID + "';"); try {
} catch (SQLException e) { PlayerParticles.mySQL.updateSQL("UPDATE pp_data_note SET note = " + (byte) (particleNoteColorData.getValueX() * 24) + " WHERE uuid = '" + playerUUID + "';");
e.printStackTrace(); } catch (SQLException e) {
} e.printStackTrace();
}
});
} }
getPPlayer(playerUUID, (pplayer) -> { getPPlayer(playerUUID, (pplayer) -> {
@ -522,52 +538,54 @@ public class ConfigManager {
save(); save();
ParticleManager.addFixedEffect(fixedEffect); ParticleManager.addFixedEffect(fixedEffect);
} else { } else {
PlayerParticles.mySQL.connect((connection) -> { async(() -> {
try (Statement statement = connection.createStatement(); PlayerParticles.mySQL.connect((connection) -> {
ResultSet res = statement.executeQuery("SELECT * FROM pp_fixed WHERE player_uuid = '" + fixedEffect.getOwnerUniqueId() + "' AND id = " + fixedEffect.getId())) { try (Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery("SELECT * FROM pp_fixed WHERE player_uuid = '" + fixedEffect.getOwnerUniqueId() + "' AND id = " + fixedEffect.getId())) {
if (res.next()) { if (res.next()) {
System.out.println("Tried to create a fixed effect with ID " + fixedEffect.getId() + " that already in the database!"); System.out.println("Tried to create a fixed effect with ID " + fixedEffect.getId() + " that already in the database!");
return; return;
}
String fixedEffectUUID = UUID.randomUUID().toString();
PlayerParticles.mySQL.updateSQL("INSERT INTO pp_fixed (uuid, player_uuid, id, effect, style, worldName, xPos, yPos, zPos) VALUES (" +
"'" + fixedEffectUUID + "', " +
"'" + fixedEffect.getOwnerUniqueId().toString() + "', " +
fixedEffect.getId() + ", " +
"'" + fixedEffect.getParticleEffect().getName() + "', " +
"'" + fixedEffect.getParticleStyle().getName() + "', " +
"'" + fixedEffect.getLocation().getWorld().getName() + "', " +
fixedEffect.getLocation().getX() + ", " +
fixedEffect.getLocation().getY() + ", " +
fixedEffect.getLocation().getZ() +
"); " +
"INSERT INTO pp_data_item (uuid, material, data) VALUES (" +
"'" + fixedEffectUUID + "', " +
"'" + fixedEffect.getItemData().getMaterial().name() + "', " +
fixedEffect.getItemData().getData() +
"); " +
"INSERT INTO pp_data_block (uuid, material, data) VALUES (" +
"'" + fixedEffectUUID + "', " +
"'" + fixedEffect.getBlockData().getMaterial().name() + "', " +
fixedEffect.getBlockData().getData() +
"); " +
"INSERT INTO pp_data_color (uuid, r, g, b) VALUES (" +
"'" + fixedEffectUUID + "', " +
fixedEffect.getColorData().getRed() + ", " +
fixedEffect.getColorData().getGreen() + ", " +
fixedEffect.getColorData().getBlue() +
"); " +
"INSERT INTO pp_data_note (uuid, note) VALUES (" +
"'" + fixedEffectUUID + "', " +
(byte) (fixedEffect.getNoteColorData().getValueX() * 24) +
");"
);
sync(() -> ParticleManager.addFixedEffect(fixedEffect));
} }
});
String fixedEffectUUID = UUID.randomUUID().toString();
PlayerParticles.mySQL.updateSQL("INSERT INTO pp_fixed (uuid, player_uuid, id, effect, style, worldName, xPos, yPos, zPos) VALUES (" +
"'" + fixedEffectUUID + "', " +
"'" + fixedEffect.getOwnerUniqueId().toString() + "', " +
fixedEffect.getId() + ", " +
"'" + fixedEffect.getParticleEffect().getName() + "', " +
"'" + fixedEffect.getParticleStyle().getName() + "', " +
"'" + fixedEffect.getLocation().getWorld().getName() + "', " +
fixedEffect.getLocation().getX() + ", " +
fixedEffect.getLocation().getY() + ", " +
fixedEffect.getLocation().getZ() +
"); " +
"INSERT INTO pp_data_item (uuid, material, data) VALUES (" +
"'" + fixedEffectUUID + "', " +
"'" + fixedEffect.getItemData().getMaterial().name() + "', " +
fixedEffect.getItemData().getData() +
"); " +
"INSERT INTO pp_data_block (uuid, material, data) VALUES (" +
"'" + fixedEffectUUID + "', " +
"'" + fixedEffect.getBlockData().getMaterial().name() + "', " +
fixedEffect.getBlockData().getData() +
"); " +
"INSERT INTO pp_data_color (uuid, r, g, b) VALUES (" +
"'" + fixedEffectUUID + "', " +
fixedEffect.getColorData().getRed() + ", " +
fixedEffect.getColorData().getGreen() + ", " +
fixedEffect.getColorData().getBlue() +
"); " +
"INSERT INTO pp_data_note (uuid, note) VALUES (" +
"'" + fixedEffectUUID + "', " +
(byte) (fixedEffect.getNoteColorData().getValueX() * 24) +
");"
);
ParticleManager.addFixedEffect(fixedEffect);
}
}); });
} }
} }
@ -577,7 +595,7 @@ public class ConfigManager {
* *
* @param playerUUID The player who owns the effect * @param playerUUID The player who owns the effect
* @param id The id of the effect to remove * @param id The id of the effect to remove
* @return If the effect was removed * @param callback The callback to execute with if the fixed effect was removed or not
*/ */
public void removeFixedEffect(UUID playerUUID, int id, ConfigurationCallback<Boolean> callback) { public void removeFixedEffect(UUID playerUUID, int id, ConfigurationCallback<Boolean> callback) {
if (!PlayerParticles.useMySQL) { if (!PlayerParticles.useMySQL) {
@ -592,25 +610,30 @@ public class ConfigManager {
ParticleManager.removeFixedEffectForPlayer(playerUUID, id); ParticleManager.removeFixedEffectForPlayer(playerUUID, id);
callback.execute(true); callback.execute(true);
} else { } else {
PlayerParticles.mySQL.connect((connection) -> { async(() -> {
try (Statement statement = connection.createStatement(); PlayerParticles.mySQL.connect((connection) -> {
ResultSet res = statement.executeQuery("SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "' AND id = " + id)) { try (Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery("SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "' AND id = " + id)) {
if (!res.next()) { if (!res.next()) {
callback.execute(false); callback.execute(false);
return; return;
}
String uuid = res.getString("uuid");
PlayerParticles.mySQL.updateSQL("DELETE FROM pp_fixed WHERE uuid = '" + uuid + "';" +
"DELETE FROM pp_data_item WHERE uuid = '" + uuid + "';" +
"DELETE FROM pp_data_block WHERE uuid = '" + uuid + "';" +
"DELETE FROM pp_data_color WHERE uuid = '" + uuid + "';" +
"DELETE FROM pp_data_note WHERE uuid = '" + uuid + "';"
);
sync(() -> {
ParticleManager.removeFixedEffectForPlayer(playerUUID, id);
callback.execute(true);
});
} }
});
String uuid = res.getString("uuid");
PlayerParticles.mySQL.updateSQL("DELETE FROM pp_fixed WHERE uuid = '" + uuid + "';" +
"DELETE FROM pp_data_item WHERE uuid = '" + uuid + "';" +
"DELETE FROM pp_data_block WHERE uuid = '" + uuid + "';" +
"DELETE FROM pp_data_color WHERE uuid = '" + uuid + "';" +
"DELETE FROM pp_data_note WHERE uuid = '" + uuid + "';"
);
ParticleManager.removeFixedEffectForPlayer(playerUUID, id);
callback.execute(true);
}
}); });
} }
} }
@ -625,16 +648,18 @@ public class ConfigManager {
playerDataYaml.set(playerUUID.toString() + ".fixedEffect", null); playerDataYaml.set(playerUUID.toString() + ".fixedEffect", null);
save(); save();
} else { } else {
try { // @formatter:off async(() -> {
PlayerParticles.mySQL.updateSQL("DELETE FROM pp_data_item WHERE uuid IN (SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "');" + try { // @formatter:off
"DELETE FROM pp_data_block WHERE uuid IN (SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "');" + PlayerParticles.mySQL.updateSQL("DELETE FROM pp_data_item WHERE uuid IN (SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "');" +
"DELETE FROM pp_data_color WHERE uuid IN (SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "');" + "DELETE FROM pp_data_block WHERE uuid IN (SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "');" +
"DELETE FROM pp_data_note WHERE uuid IN (SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "');" + "DELETE FROM pp_data_color WHERE uuid IN (SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "');" +
"DELETE FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "';" "DELETE FROM pp_data_note WHERE uuid IN (SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "');" +
); // @formatter:on "DELETE FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "';"
} catch (SQLException e) { ); // @formatter:on
e.printStackTrace(); } catch (SQLException e) {
} e.printStackTrace();
}
});
} }
ParticleManager.removeAllFixedEffectsForPlayer(playerUUID); ParticleManager.removeAllFixedEffectsForPlayer(playerUUID);
@ -643,7 +668,7 @@ public class ConfigManager {
/** /**
* Gets a list of all saved fixed particle effects * Gets a list of all saved fixed particle effects
* *
* @return A list of all saved fixed particle effects * @param callback The callback to execute with a list of all saved fixed particle effects
*/ */
public void getAllFixedEffects(ConfigurationCallback<List<FixedParticleEffect>> callback) { public void getAllFixedEffects(ConfigurationCallback<List<FixedParticleEffect>> callback) {
if (!PlayerParticles.useMySQL) { if (!PlayerParticles.useMySQL) {
@ -682,36 +707,38 @@ public class ConfigManager {
callback.execute(fixedEffects); callback.execute(fixedEffects);
} else { // @formatter:off } else { // @formatter:off
PlayerParticles.mySQL.connect((connection) -> { async(() -> {
String query = "SELECT * FROM pp_fixed f " + PlayerParticles.mySQL.connect((connection) -> {
"JOIN pp_data_item i ON f.uuid = i.uuid " + String query = "SELECT * FROM pp_fixed f " +
"JOIN pp_data_block b ON f.uuid = b.uuid " + "JOIN pp_data_item i ON f.uuid = i.uuid " +
"JOIN pp_data_color c ON f.uuid = c.uuid " + "JOIN pp_data_block b ON f.uuid = b.uuid " +
"JOIN pp_data_note n ON f.uuid = n.uuid"; "JOIN pp_data_color c ON f.uuid = c.uuid " +
try (Statement statement = connection.createStatement(); "JOIN pp_data_note n ON f.uuid = n.uuid";
ResultSet res = statement.executeQuery(query)) { try (Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery(query)) {
List<FixedParticleEffect> fixedEffects = new ArrayList<FixedParticleEffect>(); List<FixedParticleEffect> fixedEffects = new ArrayList<FixedParticleEffect>();
while (res.next()) { while (res.next()) {
UUID pplayerUUID = UUID.fromString(res.getString("f.player_uuid")); UUID pplayerUUID = UUID.fromString(res.getString("f.player_uuid"));
int id = res.getInt("f.id"); int id = res.getInt("f.id");
String worldName = res.getString("f.worldName"); String worldName = res.getString("f.worldName");
double xPos = res.getDouble("f.xPos"); double xPos = res.getDouble("f.xPos");
double yPos = res.getDouble("f.yPos"); double yPos = res.getDouble("f.yPos");
double zPos = res.getDouble("f.zPos"); double zPos = res.getDouble("f.zPos");
ParticleEffect particleEffect = ParticleManager.effectFromString(res.getString("f.effect")); ParticleEffect particleEffect = ParticleManager.effectFromString(res.getString("f.effect"));
ParticleStyle particleStyle = ParticleStyleManager.styleFromString(res.getString("f.style")); ParticleStyle particleStyle = ParticleStyleManager.styleFromString(res.getString("f.style"));
ItemData particleItemData = new ItemData(Material.matchMaterial(res.getString("i.material")), res.getByte("i.data")); ItemData particleItemData = new ItemData(Material.matchMaterial(res.getString("i.material")), res.getByte("i.data"));
BlockData particleBlockData = new BlockData(Material.matchMaterial(res.getString("b.material")), res.getByte("b.data")); BlockData particleBlockData = new BlockData(Material.matchMaterial(res.getString("b.material")), res.getByte("b.data"));
OrdinaryColor particleColorData = new OrdinaryColor(res.getInt("c.r"), res.getInt("c.g"), res.getInt("c.b")); OrdinaryColor particleColorData = new OrdinaryColor(res.getInt("c.r"), res.getInt("c.g"), res.getInt("c.b"));
NoteColor particleNoteColorData = new NoteColor(res.getByte("n.note")); NoteColor particleNoteColorData = new NoteColor(res.getByte("n.note"));
fixedEffects.add(new FixedParticleEffect(pplayerUUID, id, worldName, xPos, yPos, zPos, particleEffect, particleStyle, particleItemData, particleBlockData, particleColorData, particleNoteColorData)); fixedEffects.add(new FixedParticleEffect(pplayerUUID, id, worldName, xPos, yPos, zPos, particleEffect, particleStyle, particleItemData, particleBlockData, particleColorData, particleNoteColorData));
}
sync(() -> callback.execute(fixedEffects));
} }
});
callback.execute(fixedEffects);
}
}); });
} }
} }
@ -721,7 +748,7 @@ public class ConfigManager {
* *
* @param pplayerUUID The player who owns the effect * @param pplayerUUID The player who owns the effect
* @param id The id for the effect to get * @param id The id for the effect to get
* @return The effect if one exists * @param callback The callback to execute with the effect, if one exists
*/ */
public void getFixedEffectForPlayerById(UUID pplayerUUID, int id, ConfigurationCallback<FixedParticleEffect> callback) { public void getFixedEffectForPlayerById(UUID pplayerUUID, int id, ConfigurationCallback<FixedParticleEffect> callback) {
if (!PlayerParticles.useMySQL) { if (!PlayerParticles.useMySQL) {
@ -748,31 +775,33 @@ public class ConfigManager {
callback.execute(new FixedParticleEffect(pplayerUUID, id, worldName, xPos, yPos, zPos, particleEffect, particleStyle, particleItemData, particleBlockData, particleColorData, particleNoteColorData)); callback.execute(new FixedParticleEffect(pplayerUUID, id, worldName, xPos, yPos, zPos, particleEffect, particleStyle, particleItemData, particleBlockData, particleColorData, particleNoteColorData));
} }
} else { } else {
PlayerParticles.mySQL.connect((connection) -> { // @formatter:off async(() -> {
String query = "SELECT * FROM pp_fixed f " + PlayerParticles.mySQL.connect((connection) -> { // @formatter:off
"JOIN pp_data_item i ON f.uuid = i.uuid " + String query = "SELECT * FROM pp_fixed f " +
"JOIN pp_data_block b ON f.uuid = b.uuid " + "JOIN pp_data_item i ON f.uuid = i.uuid " +
"JOIN pp_data_color c ON f.uuid = c.uuid " + "JOIN pp_data_block b ON f.uuid = b.uuid " +
"JOIN pp_data_note n ON f.uuid = n.uuid " + "JOIN pp_data_color c ON f.uuid = c.uuid " +
"WHERE f.player_uuid = '" + pplayerUUID.toString() + "' AND f.id = '" + id + "'"; // @formatter:on "JOIN pp_data_note n ON f.uuid = n.uuid " +
try (Statement statement = connection.createStatement(); "WHERE f.player_uuid = '" + pplayerUUID.toString() + "' AND f.id = '" + id + "'"; // @formatter:on
ResultSet res = statement.executeQuery(query)) { try (Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery(query)) {
if (res.next()) { if (res.next()) {
String worldName = res.getString("f.worldName"); String worldName = res.getString("f.worldName");
double xPos = res.getDouble("f.xPos"); double xPos = res.getDouble("f.xPos");
double yPos = res.getDouble("f.yPos"); double yPos = res.getDouble("f.yPos");
double zPos = res.getDouble("f.zPos"); double zPos = res.getDouble("f.zPos");
ParticleEffect particleEffect = ParticleManager.effectFromString(res.getString("f.effect")); ParticleEffect particleEffect = ParticleManager.effectFromString(res.getString("f.effect"));
ParticleStyle particleStyle = ParticleStyleManager.styleFromString(res.getString("f.style")); ParticleStyle particleStyle = ParticleStyleManager.styleFromString(res.getString("f.style"));
ItemData particleItemData = new ItemData(Material.matchMaterial(res.getString("i.material")), res.getByte("i.data")); ItemData particleItemData = new ItemData(Material.matchMaterial(res.getString("i.material")), res.getByte("i.data"));
BlockData particleBlockData = new BlockData(Material.matchMaterial(res.getString("b.material")), res.getByte("b.data")); BlockData particleBlockData = new BlockData(Material.matchMaterial(res.getString("b.material")), res.getByte("b.data"));
OrdinaryColor particleColorData = new OrdinaryColor(res.getInt("c.r"), res.getInt("c.g"), res.getInt("c.b")); OrdinaryColor particleColorData = new OrdinaryColor(res.getInt("c.r"), res.getInt("c.g"), res.getInt("c.b"));
NoteColor particleNoteColorData = new NoteColor(res.getByte("n.note")); NoteColor particleNoteColorData = new NoteColor(res.getByte("n.note"));
callback.execute(new FixedParticleEffect(pplayerUUID, id, worldName, xPos, yPos, zPos, particleEffect, particleStyle, particleItemData, particleBlockData, particleColorData, particleNoteColorData)); sync(() -> callback.execute(new FixedParticleEffect(pplayerUUID, id, worldName, xPos, yPos, zPos, particleEffect, particleStyle, particleItemData, particleBlockData, particleColorData, particleNoteColorData)));
}
} }
} });
}); });
} }
} }
@ -781,7 +810,7 @@ public class ConfigManager {
* Gets a list of all fixed effect ids for a player * Gets a list of all fixed effect ids for a player
* *
* @param pplayerUUID The player * @param pplayerUUID The player
* @return A list of all fixed effect ids for the given player * @param callback The callback to execute with a list of all fixed effect ids for the given player
*/ */
public void getFixedEffectIdsForPlayer(UUID pplayerUUID, ConfigurationCallback<List<Integer>> callback) { public void getFixedEffectIdsForPlayer(UUID pplayerUUID, ConfigurationCallback<List<Integer>> callback) {
if (!PlayerParticles.useMySQL) { if (!PlayerParticles.useMySQL) {
@ -796,18 +825,20 @@ public class ConfigManager {
callback.execute(ids); callback.execute(ids);
} else { } else {
PlayerParticles.mySQL.connect((connection) -> { async(() -> {
try (Statement statement = connection.createStatement(); PlayerParticles.mySQL.connect((connection) -> {
ResultSet res = statement.executeQuery("SELECT id FROM pp_fixed WHERE player_uuid = '" + pplayerUUID.toString() + "'")) { try (Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery("SELECT id FROM pp_fixed WHERE player_uuid = '" + pplayerUUID.toString() + "'")) {
List<Integer> ids = new ArrayList<Integer>(); List<Integer> ids = new ArrayList<Integer>();
while (res.next()) { while (res.next()) {
ids.add(res.getInt(1)); ids.add(res.getInt(1));
}
sync(() -> callback.execute(ids));
} }
});
callback.execute(ids);
}
}); });
} }
} }
@ -816,7 +847,7 @@ public class ConfigManager {
* Checks if the given player has reached the max number of fixed effects * Checks if the given player has reached the max number of fixed effects
* *
* @param pplayerUUID The player to check * @param pplayerUUID The player to check
* @return If the player can create any more fixed effects * @param callback The callback to execute with if the player can create any more fixed effects
*/ */
public void hasPlayerReachedMaxFixedEffects(UUID pplayerUUID, ConfigurationCallback<Boolean> callback) { public void hasPlayerReachedMaxFixedEffects(UUID pplayerUUID, ConfigurationCallback<Boolean> callback) {
if (maxFixedEffects == -1) { // Initialize on the fly if (maxFixedEffects == -1) { // Initialize on the fly
@ -833,14 +864,20 @@ public class ConfigManager {
callback.execute(playerDataYaml.getConfigurationSection(pplayerUUID.toString() + ".fixedEffect").getKeys(false).size() >= maxFixedEffects); callback.execute(playerDataYaml.getConfigurationSection(pplayerUUID.toString() + ".fixedEffect").getKeys(false).size() >= maxFixedEffects);
} else callback.execute(false); } else callback.execute(false);
} else { } else {
PlayerParticles.mySQL.connect((connection) -> { async(() -> {
try (Statement statement = connection.createStatement(); PlayerParticles.mySQL.connect((connection) -> {
ResultSet res = statement.executeQuery("SELECT COUNT(1) FROM pp_fixed WHERE player_uuid = '" + pplayerUUID.toString() + "'")) { try (Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery("SELECT COUNT(1) FROM pp_fixed WHERE player_uuid = '" + pplayerUUID.toString() + "'")) {
if (res.next()) { boolean hasReachedMax;
callback.execute(res.getInt(1) >= maxFixedEffects); if (res.next())
} else callback.execute(false); hasReachedMax = res.getInt(1) >= maxFixedEffects;
} else
hasReachedMax = false;
sync(() -> callback.execute(hasReachedMax));
}
});
}); });
} }
} }
@ -849,7 +886,7 @@ public class ConfigManager {
* Gets the next Id for a player's fixed effects * Gets the next Id for a player's fixed effects
* *
* @param pplayerUUID The player to get the Id for * @param pplayerUUID The player to get the Id for
* @return The smallest available Id the player can use * @param callback The callback to execute with the smallest available Id the player can use
*/ */
public void getNextFixedEffectId(UUID pplayerUUID, ConfigurationCallback<Integer> callback) { public void getNextFixedEffectId(UUID pplayerUUID, ConfigurationCallback<Integer> callback) {
if (!PlayerParticles.useMySQL) { if (!PlayerParticles.useMySQL) {
@ -869,27 +906,29 @@ public class ConfigManager {
callback.execute(ParticleUtils.getSmallestPositiveInt(ids)); callback.execute(ParticleUtils.getSmallestPositiveInt(ids));
} else { } else {
PlayerParticles.mySQL.connect((connection) -> { async(() -> {
try (Statement statement = connection.createStatement(); PlayerParticles.mySQL.connect((connection) -> {
ResultSet res = statement.executeQuery("SELECT id FROM pp_fixed WHERE player_uuid = '" + pplayerUUID.toString() + "'")) { try (Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery("SELECT id FROM pp_fixed WHERE player_uuid = '" + pplayerUUID.toString() + "'")) {
Set<String> idsSet = new HashSet<String>(); Set<String> idsSet = new HashSet<String>();
while (res.next()) while (res.next())
idsSet.add(res.getInt(1) + ""); idsSet.add(res.getInt(1) + "");
if (idsSet.isEmpty()) { if (idsSet.isEmpty()) {
callback.execute(1); sync(() -> callback.execute(1));
return; return;
}
int[] ids = new int[idsSet.size()];
int i = 0;
for (String key : idsSet)
ids[i++] = Integer.parseInt(key);
sync(() -> callback.execute(ParticleUtils.getSmallestPositiveInt(ids)));
} }
});
int[] ids = new int[idsSet.size()];
int i = 0;
for (String key : idsSet)
ids[i++] = Integer.parseInt(key);
callback.execute(ParticleUtils.getSmallestPositiveInt(ids));
}
}); });
} }
} }
@ -928,6 +967,41 @@ public class ConfigManager {
return disabledWorlds; return disabledWorlds;
} }
/**
* Asynchronizes the callback with it's own thread
*
* @param asyncCallback The callback to run on a separate thread
*/
private void async(SyncInterface asyncCallback) {
new BukkitRunnable() {
public void run() {
System.out.println("Async with thread: " + Thread.currentThread().getName());
asyncCallback.execute();
}
}.runTaskAsynchronously(PlayerParticles.getPlugin());
}
/**
* Synchronizes the callback with the main thread
*
* @param syncCallback The callback to run on the main thread
*/
private void sync(SyncInterface syncCallback) {
new BukkitRunnable() {
public void run() {
System.out.println("Resynced with thread: " + Thread.currentThread().getName());
syncCallback.execute();
}
}.runTask(PlayerParticles.getPlugin());
}
/**
* Provides an easy way to run a section of code either synchronously or asynchronously using a callback
*/
private static interface SyncInterface {
public void execute();
}
/** /**
* Allows callbacks to be passed between configuration methods and executed for returning objects after database queries * Allows callbacks to be passed between configuration methods and executed for returning objects after database queries
*/ */

View file

@ -53,13 +53,9 @@ public class DatabaseManager {
} }
/** /**
* Executes a callback with a Connection passed * Executes a callback with a Connection passed and automatically closes it
* Automatically closes connection
* You need to close any resulting Statements or ResultSets on your own
* *
* @param callback The callback to execute once the connection is retrieved * @param callback The callback to execute once the connection is retrieved
* @return
* @throws SQLException
*/ */
public void connect(ConnectionCallback callback) { public void connect(ConnectionCallback callback) {
try (Connection connection = hikari.getConnection()) { try (Connection connection = hikari.getConnection()) {
@ -73,8 +69,8 @@ public class DatabaseManager {
* Executes an update statement and cleans up all resources * Executes an update statement and cleans up all resources
* *
* @param query The update statement to run * @param query The update statement to run
* @return * @return An int with the status of the first statement in the query
* @throws SQLException * @throws SQLException If an SQL problem occurs executing the statement
*/ */
public int updateSQL(String query) throws SQLException { public int updateSQL(String query) throws SQLException {
Connection connection = null; Connection connection = null;