Add support for insertion text in RichMessage

This commit is contained in:
Felix Cravic 2020-08-01 01:00:53 +02:00
parent fe73c43eef
commit f95feeaaf1
2 changed files with 34 additions and 5 deletions

View File

@ -295,6 +295,8 @@ public class PlayerInit {
RichMessage richMessage = RichMessage.of(ColoredText.of(ChatColor.RED + "test item")); RichMessage richMessage = RichMessage.of(ColoredText.of(ChatColor.RED + "test item"));
richMessage.setHoverEvent(ChatHoverEvent.showItem(new ItemStack(Material.DIAMOND, (byte) 1))); richMessage.setHoverEvent(ChatHoverEvent.showItem(new ItemStack(Material.DIAMOND, (byte) 1)));
richMessage.setInsertion("Test Insert");
System.out.println(richMessage.toString());
player.sendMessage(richMessage); player.sendMessage(richMessage);
//EntityBoat entityBoat = new EntityBoat(player.getPosition()); //EntityBoat entityBoat = new EntityBoat(player.getPosition());

View File

@ -63,6 +63,19 @@ public class RichMessage {
return this; return this;
} }
/**
* Set the insertion string of the current rich component
*
* @param insertion the string to insert in the chat box
* @return the rich message
*/
public RichMessage setInsertion(String insertion) {
Check.notNull(insertion, "the insertion cannot be null");
currentComponent.setInsertion(insertion);
return this;
}
/** /**
* Add a new rich component to the message * Add a new rich component to the message
* *
@ -142,14 +155,15 @@ public class RichMessage {
* @return a list of processed components * @return a list of processed components
*/ */
private List<JsonObject> getComponentObject(RichComponent component) { private List<JsonObject> getComponentObject(RichComponent component) {
ColoredText coloredText = component.getText(); final ColoredText coloredText = component.getText();
List<JsonObject> componentObjects = coloredText.getComponents(); final List<JsonObject> componentObjects = coloredText.getComponents();
ChatClickEvent clickEvent = component.getClickEvent(); final ChatClickEvent clickEvent = component.getClickEvent();
ChatHoverEvent hoverEvent = component.getHoverEvent(); final ChatHoverEvent hoverEvent = component.getHoverEvent();
final String insertion = component.getInsertion();
// Nothing to process // Nothing to process
if (clickEvent == null && hoverEvent == null) { if (clickEvent == null && hoverEvent == null && insertion == null) {
return componentObjects; return componentObjects;
} }
@ -175,6 +189,10 @@ public class RichMessage {
} }
componentObject.add("hoverEvent", hoverObject); componentObject.add("hoverEvent", hoverObject);
} }
// Add insertion if any
if (insertion != null) {
componentObject.addProperty("insertion", insertion);
}
} }
return componentObjects; return componentObjects;
@ -200,6 +218,7 @@ public class RichMessage {
private FormatRetention formatRetention; private FormatRetention formatRetention;
private ChatClickEvent clickEvent; private ChatClickEvent clickEvent;
private ChatHoverEvent hoverEvent; private ChatHoverEvent hoverEvent;
private String insertion;
private RichComponent(ColoredText text, FormatRetention formatRetention) { private RichComponent(ColoredText text, FormatRetention formatRetention) {
this.text = text; this.text = text;
@ -229,6 +248,14 @@ public class RichMessage {
public void setHoverEvent(ChatHoverEvent hoverEvent) { public void setHoverEvent(ChatHoverEvent hoverEvent) {
this.hoverEvent = hoverEvent; this.hoverEvent = hoverEvent;
} }
public String getInsertion() {
return insertion;
}
public void setInsertion(String insertion) {
this.insertion = insertion;
}
} }
} }