2018-12-20 00:12:59 +01:00
|
|
|
package de.ellpeck.naturesaura.chunk.effect;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.ModConfig;
|
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
|
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IDrainSpotEffect;
|
|
|
|
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
|
2021-12-15 16:24:53 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.core.particles.ParticleTypes;
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
import net.minecraft.util.Mth;
|
|
|
|
import net.minecraft.world.entity.EntityType;
|
|
|
|
import net.minecraft.world.entity.animal.Animal;
|
|
|
|
import net.minecraft.world.entity.animal.Chicken;
|
|
|
|
import net.minecraft.world.entity.item.ItemEntity;
|
|
|
|
import net.minecraft.world.entity.player.Player;
|
|
|
|
import net.minecraft.world.item.EggItem;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.item.Items;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.chunk.LevelChunk;
|
|
|
|
import net.minecraft.world.phys.AABB;
|
2021-06-28 14:31:37 +02:00
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
2018-12-20 00:12:59 +01:00
|
|
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
public class AnimalEffect implements IDrainSpotEffect {
|
|
|
|
|
|
|
|
public static final ResourceLocation NAME = new ResourceLocation(NaturesAura.MOD_ID, "animal");
|
|
|
|
|
2019-02-09 21:55:40 +01:00
|
|
|
private int chance;
|
2021-12-15 14:26:42 +01:00
|
|
|
private AABB bb;
|
2019-02-09 21:55:40 +01:00
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
private boolean calcValues(Level level, BlockPos pos, Integer spot) {
|
2018-12-20 00:12:59 +01:00
|
|
|
if (spot <= 0)
|
2019-02-09 21:55:40 +01:00
|
|
|
return false;
|
2021-12-15 16:30:22 +01:00
|
|
|
var auraAndSpots = IAuraChunk.getAuraAndSpotAmountInArea(level, pos, 30);
|
2021-06-28 14:31:37 +02:00
|
|
|
int aura = auraAndSpots.getLeft();
|
2019-01-29 11:46:38 +01:00
|
|
|
if (aura < 1500000)
|
2019-02-09 21:55:40 +01:00
|
|
|
return false;
|
2021-12-04 19:17:21 +01:00
|
|
|
this.chance = Math.min(50, Mth.ceil(Math.abs(aura) / 500000F / auraAndSpots.getRight()));
|
2019-02-09 21:55:40 +01:00
|
|
|
if (this.chance <= 0)
|
|
|
|
return false;
|
2021-12-15 16:30:22 +01:00
|
|
|
var dist = Mth.clamp(Math.abs(aura) / 150000, 5, 35);
|
2021-12-15 16:24:53 +01:00
|
|
|
this.bb = new AABB(pos).inflate(dist);
|
2019-02-09 21:55:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-15 16:24:53 +01:00
|
|
|
public ActiveType isActiveHere(Player player, LevelChunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!this.calcValues(player.level, pos, spot))
|
2020-01-23 16:05:52 +01:00
|
|
|
return ActiveType.INACTIVE;
|
2021-12-15 16:24:53 +01:00
|
|
|
if (!this.bb.contains(player.getEyePosition()))
|
2020-01-23 16:05:52 +01:00
|
|
|
return ActiveType.INACTIVE;
|
2021-12-15 16:24:53 +01:00
|
|
|
if (!NaturesAuraAPI.instance().isEffectPowderActive(player.level, player.blockPosition(), NAME))
|
2020-01-23 16:05:52 +01:00
|
|
|
return ActiveType.INHIBITED;
|
|
|
|
return ActiveType.ACTIVE;
|
2019-02-09 21:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getDisplayIcon() {
|
|
|
|
return new ItemStack(Items.EGG);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-15 16:24:53 +01:00
|
|
|
public void update(Level level, LevelChunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (level.getGameTime() % 200 != 0)
|
2021-01-30 17:21:41 +01:00
|
|
|
return;
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!this.calcValues(level, pos, spot))
|
2019-02-09 21:55:40 +01:00
|
|
|
return;
|
2018-12-20 00:12:59 +01:00
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var animals = level.getEntitiesOfClass(Animal.class, this.bb);
|
2021-03-21 17:53:36 +01:00
|
|
|
if (animals.size() >= ModConfig.instance.maxAnimalsAroundPowder.get())
|
2018-12-20 00:12:59 +01:00
|
|
|
return;
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var items = level.getEntitiesOfClass(ItemEntity.class, this.bb);
|
|
|
|
for (var item : items) {
|
2021-01-30 17:21:41 +01:00
|
|
|
if (!item.isAlive())
|
|
|
|
continue;
|
2021-12-15 16:24:53 +01:00
|
|
|
if (!NaturesAuraAPI.instance().isEffectPowderActive(level, item.blockPosition(), NAME))
|
2021-01-30 17:21:41 +01:00
|
|
|
continue;
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var stack = item.getItem();
|
2021-01-30 17:21:41 +01:00
|
|
|
if (!(stack.getItem() instanceof EggItem))
|
|
|
|
continue;
|
2021-12-15 16:24:53 +01:00
|
|
|
if (item.getAge() < item.lifespan / 2)
|
2021-01-30 17:21:41 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (stack.getCount() <= 1)
|
2021-12-15 16:24:53 +01:00
|
|
|
item.kill();
|
2021-01-30 17:21:41 +01:00
|
|
|
else {
|
|
|
|
stack.shrink(1);
|
|
|
|
item.setItem(stack);
|
2018-12-20 00:12:59 +01:00
|
|
|
}
|
2021-01-30 17:21:41 +01:00
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var chicken = new Chicken(EntityType.CHICKEN, level);
|
2021-12-15 16:24:53 +01:00
|
|
|
chicken.setAge(-24000);
|
|
|
|
chicken.setPos(item.getX(), item.getY(), item.getZ());
|
|
|
|
level.addFreshEntity(chicken);
|
2021-01-30 17:21:41 +01:00
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var closestSpot = IAuraChunk.getHighestSpot(level, item.blockPosition(), 35, pos);
|
2021-12-04 15:40:09 +01:00
|
|
|
IAuraChunk.getAuraChunk(level, closestSpot).drainAura(closestSpot, 2000);
|
2018-12-20 00:12:59 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 16:24:53 +01:00
|
|
|
if (level.random.nextInt(20) <= this.chance) {
|
2018-12-20 00:12:59 +01:00
|
|
|
if (animals.size() < 2)
|
|
|
|
return;
|
2021-12-15 16:30:22 +01:00
|
|
|
var first = animals.get(level.random.nextInt(animals.size()));
|
2021-12-15 16:24:53 +01:00
|
|
|
if (first.isBaby() || first.isInLove())
|
2018-12-20 00:12:59 +01:00
|
|
|
return;
|
2021-12-15 16:24:53 +01:00
|
|
|
if (!NaturesAuraAPI.instance().isEffectPowderActive(level, first.blockPosition(), NAME))
|
2018-12-20 00:12:59 +01:00
|
|
|
return;
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var secondOptional = animals.stream()
|
2021-12-15 16:24:53 +01:00
|
|
|
.filter(e -> e != first && !e.isInLove() && !e.isBaby())
|
|
|
|
.min(Comparator.comparingDouble(e -> e.distanceToSqr(first)));
|
|
|
|
if (secondOptional.isEmpty())
|
2018-12-20 00:12:59 +01:00
|
|
|
return;
|
2021-12-15 16:30:22 +01:00
|
|
|
var second = secondOptional.get();
|
2021-12-15 16:24:53 +01:00
|
|
|
if (second.distanceToSqr(first) > 5 * 5)
|
2019-01-10 23:57:48 +01:00
|
|
|
return;
|
2018-12-20 00:12:59 +01:00
|
|
|
|
|
|
|
this.setInLove(first);
|
|
|
|
this.setInLove(second);
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var closestSpot = IAuraChunk.getHighestSpot(level, first.blockPosition(), 35, pos);
|
2021-12-04 15:40:09 +01:00
|
|
|
IAuraChunk.getAuraChunk(level, closestSpot).drainAura(closestSpot, 3500);
|
2018-12-20 00:12:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 16:24:53 +01:00
|
|
|
private void setInLove(Animal animal) {
|
2018-12-20 00:12:59 +01:00
|
|
|
animal.setInLove(null);
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var j = 0; j < 7; j++)
|
2021-12-04 15:40:09 +01:00
|
|
|
animal.level.addParticle(ParticleTypes.HEART,
|
2021-12-15 16:24:53 +01:00
|
|
|
animal.getX() + (double) (animal.level.random.nextFloat() * animal.getBbWidth() * 2.0F) - animal.getBbWidth(),
|
|
|
|
animal.getY() + 0.5D + (double) (animal.level.random.nextFloat() * animal.getBbHeight()),
|
|
|
|
animal.getZ() + (double) (animal.level.random.nextFloat() * animal.getBbWidth() * 2.0F) - animal.getBbWidth(),
|
|
|
|
animal.level.random.nextGaussian() * 0.02D,
|
|
|
|
animal.level.random.nextGaussian() * 0.02D,
|
|
|
|
animal.level.random.nextGaussian() * 0.02D);
|
2018-12-20 00:12:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-15 16:24:53 +01:00
|
|
|
public boolean appliesHere(LevelChunk chunk, IAuraChunk auraChunk, IAuraType type) {
|
2020-01-24 17:05:41 +01:00
|
|
|
return ModConfig.instance.animalEffect.get();
|
2018-12-20 00:12:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getName() {
|
|
|
|
return NAME;
|
|
|
|
}
|
|
|
|
}
|