Initial commit

This commit is contained in:
2025-07-08 02:30:03 +02:00
parent 68ef26b9c4
commit 1376c1b738
20 changed files with 726 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
package com.kasetoatz.hungryfrog;
import com.kasetoatz.hungryfrog.item.FrogInfestationItem;
import com.kasetoatz.hungryfrog.sensor.NearestBlockSensor;
import com.mojang.serialization.codecs.ListCodec;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.entity.ai.brain.MemoryModuleType;
import net.minecraft.entity.ai.brain.sensor.SensorType;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroups;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
public class HungryFrog implements ModInitializer
{
public static final MemoryModuleType<BlockPos> BLOCK_TO_EAT = new MemoryModuleType<>(Optional.of(BlockPos.CODEC));
public static final MemoryModuleType<List<BlockPos>> UNREACHABLE_BLOCK_TARGETS = new MemoryModuleType<>(Optional.of(new ListCodec<>(BlockPos.CODEC, 0, 5)));
public static final SensorType<NearestBlockSensor> NEAREST_BLOCK_SENSOR = new SensorType<>(NearestBlockSensor::new);
public static final Item FROG_INFESTATION = register("frog_infestation", FrogInfestationItem::new, new Item.Settings().maxCount(1));
public static Item register(String path, Function<Item.Settings, Item> factory, Item.Settings settings)
{
final RegistryKey<Item> registryKey = RegistryKey.of(RegistryKeys.ITEM, Identifier.of("hungryfrog", path));
return Items.register(registryKey, factory, settings);
}
@Override
public void onInitialize()
{
Registry.register(Registries.MEMORY_MODULE_TYPE, Identifier.of("hungryfrog", "block_to_eat"), BLOCK_TO_EAT);
Registry.register(Registries.MEMORY_MODULE_TYPE, Identifier.of("hungryfrog", "unreachable_block_targets"), UNREACHABLE_BLOCK_TARGETS);
Registry.register(Registries.SENSOR_TYPE, Identifier.of("hungryfrog", "nearest_block_sensor"), NEAREST_BLOCK_SENSOR);
ItemGroupEvents.modifyEntriesEvent(ItemGroups.TOOLS).register(entries -> {
entries.add(FROG_INFESTATION);
});
}
}

View File

@@ -0,0 +1,37 @@
package com.kasetoatz.hungryfrog.item;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.passive.FrogEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
public class FrogInfestationItem extends Item {
public FrogInfestationItem(Settings settings) {
super(settings);
}
public ActionResult use(World world, PlayerEntity user, Hand hand)
{
HitResult hit = user.raycast(1000, 1.F, true);
if (hit.getType() == HitResult.Type.BLOCK)
{
Vec3d pos = hit.getPos().add(0.F, 1.F, 0.F);
for (int i = 0; i < 100; i++)
{
FrogEntity frog = EntityType.FROG.create(world, SpawnReason.MOB_SUMMONED);
if (frog != null)
{
frog.setPos(pos.getX(), pos.getY(), pos.getZ());
world.spawnEntity(frog);
}
}
}
return ActionResult.SUCCESS;
}
}

View File

@@ -0,0 +1,22 @@
package com.kasetoatz.hungryfrog.mixin;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.brain.Activity;
import net.minecraft.entity.ai.brain.Brain;
import net.minecraft.entity.ai.brain.sensor.Sensor;
import net.minecraft.entity.ai.brain.sensor.SensorType;
import net.minecraft.entity.ai.brain.task.Task;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import java.util.Map;
import java.util.Set;
@Mixin(Brain.class)
public interface BrainAccessor<E extends LivingEntity>
{
@Accessor("sensors")
Map<SensorType<? extends Sensor<? super E>>, Sensor<? super E>> getSensors();
@Accessor("tasks")
Map<Integer, Map<Activity, Set<Task<? super E>>>> getTasks();
}

View File

