Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Decent Holograms fix #11

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ repositories {
name = 'aikar-repo'
url = 'https://repo.aikar.co/content/groups/aikar/'
}

maven {
name = 'jitpack'
url = 'https://jitpack.io'
}
}

dependencies {
compileOnly 'org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT'

compileOnly 'com.sk89q.worldguard:worldguard-legacy:6.2'
compileOnly 'com.github.decentsoftware-eu:decentholograms:2.5.2'

shadow 'dev.dejvokep:boosted-yaml-spigot:1.3'
shadow 'co.aikar:acf-paper:0.5.1-SNAPSHOT'
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tk/bteitalia/core/BTEItalyCorePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BTEItalyCorePlugin : JavaPlugin() {
return
}

val fixDHListener = FixDHListener(this, worldGuard, config.features.fixDH)
val fixDHListener = FixDHListener(this, worldGuard, config.features.fixDH, logger)
server.pluginManager.registerEvents(fixDHListener, this)

if (commandManager == null) commandManager = PaperCommandManager(this)
Expand Down
71 changes: 50 additions & 21 deletions src/main/java/tk/bteitalia/core/feature/fixdh/FixDHListener.kt
Original file line number Diff line number Diff line change
@@ -1,60 +1,89 @@
package tk.bteitalia.core.feature.fixdh

import com.sk89q.worldguard.bukkit.WorldGuardPlugin
import eu.decentsoftware.holograms.api.DecentHologramsAPI
import org.bukkit.Location
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerTeleportEvent
import tk.bteitalia.core.BTEItalyCorePlugin
import tk.bteitalia.core.config.FixDHConfig
import tk.bteitalia.core.worldguard.WGRegionEnterEvent
import java.util.logging.Logger
import kotlin.math.max

internal class FixDHListener(
private val corePlugin: BTEItalyCorePlugin,
private val worldGuardPlugin: WorldGuardPlugin,
private val config: FixDHConfig
private val config: FixDHConfig,
private val logger: Logger? = null
) : Listener {
private fun reloadDH() {
val ticksDelay = max(0, (config.delay * 20).toLong())
private fun reloadDH(player: Player) {
val tps = 20
val ticksDelay = max(1, (config.delay * tps).toLong())

logger?.info("Scheduling reloading holograms for player ${player.name} in the next $ticksDelay ticks...")

corePlugin.server.scheduler.scheduleSyncDelayedTask(corePlugin, {
val console = corePlugin.server.consoleSender
logger?.info("Start reloading holograms for player ${player.name}")

try {
val dh = DecentHologramsAPI.get()
dh.hologramManager.updateVisibility(player)

for (command in config.executeCommands) {
corePlugin.server.dispatchCommand(console, command)
logger?.info("Done reloading holograms for player ${player.name}")
} catch (ex: Exception) {
logger?.severe("Unable to access DecentHolograms plugin. Is it missing?")
}
}, ticksDelay)
}

private fun processRegionEnter(player: Player, to: Location) {
if (!config.enabled) return

val regions = worldGuardPlugin.regionContainer.get(to.world) ?: return
val worldRegions = regions.regions.values.filter { worldReg ->
config.regions.map { it.lowercase() }.contains(worldReg.id.lowercase())
}

logger?.info("Player ${player.name} entered the location $to")

for (region in worldRegions) {
if (!region.contains(to.blockX, to.blockY, to.blockZ)) continue

logger?.info("Location $to is found in the region ${region.id}")

reloadDH(player)
return
}
}

@EventHandler(ignoreCancelled = true)
fun onWGRegionEnter(event: WGRegionEnterEvent) {
if (!config.enabled) return

val name = event.region.id
logger?.info("Player ${event.player.name} stepped into the region ${name}.")

for (regionName in config.regions) {
if (name.equals(regionName, ignoreCase = true)) {
reloadDH()
logger?.info("The region $name is found in the list.")

reloadDH(event.player)
return
}
}
}

@EventHandler(ignoreCancelled = true)
fun onPlayerJoin(event: PlayerJoinEvent) {
if (!config.enabled) return

val regions = worldGuardPlugin.regionContainer.get(event.player.world) ?: return
val spawnRegions = regions.regions.values.filter { it.id.equals("newspawn", ignoreCase = true) }

val location = event.player.location

for (region in spawnRegions) {
if (!region.contains(location.blockX, location.blockY, location.blockZ)) continue
processRegionEnter(event.player, event.player.location)
}

for (regionName in config.regions) {
reloadDH()
return
}
}
@EventHandler(ignoreCancelled = true)
fun onPlayerTeleport(event: PlayerTeleportEvent) {
processRegionEnter(event.player, event.to)
}
}
3 changes: 2 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ authors:
prefix: BTEICore
api-version: 1.12
main: tk.bteitalia.core.BTEItalyCorePlugin
load: STARTUP
load: POSTWORLD

depend:
- WorldGuard
- DecentHolograms

permissions:
bte-italy.core.admin:
Expand Down