Archived
0

Sonar: [squid:S3776] make the method easier

This commit is contained in:
2019-02-11 14:44:57 +03:00
parent d4a3a49b47
commit 441e0e91d2

View File

@@ -19,6 +19,42 @@ public class CoreEventListener {
EventBus.getInstance().registerSubscribes(this);
}
private void processLoadChunks(CS_PlayerMoveEvent event, int cMinX, int cMaxX, int cMinZ, int cMaxZ) {
SC_ChunkLoadEvent eventChunkLoad = new SC_ChunkLoadEvent(event.getPlayer());
for (int cZ = cMinZ; cZ <= cMaxZ; cZ++) {
for (int cX = cMinX; cX <= cMaxX; cX++) {
int compressXZ = CompactedCoords.compressXZ(cX, cZ);
if (!event.getPlayer().getLoadedChunks().contains(compressXZ)) {
if (!event.getPlayer().getLoadedChunks().contains(compressXZ)) {
eventChunkLoad.getNeedLoadChunks().add(compressXZ);
event.getPlayer().getLoadedChunks().add(compressXZ);
}
}
}
}
if (!eventChunkLoad.getNeedLoadChunks().isEmpty()) {
EventBus.getInstance().post(eventChunkLoad);
}
}
private void processUnloadChunks(CS_PlayerMoveEvent event, int cMinX, int cMaxX, int cMinZ, int cMaxZ) {
SC_ChunkUnloadEvent eventChunkUnload = new SC_ChunkUnloadEvent(event.getPlayer());
Iterator<Integer> itr = event.getPlayer().getLoadedChunks().iterator();
while(itr.hasNext()) {
int compressXZ = itr.next();
int[] xz = CompactedCoords.uncompressXZ(compressXZ);
if (xz[0] > cMaxX || xz[0] < cMinX || xz[1] > cMaxZ || xz[1] < cMinZ) {
eventChunkUnload.getNeedUnloadChunks().add(compressXZ);
itr.remove();
}
}
if (!eventChunkUnload.getNeedUnloadChunks().isEmpty()) {
EventBus.getInstance().post(eventChunkUnload);
}
}
@Subscriber
public void handlerPlayerMoveEvent(CS_PlayerMoveEvent event) {
Chunk chunk;
@@ -33,42 +69,14 @@ public class CoreEventListener {
if (event.isRecalcChunk() || (ncX != ccX || ncZ != ccZ)) {
final int viewDistance = event.getPlayer().getSettings().getViewDistance() + 1;
int cMinX = chunk.getX() - viewDistance;
int cMaxX = chunk.getX() + viewDistance;
int cMinZ = chunk.getZ() - viewDistance;
int cMaxZ = chunk.getZ() + viewDistance;
final int cMinX = chunk.getX() - viewDistance;
final int cMaxX = chunk.getX() + viewDistance;
final int cMinZ = chunk.getZ() - viewDistance;
final int cMaxZ = chunk.getZ() + viewDistance;
SC_ChunkLoadEvent eventChunkLoad = new SC_ChunkLoadEvent(event.getPlayer());
for (int cZ = cMinZ; cZ <= cMaxZ; cZ++) {
for (int cX = cMinX; cX <= cMaxX; cX++) {
int compressXZ = CompactedCoords.compressXZ(cX, cZ);
if (!event.getPlayer().getLoadedChunks().contains(compressXZ)) {
if (!event.getPlayer().getLoadedChunks().contains(compressXZ)) {
eventChunkLoad.getNeedLoadChunks().add(compressXZ);
event.getPlayer().getLoadedChunks().add(compressXZ);
}
}
}
}
processLoadChunks(event, cMinX, cMaxX, cMinZ, cMaxZ);
processUnloadChunks(event, cMinX, cMaxX, cMinZ, cMaxZ);
if (!eventChunkLoad.getNeedLoadChunks().isEmpty()) {
EventBus.getInstance().post(eventChunkLoad);
}
SC_ChunkUnloadEvent eventChunkUnload = new SC_ChunkUnloadEvent(event.getPlayer());
Iterator<Integer> itr = event.getPlayer().getLoadedChunks().iterator();
while(itr.hasNext()) {
int compressXZ = itr.next();
int[] xz = CompactedCoords.uncompressXZ(compressXZ);
if (xz[0] > cMaxX || xz[0] < cMinX || xz[1] > cMaxZ || xz[1] < cMinZ) {
eventChunkUnload.getNeedUnloadChunks().add(compressXZ);
itr.remove();
}
}
if (!eventChunkUnload.getNeedUnloadChunks().isEmpty()) {
EventBus.getInstance().post(eventChunkUnload);
}
}
event.getPlayer().getLocation().setXYZ(