Skip to content

Commit

Permalink
[plugin] add warning for invalid requirement variables
Browse files Browse the repository at this point in the history
  • Loading branch information
iGabyTM committed Jan 13, 2023
1 parent a86523c commit 849a5f1
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.common.variable

import me.gabytm.minecraft.arcanevouchers.functions.papi
import me.gabytm.minecraft.arcanevouchers.functions.warning
import org.bukkit.entity.Player

data class BooleanVariable(
private val stringValue: String
private val string: String
) : Variable<Boolean>(
{ player -> stringValue.toBooleanStrictOrNull() ?: stringValue.papi(player).toBooleanStrictOrNull() }
)
{ player -> string.toBooleanStrictOrNull() ?: string.papi(player).toBooleanStrictOrNull() }
) {

override fun warn(player: Player?, requirement: String) {
warning("[$requirement] '$string' (${string.papi(player)}) is not a valid boolean (accepted values: true, false)")
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.common.variable

import me.gabytm.minecraft.arcanevouchers.functions.papi
import me.gabytm.minecraft.arcanevouchers.functions.warning
import org.bukkit.entity.Player

data class DoubleVariable(
private val string: String
) : Variable<Double>(
{ player -> string.toDoubleOrNull() ?: string.papi(player).toDoubleOrNull() }
)
) {

override fun warn(player: Player?, requirement: String) {
warning("[$requirement] '$string' (${string.papi(player)}) is not a valid Double")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.

import me.gabytm.minecraft.arcanevouchers.Constant
import me.gabytm.minecraft.arcanevouchers.functions.papi
import me.gabytm.minecraft.arcanevouchers.functions.warning
import org.bukkit.Bukkit
import org.bukkit.Location
import org.bukkit.entity.Player

data class LocationVariable(
private val string: String
val string: String
) : Variable<Location>(
transformer@{ player ->
// world;x;y;z
Expand All @@ -22,4 +24,10 @@ data class LocationVariable(
val z = parts[3].toDoubleOrNull() ?: return@transformer null
return@transformer Location(world, x, y, z)
}
)
) {

override fun warn(player: Player?, requirement: String) {
warning("[$requirement] '$string' (${string.papi(player)}) is not a valid location (expected format: world;x;y;z)")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ abstract class Variable<T>(

fun get(player: Player?): T? = parsed ?: transformer(player)

abstract fun warn(player: Player?, requirement: String)

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ class DistanceRequirement(
return false
}

val distance = distanceVariable.get(player) ?: return false
val location = locationVariable.get(player) ?: return false
val distance = distanceVariable.get(player) ?: kotlin.run {
distanceVariable.warn(player, getName())
return false
}
val location = locationVariable.get(player) ?: kotlin.run {
locationVariable.warn(player, getName())
return false
}

if (player.world != location.world) {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class NumberRequirement(
name, optional, negated, failActions, actionManager
) {

override fun check(t: Player?, arguments: Arguments): Boolean {
val leftNumber = left.get(t)
val rightNumber = right.get(t)

if (leftNumber == null || rightNumber == null) {
override fun check(player: Player?, arguments: Arguments): Boolean {
val leftNumber = left.get(player) ?: kotlin.run {
left.warn(player, getName())
return false
}
val rightNumber = right.get(player) ?: kotlin.run {
right.warn(player, getName())
return false
}

Expand Down

0 comments on commit 849a5f1

Please sign in to comment.