Merge pull request #14 from rudde0/master

PreFarmerMinionHarvestEvent + Pitcher / Torchflower crop support
This commit is contained in:
TomTom 2024-05-08 08:56:45 +02:00 committed by GitHub
commit 434b27835f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 2 deletions

View File

@ -0,0 +1,30 @@
package com.artillexstudios.axminions.api.events
import com.artillexstudios.axminions.api.minions.Minion
import org.bukkit.block.Block
import org.bukkit.event.Cancellable
import org.bukkit.event.HandlerList
class PreFarmerMinionHarvestEvent(minion: Minion, val block: Block) : MinionEvent(minion), Cancellable {
companion object {
private val handlerList = HandlerList()
@JvmStatic
fun getHandlerList(): HandlerList = handlerList
}
private var isCancelled = false
override fun getHandlers(): HandlerList {
return handlerList
}
override fun isCancelled(): Boolean {
return isCancelled
}
override fun setCancelled(cancelled: Boolean) {
isCancelled = cancelled
}
}

View File

@ -1,6 +1,7 @@
package com.artillexstudios.axminions.minions.miniontype package com.artillexstudios.axminions.minions.miniontype
import com.artillexstudios.axminions.AxMinionsPlugin import com.artillexstudios.axminions.AxMinionsPlugin
import com.artillexstudios.axminions.api.events.PreFarmerMinionHarvestEvent
import com.artillexstudios.axminions.api.minions.Minion import com.artillexstudios.axminions.api.minions.Minion
import com.artillexstudios.axminions.api.minions.miniontype.MinionType import com.artillexstudios.axminions.api.minions.miniontype.MinionType
import com.artillexstudios.axminions.api.utils.LocationUtils import com.artillexstudios.axminions.api.utils.LocationUtils
@ -9,6 +10,7 @@ import com.artillexstudios.axminions.api.utils.fastFor
import com.artillexstudios.axminions.api.warnings.Warnings import com.artillexstudios.axminions.api.warnings.Warnings
import com.artillexstudios.axminions.minions.MinionTicker import com.artillexstudios.axminions.minions.MinionTicker
import dev.lone.itemsadder.api.CustomBlock import dev.lone.itemsadder.api.CustomBlock
import org.bukkit.Bukkit
import kotlin.math.roundToInt import kotlin.math.roundToInt
import org.bukkit.Material import org.bukkit.Material
import org.bukkit.block.data.Ageable import org.bukkit.block.data.Ageable
@ -65,6 +67,9 @@ class FarmerMinionType : MinionType("farmer", AxMinionsPlugin.INSTANCE.getResour
if (AxMinionsPlugin.integrations.itemsAdderIntegration) { if (AxMinionsPlugin.integrations.itemsAdderIntegration) {
val customBlock = CustomBlock.byAlreadyPlaced(block) val customBlock = CustomBlock.byAlreadyPlaced(block)
if (customBlock !== null) { if (customBlock !== null) {
val preHarvestEvent = PreFarmerMinionHarvestEvent(minion, block)
Bukkit.getPluginManager().callEvent(preHarvestEvent)
if (preHarvestEvent.isCancelled) return@fastFor
val blockDrops = customBlock.getLoot(minion.getTool(), false) val blockDrops = customBlock.getLoot(minion.getTool(), false)
size += blockDrops.size size += blockDrops.size
drops.addAll(blockDrops) drops.addAll(blockDrops)
@ -75,6 +80,9 @@ class FarmerMinionType : MinionType("farmer", AxMinionsPlugin.INSTANCE.getResour
when (block.type) { when (block.type) {
Material.CACTUS, Material.SUGAR_CANE, Material.BAMBOO -> { Material.CACTUS, Material.SUGAR_CANE, Material.BAMBOO -> {
val preHarvestEvent = PreFarmerMinionHarvestEvent(minion, block)
Bukkit.getPluginManager().callEvent(preHarvestEvent)
if (preHarvestEvent.isCancelled) return@fastFor
MinionUtils.getPlant(block).fastFor { MinionUtils.getPlant(block).fastFor {
val blockDrops = it.getDrops(minion.getTool()) val blockDrops = it.getDrops(minion.getTool())
size++ size++
@ -83,16 +91,22 @@ class FarmerMinionType : MinionType("farmer", AxMinionsPlugin.INSTANCE.getResour
} }
} }
Material.MELON, Material.PUMPKIN -> { Material.MELON, Material.PUMPKIN, Material.TORCHFLOWER -> {
val preHarvestEvent = PreFarmerMinionHarvestEvent(minion, block)
Bukkit.getPluginManager().callEvent(preHarvestEvent)
if (preHarvestEvent.isCancelled) return@fastFor
val blockDrops = block.getDrops(minion.getTool()) val blockDrops = block.getDrops(minion.getTool())
size++ size++
drops.addAll(blockDrops) drops.addAll(blockDrops)
block.type = Material.AIR block.type = Material.AIR
} }
Material.COCOA_BEANS, Material.COCOA, Material.NETHER_WART, Material.WHEAT, Material.CARROTS, Material.BEETROOTS, Material.POTATOES -> { Material.COCOA_BEANS, Material.COCOA, Material.NETHER_WART, Material.WHEAT, Material.CARROTS, Material.BEETROOTS, Material.POTATOES, Material.PITCHER_CROP -> {
val ageable = block.blockData as Ageable val ageable = block.blockData as Ageable
if (ageable.age != ageable.maximumAge) return@fastFor if (ageable.age != ageable.maximumAge) return@fastFor
val preHarvestEvent = PreFarmerMinionHarvestEvent(minion, block)
Bukkit.getPluginManager().callEvent(preHarvestEvent)
if (preHarvestEvent.isCancelled) return@fastFor
val blockDrops = block.getDrops(minion.getTool()) val blockDrops = block.getDrops(minion.getTool())
size++ size++
drops.addAll(blockDrops) drops.addAll(blockDrops)
@ -103,6 +117,9 @@ class FarmerMinionType : MinionType("farmer", AxMinionsPlugin.INSTANCE.getResour
Material.SWEET_BERRY_BUSH -> { Material.SWEET_BERRY_BUSH -> {
val ageable = block.blockData as Ageable val ageable = block.blockData as Ageable
if (ageable.age != ageable.maximumAge) return@fastFor if (ageable.age != ageable.maximumAge) return@fastFor
val preHarvestEvent = PreFarmerMinionHarvestEvent(minion, block)
Bukkit.getPluginManager().callEvent(preHarvestEvent)
if (preHarvestEvent.isCancelled) return@fastFor
val blockDrops = block.getDrops(minion.getTool()) val blockDrops = block.getDrops(minion.getTool())
size++ size++
drops.addAll(blockDrops) drops.addAll(blockDrops)