1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-26 04:25:15 +01:00

ExploreRegion: Fix encoding X and Z

Use a long to store the two ints (x is upper and z is lower) - this is guranteed to work with any world map size out there.

It also properly handle negative values that the old implementation some times did not properly manage.

This might use slighly more RAM, but should provide support for actual infinite / huge map sizes

Signed-off-by: Christian Winther <jippignu@gmail.com>
This commit is contained in:
Christian Winther 2022-01-07 22:35:22 +00:00
parent 93f5423300
commit a340b99349
2 changed files with 61 additions and 81 deletions

View File

@ -1,75 +1,55 @@
package com.gamingmesh.jobs.container; package com.gamingmesh.jobs.container;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.bukkit.Chunk; import org.bukkit.Chunk;
public class ExploreRegion { public class ExploreRegion {
int x; int x;
int z; int z;
private final Map<Short, ExploreChunk> chunks = new HashMap<>(); private final Map<Long, ExploreChunk> chunks = new HashMap<>();
public ExploreRegion(int x, int z) { public ExploreRegion(int x, int z) {
this.x = x; this.x = x;
this.z = z; this.z = z;
} }
public void addChunk(int x, int z, ExploreChunk chunk) { public void addChunk(int x, int z, ExploreChunk chunk) {
chunks.put(getPlace(x, z), chunk); chunks.put(getPlace(x, z), chunk);
} }
public Map<Short, ExploreChunk> getChunks() { public Map<Long, ExploreChunk> getChunks() {
return chunks; return chunks;
} }
public ExploreChunk getChunk(int x, int z) { public ExploreChunk getChunk(int x, int z) {
return getChunk(getPlace(x, z)); return getChunk(getPlace(x, z));
} }
public ExploreChunk getChunk(Chunk chunk) { public ExploreChunk getChunk(Chunk chunk) {
return getChunk(getPlace(chunk)); return getChunk(getPlace(chunk));
} }
public ExploreChunk getChunk(short place) { public ExploreChunk getChunk(long place) {
return chunks.get(place); return chunks.get(place);
} }
private static short getPlace(Chunk chunk) { private static long getPlace(Chunk chunk) {
return getPlace(chunk.getX(), chunk.getZ()); return getPlace(chunk.getX(), chunk.getZ());
} }
private static short getPlace(int x, int z) { private static long getPlace(int x, int z) {
return (short) ((x - ((x >> 5) * 32)) + ((z - ((z >> 5) * 32)) * 32)); return (((long) x) << 32) | (z & 0xffffffff);
} }
public int getChunkX(short place) { public int getChunkX(long place) {
int endX = place % 32; return (int)(place >> 32);
}
if (x < 0)
endX = -endX; public int getChunkZ(long place) {
return (int) place;
endX = x * 32 + endX; }
}
if (endX < 0) {
endX += 32;
}
return endX;
}
public int getChunkZ(short place) {
int endZ = (place - (place % 32)) / 32;
if (z < 0)
endZ = -endZ;
endZ = z * 32 + endZ;
if (endZ < 0) {
endZ += 32;
}
return endZ;
}
}

View File

@ -1542,7 +1542,7 @@ public abstract class JobsDAO {
* Join a job (create player-job entry from storage) * Join a job (create player-job entry from storage)
* @param player - player that wishes to join the job * @param player - player that wishes to join the job
* @param job - job that the player wishes to join * @param job - job that the player wishes to join
* @throws SQLException * @throws SQLException
*/ */
public List<Convert> convertDatabase() throws SQLException { public List<Convert> convertDatabase() throws SQLException {
List<Convert> list = new ArrayList<>(); List<Convert> list = new ArrayList<>();
@ -2466,7 +2466,7 @@ public abstract class JobsDAO {
int id = jobsWorld == null ? 0 : jobsWorld.getId(); int id = jobsWorld == null ? 0 : jobsWorld.getId();
if (id != 0) if (id != 0)
for (Entry<Short, ExploreChunk> oneChunk : region.getValue().getChunks().entrySet()) { for (Entry<Long, ExploreChunk> oneChunk : region.getValue().getChunks().entrySet()) {
ExploreChunk chunk = oneChunk.getValue(); ExploreChunk chunk = oneChunk.getValue();
if (chunk.getDbId() != -1) if (chunk.getDbId() != -1)
continue; continue;
@ -2599,7 +2599,7 @@ public abstract class JobsDAO {
/** /**
* Save player-job information * Save player-job information
* @param jobInfo - the information getting saved * @param jobInfo - the information getting saved
* @return * @return
*/ */
public List<Integer> getLognameList(int fromtime, int untiltime) { public List<Integer> getLognameList(int fromtime, int untiltime) {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
@ -2631,7 +2631,7 @@ public abstract class JobsDAO {
/** /**
* Show top list * Show top list
* @param toplist - toplist by jobs name * @param toplist - toplist by jobs name
* @return * @return
*/ */
public List<TopList> toplist(String jobsname) { public List<TopList> toplist(String jobsname) {
return toplist(jobsname, 0); return toplist(jobsname, 0);
@ -2640,7 +2640,7 @@ public abstract class JobsDAO {
/** /**
* Show top list * Show top list
* @param toplist - toplist by jobs name * @param toplist - toplist by jobs name
* @return * @return
*/ */
public List<TopList> toplist(String jobsname, int limit) { public List<TopList> toplist(String jobsname, int limit) {
List<TopList> jobs = new ArrayList<>(); List<TopList> jobs = new ArrayList<>();
@ -2791,4 +2791,4 @@ public abstract class JobsDAO {
return DBTables.JobsTable.getTableName(); return DBTables.JobsTable.getTableName();
} }
} }