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

57 lines
2.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.entity.passive.Animal;
import net.minecraft.tileentity.ITickableBlockEntity;
2020-02-02 18:00:52 +01:00
import net.minecraft.util.math.AxisAlignedBB;
2020-09-22 03:17:02 +02:00
import net.minecraft.util.math.vector.Vector3d;
2021-12-04 15:40:09 +01:00
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
2020-02-02 18:00:52 +01:00
import java.util.HashSet;
import java.util.List;
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) {
super(ModTileEntities.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;
int radius = this.getRadius();
2021-12-04 15:40:09 +01:00
Set<Animal> animalsInRange = new HashSet<>(this.level.getEntitiesWithinAABB(Animal.class, new AxisAlignedBB(this.worldPosition).grow(radius - 1)));
List<Animal> animalsOutRange = this.level.getEntitiesWithinAABB(Animal.class, new AxisAlignedBB(this.worldPosition).grow(radius + 1));
for (Animal animal : animalsOutRange) {
2020-02-02 18:00:52 +01:00
if (animalsInRange.contains(animal))
continue;
2021-12-04 15:40:09 +01:00
Vec3 pos = animal.position();
Vec3 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);
animal.setMotion(distance);
2020-02-02 19:43:40 +01:00
2021-12-04 15:40:09 +01:00
if (this.level.rand.nextBoolean()) {
Vec3 eye = animal.getEyePosition(1).add(animal.getLookAngle());
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
}
}
}