@@ -0,0 +1,30 @@
package com.kasetoatz.hungryfrog.mixin;
import com.kasetoatz.hungryfrog.HungryFrog;
import com.kasetoatz.hungryfrog.sensor.NearestBlockSensor;
import com.kasetoatz.hungryfrog.task.FrogEatBlockTask;
import net.minecraft.entity.ai.brain.Activity;
import net.minecraft.entity.ai.brain.Brain;
import net.minecraft.entity.passive.FrogBrain;
import net.minecraft.entity.passive.FrogEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.HashMap;
import java.util.LinkedHashSet;
@Mixin(FrogBrain.class)
public class FrogBrainMixin
{
@Inject(method = "create", at = @At("RETURN"), cancellable = true)
private static void create(Brain<FrogEntity> brain, CallbackInfoReturnable<Brain<?>> cir)
{
Brain<?> frog = cir.getReturnValue();
@SuppressWarnings("unchecked")
BrainAccessor<FrogEntity> accessor = (BrainAccessor<FrogEntity>) frog;
accessor.getSensors().put(HungryFrog.NEAREST_BLOCK_SENSOR, new NearestBlockSensor());
accessor.getTasks().computeIfAbsent(1, p -> new HashMap<>()).computeIfAbsent(Activity.IDLE, a -> new LinkedHashSet<>()).add(new FrogEatBlockTask());
cir.setReturnValue(frog);
}
}

View File

@@ -0,0 +1,50 @@
package com.kasetoatz.hungryfrog.mixin;
import com.kasetoatz.hungryfrog.HungryFrog;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.brain.Brain;
import net.minecraft.entity.ai.brain.MemoryModuleType;
import net.minecraft.entity.ai.brain.sensor.Sensor;
import net.minecraft.entity.ai.brain.sensor.SensorType;
import net.minecraft.entity.attribute.DefaultAttributeContainer;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.passive.FrogEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.ArrayList;
@Mixin(FrogEntity.class)
public class FrogEntityMixin
{
@Inject(method = "isValidFrogFood", at = @At("HEAD"), cancellable = true)
private static void isValidFrogFood(LivingEntity entity, CallbackInfoReturnable<Boolean> cir)
{
cir.setReturnValue(!(entity instanceof FrogEntity));
}
@Inject(method = "createFrogAttributes", at = @At("RETURN"), cancellable = true)
private static void createFrogAttributes(CallbackInfoReturnable<DefaultAttributeContainer.Builder> cir)
{
DefaultAttributeContainer.Builder attr = cir.getReturnValue();
attr.add(EntityAttributes.ATTACK_DAMAGE, Double.MAX_VALUE);
cir.setReturnValue(attr);
}
@Inject(method = "createBrainProfile", at = @At("RETURN"), cancellable = true)
private void createBrainProfile(CallbackInfoReturnable<Brain.Profile<FrogEntity>> cir)
{
@SuppressWarnings("unchecked")
ProfileAccessor<FrogEntity> profile = (ProfileAccessor<FrogEntity>)(Object)cir.getReturnValue();
if (profile != null)
{
ArrayList<MemoryModuleType<?>> memories = new ArrayList<>(profile.getMemoryModules());
ArrayList<SensorType<? extends Sensor<? super FrogEntity>>> sensors = new ArrayList<>(profile.getSensors());
memories.add(HungryFrog.BLOCK_TO_EAT);
memories.add(HungryFrog.UNREACHABLE_BLOCK_TARGETS);
sensors.add(HungryFrog.NEAREST_BLOCK_SENSOR);
cir.setReturnValue(Brain.createProfile(memories, sensors));
}
}
}

View File

@@ -0,0 +1,22 @@
package com.kasetoatz.hungryfrog.mixin;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.passive.FrogEntity;
import net.minecraft.registry.tag.DamageTypeTags;
import net.minecraft.server.world.ServerWorld;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(LivingEntity.class)
public class LivingEntityMixin {
@Inject(method = "damage", at = @At("HEAD"), cancellable = true)
private void damage(ServerWorld world, DamageSource source, float amount, CallbackInfoReturnable<Boolean> cir)
{
if ((LivingEntity)(Object)this instanceof FrogEntity && source.isIn(DamageTypeTags.IS_FIRE)) {
cir.setReturnValue(false);
}
}
}

