Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🪆 NPCs #7

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/main/kotlin/cc/lixou/stracciatella/Stracciatella.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ package cc.lixou.stracciatella

import cc.lixou.stracciatella.config.Config
import cc.lixou.stracciatella.game.GameManager
import cc.lixou.stracciatella.inventory.extensions.styleRadialBackground
import cc.lixou.stracciatella.npc.EntityNPC
import net.kyori.adventure.text.Component
import net.minestom.server.MinecraftServer
import net.minestom.server.coordinate.Pos
import net.minestom.server.entity.EntityType
import net.minestom.server.entity.metadata.villager.VillagerMeta
import net.minestom.server.event.player.PlayerDisconnectEvent
import net.minestom.server.event.player.PlayerLoginEvent
import net.minestom.server.instance.block.Block
import net.minestom.server.inventory.Inventory
import net.minestom.server.inventory.InventoryType
import net.minestom.server.item.Material
import org.slf4j.LoggerFactory
import java.nio.file.Path

Expand All @@ -27,6 +35,20 @@ class Stracciatella {
// endregion


val npc = EntityNPC(EntityType.VILLAGER, Pos(2.0, 42.0, 5.0), instanceContainer).meta<VillagerMeta> {
it.customName = Component.text("KAKA")
it.isCustomNameVisible = true
val villagerData = it.villagerData
villagerData.type = VillagerMeta.Type.DESERT
villagerData.level = VillagerMeta.Level.MASTER
villagerData.profession = VillagerMeta.Profession.FARMER
it.villagerData = villagerData
}.interact {
val inventory = Inventory(InventoryType.CHEST_5_ROW, Component.text("Test"))
inventory.styleRadialBackground(Material.PINK_STAINED_GLASS_PANE, 5)
it.openInventory(inventory)
}.spawn()

val eventHandler = MinecraftServer.getGlobalEventHandler()
eventHandler.addListener(PlayerDisconnectEvent::class.java) { event ->
GameManager.unregisterPlayer(event.player)
Expand Down
53 changes: 53 additions & 0 deletions src/main/kotlin/cc/lixou/stracciatella/npc/EntityNPC.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cc.lixou.stracciatella.npc

import net.minestom.server.coordinate.Pos
import net.minestom.server.entity.Entity
import net.minestom.server.entity.EntityType
import net.minestom.server.entity.Player
import net.minestom.server.entity.metadata.EntityMeta
import net.minestom.server.event.player.PlayerEntityInteractEvent
import net.minestom.server.instance.Instance
import world.cepi.kstom.Manager
import world.cepi.kstom.event.listenOnly

class EntityNPC(
type: EntityType,
private val pos: Pos,
private val instance: Instance
) {

companion object {
private val interactions = mutableMapOf<Int, (Player) -> Unit>()

init {
Manager.globalEvent.listenOnly<PlayerEntityInteractEvent> { interactions[target.entityId]?.invoke(player) }
}
}

val entity: Entity = Entity(type)

inline fun <reified T : EntityMeta> meta(callback: (T) -> Unit): EntityNPC {
val meta = entity.entityMeta as T
meta.setNotifyAboutChanges(false)
callback.invoke(meta)
meta.setNotifyAboutChanges(true)

return this
}

fun spawn(): EntityNPC {
entity.setInstance(instance, pos)
return this
}

fun remove(): EntityNPC {
entity.remove()
return this
}

fun interact(interaction: (Player) -> Unit): EntityNPC {
interactions[entity.entityId] = interaction
return this
}

}
18 changes: 18 additions & 0 deletions src/main/kotlin/cc/lixou/stracciatella/npc/PlayerNPC.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cc.lixou.stracciatella.npc

import net.minestom.server.entity.fakeplayer.FakePlayer
import net.minestom.server.entity.fakeplayer.FakePlayerOption
import java.util.*

class PlayerNPC(
username: String,
options: FakePlayerOption,
uuid: UUID = UUID.randomUUID(),
spawnCallback: (FakePlayer) -> Unit
) {

init {
FakePlayer.initPlayer(uuid, username, options, spawnCallback)
}

}