Skip to content

Commit

Permalink
Move logic into late-loading class
Browse files Browse the repository at this point in the history
Should fix intave#1 as intave api classes are not referenced from main class anymore
  • Loading branch information
Janmm14 committed Aug 12, 2023
1 parent 8150bf7 commit 25e5152
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 45 deletions.
47 changes: 47 additions & 0 deletions src/main/kotlin/de/intave/bedrock/BedrockSupport.kt
Original file line number Diff line number Diff line change
@@ -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<IntaveAccess> = 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)
}
}

}
50 changes: 5 additions & 45 deletions src/main/kotlin/de/intave/bedrock/BedrockSupportPlugin.kt
Original file line number Diff line number Diff line change
@@ -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<IntaveAccess>

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)
}
}

0 comments on commit 25e5152

Please sign in to comment.