From 84377484773fe3231b15ff395c2002e37535e192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Szab=C3=B3?= Date: Wed, 23 Oct 2024 15:29:55 +0200 Subject: [PATCH 1/2] Fix invis bug --- .../src/main/kotlin/com/willfp/ecopets/pets/PetDisplay.kt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/PetDisplay.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/PetDisplay.kt index 310d10a..10a785e 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/PetDisplay.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/PetDisplay.kt @@ -82,10 +82,6 @@ class PetDisplay( } private fun getOrNew(player: Player): ArmorStand? { - if (player.isInvisible) { - return null - } - val tracked = trackedEntities[player.uniqueId] val existing = tracked?.stand From 02265a3532657b191359ae7e071730182c6e5ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Szab=C3=B3?= Date: Wed, 23 Oct 2024 17:43:11 +0200 Subject: [PATCH 2/2] Add item display pet entities --- eco-core/core-plugin/build.gradle.kts | 2 +- .../kotlin/com/willfp/ecopets/pets/Pet.kt | 2 +- .../com/willfp/ecopets/pets/PetDisplay.kt | 42 ++++++++++--------- .../pets/entity/ItemDisplayPetEntity.kt | 30 +++++++++++++ .../pets/entity/ModelEnginePetEntity.kt | 4 +- .../willfp/ecopets/pets/entity/PetEntity.kt | 9 +++- .../ecopets/pets/entity/SkullPetEntity.kt | 4 +- .../core-plugin/src/main/resources/config.yml | 4 ++ 8 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/ItemDisplayPetEntity.kt diff --git a/eco-core/core-plugin/build.gradle.kts b/eco-core/core-plugin/build.gradle.kts index bb5e4d0..3967ec9 100644 --- a/eco-core/core-plugin/build.gradle.kts +++ b/eco-core/core-plugin/build.gradle.kts @@ -2,7 +2,7 @@ group = "com.willfp" version = rootProject.version dependencies { - compileOnly("io.papermc.paper:paper-api:1.19.3-R0.1-SNAPSHOT") + compileOnly("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT") compileOnly("com.github.ben-manes.caffeine:caffeine:3.0.2") implementation("com.willfp:ecomponent:1.3.0") diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/Pet.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/Pet.kt index db1d5b8..e6a2291 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/Pet.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/Pet.kt @@ -238,7 +238,7 @@ class Pet( } fun makePetEntity(): PetEntity { - return PetEntity.create(this) + return PetEntity.create(plugin, this) } fun getLevel(level: Int): PetLevel = levels.get(level) { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/PetDisplay.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/PetDisplay.kt index 10a785e..eb55ba3 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/PetDisplay.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/PetDisplay.kt @@ -6,13 +6,14 @@ import com.willfp.eco.util.formatEco import org.bukkit.Bukkit import org.bukkit.Location import org.bukkit.entity.ArmorStand +import org.bukkit.entity.Entity import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.player.PlayerChangedWorldEvent import org.bukkit.event.player.PlayerQuitEvent import org.bukkit.event.player.PlayerTeleportEvent -import java.util.UUID +import java.util.* import kotlin.math.PI import kotlin.math.abs @@ -21,7 +22,7 @@ class PetDisplay( ) : Listener { private var tick = 0 - private val trackedEntities = mutableMapOf() + private val trackedEntities = mutableMapOf() fun tickAll() { for (player in Bukkit.getOnlinePlayers()) { @@ -32,7 +33,7 @@ class PetDisplay( } private fun tickPlayer(player: Player) { - val stand = getOrNew(player) ?: return + val entity = getOrNew(player) ?: return val pet = player.activePet if (pet != null) { @@ -42,31 +43,31 @@ class PetDisplay( } @Suppress("DEPRECATION") - stand.customName = plugin.configYml.getString("pet-entity.name") + entity.customName = plugin.configYml.getString("pet-entity.name") .replace("%player%", player.displayName) .replace("%pet%", pet.name) .replace("%level%", player.getPetLevel(pet).toString()) .formatEco(player) - val location = getLocation(player) + val location = getLocation(player, if ((entity is ArmorStand)) 0.0 else 1.0) location.y += NumberUtils.fastSin(tick / (2 * PI) * 0.5) * 0.15 if (location.world != null) { - stand.teleport(location) + entity.teleport(location) } if (!pet.entityTexture.contains(":")) { - stand.setRotation((20 * tick / (2 * PI)).toFloat(), 0f) + entity.setRotation((20 * tick / (2 * PI)).toFloat(), 0f) } } } - private fun getLocation(player: Player): Location { + private fun getLocation(player: Player, yOffset: Double): Location { val offset = player.eyeLocation.direction.clone().normalize() .multiply(-1) .apply { - y = abs(y) + y = abs(y) + yOffset if (abs(x) < 0.5) { x = 0.5 @@ -81,13 +82,13 @@ class PetDisplay( return player.eyeLocation.clone().add(offset) } - private fun getOrNew(player: Player): ArmorStand? { + private fun getOrNew(player: Player): Entity? { val tracked = trackedEntities[player.uniqueId] - val existing = tracked?.stand + val existing = tracked?.entity val pet = player.activePet if (pet != tracked?.pet) { - tracked?.stand?.remove() + tracked?.entity?.remove() } if (existing == null || existing.isDead || pet == null) { @@ -98,25 +99,26 @@ class PetDisplay( return null } - val location = getLocation(player) - val stand = pet.makePetEntity().spawn(location) - trackedEntities[player.uniqueId] = PetArmorStand(stand, pet) + val location = getLocation(player, 0.0) + val entity = pet.makePetEntity().spawn(location) + + trackedEntities[player.uniqueId] = PetDisplayEntity(entity, pet) } - return trackedEntities[player.uniqueId]?.stand + return trackedEntities[player.uniqueId]?.entity } fun shutdown() { for (stand in trackedEntities.values) { - stand.stand.remove() + stand.entity.remove() } trackedEntities.clear() } private fun remove(player: Player) { - trackedEntities[player.uniqueId]?.stand?.remove() + trackedEntities[player.uniqueId]?.entity?.remove() trackedEntities.remove(player.uniqueId) } @@ -135,8 +137,8 @@ class PetDisplay( remove(event.player) } - private data class PetArmorStand( - val stand: ArmorStand, + private data class PetDisplayEntity( + val entity: Entity, val pet: Pet ) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/ItemDisplayPetEntity.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/ItemDisplayPetEntity.kt new file mode 100644 index 0000000..bd2c33b --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/ItemDisplayPetEntity.kt @@ -0,0 +1,30 @@ +package com.willfp.ecopets.pets.entity + +import com.willfp.eco.core.items.builder.SkullBuilder +import com.willfp.ecopets.EcoPetsPlugin +import com.willfp.ecopets.pets.Pet +import org.bukkit.Location +import org.bukkit.entity.Entity +import org.bukkit.entity.ItemDisplay +import org.bukkit.inventory.ItemStack + +class ItemDisplayPetEntity( + pet: Pet, + private val plugin: EcoPetsPlugin +) : PetEntity(pet) { + override fun spawn(location: Location): Entity { + val skull: ItemStack = SkullBuilder() + .setSkullTexture(pet.entityTexture) + .build() + + val itemDisplay = location.world!!.spawn(location, ItemDisplay::class.java) { + it.itemStack = skull + it.isCustomNameVisible = true + @Suppress("DEPRECATION") + it.customName = pet.name + it.teleportDuration = plugin.configYml.getInt("pet-entity.item-display.teleport-duration", 3) + } + + return itemDisplay; + } +} \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/ModelEnginePetEntity.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/ModelEnginePetEntity.kt index 38b540b..69ae990 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/ModelEnginePetEntity.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/ModelEnginePetEntity.kt @@ -4,14 +4,14 @@ import com.willfp.ecopets.EcoPetsPlugin import com.willfp.ecopets.pets.Pet import com.willfp.modelenginebridge.ModelEngineBridge import org.bukkit.Location -import org.bukkit.entity.ArmorStand +import org.bukkit.entity.Entity class ModelEnginePetEntity( pet: Pet, private val modelID: String, private val plugin: EcoPetsPlugin ) : PetEntity(pet) { - override fun spawn(location: Location): ArmorStand { + override fun spawn(location: Location): Entity { val stand = emptyArmorStandAt(location, pet) val model = ModelEngineBridge.instance.createActiveModel(modelID) ?: return stand diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/PetEntity.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/PetEntity.kt index 919da59..f865680 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/PetEntity.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/PetEntity.kt @@ -1,15 +1,17 @@ package com.willfp.ecopets.pets.entity +import com.willfp.ecopets.EcoPetsPlugin import com.willfp.ecopets.pets.Pet import org.bukkit.Location import org.bukkit.entity.ArmorStand +import org.bukkit.entity.Entity import org.bukkit.entity.EntityType import org.bukkit.inventory.EquipmentSlot abstract class PetEntity( val pet: Pet ) { - abstract fun spawn(location: Location): ArmorStand + abstract fun spawn(location: Location): Entity companion object { private val registrations = mutableMapOf PetEntity>() @@ -20,10 +22,13 @@ abstract class PetEntity( } @JvmStatic - fun create(pet: Pet): PetEntity { + fun create(plugin: EcoPetsPlugin, pet: Pet): PetEntity { val texture = pet.entityTexture if (!texture.contains(":")) { + if (plugin.configYml.getBool("pet-entity.item-display.enabled")) { + return ItemDisplayPetEntity(pet, plugin) + } return SkullPetEntity(pet) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/SkullPetEntity.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/SkullPetEntity.kt index 3d9dbab..a4023a6 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/SkullPetEntity.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecopets/pets/entity/SkullPetEntity.kt @@ -3,11 +3,11 @@ package com.willfp.ecopets.pets.entity import com.willfp.eco.core.items.builder.SkullBuilder import com.willfp.ecopets.pets.Pet import org.bukkit.Location -import org.bukkit.entity.ArmorStand +import org.bukkit.entity.Entity import org.bukkit.inventory.ItemStack class SkullPetEntity(pet: Pet) : PetEntity(pet) { - override fun spawn(location: Location): ArmorStand { + override fun spawn(location: Location): Entity { val stand = emptyArmorStandAt(location, pet) val skull: ItemStack = SkullBuilder() diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index 12a0aa4..0f03599 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -261,6 +261,10 @@ level-gui: pet-entity: enabled: true # If you disable this, there will be no floating pets name: "%player%&f's %pet%&f (Lvl. %level%)" + item-display: + enabled: false + teleport-duration: 3 + level-up: message: