From e884aad5a9cac3cf12d63021f5c8f0d09f536c9e Mon Sep 17 00:00:00 2001 From: iGabyTM Date: Thu, 12 Jan 2023 14:22:22 +0200 Subject: [PATCH] [plugin] implement location related requirement (distance from coordinate) --- .../ArcaneRequirementProcessor.kt | 4 ++ .../location/DistanceRequirement.kt | 45 +++++++++++++++++++ .../location/DistanceRequirementFactory.kt | 33 ++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/implementations/location/DistanceRequirement.kt create mode 100644 plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/implementations/location/DistanceRequirementFactory.kt diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/ArcaneRequirementProcessor.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/ArcaneRequirementProcessor.kt index b111e9b..b757fa3 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/ArcaneRequirementProcessor.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/ArcaneRequirementProcessor.kt @@ -1,6 +1,7 @@ package me.gabytm.minecraft.arcanevouchers.voucher.requirements import me.gabytm.minecraft.arcanevouchers.actions.ArcaneActionManager +import me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.location.DistanceRequirementFactory import me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.number.NumberRequirementFactory import me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.string.StringRequirementFactory import me.gabytm.minecraft.util.requirements.RequirementsList @@ -13,6 +14,9 @@ class ArcaneRequirementProcessor(private val actionManager: ArcaneActionManager) private val requirementFactories = mutableSetOf>() init { + // Location + registerFactory(DistanceRequirementFactory()) + registerFactory(NumberRequirementFactory()) registerFactory(StringRequirementFactory()) } diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/implementations/location/DistanceRequirement.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/implementations/location/DistanceRequirement.kt new file mode 100644 index 0000000..0eae52b --- /dev/null +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/implementations/location/DistanceRequirement.kt @@ -0,0 +1,45 @@ +package me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.location + +import me.gabytm.minecraft.arcanevouchers.actions.ArcaneAction +import me.gabytm.minecraft.arcanevouchers.actions.ArcaneActionManager +import me.gabytm.minecraft.arcanevouchers.voucher.requirements.ArcaneRequirement +import me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.common.variable.DoubleVariable +import me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.common.variable.LocationVariable +import me.gabytm.minecraft.util.requirements.Arguments +import org.bukkit.entity.Player +import kotlin.math.pow + +class DistanceRequirement( + name: String, + optional: Boolean, + negated: Boolean, + failActions: List, + actionManager: ArcaneActionManager, + private val locationVariable: LocationVariable, + private val distanceVariable: DoubleVariable +) : ArcaneRequirement( + name, optional, negated, failActions, actionManager +) { + + override fun check(player: Player?, arguments: Arguments): Boolean { + if (player == null) { + return false + } + + val distance = distanceVariable.get(player) ?: return false + val location = locationVariable.get(player) ?: return false + + if (player.world != location.world) { + return false + } + + return (player.location.distanceSquared(location) <= distance.pow(2)) == !negated + } + + companion object { + + const val TYPE: String = "distance" + + } + +} \ No newline at end of file diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/implementations/location/DistanceRequirementFactory.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/implementations/location/DistanceRequirementFactory.kt new file mode 100644 index 0000000..0358bbb --- /dev/null +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/implementations/location/DistanceRequirementFactory.kt @@ -0,0 +1,33 @@ +package me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.location + +import me.gabytm.minecraft.arcanevouchers.Constant.Requirement +import me.gabytm.minecraft.arcanevouchers.actions.ArcaneActionManager +import me.gabytm.minecraft.arcanevouchers.voucher.requirements.ArcaneRequirementFactory +import me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.common.variable.DoubleVariable +import me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.common.variable.LocationVariable +import org.bukkit.configuration.ConfigurationSection + +class DistanceRequirementFactory : ArcaneRequirementFactory() { + + override fun matches(source: ConfigurationSection): Boolean { + var type = source.getString(Requirement.TYPE)?.trim() ?: return false + + if (type.startsWith(Requirement.NEGATION)) { + type = type.substring(1) + } + + return type.equals(DistanceRequirement.TYPE, true) + } + + override fun create(source: ConfigurationSection, actionManager: ArcaneActionManager): DistanceRequirement? { + val type = source.getString(Requirement.TYPE)?.trim() ?: return null + val negated = type.startsWith(Requirement.NEGATION) + val optional = source.getBoolean(Requirement.OPTIONAL) + + val location = LocationVariable(source.getString("location") ?: return null) + val distance = DoubleVariable(source.getString("distance") ?: return null) + val failActions = actionManager.parseActions(source.getStringList(Requirement.FAIL_ACTIONS)) + return DistanceRequirement(source.name, optional, negated, failActions, actionManager, location, distance) + } + +} \ No newline at end of file