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

Legacy support & Minimessage configuration options #80

Closed
wants to merge 7 commits into from

Conversation

MaksyKun
Copy link

@MaksyKun MaksyKun commented Aug 24, 2024

Related to #78:

  • Transform the old legacy color codes into proper minimessages
  • Also does the same with links without - component
  • New Permissions for further customization:

Click-Components

  • gpflags.messages.click.suggest_command - Allows the suggest_command value of click-component
  • gpflags.messages.click.run_command - Allows the run_command value of click-component
  • gpflags.messages.click.run_command - Allows the run_command value of click-component

Hover-Components

  • gpflags.messages.hover.show_text - Allows the show_textvalue of hover-component
  • gpflags.messages.hover.show_item - Allows the show_itemvalue of hover-component
  • gpflags.messages.hover.show_entity - Allows the show_entityvalue of hover-component

Others

  • gpflags.messages.hex-colors - Allows the use of hex-colors in minimessages
  • gpflags.messages.colors - Allows the use of default colors (former legacy codes) in minimessages
  • [EDIT] gpflags.messages.style.bold - Allows usage of bold messages
  • [EDIT] gpflags.messages.style.underlined - Allows usage of underlinedmessages
  • [EDIT] gpflags.messages.style.italic - Allows usage of italicmessages
  • [EDIT] gpflags.messages.style.strikethrough - Allows usage of strikethrough messages
  • [EDIT] gpflags.messages.style.obfuscated - Allows usage of obfuscated messages
  • [EDIT] gpflags.messages.links : Allow using legacy links without <click:open_url:...>

Copy link
Owner

@akdukaan akdukaan left a comment

Choose a reason for hiding this comment

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

https://docs.advntr.dev/minimessage/api.html#builder
The docs talked about a builder where we can specify exactly which tags we want it to be able to resolve. It sounds like we might be able to use that for the minimessage configuration options part. Could be good to use that if minimessage keeps expanding its api so its easy for me to keep GPF with all the latest features, but I also couldn't really figure out if it could actually be used in the way we want here. Feel free to look into it if you want but its also fine to ignore I think.

src/main/java/me/ryanhamshire/GPFlags/util/ChatUtil.java Outdated Show resolved Hide resolved
src/main/java/me/ryanhamshire/GPFlags/util/ChatUtil.java Outdated Show resolved Hide resolved
src/main/java/me/ryanhamshire/GPFlags/util/ChatUtil.java Outdated Show resolved Hide resolved
src/main/java/me/ryanhamshire/GPFlags/util/ChatUtil.java Outdated Show resolved Hide resolved
src/main/java/me/ryanhamshire/GPFlags/util/ChatUtil.java Outdated Show resolved Hide resolved
}
matcher = pattern.matcher(message);

if (!requiredChanges) {
Copy link
Owner

Choose a reason for hiding this comment

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

I can't tell what this variable is trying to do in this one. If I don't have permission for a click action I tried to use, it removes my click component, but leaves all the other click components in the message?

Copy link
Author

Choose a reason for hiding this comment

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

The problem was, when you find something in the pattern, but have the permission to use it, there is no need for change. But if there is no need for change, the while loop will keep iterating over and over until the server runs out of memory.

Copy link
Author

Choose a reason for hiding this comment

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

But we could make the permission check before the actually pattern initialization. However it could become annoying with the click and hover components. What do you think about this?

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, I think it makes sense for perm check before pattern initialization. What makes it annoying with click and hover components? That there are multiple types of them? Thats fine, just split it up more

if (!sender.hasPermission("gpflags.messages.hover.show_text")) {
// replace all "<hover:show_text>" with "\<hover:show_text>"
}
if (!sender.hasPermission("gpflags.messages.hover.show_item")) {
// replace all "<hover:show_item>" with "\<hover:show_item>"
}
...

As you see in my example above, I also chose to escape out the tag rather than repalcing it with "".
I think if you choose to replace it with "", it could result in some cases where for example <re<blue>d> would turn into <red> which we want to avoid.

Copy link
Author

@MaksyKun MaksyKun Aug 25, 2024

Choose a reason for hiding this comment

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

Dont get this, Im sorry.

Do you want to put a \ before the hover:.... stuff? I think this would still setup the component itself. Also, we are having a while loop that repeats until there is no component left. So i think thats not bad at all atm.

All i could imagine to reduce iteration time:

if(!sender.hasPermission("gpflags.messages.hover.show_text") || !sender.hasPermission("gpflags.messages.hover.show_item") || !sender.hasPermission("gpflags.messages.hover.show_entity")) {
        pattern = Pattern.compile("<hover:.*?>");
        matcher = pattern.matcher(message);
        while (matcher.find()) {
            boolean requiredChanges = false;

            String hoverComponent = message.substring(matcher.start(), matcher.end());
            if (hoverComponent.contains("show_text") && !sender.hasPermission("gpflags.messages.hover.show_text")) {
                message = message.replace(hoverComponent, "");
                requiredChanges = true;
            }
            if (hoverComponent.contains("show_item") && !sender.hasPermission("gpflags.messages.hover.show_item")) {
                message = message.replace(hoverComponent, "");
                requiredChanges = true;
            }
            if (hoverComponent.contains("show_entity") && !sender.hasPermission("gpflags.messages.hover.show_entity")) {
                message = message.replace(hoverComponent, "");
                requiredChanges = true;
            }
            matcher = pattern.matcher(message);

            if (!requiredChanges) {
                break;
            }
        }
}

Copy link
Owner

Choose a reason for hiding this comment

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

Do you want to put a \ before the hover:.... stuff? I think this would still setup the component itself.

Yes that's exactly what Im saying. You can use the minimessage viewer at https://webui.advntr.dev/ to see that this does make the tag show without it serving as a tag.

Copy link
Owner

Choose a reason for hiding this comment

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

Also, don't worry much about reducing iteration time here. This won't get called so often to where it's too big a consideration so I'd much rather it be easy to read and maintain. I still think three individual loops for the hover would be the best, and similar for the other parts

src/main/java/me/ryanhamshire/GPFlags/util/ChatUtil.java Outdated Show resolved Hide resolved
@MaksyKun MaksyKun marked this pull request as draft August 25, 2024 10:40
@MaksyKun
Copy link
Author

MaksyKun commented Aug 25, 2024

https://docs.advntr.dev/minimessage/api.html#builder The docs talked about a builder where we can specify exactly which tags we want it to be able to resolve. It sounds like we might be able to use that for the minimessage configuration options part. Could be good to use that if minimessage keeps expanding its api so its easy for me to keep GPF with all the latest features, but I also couldn't really figure out if it could actually be used in the way we want here. Feel free to look into it if you want but its also fine to ignore I think.

Im sure this is powerful and its worth looking into it someday. However, Im not capable atm to hook into it so i propose to keep it with the method in ChatUtil yet. It might be something for a v2 feature and should not take too much time to refactor it once on it.

From my side, the requested changes shall be done now. Feel free to request new request if you are missing anything else related to the existing code.

@MaksyKun MaksyKun marked this pull request as ready for review August 25, 2024 12:30
@MaksyKun MaksyKun requested a review from akdukaan August 25, 2024 12:31
}
matcher = pattern.matcher(message);

