ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFeeder.java

113 lines
4.5 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityFeeder.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2018-08-10 05:04:07 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover;
import net.minecraft.entity.passive.EntityAnimal;
import net.minecraft.entity.passive.EntityHorse;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.EnumParticleTypes;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.AxisAlignedBB;
2018-01-29 09:17:38 +01:00
import net.minecraft.util.math.MathHelper;
2021-02-26 22:15:48 +01:00
import java.util.List;
import java.util.Optional;
2018-08-10 05:04:07 +02:00
public class TileEntityFeeder extends TileEntityInventoryBase {
2015-12-01 19:48:09 +01:00
public static final int THRESHOLD = 30;
private static final int TIME = 100;
public int currentTimer;
public int currentAnimalAmount;
private int lastAnimalAmount;
private int lastTimer;
2018-08-10 05:04:07 +02:00
public TileEntityFeeder() {
super(ActuallyTiles.FEEDER_TILE.get(), 1);
}
2018-08-10 05:04:07 +02:00
public int getCurrentTimerToScale(int i) {
return this.currentTimer * i / TIME;
2015-12-01 19:48:09 +01:00
}
@Override
2021-02-26 22:15:48 +01:00
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
super.writeSyncableNBT(compound, type);
compound.setInteger("Timer", this.currentTimer);
2018-08-10 05:04:07 +02:00
if (type == NBTType.SYNC) {
compound.setInteger("Animals", this.currentAnimalAmount);
}
}
@Override
2021-02-26 22:15:48 +01:00
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
super.readSyncableNBT(compound, type);
this.currentTimer = compound.getInteger("Timer");
2018-08-10 05:04:07 +02:00
if (type == NBTType.SYNC) {
this.currentAnimalAmount = compound.getInteger("Animals");
}
}
@Override
2018-08-10 05:04:07 +02:00
public void updateEntity() {
super.updateEntity();
2019-02-27 19:53:05 +01:00
this.currentTimer = MathHelper.clamp(++this.currentTimer, 0, 100);
2021-02-26 22:15:48 +01:00
if (this.world.isRemote) {
return;
}
2018-01-29 09:17:38 +01:00
int range = 5;
ItemStack stack = this.inv.getStackInSlot(0);
2018-08-10 05:04:07 +02:00
if (!stack.isEmpty() && this.currentTimer >= TIME) {
List<EntityAnimal> animals = this.world.getEntitiesWithinAABB(EntityAnimal.class, new AxisAlignedBB(this.pos.getX() - range, this.pos.getY() - range, this.pos.getZ() - range, this.pos.getX() + range, this.pos.getY() + range, this.pos.getZ() + range));
this.currentAnimalAmount = animals.size();
2019-02-27 19:53:05 +01:00
if (this.currentAnimalAmount >= 2 && this.currentAnimalAmount < THRESHOLD) {
2018-08-10 05:04:07 +02:00
Optional<EntityAnimal> opt = animals.stream().filter((e) -> canBeFed(stack, e)).findAny();
if (opt.isPresent()) {
feedAnimal(opt.get());
stack.shrink(1);
this.currentTimer = 0;
2019-02-27 19:53:05 +01:00
this.markDirty();
2018-08-10 05:04:07 +02:00
}
}
2018-01-29 09:17:38 +01:00
}
2018-08-10 05:04:07 +02:00
if ((this.lastAnimalAmount != this.currentAnimalAmount || this.lastTimer != this.currentTimer) && this.sendUpdateWithInterval()) {
this.lastAnimalAmount = this.currentAnimalAmount;
this.lastTimer = this.currentTimer;
}
}
2016-02-01 17:49:55 +01:00
@Override
2018-08-10 05:04:07 +02:00
public IRemover getRemover() {
return (slot, automation) -> !automation;
2018-01-29 09:17:38 +01:00
}
2018-08-10 05:04:07 +02:00
private static void feedAnimal(EntityAnimal animal) {
animal.setInLove(null);
2018-08-10 05:04:07 +02:00
for (int i = 0; i < 7; i++) {
double d = animal.world.rand.nextGaussian() * 0.02D;
double d1 = animal.world.rand.nextGaussian() * 0.02D;
double d2 = animal.world.rand.nextGaussian() * 0.02D;
2019-02-27 19:53:05 +01:00
animal.world.spawnParticle(EnumParticleTypes.HEART, animal.posX + animal.world.rand.nextFloat() * animal.width * 2.0F - animal.width, animal.posY + 0.5D + animal.world.rand.nextFloat() * animal.height, animal.posZ + animal.world.rand.nextFloat() * animal.width * 2.0F - animal.width, d, d1, d2);
}
}
2018-08-10 05:04:07 +02:00
private static boolean canBeFed(ItemStack stack, EntityAnimal animal) {
if (animal instanceof EntityHorse && ((EntityHorse) animal).isTame()) {
Item item = stack.getItem();
2019-02-27 19:53:05 +01:00
return animal.getGrowingAge() == 0 && !animal.isInLove() && (item == Items.GOLDEN_APPLE || item == Items.GOLDEN_CARROT);
2018-01-29 09:17:38 +01:00
}
return animal.getGrowingAge() == 0 && !animal.isInLove() && animal.isBreedingItem(stack);
}
}