-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[plugin] implement location related requirement (distance from coordi…
…nate)
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...craft/arcanevouchers/voucher/requirements/implementations/location/DistanceRequirement.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...rcanevouchers/voucher/requirements/implementations/location/DistanceRequirementFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |