fix crash when using MaxEntityCollisions rule in Singleplayer world

This commit is contained in:
Arian Baishya authored and gnembon committed 2025-02-05 17:29:10 +01:00
1 parent 2a2c41ddce
commit 85803cecbb
1 file changed
+11 -4
@@ -2,6 +2,7 @@ package carpet.mixins;
import carpet.CarpetSettings;
import carpet.fakes.LevelInterface;
import net.minecraft.server.level.ServerLevel;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
@@ -33,12 +34,18 @@ public abstract class LivingEntity_maxCollisionsMixin extends Entity
{
return;
}
if (!(this.level() instanceof ServerLevel serverLevel))
{
return;
}
List<Entity> entities;
int maxEntityCramming =-1;
if (CarpetSettings.maxEntityCollisions > 0)
{
maxEntityCramming = this.getServer().getGameRules().getInt(GameRules.RULE_MAX_ENTITY_CRAMMING);
entities = ((LevelInterface) this.level()).getOtherEntitiesLimited(
maxEntityCramming = serverLevel.getGameRules().getInt(GameRules.RULE_MAX_ENTITY_CRAMMING);
entities = ((LevelInterface) serverLevel).getOtherEntitiesLimited(
this,
this.getBoundingBox(),
EntitySelector.pushableBy(this),
@@ -46,11 +53,11 @@ public abstract class LivingEntity_maxCollisionsMixin extends Entity
}
else
{
entities = this.level().getEntities(this, this.getBoundingBox(), EntitySelector.pushableBy(this));
entities = serverLevel.getEntities(this, this.getBoundingBox(), EntitySelector.pushableBy(this));
}
if (!entities.isEmpty()) {
if (maxEntityCramming < 0) maxEntityCramming = this.getServer().getGameRules().getInt(GameRules.RULE_MAX_ENTITY_CRAMMING);
if (maxEntityCramming < 0) maxEntityCramming = serverLevel.getGameRules().getInt(GameRules.RULE_MAX_ENTITY_CRAMMING);
if (maxEntityCramming > 0 && entities.size() > maxEntityCramming - 1 && this.random.nextInt(4) == 0) {
int candidates = 0;