Skip to content

Commit

Permalink
Tag support for Drum of the Gathering (#4454)
Browse files Browse the repository at this point in the history
The drum now uses entity tags to determine which mobs can fill milk
buckets and which shearable mobs can NOT be sheared:
`botania:drum/milkable` (defaults to cow, mooshroom, and goat) and
`botania:drum/no_shearing` (defaults to only mooshroom)
Additionally, if a brown mooshroom was fed a flower, the Drum of the
Gathering will fill the first bowl for that mooshroom with the
appropriate type of suspicious stew. Any additional bowls will be filled
with mushroom stew as usual.
And finally, baby animals are excluded from filling buckets or bowls.
(Whether they can be sheared is already determined by the mob's shearing
logic, which usually excludes babies as well.)

(closes #4453)
  • Loading branch information
TheRealWormbo authored Oct 3, 2023
1 parent 175d606 commit b4f47b5
Show file tree
Hide file tree
Showing 12 changed files with 371 additions and 52 deletions.
1 change: 1 addition & 0 deletions Fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"vazkii.botania.test.block.TargetBlockTest",
"vazkii.botania.test.block.ApothecaryRecipeTest",
"vazkii.botania.test.block.EntropinnyumUnethicalTntDetectionTest",
"vazkii.botania.test.block.DrumBlockTest",
"vazkii.botania.test.item.AstrolabeTest",
"vazkii.botania.test.item.CacophoniumTest",
"vazkii.botania.test.item.FlowerPouchTest",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

140 changes: 89 additions & 51 deletions Xplat/src/main/java/vazkii/botania/common/block/mana/DrumBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.Shearable;
import net.minecraft.world.entity.animal.Cow;
import net.minecraft.world.entity.animal.MushroomCow;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.SuspiciousStewItem;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
Expand All @@ -35,15 +35,21 @@
import vazkii.botania.common.block.BotaniaWaterloggedBlock;
import vazkii.botania.common.block.flower.functional.BergamuteBlockEntity;
import vazkii.botania.common.handler.BotaniaSounds;
import vazkii.botania.common.helper.EntityHelper;
import vazkii.botania.common.item.BotaniaItems;
import vazkii.botania.common.item.HornItem;
import vazkii.botania.common.lib.BotaniaTags;
import vazkii.botania.mixin.MushroomCowAccessor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DrumBlock extends BotaniaWaterloggedBlock {

public static final int MAX_NUM_SHEARED = 4;
public static final int GATHER_RANGE = 10;

public enum Variant {
WILD,
GATHERING,
Expand All @@ -64,23 +70,85 @@ public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, Co
return SHAPE;
}

private static void convertNearby(Entity entity, Item from, Item to) {
Level world = entity.level();
List<ItemEntity> items = world.getEntitiesOfClass(ItemEntity.class, entity.getBoundingBox());
for (ItemEntity item : items) {
ItemStack itemstack = item.getItem();
if (!itemstack.isEmpty() && itemstack.is(from) && !world.isClientSide) {
while (itemstack.getCount() > 0) {
ItemEntity ent = entity.spawnAtLocation(new ItemStack(to), 1.0F);
ent.setDeltaMovement(ent.getDeltaMovement().add(
world.random.nextFloat() * 0.05F,
(world.random.nextFloat() - world.random.nextFloat()) * 0.1F,
(world.random.nextFloat() - world.random.nextFloat()) * 0.1F
));
itemstack.shrink(1);
public static void gatherProduce(Level world, BlockPos pos) {
List<Mob> mobs = world.getEntitiesOfClass(Mob.class, new AABB(pos.offset(-GATHER_RANGE, -GATHER_RANGE, -GATHER_RANGE), pos.offset(GATHER_RANGE + 1, GATHER_RANGE + 1, GATHER_RANGE + 1)),
mob -> mob.isAlive() && !BergamuteBlockEntity.isBergamuteNearby(world, mob.getX(), mob.getY(), mob.getZ()));
List<Shearable> shearables = new ArrayList<>();

for (Mob mob : mobs) {
if (mob.getType().is(BotaniaTags.Entities.DRUM_MILKABLE) && !mob.isBaby()) {
convertNearby(mob, Items.BUCKET, Items.MILK_BUCKET);
}
if (mob instanceof MushroomCow mooshroom && !mooshroom.isBaby()) {
if (mooshroom.getVariant() == MushroomCow.MushroomType.BROWN && ((MushroomCowAccessor) mooshroom).getEffect() != null) {
fillBowlSuspiciously(mooshroom);
}
item.discard();
convertNearby(mob, Items.BOWL, Items.MUSHROOM_STEW);
}
if (mob instanceof Shearable shearable && !mob.getType().is(BotaniaTags.Entities.DRUM_NO_SHEARING) && shearable.readyForShearing()) {
shearables.add(shearable);
}
}

Collections.shuffle(shearables);
int sheared = 0;

for (Shearable shearable : shearables) {
if (sheared > MAX_NUM_SHEARED) {
break;
}

shearable.shear(SoundSource.BLOCKS);
++sheared;
}
}

private static void convertNearby(Mob mob, Item from, Item to) {
Level world = mob.level();
List<ItemEntity> fromEntities = world.getEntitiesOfClass(ItemEntity.class, mob.getBoundingBox(),
itemEntity -> itemEntity.isAlive() && itemEntity.getItem().is(from));
for (ItemEntity fromEntity : fromEntities) {
ItemStack fromStack = fromEntity.getItem();
for (int i = fromStack.getCount(); i > 0; i--) {
spawnItem(mob, new ItemStack(to));
}
fromEntity.discard();
}
}

private static void spawnItem(Mob mob, ItemStack to) {
Level world = mob.level();
ItemEntity ent = mob.spawnAtLocation(to, 1.0F);
ent.setDeltaMovement(ent.getDeltaMovement().add(
world.random.nextFloat() * 0.05F,
(world.random.nextFloat() - world.random.nextFloat()) * 0.1F,
(world.random.nextFloat() - world.random.nextFloat()) * 0.1F
));
}

private static void fillBowlSuspiciously(MushroomCow mushroomCow) {
MushroomCowAccessor mushroomCowAccessor = (MushroomCowAccessor) mushroomCow;
MobEffect effect = mushroomCowAccessor.getEffect();
int effectDuration = mushroomCowAccessor.getEffectDuration();

Level world = mushroomCow.level();
List<ItemEntity> bowlItemEntities = world.getEntitiesOfClass(ItemEntity.class, mushroomCow.getBoundingBox(),
itemEntity -> itemEntity.getItem().is(Items.BOWL) && !itemEntity.getItem().isEmpty());
for (ItemEntity bowlItemEntity : bowlItemEntities) {
ItemStack bowlItem = bowlItemEntity.getItem();
ItemStack stewItem = new ItemStack(Items.SUSPICIOUS_STEW);
SuspiciousStewItem.saveMobEffect(stewItem, effect, effectDuration);
spawnItem(mushroomCow, stewItem);

EntityHelper.shrinkItem(bowlItemEntity);
if (bowlItem.getCount() == 0) {
bowlItemEntity.discard();
}

// only one suspicious stew per flower fed
mushroomCowAccessor.setEffect(null);
mushroomCowAccessor.setEffectDuration(0);
break;
}
}

Expand All @@ -104,40 +172,10 @@ public void onBurstCollision(ManaBurst burst) {
world.addParticle(ParticleTypes.NOTE, pos.getX() + 0.5, pos.getY() + 1.2, pos.getZ() + 0.5D, 1.0 / 24.0, 0, 0);
return;
}
if (variant == Variant.WILD) {
HornItem.breakGrass(world, new ItemStack(BotaniaItems.grassHorn), pos, null);
} else if (variant == Variant.CANOPY) {
HornItem.breakGrass(world, new ItemStack(BotaniaItems.leavesHorn), pos, null);
} else {
int range = 10;
List<Mob> entities = world.getEntitiesOfClass(Mob.class, new AABB(pos.offset(-range, -range, -range), pos.offset(range + 1, range + 1, range + 1)), e -> !BergamuteBlockEntity.isBergamuteNearby(world, e.getX(), e.getY(), e.getZ()));
List<Mob> shearables = new ArrayList<>();

for (Mob entity : entities) {
if (entity instanceof Cow) {
convertNearby(entity, Items.BUCKET, Items.MILK_BUCKET);
if (entity instanceof MushroomCow) {
convertNearby(entity, Items.BOWL, Items.MUSHROOM_STEW);
}
} else if (entity instanceof Shearable shearable && shearable.readyForShearing()) {
shearables.add(entity);
}
}

Collections.shuffle(shearables);
int sheared = 0;

for (Mob entity : shearables) {
if (sheared > 4) {
break;
}

if (entity instanceof Shearable shearable) {
shearable.shear(SoundSource.BLOCKS);
}

++sheared;
}
switch (variant) {
case WILD -> HornItem.breakGrass(world, new ItemStack(BotaniaItems.grassHorn), pos, null);
case CANOPY -> HornItem.breakGrass(world, new ItemStack(BotaniaItems.leavesHorn), pos, null);
case GATHERING -> gatherProduce(world, pos);
}

for (int i = 0; i < 10; i++) {
Expand Down
10 changes: 10 additions & 0 deletions Xplat/src/main/java/vazkii/botania/common/lib/BotaniaTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ public static class Entities {
public static final TagKey<EntityType<?>> COCOON_COMMON_AQUATIC = tag("cocoon/common_aquatic");
public static final TagKey<EntityType<?>> COCOON_RARE_AQUATIC = tag("cocoon/rare_aquatic");

/**
* The Drum of the Gathering fills milk buckets for mobs in this tag.
*/
public static final TagKey<EntityType<?>> DRUM_MILKABLE = tag("drum/milkable");

/**
* The Drum of the Gathering will not shear mobs in this tag, even if they could be sheared.
*/
public static final TagKey<EntityType<?>> DRUM_NO_SHEARING = tag("drum/no_shearing");

/**
* Entities in this tag are immune to damage from the Key of the King's Law
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ protected void addTags(HolderLookup.Provider provider) {
.addOptional(new ResourceLocation("quark", "crab"));
tag(BotaniaTags.Entities.COCOON_RARE_AQUATIC).add(EntityType.DOLPHIN, EntityType.GLOW_SQUID, EntityType.AXOLOTL);

tag(BotaniaTags.Entities.DRUM_MILKABLE).add(EntityType.COW, EntityType.MOOSHROOM, EntityType.GOAT);
tag(BotaniaTags.Entities.DRUM_NO_SHEARING).add(EntityType.MOOSHROOM);

tag(BotaniaTags.Entities.SHADED_MESA_BLACKLIST).add(EntityType.ENDER_DRAGON, EntityType.WITHER,
EntityType.ITEM_FRAME, EntityType.GLOW_ITEM_FRAME, EntityType.END_CRYSTAL, EntityType.PAINTING,
EntityType.COMMAND_BLOCK_MINECART, EntityType.MARKER, EntityType.AREA_EFFECT_CLOUD,
Expand Down
24 changes: 24 additions & 0 deletions Xplat/src/main/java/vazkii/botania/mixin/MushroomCowAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package vazkii.botania.mixin;

import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.entity.animal.MushroomCow;

import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(MushroomCow.class)
public interface MushroomCowAccessor {
@Accessor
@Nullable
MobEffect getEffect();

@Accessor
void setEffect(@Nullable MobEffect effect);

@Accessor
int getEffectDuration();

@Accessor
void setEffectDuration(int effectDuration);
}
Loading

0 comments on commit b4f47b5

Please sign in to comment.