View File

@@ -0,0 +1,20 @@
package com.kasetoatz.hungryfrog.mixin;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.brain.Brain;
import net.minecraft.entity.ai.brain.MemoryModuleType;
import net.minecraft.entity.ai.brain.sensor.Sensor;
import net.minecraft.entity.ai.brain.sensor.SensorType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import java.util.Collection;
@Mixin(Brain.Profile.class)
public interface ProfileAccessor<E extends LivingEntity>
{
@Accessor("memoryModules")
Collection<? extends MemoryModuleType<?>> getMemoryModules();
@Accessor("sensors")
Collection<? extends SensorType<? extends Sensor<? super E>>> getSensors();
}

View File

@@ -0,0 +1,59 @@
package com.kasetoatz.hungryfrog.sensor;
import com.google.common.collect.ImmutableSet;
import com.kasetoatz.hungryfrog.HungryFrog;
import net.minecraft.block.Blocks;
import net.minecraft.entity.ai.brain.Brain;
import net.minecraft.entity.ai.brain.MemoryModuleType;
import net.minecraft.entity.ai.brain.sensor.Sensor;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.passive.FrogEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import java.util.*;
public class NearestBlockSensor extends Sensor<FrogEntity>
{
protected void sense(ServerWorld world, FrogEntity frog)
{
int range = (int) frog.getAttributeValue(EntityAttributes.FOLLOW_RANGE);
Set<BlockPos> unreachable = new HashSet<>(frog.getBrain().getOptionalRegisteredMemory(HungryFrog.UNREACHABLE_BLOCK_TARGETS).orElse(new ArrayList<>()));
BlockPos entityPos = frog.getBlockPos();
BlockPos.Mutable pos = new BlockPos.Mutable();
BlockPos closest = null;
double closestDistance = Double.MAX_VALUE;
for (int dx = -range; dx < range; dx++)
{
for (int dy = -range; dy < range; dy++)
{
for (int dz = -range; dz < range; dz++)
{
pos.set(entityPos.getX() + dx, entityPos.getY() + dy, entityPos.getZ() + dz);
if (!unreachable.contains(pos) && !world.getBlockState(pos).isAir())
{
double distance = entityPos.getSquaredDistance(pos);
if (distance < closestDistance)
{
closestDistance = distance;
closest = pos.toImmutable();
}
}
}
}
}
Brain<?> brain = frog.getBrain();
if (closest != null)
{
brain.remember(HungryFrog.BLOCK_TO_EAT, closest);
}
else
{
brain.forget(HungryFrog.BLOCK_TO_EAT);
}
}
public Set<MemoryModuleType<?>> getOutputMemoryModules()
{
return ImmutableSet.of(HungryFrog.BLOCK_TO_EAT);
}
}

View File

