Skip to content

Commit

Permalink
修正基岩地面导致的生成卡顿问题
Browse files Browse the repository at this point in the history
  • Loading branch information
TartaricAcid committed Sep 20, 2024
1 parent c953dff commit 5bdc6ae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import com.github.tartaricacid.touhoulittlemaid.entity.ai.goal.FairyAttackGoal;
import com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid;
import com.github.tartaricacid.touhoulittlemaid.entity.projectile.DanmakuShoot;
import com.github.tartaricacid.touhoulittlemaid.init.InitPoi;
import com.github.tartaricacid.touhoulittlemaid.init.InitSounds;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.util.RandomSource;
import net.minecraft.world.Difficulty;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.damagesource.DamageSource;
Expand All @@ -22,6 +26,7 @@
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
import net.minecraft.world.entity.ai.navigation.PathNavigation;
import net.minecraft.world.entity.ai.village.poi.PoiManager;
import net.minecraft.world.entity.animal.FlyingAnimal;
import net.minecraft.world.entity.animal.IronGolem;
import net.minecraft.world.entity.monster.Monster;
Expand Down Expand Up @@ -58,6 +63,15 @@ public static AttributeSupplier.Builder createFairyAttributes() {
.add(Attributes.FLYING_SPEED, 0.4);
}

public static boolean checkFairySpawnRules(EntityType<EntityFairy> entityType, ServerLevelAccessor levelAccessor, MobSpawnType spawnType, BlockPos pos, RandomSource randomSource) {
if (Monster.checkMonsterSpawnRules(entityType, levelAccessor, spawnType, pos, randomSource) && levelAccessor instanceof ServerLevel level) {
int scarecrowRange = MiscConfig.SCARECROW_RANGE.get();
long findCount = level.getPoiManager().getInSquare(type -> type.value().equals(InitPoi.SCARECROW.get()), pos, scarecrowRange, PoiManager.Occupancy.ANY).count();
return findCount <= 0;
}
return false;
}

@Override
protected void registerGoals() {
goalSelector.addGoal(0, new FloatGoal(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import net.minecraft.world.entity.ai.behavior.PositionTracker;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.ai.sensing.SensorType;
import net.minecraft.world.entity.monster.Monster;
import net.minecraft.world.entity.schedule.Activity;
import net.minecraft.world.entity.schedule.Schedule;
import net.minecraft.world.entity.schedule.ScheduleBuilder;
Expand Down Expand Up @@ -100,6 +99,6 @@ public static void addEntityAttributeEvent(EntityAttributeCreationEvent event) {

@SubscribeEvent
public static void addEntitySpawnPlacement(RegisterSpawnPlacementsEvent event) {
event.register(InitEntities.FAIRY.get(), SpawnPlacementTypes.ON_GROUND, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, Monster::checkMonsterSpawnRules, RegisterSpawnPlacementsEvent.Operation.REPLACE);
event.register(InitEntities.FAIRY.get(), SpawnPlacementTypes.ON_GROUND, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, EntityFairy::checkFairySpawnRules, RegisterSpawnPlacementsEvent.Operation.REPLACE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import com.github.tartaricacid.touhoulittlemaid.config.subconfig.MiscConfig;
import com.github.tartaricacid.touhoulittlemaid.init.InitEntities;
import com.github.tartaricacid.touhoulittlemaid.init.InitPoi;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.ai.village.poi.PoiManager;
import net.minecraft.world.level.biome.MobSpawnSettings;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
Expand Down Expand Up @@ -36,18 +34,7 @@ public static void addMobSpawnInfo(LevelEvent.PotentialSpawns event) {
if (SPAWNER_DATA == null) {
SPAWNER_DATA = new MobSpawnSettings.SpawnerData(InitEntities.FAIRY.get(), spawnProbability, 2, 4);
}
if (!canZombieSpawn) {
return;
}
// 检查稻草人
int scarecrowRange = MiscConfig.SCARECROW_RANGE.get();
// 优先判断等于 0 的情况,减少性能消耗
if (scarecrowRange <= 0) {
event.addSpawnerData(SPAWNER_DATA);
return;
}
long findCount = level.getPoiManager().getInSquare(type -> type.value().equals(InitPoi.SCARECROW.get()), event.getPos(), scarecrowRange, PoiManager.Occupancy.ANY).count();
if (findCount <= 0) {
if (canZombieSpawn) {
event.addSpawnerData(SPAWNER_DATA);
}
}
Expand Down

0 comments on commit 5bdc6ae

Please sign in to comment.