1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-12-31 21:37:57 +01:00

PreLoad player data in async mode to minimize server load time

Fix for converter not finding lastseen time
Get player name from appropriate place if not defined from before
This commit is contained in:
Zrips 2016-11-19 10:47:40 +02:00
parent e44b208fad
commit 911907818a
4 changed files with 141 additions and 126 deletions

View File

@ -139,6 +139,7 @@ public class Jobs extends JavaPlugin {
private static NMS nms;
private static ActionBar actionbar;
private boolean running = false;
public void setMcMMOlistener() {
McMMOlistener = new McMMOlistener(this);
@ -462,29 +463,37 @@ public class Jobs extends JavaPlugin {
} catch (IOException e1) {
e1.printStackTrace();
}
int i = 0;
int y = 0;
int total = Jobs.getPlayerManager().getPlayerMap().size();
long time = System.currentTimeMillis();
for (Entry<String, PlayerInfo> one : Jobs.getPlayerManager().getPlayerMap().entrySet()) {
try {
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayerOffline(one);
if (jPlayer == null)
continue;
Jobs.getPlayerManager().getPlayersCache().put(one.getValue().getName().toLowerCase(), jPlayer);
} catch (Exception e) {
}
i++;
y++;
if (y >= 1000) {
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Loaded " + i + "/" + total + " players data");
y = 0;
}
}
dao.getMap().clear();
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Preloaded " + i + " players data in " + ((int) (((System.currentTimeMillis() - time)
/ 1000d) * 100) / 100D));
Bukkit.getScheduler().runTaskAsynchronously(this, new Runnable() {
@Override
public void run() {
int i = 0;
int y = 0;
int total = Jobs.getPlayerManager().getPlayerMap().size();
long time = System.currentTimeMillis();
for (Entry<String, PlayerInfo> one : Jobs.getPlayerManager().getPlayerMap().entrySet()) {
if (!running)
return;
try {
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayerOffline(one);
if (jPlayer == null)
continue;
Jobs.getPlayerManager().getPlayersCache().put(one.getValue().getName().toLowerCase(), jPlayer);
} catch (Exception e) {
}
i++;
y++;
if (y >= 1000) {
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Loaded " + i + "/" + total + " players data");
y = 0;
}
}
dao.getMap().clear();
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Preloaded " + i + " players data in " + ((int) (((System.currentTimeMillis() - time)
/ 1000d) * 100) / 100D));
return;
}
});
// add all online players
for (Player online : Bukkit.getServer().getOnlinePlayers()) {
Jobs.getPlayerManager().playerJoin(online);
@ -644,7 +653,7 @@ public class Jobs extends JavaPlugin {
@Override
public void onEnable() {
running = true;
String packageName = getServer().getClass().getPackage().getName();
String[] packageSplit = packageName.split("\\.");
version = packageSplit[packageSplit.length - 1].substring(0, packageSplit[packageSplit.length - 1].length() - 3);
@ -744,6 +753,7 @@ public class Jobs extends JavaPlugin {
@Override
public void onDisable() {
running = false;
GUIManager.CloseInventories();
shopManager.CloseInventories();
dao.saveExplore();
@ -770,15 +780,15 @@ public class Jobs extends JavaPlugin {
public static void action(JobsPlayer jPlayer, ActionInfo info, Block block) {
action(jPlayer, info, block, null, null);
}
public static void action(JobsPlayer jPlayer, ActionInfo info, Entity ent) {
action(jPlayer, info, null, ent, null);
}
public static void action(JobsPlayer jPlayer, ActionInfo info, Entity ent, LivingEntity victim) {
action(jPlayer, info, null, ent, victim);
}
public static void action(JobsPlayer jPlayer, ActionInfo info, Block block, Entity ent, LivingEntity victim) {
if (jPlayer == null)

View File

@ -1,98 +1,99 @@
package com.gamingmesh.jobs.commands.list;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.commands.Cmd;
import com.gamingmesh.jobs.commands.JobCommand;
import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.container.Log;
import com.gamingmesh.jobs.container.LogAmounts;
import com.gamingmesh.jobs.container.PlayerInfo;
import com.gamingmesh.jobs.stuff.Sorting;
import com.gamingmesh.jobs.stuff.TimeManage;
public class glog implements Cmd {
@Override
@JobCommand(1200)
public boolean perform(Jobs plugin, final CommandSender sender, final String[] args) {
if (args.length != 0) {
Jobs.getCommandManager().sendUsage(sender, "glog");
return true;
}
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
Map<LogAmounts, Double> unsortMap = new HashMap<LogAmounts, Double>();
int time = TimeManage.timeInInt();
for (Integer OneP : Jobs.getJobsDAO().getLognameList(time, time)) {
Entry<String, PlayerInfo> info = Jobs.getPlayerManager().getPlayerInfoById(OneP);
if (info == null)
continue;
String name = info.getValue().getName();
if (name == null)
continue;
JobsPlayer JPlayer = Jobs.getPlayerManager().getJobsPlayer(name);
if (JPlayer == null)
continue;
List<Log> logList = JPlayer.getLog();
if (logList.size() == 0)
continue;
for (Log one : logList) {
HashMap<String, LogAmounts> AmountList = one.getAmountList();
for (Entry<String, LogAmounts> oneMap : AmountList.entrySet()) {
oneMap.getValue().setUsername(name);
oneMap.getValue().setAction(one.getActionType());
unsortMap.put(oneMap.getValue(), oneMap.getValue().getMoney());
}
}
}
unsortMap = Sorting.sortDoubleDESCByLog(unsortMap);
int count = 1;
int max = 10;
sender.sendMessage(Jobs.getLanguage().getMessage("command.glog.output.topline"));
for (Entry<LogAmounts, Double> one : unsortMap.entrySet()) {
LogAmounts info = one.getKey();
sender.sendMessage(Jobs.getLanguage().getMessage("command.glog.output.list",
"%username%", one.getKey().getUsername(),
"%number%", count,
"%action%", info.getAction(),
"%item%", one.getKey().getItemName().replace(":0", "").replace("_", " ").toLowerCase(),
"%qty%", one.getKey().getCount(),
"%money%", one.getKey().getMoney(),
"%exp%", one.getKey().getExp()));
count++;
if (count > max)
break;
}
if (unsortMap.size() == 0) {
sender.sendMessage(Jobs.getLanguage().getMessage("command.glog.output.nodata"));
}
sender.sendMessage(Jobs.getLanguage().getMessage("command.glog.output.bottomline"));
return;
}
});
return true;
}
}
package com.gamingmesh.jobs.commands.list;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.commands.Cmd;
import com.gamingmesh.jobs.commands.JobCommand;
import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.container.Log;
import com.gamingmesh.jobs.container.LogAmounts;
import com.gamingmesh.jobs.container.PlayerInfo;
import com.gamingmesh.jobs.stuff.Debug;
import com.gamingmesh.jobs.stuff.Sorting;
import com.gamingmesh.jobs.stuff.TimeManage;
public class glog implements Cmd {
@Override
@JobCommand(1200)
public boolean perform(Jobs plugin, final CommandSender sender, final String[] args) {
if (args.length != 0) {
Jobs.getCommandManager().sendUsage(sender, "glog");
return true;
}
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
Map<LogAmounts, Double> unsortMap = new HashMap<LogAmounts, Double>();
int time = TimeManage.timeInInt();
for (Integer OneP : Jobs.getJobsDAO().getLognameList(time, time)) {
Entry<String, PlayerInfo> info = Jobs.getPlayerManager().getPlayerInfoById(OneP);
if (info == null)
continue;
String name = info.getValue().getName();
if (name == null)
continue;
JobsPlayer JPlayer = Jobs.getPlayerManager().getJobsPlayer(name);
if (JPlayer == null)
continue;
List<Log> logList = JPlayer.getLog();
if (logList.size() == 0)
continue;
for (Log one : logList) {
HashMap<String, LogAmounts> AmountList = one.getAmountList();
for (Entry<String, LogAmounts> oneMap : AmountList.entrySet()) {
oneMap.getValue().setUsername(name);
oneMap.getValue().setAction(one.getActionType());
unsortMap.put(oneMap.getValue(), oneMap.getValue().getMoney());
}
}
}
unsortMap = Sorting.sortDoubleDESCByLog(unsortMap);
int count = 1;
int max = 10;
sender.sendMessage(Jobs.getLanguage().getMessage("command.glog.output.topline"));
for (Entry<LogAmounts, Double> one : unsortMap.entrySet()) {
LogAmounts info = one.getKey();
sender.sendMessage(Jobs.getLanguage().getMessage("command.glog.output.list",
"%username%", one.getKey().getUsername(),
"%number%", count,
"%action%", info.getAction(),
"%item%", one.getKey().getItemName().replace(":0", "").replace("_", " ").toLowerCase(),
"%qty%", one.getKey().getCount(),
"%money%", one.getKey().getMoney(),
"%exp%", one.getKey().getExp()));
count++;
if (count > max)
break;
}
if (unsortMap.size() == 0) {
sender.sendMessage(Jobs.getLanguage().getMessage("command.glog.output.nodata"));
}
sender.sendMessage(Jobs.getLanguage().getMessage("command.glog.output.bottomline"));
return;
}
});
return true;
}
}

View File

@ -69,7 +69,7 @@ public class JobsPlayer {
// log
private List<Log> logList = new ArrayList<Log>();
private Long seen;
private Long seen = System.currentTimeMillis();
public JobsPlayer(String userName, OfflinePlayer player) {
this.userName = userName;
@ -309,6 +309,10 @@ public class JobsPlayer {
* @return the userName
*/
public String getUserName() {
if (userName == null && player != null)
userName = player.getName();
if (userName == null && OffPlayer != null)
userName = OffPlayer.getName();
return userName;
}

View File

@ -521,7 +521,7 @@ public abstract class JobsDAO {
insert.setInt(1, oneUser.getValue().getUserId());
insert.setString(2, oneUser.getValue().getPlayerUUID().toString());
insert.setString(3, oneUser.getValue().getUserName());
insert.setLong(4, oneUser.getValue().getSeen());
insert.setLong(4, oneUser.getValue().getSeen() == null ? System.currentTimeMillis() : oneUser.getValue().getSeen());
insert.addBatch();
}
insert.executeBatch();