@@ -0,0 +1,235 @@
package com.kasetoatz.hungryfrog.task;
import com.google.common.collect.ImmutableMap;
import com.kasetoatz.hungryfrog.HungryFrog;
import net.minecraft.block.*;
import net.minecraft.command.argument.EntityAnchorArgumentType;
import net.minecraft.entity.EntityPose;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.ai.brain.MemoryModuleState;
import net.minecraft.entity.ai.brain.MemoryModuleType;
import net.minecraft.entity.ai.brain.WalkTarget;
import net.minecraft.entity.ai.brain.task.MultiTickTask;
import net.minecraft.entity.ai.pathing.Path;
import net.minecraft.entity.passive.FrogEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.tag.FluidTags;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class FrogEatBlockTask extends MultiTickTask<FrogEntity>
{
public static final SoundEvent TONGUE_SOUND = SoundEvents.ENTITY_FROG_TONGUE;
public static final SoundEvent EAT_SOUND = SoundEvents.ENTITY_FROG_EAT;
private int eatTick;
private int moveToTargetTick;
private Phase phase;
public FrogEatBlockTask()
{
super(ImmutableMap.of(HungryFrog.BLOCK_TO_EAT, MemoryModuleState.VALUE_PRESENT), 100);
this.phase = Phase.DONE;
}
protected boolean shouldRun(ServerWorld world, FrogEntity frog)
{
Optional<BlockPos> memory = frog.getBrain().getOptionalRegisteredMemory(HungryFrog.BLOCK_TO_EAT);
if (memory.isEmpty())
{
return false;
}
BlockPos pos = memory.get();
boolean reachable = this.isBlockReachable(frog, pos);
if (!reachable)
{
frog.getBrain().forget(HungryFrog.BLOCK_TO_EAT);
this.markTargetAsUnreachable(frog, pos);
}
return reachable && frog.getPose() != EntityPose.CROAKING && frog.getBrain().getOptionalRegisteredMemory(MemoryModuleType.ATTACK_TARGET).isEmpty();
}
protected boolean shouldKeepRunning(ServerWorld world, FrogEntity frog, long time)
{
return frog.getBrain().hasMemoryModule(HungryFrog.BLOCK_TO_EAT) && this.phase != Phase.DONE && !frog.getBrain().hasMemoryModule(MemoryModuleType.IS_PANICKING);
}
protected void run(ServerWorld world, FrogEntity frog, long time)
{
Optional<BlockPos> value = frog.getBrain().getOptionalRegisteredMemory(HungryFrog.BLOCK_TO_EAT);
if (value.isEmpty())
{
return;
}
BlockPos pos = value.get();
frog.lookAt(EntityAnchorArgumentType.EntityAnchor.EYES, Vec3d.of(pos));
frog.getBrain().remember(MemoryModuleType.WALK_TARGET, new WalkTarget(pos, 2.F, 0));
this.moveToTargetTick = 10;
this.phase = Phase.MOVE_TO_TARGET;
}
protected void finishRunning(ServerWorld world, FrogEntity frog, long time)
{
frog.getBrain().forget(HungryFrog.BLOCK_TO_EAT);
frog.setPose(EntityPose.STANDING);
}
private void absorb(ServerWorld world, BlockPos pos)
{
BlockPos.iterateRecursively(pos, 6, 65, (currentPos, queuer) ->
{
for (Direction direction : Direction.values())
{
queuer.accept(currentPos.offset(direction));
}
}, currentPos ->
{
if (currentPos.equals(pos))
{
return BlockPos.IterationState.ACCEPT;
}
else
{
BlockState blockState = world.getBlockState(currentPos);
FluidState fluidState = world.getFluidState(currentPos);
if (!fluidState.isIn(FluidTags.WATER) && !fluidState.isIn(FluidTags.LAVA))
{
return BlockPos.IterationState.SKIP;
}
else if (blockState.getBlock() instanceof FluidDrainable fluidDrainable && !fluidDrainable.tryDrainFluid(null, world, currentPos, blockState).isEmpty())
{
return BlockPos.IterationState.ACCEPT;
}
else
{
if (blockState.getBlock() instanceof FluidBlock)
{
world.setBlockState(currentPos, Blocks.AIR.getDefaultState(), Block.NOTIFY_ALL);
}
else
{
if (!blockState.isOf(Blocks.KELP) && !blockState.isOf(Blocks.KELP_PLANT) && !blockState.isOf(Blocks.SEAGRASS) && !blockState.isOf(Blocks.TALL_SEAGRASS))
{
return BlockPos.IterationState.SKIP;
}
world.setBlockState(currentPos, Blocks.AIR.getDefaultState(), Block.NOTIFY_ALL);
}
return BlockPos.IterationState.ACCEPT;
}
}
});
}
private void eat(ServerWorld world, FrogEntity frog, BlockPos pos)
{
world.playSoundFromEntity(null, frog, EAT_SOUND, SoundCategory.NEUTRAL, 2.0F, 1.0F);
BlockState state = world.getBlockState(pos);
Item item = Item.BLOCK_ITEMS.get(state.getBlock());
if (state.isOf(Blocks.WATER) || state.isOf(Blocks.LAVA))
{
world.setBlockState(pos, Blocks.AIR.getDefaultState(), Block.NOTIFY_ALL);
this.absorb(world, pos);
}
else
{
world.removeBlock(pos, false);
}
if (item == null)
{
return;
}
ItemStack stack = new ItemStack(item);
ItemEntity entity = new ItemEntity(world, frog.getX(), frog.getY(), frog.getZ(), stack);
world.spawnEntity(entity);
}
protected void keepRunning(ServerWorld world, FrogEntity frog, long time)
{
Optional<BlockPos> memory = frog.getBrain().getOptionalRegisteredMemory(HungryFrog.BLOCK_TO_EAT);
if (memory.isEmpty())
{
return;
}
BlockPos pos = memory.get();
switch (this.phase)
{
case MOVE_TO_TARGET:
if (frog.getPos().distanceTo(Vec3d.of(pos)) < 1.75F)
{
world.playSoundFromEntity(null, frog, TONGUE_SOUND, SoundCategory.NEUTRAL, 2.0F, 1.0F);
frog.setPose(EntityPose.USING_TONGUE);
this.eatTick = 0;
this.phase = Phase.CATCH_ANIMATION;
}
else if (this.moveToTargetTick <= 0)
{
frog.getBrain().remember(MemoryModuleType.WALK_TARGET, new WalkTarget(pos, 2.F, 0));
this.moveToTargetTick = 10;
}
else
{
this.moveToTargetTick--;
}
break;
case CATCH_ANIMATION:
if (this.eatTick++ >= 6)
{
this.phase = Phase.EAT_ANIMATION;
this.eat(world, frog, pos);
}
break;
case EAT_ANIMATION:
if (this.eatTick >= 10)
{
this.phase = Phase.DONE;
}
else
{
this.eatTick++;
}
}
}
private boolean isBlockReachable(FrogEntity frog, BlockPos pos)
{
Path path = frog.getNavigation().findPathTo(pos, 0);
return path != null && path.getManhattanDistanceFromTarget() < 1.75F;
}
private void markTargetAsUnreachable(FrogEntity frog, BlockPos pos) {
try
{
List<BlockPos> list = frog.getBrain().getOptionalRegisteredMemory(HungryFrog.UNREACHABLE_BLOCK_TARGETS).orElse(new ArrayList<>());
boolean contains = !list.contains(pos);
if (list.size() == 5 && contains) {
list.removeFirst();
}
if (contains) {
list.add(pos);
}
frog.getBrain().remember(HungryFrog.UNREACHABLE_BLOCK_TARGETS, list, 100L);
}
catch (Exception e)
{
frog.getBrain().forget(HungryFrog.UNREACHABLE_BLOCK_TARGETS);
}
}
private enum Phase
{
MOVE_TO_TARGET,
CATCH_ANIMATION,
EAT_ANIMATION,
DONE
}
}

