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;
|
2015-02-17 16:15:16 +01:00
|
|
|
|
2021-03-01 18:46:12 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.ContainerFeeder;
|
2018-08-10 05:04:07 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.entity.passive.AnimalEntity;
|
|
|
|
import net.minecraft.entity.passive.horse.HorseEntity;
|
2021-03-01 18:46:12 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.entity.player.PlayerInventory;
|
|
|
|
import net.minecraft.inventory.container.Container;
|
|
|
|
import net.minecraft.inventory.container.INamedContainerProvider;
|
2016-10-19 15:05:42 +02:00
|
|
|
import net.minecraft.item.Item;
|
2015-03-07 02:23:31 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.item.Items;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.particles.ParticleTypes;
|
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-03-01 18:46:12 +01:00
|
|
|
import net.minecraft.util.text.ITextComponent;
|
|
|
|
import net.minecraft.util.text.StringTextComponent;
|
2017-06-29 18:36:33 +02:00
|
|
|
|
2021-03-01 18:46:12 +01:00
|
|
|
import javax.annotation.Nullable;
|
2021-02-26 22:15:48 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase.NBTType;
|
|
|
|
|
2021-03-01 18:46:12 +01:00
|
|
|
public class TileEntityFeeder extends TileEntityInventoryBase implements INamedContainerProvider {
|
2015-02-17 16:15:16 +01:00
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
public static final int THRESHOLD = 30;
|
|
|
|
private static final int TIME = 100;
|
2015-02-17 16:15:16 +01:00
|
|
|
public int currentTimer;
|
|
|
|
public int currentAnimalAmount;
|
2015-07-02 04:21:21 +02:00
|
|
|
private int lastAnimalAmount;
|
|
|
|
private int lastTimer;
|
2015-02-17 16:15:16 +01:00
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
public TileEntityFeeder() {
|
2021-02-27 13:24:45 +01:00
|
|
|
super(ActuallyTiles.FEEDER_TILE.get(), 1);
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-02-01 20:32:49 +01:00
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putInt("Timer", this.currentTimer);
|
2018-08-10 05:04:07 +02:00
|
|
|
if (type == NBTType.SYNC) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putInt("Animals", this.currentAnimalAmount);
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2021-02-27 16:33:00 +01:00
|
|
|
this.currentTimer = compound.getInt("Timer");
|
2018-08-10 05:04:07 +02:00
|
|
|
if (type == NBTType.SYNC) {
|
2021-02-27 16:33:00 +01:00
|
|
|
this.currentAnimalAmount = compound.getInt("Animals");
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public void updateEntity() {
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2019-02-27 19:53:05 +01:00
|
|
|
this.currentTimer = MathHelper.clamp(++this.currentTimer, 0, 100);
|
2021-08-22 17:09:06 +02:00
|
|
|
if (this.level.isClientSide) {
|
2021-02-26 22:15:48 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-01-29 09:17:38 +01:00
|
|
|
int range = 5;
|
2018-06-23 01:39:30 +02:00
|
|
|
ItemStack stack = this.inv.getStackInSlot(0);
|
2018-08-10 05:04:07 +02:00
|
|
|
if (!stack.isEmpty() && this.currentTimer >= TIME) {
|
2021-08-22 17:09:06 +02:00
|
|
|
List<AnimalEntity> animals = this.level.getEntitiesOfClass(AnimalEntity.class, new AxisAlignedBB(this.worldPosition.getX() - range, this.worldPosition.getY() - range, this.worldPosition.getZ() - range, this.worldPosition.getX() + range, this.worldPosition.getY() + range, this.worldPosition.getZ() + range));
|
2018-08-10 05:04:07 +02:00
|
|
|
this.currentAnimalAmount = animals.size();
|
2019-02-27 19:53:05 +01:00
|
|
|
if (this.currentAnimalAmount >= 2 && this.currentAnimalAmount < THRESHOLD) {
|
2021-02-27 16:33:00 +01:00
|
|
|
Optional<AnimalEntity> opt = animals.stream().filter((e) -> canBeFed(stack, e)).findAny();
|
2018-08-10 05:04:07 +02:00
|
|
|
if (opt.isPresent()) {
|
|
|
|
feedAnimal(opt.get());
|
|
|
|
stack.shrink(1);
|
|
|
|
this.currentTimer = 0;
|
2021-08-22 17:09:06 +02:00
|
|
|
this.setChanged();
|
2018-08-10 05:04:07 +02:00
|
|
|
}
|
2015-02-17 16:15:16 +01: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-10-19 15:05:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
private static void feedAnimal(AnimalEntity animal) {
|
2016-01-07 23:42:42 +01:00
|
|
|
animal.setInLove(null);
|
2018-08-10 05:04:07 +02:00
|
|
|
for (int i = 0; i < 7; i++) {
|
2021-08-22 17:09:06 +02:00
|
|
|
double d = animal.level.random.nextGaussian() * 0.02D;
|
|
|
|
double d1 = animal.level.random.nextGaussian() * 0.02D;
|
|
|
|
double d2 = animal.level.random.nextGaussian() * 0.02D;
|
|
|
|
animal.level.addParticle(ParticleTypes.HEART, animal.getX() + animal.level.random.nextFloat() * animal.getBbWidth() * 2.0F - animal.getBbWidth(), animal.getY() + 0.5D + animal.level.random.nextFloat() * animal.getBbHeight(), animal.getZ() + animal.level.random.nextFloat() * animal.getBbWidth() * 2.0F - animal.getBbWidth(), d, d1, d2);
|
2015-02-20 22:45:33 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-10 05:04:07 +02:00
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
private static boolean canBeFed(ItemStack stack, AnimalEntity animal) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (animal instanceof HorseEntity && ((HorseEntity) animal).isTamed()) {
|
2018-08-10 05:04:07 +02:00
|
|
|
Item item = stack.getItem();
|
2021-08-22 17:09:06 +02:00
|
|
|
return animal.getAge() == 0 && !animal.isInLove() && (item == Items.GOLDEN_APPLE || item == Items.GOLDEN_CARROT);
|
2018-01-29 09:17:38 +01:00
|
|
|
}
|
2021-08-22 17:09:06 +02:00
|
|
|
return animal.getAge() == 0 && !animal.isInLove() && animal.isFood(stack);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
2021-03-01 18:46:12 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public ITextComponent getDisplayName() {
|
|
|
|
return StringTextComponent.EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public Container createMenu(int windowId, PlayerInventory playerInventory, PlayerEntity p_createMenu_3_) {
|
|
|
|
return new ContainerFeeder(windowId, playerInventory, this);
|
|
|
|
}
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|