Skip to content
This repository was archived by the owner on Sep 23, 2024. It is now read-only.

Commit 3ce5949

Browse files
author
DaRacci
committed
fix: NullPointer Exception
1 parent 09b6617 commit 3ce5949

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

Minix-API/api/Minix-API.api

+5
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,7 @@ public final class dev/racci/minix/api/integrations/Integration$DefaultImpls {
15101510
public class dev/racci/minix/api/integrations/IntegrationManager {
15111511
public static final field Companion Ldev/racci/minix/api/integrations/IntegrationManager$Companion;
15121512
public fun <init> ()V
1513+
public final fun get ()Ljava/util/Optional;
15131514
public final fun getFirstRegistered (Lkotlin/jvm/functions/Function1;)Ljava/util/Optional;
15141515
public final fun getREGISTERED ()Ljava/util/Set;
15151516
public final fun getUNREGISTERED ()Ljava/util/Set;
@@ -1556,6 +1557,7 @@ public final class dev/racci/minix/api/integrations/placeholders/PlaceholderMana
15561557
}
15571558

15581559
public abstract interface class dev/racci/minix/api/integrations/regions/Region {
1560+
public static final field Companion Ldev/racci/minix/api/integrations/regions/Region$Companion;
15591561
public abstract fun canAttack (Lorg/bukkit/entity/Player;Lorg/bukkit/entity/Entity;)Z
15601562
public abstract fun canBreak (Lorg/bukkit/entity/Player;)Z
15611563
public abstract fun canBuild (Lorg/bukkit/entity/Player;)Z
@@ -1566,6 +1568,9 @@ public abstract interface class dev/racci/minix/api/integrations/regions/Region
15661568
public abstract fun getWorld ()Lorg/bukkit/World;
15671569
}
15681570

1571+
public final class dev/racci/minix/api/integrations/regions/Region$Companion {
1572+
}
1573+
15691574
public abstract interface class dev/racci/minix/api/integrations/regions/RegionIntegration : dev/racci/minix/api/integrations/Integration {
15701575
public abstract fun canAttack (Ldev/racci/minix/api/utils/minecraft/BlockPos;Lorg/bukkit/World;Lorg/bukkit/entity/Player;Lorg/bukkit/entity/Entity;)Z
15711576
public abstract fun canBreak (Ldev/racci/minix/api/utils/minecraft/BlockPos;Lorg/bukkit/World;Lorg/bukkit/entity/Player;)Z

Minix-API/src/main/kotlin/dev/racci/minix/api/integrations/IntegrationManager.kt

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ open class IntegrationManager<I : Integration> {
1818
this.REGISTERED.add(integration)
1919
}
2020

21+
fun get(): Optional<I> = REGISTERED.firstOrNull().let { Optional.ofNullable(it) }
22+
2123
/** Runs the action of the first found registered integration */
24+
@Deprecated("Use get instead", ReplaceWith("get().ifPresent(action)"))
2225
fun onFirstRegistered(action: (I) -> Unit) {
23-
if (this.REGISTERED.isEmpty()) return
24-
25-
this.REGISTERED.first().let(action)
26+
this.REGISTERED.firstOrNull()?.let(action)
2627
}
2728

2829
/** Runs the action of the first found registered integration and returns an optional of [T]. */
30+
@Deprecated("Use get instead", ReplaceWith("get().map(action)"))
2931
fun <T : Any> getFirstRegistered(action: (I) -> T): Optional<T> {
3032
var result: T? = null
3133
this.onFirstRegistered { integration -> result = action(integration) }

Minix-API/src/main/kotlin/dev/racci/minix/api/integrations/regions/Region.kt

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.racci.minix.api.integrations.regions
22

33
import org.bukkit.World
4+
import org.bukkit.WorldCreator
45
import org.bukkit.entity.Entity
56
import org.bukkit.entity.Player
67
import java.util.UUID
@@ -21,4 +22,24 @@ interface Region {
2122
player: Player,
2223
target: Entity
2324
): Boolean
25+
26+
companion object {
27+
internal val NONE: Region = object : Region {
28+
override val id: Int = -1
29+
override val name: String = "none"
30+
override val owner: UUID = UUID.randomUUID()
31+
override val world: World = WorldCreator("none").createWorld()!!
32+
33+
override fun canBuild(player: Player): Boolean = true
34+
35+
override fun canBreak(player: Player): Boolean = true
36+
37+
override fun canInteract(player: Player): Boolean = true
38+
39+
override fun canAttack(
40+
player: Player,
41+
target: Entity
42+
): Boolean = true
43+
}
44+
}
2445
}

Minix-API/src/main/kotlin/dev/racci/minix/api/integrations/regions/RegionManager.kt

+9-11
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,36 @@ object RegionManager : IntegrationManager<RegionIntegration>() {
1212
fun getRegion(
1313
pos: BlockPos,
1414
world: World
15-
): Optional<Region> = this.getFirstRegistered {
16-
it.getRegion(pos, world).orElse(null)
17-
}
15+
): Optional<Region> = this.get().map { it.getRegion(pos, world).orElse(Region.NONE) }
1816

1917
fun insideRegion(
2018
pos: BlockPos,
2119
world: World
22-
): Boolean = this.getFirstRegistered {
20+
): Boolean = this.get().map {
2321
it.insideRegion(pos, world)
2422
}.orElse(false)
2523

2624
fun canBuild(
2725
pos: BlockPos,
2826
world: World,
2927
player: Player
30-
): Boolean = this.getFirstRegistered {
28+
): Boolean = this.get().map {
3129
it.canBuild(pos, world, player)
3230
}.orElse(true)
3331

3432
fun canBreak(
3533
pos: BlockPos,
3634
world: World,
3735
player: Player
38-
): Boolean = this.getFirstRegistered {
36+
): Boolean = this.get().map {
3937
it.canBreak(pos, world, player)
4038
}.orElse(true)
4139

4240
fun canInteract(
4341
pos: BlockPos,
4442
world: World,
4543
player: Player
46-
): Boolean = this.getFirstRegistered {
44+
): Boolean = this.get().map {
4745
it.canInteract(pos, world, player)
4846
}.orElse(true)
4947

@@ -52,31 +50,31 @@ object RegionManager : IntegrationManager<RegionIntegration>() {
5250
world: World,
5351
player: Player,
5452
target: Entity
55-
): Boolean = this.getFirstRegistered {
53+
): Boolean = this.get().map {
5654
it.canAttack(pos, world, player, target)
5755
}.orElse(true)
5856

5957
fun ifWilderness(
6058
pos: BlockPos,
6159
world: World,
6260
action: () -> Unit
63-
): Boolean = this.getFirstRegistered {
61+
): Boolean = this.get().map {
6462
it.ifWilderness(pos, world, action)
6563
}.orElse(true) // if no region integration is found, assume wilderness
6664

6765
fun ifTrustedInRegion(
6866
pos: BlockPos,
6967
player: Player,
7068
action: () -> Unit
71-
): Boolean = this.getFirstRegistered {
69+
): Boolean = this.get().map {
7270
it.ifTrustedInRegion(pos, player, action)
7371
}.orElse(false)
7472

7573
fun ifWildernessOrTrusted(
7674
pos: BlockPos,
7775
player: Player,
7876
action: () -> Unit
79-
): Boolean = this.getFirstRegistered {
77+
): Boolean = this.get().map {
8078
it.ifWildernessOrTrusted(pos, player, action)
8179
}.orElse(true)
8280
}

0 commit comments

Comments
 (0)