Skip to content

Conversation

@huanmeng-qwq
Copy link
Contributor

@huanmeng-qwq huanmeng-qwq commented Sep 1, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Resolved an issue where entity metadata could be unintentionally modified during packet rendering, preventing inconsistent visuals and state leaks.
  • Refactor
    • Improved metadata handling by isolating processing to a safe copy, enhancing stability without altering behavior.

@coderabbitai
Copy link

coderabbitai bot commented Sep 1, 2025

Walkthrough

Introduces a defensive copy of entity metadata in PacketUtil.renderPacket by duplicating each EntityData into a new ArrayList and setting it on the metadata before translation/mutation. Subsequent processing modifies the copied list rather than the original. No public APIs or external behavior are changed.

Changes

Cohort / File(s) Summary of Changes
Metadata defensive copy in renderPacket
api/src/main/java/me/tofaa/entitylib/utils/PacketUtil.java
Added import for java.util.ArrayList. In renderPacket, duplicates metadata.getEntityMetadata() into a new ArrayList<EntityData<?>> via element-wise new EntityData(index, type, value). Replaces original list via metadata.setEntityMetadata(copiedEntityData). Subsequent translation/mutations operate on the copy.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Caller
  participant PacketUtil
  participant Metadata
  participant Translator

  Caller->>PacketUtil: renderPacket(packet, metadata, ...)

  rect rgba(230,240,255,0.5)
    note over PacketUtil: Create defensive copy of EntityData list
    PacketUtil->>Metadata: getEntityMetadata()
    PacketUtil->>PacketUtil: copy each EntityData(index,type,value)
    PacketUtil->>Metadata: setEntityMetadata(copiedList)
  end

  rect rgba(240,255,240,0.5)
    note over PacketUtil,Translator: Translate/mutate ADV_COMPONENT fields on copied list
    PacketUtil->>Translator: translate(copiedList)
    Translator-->>PacketUtil: translated list
  end

  PacketUtil-->>Caller: return processed packet/metadata
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I nibbled bytes and made a twin,
A mirror list to work within.
No footprints left on fields before—
I tweak the copy, nothing more.
Packets prance; the carrots stay—
Safe metadata hops away! 🥕🐇

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
api/src/main/java/me/tofaa/entitylib/utils/PacketUtil.java (3)

19-23: Add null/empty guard and pre-size the copy; program to List.

Avoid NPE when metadata has no entries, and trim allocations by pre-sizing. Also reduce coupling by using the List interface.

-        final ArrayList<EntityData<?>> copiedEntityData = new ArrayList<>();
-        metadata.getEntityMetadata().forEach(entityData ->
-                copiedEntityData.add(new EntityData(entityData.getIndex(), entityData.getType(), entityData.getValue()))
-        );
-        metadata.setEntityMetadata(copiedEntityData);
+        final java.util.List<EntityData<?>> original = metadata.getEntityMetadata();
+        if (original == null || original.isEmpty()) {
+            return;
+        }
+        final java.util.List<EntityData<?>> copiedEntityData = new ArrayList<>(original.size());
+        original.forEach(entityData ->
+                copiedEntityData.add(new EntityData(entityData.getIndex(), entityData.getType(), entityData.getValue()))
+        );
+        metadata.setEntityMetadata(copiedEntityData);

21-21: Nit: avoid raw type constructor.

If it type-checks in your target Java/Packetevents versions, prefer the diamond to suppress raw-type warnings:

-                copiedEntityData.add(new EntityData(entityData.getIndex(), entityData.getType(), entityData.getValue()))
+                copiedEntityData.add(new EntityData<>(entityData.getIndex(), entityData.getType(), entityData.getValue()))

7-7: Import List if you adopt the List-typed locals.

Add:

+import java.util.List;
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 598d71f and 5add72f.

📒 Files selected for processing (1)
  • api/src/main/java/me/tofaa/entitylib/utils/PacketUtil.java (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build
🔇 Additional comments (1)
api/src/main/java/me/tofaa/entitylib/utils/PacketUtil.java (1)

19-23: Defensive copy prevents original metadata mutations — good fix.

Replacing the metadata list with a freshly copied list of EntityData objects ensures downstream rendering doesn’t overwrite the caller’s originals. This directly addresses the PR’s objective.

@Tofaa2 Tofaa2 merged commit cef23ea into Tofaa2:master Sep 1, 2025
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants