Skip to content

Commit

Permalink
Add support for keybind components (SkriptLang#5894)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderscoreTud authored and Moderocky committed Sep 16, 2023
1 parent 3e63338 commit 8790d6c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
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

0 comments on commit 8790d6c

Please sign in to comment.