Skip to content

Commit

Permalink
refactor: bump ido and geary
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Oct 27, 2024
1 parent bbbc645 commit c907fb6
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 126 deletions.
10 changes: 10 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ dependencies {
compileOnly(libs.blocky)
compileOnly(libs.minecraft.plugin.axiompaper)
}

kotlin {
compilerOptions {
freeCompilerArgs.addAll(
"-opt-in=kotlinx.serialization.ExperimentalSerializationApi",
"-opt-in=kotlin.ExperimentalUnsignedTypes",
"-Xcontext-receivers"
)
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group=com.mineinabyss
version=1.11
idofrontVersion=0.25.5
idofrontVersion=0.25.17-dev.2
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
gearyPaper = "0.30.21"
gearyPaper = "0.31.0-dev.3"
blocky = "0.10.9"

[libraries]
Expand Down
15 changes: 15 additions & 0 deletions src/main/kotlin/com/mineinabyss/bonfire/BonfireAddon.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mineinabyss.bonfire

import com.mineinabyss.bonfire.systems.bonfireEffectSystem
import com.mineinabyss.geary.addons.dsl.createAddon
import com.mineinabyss.geary.autoscan.autoscan

val BonfireAddon = createAddon("Bonfire", configuration = {
autoscan(BonfirePlugin::class.java.classLoader, "com.mineinabyss.bonfire") {
all()
}
}) {
systems {
bonfireEffectSystem()
}
}
129 changes: 60 additions & 69 deletions src/main/kotlin/com/mineinabyss/bonfire/BonfireCommands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import com.mineinabyss.geary.papermc.datastore.decode
import com.mineinabyss.geary.papermc.datastore.encode
import com.mineinabyss.geary.papermc.datastore.encodeComponentsTo
import com.mineinabyss.geary.papermc.datastore.remove
import com.mineinabyss.geary.papermc.gearyPaper
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull
import com.mineinabyss.geary.papermc.withGeary
import com.mineinabyss.geary.serialization.setPersisting
import com.mineinabyss.idofront.commands.brigadier.IdoCommandContext
import com.mineinabyss.idofront.commands.brigadier.commands
import com.mineinabyss.idofront.commands.brigadier.context.IdoCommandContext
import com.mineinabyss.idofront.commands.brigadier.executes
import com.mineinabyss.idofront.commands.brigadier.playerExecutes
import com.mineinabyss.idofront.messaging.error
import com.mineinabyss.idofront.messaging.info
import com.mineinabyss.idofront.messaging.success
Expand All @@ -36,20 +40,21 @@ object BonfireCommands {
"bonfire" {
"debug" {
playerExecutes {
with(player.toGeary()) {
when {
has<BonfireDebug>() -> {
remove<BonfireDebug>()
sender.error("Bonfire debug mode disabled")
}
else -> {
setPersisting(BonfireDebug())
sender.success("Bonfire debug mode enabled")
player.withGeary {
with(player.toGeary()) {
when {
has<BonfireDebug>() -> {
remove<BonfireDebug>()
sender.error("Bonfire debug mode disabled")
}
else -> {
setPersisting(BonfireDebug())
sender.success("Bonfire debug mode enabled")
}
}
encodeComponentsTo(player)
}
encodeComponentsTo(player)
}

}
}
"reload" {
Expand All @@ -59,61 +64,54 @@ object BonfireCommands {
}
}
"players" {
fun IdoCommandContext.runPlayersCommand(location: Location) {
val bonfireLoc = location.toBlockCenterLocation()
val (x,y,z) = bonfireLoc.blockX to bonfireLoc.blockY to bonfireLoc.blockZ
playerExecutes(
ArgumentTypes.finePosition(true).suggests {
suggestFiltering("${location.blockX} ${location.blockY} ${location.blockZ}")
}.named("location"),
) { location ->
val location = location.resolve(context.source).toLocation(player.world)
val (x,y,z) = location.blockX() to location.blockY() to location.blockZ()

location.world.getChunkAtAsync(location).thenAccept {
val bonfireEntity = bonfireLoc.getNearbyEntitiesByType(ItemDisplay::class.java, 5.0).firstOrNull()
bonfireEntity?.toGeary()?.get<Bonfire>()?.let { bonfire ->
sender.info(
"Players with their respawn set at this bonfire: ${
bonfire.bonfirePlayers.joinToString(", ") { Bukkit.getOfflinePlayer(it).name ?: "Unknown" }
}"
)
} ?: sender.error("Could not find bonfire at $x $y $z")
}
}

executes { context.source.executor?.location?.let { runPlayersCommand(it) } }
val location by ArgumentTypes.blockPosition().suggests {
(context.source.executor as? Player)?.location?.let {
suggestFiltering("${it.blockX} ${it.blockY} ${it.blockZ}")
location.world.getChunkAtAsync(location).thenAccept { chunk ->
chunk.addPluginChunkTicket(bonfire.plugin)
location.getNearbyEntitiesByType(ItemDisplay::class.java, 2.0)
.firstOrNull()?.toGeary()?.get<Bonfire>()?.let { bonfire ->
val bonfireNames = bonfire.bonfirePlayers.joinToString(", ") { Bukkit.getOfflinePlayer(it).name ?: "Unknown" }
sender.info("Players with their respawn set at this bonfire: $bonfireNames")
} ?: sender.error("Could not find bonfire at $x $y $z")
}
}
executes { runPlayersCommand(location().toLocation(context.source.location.world)!!) }
val world by ArgumentTypes.world().suggests {
suggest(Bukkit.getWorlds().map { it.key.asString() })
}
executes { runPlayersCommand(location().toLocation(world()!!)) }
}

"respawn" {
val offlinePlayer by StringArgumentType.word()
"get" {
executes {
val offlinePlayer = Bukkit.getOfflinePlayer(offlinePlayer())
executes(StringArgumentType.word().named("offlinePlayer")) { offlinePlayer ->
val offlinePlayer = Bukkit.getOfflinePlayer(offlinePlayer)
val respawn = when {
offlinePlayer.isOnline -> offlinePlayer.player?.toGearyOrNull()?.get<BonfireRespawn>()
else -> offlinePlayer.getOfflinePDC()?.decode<BonfireRespawn>()
}?.bonfireLocation ?: return@executes sender.error("Could not find BonfireRespawn for the given Player")
else -> with(gearyPaper.worldManager.global) {
offlinePlayer.getOfflinePDC()?.decode<BonfireRespawn>()
}
}?.bonfireLocation
?: return@executes sender.error("Could not find BonfireRespawn for the given Player")
sender.info("Bonfire-Respawn for ${offlinePlayer.name} is at ${respawn.x}, ${respawn.y}, ${respawn.z} in ${respawn.world.name}")
}
}
"set" {
val location by ArgumentTypes.blockPosition().suggests {
(context.source.executor as? Player)?.location?.let {
suggestFiltering("${it.blockX} ${it.blockY} ${it.blockZ}")
}
}

fun IdoCommandContext.handleRespawnSet(offlinePlayer: OfflinePlayer, location: Location) {
playerExecutes(
StringArgumentType.word().named("offlinePlayer"),
ArgumentTypes.finePosition(true).suggests {
suggestFiltering("${location.blockX} ${location.blockY} ${location.blockZ}")
}.named("location"),
) { offlinePlayer, location ->
val offlinePlayer = Bukkit.getOfflinePlayer(offlinePlayer)
// Ensures the player has a datafile, aka joined the server before, so we can save the bonfire location
offlinePlayer.getOfflinePDC() ?: return sender.error("Could not find PDC for the given OfflinePlayer")
val world = location.world
offlinePlayer.getOfflinePDC() ?: return@playerExecutes sender.error("Could not find PDC for the given OfflinePlayer")
val location = location.resolve(context.source).toLocation(player.world)
val bonfireLoc = location.toBlockCenterLocation()
val (x,y,z) = bonfireLoc.blockX to bonfireLoc.blockY to bonfireLoc.blockZ

world.getChunkAtAsync(location).thenAccept {
player.world.getChunkAtAsync(location).thenAccept {
val bonfireEntity = bonfireLoc.getNearbyEntitiesByType(ItemDisplay::class.java, 0.5).firstOrNull()

bonfireEntity?.toGearyOrNull()?.get<Bonfire>()?.let { bonfire ->
Expand All @@ -122,44 +120,37 @@ object BonfireCommands {
sender.error("Player is already registered to this bonfire")
bonfire.bonfirePlayers.size >= bonfire.maxPlayerCount ->
sender.error("Bonfire is full")
else -> {
else -> with(gearyPaper.worldManager.global) {
offlinePlayer.editOfflinePDC {
encode(BonfireRespawn(bonfireEntity.uniqueId, bonfireEntity.location))
}
bonfire.bonfirePlayers += offlinePlayer.uniqueId
bonfireEntity.updateBonfireState()
BonfirePacketHelpers.sendAddonPacket(bonfireEntity)
sender.success("Set respawn point for ${offlinePlayer.name} to $x $y $z in ${world.name}")
sender.success("Set respawn point for ${offlinePlayer.name} to $x $y $z in ${player.world.name}")
}
}
} ?: sender.error("Could not find bonfire at $x $y $z")
}
}
executes {
handleRespawnSet(Bukkit.getOfflinePlayer(offlinePlayer()), location().toLocation(context.source.location.world))
}
val world by ArgumentTypes.world().suggests {
suggest(Bukkit.getWorlds().map { it.key.asString() })
}
executes {
handleRespawnSet(Bukkit.getOfflinePlayer(offlinePlayer()), location().toLocation(world()!!))
}
}
"remove" {
executes {
val offlinePlayer = Bukkit.getOfflinePlayer(offlinePlayer())
executes(StringArgumentType.word().named("offlinePlayer")) { offlinePlayer ->
val offlinePlayer = Bukkit.getOfflinePlayer(offlinePlayer)
val respawn = when {
offlinePlayer.isOnline -> {
val respawn = offlinePlayer.player?.toGearyOrNull()?.get<BonfireRespawn>()
offlinePlayer.player?.toGeary()?.remove<BonfireRespawn>()
respawn
}
else -> {
val pdc = offlinePlayer.getOfflinePDC() ?: return@executes sender.error("Could not find PDC for the given OfflinePlayer")
val respawn = pdc.decode<BonfireRespawn>() ?: return@executes sender.error("OfflinePlayer has no bonfire set")
pdc.remove<BonfireRespawn>()
offlinePlayer.saveOfflinePDC(pdc)
respawn
with(gearyPaper.worldManager.global) {
val pdc = offlinePlayer.getOfflinePDC() ?: return@executes sender.error("Could not find PDC for the given OfflinePlayer")
val respawn = pdc.decode<BonfireRespawn>() ?: return@executes sender.error("OfflinePlayer has no bonfire set")
pdc.remove<BonfireRespawn>()
offlinePlayer.saveOfflinePDC(pdc)
respawn
}
}
} ?: return@executes sender.error("Player has no respawn point set")

Expand Down
14 changes: 5 additions & 9 deletions src/main/kotlin/com/mineinabyss/bonfire/BonfirePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import com.mineinabyss.bonfire.listeners.FixUntrackedBonfiresListener
import com.mineinabyss.bonfire.listeners.PlayerListener
import com.mineinabyss.bonfire.systems.bonfireEffectSystem
import com.mineinabyss.geary.addons.GearyPhase
import com.mineinabyss.geary.addons.dsl.createAddon
import com.mineinabyss.geary.autoscan.autoscan
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.papermc.configure
import com.mineinabyss.geary.papermc.gearyPaper
import com.mineinabyss.idofront.config.config
import com.mineinabyss.idofront.di.DI
import com.mineinabyss.idofront.messaging.ComponentLogger
Expand All @@ -20,10 +23,8 @@ import org.bukkit.plugin.java.JavaPlugin
class BonfirePlugin : JavaPlugin() {
override fun onLoad() {
registerBonfireContext()
geary {
autoscan(classLoader, "com.mineinabyss.bonfire") {
all()
}
gearyPaper.configure {
install(BonfireAddon)
}
}

Expand All @@ -36,11 +37,6 @@ class BonfirePlugin : JavaPlugin() {
DebugListener(),
FixUntrackedBonfiresListener()
)

geary.pipeline.runOnOrAfter(GearyPhase.INIT_SYSTEMS) {
geary.bonfireEffectSystem()
}

}

fun registerBonfireContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import com.mineinabyss.bonfire.components.BonfireRemoved
import com.mineinabyss.bonfire.components.BonfireRespawn
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull
import com.mineinabyss.geary.papermc.tracking.items.gearyItems
import com.mineinabyss.geary.papermc.tracking.items.ItemTracking
import com.mineinabyss.geary.papermc.withGeary
import com.mineinabyss.idofront.entities.toPlayer
import kotlinx.coroutines.delay
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket
Expand Down Expand Up @@ -60,28 +61,31 @@ fun Player.removeOldBonfire() {
* Updates the bonfire state for all players.
*/
fun ItemDisplay.updateBonfireState() {
val plugin = bonfire.plugin
val bonfire = toGearyOrNull()?.get<Bonfire>() ?: return
withGeary {
val plugin = bonfire.plugin
val bonfire = toGearyOrNull()?.get<Bonfire>() ?: return
val itemTracking = getAddon(ItemTracking)

when {// Set the base-furniture item to the correct state
bonfire.bonfirePlayers.isEmpty() -> {
this.brightness = toGearyOrNull()?.get<BlockyFurniture>()?.properties?.brightness
gearyItems.createItem(bonfire.states.unlit)?.let { setItemStack(it) }
}
else -> {
this.brightness = Display.Brightness(15, 15)
gearyItems.createItem(bonfire.states.lit)?.let { setItemStack(it) }
when {// Set the base-furniture item to the correct state
bonfire.bonfirePlayers.isEmpty() -> {
brightness = toGearyOrNull()?.get<BlockyFurniture>()?.properties?.brightness
itemTracking.createItem(bonfire.states.unlit)?.let { setItemStack(it) }
}
else -> {
brightness = Display.Brightness(15, 15)
itemTracking.createItem(bonfire.states.lit)?.let { setItemStack(it) }

// Set state via packets to 'set' for all online players currently at the bonfire
val stateItem = gearyItems.createItem(bonfire.states.set) ?: return
val metadataPacket = ClientboundSetEntityDataPacket(entityId,
listOf(SynchedEntityData.DataValue(23, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(stateItem)))
)
// Set state via packets to 'set' for all online players currently at the bonfire
val stateItem = itemTracking.createItem(bonfire.states.set) ?: return
val metadataPacket = ClientboundSetEntityDataPacket(entityId,
listOf(SynchedEntityData.DataValue(23, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(stateItem)))
)

plugin.launch {
delay(2.ticks)
bonfire.bonfirePlayers.mapNotNull { it.toPlayer()?.takeIf( { p -> p.canSee(this@updateBonfireState) }) }.forEach {
(it as CraftPlayer).handle.connection.send(metadataPacket)
plugin.launch {
delay(2.ticks)
bonfire.bonfirePlayers.mapNotNull { it.toPlayer()?.takeIf( { p -> p.canSee(this@updateBonfireState) }) }.forEach {
(it as CraftPlayer).handle.connection.send(metadataPacket)
}
}
}
}
Expand Down
Loading

0 comments on commit c907fb6

Please sign in to comment.