View File

@@ -0,0 +1,6 @@
{
"model": {
"type": "model",
"model": "hungryfrog:item/frog_infestation"
}
}

View File

@@ -0,0 +1,3 @@
{
"item.hungryfrog.frog_infestation": "Frog Infestation"
}

View File

@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "minecraft:item/stick"
}
}

View File

@@ -0,0 +1,24 @@
{
"schemaVersion": 1,
"id": "hungryfrog",
"version": "${version}",
"name": "HungryFrog",
"description": "",
"authors": [],
"contact": {},
"license": "MIT",
"environment": "*",
"entrypoints": {
"main": [
"com.kasetoatz.hungryfrog.HungryFrog"
]
},
"mixins": [
"hungryfrog.mixins.json"
],
"depends": {
"fabricloader": ">=${loader_version}",
"fabric": "*",
"minecraft": "${minecraft_version}"
}
}

View File

@@ -0,0 +1,19 @@
{
"required": true,
"minVersion": "0.8",
"package": "com.kasetoatz.hungryfrog.mixin",
"compatibilityLevel": "JAVA_21",
"mixins": [
"BrainAccessor",
"FrogBrainMixin",
"FrogEntityMixin",
"LivingEntityMixin",
"ProfileAccessor"
],
"injectors": {
"defaultRequire": 1
},
"overwrites": {
"requireAnnotations": true
}
}