mirror of
https://github.com/Ellpeck/NaturesAura.git
synced 2024-11-05 12:59:08 +01:00
46 lines
1.5 KiB
Java
46 lines
1.5 KiB
Java
|
package de.ellpeck.naturesaura.blocks.tiles;
|
||
|
|
||
|
import net.minecraft.entity.passive.AnimalEntity;
|
||
|
import net.minecraft.tileentity.ITickableTileEntity;
|
||
|
import net.minecraft.util.math.AxisAlignedBB;
|
||
|
import net.minecraft.util.math.Vec3d;
|
||
|
|
||
|
import java.util.HashSet;
|
||
|
import java.util.List;
|
||
|
import java.util.Set;
|
||
|
|
||
|
public class TileEntityAnimalContainer extends TileEntityImpl implements ITickableTileEntity {
|
||
|
|
||
|
public TileEntityAnimalContainer() {
|
||
|
super(ModTileEntities.ANIMAL_CONTAINER);
|
||
|
}
|
||
|
|
||
|
public int getRadius() {
|
||
|
return this.redstonePower / 2;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onRedstonePowerChange(int newPower) {
|
||
|
super.onRedstonePowerChange(newPower);
|
||
|
this.sendToClients();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void tick() {
|
||
|
if (this.world.isRemote || this.world.getGameTime() % 2 != 0)
|
||
|
return;
|
||
|
|
||
|
int radius = this.getRadius();
|
||
|
Set<AnimalEntity> animalsInRange = new HashSet<>(this.world.getEntitiesWithinAABB(AnimalEntity.class, new AxisAlignedBB(this.pos).grow(radius - 1)));
|
||
|
List<AnimalEntity> animalsOutRange = this.world.getEntitiesWithinAABB(AnimalEntity.class, new AxisAlignedBB(this.pos).grow(radius));
|
||
|
for (AnimalEntity animal : animalsOutRange) {
|
||
|
if (animalsInRange.contains(animal))
|
||
|
continue;
|
||
|
Vec3d pos = animal.getPositionVec();
|
||
|
Vec3d distance = pos.subtract(this.pos.getX(), pos.getY(), this.pos.getZ());
|
||
|
distance = distance.normalize().scale(-0.15F);
|
||
|
animal.setMotion(distance);
|
||
|
}
|
||
|
}
|
||
|
}
|