Делаю мод на улучшение лошадей со своими характеристиками. Сделал класс унаследованный от MoveToBlockGoal, что бы она могла кушать из кормушки. В математике вообще не силён и в вот этих вот формулах. Может кто подскажет как сделать так что бы она подходила к кормушке, а не становилась на неё сверху. По идеи дело в строках 65 - 71 Если потребуется любая друга часть кода - сообщите
MoveToBlockGoal:
package com.pyotcho.allabouthorses.events.goals;
import com.pyotcho.allabouthorses.api.capabilities.AAHCapabilities;
import com.pyotcho.allabouthorses.api.hunger.IHunger;
import com.pyotcho.allabouthorses.common.block.ModBlocks;
import com.pyotcho.allabouthorses.common.block.blocks.FeederBlock;
import com.pyotcho.allabouthorses.common.block.blocks.FullFeederBlock;
import com.pyotcho.allabouthorses.config.ClientConfig;
import com.pyotcho.allabouthorses.util.init.ModPackets;
import com.pyotcho.allabouthorses.util.init.ModPacketsHandler;
import net.minecraft.core.BlockPos;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.ai.goal.MoveToBlockGoal;
import net.minecraft.world.entity.animal.horse.Horse;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.network.PacketDistributor;
import org.jetbrains.annotations.NotNull;
public class HorseEatFeederGoal extends MoveToBlockGoal {
private final Horse horse;
private static final int WAIT_TICKS = 80;
private int ticksWaited;
private IHunger hunger;
public HorseEatFeederGoal(Horse horse, double speed, int searchRange, int verticalRange, IHunger hunger) {
super(horse, speed, searchRange, verticalRange);
this.horse = horse;
this.hunger = hunger;
}
@Override
public boolean canUse() {
return hunger.getHunger() < 30 && super.canUse();
}
@Override
public boolean canContinueToUse() {
return hunger.getHunger() < 30 && this.isValidTarget(this.horse.level, this.blockPos);
}
@Override
protected boolean isValidTarget(LevelReader level, @NotNull BlockPos blockPos) {
BlockState blockState = level.getBlockState(blockPos);
return blockState.is(ModBlocks.FULL_FEEDER_BLOCK.get());
}
protected void onReachedTarget() {
if (!horse.level.isClientSide() && this.hunger.getHunger() < 30) {
horse.setEating(true);
checkTheValueOfBlock(horse.level.getBlockState(blockPos));
hunger.setHunger(Math.min(hunger.getHunger() + ClientConfig.feederHungerRestore.get(), ClientConfig.hungerMaxLevel.get()));
horse.getCapability(AAHCapabilities.THIRST).ifPresent(thirst -> ModPacketsHandler.INSTANCE.send(
PacketDistributor.TRACKING_ENTITY.with(() -> horse),
new ModPackets(horse.getId(), hunger.getHunger(), thirst.getThirst())
));
horse.setEating(false);
hunger.setTickTimer(1);
}
}
private boolean isCloseEnoughToTarget() {
return this.horse.blockPosition()
.distToCenterSqr(
blockPos.getX() + 0.5D,
blockPos.getY() + 0.5D,
blockPos.getZ() + 0.5D)
<= this.acceptedDistance() * this.acceptedDistance();
}
@Override
public void tick() {
if(hunger.getHunger() >= 30){
this.stop();
return;
}
if (this.isCloseEnoughToTarget()) {
if (this.ticksWaited >= WAIT_TICKS) {
this.onReachedTarget();
this.ticksWaited = 0;
} else {
this.ticksWaited++;
}
} else if (horse.getRandom().nextFloat() < 0.05F) {
horse.playSound(SoundEvents.HORSE_STEP, 1.0F, 1.0F);
}
super.tick();
}
@Override
public void start() {
this.ticksWaited = 0;
super.start();
}
@Override
public double acceptedDistance() {
return 2.0D;
}
@Override
public boolean shouldRecalculatePath() {
return this.tryTicks % 20 == 0;
}
private void checkTheValueOfBlock(BlockState block) {
if (block.getBlock() instanceof FullFeederBlock) {
int currentLevel = block.getValue(FullFeederBlock.LEVEL);
if (currentLevel > 1) {
horse.level.setBlock(blockPos, block.setValue(FullFeederBlock.LEVEL, currentLevel - 1), 3);
} else {
horse.level.setBlock(blockPos, ModBlocks.FEEDER_BLOCK.get().defaultBlockState()
.setValue(FeederBlock.FACING, block.getValue(FullFeederBlock.FACING)), 3);
}
}
}
}