Skip to content

Commit

Permalink
Potential fix for lag on mob spawn protections
Browse files Browse the repository at this point in the history
  • Loading branch information
Dart2112 committed Apr 17, 2023
1 parent e11f945 commit 6f3e793
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions src/main/java/net/lapismc/afkplus/util/EntitySpawnManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
package net.lapismc.afkplus.util;

import net.lapismc.afkplus.AFKPlus;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import java.io.IOException;
Expand Down Expand Up @@ -62,7 +62,7 @@ public boolean shouldNaturalSpawn(Location loc) {
//Get all player entities and add them to nearbyPlayers
//This code is derived from information provided in this spigot post
//https://www.spigotmc.org/threads/what-exactly-does-mob-spawn-range-do.176889/#post-3221175
for (int i = -(spawnRange); i < (spawnRange); i++) {
/*for (int i = -(spawnRange); i < (spawnRange); i++) {
for (int j = -(spawnRange); j < (spawnRange); j++) {
Chunk chunk = loc.getWorld().getChunkAt(loc.getChunk().getX() + i, loc.getChunk().getZ() + j);
Expand All @@ -77,7 +77,29 @@ public boolean shouldNaturalSpawn(Location loc) {
playersInRange.add(p);
}
}
}*/
//Theoretically faster way of checking players
//TODO: Test this and remove if not worth it
int chunkX = loc.getChunk().getX();
int maxX = chunkX + spawnRange;
int minX = chunkX - spawnRange;
int chunkZ = loc.getChunk().getZ();
int maxZ = chunkZ + spawnRange;
int minZ = chunkZ - spawnRange;
for (Player p : Bukkit.getOnlinePlayers()) {
if (p.getGameMode() == GameMode.SPECTATOR)
continue;
Chunk c = p.getLocation().getChunk();
int playerChunkX = p.getLocation().getChunk().getX();
if (playerChunkX < maxX && playerChunkX > minX) {
int playerChunkZ = p.getLocation().getChunk().getZ();
if (playerChunkZ < maxZ && playerChunkZ > minZ) {
playersInRange.add(p);
}
}

}

//Check if all players found are AFK
if (playersInRange.size() == 0) {
//No players in range so this is just a distant natural spawn
Expand Down

0 comments on commit 6f3e793

Please sign in to comment.