Skip to content

Commit

Permalink
Merge pull request #32 from Esophose/async
Browse files Browse the repository at this point in the history
Asynchronize most of the plugin
  • Loading branch information
Esophose authored Feb 18, 2020
2 parents 621862a + 7c60062 commit 71d1066
Show file tree
Hide file tree
Showing 16 changed files with 148 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
import dev.esophose.playerparticles.styles.ParticleStyle;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.bukkit.Location;
import org.bukkit.Material;
Expand Down Expand Up @@ -525,7 +525,7 @@ public ParticleGroup savePlayerParticleGroup(@NotNull Player player, @NotNull St
Objects.requireNonNull(groupName);
Objects.requireNonNull(particles);

Map<Integer, ParticlePair> mappedParticles = new HashMap<>();
Map<Integer, ParticlePair> mappedParticles = new ConcurrentHashMap<>();
particles.forEach(x -> mappedParticles.put(x.getId(), x));
ParticleGroup particleGroup = new ParticleGroup(groupName.toLowerCase(), mappedParticles);
return this.savePlayerParticleGroup(player, particleGroup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;

Expand Down Expand Up @@ -101,7 +101,7 @@ private void onSave(PPlayer pplayer, String groupName) {
ParticleGroup group = pplayer.getParticleGroupByName(groupName);
boolean groupUpdated = false;
if (group == null) {
Map<Integer, ParticlePair> particles = new HashMap<>();
Map<Integer, ParticlePair> particles = new ConcurrentHashMap<>();
for (ParticlePair particle : pplayer.getActiveParticles())
particles.put(particle.getId(), particle.clone()); // Make sure the ParticlePairs aren't the same references in both the active and saved group
group = new ParticleGroup(groupName, particles);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@
import dev.esophose.playerparticles.styles.DefaultStyles;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;

public class ReloadCommandModule implements CommandModule {

public void onCommandExecute(PPlayer pplayer, String[] args) {
PlayerParticles playerParticles = PlayerParticles.getInstance();
LocaleManager localeManager = playerParticles.getManager(LocaleManager.class);
if (playerParticles.getManager(PermissionManager.class).canReloadPlugin(pplayer.getUnderlyingExecutor())) {
playerParticles.reload();
ParticleEffect.reloadSettings();
DefaultStyles.reloadSettings(playerParticles.getManager(ParticleStyleManager.class));
localeManager.sendMessage(pplayer, "reload-success");
playerParticles.getLogger().info("Reloaded configuration.");
} else {
localeManager.sendMessage(pplayer, "reload-no-permission");
}
Bukkit.getScheduler().runTask(playerParticles, () -> {
LocaleManager localeManager = playerParticles.getManager(LocaleManager.class);
if (playerParticles.getManager(PermissionManager.class).canReloadPlugin(pplayer.getUnderlyingExecutor())) {
playerParticles.reload();
ParticleEffect.reloadSettings();
DefaultStyles.reloadSettings(playerParticles.getManager(ParticleStyleManager.class));
localeManager.sendMessage(pplayer, "reload-success");
playerParticles.getLogger().info("Reloaded configuration.");
} else {
localeManager.sendMessage(pplayer, "reload-no-permission");
}
});
}

public List<String> onTabComplete(PPlayer pplayer, String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package dev.esophose.playerparticles.gui;

import dev.esophose.playerparticles.PlayerParticles;
import dev.esophose.playerparticles.manager.ConfigurationManager.Setting;
import dev.esophose.playerparticles.particles.PPlayer;
import dev.esophose.playerparticles.util.ParticleUtils;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -161,4 +163,8 @@ public void onClick(InventoryClickEvent event) {
}
}

public void close() {
Bukkit.getScheduler().runTask(PlayerParticles.getInstance(), () -> this.pplayer.getPlayer().closeInventory());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ public GuiInventoryLoadPresetGroups(PPlayer pplayer, boolean isEndPoint) {
}
PlayerParticlesAPI.getInstance().savePlayerParticleGroup(pplayer.getPlayer(), activeGroup);

if (Setting.GUI_CLOSE_AFTER_GROUP_SELECTED.getBoolean()) {
pplayer.getPlayer().closeInventory();
}
if (Setting.GUI_CLOSE_AFTER_GROUP_SELECTED.getBoolean())
this.close();
});
this.actionButtons.add(groupButton);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import dev.esophose.playerparticles.util.StringPlaceholders;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;

public class GuiInventoryManageGroups extends GuiInventory {
Expand Down Expand Up @@ -76,9 +76,8 @@ public GuiInventoryManageGroups(PPlayer pplayer) {
activeGroup.getParticles().put(particle.getId(), particle.clone());
PlayerParticlesAPI.getInstance().savePlayerParticleGroup(pplayer.getPlayer(), activeGroup);

if (Setting.GUI_CLOSE_AFTER_GROUP_SELECTED.getBoolean()) {
pplayer.getPlayer().closeInventory();
}
if (Setting.GUI_CLOSE_AFTER_GROUP_SELECTED.getBoolean())
this.close();
}
});
this.actionButtons.add(groupButton);
Expand Down Expand Up @@ -138,7 +137,7 @@ public GuiInventoryManageGroups(PPlayer pplayer) {
ParticleGroup group = pplayer.getParticleGroupByName(groupName);
boolean groupUpdated = false;
if (group == null) {
Map<Integer, ParticlePair> particles = new HashMap<>();
Map<Integer, ParticlePair> particles = new ConcurrentHashMap<>();
for (ParticlePair particle : pplayer.getActiveParticles())
particles.put(particle.getId(), particle.clone()); // Make sure the ParticlePairs aren't the same references in both the active and saved group
group = new ParticleGroup(groupName, particles);
Expand All @@ -157,7 +156,7 @@ public GuiInventoryManageGroups(PPlayer pplayer) {
guiManager.transition(new GuiInventoryManageGroups(pplayer));
}
}));
pplayer.getPlayer().closeInventory();
this.close();
});
this.actionButtons.add(saveGroupButton);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,34 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
return true;
}

