Skip to content

Commit

Permalink
Fixed #72 and #75 by moving the removeWhenFarAway check to a later …
Browse files Browse the repository at this point in the history
…stage to exclude the check for the Endergetic Expansion mod.

Smaller code optimizations and performance improvements.
  • Loading branch information
MarkusBordihn committed Oct 15, 2024
1 parent f5abad8 commit 46e6afe
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 110 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
This change log includes the summarized changes.
For the full changelog, please go to the [GitHub History][history] instead.

### v11.3.0

- Fixed #72 and #75 by moving the `removeWhenFarAway` check to a later stage to exclude the check
for the Endergetic Expansion mod.
- Smaller code optimizations and performance improvements.

### v11.2.0

- Fixed #73 by ignoring corpse entities.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public final class CoreConstants {
public static final String EASY_NPC_MOD = "easy_npc";
public static final String EASY_NPC_NAME = "Easy NPC";
public static final boolean EASY_NPC_LOADED = ModList.get().isLoaded(EASY_NPC_MOD);
public static final String THE_ENDERGETIC_EXPANSION_MOD = "endergetic";
public static final String THE_ENDERGETIC_EXPANSION_NAME = "The Endergetic Expansion";
public static final boolean THE_ENDERGETIC_EXPANSION_LOADED =
ModList.get().isLoaded(THE_ENDERGETIC_EXPANSION_MOD);
public static final String FISH_OF_THIEVES_MOD = "fishofthieves";
public static final String FISH_OF_THIEVES_NAME = "Fish's Undead Rising";
public static final boolean FISH_OF_THIEVES_LOADED = ModList.get().isLoaded(FISH_OF_THIEVES_MOD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,7 @@ public static boolean isRelevantEntity(Entity entity) {
|| (entity instanceof TamableAnimal tamableAnimal
&& (tamableAnimal.getOwner() != null || tamableAnimal.getOwnerUUID() != null))
|| (entity instanceof Mob mob
&& (mob.isLeashed()
|| mob.isPersistenceRequired()
|| mob.requiresCustomPersistence()
|| !mob.removeWhenFarAway(512)))
&& (mob.isLeashed() || mob.isPersistenceRequired() || mob.requiresCustomPersistence()))
|| (entity instanceof Bee bee && bee.hasHive())
|| entity.hasCustomName());
}
Expand Down Expand Up @@ -553,6 +550,14 @@ public static boolean isRelevantEntity(Entity entity, String entityName) {
return false;
}

// Disable specific checks for mods which accessing chunk data during entity spawn / despawn.
if (!(CoreConstants.THE_ENDERGETIC_EXPANSION_LOADED
&& entityName.startsWith(CoreConstants.THE_ENDERGETIC_EXPANSION_MOD))
&& entity instanceof Mob mob
&& !mob.removeWhenFarAway(512)) {
return false;
}

// Checking entity NBT data to catch custom entities which are not extending the right classes
// or using some custom definitions which could not be easily checked.
CompoundTag compoundTag = entity.getPersistentData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public static String chunkPregeneratorModWarning(String modName) {
modName, modName);
}

public static String knownIssuesModWarning(String modName) {
public static String knownIssuesGeneralModWarning(String modName) {
return String.format(
"⚠ There are known issue with the %s mod, please do not report any issue!", modName);
"⚠ There are known issues with the %s mod, please report any related issues to the %s mod author first!",
modName, modName);
}

