diff --git a/src/main/java/com/github/quiltservertools/ledger/mixin/ServerCommonPacketListenerImplMixin.java b/src/main/java/com/github/quiltservertools/ledger/mixin/ServerCommonPacketListenerImplMixin.java new file mode 100644 index 00000000..f1eba08f --- /dev/null +++ b/src/main/java/com/github/quiltservertools/ledger/mixin/ServerCommonPacketListenerImplMixin.java @@ -0,0 +1,29 @@ +package com.github.quiltservertools.ledger.mixin; + +import com.github.quiltservertools.ledger.commands.subcommands.PageCommand; +import com.github.quiltservertools.ledger.utility.MessageUtils; +import net.minecraft.nbt.Tag; +import net.minecraft.network.protocol.common.ServerboundCustomClickActionPacket; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.network.ServerCommonPacketListenerImpl; +import net.minecraft.server.network.ServerGamePacketListenerImpl; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(ServerCommonPacketListenerImpl.class) +public class ServerCommonPacketListenerImplMixin { + @Inject(method = "handleCustomClickAction", at = @At("HEAD"), cancellable = true) + private void handleLedgerPageChangeClick(ServerboundCustomClickActionPacket packet, CallbackInfo ci) { + if (!packet.id().equals(MessageUtils.INSTANCE.getPageChangeAction())) return; + if (!((Object) this instanceof ServerGamePacketListenerImpl game)) return; + ServerPlayer player = game.player; + packet.payload().flatMap(Tag::asCompound) + .flatMap(root -> root.getInt("page")) + .ifPresent(page -> + PageCommand.INSTANCE.page(player.createCommandSourceStack(), page) + ); + ci.cancel(); + } +} diff --git a/src/main/kotlin/com/github/quiltservertools/ledger/commands/subcommands/PageCommand.kt b/src/main/kotlin/com/github/quiltservertools/ledger/commands/subcommands/PageCommand.kt index 67bc9c4b..3ebf0484 100644 --- a/src/main/kotlin/com/github/quiltservertools/ledger/commands/subcommands/PageCommand.kt +++ b/src/main/kotlin/com/github/quiltservertools/ledger/commands/subcommands/PageCommand.kt @@ -3,12 +3,12 @@ package com.github.quiltservertools.ledger.commands.subcommands import com.github.quiltservertools.ledger.Ledger import com.github.quiltservertools.ledger.commands.BuildableCommand import com.github.quiltservertools.ledger.database.DatabaseManager -import com.github.quiltservertools.ledger.utility.Context import com.github.quiltservertools.ledger.utility.LiteralNode import com.github.quiltservertools.ledger.utility.MessageUtils import com.github.quiltservertools.ledger.utility.TextColorPallet import com.mojang.brigadier.arguments.IntegerArgumentType import kotlinx.coroutines.launch +import net.minecraft.commands.CommandSourceStack import net.minecraft.commands.Commands.argument import net.minecraft.commands.Commands.literal import net.minecraft.network.chat.Component @@ -18,13 +18,11 @@ object PageCommand : BuildableCommand { literal("page") .then( argument("page", IntegerArgumentType.integer(1)) - .executes { page(it, IntegerArgumentType.getInteger(it, "page")) } + .executes { page(it.source, IntegerArgumentType.getInteger(it, "page")) } ) .build() - private fun page(context: Context, page: Int): Int { - val source = context.source - + fun page(source: CommandSourceStack, page: Int): Int { val params = Ledger.searchCache[source.textName] if (params != null) { Ledger.launch { diff --git a/src/main/kotlin/com/github/quiltservertools/ledger/utility/MessageUtils.kt b/src/main/kotlin/com/github/quiltservertools/ledger/utility/MessageUtils.kt index 875b2617..af3c4f72 100644 --- a/src/main/kotlin/com/github/quiltservertools/ledger/utility/MessageUtils.kt +++ b/src/main/kotlin/com/github/quiltservertools/ledger/utility/MessageUtils.kt @@ -8,19 +8,24 @@ import com.github.quiltservertools.ledger.network.Networking.hasNetworking import com.github.quiltservertools.ledger.network.packet.action.ActionS2CPacket import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking import net.minecraft.commands.CommandSourceStack +import net.minecraft.nbt.CompoundTag import net.minecraft.network.chat.ClickEvent import net.minecraft.network.chat.Component import net.minecraft.network.chat.HoverEvent import net.minecraft.network.chat.MutableComponent import net.minecraft.network.chat.Style +import net.minecraft.resources.Identifier import java.time.Duration import java.time.Instant import java.time.format.DateTimeFormatter import java.time.format.FormatStyle +import java.util.* import kotlin.time.ExperimentalTime import kotlin.time.toKotlinDuration object MessageUtils { + val pageChangeAction: Identifier = Ledger.identifier("page-change") + @OptIn(ExperimentalTime::class) suspend fun sendSearchResults(source: CommandSourceStack, results: SearchResults, header: Component) { // If the player has a Ledger compatible client, we send results as action packets rather than as chat messages @@ -46,13 +51,15 @@ object MessageUtils { Component.translatable( "text.ledger.footer.page_backward" ).setStyle(TextColorPallet.primaryVariant).withStyle { + val tag: CompoundTag = CompoundTag().apply { this.putInt("page", results.page-1) } + if (results.page > 1) { it.withHoverEvent( HoverEvent.ShowText( Component.translatable("text.ledger.footer.page_backward.hover") ) ).withClickEvent( - ClickEvent.RunCommand("/lg pg ${results.page - 1}") + ClickEvent.Custom(pageChangeAction, Optional.of(tag)) ) } else { Style.EMPTY @@ -63,13 +70,15 @@ object MessageUtils { Component.translatable( "text.ledger.footer.page_forward" ).setStyle(TextColorPallet.primaryVariant).withStyle { + val tag: CompoundTag = CompoundTag().apply { this.putInt("page", results.page+1) } + if (results.page < results.pages) { it.withHoverEvent( HoverEvent.ShowText( Component.translatable("text.ledger.footer.page_forward.hover") ) ).withClickEvent( - ClickEvent.RunCommand("/lg pg ${results.page + 1}") + ClickEvent.Custom(pageChangeAction, Optional.of(tag)) ) } else { Style.EMPTY diff --git a/src/main/resources/ledger.mixins.json b/src/main/resources/ledger.mixins.json index e2e1d398..9980204a 100644 --- a/src/main/resources/ledger.mixins.json +++ b/src/main/resources/ledger.mixins.json @@ -2,37 +2,36 @@ "required": true, "package": "com.github.quiltservertools.ledger.mixin", "mixins": [ - "BlockBehaviourMixin", + "AbstractContainerMenuMixin", "AnvilMenuMixin", "AxeItemMixin", + "BaseContainerBlockEntityMixin", + "BlockBehaviourMixin", "BlockItemMixin", "BlockMixin", "BucketDispenserBehaviorMixin", "BucketItemMixin", "CampfireBlockEntityMixin", "ChestBlockMixin", - "CopperGolemGiveMixin", "CompoundContainerMixin", + "ContainersMixin", + "CopperGolemGiveMixin", "DyeItemMixin", - "ServerExplosionMixin", "FillCommandMixin", "FlintAndSteelItemMixin", "FrostWalkerEnchantmentMixin", "HoeItemMixin", "HoneycombItemMixin", - "ContainersMixin", "JukeboxPlayableMixin", - "WaterlilyBlockMixin", - "BaseContainerBlockEntityMixin", - "TransportItemsBetweenContainersMixin", "PortalShapeMixin", - "AbstractContainerMenuMixin", + "ServerCommonPacketListenerImplMixin", + "ServerExplosionMixin", "ServerPlayerMixin", "SetBlockCommandMixin", "ShovelItemMixin", "SlotMixin", - "blocks.GrowingPlantBlockMixin", - "blocks.GrowingPlantHeadBlockMixin", + "TransportItemsBetweenContainersMixin", + "WaterlilyBlockMixin", "blocks.BambooStalkBlockMixin", "blocks.BedBlockMixin", "blocks.ButtonBlockMixin", @@ -52,13 +51,14 @@ "blocks.FenceGateBlockMixin", "blocks.FireBlockMixin", "blocks.FlowerPotBlockMixin", - "blocks.LiquidBlockMixin", + "blocks.GrowingPlantBlockMixin", + "blocks.GrowingPlantHeadBlockMixin", "blocks.IceBlockMixin", "blocks.JukeboxBlockMixin", "blocks.LeavesBlockMixin", "blocks.LeverBlockMixin", + "blocks.LiquidBlockMixin", "blocks.NoteBlockMixin", - "blocks.WeatheringCopperGolemStatueBlockMixin", "blocks.PointedDripstoneBlockMixin", "blocks.RepeaterBlockMixin", "blocks.RootedDirtBlockMixin", @@ -68,19 +68,20 @@ "blocks.SpreadingSnowyDirtBlockMixin", "blocks.SugarCaneBlockMixin", "blocks.TrapDoorBlockMixin", + "blocks.WeatheringCopperGolemStatueBlockMixin", "blocks.WetSpongeBlockMixin", - "blocks.cauldron.CauldronInteractionMixin", "blocks.cauldron.CauldronBlockMixin", + "blocks.cauldron.CauldronInteractionMixin", "blocks.cauldron.LayeredCauldronBlockMixin", - "blocks.coral.CoralPlantBlockMixin", - "blocks.coral.CoralPlantBlockMixin", "blocks.coral.CoralFanBlockMixin", + "blocks.coral.CoralPlantBlockMixin", "blocks.coral.CoralWallFanBlockMixin", + "blocks.lectern.AbstractContainerMenuMixin", "blocks.lectern.LecternBlockMixin", "blocks.lectern.LecternMenuMixin", - "blocks.lectern.AbstractContainerMenuMixin", - "blocks.sign.SignBlockMixin", "blocks.sign.SignBlockEntityMixin", + "blocks.sign.SignBlockMixin", + "entities.AbstractHurtingProjectileMixin", "entities.ArmorStandMixin", "entities.CatMixin", "entities.CopperGolemMixin", @@ -88,7 +89,6 @@ "entities.EndCrystalMixin", "entities.EnderDragonMixin", "entities.EvokerEntityWololoGoalMixin", - "entities.AbstractHurtingProjectileMixin", "entities.FallingBlockEntityMixin", "entities.ItemEntityMixin", "entities.ItemFrameMixin",