Skip to content

Commit

Permalink
[plugin] implement location related requirement (distance from coordi…
Browse files Browse the repository at this point in the history
…nate)
  • Loading branch information
iGabyTM committed Jan 12, 2023
1 parent 6ae5b24 commit e884aad
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +14,9 @@ class ArcaneRequirementProcessor(private val actionManager: ArcaneActionManager)
private val requirementFactories = mutableSetOf<ArcaneRequirementFactory<out ArcaneRequirement>>()

init {
// Location
registerFactory(DistanceRequirementFactory())

registerFactory(NumberRequirementFactory())
registerFactory(StringRequirementFactory())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ArcaneAction>,
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"

}

}
Original file line number Diff line number Diff line change
@@ -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<DistanceRequirement>() {

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

}

0 comments on commit e884aad

Please sign in to comment.