| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import net.minecraft.tags.EntityTypeTags;
|
| 5 | import net.minecraft.world.entity.LivingEntity;
|
| 6 | import net.minecraft.world.entity.Mob;
|
| 7 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 8 | import net.minecraft.world.entity.animal.golem.CopperGolem;
|
| 9 | import net.minecraft.world.entity.animal.golem.IronGolem;
|
| 10 | import net.minecraft.world.item.Item;
|
| 11 | import net.minecraft.world.item.Items;
|
| 12 | import net.minecraft.world.phys.AABB;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public class OfferFlowerGoal extends Goal {
|
| 16 | private static final TargetingConditions OFFER_TARGET_CONTEXT = TargetingConditions.forNonCombat().range(6.0);
|
| 17 | private static final Item OFFER_ITEM = Items.POPPY;
|
| 18 | public static final int OFFER_TICKS = 400;
|
| 19 | private final IronGolem golem;
|
| 20 | private @Nullable LivingEntity entity;
|
| 21 | private int tick;
|
| 22 |
|
| 23 | public OfferFlowerGoal(final IronGolem golem) {
|
| 24 | this.golem = golem;
|
| 25 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public boolean canUse() {
|
| 30 | if (!this.golem.level().isBrightOutside()) {
|
| 31 | return false;
|
| 32 | }
|
| 33 |
|
| 34 | if (this.golem.getRandom().nextInt(8000) != 0) {
|
| 35 | return false;
|
| 36 | }
|
| 37 |
|
| 38 | this.entity = getServerLevel(this.golem)
|
| 39 | .getNearestEntity(
|
| 40 | EntityTypeTags.CANDIDATE_FOR_IRON_GOLEM_GIFT,
|
| 41 | OFFER_TARGET_CONTEXT,
|
| 42 | this.golem,
|
| 43 | this.golem.getX(),
|
| 44 | this.golem.getY(),
|
| 45 | this.golem.getZ(),
|
| 46 | this.getGolemBoundingBox()
|
| 47 | );
|
| 48 | return this.entity != null;
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public boolean canContinueToUse() {
|
| 53 | return this.tick > 0;
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public void start() {
|
| 58 | this.tick = this.adjustedTickDelay(400);
|
| 59 | this.golem.offerFlower(true);
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public void stop() {
|
| 64 | this.golem.offerFlower(false);
|
| 65 | if (this.tick == 0
|
| 66 | && this.entity instanceof Mob mob
|
| 67 | && mob.is(EntityTypeTags.ACCEPTS_IRON_GOLEM_GIFT)
|
| 68 | && mob.getItemBySlot(CopperGolem.EQUIPMENT_SLOT_ANTENNA).isEmpty()
|
| 69 | && this.getGolemBoundingBox().intersects(mob.getBoundingBox())) {
|
| 70 | mob.setItemSlot(CopperGolem.EQUIPMENT_SLOT_ANTENNA, OFFER_ITEM.getDefaultInstance());
|
| 71 | mob.setGuaranteedDrop(CopperGolem.EQUIPMENT_SLOT_ANTENNA);
|
| 72 | }
|
| 73 |
|
| 74 | this.entity = null;
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public void tick() {
|
| 79 | if (this.entity != null) {
|
| 80 | this.golem.getLookControl().setLookAt(this.entity, 30.0F, 30.0F);
|
| 81 | }
|
| 82 |
|
| 83 | this.tick--;
|
| 84 | }
|
| 85 |
|
| 86 | private AABB getGolemBoundingBox() {
|
| 87 | return this.golem.getBoundingBox().inflate(6.0, 2.0, 6.0);
|
| 88 | }
|
| 89 | }
|
| 90 |
|