Use maps instead of lists where applicable. Start work on API

This commit is contained in:
Esophose 2020-01-05 04:39:27 -07:00
parent 7774e500ca
commit ca9cf08c70
19 changed files with 283 additions and 109 deletions

View file

@ -24,6 +24,7 @@ dependencies {
compile 'org.slf4j:slf4j-api:1.7.25'
compile 'org.slf4j:slf4j-nop:1.7.25'
compile 'com.zaxxer:HikariCP:3.2.0'
shadow 'org.jetbrains:annotations:16.0.2'
shadow 'me.clip:placeholderapi:2.10.4'
shadow 'org.xerial:sqlite-jdbc:3.23.1'
shadow 'org.spigotmc:spigot-api:1.15-R0.1-SNAPSHOT'

View file

@ -1,7 +1,171 @@
package dev.esophose.playerparticles.api;
import dev.esophose.playerparticles.PlayerParticles;
import dev.esophose.playerparticles.manager.DataManager;
import dev.esophose.playerparticles.particles.PPlayer;
import dev.esophose.playerparticles.particles.ParticleEffect;
import dev.esophose.playerparticles.particles.ParticleEffect.OrdinaryColor;
import dev.esophose.playerparticles.particles.ParticleEffect.NoteColor;
import dev.esophose.playerparticles.particles.ParticleGroup;
import dev.esophose.playerparticles.particles.ParticlePair;
import dev.esophose.playerparticles.styles.ParticleStyle;
import java.util.Objects;
import java.util.UUID;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class PlayerParticlesAPI {
private static PlayerParticlesAPI INSTANCE;
private PlayerParticles playerParticles;
private PlayerParticlesAPI() {
this.playerParticles = PlayerParticles.getInstance();
}
public static PlayerParticlesAPI getInstance() {
if (INSTANCE == null)
INSTANCE = new PlayerParticlesAPI();
return INSTANCE;
}
@Nullable
public PPlayer getPPlayer(@NotNull UUID uuid) {
Objects.requireNonNull(uuid);
return this.playerParticles.getManager(DataManager.class).getPPlayer(uuid);
}
@Nullable
public PPlayer getPPlayer(@NotNull Player player) {
return this.getPPlayer(player.getUniqueId());
}
//region Manage Active Player Particles
public void addActivePlayerParticle(@NotNull Player player, @NotNull ParticlePair particle) {
Objects.requireNonNull(particle);
DataManager dataManager = this.playerParticles.getManager(DataManager.class);
PPlayer pplayer = this.getPPlayer(player);
if (pplayer == null)
return;
ParticleGroup particleGroup = pplayer.getActiveParticleGroup();
if (particleGroup.getParticles().containsKey(particle.getId()))
throw new IllegalArgumentException("A particle with the id " + particle.getId() + " already exists");
pplayer.getActiveParticleGroup().getParticles().put(particle.getId(), particle);
dataManager.saveParticleGroup(player.getUniqueId(), pplayer.getActiveParticleGroup());
}
public void editActivePlayerParticle(@NotNull Player player, int id, @NotNull ParticleEffect effect) {
Objects.requireNonNull(effect);
DataManager dataManager = this.playerParticles.getManager(DataManager.class);
ParticleGroup group = this.validateActivePlayerParticle(player, id);
if (group != null) {
group.getParticles().get(id).setEffect(effect);
dataManager.saveParticleGroup(player.getUniqueId(), group);
}
}
public void editActivePlayerParticle(@NotNull Player player, int id, @NotNull ParticleStyle style) {
Objects.requireNonNull(style);
DataManager dataManager = this.playerParticles.getManager(DataManager.class);
ParticleGroup group = this.validateActivePlayerParticle(player, id);
if (group != null) {
group.getParticles().get(id).setStyle(style);
dataManager.saveParticleGroup(player.getUniqueId(), group);
}
}
public void editActivePlayerParticle(@NotNull Player player, int id, @NotNull OrdinaryColor color) {
Objects.requireNonNull(color);
DataManager dataManager = this.playerParticles.getManager(DataManager.class);
ParticleGroup group = this.validateActivePlayerParticle(player, id);
if (group != null) {
group.getParticles().get(id).setColor(color);
dataManager.saveParticleGroup(player.getUniqueId(), group);
}
}
public void editActivePlayerParticle(@NotNull Player player, int id, @NotNull NoteColor noteColor) {
Objects.requireNonNull(noteColor);
DataManager dataManager = this.playerParticles.getManager(DataManager.class);
ParticleGroup group = this.validateActivePlayerParticle(player, id);
if (group != null) {
group.getParticles().get(id).setNoteColor(noteColor);
dataManager.saveParticleGroup(player.getUniqueId(), group);
}
}
public void editActivePlayerParticle(@NotNull Player player, int id, @NotNull Material material) {
Objects.requireNonNull(material);
DataManager dataManager = this.playerParticles.getManager(DataManager.class);
ParticleGroup group = this.validateActivePlayerParticle(player, id);
if (group != null) {
if (material.isBlock()) {
group.getParticles().get(id).setBlockMaterial(material);
} else {
group.getParticles().get(id).setItemMaterial(material);
}
dataManager.saveParticleGroup(player.getUniqueId(), group);
}
}
public void removeActivePlayerParticle(@NotNull Player player, int id) {
DataManager dataManager = this.playerParticles.getManager(DataManager.class);
ParticleGroup group = this.validateActivePlayerParticle(player, id);
if (group != null) {
group.getParticles().remove(id);
dataManager.saveParticleGroup(player.getUniqueId(), group);
}
}
public void removeActivePlayerParticle(@NotNull Player player, @NotNull ParticleEffect effect) {
Objects.requireNonNull(effect);
DataManager dataManager = this.playerParticles.getManager(DataManager.class);
PPlayer pplayer = this.getPPlayer(player);
if (pplayer == null)
return;
ParticleGroup group = pplayer.getActiveParticleGroup();
group.getParticles().values().removeIf(x -> x.getEffect().equals(effect));
dataManager.saveParticleGroup(player.getUniqueId(), group);
}
public void removeActivePlayerParticle(@NotNull Player player, @NotNull ParticleStyle style) {
Objects.requireNonNull(style);
DataManager dataManager = this.playerParticles.getManager(DataManager.class);
PPlayer pplayer = this.getPPlayer(player);
if (pplayer == null)
return;
ParticleGroup group = pplayer.getActiveParticleGroup();
group.getParticles().values().removeIf(x -> x.getStyle().equals(style));
dataManager.saveParticleGroup(player.getUniqueId(), group);
}
private ParticleGroup validateActivePlayerParticle(Player player, int id) {
PPlayer pplayer = this.getPPlayer(player);
if (pplayer == null)
return null;
ParticleGroup particleGroup = pplayer.getActiveParticleGroup();
if (!particleGroup.getParticles().containsKey(id))
throw new IllegalArgumentException("No particle exists with the id " + id);
return particleGroup;
}
//endregion
}

View file

@ -132,7 +132,7 @@ public class AddCommandModule implements CommandModule {
ParticleGroup group = pplayer.getActiveParticleGroup();
ParticlePair newParticle = new ParticlePair(pplayer.getUniqueId(), pplayer.getNextActiveParticleId(), effect, style, itemData, blockData, colorData, noteColorData);
group.getParticles().add(newParticle);
group.getParticles().put(newParticle.getId(), newParticle);
PlayerParticles.getInstance().getManager(DataManager.class).saveParticleGroup(pplayer.getUniqueId(), group);
StringPlaceholders addParticlePlaceholders = StringPlaceholders.builder("effect", newParticle.getEffect().getName())

View file

@ -87,7 +87,7 @@ public class EditCommandModule implements CommandModule {
}
ParticleGroup group = pplayer.getActiveParticleGroup();
for (ParticlePair particle : group.getParticles()) {
for (ParticlePair particle : group.getParticles().values()) {
if (particle.getId() == id) {
particle.setEffect(effect);
break;
@ -118,7 +118,7 @@ public class EditCommandModule implements CommandModule {
}
ParticleGroup group = pplayer.getActiveParticleGroup();
for (ParticlePair particle : group.getParticles()) {
for (ParticlePair particle : group.getParticles().values()) {
if (particle.getId() == id) {
particle.setStyle(style);
break;
@ -215,7 +215,7 @@ public class EditCommandModule implements CommandModule {
String updatedDataString = null;
ParticleGroup group = pplayer.getActiveParticleGroup();
for (ParticlePair particle : group.getParticles()) {
for (ParticlePair particle : group.getParticles().values()) {
if (particle.getId() == id) {
if (itemData != null) particle.setItemMaterial(itemData);
if (blockData != null) particle.setBlockMaterial(blockData);

View file

@ -531,7 +531,7 @@ public class FixedCommandModule implements CommandModule {
private void handleList(PPlayer pplayer, Player p, String[] args) {
LocaleManager localeManager = PlayerParticles.getInstance().getManager(LocaleManager.class);
List<Integer> ids = pplayer.getFixedEffectIds();
List<Integer> ids = new ArrayList<>(pplayer.getFixedEffectIds());
Collections.sort(ids);
if (ids.isEmpty()) {

View file

@ -13,7 +13,9 @@ import dev.esophose.playerparticles.util.StringPlaceholders;
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 org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
@ -99,9 +101,9 @@ public class GroupCommandModule implements CommandModule {
ParticleGroup group = pplayer.getParticleGroupByName(groupName);
boolean groupUpdated = false;
if (group == null) {
List<ParticlePair> particles = new ArrayList<>();
Map<Integer, ParticlePair> particles = new HashMap<>();
for (ParticlePair particle : pplayer.getActiveParticles())
particles.add(particle.clone()); // Make sure the ParticlePairs aren't the same references in both the active and saved group
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);
} else {
groupUpdated = true;
@ -159,8 +161,8 @@ public class GroupCommandModule implements CommandModule {
// Empty out the active group and fill it with clones from the target group
ParticleGroup activeGroup = pplayer.getActiveParticleGroup();
activeGroup.getParticles().clear();
for (ParticlePair particle : group.getParticles())
activeGroup.getParticles().add(particle.clone());
for (ParticlePair particle : group.getParticles().values())
activeGroup.getParticles().put(particle.getId(), particle.clone());
// Update group and notify player
PlayerParticles.getInstance().getManager(DataManager.class).saveParticleGroup(pplayer.getUniqueId(), activeGroup);
@ -236,7 +238,7 @@ public class GroupCommandModule implements CommandModule {
group = presetGroup.getGroup();
}
List<ParticlePair> particles = group.getParticles();
List<ParticlePair> particles = new ArrayList<>(group.getParticles().values());
particles.sort(Comparator.comparingInt(ParticlePair::getId));
localeManager.sendMessage(pplayer, "group-info-header", StringPlaceholders.single("group", groupName));
@ -258,7 +260,7 @@ public class GroupCommandModule implements CommandModule {
private void onList(PPlayer pplayer) {
LocaleManager localeManager = PlayerParticles.getInstance().getManager(LocaleManager.class);
List<ParticleGroup> groups = pplayer.getParticleGroups();
List<ParticleGroup> groups = new ArrayList<>(pplayer.getParticleGroups().values());
groups.sort(Comparator.comparing(ParticleGroup::getName));
Player player = pplayer.getPlayer();
@ -303,7 +305,7 @@ public class GroupCommandModule implements CommandModule {
matches.add("<groupName>");
} else {
List<String> groupNames = new ArrayList<>();
for (ParticleGroup group : pplayer.getParticleGroups())
for (ParticleGroup group : pplayer.getParticleGroups().values())
if (!group.getName().equals(ParticleGroup.DEFAULT_NAME))
groupNames.add(group.getName());
if (!args[0].equals("remove"))

View file

@ -14,7 +14,7 @@ public class ListCommandModule implements CommandModule {
public void onCommandExecute(PPlayer pplayer, String[] args) {
LocaleManager localeManager = PlayerParticles.getInstance().getManager(LocaleManager.class);
List<ParticlePair> particles = pplayer.getActiveParticles();
List<ParticlePair> particles = new ArrayList<>(pplayer.getActiveParticles());
particles.sort(Comparator.comparingInt(ParticlePair::getId));
if (particles.isEmpty()) {

View file

@ -41,15 +41,8 @@ public class RemoveCommandModule implements CommandModule {
return;
}
boolean removed = false;
ParticleGroup activeGroup = pplayer.getActiveParticleGroup();
for (ParticlePair particle : activeGroup.getParticles()) {
if (particle.getId() == id) {
activeGroup.getParticles().remove(particle);
removed = true;
break;
}
}
boolean removed = activeGroup.getParticles().remove(id) != null;
if (!removed) {
localeManager.sendMessage(pplayer, "id-unknown", StringPlaceholders.single("id", id));

View file

@ -126,14 +126,14 @@ public class GuiInventoryDefault extends GuiInventory {
callbacks.add(() -> {
ParticleGroup group = pplayer.getActiveParticleGroup();
if (canEditPrimaryStyleAndData) {
for (ParticlePair particle : group.getParticles()) {
for (ParticlePair particle : group.getParticles().values()) {
if (particle.getId() == editingParticle.getId()) {
particle.setEffect(editingParticle.getEffect());
break;
}
}
} else {
group.getParticles().add(editingParticle);
group.getParticles().put(editingParticle.getId(), editingParticle);
}
dataManager.saveParticleGroup(pplayer.getUniqueId(), group);
@ -167,7 +167,7 @@ public class GuiInventoryDefault extends GuiInventory {
callbacks.add(() -> guiManager.transition(new GuiInventoryEditStyle(pplayer, editingParticle, 1, callbacks, 1)));
callbacks.add(() -> {
ParticleGroup group = pplayer.getActiveParticleGroup();
for (ParticlePair particle : group.getParticles()) {
for (ParticlePair particle : group.getParticles().values()) {
if (particle.getId() == editingParticle.getId()) {
particle.setStyle(editingParticle.getStyle());
break;
@ -210,7 +210,7 @@ public class GuiInventoryDefault extends GuiInventory {
callbacks.add(() -> guiManager.transition(new GuiInventoryEditData(pplayer, editingParticle, 1, callbacks, 1)));
callbacks.add(() -> {
ParticleGroup group = pplayer.getActiveParticleGroup();
for (ParticlePair particle : group.getParticles()) {
for (ParticlePair particle : group.getParticles().values()) {
if (particle.getId() == editingParticle.getId()) {
particle.setColor(editingParticle.getColor());
particle.setNoteColor(editingParticle.getNoteColor());

View file

@ -52,7 +52,7 @@ public class GuiInventoryEditParticle extends GuiInventory {
callbacks.add(() -> guiManager.transition(new GuiInventoryEditEffect(pplayer, editingParticle, 1, callbacks, 1)));
callbacks.add(() -> {
ParticleGroup group = pplayer.getActiveParticleGroup();
for (ParticlePair particle : group.getParticles()) {
for (ParticlePair particle : group.getParticles().values()) {
if (particle.getId() == editingParticle.getId()) {
particle.setEffect(editingParticle.getEffect());
break;
@ -78,7 +78,7 @@ public class GuiInventoryEditParticle extends GuiInventory {
callbacks.add(() -> guiManager.transition(new GuiInventoryEditStyle(pplayer, editingParticle, 1, callbacks, 1)));
callbacks.add(() -> {
ParticleGroup group = pplayer.getActiveParticleGroup();
for (ParticlePair particle : group.getParticles()) {
for (ParticlePair particle : group.getParticles().values()) {
if (particle.getId() == editingParticle.getId()) {
particle.setStyle(editingParticle.getStyle());
break;
@ -107,7 +107,7 @@ public class GuiInventoryEditParticle extends GuiInventory {
callbacks.add(() -> guiManager.transition(new GuiInventoryEditData(pplayer, editingParticle, 1, callbacks, 1)));
callbacks.add(() -> {
ParticleGroup group = pplayer.getActiveParticleGroup();
for (ParticlePair particle : group.getParticles()) {
for (ParticlePair particle : group.getParticles().values()) {
if (particle.getId() == editingParticle.getId()) {
particle.setColor(editingParticle.getColor());
particle.setNoteColor(editingParticle.getNoteColor());

View file

@ -13,6 +13,7 @@ import dev.esophose.playerparticles.particles.ParticleGroupPreset;
import dev.esophose.playerparticles.particles.ParticlePair;
import dev.esophose.playerparticles.util.ParticleUtils;
import dev.esophose.playerparticles.util.StringPlaceholders;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.bukkit.Bukkit;
@ -39,7 +40,7 @@ public class GuiInventoryLoadPresetGroups extends GuiInventory {
if (!group.canPlayerUse(player))
continue;
List<ParticlePair> particles = group.getGroup().getParticles();
List<ParticlePair> particles = new ArrayList<>(group.getGroup().getParticles().values());
particles.sort(Comparator.comparingInt(ParticlePair::getId));
String[] lore = new String[particles.size() + 1];
@ -62,7 +63,7 @@ public class GuiInventoryLoadPresetGroups extends GuiInventory {
for (ParticlePair particle : particles) {
ParticlePair clonedParticle = particle.clone();
clonedParticle.setOwner(pplayer);
activeGroup.getParticles().add(clonedParticle);
activeGroup.getParticles().put(clonedParticle.getId(), clonedParticle);
}
dataManager.saveParticleGroup(pplayer.getUniqueId(), activeGroup);

View file

@ -16,7 +16,9 @@ import dev.esophose.playerparticles.util.ParticleUtils;
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 org.bukkit.Bukkit;
public class GuiInventoryManageGroups extends GuiInventory {
@ -33,13 +35,13 @@ public class GuiInventoryManageGroups extends GuiInventory {
int index = 10;
int nextWrap = 17;
int maxIndex = 35;
List<ParticleGroup> groups = pplayer.getParticleGroups();
List<ParticleGroup> groups = new ArrayList<>(pplayer.getParticleGroups().values());
groups.sort(Comparator.comparing(ParticleGroup::getName));
for (ParticleGroup group : groups) {
if (group.getName().equals(ParticleGroup.DEFAULT_NAME)) continue;
List<ParticlePair> particles = group.getParticles();
List<ParticlePair> particles = new ArrayList<>(group.getParticles().values());
particles.sort(Comparator.comparingInt(ParticlePair::getId));
String[] lore = new String[particles.size() + 2];
@ -72,7 +74,7 @@ public class GuiInventoryManageGroups extends GuiInventory {
ParticleGroup activeGroup = pplayer.getActiveParticleGroup();
activeGroup.getParticles().clear();
for (ParticlePair particle : particles)
activeGroup.getParticles().add(particle.clone());
activeGroup.getParticles().put(particle.getId(), particle.clone());
dataManager.saveParticleGroup(pplayer.getUniqueId(), activeGroup);
if (Setting.GUI_CLOSE_AFTER_GROUP_SELECTED.getBoolean()) {
@ -137,9 +139,9 @@ public class GuiInventoryManageGroups extends GuiInventory {
ParticleGroup group = pplayer.getParticleGroupByName(groupName);
boolean groupUpdated = false;
if (group == null) {
List<ParticlePair> particles = new ArrayList<ParticlePair>();
Map<Integer, ParticlePair> particles = new HashMap<>();
for (ParticlePair particle : pplayer.getActiveParticles())
particles.add(particle.clone()); // Make sure the ParticlePairs aren't the same references in both the active and saved group
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);
} else {
groupUpdated = true;

View file

@ -29,7 +29,7 @@ public class GuiInventoryManageParticles extends GuiInventory {
this.fillBorder(BorderColor.ORANGE);
// Manage/Delete Particle Buttons
List<ParticlePair> particles = pplayer.getActiveParticles();
List<ParticlePair> particles = new ArrayList<>(pplayer.getActiveParticles());
particles.sort(Comparator.comparingInt(ParticlePair::getId));
int index = 10;
@ -56,12 +56,7 @@ public class GuiInventoryManageParticles extends GuiInventory {
} else {
// Delete particle
ParticleGroup activeGroup = pplayer.getActiveParticleGroup();
for (ParticlePair pp : activeGroup.getParticles()) {
if (pp.getId() == particle.getId()) {
activeGroup.getParticles().remove(pp);
break;
}
}
activeGroup.getParticles().remove(particle.getId());
dataManager.saveParticleGroup(pplayer.getUniqueId(), activeGroup);
// Update inventory to reflect deletion
@ -104,7 +99,7 @@ public class GuiInventoryManageParticles extends GuiInventory {
callbacks.add(() -> {
// Save new particle
ParticleGroup group = pplayer.getActiveParticleGroup();
group.getParticles().add(editingParticle);
group.getParticles().put(editingParticle.getId(), editingParticle);
dataManager.saveParticleGroup(pplayer.getUniqueId(), group);
// Reopen the manage particle inventory

View file

@ -17,7 +17,10 @@ import dev.esophose.playerparticles.util.ParticleUtils;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import org.bukkit.Bukkit;
@ -76,7 +79,7 @@ public class DataManager extends Manager {
* @return The PPlayer from cache
*/
public PPlayer getPPlayer(UUID playerUUID) {
List<PPlayer> pplayers;
Collection<PPlayer> pplayers;
synchronized (pplayers = this.playerParticles.getManager(ParticleManager.class).getPPlayers()) { // Under rare circumstances, the PPlayers list can be changed while it's looping
for (PPlayer pp : pplayers)
if (pp.getUniqueId().equals(playerUUID))
@ -101,8 +104,8 @@ public class DataManager extends Manager {
}
this.async(() -> {
List<ParticleGroup> groups = new ArrayList<>();
List<FixedParticleEffect> fixedParticles = new ArrayList<>();
Map<String, ParticleGroup> groups = new HashMap<>();
Map<Integer, FixedParticleEffect> fixedParticles = new HashMap<>();
this.databaseConnector.connect((connection) -> {
// Load settings
@ -151,9 +154,9 @@ public class DataManager extends Manager {
// Try to add particle to an existing group
boolean groupAlreadyExists = false;
for (ParticleGroup group : groups) {
for (ParticleGroup group : groups.values()) {
if (group.getName().equalsIgnoreCase(groupName)) {
group.getParticles().add(particle);
group.getParticles().put(particle.getId(), particle);
groupAlreadyExists = true;
break;
}
@ -161,10 +164,10 @@ public class DataManager extends Manager {
// Add the particle to a new group if one didn't already exist
if (!groupAlreadyExists) {
List<ParticlePair> particles = new ArrayList<>();
particles.add(particle);
HashMap<Integer, ParticlePair> particles = new HashMap<>();
particles.put(particle.getId(), particle);
ParticleGroup newGroup = new ParticleGroup(groupName, particles);
groups.add(newGroup);
groups.put(newGroup.getName().toLowerCase(), newGroup);
}
}
}
@ -200,13 +203,13 @@ public class DataManager extends Manager {
OrdinaryColor color = new OrdinaryColor(result.getInt("r"), result.getInt("g"), result.getInt("b"));
ParticlePair particle = new ParticlePair(playerUUID, particleId, effect, style, itemMaterial, blockMaterial, color, noteColor);
fixedParticles.add(new FixedParticleEffect(playerUUID, fixedEffectId, world, xPos, yPos, zPos, particle));
fixedParticles.put(fixedEffectId, new FixedParticleEffect(playerUUID, fixedEffectId, world, xPos, yPos, zPos, particle));
}
}
// If there aren't any groups then this is a brand new PPlayer and we need to save a new active group for them
boolean activeGroupExists = false;
for (ParticleGroup group : groups) {
for (ParticleGroup group : groups.values()) {
if (group.getName().equals(ParticleGroup.DEFAULT_NAME)) {
activeGroupExists = true;
break;
@ -214,9 +217,9 @@ public class DataManager extends Manager {
}
if (!activeGroupExists) {
ParticleGroup activeGroup = new ParticleGroup(ParticleGroup.DEFAULT_NAME, new ArrayList<>());
ParticleGroup activeGroup = new ParticleGroup(ParticleGroup.DEFAULT_NAME, new HashMap<>());
this.saveParticleGroup(playerUUID, activeGroup);
groups.add(activeGroup);
groups.put(activeGroup.getName(), activeGroup);
}
PPlayer loadedPPlayer = new PPlayer(playerUUID, groups, fixedParticles, particlesHidden);
@ -309,7 +312,7 @@ public class DataManager extends Manager {
// Fill group with new particles
String createParticlesQuery = "INSERT INTO " + this.getTablePrefix() + "particle (uuid, group_uuid, id, effect, style, item_material, block_material, note, r, g, b) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement particlesStatement = connection.prepareStatement(createParticlesQuery)) {
for (ParticlePair particle : group.getParticles()) {
for (ParticlePair particle : group.getParticles().values()) {
particlesStatement.setString(1, UUID.randomUUID().toString());
particlesStatement.setString(2, groupUUID);
particlesStatement.setInt(3, particle.getId());
@ -329,13 +332,13 @@ public class DataManager extends Manager {
}));
this.getPPlayer(playerUUID, (pplayer) -> {
for (ParticleGroup existing : pplayer.getParticleGroups()) {
for (ParticleGroup existing : pplayer.getParticleGroups().values()) {
if (group.getName().equalsIgnoreCase(existing.getName())) {
pplayer.getParticleGroups().remove(existing);
pplayer.getParticleGroups().remove(existing.getName());
break;
}
}
pplayer.getParticleGroups().add(group);
pplayer.getParticleGroups().put(group.getName(), group);
});
}

View file

@ -17,7 +17,9 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.bukkit.Material;
@ -62,7 +64,7 @@ public class ParticleGroupPresetManager extends Manager {
Set<String> groupNames = groupsYaml.getKeys(false);
for (String groupName : groupNames) {
try {
List<ParticlePair> particles = new ArrayList<>();
Map<Integer, ParticlePair> particles = new HashMap<>();
String displayName = "";
Material guiIcon = Material.ENDER_CHEST;
String permission = "";
@ -184,7 +186,7 @@ public class ParticleGroupPresetManager extends Manager {
}
}
particles.add(new ParticlePair(null, id, effect, style, itemData, blockData, colorData, noteColorData));
particles.put(id, new ParticlePair(null, id, effect, style, itemData, blockData, colorData, noteColorData));
}
this.presetGroups.add(new ParticleGroupPreset(displayName, guiIcon, permission, allowPermissionOverride, new ParticleGroup(groupName, particles)));

View file

@ -3,17 +3,20 @@ package dev.esophose.playerparticles.manager;
import dev.esophose.playerparticles.PlayerParticles;
import dev.esophose.playerparticles.manager.ConfigurationManager.Setting;
import dev.esophose.playerparticles.particles.FixedParticleEffect;
import dev.esophose.playerparticles.particles.PParticle;
import dev.esophose.playerparticles.particles.PPlayer;
import dev.esophose.playerparticles.particles.ParticleEffect;
import dev.esophose.playerparticles.particles.ParticleEffect.NoteColor;
import dev.esophose.playerparticles.particles.ParticleEffect.OrdinaryColor;
import dev.esophose.playerparticles.particles.ParticlePair;
import dev.esophose.playerparticles.styles.DefaultStyles;
import dev.esophose.playerparticles.particles.PParticle;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
@ -28,9 +31,9 @@ import org.bukkit.scheduler.BukkitTask;
public class ParticleManager extends Manager implements Listener, Runnable {
/**
* The list containing all the loaded PPlayer info
* The map containing all the loaded PPlayer info
*/
private List<PPlayer> particlePlayers = new ArrayList<>();
private Map<UUID, PPlayer> particlePlayers = new HashMap<>();
/**
* The task that spawns the particles
@ -92,7 +95,7 @@ public class ParticleManager extends Manager implements Listener, Runnable {
public void onPlayerQuit(PlayerQuitEvent e) {
PPlayer pplayer = this.playerParticles.getManager(DataManager.class).getPPlayer(e.getPlayer().getUniqueId());
if (pplayer != null && pplayer.getFixedEffectIds().isEmpty())
this.particlePlayers.remove(pplayer); // Unload the PPlayer if they don't have any fixed effects
this.particlePlayers.remove(pplayer.getUniqueId()); // Unload the PPlayer if they don't have any fixed effects
}
/**
@ -100,8 +103,8 @@ public class ParticleManager extends Manager implements Listener, Runnable {
*
* @return The loaded PPlayers
*/
public List<PPlayer> getPPlayers() {
return this.particlePlayers;
public Collection<PPlayer> getPPlayers() {
return this.particlePlayers.values();
}
/**
@ -121,9 +124,8 @@ public class ParticleManager extends Manager implements Listener, Runnable {
PermissionManager permissionManager = this.playerParticles.getManager(PermissionManager.class);
// Loop over backwards so we can remove pplayers if need be
for (int i = this.particlePlayers.size() - 1; i >= 0; i--) {
PPlayer pplayer = this.particlePlayers.get(i);
// Spawn particles for each player
for (PPlayer pplayer : this.particlePlayers.values()) {
Player player = pplayer.getPlayer();
// Don't show their particles if they are in spectator mode

View file

@ -1,6 +1,7 @@
package dev.esophose.playerparticles.particles;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import org.bukkit.command.CommandSender;
@ -9,13 +10,13 @@ public class OtherPPlayer extends PPlayer {
private CommandSender sender;
public OtherPPlayer(CommandSender sender) {
super(UUID.randomUUID(), new ArrayList<>(), new ArrayList<>(), false);
super(UUID.randomUUID(), new HashMap<>(), new HashMap<>(), false);
this.sender = sender;
}
public OtherPPlayer(CommandSender sender, PPlayer other) {
super(other.getUniqueId(), other.getParticleGroups(), other.getFixedParticles(), !other.canSeeParticles());
super(other.getUniqueId(), other.getParticleGroups(), other.getFixedParticlesMap(), !other.canSeeParticles());
this.sender = sender;
}

View file

@ -2,7 +2,9 @@ package dev.esophose.playerparticles.particles;
import dev.esophose.playerparticles.styles.ParticleStyle;
import dev.esophose.playerparticles.util.ParticleUtils;
import java.util.List;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
@ -17,14 +19,14 @@ public class PPlayer {
private final UUID playerUUID;
/**
* A List of ParticleGroups this player has, the active particle group has an id of null
* A map of ParticleGroups this player has, the active particle group has an id of null
*/
private List<ParticleGroup> particleGroups;
private Map<String, ParticleGroup> particleGroups;
/**
* A List of FixedParticleEffects this user has applied
* A map of FixedParticleEffects this user has applied
*/
private List<FixedParticleEffect> fixedParticles;
private Map<Integer, FixedParticleEffect> fixedParticles;
/**
* If True, the player will not see any particles spawned by the plugin
@ -44,7 +46,7 @@ public class PPlayer {
* @param fixedParticles The FixedParticleEffects this PPlayer has
* @param particlesHidden If this player has all particles hidden from view
*/
public PPlayer(UUID uuid, List<ParticleGroup> particleGroups, List<FixedParticleEffect> fixedParticles, boolean particlesHidden) {
public PPlayer(UUID uuid, Map<String, ParticleGroup> particleGroups, Map<Integer, FixedParticleEffect> fixedParticles, boolean particlesHidden) {
this.playerUUID = uuid;
this.particleGroups = particleGroups;
this.fixedParticles = fixedParticles;
@ -99,11 +101,11 @@ public class PPlayer {
}
/**
* Get a List of ParticleGroups this user has saved
* Get a map of ParticleGroups this user has saved
*
* @return A List of ParticleGroups this player has
*/
public List<ParticleGroup> getParticleGroups() {
public Map<String, ParticleGroup> getParticleGroups() {
return this.particleGroups;
}
@ -132,7 +134,7 @@ public class PPlayer {
* @return The target named ParticleGroup
*/
public ParticleGroup getParticleGroupByName(String name) {
return this.particleGroups.stream().filter(x -> x.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
return this.particleGroups.get(name.toLowerCase());
}
/**
@ -140,8 +142,8 @@ public class PPlayer {
*
* @return A List of ParticlePairs this player has set
*/
public List<ParticlePair> getActiveParticles() {
return this.getActiveParticleGroup().getParticles();
public Collection<ParticlePair> getActiveParticles() {
return this.getActiveParticleGroup().getParticles().values();
}
/**
@ -150,10 +152,7 @@ public class PPlayer {
* @return A ParticleGroup of this player's active particles
*/
public ParticleGroup getActiveParticleGroup() {
return this.particleGroups.stream()
.filter(x -> x.getName().equals(ParticleGroup.DEFAULT_NAME))
.findFirst()
.orElseThrow(IllegalStateException::new);
return this.particleGroups.get(ParticleGroup.DEFAULT_NAME);
}
/**
@ -162,8 +161,8 @@ public class PPlayer {
* @param style The style to match
* @return A List of ParticlePairs with a matching style
*/
public List<ParticlePair> getActiveParticlesForStyle(ParticleStyle style) {
return this.getActiveParticles().stream().filter(x -> x.getStyle().equals(style)).collect(Collectors.toList());
public Set<ParticlePair> getActiveParticlesForStyle(ParticleStyle style) {
return this.getActiveParticles().stream().filter(x -> x.getStyle().equals(style)).collect(Collectors.toSet());
}
/**
@ -173,15 +172,24 @@ public class PPlayer {
* @return A ParticlePair with the given id, otherwise null
*/
public ParticlePair getActiveParticle(int id) {
return this.getActiveParticles().stream().filter(x -> x.getId() == id).findFirst().orElse(null);
return this.getActiveParticleGroup().getParticles().get(id);
}
/**
* Get the effect/style/data for all fixed particles this has has set
*
* @return A List of FixedParticleEffects this player has set
* @return A collection of FixedParticleEffects this player has set
*/
public List<FixedParticleEffect> getFixedParticles() {
public Collection<FixedParticleEffect> getFixedParticles() {
return this.fixedParticles.values();
}
/**
* Get a map of the effect/style/data for all fixed particles this has has set
*
* @return A map of FixedParticleEffects this player has set
*/
public Map<Integer, FixedParticleEffect> getFixedParticlesMap() {
return this.fixedParticles;
}
@ -192,16 +200,16 @@ public class PPlayer {
* @return The FixedParticleEffect the player owns
*/
public FixedParticleEffect getFixedEffectById(int id) {
return this.fixedParticles.stream().filter(x -> x.getId() == id).findFirst().orElse(null);
return this.fixedParticles.get(id);
}
/**
* Gets a list of ids of all fixed effect this player has
* Gets a set of ids of all fixed effect this player has
*
* @return A List of Integer ids this player's fixed effects have
* @return A set of Integer ids this player's fixed effects have
*/
public List<Integer> getFixedEffectIds() {
return this.fixedParticles.stream().map(FixedParticleEffect::getId).collect(Collectors.toList());
public Set<Integer> getFixedEffectIds() {
return this.fixedParticles.keySet();
}
/**
@ -210,7 +218,7 @@ public class PPlayer {
* @param fixedEffect The fixed effect to add
*/
public void addFixedEffect(FixedParticleEffect fixedEffect) {
this.fixedParticles.add(fixedEffect);
this.fixedParticles.put(fixedEffect.getId(), fixedEffect);
}
/**
@ -219,7 +227,7 @@ public class PPlayer {
* @param id The id of the fixed effect to remove
*/
public void removeFixedEffect(int id) {
this.fixedParticles.removeIf(x -> x.getId() == id);
this.fixedParticles.remove(id);
}
/**
@ -228,7 +236,7 @@ public class PPlayer {
* @return The next available fixed effect id
*/
public int getNextFixedEffectId() {
return ParticleUtils.getSmallestPositiveInt(this.fixedParticles.stream().mapToInt(FixedParticleEffect::getId).toArray());
return ParticleUtils.getSmallestPositiveInt(this.fixedParticles.keySet().stream().mapToInt(Integer::intValue).toArray());
}
/**

View file

@ -2,8 +2,8 @@ package dev.esophose.playerparticles.particles;
import dev.esophose.playerparticles.PlayerParticles;
import dev.esophose.playerparticles.manager.PermissionManager;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.entity.Player;
public class ParticleGroup {
@ -11,9 +11,9 @@ public class ParticleGroup {
public static final String DEFAULT_NAME = "active";
private String name;
private List<ParticlePair> particles;
private Map<Integer, ParticlePair> particles;
public ParticleGroup(String name, List<ParticlePair> particles) {
public ParticleGroup(String name, Map<Integer, ParticlePair> particles) {
this.name = name;
this.particles = particles;
}
@ -29,11 +29,11 @@ public class ParticleGroup {
}
/**
* Get the List of ParticlePairs in this group
* Get the map of ParticlePairs in this group
*
* @return The particles in this group
*/
public List<ParticlePair> getParticles() {
public Map<Integer, ParticlePair> getParticles() {
return this.particles;
}
@ -43,7 +43,7 @@ public class ParticleGroup {
* @return The default empty active ParticleGroup
*/
public static ParticleGroup getDefaultGroup() {
return new ParticleGroup(DEFAULT_NAME, new ArrayList<>());
return new ParticleGroup(DEFAULT_NAME, new HashMap<>());
}
/**
@ -60,7 +60,7 @@ public class ParticleGroup {
return false;
// Make sure the player has permission for all effects/styles in the group
for (ParticlePair particle : this.particles) {
for (ParticlePair particle : this.particles.values()) {
if (!permissionManager.hasEffectPermission(player, particle.getEffect()))
return false;