From 0d3475ac1379908e3204339e9431e88097e40702 Mon Sep 17 00:00:00 2001 From: Drex Date: Sat, 28 Sep 2024 17:56:20 +0200 Subject: [PATCH 1/2] Fix search packet --- .../packet/receiver/SearchC2SPacket.kt | 77 +++++++------------ .../ClientPlayerInteractionManagerMixin.java | 4 +- .../ledger/testmod/LedgerTest.kt | 8 +- 3 files changed, 33 insertions(+), 56 deletions(-) diff --git a/src/main/kotlin/com/github/quiltservertools/ledger/network/packet/receiver/SearchC2SPacket.kt b/src/main/kotlin/com/github/quiltservertools/ledger/network/packet/receiver/SearchC2SPacket.kt index 46c4cf9a..78467dea 100644 --- a/src/main/kotlin/com/github/quiltservertools/ledger/network/packet/receiver/SearchC2SPacket.kt +++ b/src/main/kotlin/com/github/quiltservertools/ledger/network/packet/receiver/SearchC2SPacket.kt @@ -9,7 +9,7 @@ import com.github.quiltservertools.ledger.network.packet.response.ResponseCodes import com.github.quiltservertools.ledger.network.packet.response.ResponseContent import com.github.quiltservertools.ledger.network.packet.response.ResponseS2CPacket import com.github.quiltservertools.ledger.utility.MessageUtils -import com.github.quiltservertools.ledger.utility.launchMain +import com.github.quiltservertools.ledger.utility.TextColorPallet import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import me.lucko.fabric.api.permissions.v0.Permissions @@ -17,26 +17,27 @@ import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking import net.minecraft.network.PacketByteBuf import net.minecraft.network.codec.PacketCodec import net.minecraft.network.packet.CustomPayload +import net.minecraft.text.Text -data class SearchC2SPacket(val restore: Boolean, val args: String) : CustomPayload { +data class SearchC2SPacket(val args: String, val pages: Int) : CustomPayload { override fun getId() = ID companion object : ServerPlayNetworking.PlayPayloadHandler { val ID: CustomPayload.Id = CustomPayload.Id(LedgerPacketTypes.SEARCH.id) val CODEC: PacketCodec = CustomPayload.codecOf({ _, _ -> TODO() }, { - SearchC2SPacket(it.readBoolean(), it.readString()) + SearchC2SPacket(it.readString(), it.readInt()) }) override fun receive(payload: SearchC2SPacket, context: ServerPlayNetworking.Context) { val player = context.player() val sender = context.responseSender() if (!Permissions.check(player, "ledger.networking", CommandConsts.PERMISSION_LEVEL) || - !Permissions.check(player, "ledger.commands.rollback", CommandConsts.PERMISSION_LEVEL) + !Permissions.check(player, "ledger.commands.search", CommandConsts.PERMISSION_LEVEL) ) { ResponseS2CPacket.sendResponse( ResponseContent( - LedgerPacketTypes.ROLLBACK.id, + LedgerPacketTypes.SEARCH.id, ResponseCodes.NO_PERMISSION.code ), sender @@ -49,58 +50,34 @@ data class SearchC2SPacket(val restore: Boolean, val args: String) : CustomPaylo val params = SearchParamArgument.get(payload.args, source) ResponseS2CPacket.sendResponse( - ResponseContent(LedgerPacketTypes.ROLLBACK.id, ResponseCodes.EXECUTING.code), + ResponseContent(LedgerPacketTypes.SEARCH.id, ResponseCodes.EXECUTING.code), sender ) Ledger.launch(Dispatchers.IO) { - MessageUtils.warnBusy(source) - if (payload.restore) { - val actions = DatabaseManager.selectRestore(params) - - source.world.launchMain { - val actionIds = HashSet() - - for (action in actions) { - if (action.restore(source.server)) { - actionIds.add(action.id) - } - } - Ledger.launch(Dispatchers.IO) { - DatabaseManager.restoreActions(actionIds) - } + Ledger.searchCache[source.name] = params - ResponseS2CPacket.sendResponse( - ResponseContent( - LedgerPacketTypes.ROLLBACK.id, - ResponseCodes.COMPLETED.code - ), - sender - ) - } - } else { - val actions = DatabaseManager.selectRollback(params) - - source.world.launchMain { - val actionIds = HashSet() + MessageUtils.warnBusy(source) + val results = DatabaseManager.searchActions(params, 1) - for (action in actions) { - if (action.rollback(source.server)) { - actionIds.add(action.id) - } - } - Ledger.launch(Dispatchers.IO) { - DatabaseManager.rollbackActions(actionIds) - } - ResponseS2CPacket.sendResponse( - ResponseContent( - LedgerPacketTypes.ROLLBACK.id, - ResponseCodes.COMPLETED.code - ), - sender - ) - } + for (i in 1..payload.pages) { + val page = DatabaseManager.searchActions(results.searchParams, i) + MessageUtils.sendSearchResults( + source, + page, + Text.translatable( + "text.ledger.header.search" + ).setStyle(TextColorPallet.primary) + ) } + + ResponseS2CPacket.sendResponse( + ResponseContent( + LedgerPacketTypes.SEARCH.id, + ResponseCodes.COMPLETED.code + ), + sender + ) } } } diff --git a/src/testmod/java/com/github/quiltservertools/ledger/testmod/mixin/ClientPlayerInteractionManagerMixin.java b/src/testmod/java/com/github/quiltservertools/ledger/testmod/mixin/ClientPlayerInteractionManagerMixin.java index 914045de..477b7c1c 100644 --- a/src/testmod/java/com/github/quiltservertools/ledger/testmod/mixin/ClientPlayerInteractionManagerMixin.java +++ b/src/testmod/java/com/github/quiltservertools/ledger/testmod/mixin/ClientPlayerInteractionManagerMixin.java @@ -19,11 +19,11 @@ public class ClientPlayerInteractionManagerMixin { method = "interactBlock", at = @At( value = "INVOKE", - target = "Lnet/minecraft/client/network/ClientPlayNetworkHandler;sendPacket(Lnet/minecraft/network/Packet;)V" + target = "Lnet/minecraft/client/network/ClientPlayerInteractionManager;sendSequencedPacket(Lnet/minecraft/client/world/ClientWorld;Lnet/minecraft/client/network/SequencedPacketCreator;)V" ), cancellable = true ) - private void onInteractBlock(ClientPlayerEntity player, ClientWorld world, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable cir) { + private void onInteractBlock(ClientPlayerEntity player, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable cir) { if (!InspectCommand.INSTANCE.getInspectOn()) return; diff --git a/src/testmod/kotlin/com/github/quiltservertools/ledger/testmod/LedgerTest.kt b/src/testmod/kotlin/com/github/quiltservertools/ledger/testmod/LedgerTest.kt index 85bf2f5b..284ea3bf 100644 --- a/src/testmod/kotlin/com/github/quiltservertools/ledger/testmod/LedgerTest.kt +++ b/src/testmod/kotlin/com/github/quiltservertools/ledger/testmod/LedgerTest.kt @@ -17,10 +17,10 @@ import org.apache.logging.log4j.LogManager import org.apache.logging.log4j.Logger object LedgerTest : ClientModInitializer { - val HANDSHAKE = Identifier("ledger", "handshake") - val INSPECT = Identifier("ledger", "inspect") - val SEARCH = Identifier("ledger", "search") - val ACTION = Identifier("ledger", "action") + val HANDSHAKE = Identifier.of("ledger", "handshake") + val INSPECT = Identifier.of("ledger", "inspect") + val SEARCH = Identifier.of("ledger", "search") + val ACTION = Identifier.of("ledger", "action") val LOGGER: Logger = LogManager.getLogger("LedgerTestmod") override fun onInitializeClient() { From 260bee1f60b871db936c4c0caaa0244bc36687f0 Mon Sep 17 00:00:00 2001 From: Drex Date: Sat, 28 Sep 2024 18:39:03 +0200 Subject: [PATCH 2/2] Fix entity change action extra data --- .../ledger/actions/EntityChangeActionType.kt | 25 +++++++++++++------ .../ledger/actionutils/ActionFactory.kt | 2 +- .../ledger/utility/NbtUtils.kt | 2 +- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/com/github/quiltservertools/ledger/actions/EntityChangeActionType.kt b/src/main/kotlin/com/github/quiltservertools/ledger/actions/EntityChangeActionType.kt index 5aa20e77..360f7e15 100644 --- a/src/main/kotlin/com/github/quiltservertools/ledger/actions/EntityChangeActionType.kt +++ b/src/main/kotlin/com/github/quiltservertools/ledger/actions/EntityChangeActionType.kt @@ -4,6 +4,7 @@ import com.github.quiltservertools.ledger.utility.TextColorPallet import com.github.quiltservertools.ledger.utility.UUID import com.github.quiltservertools.ledger.utility.getWorld import com.github.quiltservertools.ledger.utility.literal +import com.mojang.brigadier.exceptions.CommandSyntaxException import net.minecraft.entity.LivingEntity import net.minecraft.entity.decoration.AbstractDecorationEntity import net.minecraft.entity.decoration.ItemFrameEntity @@ -11,6 +12,7 @@ import net.minecraft.item.AliasedBlockItem import net.minecraft.item.BlockItem import net.minecraft.item.ItemStack import net.minecraft.nbt.StringNbtReader +import net.minecraft.registry.DynamicRegistryManager import net.minecraft.registry.Registries import net.minecraft.server.MinecraftServer import net.minecraft.server.command.ServerCommandSource @@ -23,7 +25,7 @@ class EntityChangeActionType : AbstractActionType() { override val identifier = "entity-change" override fun getTranslationType(): String { - val item = Registries.ITEM.get(Identifier.of(extraData)) + val item = getStack(DynamicRegistryManager.EMPTY).item return if (item is BlockItem && item !is AliasedBlockItem) { "block" } else { @@ -31,6 +33,18 @@ class EntityChangeActionType : AbstractActionType() { } } + private fun getStack(registryManager: DynamicRegistryManager): ItemStack { + if (extraData == null) return ItemStack.EMPTY + try { + val itemTag = StringNbtReader.parse(extraData) + return ItemStack.fromNbt(registryManager, itemTag).orElse(ItemStack.EMPTY) + } catch (_: CommandSyntaxException) { + // In an earlier version of ledger extraData only stored the item id + val item = Registries.ITEM.get(Identifier.of(extraData)) + return item.defaultStack + } + } + override fun getObjectMessage(source: ServerCommandSource): Text { val text = Text.literal("") text.append( @@ -49,15 +63,12 @@ class EntityChangeActionType : AbstractActionType() { } ) - if (extraData != null && Identifier.of(extraData) != Identifier.tryParse("minecraft:air")) { - val stack = ItemStack(Registries.ITEM.get(Identifier.of(extraData))) + val stack = getStack(source.registryManager) + if (!stack.isEmpty) { text.append(Text.literal(" ").append(Text.translatable("text.ledger.action_message.with")).append(" ")) text.append( Text.translatable( - Util.createTranslationKey( - this.getTranslationType(), - Identifier.of(extraData) - ) + stack.translationKey ).setStyle(TextColorPallet.secondaryVariant).styled { it.withHoverEvent( HoverEvent( diff --git a/src/main/kotlin/com/github/quiltservertools/ledger/actionutils/ActionFactory.kt b/src/main/kotlin/com/github/quiltservertools/ledger/actionutils/ActionFactory.kt index 212b8fbe..a5af6aad 100644 --- a/src/main/kotlin/com/github/quiltservertools/ledger/actionutils/ActionFactory.kt +++ b/src/main/kotlin/com/github/quiltservertools/ledger/actionutils/ActionFactory.kt @@ -248,7 +248,7 @@ object ActionFactory { action.oldObjectIdentifier = Registries.ENTITY_TYPE.getId(entity.type) if (itemStack != null) { - action.extraData = Registries.ITEM.getId(itemStack.item).toString() + action.extraData = itemStack.encode(world.registryManager)?.asString() } action.oldObjectState = oldEntityTags.asString() action.objectState = entity.writeNbt(NbtCompound())?.asString() diff --git a/src/main/kotlin/com/github/quiltservertools/ledger/utility/NbtUtils.kt b/src/main/kotlin/com/github/quiltservertools/ledger/utility/NbtUtils.kt index 346139f5..e0ba58be 100644 --- a/src/main/kotlin/com/github/quiltservertools/ledger/utility/NbtUtils.kt +++ b/src/main/kotlin/com/github/quiltservertools/ledger/utility/NbtUtils.kt @@ -60,6 +60,6 @@ object NbtUtils { ).cast(NbtOps.INSTANCE) as NbtCompound? } - return ItemStack.fromNbt(registries, itemTag).get() + return ItemStack.fromNbt(registries, itemTag).orElse(ItemStack.EMPTY) } }