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

142 lines
5.7 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;
2021-11-13 18:16:25 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerFeeder;
2018-08-10 05:04:07 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.animal.horse.Horse;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import javax.annotation.Nullable;
2021-02-26 22:15:48 +01:00
import java.util.List;
import java.util.Optional;
2024-03-02 21:23:08 +01:00
public class TileEntityFeeder extends TileEntityInventoryBase implements MenuProvider {
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;
2024-03-02 21:23:08 +01:00
public TileEntityFeeder(BlockPos pos, BlockState state) {
super(ActuallyBlocks.FEEDER.getTileEntityType(), pos, state,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
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
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);
}
}
@Override
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
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");
}
}
2024-03-02 21:23:08 +01:00
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityFeeder tile) {
tile.clientTick();
tile.currentTimer = Mth.clamp(++tile.currentTimer, 0, 100);
2021-02-26 22:15:48 +01:00
}
2024-03-02 21:23:08 +01:00
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityFeeder tile) {
tile.serverTick();
tile.currentTimer = Mth.clamp(++tile.currentTimer, 0, 100);
int range = 5;
ItemStack stack = tile.inv.getStackInSlot(0);
if (!stack.isEmpty() && tile.currentTimer >= TIME) {
2024-03-12 16:35:48 +01:00
List<Animal> animals = level.getEntitiesOfClass(Animal.class, new AABB(pos).inflate(range));
2024-03-02 21:23:08 +01:00
tile.currentAnimalAmount = animals.size();
if (tile.currentAnimalAmount >= 2 && tile.currentAnimalAmount < THRESHOLD) {
2024-03-12 16:35:48 +01:00
Optional<Animal> opt = animals.stream().filter((e) -> tile.canBeFed(stack, e)).findAny();
2024-03-02 21:23:08 +01:00
if (opt.isPresent()) {
2024-03-12 16:35:48 +01:00
tile.feedAnimal(opt.get());
2024-03-02 21:23:08 +01:00
stack.shrink(1);
tile.currentTimer = 0;
tile.setChanged();
}
2018-08-10 05:04:07 +02:00
}
}
2024-03-02 21:23:08 +01:00
if ((tile.lastAnimalAmount != tile.currentAnimalAmount || tile.lastTimer != tile.currentTimer) && tile.sendUpdateWithInterval()) {
tile.lastAnimalAmount = tile.currentAnimalAmount;
tile.lastTimer = tile.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
2024-03-12 16:35:48 +01:00
private void feedAnimal(Animal animal) {
animal.setInLove(null);
2018-08-10 05:04:07 +02:00
for (int i = 0; i < 7; i++) {
2024-03-03 01:20:53 +01: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);
}
}
2018-08-10 05:04:07 +02:00
2024-03-12 16:35:48 +01:00
private boolean canBeFed(ItemStack stack, Animal animal) {
2024-03-02 21:23:08 +01:00
if (animal instanceof Horse && ((Horse) animal).isTamed()) {
2018-08-10 05:04:07 +02:00
Item item = stack.getItem();
return animal.getAge() == 0 && !animal.isInLove() && (item == Items.GOLDEN_APPLE || item == Items.GOLDEN_CARROT);
2018-01-29 09:17:38 +01:00
}
return animal.getAge() == 0 && !animal.isInLove() && animal.isFood(stack);
}
@Override
2024-03-02 21:23:08 +01:00
public Component getDisplayName() {
2024-03-03 01:20:53 +01:00
return Component.empty();
}
@Nullable
@Override
2024-03-02 21:23:08 +01:00
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player p_createMenu_3_) {
return new ContainerFeeder(windowId, playerInventory, this);
}
}