Skip to content

Commit

Permalink
Implement database for name histories.
Browse files Browse the repository at this point in the history
  • Loading branch information
Starjon committed Jun 20, 2019
1 parent 0f0d4df commit 46fb2aa
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 138 deletions.
47 changes: 38 additions & 9 deletions src/main/java/de/iani/playerUUIDCache/PlayerUUIDCache.java
Expand Up @@ -14,8 +14,10 @@
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
Expand Down Expand Up @@ -600,7 +602,6 @@ protected synchronized void updateProfileProperties(boolean updateDB, CachedPlay

protected synchronized void updateHistory(boolean updateDB, NameHistory history) {
if (nameHistories != null) {
NameHistory oldHistory = nameHistories.get(history.getUUID());
nameHistories.put(history.getUUID(), history);
}
if (updateDB) {
Expand All @@ -612,14 +613,6 @@ protected synchronized void updateHistory(boolean updateDB, NameHistory history)
getLogger().log(Level.SEVERE, "Error while trying to access the database", e);
}
}
if (binaryStorage != null) {
try {
databaseUpdates++;
binaryStorage.addOrUpdateHistory(history);
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Error while trying to access the storage file", e);
}
}
}
}

Expand Down Expand Up @@ -725,4 +718,40 @@ public Future<NameHistory> loadNameHistoryAsynchronously(UUID playerUUID) {
return null;
}

@Override
public Set<UUID> getCurrentAndPreviousPlayers(String name) {
Set<UUID> result = null;
if (database != null) {
try {
databaseQueries++;
result = database.getKnownUsersFromHistory(name);
} catch (SQLException e) {
getLogger().log(Level.SEVERE, "Error while trying to access the database", e);
}
}

if (result == null) {
result = new HashSet<>();
for (NameHistory history : nameHistories.values()) {
if (history.getFirstName().equals(name)) {
result.add(history.getUUID());
continue;
}
for (NameChange change : history.getNameChanges()) {
if (change.getNewName().equals(name)) {
result.add(history.getUUID());
break;
}
}
}
}

CachedPlayer current = getPlayer(name, false);
if (current != null) {
result.add(current.getUniqueId());
}

return result;
}

}
11 changes: 11 additions & 0 deletions src/main/java/de/iani/playerUUIDCache/PlayerUUIDCacheAPI.java
@@ -1,6 +1,7 @@
package de.iani.playerUUIDCache;

import java.util.Collection;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Future;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -222,4 +223,14 @@ public interface PlayerUUIDCacheAPI {
* @return a Future to query the result
*/
Future<NameHistory> loadNameHistoryAsynchronously(UUID playerUUID);

/**
* Returns the UUIDs of all player known to have used the given name in the past or present.
* This method will never query mojang. If no players are found, an empty set is returned.
*
* @param name
* the name to search for
* @return a set of all known players associated with that name
*/
Set<UUID> getCurrentAndPreviousPlayers(String name);
}
14 changes: 14 additions & 0 deletions src/main/java/de/iani/playerUUIDCache/SQLConfig.java
Expand Up @@ -15,6 +15,10 @@ public class SQLConfig {

private String profilestablename = "playerprofiles";

private String namehistoriestablename = "namehistories";

private String namechangestablename = "namechanges";

public SQLConfig(ConfigurationSection section) {
if (section != null) {
host = section.getString("host", host);
Expand All @@ -23,6 +27,8 @@ public SQLConfig(ConfigurationSection section) {
database = section.getString("database", database);
tablename = section.getString("tablename", tablename);
profilestablename = section.getString("profilestablename", profilestablename);
namehistoriestablename = section.getString("namehistoriestablename", namehistoriestablename);
namechangestablename = section.getString("namechangestablename", namechangestablename);
}
}

Expand All @@ -49,4 +55,12 @@ public String getTableName() {
public String getProfilesTableName() {
return profilestablename;
}

public String getNameHistoriesTableName() {
return namehistoriestablename;
}

public String getNameChangesTableName() {
return namechangestablename;
}
}

0 comments on commit 46fb2aa

Please sign in to comment.