Skip to content

Commit

Permalink
fix: spawn egg entity types access logic
Browse files Browse the repository at this point in the history
fix #21
  • Loading branch information
iGabyTM committed Mar 17, 2024
1 parent 04bee04 commit 0e5aa89
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
31 changes: 23 additions & 8 deletions src/main/kotlin/me/gabytm/minecraft/guihelper/functions/Items.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ private val potions = if (ServerVersion.IS_ANCIENT) {
private val fireworkRocket = if (ServerVersion.IS_LEGACY) Material.valueOf("FIREWORK") else Material.FIREWORK_ROCKET
private val fireworkStar = if (ServerVersion.IS_LEGACY) Material.valueOf("FIREWORK_CHARGE") else Material.FIREWORK_STAR

private val entityTypeByMaterial = if (ServerVersion.IS_LEGACY) {
emptyMap<Material, EntityType>()
} else {
Material.values()
.filter { material -> material.name.endsWith("_SPAWN_EGG") }
.associateWith { material ->
@Suppress("DEPRECATION")
EntityType.fromName(material.name.replace("_SPAWN_EGG", ""))
}
}

/**
* Gets a copy of the item the player is currently holding using the right method for each version
* @since 2.0.0
Expand Down Expand Up @@ -162,15 +173,19 @@ val ItemStack.meta: ItemMeta?
@Suppress("DEPRECATION")
val ItemStack.spawnEggType: EntityType
get() {
return if (ServerVersion.HAS_SPAWN_EGG_META) {
if (!hasItemMeta()) {
return EntityType.UNKNOWN
}
return when {
!ServerVersion.IS_LEGACY -> entityTypeByMaterial[this.type]

(itemMeta as SpawnEggMeta).spawnedType ?: EntityType.UNKNOWN
} else {
(data as SpawnEgg).spawnedType
}
ServerVersion.HAS_SPAWN_EGG_META -> {
if (!hasItemMeta()) {
return EntityType.UNKNOWN
}

(this.itemMeta as SpawnEggMeta).spawnedType
}

else -> (this.data as SpawnEgg).spawnedType
} ?: EntityType.UNKNOWN
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class DeluxeMenusGenerator(
item.isFireworkStar -> {
handFireworkStars(section, meta as FireworkEffectMeta)
}
item.isSpawnEgg -> {
settings[Setting.SETTINGS__SET_ENTITY_TYPE_FOR_SPAWN_EGGS] && item.isSpawnEgg -> {
section["entity_type"] = item.spawnEggType.name
}
}
Expand Down Expand Up @@ -254,6 +254,10 @@ class DeluxeMenusGenerator(
@Path("settings.saveItemFlagsAsList")
val SETTINGS__SET_ITEM_FLAGS_AS_LIST = create(false)

@Comment("Starting with version 1.14.0, the option 'entity_type' is no longer required because each spawn egg has a different material")
@Path("settings.setEntityTypeForSpawnEggs")
val SETTINGS__SET_ENTITY_TYPE_FOR_SPAWN_EGGS = create(false)

}

}

0 comments on commit 0e5aa89

Please sign in to comment.