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;
|
2020-01-21 21:04:44 +01:00
|
|
|
import net.minecraft.entity.EntityType;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.entity.item.ItemEntity;
|
|
|
|
import net.minecraft.entity.passive.AnimalEntity;
|
|
|
|
import net.minecraft.entity.passive.ChickenEntity;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.item.EggItem;
|
2018-12-20 00:12:59 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2020-01-21 21:04:44 +01:00
|
|
|
import net.minecraft.item.Items;
|
|
|
|
import net.minecraft.particles.ParticleTypes;
|
2018-12-20 00:12:59 +01:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.math.MathHelper;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraft.world.chunk.Chunk;
|
|
|
|
|
|
|
|
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;
|
|
|
|
private AxisAlignedBB bb;
|
|
|
|
|
|
|
|
private boolean calcValues(World world, 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;
|
2018-12-20 00:12:59 +01:00
|
|
|
int aura = IAuraChunk.getAuraInArea(world, pos, 30);
|
2019-01-29 11:46:38 +01:00
|
|
|
if (aura < 1500000)
|
2019-02-09 21:55:40 +01:00
|
|
|
return false;
|
2019-02-17 12:49:07 +01:00
|
|
|
this.chance = Math.min(50, MathHelper.ceil(Math.abs(aura) / 500000F / IAuraChunk.getSpotAmountInArea(world, pos, 30)));
|
2019-02-09 21:55:40 +01:00
|
|
|
if (this.chance <= 0)
|
|
|
|
return false;
|
2019-01-29 11:46:38 +01:00
|
|
|
int dist = MathHelper.clamp(Math.abs(aura) / 150000, 5, 35);
|
2019-02-09 21:55:40 +01:00
|
|
|
this.bb = new AxisAlignedBB(pos).grow(dist);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
public int isActiveHere(PlayerEntity player, Chunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot) {
|
2019-02-09 21:55:40 +01:00
|
|
|
if (!this.calcValues(player.world, pos, spot))
|
|
|
|
return -1;
|
|
|
|
if (!this.bb.contains(player.getPositionVector()))
|
|
|
|
return -1;
|
2019-02-10 14:13:42 +01:00
|
|
|
if (!NaturesAuraAPI.instance().isEffectPowderActive(player.world, player.getPosition(), NAME))
|
2019-02-09 21:55:40 +01:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getDisplayIcon() {
|
|
|
|
return new ItemStack(Items.EGG);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update(World world, Chunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot) {
|
|
|
|
if (!this.calcValues(world, pos, spot))
|
|
|
|
return;
|
2018-12-20 00:12:59 +01:00
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
List<AnimalEntity> animals = world.getEntitiesWithinAABB(AnimalEntity.class, this.bb);
|
2019-01-27 01:40:08 +01:00
|
|
|
if (animals.size() >= 200)
|
2018-12-20 00:12:59 +01:00
|
|
|
return;
|
|
|
|
|
2020-01-21 21:04:44 +01:00
|
|
|
if (world.getGameTime() % 200 == 0) {
|
2019-10-20 22:30:49 +02:00
|
|
|
List<ItemEntity> items = world.getEntitiesWithinAABB(ItemEntity.class, this.bb);
|
|
|
|
for (ItemEntity item : items) {
|
2020-01-21 21:04:44 +01:00
|
|
|
if (!item.isAlive())
|
2018-12-20 00:12:59 +01:00
|
|
|
continue;
|
2019-02-10 14:13:42 +01:00
|
|
|
if (!NaturesAuraAPI.instance().isEffectPowderActive(world, item.getPosition(), NAME))
|
2018-12-20 00:12:59 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
ItemStack stack = item.getItem();
|
2019-10-20 22:30:49 +02:00
|
|
|
if (!(stack.getItem() instanceof EggItem))
|
2019-01-10 17:02:56 +01:00
|
|
|
continue;
|
|
|
|
// The getAge() method is private for absolutely no reason but I want it so I don't care
|
2020-01-21 21:04:44 +01:00
|
|
|
int age = item.getAge();
|
2019-01-10 17:02:56 +01:00
|
|
|
if (age < item.lifespan / 2)
|
2018-12-20 00:12:59 +01:00
|
|
|
continue;
|
|
|
|
|
2019-01-10 17:02:56 +01:00
|
|
|
if (stack.getCount() <= 1)
|
2020-01-21 21:04:44 +01:00
|
|
|
item.remove();
|
2019-01-10 17:02:56 +01:00
|
|
|
else {
|
|
|
|
stack.shrink(1);
|
|
|
|
item.setItem(stack);
|
|
|
|
}
|
2018-12-20 00:12:59 +01:00
|
|
|
|
2020-01-21 21:04:44 +01:00
|
|
|
ChickenEntity chicken = new ChickenEntity(EntityType.CHICKEN, world);
|
2018-12-20 00:12:59 +01:00
|
|
|
chicken.setGrowingAge(-24000);
|
|
|
|
chicken.setPosition(item.posX, item.posY, item.posZ);
|
2020-01-21 21:04:44 +01:00
|
|
|
world.addEntity(chicken);
|
2018-12-20 00:12:59 +01:00
|
|
|
|
|
|
|
BlockPos closestSpot = IAuraChunk.getHighestSpot(world, item.getPosition(), 35, pos);
|
2019-01-29 11:46:38 +01:00
|
|
|
IAuraChunk.getAuraChunk(world, closestSpot).drainAura(closestSpot, 2000);
|
2018-12-20 00:12:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 21:55:40 +01:00
|
|
|
if (world.rand.nextInt(200) <= this.chance) {
|
2018-12-20 00:12:59 +01:00
|
|
|
if (animals.size() < 2)
|
|
|
|
return;
|
2019-10-20 22:30:49 +02:00
|
|
|
AnimalEntity first = animals.get(world.rand.nextInt(animals.size()));
|
2018-12-20 00:12:59 +01:00
|
|
|
if (first.isChild() || first.isInLove())
|
|
|
|
return;
|
2019-02-10 14:13:42 +01:00
|
|
|
if (!NaturesAuraAPI.instance().isEffectPowderActive(world, first.getPosition(), NAME))
|
2018-12-20 00:12:59 +01:00
|
|
|
return;
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
Optional<AnimalEntity> secondOptional = animals.stream()
|
2018-12-20 00:12:59 +01:00
|
|
|
.filter(e -> e != first && !e.isInLove() && !e.isChild())
|
|
|
|
.min(Comparator.comparingDouble(e -> e.getDistanceSq(first)));
|
|
|
|
if (!secondOptional.isPresent())
|
|
|
|
return;
|
2019-10-20 22:30:49 +02:00
|
|
|
AnimalEntity second = secondOptional.get();
|
2019-01-10 23:57:48 +01:00
|
|
|
if (second.getDistanceSq(first) > 5 * 5)
|
|
|
|
return;
|
2018-12-20 00:12:59 +01:00
|
|
|
|
|
|
|
this.setInLove(first);
|
|
|
|
this.setInLove(second);
|
|
|
|
|
|
|
|
BlockPos closestSpot = IAuraChunk.getHighestSpot(world, first.getPosition(), 35, pos);
|
2019-01-29 11:46:38 +01:00
|
|
|
IAuraChunk.getAuraChunk(world, closestSpot).drainAura(closestSpot, 3500);
|
2018-12-20 00:12:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
private void setInLove(AnimalEntity animal) {
|
2018-12-20 00:12:59 +01:00
|
|
|
animal.setInLove(null);
|
|
|
|
for (int j = 0; j < 7; j++)
|
2020-01-21 21:04:44 +01:00
|
|
|
animal.world.addParticle(ParticleTypes.HEART,
|
|
|
|
(animal.posX + (double) (animal.world.rand.nextFloat() * animal.getWidth() * 2.0F)) - animal.getWidth(),
|
|
|
|
animal.posY + 0.5D + (double) (animal.world.rand.nextFloat() * animal.getHeight()),
|
|
|
|
(animal.posZ + (double) (animal.world.rand.nextFloat() * animal.getWidth() * 2.0F)) - animal.getWidth(),
|
2018-12-20 00:12:59 +01:00
|
|
|
animal.world.rand.nextGaussian() * 0.02D,
|
|
|
|
animal.world.rand.nextGaussian() * 0.02D,
|
|
|
|
animal.world.rand.nextGaussian() * 0.02D);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean appliesHere(Chunk chunk, IAuraChunk auraChunk, IAuraType type) {
|
|
|
|
return ModConfig.enabledFeatures.animalEffect;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getName() {
|
|
|
|
return NAME;
|
|
|
|
}
|
|
|
|
}
|