mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-30 16:28:00 +01:00
Butterflies will now spawn in the world and repopulate if killed
This commit is contained in:
parent
0716c5bf20
commit
762b211124
2 changed files with 63 additions and 16 deletions
|
@ -1,10 +1,15 @@
|
||||||
package com.minelittlepony.unicopia;
|
package com.minelittlepony.unicopia;
|
||||||
|
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
import com.minelittlepony.unicopia.entity.ButterflyEntity;
|
import com.minelittlepony.unicopia.entity.ButterflyEntity;
|
||||||
import com.minelittlepony.unicopia.entity.CastSpellEntity;
|
import com.minelittlepony.unicopia.entity.CastSpellEntity;
|
||||||
import com.minelittlepony.unicopia.entity.FloatingArtefactEntity;
|
import com.minelittlepony.unicopia.entity.FloatingArtefactEntity;
|
||||||
import com.minelittlepony.unicopia.projectile.MagicProjectileEntity;
|
import com.minelittlepony.unicopia.projectile.MagicProjectileEntity;
|
||||||
|
|
||||||
|
import net.fabricmc.fabric.api.biome.v1.BiomeModifications;
|
||||||
|
import net.fabricmc.fabric.api.biome.v1.BiomeSelectionContext;
|
||||||
|
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.FabricDefaultAttributeRegistry;
|
||||||
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityTypeBuilder;
|
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityTypeBuilder;
|
||||||
import net.minecraft.entity.Entity;
|
import net.minecraft.entity.Entity;
|
||||||
|
@ -13,7 +18,10 @@ import net.minecraft.entity.EntityType;
|
||||||
import net.minecraft.entity.SpawnGroup;
|
import net.minecraft.entity.SpawnGroup;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
import net.minecraft.util.registry.Registry;
|
import net.minecraft.util.registry.Registry;
|
||||||
|
import net.minecraft.world.biome.Biome;
|
||||||
|
import net.minecraft.world.biome.Biome.Category;
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
public interface UEntities {
|
public interface UEntities {
|
||||||
|
|
||||||
EntityType<ButterflyEntity> BUTTERFLY = register("butterfly", FabricEntityTypeBuilder.create(SpawnGroup.AMBIENT, ButterflyEntity::new)
|
EntityType<ButterflyEntity> BUTTERFLY = register("butterfly", FabricEntityTypeBuilder.create(SpawnGroup.AMBIENT, ButterflyEntity::new)
|
||||||
|
@ -36,5 +44,11 @@ public interface UEntities {
|
||||||
|
|
||||||
static void bootstrap() {
|
static void bootstrap() {
|
||||||
FabricDefaultAttributeRegistry.register(BUTTERFLY, ButterflyEntity.createButterflyAttributes());
|
FabricDefaultAttributeRegistry.register(BUTTERFLY, ButterflyEntity.createButterflyAttributes());
|
||||||
|
|
||||||
|
final Predicate<BiomeSelectionContext> butterflySpawnable = BiomeSelectors.foundInOverworld()
|
||||||
|
.and(ctx -> ctx.getBiome().getPrecipitation() == Biome.Precipitation.RAIN);
|
||||||
|
|
||||||
|
BiomeModifications.addSpawn(butterflySpawnable.and(BiomeSelectors.categories(Category.RIVER, Category.FOREST, Category.EXTREME_HILLS)), SpawnGroup.AMBIENT, BUTTERFLY, 3, 3, 12);
|
||||||
|
BiomeModifications.addSpawn(butterflySpawnable.and(BiomeSelectors.categories(Category.PLAINS, Category.JUNGLE)), SpawnGroup.AMBIENT, BUTTERFLY, 7, 5, 19);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,8 +31,11 @@ public class ButterflyEntity extends AmbientEntity {
|
||||||
private static final TrackedData<Boolean> RESTING = DataTracker.registerData(ButterflyEntity.class, TrackedDataHandlerRegistry.BOOLEAN);
|
private static final TrackedData<Boolean> RESTING = DataTracker.registerData(ButterflyEntity.class, TrackedDataHandlerRegistry.BOOLEAN);
|
||||||
private static final TrackedData<Integer> VARIANT = DataTracker.registerData(ButterflyEntity.class, TrackedDataHandlerRegistry.INTEGER);
|
private static final TrackedData<Integer> VARIANT = DataTracker.registerData(ButterflyEntity.class, TrackedDataHandlerRegistry.INTEGER);
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private BlockPos hoveringPosition;
|
private BlockPos hoveringPosition;
|
||||||
|
|
||||||
|
private int ticksResting;
|
||||||
|
|
||||||
public ButterflyEntity(EntityType<ButterflyEntity> type, World world) {
|
public ButterflyEntity(EntityType<ButterflyEntity> type, World world) {
|
||||||
super(type, world);
|
super(type, world);
|
||||||
setVariant(Variant.random(world.random));
|
setVariant(Variant.random(world.random));
|
||||||
|
@ -75,7 +78,13 @@ public class ButterflyEntity extends AmbientEntity {
|
||||||
@Override
|
@Override
|
||||||
public boolean collides() {
|
public boolean collides() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void pushAway(Entity entity) { }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void tickCramming() { }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void tick() {
|
public void tick() {
|
||||||
|
@ -97,8 +106,7 @@ public class ButterflyEntity extends AmbientEntity {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Variant getVariant() {
|
public Variant getVariant() {
|
||||||
Variant[] values = Variant.values();
|
return Variant.byId(getDataTracker().get(VARIANT));
|
||||||
return values[getDataTracker().get(VARIANT) % values.length];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVariant(Variant variant) {
|
public void setVariant(Variant variant) {
|
||||||
|
@ -117,44 +125,45 @@ public class ButterflyEntity extends AmbientEntity {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (player.isSprinting() || player.handSwinging || player.forwardSpeed > 0 || player.sidewaysSpeed > 0) {
|
if (player.isSprinting() || player.forwardSpeed > 0 || player.sidewaysSpeed > 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else if (!EntityPredicates.EXCEPT_CREATIVE_OR_SPECTATOR.test(e)) {
|
} else if (!EntityPredicates.EXCEPT_CREATIVE_OR_SPECTATOR.test(e)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return e.getVelocity().x != 0 || e.getVelocity().z != 0;
|
return e.getVelocity().horizontalLength() > 1.4F;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void tickMovement() {
|
public void tickMovement() {
|
||||||
super.tickMovement();
|
super.tickMovement();
|
||||||
|
|
||||||
BlockPos pos = getBlockPos();
|
|
||||||
BlockPos below = new BlockPos(getPos().add(0, -0.5, 0));
|
BlockPos below = new BlockPos(getPos().add(0, -0.5, 0));
|
||||||
|
|
||||||
if (isResting()) {
|
if (isResting()) {
|
||||||
if (world.getBlockState(below).isAir()) {
|
if (world.getBlockState(below).isAir()
|
||||||
|
|| !world.getOtherEntities(this, getBoundingBox().expand(7), this::isAggressor).isEmpty()
|
||||||
|
|| (ticksResting++ > 40 && world.random.nextInt(500) == 0)
|
||||||
|
|| world.hasRain(below)) {
|
||||||
setResting(false);
|
setResting(false);
|
||||||
} else {
|
|
||||||
if (!world.getOtherEntities(this, getBoundingBox().expand(7), this::isAggressor).isEmpty()) {
|
|
||||||
setResting(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
ticksResting = 0;
|
||||||
|
|
||||||
// invalidate the hovering position
|
// invalidate the hovering position
|
||||||
if (hoveringPosition != null && (!world.isAir(hoveringPosition) || hoveringPosition.getY() < 1)) {
|
if (hoveringPosition != null && (!world.isAir(hoveringPosition) || hoveringPosition.getY() < 1)) {
|
||||||
hoveringPosition = null;
|
hoveringPosition = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BlockPos pos = getBlockPos();
|
||||||
|
|
||||||
// select a new hovering position
|
// select a new hovering position
|
||||||
if (hoveringPosition == null || random.nextInt(30) == 0 || hoveringPosition.getSquaredDistance(pos) < 4) {
|
if (hoveringPosition == null || random.nextInt(30) == 0 || hoveringPosition.getSquaredDistance(pos) < 4) {
|
||||||
hoveringPosition = new BlockPos(
|
hoveringPosition = pos.add(
|
||||||
getX() + random.nextInt(7) - random.nextInt(7),
|
random.nextInt(7) - random.nextInt(7),
|
||||||
getY() + random.nextInt(6) - 2,
|
random.nextInt(6) - 2,
|
||||||
getZ() + random.nextInt(7) - random.nextInt(7)
|
random.nextInt(7) - random.nextInt(7)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,9 +185,27 @@ public class ButterflyEntity extends AmbientEntity {
|
||||||
if (random.nextInt(100) == 0 && world.getBlockState(below).isOpaque()) {
|
if (random.nextInt(100) == 0 && world.getBlockState(below).isOpaque()) {
|
||||||
setResting(true);
|
setResting(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!world.isClient && age % 20 == 0 && world.random.nextInt(200) == 0) {
|
||||||
|
for (Entity i : world.getOtherEntities(this, getBoundingBox().expand(20))) {
|
||||||
|
if (i.getType() == getType()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ButterflyEntity copy = (ButterflyEntity)getType().create(world);
|
||||||
|
copy.copyPositionAndRotation(this);
|
||||||
|
world.spawnEntity(copy);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean shouldRender(double distance) {
|
||||||
|
double d = 64 * getRenderDistanceMultiplier();
|
||||||
|
return distance < d * d;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean handleFallDamage(float distance, float damageMultiplier, DamageSource cause) {
|
public boolean handleFallDamage(float distance, float damageMultiplier, DamageSource cause) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -190,7 +217,7 @@ public class ButterflyEntity extends AmbientEntity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canSpawn(WorldAccess world, SpawnReason reason) {
|
public boolean canSpawn(WorldAccess world, SpawnReason reason) {
|
||||||
return reason != SpawnReason.NATURAL || (getY() < world.getSeaLevel() && world.getLightLevel(getBlockPos()) > 7);
|
return reason != SpawnReason.NATURAL || (getY() >= world.getSeaLevel() && world.getLightLevel(getBlockPos()) > 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -215,12 +242,18 @@ public class ButterflyEntity extends AmbientEntity {
|
||||||
WHITE_MONARCH,
|
WHITE_MONARCH,
|
||||||
BRIMSTONE;
|
BRIMSTONE;
|
||||||
|
|
||||||
|
private static final Variant[] VALUES = Variant.values();
|
||||||
|
|
||||||
private final Identifier skin = new Identifier("unicopia", "textures/entity/butterfly/" + name().toLowerCase() + ".png");
|
private final Identifier skin = new Identifier("unicopia", "textures/entity/butterfly/" + name().toLowerCase() + ".png");
|
||||||
|
|
||||||
public Identifier getSkin() {
|
public Identifier getSkin() {
|
||||||
return skin;
|
return skin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Variant byId(int index) {
|
||||||
|
return VALUES[Math.max(0, index) % VALUES.length];
|
||||||
|
}
|
||||||
|
|
||||||
static Variant random(Random rand) {
|
static Variant random(Random rand) {
|
||||||
Variant[] values = values();
|
Variant[] values = values();
|
||||||
return values[rand.nextInt(values.length)];
|
return values[rand.nextInt(values.length)];
|
||||||
|
|
Loading…
Reference in a new issue