-
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.
Showing
6 changed files
with
112 additions
and
68 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
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
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
47 changes: 47 additions & 0 deletions
47
common/src/main/kotlin/dev/erdragh/astralbot/handlers/FileWatcher.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,47 @@ | ||
package dev.erdragh.astralbot.handlers | ||
|
||
import dev.erdragh.astralbot.LOGGER | ||
import kotlinx.coroutines.* | ||
import java.nio.file.* | ||
|
||
class FileWatcher(private val directoryPath: Path, private val handler: (event: WatchEvent<Path>) -> Unit) { | ||
private var job: Job? = null | ||
private var watchService: WatchService? = null | ||
|
||
fun startWatching() { | ||
job = GlobalScope.launch(Dispatchers.IO) { | ||
watchService = FileSystems.getDefault().newWatchService() | ||
directoryPath.register( | ||
watchService, | ||
StandardWatchEventKinds.ENTRY_CREATE, | ||
StandardWatchEventKinds.ENTRY_MODIFY, | ||
StandardWatchEventKinds.ENTRY_DELETE | ||
) | ||
|
||
try { | ||
while (isActive) { | ||
LOGGER.info("Waiting for watchService WatchKey") | ||
val key = watchService?.take() ?: break | ||
LOGGER.info("Got watchService WatchKey") | ||
|
||
for (event in key.pollEvents()) { | ||
LOGGER.info("Event: {}", event.kind()) | ||
// Send the event to the channel | ||
handler(event as WatchEvent<Path>) | ||
} | ||
|
||
key.reset() | ||
} | ||
} catch (_: ClosedWatchServiceException) { | ||
// Do nothing, this exception means we should just stop | ||
} | ||
|
||
LOGGER.info("WatchService ending") | ||
} | ||
} | ||
|
||
fun stopWatching() { | ||
watchService?.close() | ||
job?.cancel() | ||
} | ||
} |
18 changes: 10 additions & 8 deletions
18
fabric/src/main/kotlin/dev/erdragh/astralbot/fabric/BotMod.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 |
---|---|---|
@@ -1,19 +1,21 @@ | ||
package dev.erdragh.astralbot.fabric | ||
|
||
import dev.erdragh.astralbot.LOGGER | ||
import dev.erdragh.astralbot.setupAstralbot | ||
import dev.erdragh.astralbot.startAstralbot | ||
import dev.erdragh.astralbot.stopAstralbot | ||
import net.fabricmc.api.ModInitializer | ||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents | ||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
object BotMod : ModInitializer { | ||
private val LOADED = AtomicBoolean(false) | ||
override fun onInitialize() { | ||
ServerWorldEvents.LOAD.register { server, _ -> | ||
if (!LOADED.getAndSet(true)) { | ||
LOGGER.info("Starting AstralBot on Fabric") | ||
setupAstralbot(server) | ||
} | ||
ServerLifecycleEvents.SERVER_STARTED.register { | ||
LOGGER.info("Starting AstralBot on Fabric") | ||
startAstralbot(it) | ||
} | ||
|
||
ServerLifecycleEvents.SERVER_STOPPING.register { | ||
stopAstralbot() | ||
} | ||
} | ||
} |
26 changes: 13 additions & 13 deletions
26
forge/src/main/kotlin/dev/erdragh/astralbot/forge/BotMod.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 |
---|---|---|
@@ -1,28 +1,28 @@ | ||
package dev.erdragh.astralbot.forge | ||
|
||
import dev.erdragh.astralbot.LOGGER | ||
import dev.erdragh.astralbot.setupAstralbot | ||
import dev.erdragh.astralbot.startAstralbot | ||
import dev.erdragh.astralbot.stopAstralbot | ||
import net.minecraftforge.event.level.LevelEvent | ||
import net.minecraftforge.event.server.ServerStartedEvent | ||
import net.minecraftforge.event.server.ServerStoppingEvent | ||
import net.minecraftforge.fml.common.Mod | ||
import thedarkcolour.kotlinforforge.forge.FORGE_BUS | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
@Mod("astralbot") | ||
object BotMod { | ||
private val LOADED = AtomicBoolean(false) | ||
init { | ||
FORGE_BUS.addListener(::onWorldLoad) | ||
FORGE_BUS.addListener(::onServerStart) | ||
FORGE_BUS.addListener(::onServerStop) | ||
} | ||
|
||
private fun onWorldLoad(event: LevelEvent.Load) { | ||
if (!LOADED.getAndSet(true)) { | ||
LOGGER.info("AstralBot starting on Forge") | ||
val server = event.level.server | ||
if (server == null) { | ||
throw IllegalStateException("No server accessible onWorldLoad") | ||
} else { | ||
setupAstralbot(server) | ||
} | ||
} | ||
private fun onServerStart(event: ServerStartedEvent) { | ||
LOGGER.info("AstralBot starting on Forge") | ||
startAstralbot(event.server) | ||
} | ||
|
||
private fun onServerStop(event: ServerStoppingEvent) { | ||
stopAstralbot() | ||
} | ||
} |