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

Item display as pet entites #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion eco-core/core-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -21,7 +22,7 @@ class PetDisplay(
) : Listener {
private var tick = 0

private val trackedEntities = mutableMapOf<UUID, PetArmorStand>()
private val trackedEntities = mutableMapOf<UUID, PetDisplayEntity>()

fun tickAll() {
for (player in Bukkit.getOnlinePlayers()) {
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -81,17 +82,13 @@ class PetDisplay(
return player.eyeLocation.clone().add(offset)
}

private fun getOrNew(player: Player): ArmorStand? {
if (player.isInvisible) {
return null
}

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) {
Expand All @@ -102,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)
}

Expand All @@ -139,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
)
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, (Pet, String) -> PetEntity>()
Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions eco-core/core-plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down