Add allowLeads gameplay feature to allow leads on all mobs.

This commit is contained in:
Logan Gorence
2022-01-16 14:04:04 -08:00
parent 25c72d1ce3
commit 4ca241aa5b
3 changed files with 44 additions and 0 deletions

View File

@ -11,4 +11,5 @@ data class GameplayConfig(
data class MobsConfig(
val disableEndermanGriefing: Boolean,
val disableFreezeDamage: Boolean,
val allowLeads: Boolean,
)

View File

@ -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
}
}
}

View File

@ -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