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

Add support for keybind components #5894

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/main/java/ch/njol/skript/util/chat/BungeeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.KeybindComponent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.TranslatableComponent;

Expand All @@ -42,11 +43,10 @@ public static BaseComponent convert(MessageComponent origin) {
if (origin.translation != null) {
String[] strings = origin.translation.split(":");
String key = strings[0];
if (strings.length > 1) {
base = new TranslatableComponent(key, Arrays.copyOfRange(strings, 1, strings.length, Object[].class));
} else {
base = new TranslatableComponent(key);
}
base = new TranslatableComponent(key, Arrays.copyOfRange(strings, 1, strings.length, Object[].class));
base.addExtra(new TextComponent(origin.text));
} else if (origin.keybind != null) {
base = new KeybindComponent(origin.keybind);
base.addExtra(new TextComponent(origin.text));
} else {
base = new TextComponent(origin.text);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/ch/njol/skript/util/chat/ChatMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,8 @@ public static void copyStyles(MessageComponent from, MessageComponent to) {
to.insertion = from.insertion;
if (to.hoverEvent == null)
to.hoverEvent = from.hoverEvent;
if (to.font == null)
to.font = from.font;
}

public static void shareStyles(MessageComponent[] components) {
Expand Down Expand Up @@ -591,6 +593,10 @@ public static String stripStyles(String text) {
List<MessageComponent> components = parse(result);
StringBuilder builder = new StringBuilder();
for (MessageComponent component : components) { // This also strips bracket tags ex. <red> <ttp:..> etc.
if (component.translation != null)
builder.append(component.translation);
if (component.keybind != null)
builder.append(component.keybind);
builder.append(component.text);
}
String plain = builder.toString();
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/ch/njol/skript/util/chat/MessageComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public class MessageComponent {

@Nullable
public String translation;


@Nullable
public String keybind;

public static class ClickEvent {
public ClickEvent(ClickEvent.Action action, String value) {
this.action = action;
Expand Down Expand Up @@ -176,6 +179,8 @@ public MessageComponent copy() {
messageComponent.clickEvent = this.clickEvent;
messageComponent.font = this.font;
messageComponent.hoverEvent = this.hoverEvent;
messageComponent.translation = translation;
messageComponent.keybind = keybind;
return messageComponent;
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/ch/njol/skript/util/chat/SkriptChatCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ public void updateComponent(MessageComponent component, String param) {
public void updateComponent(MessageComponent component, String param) {
component.translation = param;
}
},

keybind(true) {
@Override
public void updateComponent(MessageComponent component, String param) {
component.keybind = param;
}
};

private boolean hasParam;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ chat styles:
font: font, f
insertion: insertion, insert, ins
translate: translate, tr, lang
keybind: keybind, key

# -- Directions --
directions:
Expand Down