1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 14:05:25 +01:00

Use multiple maps so we can get more accurate data

This commit is contained in:
Zrips 2017-01-14 12:55:59 +02:00
parent 764cabd121
commit 8f0d598c98
10 changed files with 199 additions and 159 deletions

View File

@ -25,6 +25,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.WeakHashMap;
import java.util.logging.Logger;
@ -465,16 +466,16 @@ public class Jobs extends JavaPlugin {
public void run() {
int i = 0;
int y = 0;
int total = Jobs.getPlayerManager().getPlayerMap().size();
int total = Jobs.getPlayerManager().getMapSize();
long time = System.currentTimeMillis();
for (Entry<String, PlayerInfo> one : Jobs.getPlayerManager().getPlayerMap().entrySet()) {
for (Entry<UUID, PlayerInfo> one : Jobs.getPlayerManager().getPlayersInfoUUIDMap().entrySet()) {
if (!running)
return;
try {
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayerOffline(one);
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayerOffline(one.getValue());
if (jPlayer == null)
continue;
Jobs.getPlayerManager().getPlayersCache().put(one.getValue().getName().toLowerCase(), jPlayer);
Jobs.getPlayerManager().addPlayerToCache(jPlayer);
} catch (Exception e) {
}
i++;

View File

@ -59,11 +59,15 @@ import com.gamingmesh.jobs.stuff.Perm;
public class PlayerManager {
// private Map<String, JobsPlayer> players = Collections.synchronizedMap(new HashMap<String, JobsPlayer>());
private ConcurrentHashMap<String, JobsPlayer> playersCache = new ConcurrentHashMap<String, JobsPlayer>();
private ConcurrentHashMap<UUID, JobsPlayer> playersUUIDCache = new ConcurrentHashMap<UUID, JobsPlayer>();
private ConcurrentHashMap<String, JobsPlayer> players = new ConcurrentHashMap<String, JobsPlayer>();
private ConcurrentHashMap<UUID, JobsPlayer> playersUUID = new ConcurrentHashMap<UUID, JobsPlayer>();
private PointsData PointsDatabase = new PointsData();
private final String mobSpawnerMetadata = "jobsMobSpawner";
private HashMap<String, PlayerInfo> PlayerMap = new HashMap<String, PlayerInfo>();
private HashMap<UUID, PlayerInfo> PlayerUUIDMap = new HashMap<UUID, PlayerInfo>();
private HashMap<Integer, PlayerInfo> PlayerIDMap = new HashMap<Integer, PlayerInfo>();
private HashMap<String, PlayerInfo> PlayerNameMap = new HashMap<String, PlayerInfo>();
Jobs plugin;
public PlayerManager(Jobs plugin) {
@ -74,46 +78,68 @@ public class PlayerManager {
return this.PointsDatabase;
}
public HashMap<String, PlayerInfo> getPlayerMap() {
return this.PlayerMap;
public int getMapSize() {
return PlayerUUIDMap.size();
}
public ConcurrentHashMap<String, JobsPlayer> getPlayersCache() {
return this.playersCache;
public void clearMaps() {
PlayerUUIDMap.clear();
PlayerIDMap.clear();
PlayerNameMap.clear();
}
public ConcurrentHashMap<String, JobsPlayer> getPlayers() {
return this.players;
public void addPlayerToMap(PlayerInfo info) {
this.PlayerUUIDMap.put(info.getUuid(), info);
this.PlayerIDMap.put(info.getID(), info);
this.PlayerNameMap.put(info.getName().toLowerCase(), info);
}
public int getPlayerIdByName(String name) {
for (Entry<String, PlayerInfo> one : this.PlayerMap.entrySet()) {
if (one.getValue().getName() == null)
continue;
if (one.getValue().getName().equalsIgnoreCase(name))
return one.getValue().getID();
}
return -1;
public void addPlayerToCache(JobsPlayer jPlayer) {
if (jPlayer.getUserName() != null)
this.playersCache.put(jPlayer.getUserName(), jPlayer);
if (jPlayer.getPlayerUUID() != null)
this.playersUUIDCache.put(jPlayer.getPlayerUUID(), jPlayer);
}
public Entry<String, PlayerInfo> getPlayerInfoByName(String name) {
for (Entry<String, PlayerInfo> one : this.PlayerMap.entrySet()) {
if (one.getValue().getName() == null)
continue;
if (one.getValue().getName().equalsIgnoreCase(name))
return one;
}
return null;
public void addPlayer(JobsPlayer jPlayer) {
if (jPlayer.getUserName() != null)
this.players.put(jPlayer.getUserName(), jPlayer);
if (jPlayer.getPlayerUUID() != null)
this.playersUUID.put(jPlayer.getPlayerUUID(), jPlayer);
}
public Entry<String, PlayerInfo> getPlayerInfoById(int id) {
for (Entry<String, PlayerInfo> one : this.PlayerMap.entrySet()) {
if (one.getValue().getName() == null)
continue;
if (one.getValue().getID() == id)
return one;
}
return null;
public ConcurrentHashMap<UUID, JobsPlayer> getPlayersCache() {
return this.playersUUIDCache;
}
// public ConcurrentHashMap<String, JobsPlayer> getPlayers() {
// return this.players;
// }
public HashMap<UUID, PlayerInfo> getPlayersInfoUUIDMap() {
return this.PlayerUUIDMap;
}
public int getPlayerId(String name) {
PlayerInfo info = getPlayerInfo(name);
return info == null ? -1 : info.getID();
}
public int getPlayerId(UUID uuid) {
PlayerInfo info = PlayerUUIDMap.get(uuid);
return info == null ? -1 : info.getID();
}
public PlayerInfo getPlayerInfo(String name) {
return PlayerNameMap.get(name.toLowerCase());
}
public PlayerInfo getPlayerInfo(int id) {
return PlayerIDMap.get(id);
}
public PlayerInfo getPlayerInfo(UUID uuid) {
return PlayerUUIDMap.get(uuid);
}
/**
@ -189,7 +215,14 @@ public class PlayerManager {
* @return the player job info of the player
*/
public JobsPlayer getJobsPlayer(Player player) {
return getJobsPlayer(player.getName().toLowerCase());
return getJobsPlayer(player.getUniqueId());
}
public JobsPlayer getJobsPlayer(UUID uuid) {
JobsPlayer jPlayer = this.playersUUID.get(uuid);
if (jPlayer != null)
return jPlayer;
return this.playersUUIDCache.get(uuid);
}
/**
@ -209,19 +242,19 @@ public class PlayerManager {
* @param player - the player who's job you're getting
* @return the player job info of the player
*/
public JobsPlayer getJobsPlayerOffline(Entry<String, PlayerInfo> info) {
public JobsPlayer getJobsPlayerOffline(PlayerInfo info) {
if (info == null)
return null;
if (info.getValue().getName() == null)
if (info.getName() == null)
return null;
JobsPlayer jPlayer = new JobsPlayer(info.getValue().getName(), null);
jPlayer.setPlayerUUID(UUID.fromString(info.getKey()));
jPlayer.setUserId(info.getValue().getID());
JobsPlayer jPlayer = new JobsPlayer(info.getName(), null);
jPlayer.setPlayerUUID(info.getUuid());
jPlayer.setUserId(info.getID());
List<JobsDAOData> list = Jobs.getJobsDAO().getAllJobs(info.getValue());
List<JobsDAOData> list = Jobs.getJobsDAO().getAllJobs(info.getName(), info.getUuid());
for (JobsDAOData jobdata : list) {
if (Jobs.getJob(jobdata.getJobName()) == null)
continue;

View File

@ -36,17 +36,17 @@ public class glog implements Cmd {
for (Integer OneP : Jobs.getJobsDAO().getLognameList(time, time)) {
Entry<String, PlayerInfo> info = Jobs.getPlayerManager().getPlayerInfoById(OneP);
PlayerInfo info = Jobs.getPlayerManager().getPlayerInfo(OneP);
if (info == null)
continue;
String name = info.getValue().getName();
String name = info.getName();
if (name == null)
continue;
JobsPlayer JPlayer = Jobs.getPlayerManager().getJobsPlayer(name);
JobsPlayer JPlayer = Jobs.getPlayerManager().getJobsPlayer(info.getUuid());
if (JPlayer == null)
continue;

View File

@ -26,7 +26,9 @@ public class toggle implements Cmd {
return true;
}
String PlayerName = sender.getName();
Player player = (Player) sender;
String PlayerName = player.getName();
if (PlayerName == null || !args[0].equalsIgnoreCase("bossbar") && !args[0].equalsIgnoreCase("actionbar")) {
Jobs.getCommandManager().sendUsage(sender, "toggle");
@ -53,7 +55,7 @@ public class toggle implements Cmd {
Jobs.getBossBarToggleList().put(PlayerName, false);
sender.sendMessage(ChatColor.GREEN + Jobs.getLanguage().getMessage("command.toggle.output.off"));
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(PlayerName);
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(player.getUniqueId());
if (jPlayer != null)
jPlayer.hideBossBars();

View File

@ -1,14 +1,18 @@
package com.gamingmesh.jobs.container;
import java.util.UUID;
public class PlayerInfo {
int id;
String name;
String name = "Unknown";
private Long seen;
private UUID uuid;
public PlayerInfo(String name, int id, Long seen) {
public PlayerInfo(String name, int id, UUID uuid, Long seen) {
this.name = name;
this.id = id;
this.uuid = uuid;
this.seen = seen;
}
@ -23,4 +27,8 @@ public class PlayerInfo {
public Long getSeen() {
return seen;
}
public UUID getUuid() {
return uuid;
}
}

View File

@ -1,26 +1,18 @@
package com.gamingmesh.jobs.container;
import java.util.Map.Entry;
import com.gamingmesh.jobs.Jobs;
public final class TopList {
private int id;
private int level;
private int exp;
private PlayerInfo info;
public TopList(int id, int level, int exp) {
this.id = id;
public TopList(PlayerInfo info, int level, int exp) {
this.info = info;
this.level = level;
this.exp = exp;
}
public String getPlayerName() {
Entry<String, PlayerInfo> info = Jobs.getPlayerManager().getPlayerInfoById(this.id);
if (info != null) {
if (info.getValue().getName() != null)
return info.getValue().getName();
}
return "Unknown";
return this.info.getName();
}
public int getLevel() {

View File

@ -170,7 +170,7 @@ public abstract class JobsDAO {
if (Jobs.getGCManager().MultiServerCompatability())
userData = loadPlayerData(uuid);
else
userData = Jobs.getPlayerManager().getPlayerMap().get(uuid.toString());
userData = Jobs.getPlayerManager().getPlayerInfo(uuid);
ArrayList<JobsDAOData> jobs = new ArrayList<JobsDAOData>();
@ -306,7 +306,7 @@ public abstract class JobsDAO {
res = prest.executeQuery();
res.next();
int id = res.getInt("id");
Jobs.getPlayerManager().getPlayerMap().put(uuid.toString(), new PlayerInfo(playerName, id, System.currentTimeMillis()));
Jobs.getPlayerManager().addPlayerToMap(new PlayerInfo(playerName, id, uuid, System.currentTimeMillis()));
} catch (SQLException e) {
e.printStackTrace();
} finally {
@ -364,7 +364,7 @@ public abstract class JobsDAO {
ArrayList<JobsDAOData> jobs = new ArrayList<JobsDAOData>();
Entry<String, PlayerInfo> info = Jobs.getPlayerManager().getPlayerInfoByName(userName);
PlayerInfo info = Jobs.getPlayerManager().getPlayerInfo(userName);
if (info == null)
return jobs;
@ -375,7 +375,7 @@ public abstract class JobsDAO {
ResultSet res = null;
try {
prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "jobs` WHERE `userid` = ?;");
prest.setInt(1, info.getValue().getID());
prest.setInt(1, info.getID());
res = prest.executeQuery();
while (res.next()) {
jobs.add(new JobsDAOData(res.getString(2), res.getInt(3), res.getInt(4)));
@ -578,7 +578,7 @@ public abstract class JobsDAO {
insert = conns.prepareStatement("INSERT INTO `" + getPrefix() + "users` (`id`, `player_uuid`, `username`, `seen`) VALUES (?, ?, ?, ?);");
conns.setAutoCommit(false);
for (Entry<String, JobsPlayer> oneUser : Jobs.getPlayerManager().getPlayersCache().entrySet()) {
for (Entry<UUID, JobsPlayer> oneUser : Jobs.getPlayerManager().getPlayersCache().entrySet()) {
insert.setInt(1, oneUser.getValue().getUserId());
insert.setString(2, oneUser.getValue().getPlayerUUID().toString());
insert.setString(3, oneUser.getValue().getUserName());
@ -719,15 +719,15 @@ public abstract class JobsDAO {
while (res.next()) {
Entry<String, PlayerInfo> info = Jobs.getPlayerManager().getPlayerInfoById(res.getInt(1));
PlayerInfo info = Jobs.getPlayerManager().getPlayerInfo(res.getInt("userid"));
if (info == null)
continue;
if (info.getValue().getName() == null)
if (info.getName() == null)
continue;
TopList top = new TopList(res.getInt("userid"), res.getInt("totallvl"), 0);
TopList top = new TopList(info, res.getInt("totallvl"), 0);
names.add(top);
}
@ -800,8 +800,8 @@ public abstract class JobsDAO {
prest.setString(1, uuid.toString());
res = prest.executeQuery();
while (res.next()) {
pInfo = new PlayerInfo(res.getString("username"), res.getInt("id"), res.getLong("seen"));
Jobs.getPlayerManager().getPlayerMap().put(res.getString("player_uuid"), pInfo);
pInfo = new PlayerInfo(res.getString("username"), res.getInt("id"), uuid, res.getLong("seen"));
Jobs.getPlayerManager().addPlayerToMap(pInfo);
}
} catch (SQLException e) {
e.printStackTrace();
@ -813,7 +813,7 @@ public abstract class JobsDAO {
}
public void loadPlayerData() {
Jobs.getPlayerManager().getPlayerMap().clear();
Jobs.getPlayerManager().clearMaps();
JobsConnection conn = getConnection();
if (conn == null)
return;
@ -826,9 +826,9 @@ public abstract class JobsDAO {
long seen = System.currentTimeMillis();
try {
seen = res.getLong("seen");
Jobs.getPlayerManager().addPlayerToMap(new PlayerInfo(res.getString("username"), res.getInt("id"), UUID.fromString(res.getString("player_uuid")), seen));
} catch (Exception e) {
}
Jobs.getPlayerManager().getPlayerMap().put(res.getString("player_uuid"), new PlayerInfo(res.getString("username"), res.getInt("id"), seen));
}
} catch (SQLException e) {
e.printStackTrace();
@ -864,14 +864,14 @@ public abstract class JobsDAO {
}
jPlayer.reloadMaxExperience();
jPlayer.reloadLimits();
jPlayer.setUserId(Jobs.getPlayerManager().getPlayerMap().get(player.getUniqueId().toString()).getID());
jPlayer.setUserId(Jobs.getPlayerManager().getPlayerId(player.getUniqueId()));
Jobs.getJobsDAO().loadPoints(jPlayer);
// }
return jPlayer;
}
public void loadAllData() {
Jobs.getPlayerManager().getPlayerMap().clear();
Jobs.getPlayerManager().clearMaps();
JobsConnection conn = getConnection();
if (conn == null)
return;
@ -881,8 +881,11 @@ public abstract class JobsDAO {
prest = conn.prepareStatement("SELECT * FROM `" + prefix + "users`;");
res = prest.executeQuery();
while (res.next()) {
Jobs.getPlayerManager().getPlayerMap().put(res.getString("player_uuid"), new PlayerInfo(res.getString("username"), res.getInt("id"), res.getLong(
"seen")));
try {
Jobs.getPlayerManager().addPlayerToMap(new PlayerInfo(res.getString("username"), res.getInt("id"), UUID.fromString(res.getString("player_uuid")), res.getLong(
"seen")));
} catch (Exception e) {
}
}
} catch (SQLException e) {
e.printStackTrace();
@ -1412,16 +1415,15 @@ public abstract class JobsDAO {
res = prest.executeQuery();
while (res.next()) {
Entry<String, PlayerInfo> info = Jobs.getPlayerManager().getPlayerInfoById(res.getInt("userid"));
PlayerInfo info = Jobs.getPlayerManager().getPlayerInfo(res.getInt("userid"));
if (info == null) {
continue;
}
if (info.getValue().getName() == null)
if (info == null)
continue;
String name = info.getValue().getName();
if (info.getName() == null)
continue;
String name = info.getName();
Player player = Bukkit.getPlayer(name);
if (player != null) {
@ -1430,10 +1432,10 @@ public abstract class JobsDAO {
if (job != null && jobsinfo != null) {
JobProgression prog = jobsinfo.getJobProgression(job);
if (prog != null)
jobs.add(new TopList(jobsinfo.getUserId(), prog.getLevel(), (int) prog.getExperience()));
jobs.add(new TopList(info, prog.getLevel(), (int) prog.getExperience()));
}
} else {
jobs.add(new TopList(res.getInt("userid"), res.getInt("level"), res.getInt("experience")));
jobs.add(new TopList(info, res.getInt("level"), res.getInt("experience")));
}
}
} catch (SQLException e) {

View File

@ -22,6 +22,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.UUID;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
@ -639,7 +640,7 @@ public class JobsDAOMySQL extends JobsDAO {
prestUsersT = conn.prepareStatement("SELECT * FROM " + getPrefix() + "users;");
res4 = prestUsersT.executeQuery();
while (res4.next()) {
tempPlayerMap.put(res4.getString("player_uuid"), new PlayerInfo(res4.getString("username"), res4.getInt("id"), System.currentTimeMillis()));
tempPlayerMap.put(res4.getString("player_uuid"), new PlayerInfo(res4.getString("username"), res4.getInt("id"), UUID.fromString(res4.getString("player_uuid")), System.currentTimeMillis()));
}
} catch (Exception e) {
e.printStackTrace();

View File

@ -23,6 +23,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.UUID;
import java.util.Map.Entry;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.PlayerInfo;
@ -625,7 +626,7 @@ public class JobsDAOSQLite extends JobsDAO {
prestUsers2 = conn.prepareStatement("SELECT * FROM " + getPrefix() + "users;");
res4 = prestUsers2.executeQuery();
while (res4.next()) {
tempPlayerMap.put(res4.getString("player_uuid"), new PlayerInfo(res4.getString("username"), res4.getInt("id"), System.currentTimeMillis()));
tempPlayerMap.put(res4.getString("player_uuid"), new PlayerInfo(res4.getString("username"), res4.getInt("id"), UUID.fromString(res4.getString("player_uuid")), System.currentTimeMillis()));
}
} finally {
close(res4);

View File

@ -194,7 +194,7 @@ public class BufferedEconomy {
// Action bar stuff
Jobs.getActionBar().ShowActionBar(payment);
if (payment.getOfflinePlayer().isOnline() && Jobs.getActionBar().getVersion() > 1900) {
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(payment.getOfflinePlayer().getName());
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(payment.getOfflinePlayer().getUniqueId());
Jobs.getBBManager().ShowJobProgression(jPlayer);
}
} catch (Exception e) {