Address concurrent modification of quester list, fixes #1439

This commit is contained in:
PikaMug 2020-11-04 16:11:47 -05:00
parent 42815bdaec
commit c512261fb7
2 changed files with 36 additions and 16 deletions

View File

@ -233,6 +233,11 @@ public class Quester {
public Quester(final Quests plugin) { public Quester(final Quests plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
public Quester(final Quests plugin, final UUID uuid) {
this.plugin = plugin;
this.id = uuid;
}
public UUID getUUID() { public UUID getUUID() {
return id; return id;
@ -3189,6 +3194,23 @@ public class Quester {
return data; return data;
} }
/**
* Check whether data file for this Quester exists
*
* @return file if exists, otherwise null
*/
public File getDataFile() {
File dataFile = new File(plugin.getDataFolder(), "data" + File.separator + id.toString() + ".yml");
if (!dataFile.exists()) {
final OfflinePlayer p = getOfflinePlayer();
dataFile = new File(plugin.getDataFolder(), "data" + File.separator + p.getName() + ".yml");
if (!dataFile.exists()) {
return null;
}
}
return dataFile;
}
/** /**
* Load data of the Quester from file * Load data of the Quester from file
* *
@ -3198,15 +3220,12 @@ public class Quester {
public boolean loadData() { public boolean loadData() {
final FileConfiguration data = new YamlConfiguration(); final FileConfiguration data = new YamlConfiguration();
try { try {
File dataFile = new File(plugin.getDataFolder(), "data" + File.separator + id.toString() + ".yml"); final File dataFile = getDataFile();
if (dataFile.exists() == false) { if (dataFile != null) {
final OfflinePlayer p = getOfflinePlayer(); data.load(dataFile);
dataFile = new File(plugin.getDataFolder(), "data" + File.separator + p.getName() + ".yml"); } else {
if (dataFile.exists() == false) { return false;
return false;
}
} }
data.load(dataFile);
} catch (final IOException e) { } catch (final IOException e) {
return false; return false;
} catch (final InvalidConfigurationException e) { } catch (final InvalidConfigurationException e) {

View File

@ -1304,22 +1304,24 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
if (id == null) { if (id == null) {
return null; return null;
} }
for (final Quester q: questers) { final ConcurrentSkipListSet<Quester> set = (ConcurrentSkipListSet<Quester>) questers;
for (final Quester q: set) {
if (q != null && q.getUUID().equals(id)) { if (q != null && q.getUUID().equals(id)) {
return q; return q;
} }
} }
final Quester quester = new Quester(this); final Quester quester = new Quester(this, id);
quester.setUUID(id);
if (depends.getCitizens() != null) { if (depends.getCitizens() != null) {
if (depends.getCitizens().getNPCRegistry().getByUniqueId(id) != null) { if (depends.getCitizens().getNPCRegistry().getByUniqueId(id) != null) {
return quester; return quester;
} }
} }
questers.add(quester);
if (!quester.loadData()) { if (!quester.loadData()) {
questers.remove(quester); set.add(quester);
} else {
set.remove(quester);
} }
setOfflineQuesters(set);
return quester; return quester;
} }
@ -1333,9 +1335,8 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
public LinkedList<Quester> getOnlineQuesters() { public LinkedList<Quester> getOnlineQuesters() {
final LinkedList<Quester> qs = new LinkedList<Quester>(); final LinkedList<Quester> qs = new LinkedList<Quester>();
for (final Player p : getServer().getOnlinePlayers()) { for (final Player p : getServer().getOnlinePlayers()) {
final Quester quester = new Quester(this); final Quester quester = new Quester(this, p.getUniqueId());
quester.setUUID(p.getUniqueId()); if (!quester.loadData()) {
if (quester.loadData() == false) {
quester.saveData(); quester.saveData();
} }
qs.add(quester); qs.add(quester);