String[] cmdArgs = args.length > 1 ? Arrays.copyOfRange(args, 1, args.length) : new String[0];

if (!commandModule.canConsoleExecute()) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Error: This command can only be executed by a player.");
return true;
}
} else if (sender instanceof ConsoleCommandSender) {
commandModule.onCommandExecute(PlayerParticlesAPI.getInstance().getConsolePPlayer(), cmdArgs);
return true;
}

Player p = (Player) sender;

this.playerParticles.getManager(DataManager.class).getPPlayer(p.getUniqueId(), (pplayer) -> {
PermissionManager permissionManager = PlayerParticles.getInstance().getManager(PermissionManager.class);
if (commandModule.requiresEffectsAndStyles() && (permissionManager.getEffectsUserHasPermissionFor(pplayer).isEmpty() || permissionManager.getStylesUserHasPermissionFor(pplayer).isEmpty())) {
localeManager.sendMessage(pplayer, "command-error-missing-effects-or-styles");
} else {
commandModule.onCommandExecute(pplayer, cmdArgs);
Bukkit.getScheduler().runTaskAsynchronously(this.playerParticles, () -> {
String[] cmdArgs = args.length > 1 ? Arrays.copyOfRange(args, 1, args.length) : new String[0];

if (!commandModule.canConsoleExecute()) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Error: This command can only be executed by a player.");
return;
}
} else if (sender instanceof ConsoleCommandSender) {
commandModule.onCommandExecute(PlayerParticlesAPI.getInstance().getConsolePPlayer(), cmdArgs);
return;
}

Player p = (Player) sender;

this.playerParticles.getManager(DataManager.class).getPPlayer(p.getUniqueId(), (pplayer) -> {
PermissionManager permissionManager = PlayerParticles.getInstance().getManager(PermissionManager.class);
if (commandModule.requiresEffectsAndStyles() && (permissionManager.getEffectsUserHasPermissionFor(pplayer).isEmpty() || permissionManager.getStylesUserHasPermissionFor(pplayer).isEmpty())) {
localeManager.sendMessage(pplayer, "command-error-missing-effects-or-styles");
} else {
commandModule.onCommandExecute(pplayer, cmdArgs);
}
});
});

return true;
} else if (cmd.getName().equalsIgnoreCase("ppo")) {
this.ppoCommand.onCommandExecute(sender, args);
Bukkit.getScheduler().runTaskAsynchronously(this.playerParticles, () -> this.ppoCommand.onCommandExecute(sender, args));
}

