Skip to content

Commit

Permalink
Pollidisiac feeds flowers to brown mooshrooms
Browse files Browse the repository at this point in the history
Also includes smaller optimizations to the finding and feeding logic:
* don't look for anything if out of mana
* exclude items from the search that can't be interacted with
* don't look for animals when no items are around
* exclude animals from the search that would not be fed anyway because they are a baby, or already in the mood and not a brown mooshroom
  • Loading branch information
TheRealWormbo committed Dec 30, 2023
1 parent 39bf97a commit b80a5b1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
package vazkii.botania.common.block.flower.functional;

import net.minecraft.core.BlockPos;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.entity.EntityEvent;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.animal.MushroomCow;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.SuspiciousEffectHolder;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;

Expand All @@ -20,8 +25,11 @@
import vazkii.botania.common.block.BotaniaFlowerBlocks;
import vazkii.botania.common.helper.DelayHelper;
import vazkii.botania.common.helper.EntityHelper;
import vazkii.botania.mixin.MushroomCowAccessor;

import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;

public class PollidisiacBlockEntity extends FunctionalFlowerBlockEntity {
private static final int RANGE = 6;
Expand All @@ -35,37 +43,76 @@ public PollidisiacBlockEntity(BlockPos pos, BlockState state) {
public void tickFlower() {
super.tickFlower();

if (!getLevel().isClientSide) {
var pickupBounds = new AABB(getBlockPos().offset(-RANGE, -RANGE, -RANGE), getBlockPos().offset(RANGE + 1, RANGE + 1, RANGE + 1));
List<ItemEntity> items = getLevel().getEntitiesOfClass(ItemEntity.class, pickupBounds);
var bounds = new AABB(getEffectivePos().offset(-RANGE, -RANGE, -RANGE), getEffectivePos().offset(RANGE + 1, RANGE + 1, RANGE + 1));
List<Animal> animals = getLevel().getEntitiesOfClass(Animal.class, bounds);
if (!getLevel().isClientSide && getMana() >= MANA_COST) {
var pickupBounds = new AABB(getBlockPos()).inflate(RANGE);
List<ItemEntity> items = getLevel().getEntitiesOfClass(ItemEntity.class, pickupBounds,
itemEntity -> DelayHelper.canInteractWith(this, itemEntity));
if (items.isEmpty()) {
return;
}
var bounds = new AABB(getEffectivePos()).inflate(RANGE);
List<Animal> animals = getLevel().getEntitiesOfClass(Animal.class, bounds, Predicate.not(Animal::isBaby));
Collections.shuffle(animals);

for (Animal animal : animals) {
if (getMana() < MANA_COST) {
break;
// Note: Empty item stacks are implicitly excluded in Animal::isFood and ItemStack::is(TagKey)
if (!animal.isInLove()) {
for (ItemEntity item : items) {
if (!animal.isFood(item.getItem())) {
continue;
}
consumeFoodItem(item);

animal.setInLoveTime(1200);
getLevel().broadcastEntityEvent(animal, EntityEvent.IN_LOVE_HEARTS);
break;
}

if (getMana() < MANA_COST) {
break;
}
}

if (animal.getAge() == 0 && !animal.isInLove()) {
if (isBrownMooshroomWithoutEffect(animal)) {
for (ItemEntity item : items) {
if (!DelayHelper.canInteractWith(this, item)) {
ItemStack stack = item.getItem();
if (!stack.is(ItemTags.SMALL_FLOWERS)) {
continue;
}
var effect = SuspiciousEffectHolder.tryGet(stack.getItem());
if (effect == null) {
continue;
}
consumeFoodItem(item);

if (animal.isFood(item.getItem())) {
EntityHelper.shrinkItem(item);
MushroomCowAccessor cowAccessor = (MushroomCowAccessor) animal;
cowAccessor.setEffect(effect.getSuspiciousEffect());
cowAccessor.setEffectDuration(effect.getEffectDuration());
animal.playSound(SoundEvents.MOOSHROOM_EAT, 2.0F, 1.0F);
break;
}

addMana(-MANA_COST);
animal.setInLoveTime(1200);
getLevel().broadcastEntityEvent(animal, EntityEvent.IN_LOVE_HEARTS);
break;
}
if (getMana() < MANA_COST) {
break;
}
}
}
}
}

private void consumeFoodItem(ItemEntity itemEntity) {
EntityHelper.shrinkItem(itemEntity);
addMana(-MANA_COST);
}

private static boolean isBrownMooshroomWithoutEffect(Animal animal) {
if (animal instanceof MushroomCow mushroomCow && mushroomCow.getVariant() == MushroomCow.MushroomType.BROWN) {
MushroomCowAccessor cowAccessor = (MushroomCowAccessor) animal;
return cowAccessor.getEffect() == null;
}
return false;
}

@Override
public RadiusDescriptor getRadius() {
return RadiusDescriptor.Rectangle.square(getEffectivePos(), RANGE);
Expand Down
3 changes: 3 additions & 0 deletions web/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ and start a new "Upcoming" section.

{% include changelog_header.html version="Upcoming" %}

* Add: Pollidisiac can feed flowers to brown mooshrooms so they produce suspicious stew

---

{% include changelog_header.html version="1.20.1-441" %}

Expand Down

0 comments on commit b80a5b1

Please sign in to comment.