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

Optimizing chunk explore methods to have better performance and use less

memory. Option to compact database
This commit is contained in:
Zrips 2020-01-03 13:57:45 +02:00
parent 4d0ed50e2b
commit ca8ea40f7f
9 changed files with 96 additions and 30 deletions

View File

@ -571,7 +571,7 @@ public class editjobs implements Cmd {
break;
}
Jobs.getExplore().setExploreEnabled();
Jobs.getExplore().setPlayerAmount(amount + 1);
Jobs.getExplore().setPlayerAmount(amount);
} else if (actionT == ActionType.CRAFT && myKey.startsWith("!"))
type = myKey.substring(1, myKey.length());

View File

@ -525,7 +525,7 @@ public class editquests implements Cmd {
}
Jobs.getExplore().setExploreEnabled();
Jobs.getExplore().setPlayerAmount(a + 1);
Jobs.getExplore().setPlayerAmount(a);
} else if (actionT == ActionType.CRAFT && myKey.startsWith("!"))
type = myKey.substring(1, myKey.length());

View File

@ -11,6 +11,7 @@ import com.gamingmesh.jobs.commands.Cmd;
import com.gamingmesh.jobs.commands.JobCommand;
import com.gamingmesh.jobs.container.ExploreChunk;
import com.gamingmesh.jobs.container.ExploreRegion;
import com.gamingmesh.jobs.container.PlayerInfo;
public class explored implements Cmd {
@ -40,11 +41,17 @@ public class explored implements Cmd {
sender.sendMessage(Jobs.getLanguage().getMessage("command.explored.error.noexplore"));
return false;
}
if (chunk.isFullyExplored() && Jobs.getGCManager().ExploreCompact) {
sender.sendMessage(Jobs.getLanguage().getMessage("command.explored.fullExplore"));
return true;
}
int i = 0;
for (String one : chunk.getPlayers()) {
for (Integer one : chunk.getPlayers()) {
i++;
sender.sendMessage(Jobs.getLanguage().getMessage("command.explored.list", "%place%", i, "%playername%", one));
PlayerInfo ji = Jobs.getPlayerManager().getPlayerInfo(one);
if (ji != null)
sender.sendMessage(Jobs.getLanguage().getMessage("command.explored.list", "%place%", i, "%playername%", ji.getName()));
}
sender.sendMessage(Jobs.getLanguage().getMessage("general.info.separator"));

View File

@ -1144,7 +1144,7 @@ public class ConfigManager {
continue;
}
Jobs.getExplore().setExploreEnabled();
Jobs.getExplore().setPlayerAmount(amount + 1);
Jobs.getExplore().setPlayerAmount(amount);
} else if (actionType == ActionType.CRAFT && myKey.startsWith("!"))
type = myKey.substring(1, myKey.length());

View File

@ -62,10 +62,14 @@ public class ExploreManager {
}
public ExploreRespond ChunkRespond(Player player, Chunk chunk) {
return ChunkRespond(player.getName(), chunk.getWorld().getName(), chunk.getX(), chunk.getZ());
return ChunkRespond(Jobs.getPlayerManager().getJobsPlayer(player).getUserId(), chunk.getWorld().getName(), chunk.getX(), chunk.getZ());
}
public ExploreRespond ChunkRespond(String player, String world, int x, int z) {
public ExploreRespond ChunkRespond(int playerId, Chunk chunk) {
return ChunkRespond(playerId, chunk.getWorld().getName(), chunk.getX(), chunk.getZ());
}
public ExploreRespond ChunkRespond(int playerId, String world, int x, int z) {
ExploreRegion eRegions = worlds.get(world);
if (eRegions == null) {
@ -80,7 +84,7 @@ public class ExploreManager {
eRegions.addChunk(chunk);
worlds.put(world, eRegions);
return chunk.addPlayer(player);
return chunk.addPlayer(playerId);
}
public void load(ResultSet res) {

View File

@ -158,6 +158,8 @@ public class GeneralConfigManager {
public Parser DynamicPaymentEquation;
public boolean ExploreCompact;
public boolean DisabledWorldsUse;
public List<String> DisabledWorldsList = new ArrayList<>();
@ -508,6 +510,10 @@ public class GeneralConfigManager {
DisabledWorldsUse = c.get("Optimizations.DisabledWorlds.Use", false);
DisabledWorldsList = c.get("Optimizations.DisabledWorlds.List", Arrays.asList(Bukkit.getWorlds().get(0).getName()));
c.addComment("Optimizations.Explore.Compact",
"By setting this to true when there is max amount of players explored a chunk then it will be marked as fully explored and exact players who explored it will not be saved to save some memory");
ExploreCompact = c.get("Optimizations.Explore.Compact", true);
// c.addComment("Optimizations.Purge.Use", "By setting this to true, Jobs plugin will clean data base on startup from all jobs with level 1 and at 0 exp");
// PurgeUse = c.get("Optimizations.Purge.Use", false);

View File

@ -425,6 +425,7 @@ public class LanguageManager {
c.get("command.explored.help.info", "Check who visited this chunk");
c.get("command.explored.error.noexplore", "No one visited this chunk");
c.get("command.explored.fullExplore", "&aThis chunk is fully explored");
c.get("command.explored.list", "&e%place%. %playername%");
c.get("command.browse.help.info", "List the jobs available to you.");

View File

@ -2,6 +2,7 @@ package com.gamingmesh.jobs.container;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.gamingmesh.jobs.Jobs;
@ -10,14 +11,15 @@ public class ExploreChunk {
private int x;
private int z;
private Set<String> playerNames = new HashSet<>();
private Set<Integer> playerIds = new HashSet<>();
private boolean full = false;
private Integer dbId = null;
private boolean updated = false;
public ExploreChunk(String playerName, int x, int z) {
public ExploreChunk(int playerId, int x, int z) {
this.x = x;
this.z = z;
this.playerNames.add(playerName);
this.playerIds.add(playerId);
}
public ExploreChunk(int x, int z) {
@ -25,24 +27,38 @@ public class ExploreChunk {
this.z = z;
}
public ExploreRespond addPlayer(String playerName) {
public ExploreRespond addPlayer(int playerId) {
if (full) {
return new ExploreRespond(Jobs.getExplore().getPlayerAmount() + 1, false);
}
boolean newChunkForPlayer = false;
if (!playerNames.contains(playerName)) {
if (playerNames.size() < Jobs.getExplore().getPlayerAmount()) {
playerNames.add(playerName);
if (!playerIds.contains(playerId)) {
if (playerIds.size() < Jobs.getExplore().getPlayerAmount()) {
playerIds.add(playerId);
updated = true;
}
newChunkForPlayer = true;
}
return new ExploreRespond(newChunkForPlayer ? playerNames.size() : playerNames.size() + 1, newChunkForPlayer);
if (playerIds.size() >= Jobs.getExplore().getPlayerAmount()) {
this.full = true;
if (Jobs.getGCManager().ExploreCompact)
playerIds = null;
}
public boolean isAlreadyVisited(String playerName) {
return playerNames.contains(playerName);
return new ExploreRespond(newChunkForPlayer ? getPlayers().size() : getPlayers().size() + 1, newChunkForPlayer);
}
public boolean isAlreadyVisited(int playerId) {
if (full)
return true;
return playerIds.contains(playerId);
}
public int getCount() {
return playerNames.size();
if (full)
return Jobs.getExplore().getPlayerAmount();
return playerIds.size();
}
public int getX() {
@ -53,13 +69,15 @@ public class ExploreChunk {
return z;
}
public Set<String> getPlayers() {
return playerNames;
public Set<Integer> getPlayers() {
return playerIds == null ? new HashSet<>() : playerIds;
}
public String serializeNames() {
String s = "";
for (String one : this.playerNames) {
if (playerIds == null)
return "";
for (Integer one : this.playerIds) {
if (!s.isEmpty())
s += ";";
s += one;
@ -68,10 +86,35 @@ public class ExploreChunk {
}
public void deserializeNames(String names) {
if (names.contains(";"))
playerNames.addAll(Arrays.asList(names.split(";")));
else
playerNames.add(names);
if (names.isEmpty()) {
this.full = true;
playerIds = null;
return;
}
List<String> split = Arrays.asList(names.split(";"));
for (String one : split) {
try {
int id = Integer.parseInt(one);
PlayerInfo info = Jobs.getPlayerManager().getPlayerInfo(id);
if (info != null)
playerIds.add(id);
} catch (Exception | Error e) {
updated = true;
JobsPlayer jp = Jobs.getPlayerManager().getJobsPlayer(one);
if (jp != null)
playerIds.add(jp.getUserId());
}
}
if (playerIds.size() >= Jobs.getExplore().getPlayerAmount()) {
this.full = true;
if (Jobs.getGCManager().ExploreCompact) {
playerIds = null;
if (!names.isEmpty())
updated = true;
}
}
}
public Integer getDbId() {
@ -89,4 +132,8 @@ public class ExploreChunk {
public void setUpdated(boolean updated) {
this.updated = updated;
}
public boolean isFullyExplored() {
return full;
}
}

View File

@ -26,6 +26,7 @@ import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.actions.*;
import com.gamingmesh.jobs.api.JobsChunkChangeEvent;
import com.gamingmesh.jobs.container.*;
import com.gamingmesh.jobs.stuff.Debug;
import com.gamingmesh.jobs.stuff.FurnaceBrewingHandling;
import com.gamingmesh.jobs.stuff.FurnaceBrewingHandling.ownershipFeedback;
import com.google.common.base.Objects;
@ -1743,7 +1744,7 @@ public class JobsPaymentListener implements Listener {
&& !Jobs.getGCManager().payExploringWhenGliding && player.isGliding())
return;
ExploreRespond respond = Jobs.getExplore().ChunkRespond(player, event.getNewChunk());
ExploreRespond respond = Jobs.getExplore().ChunkRespond(Jobs.getPlayerManager().getJobsPlayer(player).getUserId(), event.getNewChunk());
if (!respond.isNewChunk())
return;