mirror of
https://github.com/PikaMug/Quests.git
synced 2025-02-12 18:41:22 +01:00
Add optional MySQL implementation, part 7
This commit is contained in:
parent
a8afe550fc
commit
00a6c50c5f
@ -48,6 +48,7 @@ import org.bukkit.DyeColor;
|
||||
import org.bukkit.Effect;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Biome;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
@ -327,21 +328,35 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets every Quester that has ever played on this server
|
||||
* Get Quester from player UUID
|
||||
*
|
||||
* @return a collection of all Questers
|
||||
* @param id Player UUID
|
||||
* @return Quester, or null if UUID is null
|
||||
*/
|
||||
public Collection<Quester> getOfflineQuesters() {
|
||||
return questers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets every Quester that has ever played on this server
|
||||
*
|
||||
* @param questers a collection of Questers
|
||||
*/
|
||||
public void setOfflineQuesters(final Collection<Quester> questers) {
|
||||
this.questers = new ConcurrentSkipListSet<>(questers);
|
||||
public Quester getQuester(final UUID id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
final ConcurrentSkipListSet<Quester> set = (ConcurrentSkipListSet<Quester>) questers;
|
||||
for (final Quester q: set) {
|
||||
if (q != null && q.getUUID().equals(id)) {
|
||||
return q;
|
||||
}
|
||||
}
|
||||
final Quester quester = new Quester(this, id);
|
||||
if (depends.getCitizens() != null) {
|
||||
if (depends.getCitizens().getNPCRegistry().getByUniqueId(id) != null) {
|
||||
return quester;
|
||||
}
|
||||
}
|
||||
/*if (!quester.loadData()) {
|
||||
set.add(quester);
|
||||
} else {
|
||||
set.remove(quester);
|
||||
}*/
|
||||
final Quester q = new Quester(this, id);
|
||||
questers.add(q);
|
||||
return q;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -365,6 +380,41 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
|
||||
public void setQuesters(final LinkedList<Quester> questers) {
|
||||
this.questers = new ConcurrentSkipListSet<>(questers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get every online Quester playing on this server
|
||||
*
|
||||
* @return a collection of all online Questers
|
||||
*/
|
||||
public Collection<Quester> getOnlineQuesters() {
|
||||
final Collection<Quester> questers = new ConcurrentSkipListSet<Quester>();;
|
||||
for (final Quester q : getOfflineQuesters()) {
|
||||
if (q.getOfflinePlayer().isOnline()) {
|
||||
// Workaround for issues with the compass on fast join
|
||||
q.findCompassTarget();
|
||||
questers.add(q);
|
||||
}
|
||||
}
|
||||
return questers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets every Quester that has ever played on this server
|
||||
*
|
||||
* @return a collection of all Questers
|
||||
*/
|
||||
public Collection<Quester> getOfflineQuesters() {
|
||||
return questers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets every Quester that has ever played on this server
|
||||
*
|
||||
* @param questers a collection of Questers
|
||||
*/
|
||||
public void setOfflineQuesters(final Collection<Quester> questers) {
|
||||
this.questers = new ConcurrentSkipListSet<>(questers);
|
||||
}
|
||||
|
||||
public LinkedList<NPC> getQuestNpcs() {
|
||||
return questNpcs;
|
||||
@ -588,13 +638,15 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
|
||||
loadConditions();
|
||||
getLogger().log(Level.INFO, "Loaded " + quests.size() + " Quest(s), " + actions.size() + " Action(s), "
|
||||
+ conditions.size() + " Condition(s) and " + Lang.size() + " Phrase(s)");
|
||||
for (final Player p : getServer().getOnlinePlayers()) {
|
||||
for (final OfflinePlayer p : getServer().getOfflinePlayers()) {
|
||||
final Quester quester = new Quester(Quests.this, p.getUniqueId());
|
||||
if (quester.loadData() == false) {
|
||||
quester.saveData();
|
||||
}
|
||||
// Workaround for issues with the compass on fast join
|
||||
quester.findCompassTarget();
|
||||
if (p.isOnline()) {
|
||||
// Workaround for issues with the compass on fast join
|
||||
quester.findCompassTarget();
|
||||
}
|
||||
questers.add(quester);
|
||||
}
|
||||
if (depends.getCitizens() != null) {
|
||||
@ -1327,59 +1379,6 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Quester from player UUID
|
||||
*
|
||||
* @param id Player UUID
|
||||
* @return Quester, or null if UUID is null
|
||||
*/
|
||||
public Quester getQuester(final UUID id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
final ConcurrentSkipListSet<Quester> set = (ConcurrentSkipListSet<Quester>) questers;
|
||||
for (final Quester q: set) {
|
||||
if (q != null && q.getUUID().equals(id)) {
|
||||
return q;
|
||||
}
|
||||
}
|
||||
final Quester quester = new Quester(this, id);
|
||||
if (depends.getCitizens() != null) {
|
||||
if (depends.getCitizens().getNPCRegistry().getByUniqueId(id) != null) {
|
||||
return quester;
|
||||
}
|
||||
}
|
||||
/*if (!quester.loadData()) {
|
||||
set.add(quester);
|
||||
} else {
|
||||
set.remove(quester);
|
||||
}*/
|
||||
final Quester q = new Quester(this, id);
|
||||
questers.add(q);
|
||||
return q;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all online Questers
|
||||
*
|
||||
* @deprecated Use {@link Bukkit#getOnlinePlayers()} and then {@link #getQuester(UUID)}
|
||||
* @return list of online Questers
|
||||
*/
|
||||
@Deprecated
|
||||
public LinkedList<Quester> getOnlineQuesters() {
|
||||
final LinkedList<Quester> qs = new LinkedList<Quester>();
|
||||
for (final Player p : getServer().getOnlinePlayers()) {
|
||||
final Quester quester = new Quester(this, p.getUniqueId());
|
||||
if (!quester.loadData()) {
|
||||
quester.saveData();
|
||||
}
|
||||
qs.add(quester);
|
||||
// Workaround for issues with the compass on fast join
|
||||
quester.findCompassTarget();
|
||||
}
|
||||
return qs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load quests from file
|
||||
*/
|
||||
|
@ -52,7 +52,7 @@ public class StorageFactory {
|
||||
return storage;
|
||||
}
|
||||
|
||||
private StorageImplementation createNewImplementation(final StorageType method) {
|
||||
public StorageImplementation createNewImplementation(final StorageType method) {
|
||||
switch (method) {
|
||||
case CUSTOM:
|
||||
return CustomStorageProviders.getProvider().provide(plugin);
|
||||
|
@ -12,6 +12,7 @@
|
||||
|
||||
package me.blackvein.quests.storage.implementation;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
|
||||
import me.blackvein.quests.Quester;
|
||||
@ -33,4 +34,6 @@ public interface StorageImplementation {
|
||||
void deleteQuesterData(UUID uniqueId) throws Exception;
|
||||
|
||||
String getQuesterLastKnownName(UUID uniqueId) throws Exception;
|
||||
|
||||
Collection<UUID> getSavedUniqueIds() throws Exception;
|
||||
}
|
||||
|
@ -13,11 +13,14 @@
|
||||
package me.blackvein.quests.storage.implementation.file;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentSkipListSet;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
@ -489,6 +492,38 @@ public class SeparatedYamlStorage implements StorageImplementation {
|
||||
return data.getString("lastKnownName");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<UUID> getSavedUniqueIds() throws Exception {
|
||||
final Collection<UUID> ids = new ConcurrentSkipListSet<UUID>();
|
||||
final File folder = new File(directoryPath);
|
||||
if (!folder.exists()) {
|
||||
return ids;
|
||||
}
|
||||
final File[] listOfFiles = folder.listFiles(new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(final File dir, final String name) {
|
||||
return name.endsWith(".yml");
|
||||
}
|
||||
});
|
||||
|
||||
for (int i = 0; i < listOfFiles.length; i++) {
|
||||
if (listOfFiles[i].isFile()) {
|
||||
System.out.println("File " + listOfFiles[i].getName());
|
||||
final String name = listOfFiles[i].getName().substring(0, listOfFiles[i].getName().lastIndexOf("."));
|
||||
UUID id = null;
|
||||
try {
|
||||
id = UUID.fromString(name);
|
||||
} catch (final IllegalArgumentException e) {
|
||||
continue;
|
||||
}
|
||||
if (id != null) {
|
||||
ids.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data file for this Quester
|
||||
*
|
||||
|
@ -17,11 +17,13 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentSkipListSet;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -33,6 +35,7 @@ import me.blackvein.quests.storage.implementation.sql.connection.ConnectionFacto
|
||||
|
||||
public class SqlStorage implements StorageImplementation {
|
||||
private static final String PLAYER_SELECT = "SELECT lastknownname, questpoints FROM '{prefix}players' WHERE uuid=?";
|
||||
private static final String PLAYER_SELECT_UUID = "SELECT DISTINCT uuid FROM '{prefix}players'";
|
||||
private static final String PLAYER_SELECT_USERNAME = "SELECT lastknownname FROM '{prefix}players' WHERE uuid=? LIMIT 1";
|
||||
private static final String PLAYER_UPDATE_USERNAME = "UPDATE '{prefix}players' SET lastknownname=? WHERE uuid=?";
|
||||
private static final String PLAYER_INSERT = "INSERT INTO '{prefix}players' (uuid, lastknownname, questpoints) "
|
||||
@ -252,7 +255,6 @@ public class SqlStorage implements StorageImplementation {
|
||||
for (final Entry<Quest, Long> entry : quester.getCompletedTimes().entrySet()) {
|
||||
final int amount = quester.getAmountsCompleted().get(entry.getKey());
|
||||
try (PreparedStatement ps = c.prepareStatement(statementProcessor.apply(PLAYER_REDOABLE_QUESTS_INSERT))) {
|
||||
System.out.println("Attempting to update with amount of " + amount);
|
||||
ps.setString(1, uniqueId.toString());
|
||||
ps.setString(2, entry.getKey().getId());
|
||||
ps.setLong(3, entry.getValue());
|
||||
@ -360,4 +362,27 @@ public class SqlStorage implements StorageImplementation {
|
||||
}
|
||||
return amountsCompleted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<UUID> getSavedUniqueIds() throws Exception {
|
||||
final Collection<UUID> ids = new ConcurrentSkipListSet<UUID>();
|
||||
try (Connection c = connectionFactory.getConnection()) {
|
||||
try (PreparedStatement ps = c.prepareStatement(statementProcessor.apply(PLAYER_SELECT_UUID))) {
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
UUID id = null;
|
||||
try {
|
||||
id = UUID.fromString(rs.getString("uuid"));
|
||||
} catch (final IllegalArgumentException e) {
|
||||
continue;
|
||||
}
|
||||
if (id != null) {
|
||||
ids.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user