From b6dc6d75bae3eb03358870e324be6392d2dffb59 Mon Sep 17 00:00:00 2001 From: Cierra_Runis <2864283875@qq.com> Date: Tue, 5 Nov 2024 09:52:06 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=8DAdd=20New=20Feature=20`Pickup`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ .../pers/cierra_runis/survired/SurviRed.kt | 15 ++++---- .../pickup/PickuoRightClickEventListener.kt | 20 ----------- .../pickup/SurviRedPickupEventListener.kt | 36 +++++++++++++++++++ 4 files changed, 45 insertions(+), 28 deletions(-) delete mode 100644 src/main/kotlin/pers/cierra_runis/survired/pickup/PickuoRightClickEventListener.kt create mode 100644 src/main/kotlin/pers/cierra_runis/survired/pickup/SurviRedPickupEventListener.kt diff --git a/README.md b/README.md index 6ab1905..606e3dc 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,5 @@ SurviRed [Figma](https://www.figma.com/design/B4xgnDzzLQ11tZtaSKujFo/SurviRed?m=auto&t=UQ0XIsjsgI5N8PWV-1) + +[Villager Pickup](https://github.com/Living-Lemming/Villager-Pickup-Mod) diff --git a/src/main/kotlin/pers/cierra_runis/survired/SurviRed.kt b/src/main/kotlin/pers/cierra_runis/survired/SurviRed.kt index 8936519..102a99d 100644 --- a/src/main/kotlin/pers/cierra_runis/survired/SurviRed.kt +++ b/src/main/kotlin/pers/cierra_runis/survired/SurviRed.kt @@ -2,14 +2,13 @@ package pers.cierra_runis.survired import net.fabricmc.api.ModInitializer import org.slf4j.LoggerFactory +import pers.cierra_runis.survired.pickup.SurviRedPickupEventListener object SurviRed : ModInitializer { - private val logger = LoggerFactory.getLogger("survired") - - override fun onInitialize() { - // This code runs as soon as Minecraft is in a mod-load-ready state. - // However, some things (like resources) may still be uninitialized. - // Proceed with mild caution. - logger.info("Hello Fabric world!") - } + private val logger = LoggerFactory.getLogger(this.javaClass) + override fun onInitialize() { + logger.info("Loading...") + SurviRedPickupEventListener.registerRightClickEvent() + logger.info("Loaded!") + } } \ No newline at end of file diff --git a/src/main/kotlin/pers/cierra_runis/survired/pickup/PickuoRightClickEventListener.kt b/src/main/kotlin/pers/cierra_runis/survired/pickup/PickuoRightClickEventListener.kt deleted file mode 100644 index 5cad4c8..0000000 --- a/src/main/kotlin/pers/cierra_runis/survired/pickup/PickuoRightClickEventListener.kt +++ /dev/null @@ -1,20 +0,0 @@ -package pers.cierra_runis.survired.pickup - -import net.fabricmc.fabric.api.event.player.UseEntityCallback -import net.minecraft.item.SpawnEggItem -import net.minecraft.util.ActionResult - -class PickupRightClickEventListener { - fun registerRightClickEvent() { - UseEntityCallback.EVENT.register(UseEntityCallback { playerEntity, world, hand, entity, entityHitResult -> - if (playerEntity.isSneaking) { - entity.toString() - val spawnEgg = SpawnEggItem.forEntity(entity.type) - spawnEgg - playerEntity.giveItemStack(spawnEgg) - return@isSneaking ActionResult.SUCCESS - } - return@UseEntityCallback ActionResult.PASS - }) - } -} \ No newline at end of file diff --git a/src/main/kotlin/pers/cierra_runis/survired/pickup/SurviRedPickupEventListener.kt b/src/main/kotlin/pers/cierra_runis/survired/pickup/SurviRedPickupEventListener.kt new file mode 100644 index 0000000..851b966 --- /dev/null +++ b/src/main/kotlin/pers/cierra_runis/survired/pickup/SurviRedPickupEventListener.kt @@ -0,0 +1,36 @@ +package pers.cierra_runis.survired.pickup + +import net.fabricmc.fabric.api.event.player.UseEntityCallback +import net.minecraft.entity.Entity +import net.minecraft.item.ItemStack +import net.minecraft.item.SpawnEggItem +import net.minecraft.nbt.NbtCompound +import net.minecraft.util.ActionResult +import org.slf4j.LoggerFactory + +object SurviRedPickupEventListener { + private val logger = LoggerFactory.getLogger(this.javaClass) + + fun registerRightClickEvent() { + UseEntityCallback.EVENT.register(UseEntityCallback { playerEntity, world, hand, entity, entityHitResult -> + if (!playerEntity.isSneaking) return@UseEntityCallback ActionResult.PASS + + val spawnEgg = SpawnEggItem.forEntity(entity.type) ?: return@UseEntityCallback ActionResult.FAIL + + val entityNbt = entity.writeNbt(NbtCompound()) + for (key in arrayOf("Pos", "Motion", "Rotation", "UUID")) entityNbt.remove(key) + + val stack = ItemStack(spawnEgg).apply { + NbtCompound().apply { + put("EntityTag", entityNbt) + } + } + + if (playerEntity.inventory.emptySlot != -1) playerEntity.giveItemStack(stack) + else playerEntity.dropItem(stack, true) + + entity.remove(Entity.RemovalReason.DISCARDED) + return@UseEntityCallback ActionResult.SUCCESS + }) + } +} \ No newline at end of file