Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] New decoding & encoding system #287

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

seailz
Copy link
Member

@seailz seailz commented Mar 30, 2024

PR Details

  • I have checked the PRs for upcoming features/bug fixes.

What you've changed

  • Developer-facing library
  • Internal Code
  • Documentation

Closes #0

Description

Added a new decoding and encoding system for discord.jar. This system is designed for better:

  • Readability
  • Consistency
  • DRY

The main difference here is the removal of existing #decompile and #compile methods. These are replaced by a system built with Java Reflection to prevent repeated code and errors.

Documentation

When creating an object, you must implement the DiscordObject interface. Then, for each of your fields, you must annotate them with @DiscordObjectParameter (you can customize the values to your liking). If you want one of your fields to automatically be filled with the DiscordJar instance, you can annotate it with @DiscordJarParameter, alongside the previous annotation. You must have a constructor annotated with @DiscordObjectConstructor with the same types as the fields, in the same order.

To assign custom values to a key, you may create a static method that takes a JSONObject and returns a HashMap<String, Object> that's annotated with @DiscordObjectCustomAssignations.

To decompile a JSON payload, use
discordJar.getParser().decompileObject(jsonObject, ExampleObject.class);

An example object might look like this, meaning there's no need for a decompile or compile method:

@ToString
public class PizzaObject implements DiscordObject {

    @Getter
    @DiscordObjectParameter(excludable = false, nullable = false)
    private final String name;

    @Getter
    @DiscordObjectParameter(excludable = false, nullable = false)
    private final int size;

    @DiscordObjectParameter(excludable = false, nullable = false)
    private final String[] labels;
    
    @DiscordObjectConstructor
    public PizzaObject(String name, int size, String[] labels) {
        this.name = name;
        this.size = size;
        this.labels = labels;
    }

    @DiscordObjectCustomAssignationsMethod
    public static HashMap<String, Object> customAssignations(JSONObject data) {
        return new HashMap<>(){{
            put("name", "hi im a pizza :)");
        }};
    }
}

Status

  • Create decompile system
  • Implement compile method
  • Implement into all existing model objects
  • HashMap implementation
  • Simple array decomplication

@seailz seailz added status: in progress PR or issue is currently in progress type: feature Issue or PR is a feature that should be implemented priority: high High priority LARGE PR or issue with a large amount of additions IMPORTANT Important PR or issue that is important to be added/implemented soon. labels Mar 30, 2024
@seailz seailz added this to the discord.jar 1.0 milestone Mar 30, 2024
@seailz seailz self-assigned this Mar 30, 2024
Copy link

Qodana Community for JVM

1 new problem were found

Inspection name Severity Problems
Unused assignment 🔶 Warning 1

💡 Qodana analysis was run in the pull request mode: only the changed files were checked
☁️ View the detailed Qodana report

Contact Qodana team

Contact us at [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IMPORTANT Important PR or issue that is important to be added/implemented soon. LARGE PR or issue with a large amount of additions priority: high High priority status: in progress PR or issue is currently in progress type: feature Issue or PR is a feature that should be implemented
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant