Skip to content

Commit

Permalink
Better flee and biome spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
sZeta99 committed Aug 17, 2024
1 parent f4c469a commit d1b3880
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,48 @@
import it.crystalnest.goblin_fabrications.Constants;
import it.crystalnest.goblin_fabrications.ModLoader;
import it.crystalnest.goblin_fabrications.entity.custom.GoblinEntity;
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.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityTypeBuilder;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.SpawnGroupData;
import net.minecraft.world.entity.*;
import net.minecraft.world.level.biome.Biomes;
import net.minecraft.world.level.levelgen.Heightmap;

public final class EntityRegistry {

private static final Register<EntityType<?>> ENTITY_REGISTER = ModLoader.REGISTER_PROVIDER.of(BuiltInRegistries.ENTITY_TYPE);
public static final EntityType<GoblinEntity> GOBLIN = FabricEntityTypeBuilder.create(MobCategory.MONSTER, GoblinEntity::new).dimensions(EntityDimensions.fixed(0.5f,0.5f)).build();

public static final EntityType<GoblinEntity> GOBLIN = FabricEntityTypeBuilder
.create(MobCategory.MONSTER, GoblinEntity::new)
.dimensions(EntityDimensions.fixed(0.5f, 0.5f))
.build();

private EntityRegistry() {}

public static void register(){
public static void register() {
ENTITY_REGISTER.apply("goblin", GOBLIN);

FabricDefaultAttributeRegistry.register(GOBLIN,GoblinEntity.setAttributers());
// Registering the entity attributes
FabricDefaultAttributeRegistry.register(GOBLIN, GoblinEntity.setAttributers());

// Register spawn rules
registerEntitySpawns();
}

private static void registerEntitySpawns() {
BiomeModifications.addSpawn(
BiomeSelectors.includeByKey(Biomes.PLAINS),
MobCategory.CREATURE,
GOBLIN,
100000, // Spawn weight (rarer with lower value)
1, // Minimum spawn group size
1 // Maximum spawn group size
);

// Optional: Set spawn placement rules (e.g., ground spawns)
//SpawnPlacements.register(GOBLIN, SpawnPlacements.Type.ON_GROUND, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, GoblinEntity::checkGoblinSpawnRules);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class GoblinFleeGoal<T extends LivingEntity> extends Goal {
protected final Predicate<LivingEntity> avoidPredicate;
protected final Predicate<LivingEntity> predicateOnAvoidEntity;
private final TargetingConditions avoidEntityTargeting;
private int timeWithoutLineOfSight = 0; // Initialize the counter

public GoblinFleeGoal(PathfinderMob pathfinderMob, Class<T> class_, float f, double d, double e) {

Expand Down Expand Up @@ -61,9 +62,9 @@ public GoblinFleeGoal(PathfinderMob pathfinderMob, Class<T> class_, float f, dou

@Override
public boolean canUse() {
this.toAvoid = this.mob.level().getNearestEntity(this.mob.level().getEntitiesOfClass(this.avoidClass, this.mob.getBoundingBox().inflate(maxDist, 15.0, maxDist), avoidPredicate), this.avoidEntityTargeting, this.mob, this.mob.getX(), this.mob.getY(), this.mob.getZ());
if (this.toAvoid == null) {
return false;
if (this.toAvoid == null) {
this.toAvoid = this.mob.level().getNearestEntity(this.mob.level().getEntitiesOfClass(this.avoidClass, this.mob.getBoundingBox().inflate(maxDist, 15.0, maxDist), avoidPredicate), this.avoidEntityTargeting, this.mob, this.mob.getX(), this.mob.getY(), this.mob.getZ());
return false;
}

Vec3 avoidDirection = this.mob.position().subtract(this.toAvoid.position()).normalize().scale(16.0);
Expand All @@ -84,8 +85,9 @@ public void start() {
}

public void stop() {
this.toAvoid = null;
((GoblinEntity) this.mob).isFleeing(false);
//this.toAvoid = null;
((GoblinEntity) this.mob).isFleeing(false);
// this.mob.discard();
}

@Override
Expand All @@ -103,7 +105,18 @@ public void tick() {
Vec3 avoidPosition = this.mob.position().add(avoidDirection);

this.path = this.pathNav.createPath(avoidPosition.x, avoidPosition.y, avoidPosition.z, 0);
// Check if the Goblin still has line of sight to the player
if (this.mob.hasLineOfSight(this.toAvoid)) {
this.timeWithoutLineOfSight = 0; // Reset the counter if the player is in sight
} else {
this.timeWithoutLineOfSight++; // Increment the counter if the player is out of sight
}

// Check if the mob should despawn after 15 seconds out of sight (300 ticks)
if (this.timeWithoutLineOfSight > 300) {
this.mob.discard(); // Despawn the goblin
System.out.println("Goblin despawn");
}
}

}

0 comments on commit d1b3880

Please sign in to comment.