Skip to content

Commit 8ef68f3

Browse files
authored
Merge pull request #34 from SLNE-Development/fix/integer-overflow
Fix spawn distance overflow and update version to 3.2.7
2 parents b463b33 + edeb646 commit 8ef68f3

4 files changed

Lines changed: 32 additions & 28 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
kotlin.code.style=official
22
kotlin.stdlib.default.dependency=false
33
org.gradle.parallel=true
4-
version=3.2.6
4+
version=3.2.7

src/main/kotlin/dev/slne/surf/protect/paper/math/Mth.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ object Mth {
4848
}
4949

5050
@JvmStatic
51-
fun calculatePricePerBlock(distance: Float): Double {
51+
fun calculatePricePerBlock(distance: Double): Double {
5252
val raw = PRICE_GRADIENT * distance + PRICE_INTERCEPT
5353
val rounded = (raw * 100.0).roundToInt() / 100.0
5454
return max(config.pricing.minPerBlock, rounded)
@@ -105,7 +105,7 @@ object Mth {
105105
data class EffectiveCostResult(
106106
val effectiveCost: Double,
107107
val pricePerBlock: Double,
108-
val spawnDistance: Float,
108+
val spawnDistance: Double,
109109
val discountFactor: Double = 1.0
110110
) {
111111
val hasDiscount: Boolean get() = discountFactor < 1.0

src/main/kotlin/dev/slne/surf/protect/paper/message/Messages.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import dev.slne.surf.protect.paper.util.castCoinFormat
99
import dev.slne.surf.transaction.api.currency.Currency
1010
import net.kyori.adventure.text.Component
1111
import net.kyori.adventure.text.format.TextDecoration
12+
import java.util.*
1213
import kotlin.math.roundToInt
13-
import java.util.Locale
1414

1515
/**
1616
* The Messages object serves as a container for various predefined message and text utilities.
@@ -236,7 +236,7 @@ object Messages {
236236
effectiveCost: Double,
237237
currency: Currency,
238238
pricePerBlock: Double,
239-
distanceToSpawn: Float,
239+
distanceToSpawn: Double,
240240
discountFactor: Double = 1.0
241241
) = buildText {
242242
val distanceToSpawn = (distanceToSpawn * 100).roundToInt() / 100.0

src/main/kotlin/dev/slne/surf/protect/paper/region/utils.kt

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,46 @@ import dev.slne.surf.protect.paper.config
55
import dev.slne.surf.protect.paper.math.Mth
66
import dev.slne.surf.protect.paper.util.bukkitWorld
77
import org.bukkit.World
8-
import org.spongepowered.math.vector.Vector2i
8+
import org.spongepowered.math.vector.Vector2l
99

1010
private val spawnPositionsByEnvironment = mapOf(
1111
World.Environment.NETHER to listOf(
12-
Vector2i(0, 0),
13-
Vector2i(0, -3125),
14-
Vector2i(3125, -3125),
15-
Vector2i(-3125, -3125),
16-
Vector2i(0, 3125),
17-
Vector2i(3125, 3125),
18-
Vector2i(-3125, 3125),
19-
Vector2i(3125, 0),
20-
Vector2i(-3125, 0)
12+
Vector2l(0, 0),
13+
Vector2l(0, -3125),
14+
Vector2l(3125, -3125),
15+
Vector2l(-3125, -3125),
16+
Vector2l(0, 3125),
17+
Vector2l(3125, 3125),
18+
Vector2l(-3125, 3125),
19+
Vector2l(3125, 0),
20+
Vector2l(-3125, 0)
2121
),
22-
World.Environment.THE_END to listOf(Vector2i.ZERO),
22+
World.Environment.THE_END to listOf(Vector2l.ZERO),
2323
World.Environment.NORMAL to listOf(
24-
Vector2i(0, 0),
25-
Vector2i(0, -25000),
26-
Vector2i(25000, -25000),
27-
Vector2i(-25000, -25000),
28-
Vector2i(0, 25000),
29-
Vector2i(25000, 25000),
30-
Vector2i(-25000, 25000),
31-
Vector2i(25000, 0),
32-
Vector2i(-25000, 0)
24+
Vector2l(0, 0),
25+
Vector2l(0, -25000),
26+
Vector2l(25000, -25000),
27+
Vector2l(-25000, -25000),
28+
Vector2l(0, 25000),
29+
Vector2l(25000, 25000),
30+
Vector2l(-25000, 25000),
31+
Vector2l(25000, 0),
32+
Vector2l(-25000, 0)
3333
)
3434
)
3535

3636
fun Location.getProtectionPricePerBlock(): PricePerBlockResult {
3737
val environment = this.bukkitWorld.environment
3838
val spawns = spawnPositionsByEnvironment[environment] ?: return PricePerBlockResult.EMPTY
39-
val pos = Vector2i(blockX, blockZ)
39+
val pos = Vector2l(blockX.toLong(), blockZ.toLong())
4040

4141
val nearestSpawn = spawns.minBy { pos.distanceSquared(it) }
4242
val distance = pos.distance(nearestSpawn)
4343

44+
if (!distance.isFinite()) {
45+
return PricePerBlockResult.EMPTY
46+
}
47+
4448
if (distance < config.pricing.spawnProtectionPerBlock) {
4549
return PricePerBlockResult(Double.MAX_VALUE, distance)
4650
}
@@ -55,9 +59,9 @@ fun Location.getProtectionPricePerBlock(): PricePerBlockResult {
5559

5660
data class PricePerBlockResult(
5761
val pricePerBlock: Double,
58-
val spawnDistance: Float
62+
val spawnDistance: Double
5963
) {
6064
companion object {
61-
val EMPTY = PricePerBlockResult(Double.MAX_VALUE, Float.MAX_VALUE)
65+
val EMPTY = PricePerBlockResult(Double.MAX_VALUE, Double.MAX_VALUE)
6266
}
6367
}

0 commit comments

Comments
 (0)