package fr.azur.tcoww.utils import fr.azur.tcoww.game.Game import fr.azur.tcoww.roles.Role.Companion.werewolfRole import fr.azur.tcoww.utils.skins.Manager import io.papermc.paper.datacomponent.item.ResolvableProfile import net.kyori.adventure.text.Component import net.kyori.adventure.text.format.NamedTextColor import net.kyori.adventure.title.Title import org.bukkit.Bukkit import org.bukkit.GameMode import org.bukkit.NamespacedKey import org.bukkit.attribute.Attribute import org.bukkit.attribute.AttributeModifier import org.bukkit.entity.Mannequin import org.bukkit.entity.Player import org.bukkit.entity.Pose import org.bukkit.persistence.PersistentDataType import java.time.Duration object Players { private val transformKey = NamespacedKey("tcoww", "werewolf_transform") val corpseKey = NamespacedKey("tcoww", "corpse") val corpseRoleKey = NamespacedKey("tcoww", "corpse_role") private val noCollisionTeam = Bukkit .getServer() .scoreboardManager.mainScoreboard .getTeam("NoCollision") fun Player.tfWerewolf() { if (isTransformed()) return Manager.applyWerewolfFur(this) persistentDataContainer.set( transformKey, PersistentDataType.BOOLEAN, true, ) val speedAtr = getAttribute(Attribute.MOVEMENT_SPEED)!! val scaleAtr = getAttribute(Attribute.SCALE)!! speedAtr.addModifier( AttributeModifier( transformKey, 0.02, AttributeModifier.Operation.ADD_NUMBER, ), ) scaleAtr.addModifier( AttributeModifier( transformKey, 0.1, AttributeModifier.Operation.ADD_NUMBER, ), ) } fun Player.tfHuman() { if (!isTransformed()) return Manager.unapplySkin(this) persistentDataContainer.set( transformKey, PersistentDataType.BOOLEAN, false, ) val speedAtr = getAttribute(Attribute.MOVEMENT_SPEED)!! val scaleAtr = getAttribute(Attribute.SCALE)!! speedAtr.removeModifier(transformKey) scaleAtr.removeModifier(transformKey) } fun Player.isTransformed(): Boolean = persistentDataContainer.getOrDefault( transformKey, PersistentDataType.BOOLEAN, false, ) fun Player.kill() { if (isTransformed()) tfHuman() val current = Game.current ?: return val corpse = createCorpse(this) corpse.spawnAt(this.location) this.gameMode = GameMode.SPECTATOR this.isInvulnerable = false current.remainingPlayer.remove(this) this.showTitle( Title.title( Component.text("Vous ĂȘtes mort.", NamedTextColor.DARK_RED), Component.empty(), Title.Times.times(Duration.ZERO, Duration.ofSeconds(2), Duration.ofMillis(500)), ), ) if (current.checkGameEnd()) current.stopGame() } fun createCorpse(player: Player): Mannequin = player.world.createEntity(player.location, Mannequin::class.java).apply { profile = ResolvableProfile.resolvableProfile(player.playerProfile) noCollisionTeam?.addEntity(this) isInvulnerable = true pose = Pose.SLEEPING persistentDataContainer.set(corpseKey, PersistentDataType.BOOLEAN, true) val role = player.werewolfRole persistentDataContainer.set(corpseRoleKey, PersistentDataType.STRING, role.id.toString()) } }