Skip to content

Commit

Permalink
🚧 Start working on firefly spawning
Browse files Browse the repository at this point in the history
Works but not nearly enough of them spawn. They also seem to just decide to go above any mangrove swamp i put them in.
  • Loading branch information
CallMeEchoCodes committed Mar 4, 2024
1 parent e10d420 commit dd787e9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public void render(FireflyEntity entity, float yaw, float tickDelta, MatrixStack

@Override
protected int getBlockLight(FireflyEntity entity, BlockPos pos) {
// fake emmissive light
return 15;
}

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/dev/callmeecho/hollow/main/Hollow.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
import dev.callmeecho.hollow.main.registry.HollowEntityTypeRegistry;
import dev.callmeecho.hollow.main.registry.HollowItemRegistry;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.biome.v1.BiomeModifications;
import net.fabricmc.fabric.api.biome.v1.BiomeSelectors;
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;
import net.minecraft.entity.SpawnGroup;
import net.minecraft.entity.SpawnRestriction;
import net.minecraft.util.Identifier;
import net.minecraft.world.Heightmap;
import net.minecraft.world.biome.BiomeKeys;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -23,8 +29,14 @@ public void onInitialize() {
RegistrarHandler.process(HollowBlockRegistry.class, MODID);
RegistrarHandler.process(HollowItemRegistry.class, MODID);
RegistrarHandler.process(HollowEntityTypeRegistry.class, MODID);

FabricDefaultAttributeRegistry.register(HollowEntityTypeRegistry.FIREFLY, FireflyEntity.createFireflyAttributes());

BiomeModifications.addSpawn(BiomeSelectors.includeByKey(
BiomeKeys.SWAMP,
BiomeKeys.MANGROVE_SWAMP
), SpawnGroup.AMBIENT, HollowEntityTypeRegistry.FIREFLY, 20, 10, 30);
SpawnRestriction.register(HollowEntityTypeRegistry.FIREFLY, SpawnRestriction.Location.NO_RESTRICTIONS, Heightmap.Type.MOTION_BLOCKING_NO_LEAVES, FireflyEntity::canSpawn);

GROUP.initialize();
}
}
18 changes: 14 additions & 4 deletions src/main/java/dev/callmeecho/hollow/main/entity/FireflyEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.Flutterer;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.ai.AboveGroundTargeting;
import net.minecraft.entity.ai.NoPenaltySolidTargeting;
import net.minecraft.entity.ai.control.FlightMoveControl;
Expand All @@ -23,6 +24,9 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.LightType;
import net.minecraft.world.ServerWorldAccess;
import net.minecraft.world.World;

import java.util.EnumSet;
Expand Down Expand Up @@ -90,7 +94,7 @@ public static DefaultAttributeContainer.Builder createFireflyAttributes() {
public void tick() {
super.tick();

if (this.age % 2 == 0 && this.random.nextInt(5) == 0) {
if (this.age % 5 == 0 && this.random.nextInt(5) == 0) {
this.setLightTicks(this.getLightTicks() - this.random.nextBetween(10, 15));
} else {
this.setLightTicks(this.getLightTicks() + 1);
Expand All @@ -100,7 +104,7 @@ public void tick() {
this.setLightTicks(15);
}

if (this.getLightTicks() < 0) {
if (this.getLightTicks() < 0 || this.isWet()) {
this.setLightTicks(0);
}
}
Expand Down Expand Up @@ -128,6 +132,12 @@ protected void pushAway(Entity entity) {

}

public static boolean canSpawn(EntityType<FireflyEntity> entityType, ServerWorldAccess worldAccess, SpawnReason spawnReason, BlockPos pos, Random random) {
if (pos.getY() < 63) return false;
if (worldAccess.getLightLevel(LightType.BLOCK, pos) > 9) return false;
return FireflyEntity.canMobSpawn(entityType, worldAccess, spawnReason, pos, random);
}

static class FlyRandomlyGoal extends Goal {
private final FireflyEntity firefly;

Expand All @@ -138,10 +148,10 @@ public FlyRandomlyGoal(FireflyEntity firefly) {

protected Vec3d getWanderTarget() {
Vec3d rotation = firefly.getRotationVec(0.0F);
Vec3d target = AboveGroundTargeting.find(firefly, 8, 7, rotation.x, rotation.z, MathHelper.HALF_PI, 3, 1);
Vec3d target = AboveGroundTargeting.find(firefly, 8, 2, rotation.x, rotation.z, MathHelper.HALF_PI, 3, 1);
if (target != null) return target;

return NoPenaltySolidTargeting.find(firefly, 8, 4, 1, rotation.x, rotation.z, MathHelper.HALF_PI);
return NoPenaltySolidTargeting.find(firefly, 8, 2, -2, rotation.x, rotation.z, MathHelper.HALF_PI);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"hollow:firefly"
]
}

0 comments on commit dd787e9

Please sign in to comment.