diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/gameplay/GameplayConfig.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/gameplay/GameplayConfig.kt index c77b3e3..9a96607 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/gameplay/GameplayConfig.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/gameplay/GameplayConfig.kt @@ -11,4 +11,5 @@ data class GameplayConfig( data class MobsConfig( val disableEndermanGriefing: Boolean, val disableFreezeDamage: Boolean, + val allowLeads: Boolean, ) diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/gameplay/GameplayFeature.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/gameplay/GameplayFeature.kt index 58fe1e7..ec35df1 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/gameplay/GameplayFeature.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/gameplay/GameplayFeature.kt @@ -4,12 +4,17 @@ import cloud.kubelet.foundation.core.FoundationCorePlugin import cloud.kubelet.foundation.core.Util import cloud.kubelet.foundation.core.abstraction.Feature import com.charleskorn.kaml.Yaml +import org.bukkit.Bukkit +import org.bukkit.Material import org.bukkit.entity.EntityType +import org.bukkit.entity.LivingEntity import org.bukkit.entity.Mob import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.entity.EntityChangeBlockEvent import org.bukkit.event.entity.EntityDamageEvent +import org.bukkit.event.player.PlayerInteractEntityEvent +import org.bukkit.inventory.ItemStack import org.koin.core.component.inject import org.koin.dsl.module import kotlin.io.path.inputStream @@ -50,4 +55,37 @@ class GameplayFeature : Feature() { } } } + + @EventHandler(priority = EventPriority.HIGHEST) + private fun onPlayerInteractEntity(event: PlayerInteractEntityEvent) { + val mainHandItem = event.player.inventory.itemInMainHand + val hasLead = mainHandItem.type == Material.LEAD + val isLivingEntity = event.rightClicked is LivingEntity + + // If leads are allowed on all mobs, then start leading the mob. + if (config.mobs.allowLeads && hasLead && isLivingEntity) { + val livingEntity = event.rightClicked as LivingEntity + + // Something to do with Bukkit, leashes must happen after the event. + Bukkit.getScheduler().runTask(plugin) { -> + // If the entity is already leashed, don't do anything. + if (livingEntity.isLeashed + ) return@runTask + + val leashSuccess = livingEntity.setLeashHolder(event.player) + + if (leashSuccess) { + val newStack = if (mainHandItem.amount == 1) { + null + } else { + ItemStack(mainHandItem.type, mainHandItem.amount - 1) + } + event.player.inventory.setItemInMainHand(newStack) + } + } + + event.isCancelled = true + return + } + } } diff --git a/foundation-core/src/main/resources/gameplay.yaml b/foundation-core/src/main/resources/gameplay.yaml index 269b8d8..cc1da09 100644 --- a/foundation-core/src/main/resources/gameplay.yaml +++ b/foundation-core/src/main/resources/gameplay.yaml @@ -12,3 +12,8 @@ mobs: # When set to true, mobs will not take damage from freezing. # When set to false, mobs take damage from freezing. disableFreezeDamage: false + + # Allow leads on all mobs. + # When set to true, all mobs can have leads attached to them. + # When set to false, only specific mobs can have leads attached to them. + allowLeads: false