public static String knownIssuesSpawnModWarning(String modName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,14 @@ private static void updatePlayerPositions() {
}

PlayerList playerList = minecraftServer.getPlayerList();
if (playerList == null) {
if (playerList == null || playerList.getPlayerCount() == 0) {
playerPositionMap.clear();
return;
}

int numberOfPlayers = playerList.getPlayerCount();
if (numberOfPlayers == 0) {
if (!playerPositionMap.isEmpty()) {
playerPositionMap = new ConcurrentHashMap<>();
}
return;
}

List<ServerPlayer> serverPlayerList = playerList.getPlayers();
int viewDistance = playerList.getViewDistance();
int simulationDistance = playerList.getSimulationDistance();
for (ServerPlayer player : serverPlayerList) {
for (ServerPlayer player : playerList.getPlayers()) {
if (player.isAlive() && !player.hasDisconnected()) {
updatePlayerPosition(player, viewDistance, simulationDistance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,9 @@ public static long[] getTickTime(ResourceKey<Level> level) {

public static double getAverageTickTime(ServerLevel serverLevel) {
long[] tickTimes = getTickTime(serverLevel.dimension());
if (tickTimes != null) {
return Arrays.stream(tickTimes).average().orElse(Double.NaN) / 1000000;
}
return 0;
return (tickTimes != null)
? Arrays.stream(tickTimes).average().orElse(Double.NaN) / 1000000
: 0;
}

public static float getAverageTickTime() {
Expand All @@ -177,19 +176,13 @@ private static void updateGameDifficulty(Difficulty difficulty) {
return;
}
gameDifficulty = difficulty;
switch (difficulty) {
case EASY:
gameDifficultyFactor = COMMON.gameDifficultyFactorEasy.get();
break;
case PEACEFUL:
gameDifficultyFactor = COMMON.gameDifficultyFactorPeaceful.get();
break;
case HARD:
gameDifficultyFactor = COMMON.gameDifficultyFactorHard.get();
break;
default:
gameDifficultyFactor = COMMON.gameDifficultyFactorNormal.get();
}
gameDifficultyFactor =
switch (difficulty) {
case EASY -> COMMON.gameDifficultyFactorEasy.get();
case PEACEFUL -> COMMON.gameDifficultyFactorPeaceful.get();
case HARD -> COMMON.gameDifficultyFactorHard.get();
default -> COMMON.gameDifficultyFactorNormal.get();
};
log.info(
"{} Game difficulty is set to {} with a {} factor.",
Constants.LOG_PREFIX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,47 +148,33 @@ public boolean update(int posX, int posY, int posZ, int viewAreaDistance, String
return false;
}

// Update position for change detection and debugging purpose.
// Update position and level for change detection and better view area calculation.
this.posX = posX;
this.posY = posY;
this.posZ = posZ;

// Update level for better view area calculation.
this.levelName = levelName;
boolean isNether = levelName.equals(NETHER);
boolean isTheEnd = levelName.equals(THE_END);

// Limit max view area distance to consider mods which changes these factors.
this.viewAreaDistance = viewAreaDistance;
if (viewAreaDistance > MAX_VIEW_AREA_DISTANCE) {
viewAreaDistance = MAX_VIEW_AREA_DISTANCE;
}

// Expand view area distance for Nether and The End.
// Limit max view area distance and expand for Nether and The End.
this.viewAreaDistance = Math.min(viewAreaDistance, MAX_VIEW_AREA_DISTANCE);
if (isNether) {
viewAreaDistance = (int) (viewAreaDistance * NETHER_EXPAND_FACTOR);
} else if (isTheEnd) {
viewAreaDistance = (int) (viewAreaDistance * THE_END_EXPAND_FACTOR);
}
this.blocksViewDistance = viewAreaDistance;

// Simple calculation for X
// Simple calculation for X, Y, and Z
this.startX = posX - viewAreaDistance;
this.stopX = posX + viewAreaDistance;

// Simple calculation for Y
if (this.levelName.equals(NETHER)) {
this.startY = Math.max(posY - viewAreaDistance, MIN_BUILD_HEIGHT);
this.stopY = Math.min(posY + viewAreaDistance, MAX_BUILD_HEIGHT_NETHER);
} else if (this.levelName.equals(THE_END)) {
this.startY = Math.max(posY - viewAreaDistance, MIN_BUILD_HEIGHT);
this.stopY = Math.min(posY + viewAreaDistance, MAX_BUILD_HEIGHT_THE_END);
} else {
this.startY = Math.max(posY - viewAreaDistance, MIN_BUILD_HEIGHT);
this.stopY = Math.min(posY + viewAreaDistance, MAX_BUILD_HEIGHT);
}

// Simple calculation for Z
this.startY = Math.max(posY - viewAreaDistance, MIN_BUILD_HEIGHT);
this.stopY =
Math.min(
posY + viewAreaDistance,
isNether
? MAX_BUILD_HEIGHT_NETHER
: isTheEnd ? MAX_BUILD_HEIGHT_THE_END : MAX_BUILD_HEIGHT);
this.startZ = posZ - viewAreaDistance;
this.stopZ = posZ + viewAreaDistance;

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false
# Project
version=11.2.0
version=11.3.0
# Mod
mod_author=Markus Bordihn
vendor_name=markusbordihn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,53 +93,7 @@ public static void handleServerAboutToStartEvent(ServerAboutToStartEvent event)
ignoreDimensionList = new HashSet<>(COMMON.spawnIgnoreDimensionList.get());
}

@SubscribeEvent
public static void handleServerStarting(ServerStartingEvent event) {
if (!allowList.isEmpty()) {
log.info("{} Spawn allow list: {}", Constants.LOG_PREFIX, allowList);
}
if (!denyList.isEmpty()) {
log.info("{} Spawn deny list: {}", Constants.LOG_PREFIX, denyList);
}
if (!ignoreDimensionList.isEmpty()) {
log.info("{} Ignore dimension list: {}", Constants.LOG_PREFIX, ignoreDimensionList);
}
if (Boolean.TRUE.equals(COMMON.spawnAggressiveMode.get())) {
log.warn("Enable more aggressive spawn optimizations!");
} else {
log.info(
"If you want to use a more aggressive spawn optimization, please set 'spawnAggressiveMode' to 'true'");
}
if (Boolean.FALSE.equals(COMMON.viewAreaEnabled.get())) {
log.info("Disable view area optimizations!");
}
if (Boolean.TRUE.equals(COMMON.spawnLimitationEnabled.get())) {
if (COMMON.spawnLimitationLimiter.get() > 0) {
log.info(
"{} ✓ Enable limiter and block randomly every {} mob from spawning ...",
Constants.LOG_PREFIX,
COMMON.spawnLimitationLimiter.get());
}
if (COMMON.spawnLimitationMaxMobsPerPlayer.get() > 0) {
log.info(
"{} ✓ Enable spawn rate control with max {} per player ...",
Constants.LOG_PREFIX,
COMMON.spawnLimitationMaxMobsPerPlayer.get());
}
if (COMMON.spawnLimitationMaxMobsPerWorld.get() > 0) {
log.info(
"{} ✓ Enable spawn rate control with max {} per world ...",
Constants.LOG_PREFIX,
COMMON.spawnLimitationMaxMobsPerWorld.get());
}
if (COMMON.spawnLimitationMaxMobsPerServer.get() > 0) {
log.info(
"{} ✓ Enable spawn rate control with max {} per server ...",
Constants.LOG_PREFIX,
COMMON.spawnLimitationMaxMobsPerServer.get());
}
}

private static void showModSpecificWarnings() {
// Added warning for chunk optimization Mods
if (CoreConstants.CHUNK_PREGEN_LOADED) {
log.warn(() -> WarnMessages.chunkPregeneratorModWarning(CoreConstants.CHUNK_PREGEN_NAME));
Expand All @@ -164,6 +118,12 @@ public static void handleServerStarting(ServerStartingEvent event) {
WarnMessages.conflictingFeaturesModWarning(
CoreConstants.INCONTROL_NAME, "controls the mob spawns and entity spawns"));
}
if (CoreConstants.THE_ENDERGETIC_EXPANSION_LOADED) {
log.warn(
() ->
WarnMessages.knownIssuesGeneralModWarning(
CoreConstants.THE_ENDERGETIC_EXPANSION_NAME));
}

// Disabled optimization warning for specific Mods
if (CoreConstants.CREATE_LOADED) {
Expand All @@ -176,9 +136,6 @@ public static void handleServerStarting(ServerStartingEvent event) {
if (CoreConstants.BOTANIA_LOADED) {
log.warn(() -> WarnMessages.disabledOptimizationModWarning(CoreConstants.BOTANIA_NAME));
}
if (CoreConstants.CREATE_LOADED) {
log.warn(() -> WarnMessages.disabledOptimizationModWarning(CoreConstants.CREATE_NAME));
}
if (CoreConstants.INDUSTRIAL_FOREGOING_LOADED) {
log.warn(
() ->
Expand Down Expand Up @@ -209,6 +166,56 @@ public static void handleServerStarting(ServerStartingEvent event) {
}
}

@SubscribeEvent
public static void handleServerStarting(ServerStartingEvent event) {
if (!allowList.isEmpty()) {
log.info("{} Spawn allow list: {}", Constants.LOG_PREFIX, allowList);
}
if (!denyList.isEmpty()) {
log.info("{} Spawn deny list: {}", Constants.LOG_PREFIX, denyList);
}
if (!ignoreDimensionList.isEmpty()) {
log.info("{} Ignore dimension list: {}", Constants.LOG_PREFIX, ignoreDimensionList);
}
if (Boolean.TRUE.equals(COMMON.spawnAggressiveMode.get())) {
log.warn("Enable more aggressive spawn optimizations!");
} else {
log.info(
"If you want to use a more aggressive spawn optimization, please set 'spawnAggressiveMode' to 'true'");
}
if (Boolean.FALSE.equals(COMMON.viewAreaEnabled.get())) {
log.info("Disable view area optimizations!");
}
if (Boolean.TRUE.equals(COMMON.spawnLimitationEnabled.get())) {
if (COMMON.spawnLimitationLimiter.get() > 0) {
log.info(
"{} ✓ Enable limiter and block randomly every {} mob from spawning ...",
Constants.LOG_PREFIX,
COMMON.spawnLimitationLimiter.get());
}
if (COMMON.spawnLimitationMaxMobsPerPlayer.get() > 0) {
log.info(
"{} ✓ Enable spawn rate control with max {} per player ...",
Constants.LOG_PREFIX,
COMMON.spawnLimitationMaxMobsPerPlayer.get());
}
if (COMMON.spawnLimitationMaxMobsPerWorld.get() > 0) {
log.info(
"{} ✓ Enable spawn rate control with max {} per world ...",
Constants.LOG_PREFIX,
COMMON.spawnLimitationMaxMobsPerWorld.get());
}
if (COMMON.spawnLimitationMaxMobsPerServer.get() > 0) {
log.info(
"{} ✓ Enable spawn rate control with max {} per server ...",
Constants.LOG_PREFIX,
COMMON.spawnLimitationMaxMobsPerServer.get());
}
}

showModSpecificWarnings();
}

@SubscribeEvent
public static void handleServerStarted(ServerStartedEvent event) {
serverStarted = true;
Expand Down

0 comments on commit 46e6afe

Please sign in to comment.