if (!requiredChanges) {
Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, I think it makes sense for perm check before pattern initialization. What makes it annoying with click and hover components? That there are multiple types of them? Thats fine, just split it up more

if (!sender.hasPermission("gpflags.messages.hover.show_text")) {
// replace all "<hover:show_text>" with "\<hover:show_text>"
}
if (!sender.hasPermission("gpflags.messages.hover.show_item")) {
// replace all "<hover:show_item>" with "\<hover:show_item>"
}
...

As you see in my example above, I also chose to escape out the tag rather than repalcing it with "".
I think if you choose to replace it with "", it could result in some cases where for example <re<blue>d> would turn into <red> which we want to avoid.

src/main/java/me/ryanhamshire/GPFlags/util/ChatUtil.java Outdated Show resolved Hide resolved
@MaksyKun MaksyKun requested a review from akdukaan August 25, 2024 19:18
}

// Check for bold, italic, strikethrough, underline, obfuscated
pattern = Pattern.compile("<(bold|italic|strikethrough|underlined|obfuscated)>");
Copy link
Owner

@akdukaan akdukaan Aug 27, 2024

Choose a reason for hiding this comment

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

minimessage tags are case insensitive so this wont catch stuff that are like <BOLD> or <bOLD>

}

// Check for hex-colors
message = message.replace("<#", "<color:#");
Copy link
Owner

Choose a reason for hiding this comment

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

If someone's using <# not as a tag, we wouldn't really want to convert this. Cant really imagine someone would do that but it's short enough a string to where I could see it coming up.

while (matcher.find()) {
String link = pseudoMessage.substring(matcher.start(), matcher.end());
pseudoMessage = pseudoMessage.replace(link, "");
if(message.contains("<click:open_url:" + link + ">")) continue; // Skip if already a click-component
Copy link
Owner

Choose a reason for hiding this comment

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

What if there are two of the same links in the message? This'll only convert the first one

Copy link
Author

Choose a reason for hiding this comment

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

Not anymore, same iteration as everywhere until there is no link available.

Copy link
Owner

@akdukaan akdukaan left a comment

Choose a reason for hiding this comment

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

I added some comments. The more I look and think about this, the more I think using the Minimessage Builder API is going to be the best way around this. There's just so many edge cases that keep coming up and it feels like as soon as I'd release this, we'd find so many more being reported.

@MaksyKun
Copy link
Author

MaksyKun commented Aug 27, 2024

I added some comments. The more I look and think about this, the more I think using the Minimessage Builder API is going to be the best way around this. There's just so many edge cases that keep coming up and it feels like as soon as I'd release this, we'd find so many more being reported.

In that case, lets make a compromise: Since our server requires this change now, I will use this version I created locally and make tests if it works out.

If I get some more time for hooking into the Builder, I will provide you a solution with it instead. Until then, I put this PR on draft. Cant tell a specific time where I look into it now.

@MaksyKun MaksyKun marked this pull request as draft August 27, 2024 09:12
@MaksyKun MaksyKun closed this Sep 22, 2024
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