From 88dcf52a57bbf5387134c0064dfc916878ed2c63 Mon Sep 17 00:00:00 2001 From: xtex Date: Mon, 3 Jul 2023 10:22:18 +0800 Subject: [PATCH] feat: lost items --- .../quaedam/projection/swarm/ai/LostItem.kt | 27 +++++++++++++++++++ .../projection/swarm/ai/ProjectedPersonAI.kt | 1 + 2 files changed, 28 insertions(+) create mode 100644 common/src/main/kotlin/quaedam/projection/swarm/ai/LostItem.kt diff --git a/common/src/main/kotlin/quaedam/projection/swarm/ai/LostItem.kt b/common/src/main/kotlin/quaedam/projection/swarm/ai/LostItem.kt new file mode 100644 index 0000000..a15f421 --- /dev/null +++ b/common/src/main/kotlin/quaedam/projection/swarm/ai/LostItem.kt @@ -0,0 +1,27 @@ +package quaedam.projection.swarm.ai + +import net.minecraft.world.entity.LivingEntity +import net.minecraft.world.entity.ai.behavior.OneShot +import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder +import net.minecraft.world.entity.ai.behavior.declarative.Trigger +import net.minecraft.world.entity.npc.InventoryCarrier +import net.minecraft.world.entity.schedule.Activity +import net.minecraft.world.level.block.Block + +@Suppress("FunctionName") +fun LostItem(chance: Int): OneShot + where E : LivingEntity, E : InventoryCarrier = BehaviorBuilder.create { instance -> + instance.point(Trigger { level, entity: E, l: Long -> + if (entity.brain.isActive(Activity.REST)) return@Trigger false + if (level.random.nextInt(chance) != 0) return@Trigger false + val inventory = entity.inventory + val item = inventory.getItem(level.random.nextInt(inventory.containerSize)) + if (!item.isEmpty) { + val count = level.random.nextInt(item.count) + item.shrink(count) + inventory.setChanged() + Block.popResource(level, entity.blockPosition(), item.copyWithCount(count)) + } + return@Trigger true + }) +} \ No newline at end of file diff --git a/common/src/main/kotlin/quaedam/projection/swarm/ai/ProjectedPersonAI.kt b/common/src/main/kotlin/quaedam/projection/swarm/ai/ProjectedPersonAI.kt index 52af9c5..759c6a5 100644 --- a/common/src/main/kotlin/quaedam/projection/swarm/ai/ProjectedPersonAI.kt +++ b/common/src/main/kotlin/quaedam/projection/swarm/ai/ProjectedPersonAI.kt @@ -101,6 +101,7 @@ object ProjectedPersonAI { 3 weight LookAtTargetSink(40, 70), 3 weight MoveToTargetSink(), 3 weight InteractWithDoor.create(), + 3 weight LostItem(400), 10 weight GoToWantedItem.create(1.2f, false, 7), ) )