NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/tiles/BlockEntityAnimalContainer.java

52 lines
2 KiB
Java
Raw Normal View History

2020-02-02 18:00:52 +01:00
package de.ellpeck.naturesaura.blocks.tiles;
2020-02-02 19:43:40 +01:00
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
2021-12-04 15:40:09 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.level.block.state.BlockState;
2021-12-04 19:17:21 +01:00
import net.minecraft.world.phys.AABB;
2020-02-02 18:00:52 +01:00
import java.util.HashSet;
import java.util.Set;
2021-12-04 15:40:09 +01:00
public class BlockEntityAnimalContainer extends BlockEntityImpl implements ITickableBlockEntity {
2020-02-02 18:00:52 +01:00
2021-12-04 15:40:09 +01:00
public BlockEntityAnimalContainer(BlockPos pos, BlockState state) {
2021-12-19 15:32:45 +01:00
super(ModBlockEntities.ANIMAL_CONTAINER, pos, state);
2020-02-02 18:00:52 +01:00
}
public int getRadius() {
return this.redstonePower / 2;
}
@Override
public void onRedstonePowerChange(int newPower) {
super.onRedstonePowerChange(newPower);
this.sendToClients();
}
@Override
public void tick() {
2021-12-04 15:40:09 +01:00
if (this.level.isClientSide)
2020-02-02 18:00:52 +01:00
return;
2021-12-04 19:17:21 +01:00
var radius = this.getRadius();
Set<Animal> animalsInRange = new HashSet<>(this.level.getEntitiesOfClass(Animal.class, new AABB(this.worldPosition).inflate(radius - 1)));
var animalsOutRange = this.level.getEntitiesOfClass(Animal.class, new AABB(this.worldPosition).inflate(radius + 1));
for (var animal : animalsOutRange) {
2020-02-02 18:00:52 +01:00
if (animalsInRange.contains(animal))
continue;
2021-12-04 19:17:21 +01:00
var pos = animal.position();
var distance = pos.subtract(this.worldPosition.getX(), pos.y, this.worldPosition.getZ());
2020-02-02 18:00:52 +01:00
distance = distance.normalize().scale(-0.15F);
2021-12-04 19:17:21 +01:00
animal.setDeltaMovement(distance);
2020-02-02 19:43:40 +01:00
2021-12-04 19:17:21 +01:00
if (this.level.random.nextBoolean()) {
var eye = animal.getEyePosition(1).add(animal.getLookAngle());
2021-12-04 15:40:09 +01:00
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32,
new PacketParticles((float) eye.x, (float) eye.y, (float) eye.z, PacketParticles.Type.ANIMAL_CONTAINER));
2020-02-02 19:43:40 +01:00
}
2020-02-02 18:00:52 +01:00
}
}
}