From 85929036baa2b0875b56fb7b7f5cdbc47c099508 Mon Sep 17 00:00:00 2001 From: minemobs Date: Fri, 7 Jun 2024 20:38:35 +0200 Subject: [PATCH] Added NPC Updated minestom --- build.gradle.kts | 2 +- .../kotlin/fr/sunderia/bomberman/Bomberman.kt | 27 ++++++++++++- .../fr/sunderia/bomberman/InstanceCreator.kt | 7 +--- .../sunderia/bomberman/party/GameCommand.kt | 16 +++++--- .../kotlin/fr/sunderia/bomberman/utils/NPC.kt | 38 +++++++++++++++++++ 5 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 src/main/kotlin/fr/sunderia/bomberman/utils/NPC.kt diff --git a/build.gradle.kts b/build.gradle.kts index 6497447..a2d71e0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,7 +14,7 @@ repositories { } dependencies { - implementation("net.minestom:minestom-snapshots:63f02929ed") + implementation("net.minestom:minestom-snapshots:19bb74e942") implementation("commons-codec:commons-codec:1.16.1") } diff --git a/src/main/kotlin/fr/sunderia/bomberman/Bomberman.kt b/src/main/kotlin/fr/sunderia/bomberman/Bomberman.kt index 70a94e1..dc5a7c7 100644 --- a/src/main/kotlin/fr/sunderia/bomberman/Bomberman.kt +++ b/src/main/kotlin/fr/sunderia/bomberman/Bomberman.kt @@ -9,6 +9,7 @@ import fr.sunderia.bomberman.party.Party import fr.sunderia.bomberman.party.PartyCommand import fr.sunderia.bomberman.utils.Axis import fr.sunderia.bomberman.utils.Cooldown +import fr.sunderia.bomberman.utils.NPC import fr.sunderia.bomberman.utils.PositionUtils.Companion.removeBlockAt import fr.sunderia.bomberman.utils.PositionUtils.Companion.setBlockAt import fr.sunderia.bomberman.utils.PowerupTags @@ -17,6 +18,8 @@ import net.kyori.adventure.resource.ResourcePackInfo import net.kyori.adventure.resource.ResourcePackRequest import net.kyori.adventure.text.Component import net.kyori.adventure.text.JoinConfiguration +import net.kyori.adventure.text.event.ClickEvent +import net.kyori.adventure.text.event.HoverEvent import net.kyori.adventure.text.format.NamedTextColor import net.kyori.adventure.title.TitlePart import net.minestom.server.MinecraftServer @@ -77,11 +80,18 @@ class Bomberman { val fullBright: DimensionType = DimensionType.builder(NamespaceID.from("sunderia:full_bright")).ambientLight(2.0f).build() } + private fun createNPC(lobby: Instance) { + val npc = NPC(UUID.randomUUID(), "§aPlay Game", interactCallback = { _, event -> GameCommand.startGame(event.player) }) + npc.setInstance(lobby, Pos(0.0, 45.0, 10.0)) + npc.setView(180.0F, .0F) + } + fun initialize() { val manager = MinecraftServer.getInstanceManager() MinecraftServer.getDimensionTypeManager().addDimension(fullBright) val lobbyContainer: InstanceContainer = createInstanceContainer(manager) lobbyInstance = lobbyContainer + createNPC(lobbyContainer) OpenToLAN.open() //MojangAuth.init() registerListeners(lobbyContainer) @@ -130,13 +140,20 @@ class Bomberman { Game.playerLeft(instance) } } + + lobbyNode.addListener(PlayerEntityInteractEvent::class.java) { + if(it.instance.hasTag(Tag.Boolean("game"))) return@addListener + if(it.target !is NPC) return@addListener + if(it.hand != Player.Hand.MAIN) return@addListener + (it.target as NPC).onInteract(it) + } MinecraftServer.getPacketListenerManager().setPlayListener(ClientPlayerDiggingPacket::class.java) { packet: ClientPlayerDiggingPacket, player: Player -> val instance = player.instance if(player.gameMode != GameMode.ADVENTURE || packet.status != ClientPlayerDiggingPacket.Status.STARTED_DIGGING) return@setPlayListener PlayerDiggingListener.playerDiggingListener(packet, player) - logger.info("Digging") + //logger.info("Digging") if(!player.hasTag(PowerupTags.BOXING_GLOVE.getBool())) return@setPlayListener - logger.info("Has effect: ") + //logger.info("Has effect: ") if(!instance.getBlock(packet.blockPosition).compare(Block.BARRIER)) return@setPlayListener if(packet.blockFace == BlockFace.TOP || packet.blockFace == BlockFace.BOTTOM) return@setPlayListener val tnt = instance.entities @@ -184,6 +201,12 @@ class Bomberman { buffer.write(NetworkBuffer.FLOAT, 1f) val packet = ChangeGameStatePacket(buffer) PacketUtils.sendPacket(player, packet) + it.player.sendMessage( + Component.text("Join a game with ") + .append(Component.text("/game", NamedTextColor.GREEN) + .hoverEvent(HoverEvent.showText(Component.text("Will execute the command /game"))) + .clickEvent(ClickEvent.runCommand("/game"))) + ) } if(!it.spawnInstance.hasTag(Tag.Boolean("game"))) return@addListener player.inventory.addItemStack(ItemStack.of(Material.TNT).withMeta { it.canPlaceOn(Block.STONE, Block.BRICKS).canDestroy(Block.BARRIER).build() } ) diff --git a/src/main/kotlin/fr/sunderia/bomberman/InstanceCreator.kt b/src/main/kotlin/fr/sunderia/bomberman/InstanceCreator.kt index c9cbc90..5f8f526 100644 --- a/src/main/kotlin/fr/sunderia/bomberman/InstanceCreator.kt +++ b/src/main/kotlin/fr/sunderia/bomberman/InstanceCreator.kt @@ -11,12 +11,11 @@ import net.minestom.server.instance.InstanceContainer import net.minestom.server.instance.InstanceManager import net.minestom.server.instance.batch.AbsoluteBlockBatch import net.minestom.server.instance.block.Block -import net.minestom.server.instance.generator.GenerationUnit import net.minestom.server.utils.chunk.ChunkUtils import org.jglrxavpok.hephaistos.nbt.NBTException import org.jglrxavpok.hephaistos.nbt.NBTReader import java.io.IOException -import java.util.LinkedList +import java.util.* import java.util.stream.IntStream import kotlin.random.Random @@ -24,9 +23,7 @@ class InstanceCreator { companion object { fun createInstanceContainer(manager: InstanceManager): InstanceContainer { val container = manager.createInstanceContainer(Bomberman.fullBright) - container.setGenerator { unit: GenerationUnit -> - unit.modifier().fillHeight(0, 40, Block.STONE) - } + container.setGenerator { unit -> unit.modifier().fillHeight(0, 40, Block.STONE) } return container } diff --git a/src/main/kotlin/fr/sunderia/bomberman/party/GameCommand.kt b/src/main/kotlin/fr/sunderia/bomberman/party/GameCommand.kt index c64be11..838dc41 100644 --- a/src/main/kotlin/fr/sunderia/bomberman/party/GameCommand.kt +++ b/src/main/kotlin/fr/sunderia/bomberman/party/GameCommand.kt @@ -4,17 +4,16 @@ import net.kyori.adventure.text.Component.text import net.minestom.server.MinecraftServer import net.minestom.server.command.builder.Command import net.minestom.server.entity.Player +import net.minestom.server.instance.InstanceManager class GameCommand: Command("game") { - init { - val manager = MinecraftServer.getInstanceManager() - addSyntax({ sender, _ -> - if(sender !is Player) return@addSyntax + companion object { + fun startGame(sender: Player, manager: InstanceManager = MinecraftServer.getInstanceManager()) { val party = Party.getParty(sender) if(party != null && party.playerList.size != 0) { party.warp(Game.createGame(manager)) //Funny text to send - return@addSyntax + return } var game = Game.getNonFilledGames() @@ -27,6 +26,13 @@ class GameCommand: Command("game") { sender.instance = game.instance sender.sendMessage(text("Successfully sent you to a Bomberman game")) } + } + } + + init { + addSyntax({ sender, _ -> + if(sender !is Player) return@addSyntax + startGame(sender) }) } } \ No newline at end of file diff --git a/src/main/kotlin/fr/sunderia/bomberman/utils/NPC.kt b/src/main/kotlin/fr/sunderia/bomberman/utils/NPC.kt new file mode 100644 index 0000000..d43426d --- /dev/null +++ b/src/main/kotlin/fr/sunderia/bomberman/utils/NPC.kt @@ -0,0 +1,38 @@ +package fr.sunderia.bomberman.utils + +import net.minestom.server.entity.* +import net.minestom.server.event.player.PlayerEntityInteractEvent +import net.minestom.server.network.packet.server.play.EntityMetaDataPacket +import net.minestom.server.network.packet.server.play.PlayerInfoRemovePacket +import net.minestom.server.network.packet.server.play.PlayerInfoUpdatePacket +import java.util.UUID + +class NPC(uuid: UUID, private val name: String, private val skin: PlayerSkin = BOMBERMAN_SKIN, private val interactCallback: ((NPC, PlayerEntityInteractEvent) -> Unit)): EntityCreature(EntityType.PLAYER, uuid) { + + companion object { + val BOMBERMAN_SKIN = PlayerSkin( + "ewogICJ0aW1lc3RhbXAiIDogMTcxNzc4MzI0NTI4MywKICAicHJvZmlsZUlkIiA6ICI1OWZmOTU1YzMxYjk0MWI0YWQwNDg4NDk0ODkzNzUzOCIsCiAgInByb2ZpbGVOYW1lIiA6ICJNZWdhMzQ5IiwKICAic2lnbmF0dXJlUmVxdWlyZWQiIDogdHJ1ZSwKICAidGV4dHVyZXMiIDogewogICAgIlNLSU4iIDogewogICAgICAidXJsIiA6ICJodHRwOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlLzdlZjUwZjM5MGRjY2Y0NzBhNmM2MzNjM2UwM2NkNDMzNDE4NWVlZjc4YmE5NmM3ZmJjNmUwNmE4NjY2NmY3MmMiCiAgICB9CiAgfQp9", + "tFQkm3OXAYXi6EIjcW1liqTUUImzv+0I+sIqJR5Wb36r0pOh/LJVYV1bg28p1veCNZS+QbyumUfY6wzxz+kTAOl2ohfMDtuBlaK0JI3+TgX2OwOUOJv1z+/u+hkmq9yyO1U/bpHxrxSE3M4+SBExhRw3crfSe8bnaHDTUiMWm6nQH4u81H4sDnq1l1eraBIUUIymIxK/WwBf6fF2ylut6IPBU/SoFqvQeJN6JYcamD9aOrP+WBbkPV4A+ROuAGKfamZeFvZOoEDVx2QInfz0E3zBqqnLcM5Zqde6ce3raKzFxR1eQ24NX9ixGNkXa5/2jPq8GGrp02Tz+2X2+oxnani+CD9t8/U9V2zgzUIoXT2xTl0XVbA860cbwuhe/h7yYLA/Wsem4Q7adQXmr4tYSfS1hckF0LjXLCq1YCeTxoaIXQPuBwgB6+wHZXXBgW5+l/g5HBtCtK+qxAQA1kb1ntJr3nXwWnMq6d4AxWykZAI5VMUX64TGMyKpcO15HG6yRgj2L+WAMqpVvM/qLBvcOmt1HcHfHTrP3fkjfdfSZdBLO/DO+R2dZzpuzaUbkZSk5SZ6ZTRtp4bZ1OvoV8kzPCyBSvnEllauqGjC2VPkPFNizKrkPVi21gUYTPdz6IcRFnUU3IC+aviLPevq53pXYNeRmy1MNN7UK21eQD7I8p8=" + ) + } + + override fun updateNewViewer(player: Player) { + val properties = mutableListOf() + if(skin.textures() != null && skin.signature() != null) { + properties.add(PlayerInfoUpdatePacket.Property("textures", skin.textures(), skin.signature())) + } + val entry = PlayerInfoUpdatePacket.Entry(uuid, name, properties, false, 0, GameMode.CREATIVE, null, null) + player.sendPacket(PlayerInfoUpdatePacket(PlayerInfoUpdatePacket.Action.ADD_PLAYER, entry)) + super.updateNewViewer(player) + player.sendPackets(EntityMetaDataPacket(entityId, + java.util.Map.of(17, Metadata.Byte((127).toByte())) as Map> + )) + } + + override fun updateOldViewer(player: Player) { + super.updateOldViewer(player) + player.sendPackets(PlayerInfoRemovePacket(uuid)) + } + + fun onInteract(event: PlayerEntityInteractEvent) = this.interactCallback(this, event) +} \ No newline at end of file