Skip to content

Commit d825814

Browse files
committed
feat: add selector and using by event listener
1 parent 5050e3e commit d825814

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/main/kotlin/be4rjp/sclat/map/MapEventListener.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ package be4rjp.sclat.map
22

33
import be4rjp.sclat.match.MatchEndEvent
44
import be4rjp.sclat.match.MatchStartEvent
5+
import be4rjp.sclat.world.BukkitWorldAPI
56
import org.bukkit.event.Listener
67
import org.bukkit.event.server.ServerLoadEvent
78

89
object MapEventListener : Listener {
10+
var currentMapName: String = ""
11+
var nextMapName: String = ""
12+
913
fun onServerStart(event: ServerLoadEvent) {
1014
// todo: load 2 maps
15+
nextMap()
1116
}
1217

1318
fun onMatchStart(event: MatchStartEvent) {
@@ -17,5 +22,17 @@ object MapEventListener : Listener {
1722
fun onMatchEnd(event: MatchEndEvent) {
1823
// unload map without saving and mark as not using
1924
// load different map for future match
25+
BukkitWorldAPI.unloadWorld(currentMapName, false) ?: false
26+
nextMap()
27+
}
28+
29+
fun nextMap() {
30+
currentMapName = nextMapName
31+
nextMapName = MapSelector.selectRandomMap()
32+
BukkitWorldAPI.loadWorld(nextMapName)
33+
if (currentMapName == "") {
34+
currentMapName = MapSelector.selectRandomMap()
35+
BukkitWorldAPI.loadWorld(currentMapName)
36+
}
2037
}
2138
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package be4rjp.sclat.map
2+
3+
/**
4+
* Selector for match maps. It manages the maps and their usage status.
5+
*/
6+
object MapSelector {
7+
private val maps: MutableSet<String> = mutableSetOf()
8+
private val usingMaps: MutableSet<String> = mutableSetOf()
9+
10+
fun addMap(mapName: String) {
11+
maps.add(mapName)
12+
}
13+
14+
fun removeMap(mapName: String) {
15+
maps.remove(mapName)
16+
}
17+
18+
fun getAllMaps(): Set<String> = maps.toSet()
19+
20+
/**
21+
* get random map and mark its using.
22+
*
23+
* @return the name of the selected map, or null if no maps are available
24+
*/
25+
fun selectRandomMap(): String {
26+
if (maps.isEmpty()) throw RuntimeException("No maps available.")
27+
return maps.filter { s -> !usingMaps.contains(s) }.random().also { markUsed(it) }
28+
}
29+
30+
fun markUsed(mapName: String) {
31+
usingMaps.add(mapName)
32+
}
33+
34+
fun markUnused(mapName: String) {
35+
usingMaps.remove(mapName)
36+
}
37+
}

0 commit comments

Comments
 (0)