Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jvnet.hk2.annotations.Service;

import org.mvplugins.multiverse.core.dynamiclistener.EventRunnable;
import org.mvplugins.multiverse.core.dynamiclistener.annotations.EventClass;
import org.mvplugins.multiverse.core.dynamiclistener.annotations.EventMethod;
import org.mvplugins.multiverse.core.utils.compatibility.EntityCompatibility;
import org.mvplugins.multiverse.core.world.WorldManager;

import java.util.Arrays;
Expand Down Expand Up @@ -96,10 +98,7 @@
@Override
public void onEvent(PreCreatureSpawnEvent event) {
// Always allow custom command and plugins to spawn creatures
if (event.getReason() == SpawnReason.CUSTOM
|| event.getReason() == SpawnReason.COMMAND
|| event.getReason() == SpawnReason.BREEDING
|| event.getReason() == SpawnReason.SPAWNER_EGG) {
if (isAlwaysAllowedSpawnReason(event.getReason())) {
return;
}

Expand Down Expand Up @@ -127,10 +126,7 @@
}

// Always allow custom command and plugins to spawn creatures
if (event.getSpawnReason() == SpawnReason.CUSTOM
|| event.getSpawnReason() == SpawnReason.COMMAND
|| event.getSpawnReason() == SpawnReason.BREEDING
|| event.getSpawnReason() == SpawnReason.SPAWNER_EGG) {
if (isAlwaysAllowedSpawnReason(event.getSpawnReason())) {
return;
}

Expand Down Expand Up @@ -168,6 +164,7 @@
.peek(world -> {
long count = Arrays.stream(event.getChunk().getEntities())
.filter(entity -> !(entity instanceof Player))
.filter(entity -> !isAlwaysAllowedSpawnReason(EntityCompatibility.getEntitySpawnReason(entity)))

Check warning on line 167 in src/main/java/org/mvplugins/multiverse/core/listeners/MVEntityListener.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 124). Raw Output: /github/workspace/src/main/java/org/mvplugins/multiverse/core/listeners/MVEntityListener.java:167:0: warning: Line is longer than 120 characters (found 124). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
.filter(entity -> !world.getEntitySpawnConfig().shouldAllowSpawn(entity))
.peek(Entity::remove)
.count();
Expand All @@ -177,4 +174,11 @@
}
});
}

private boolean isAlwaysAllowedSpawnReason(@Nullable SpawnReason reason) {
return reason == SpawnReason.CUSTOM
|| reason == SpawnReason.COMMAND
|| reason == SpawnReason.BREEDING
|| reason == SpawnReason.SPAWNER_EGG;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.mvplugins.multiverse.core.utils.compatibility;

import org.bukkit.entity.Entity;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mvplugins.multiverse.core.utils.ReflectHelper;

Check warning on line 8 in src/main/java/org/mvplugins/multiverse/core/utils/compatibility/EntityCompatibility.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 'org.mvplugins.multiverse.core.utils.ReflectHelper' should be separated from previous imports. Raw Output: /github/workspace/src/main/java/org/mvplugins/multiverse/core/utils/compatibility/EntityCompatibility.java:8:1: warning: 'org.mvplugins.multiverse.core.utils.ReflectHelper' should be separated from previous imports. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)

/**
* Compatibility class used to handle API changes in {@link Entity} class.
*/
// TODO: Consider making this part of the public API in v5.9.

Check warning on line 13 in src/main/java/org/mvplugins/multiverse/core/utils/compatibility/EntityCompatibility.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Comment matches to-do format 'TODO'. Raw Output: /github/workspace/src/main/java/org/mvplugins/multiverse/core/utils/compatibility/EntityCompatibility.java:13:3: info: Comment matches to-do format 'TODO'. (com.puppycrawl.tools.checkstyle.checks.TodoCommentCheck)

Check warning on line 13 in src/main/java/org/mvplugins/multiverse/core/utils/compatibility/EntityCompatibility.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this TODO comment.

See more on https://sonarcloud.io/project/issues?id=Multiverse_Multiverse-Core&issues=AaAkzMpm3ZIos-QJNt-t&open=AaAkzMpm3ZIos-QJNt-t&pullRequest=3514
@ApiStatus.Internal
public final class EntityCompatibility {

private static final boolean HAS_GET_ENTITY_SPAWN_REASON_METHOD;

static {
HAS_GET_ENTITY_SPAWN_REASON_METHOD = ReflectHelper.hasMethod(Entity.class, "getEntitySpawnReason");
}

/**
* Gets the reason that initially spawned the entity when supported by the server.
*
* @param entity The entity to query.
* @return The entity's spawn reason, or null when the API is unavailable.
*/
@Nullable
public static SpawnReason getEntitySpawnReason(@NotNull Entity entity) {
if (HAS_GET_ENTITY_SPAWN_REASON_METHOD) {
return entity.getEntitySpawnReason();
}
return null;
}

private EntityCompatibility() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
}
Loading