diff --git a/src/main/kotlin/de/intave/bedrock/BedrockSupport.kt b/src/main/kotlin/de/intave/bedrock/BedrockSupport.kt new file mode 100644 index 0000000..38a8744 --- /dev/null +++ b/src/main/kotlin/de/intave/bedrock/BedrockSupport.kt @@ -0,0 +1,47 @@ +package de.intave.bedrock + +import de.jpx3.intave.IntaveAccessor +import de.jpx3.intave.access.IntaveAccess +import de.jpx3.intave.access.IntaveColdException +import de.jpx3.intave.access.player.trust.TrustFactor +import org.bukkit.Bukkit +import org.bukkit.entity.Player +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.player.PlayerJoinEvent +import org.geysermc.floodgate.api.FloodgateApi +import java.lang.ref.WeakReference + +class BedrockSupport(private val plugin: BedrockSupportPlugin) : Listener { + private val accessReference: WeakReference = IntaveAccessor.weakAccess() + + init { + Bukkit.getPluginManager().registerEvents(this, plugin) + } + + @EventHandler + private fun receiveJoin(event: PlayerJoinEvent) { + check(0, event.player) + } + + /** + * Checks if the player's floodgate state changes in the next 30 ticks (3 x 10 ticks) to re-assign the + * trust-factor if anything went wrong + * + * Once the player is marked as bedrock player no re-check will be scheduled + * + * @param tick The current ticks + * @param player The player being checked + */ + private fun check(tick: Int, player: Player) { + val access = accessReference.get() ?: throw IntaveColdException("Intave offline") + val intavePlayer = access.player(player) + if (FloodgateApi.getInstance().isFloodgatePlayer(player.uniqueId)) { + intavePlayer.setTrustFactor(TrustFactor.BYPASS) + } else if (tick < 3) { + // Schedule a new check in 10 ticks + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, { check(tick + 1, player) }, 10) + } + } + +} diff --git a/src/main/kotlin/de/intave/bedrock/BedrockSupportPlugin.kt b/src/main/kotlin/de/intave/bedrock/BedrockSupportPlugin.kt index 7bff13f..fb32beb 100644 --- a/src/main/kotlin/de/intave/bedrock/BedrockSupportPlugin.kt +++ b/src/main/kotlin/de/intave/bedrock/BedrockSupportPlugin.kt @@ -1,56 +1,16 @@ package de.intave.bedrock -import de.jpx3.intave.IntaveAccessor -import de.jpx3.intave.access.IntaveAccess -import de.jpx3.intave.access.IntaveColdException -import de.jpx3.intave.access.player.trust.TrustFactor import org.bukkit.Bukkit -import org.bukkit.entity.Player -import org.bukkit.event.EventHandler import org.bukkit.event.Listener -import org.bukkit.event.player.PlayerJoinEvent import org.bukkit.plugin.java.JavaPlugin -import org.geysermc.floodgate.api.FloodgateApi -import java.lang.ref.WeakReference class BedrockSupportPlugin : JavaPlugin(), Listener { - private lateinit var accessReference: WeakReference override fun onEnable() { - enableBedrockSupport() - } - - /** - * Enables the [BedrockSupportPlugin] by creating a join listener to automatically assign the [TrustFactor.BYPASS] - * to bedrock players - */ - private fun enableBedrockSupport() { - accessReference = IntaveAccessor.weakAccess() - Bukkit.getPluginManager().registerEvents(this, this) - } - - @EventHandler - private fun receiveJoin(event: PlayerJoinEvent) { - check(0, event.player) - } - - /** - * Checks if the player's floodgate state changes in the next 30 ticks (3 x 10 ticks) to re-assign the - * trust-factor if anything went wrong - * - * Once the player is marked as bedrock player no re-check will be scheduled - * - * @param tick The current ticks - * @param player The player being checked - */ - private fun check(tick: Int, player: Player) { - val access = accessReference.get() ?: throw IntaveColdException("Intave offline") - val intavePlayer = access.player(player) - if (FloodgateApi.getInstance().isFloodgatePlayer(player.uniqueId)) { - intavePlayer.setTrustFactor(TrustFactor.BYPASS) - } else if (tick < 3) { - // Schedule a new check in 10 ticks - Bukkit.getScheduler().scheduleSyncDelayedTask(this, { check(tick + 1, player) }, 10) - } + // delay any intialization to make sure that intave api classes are loaded + Bukkit.getScheduler().scheduleSyncDelayedTask(this, { + BedrockSupport(this) + logger.info("Bedrock support enabled"); + }, 1) } }