Skip to content

Commit

Permalink
Added NPC
Browse files Browse the repository at this point in the history
Updated minestom
  • Loading branch information
Minemobs committed Jun 7, 2024
1 parent a2d1167 commit 8592903
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
27 changes: 25 additions & 2 deletions src/main/kotlin/fr/sunderia/bomberman/Bomberman.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() } )
Expand Down
7 changes: 2 additions & 5 deletions src/main/kotlin/fr/sunderia/bomberman/InstanceCreator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@ 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

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
}

Expand Down
16 changes: 11 additions & 5 deletions src/main/kotlin/fr/sunderia/bomberman/party/GameCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
})
}
}
38 changes: 38 additions & 0 deletions src/main/kotlin/fr/sunderia/bomberman/utils/NPC.kt
Original file line number Diff line number Diff line change
@@ -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<PlayerInfoUpdatePacket.Property>()
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<Int, Metadata.Entry<*>>
))
}

override fun updateOldViewer(player: Player) {
super.updateOldViewer(player)
player.sendPackets(PlayerInfoRemovePacket(uuid))
}

fun onInteract(event: PlayerEntityInteractEvent) = this.interactCallback(this, event)
}

0 comments on commit 8592903

Please sign in to comment.