Skip to content

Commit

Permalink
Send bungeecord message immediately when sending spectators back to l…
Browse files Browse the repository at this point in the history
…obby
  • Loading branch information
4Ply committed Dec 22, 2024
1 parent 14c39e2 commit f446e87
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 29 deletions.
34 changes: 34 additions & 0 deletions src/main/kotlin/org/trackedout/AgroNet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.trackedout

import com.mojang.brigadier.arguments.IntegerArgumentType
import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.context.CommandContext
import io.netty.buffer.Unpooled
import me.lucko.fabric.api.permissions.v0.Permissions
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback
Expand All @@ -10,13 +12,17 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents
import net.fabricmc.fabric.api.networking.v1.PacketSender
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents
import net.fabricmc.fabric.api.resource.ResourceManagerHelper
import net.minecraft.network.PacketByteBuf
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket
import net.minecraft.resource.ResourceType
import net.minecraft.server.MinecraftServer
import net.minecraft.server.command.CommandManager.argument
import net.minecraft.server.command.CommandManager.literal
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.server.network.ServerPlayNetworkHandler
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.util.Formatting
import net.minecraft.util.Identifier
import okhttp3.OkHttpClient
import org.slf4j.LoggerFactory
import org.trackedout.RunContext.serverName
Expand Down Expand Up @@ -232,6 +238,18 @@ object AgroNet : ModInitializer {
})
}

CommandRegistrationCallback.EVENT.register { dispatcher, _, _ ->
val sendPlayerToLobby: (context: CommandContext<ServerCommandSource>) -> Int = { context ->
tasksApi.sendPlayerToLobby(context.source.name)

context.source.player?.let { sendPlayerToLobby(it) }
1
}

dispatcher.register(literal("leave").executes(sendPlayerToLobby))
dispatcher.register(literal("lobby").executes(sendPlayerToLobby))
}

if (!serverName.equals("builders", ignoreCase = true)) {
CommandRegistrationCallback.EVENT.register { dispatcher, _, _ ->
dispatcher.register(
Expand Down Expand Up @@ -312,6 +330,22 @@ object AgroNet : ModInitializer {
logger.info("Agronet online. Flee with extra flee!")
}

fun sendPlayerToLobby(player: ServerPlayerEntity) {
val buf = PacketByteBuf(Unpooled.buffer())
buf.writeString("ConnectOther")
buf.writeString(player.gameProfile.name)
buf.writeString("lobby")

player.networkHandler.sendPacket(
CustomPayloadS2CPacket(
Identifier(
"bungeecord", "main"
),
buf
)
)
}

private fun sendServerOnlineEvent(eventsApi: EventsApiWithContext, playerList: List<String>) {
try {
eventsApi.eventsPost(
Expand Down
32 changes: 10 additions & 22 deletions src/main/kotlin/org/trackedout/Extensions.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.trackedout

import net.minecraft.block.Block
import net.minecraft.block.BlockState
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.ItemStack
import net.minecraft.item.Items
Expand All @@ -22,18 +20,6 @@ fun PlayerEntity.debug(message: String) {
}
}

fun PlayerEntity.hasShulkerInInventory(): Boolean {
return this.inventory.containsAny { item -> item.isDeckedOutShulker() }
}

fun PlayerEntity.hasKeyInHand(): Boolean {
return this.handItems.any { item -> item.isDeckedOutKey() }
}

fun PlayerEntity.isReadyToStartDungeonRun(): Boolean {
return this.hasShulkerInInventory() && this.hasKeyInHand()
}

fun ServerCommandSource.sendMessage(message: String, format: Formatting) {
this.sendMessage(Text.literal(message).formatted(format))
}
Expand All @@ -42,14 +28,16 @@ fun ItemStack.isDeckedOutShulker(): Boolean {
return this.item.name == Items.CYAN_SHULKER_BOX.name
}

fun ItemStack.isDeckedOutKey(): Boolean {
return this.item.name == Items.ECHO_SHARD.name
}

fun BlockState.isDeckedOutDoor(): Boolean {
return this.isOf(Block.getBlockFromItem(Items.BLACKSTONE))
}

fun Task.updateState(api: TasksApi, state: String) {
api.tasksIdPatch(this.id!!, TasksIdPatchRequest(state))
}

fun TasksApi.sendPlayerToLobby(playerName: String) {
this.tasksPost(
Task(
server = "lobby",
type = "bungee-message",
arguments = listOf("ConnectOther", playerName, "lobby"),
)
)
}
32 changes: 30 additions & 2 deletions src/main/kotlin/org/trackedout/TaskManagement.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package org.trackedout

import io.netty.buffer.Unpooled
import net.minecraft.network.PacketByteBuf
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket
import net.minecraft.server.MinecraftServer
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import net.minecraft.util.Identifier
import org.slf4j.LoggerFactory
import org.trackedout.client.apis.TasksApi
import org.trackedout.client.models.Task


class TaskManagement(
private val tasksApi: TasksApi,
private val serverName: String,
Expand Down Expand Up @@ -38,8 +43,8 @@ class TaskManagement(
"shutdown-server-if-empty" -> {
if (server.playerManager.playerList.isEmpty()) {
logger.warn("Shutting down empty server as per dunga-dunga request")
if (!server.isStopping) {
server.shutdown()
if (server.isRunning) {
server.stop(false)
}
} else {
logger.warn("Server shutdown request ignored as ${server.playerManager.playerList.size} are online")
Expand All @@ -54,6 +59,29 @@ class TaskManagement(
}
}

"bungee-message" -> {
if (task.targetPlayer != null) {
val targetPlayer = server.overworld.players.find { it.gameProfile.name == task.targetPlayer }
if (targetPlayer != null) {
val buf = PacketByteBuf(Unpooled.buffer())
task.arguments?.forEach(buf::writeString)

targetPlayer.networkHandler.sendPacket(
CustomPayloadS2CPacket(
Identifier(
"bungeecord", "main"
),
buf
)
)
} else {
val message = "Task type is '${task.type}' and targets a player, but the player was not found"
logger.warn(message)
throw Exception(message)
}
}
}

else -> throw Exception("Unknown command type '${task.type}'")
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/kotlin/org/trackedout/commands/LogEventCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.context.CommandContext
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.text.Text
import org.trackedout.AgroNet.sendPlayerToLobby
import org.trackedout.EventsApiWithContext
import org.trackedout.client.apis.TasksApi
import org.trackedout.client.models.Event
import org.trackedout.sendPlayerToLobby

class LogEventCommand(
private val eventsApi: EventsApiWithContext,
Expand Down Expand Up @@ -59,12 +59,10 @@ class LogEventCommand(
val spectators = context.source.server.playerManager.playerList
.filter { player -> player.commandTags.contains("do2.spectating") }
.filter { player -> !player.commandTags.contains("do2.staff") }
.map { player -> player.gameProfile.name }

logger.info("game-ended event detected, sending ${spectators.size} spectators back to the lobby")
spectators.forEach { spectatorName ->
logger.info("Creating task to send $spectatorName back to the lobby")
tasksApi.sendPlayerToLobby(spectatorName)
spectators.forEach { spectator ->
sendPlayerToLobby(spectator)
}
}

Expand Down

0 comments on commit f446e87

Please sign in to comment.