return true;
Expand Down
26 changes: 15 additions & 11 deletions src/main/java/dev/esophose/playerparticles/manager/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -106,8 +106,8 @@ public void getPPlayer(UUID playerUUID, Consumer<PPlayer> callback) {
}

this.async(() -> {
Map<String, ParticleGroup> groups = new HashMap<>();
Map<Integer, FixedParticleEffect> fixedParticles = new HashMap<>();
Map<String, ParticleGroup> groups = new ConcurrentHashMap<>();
Map<Integer, FixedParticleEffect> fixedParticles = new ConcurrentHashMap<>();

this.databaseConnector.connect((connection) -> {
// Load settings
Expand All @@ -133,9 +133,9 @@ public void getPPlayer(UUID playerUUID, Consumer<PPlayer> callback) {
}

// Load particle groups
String groupQuery = "SELECT * FROM " + this.getTablePrefix() + "group g " + // @formatter:off
String groupQuery = "SELECT * FROM " + this.getTablePrefix() + "group g " +
"JOIN " + this.getTablePrefix() + "particle p ON g.uuid = p.group_uuid " +
"WHERE g.owner_uuid = ?"; // @formatter:on
"WHERE g.owner_uuid = ?";
try (PreparedStatement statement = connection.prepareStatement(groupQuery)) {
statement.setString(1, playerUUID.toString());

Expand Down Expand Up @@ -172,7 +172,7 @@ public void getPPlayer(UUID playerUUID, Consumer<PPlayer> callback) {

// Add the particle to a new group if one didn't already exist
if (!groupAlreadyExists) {
HashMap<Integer, ParticlePair> particles = new HashMap<>();
Map<Integer, ParticlePair> particles = new ConcurrentHashMap<>();
if (!invalid)
particles.put(particle.getId(), particle);
ParticleGroup newGroup = new ParticleGroup(groupName, particles);
Expand All @@ -190,9 +190,9 @@ public void getPPlayer(UUID playerUUID, Consumer<PPlayer> callback) {
}

// Load fixed effects
String fixedQuery = "SELECT f.id AS f_id, f.world, f.xPos, f.yPos, f.zPos, p.id AS p_id, p.effect, p.style, p.item_material, p.block_material, p.note, p.r, p.g, p.b FROM " + this.getTablePrefix() + "fixed f " + // @formatter:off
String fixedQuery = "SELECT f.id AS f_id, f.world, f.xPos, f.yPos, f.zPos, p.id AS p_id, p.effect, p.style, p.item_material, p.block_material, p.note, p.r, p.g, p.b FROM " + this.getTablePrefix() + "fixed f " +
"JOIN " + this.getTablePrefix() + "particle p ON f.particle_uuid = p.uuid " +
"WHERE f.owner_uuid = ?"; // @formatter:on
"WHERE f.owner_uuid = ?";
try (PreparedStatement statement = connection.prepareStatement(fixedQuery)) {
statement.setString(1, playerUUID.toString());

Expand Down Expand Up @@ -240,7 +240,7 @@ public void getPPlayer(UUID playerUUID, Consumer<PPlayer> callback) {
}

if (!activeGroupExists) {
ParticleGroup activeGroup = new ParticleGroup(ParticleGroup.DEFAULT_NAME, new HashMap<>());
ParticleGroup activeGroup = new ParticleGroup(ParticleGroup.DEFAULT_NAME, new ConcurrentHashMap<>());
this.saveParticleGroup(playerUUID, activeGroup);
groups.put(activeGroup.getName(), activeGroup);
}
Expand Down Expand Up @@ -522,12 +522,16 @@ public void removeFixedEffect(UUID playerUUID, int id) {
}

/**
* Asynchronizes the callback with it's own thread
* Asynchronizes the callback with it's own thread unless it is already not on the main thread
*
* @param asyncCallback The callback to run on a separate thread
*/
private void async(Runnable asyncCallback) {
Bukkit.getScheduler().runTaskAsynchronously(this.playerParticles, asyncCallback);
if (Bukkit.isPrimaryThread()) {
Bukkit.getScheduler().runTaskAsynchronously(this.playerParticles, asyncCallback);
} else {
asyncCallback.run();
}
}

/**
Expand Down
Loading

0 comments on commit 71d1066

Please sign in to comment.