Skip to content

Commit

Permalink
better live chat message example
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNature committed Nov 27, 2024
1 parent 89eae71 commit 0e05008
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,46 @@
package com.lunarclient.apollo.example.api.examples;

import com.lunarclient.apollo.Apollo;
import com.lunarclient.apollo.example.ApolloExamplePlugin;
import com.lunarclient.apollo.example.common.modules.impl.ChatExample;
import com.lunarclient.apollo.module.chat.ChatModule;
import com.lunarclient.apollo.recipients.Recipients;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.scheduler.BukkitRunnable;

public class ChatApiExample extends ChatExample {

private final ChatModule chatModule = Apollo.getModuleManager().getModule(ChatModule.class);

private int countdown = 5;

@Override
public void displayLiveChatMessageExample() {
this.chatModule.displayLiveChatMessage(Recipients.ofEveryone(),
Component.text("Game starting in ", NamedTextColor.GREEN)
.append(Component.text(this.countdown, NamedTextColor.BLUE)),
13
);

if (--this.countdown == 0) {
this.countdown = 5;
}
BukkitRunnable runnable = new BukkitRunnable() {

private int countdown = 5;

@Override
public void run() {
if (this.countdown > 0) {
ChatApiExample.this.chatModule.displayLiveChatMessage(Recipients.ofEveryone(),
Component.text("Game starting in ", NamedTextColor.GREEN)
.append(Component.text(this.countdown, NamedTextColor.BLUE)),
13
);

this.countdown--;
} else {
ChatApiExample.this.chatModule.displayLiveChatMessage(Recipients.ofEveryone(),
Component.text("Game started! ", NamedTextColor.GREEN),
13
);

this.cancel();
}
}
};

runnable.runTaskTimer(ApolloExamplePlugin.getPlugin(), 0L, 20L);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,48 @@
package com.lunarclient.apollo.example.json.examples;

import com.google.gson.JsonObject;
import com.lunarclient.apollo.example.ApolloExamplePlugin;
import com.lunarclient.apollo.example.common.modules.impl.ChatExample;
import com.lunarclient.apollo.example.json.AdventureUtil;
import com.lunarclient.apollo.example.json.JsonPacketUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.scheduler.BukkitRunnable;

public class ChatJsonExample extends ChatExample {

private int countdown = 5;

@Override
public void displayLiveChatMessageExample() {
JsonObject message = new JsonObject();
message.addProperty("@type", "type.googleapis.com/lunarclient.apollo.chat.v1.DisplayLiveChatMessageMessage");
message.addProperty("message_id", 13);
message.addProperty("adventure_json_lines", AdventureUtil.toJson(
Component.text("Game starting in ", NamedTextColor.GREEN)
.append(Component.text(this.countdown, NamedTextColor.BLUE))
));
BukkitRunnable runnable = new BukkitRunnable() {

if (--this.countdown == 0) {
this.countdown = 5;
}
private int countdown = 5;

JsonPacketUtil.broadcastPacket(message);
@Override
public void run() {
JsonObject message = new JsonObject();
message.addProperty("@type", "type.googleapis.com/lunarclient.apollo.chat.v1.DisplayLiveChatMessageMessage");
message.addProperty("message_id", 13);

if (this.countdown > 0) {
message.addProperty("adventure_json_lines", AdventureUtil.toJson(
Component.text("Game starting in ", NamedTextColor.GREEN)
.append(Component.text(this.countdown, NamedTextColor.BLUE))
));

JsonPacketUtil.broadcastPacket(message);
this.countdown--;
} else {
message.addProperty("adventure_json_lines", AdventureUtil.toJson(
Component.text("Game started! ", NamedTextColor.GREEN)
));

JsonPacketUtil.broadcastPacket(message);
this.cancel();
}
}
};

runnable.runTaskTimer(ApolloExamplePlugin.getPlugin(), 0L, 20L);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,47 @@

import com.lunarclient.apollo.chat.v1.DisplayLiveChatMessageMessage;
import com.lunarclient.apollo.chat.v1.RemoveLiveChatMessageMessage;
import com.lunarclient.apollo.example.ApolloExamplePlugin;
import com.lunarclient.apollo.example.common.modules.impl.ChatExample;
import com.lunarclient.apollo.example.proto.AdventureUtil;
import com.lunarclient.apollo.example.proto.ProtobufPacketUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.scheduler.BukkitRunnable;

public class ChatProtoExample extends ChatExample {

private int countdown = 5;

@Override
public void displayLiveChatMessageExample() {
DisplayLiveChatMessageMessage message = DisplayLiveChatMessageMessage.newBuilder()
.setAdventureJsonLines(AdventureUtil.toJson(
Component.text("Game starting in ", NamedTextColor.GREEN)
.append(Component.text(this.countdown, NamedTextColor.BLUE)))
)
.setMessageId(13)
.build();
BukkitRunnable runnable = new BukkitRunnable() {

if (--this.countdown == 0) {
this.countdown = 5;
}
private int countdown = 5;

ProtobufPacketUtil.broadcastPacket(message);
@Override
public void run() {
DisplayLiveChatMessageMessage.Builder builder = DisplayLiveChatMessageMessage.newBuilder()
.setMessageId(13);

if (this.countdown > 0) {
builder.setAdventureJsonLines(AdventureUtil.toJson(
Component.text("Game starting in ", NamedTextColor.GREEN)
.append(Component.text(this.countdown, NamedTextColor.BLUE)))
);

ProtobufPacketUtil.broadcastPacket(builder.build());
this.countdown--;
} else {
builder.setAdventureJsonLines(AdventureUtil.toJson(
Component.text("Game started! ", NamedTextColor.GREEN))
);

ProtobufPacketUtil.broadcastPacket(builder.build());
this.cancel();
}
}
};

runnable.runTaskTimer(ApolloExamplePlugin.getPlugin(), 0L, 20L);
}

@Override
Expand Down
126 changes: 85 additions & 41 deletions docs/developers/modules/chat.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,31 @@ Explore each integration by cycling through each tab, to find the best fit for y
### Displaying a Live Chat Message

```java
private int countdown = 5;

public void displayLiveChatMessageExample() {
this.chatModule.displayLiveChatMessage(Recipients.ofEveryone(),
Component.text("Game starting in ", NamedTextColor.GREEN)
.append(Component.text(this.countdown, NamedTextColor.BLUE)),
13
);

if (--this.countdown == 0) {
this.countdown = 5;
}
BukkitRunnable runnable = new BukkitRunnable() {

private int countdown = 5;

@Override
public void run() {
ChatApiExample.this.chatModule.displayLiveChatMessage(Recipients.ofEveryone(),
Component.text("Game starting in ", NamedTextColor.GREEN)
.append(Component.text(this.countdown, NamedTextColor.BLUE)),
13
);

if (--this.countdown == 0) {
ChatApiExample.this.chatModule.displayLiveChatMessage(Recipients.ofEveryone(),
Component.text("Game started! ", NamedTextColor.GREEN),
13
);

this.cancel();
}
}
};

runnable.runTaskTimer(ApolloExamplePlugin.getPlugin(), 0L, 20L);
}
```

Expand All @@ -57,22 +70,37 @@ public void removeLiveChatMessageExample() {
### Displaying a Live Chat Message

```java
private int countdown = 5;

@Override
public void displayLiveChatMessageExample() {
DisplayLiveChatMessageMessage message = DisplayLiveChatMessageMessage.newBuilder()
.setAdventureJsonLines(AdventureUtil.toJson(
Component.text("Game starting in ", NamedTextColor.GREEN)
.append(Component.text(this.countdown, NamedTextColor.BLUE)))
)
.setMessageId(13)
.build();

if (--this.countdown == 0) {
this.countdown = 5;
}

ProtobufPacketUtil.broadcastPacket(message);
BukkitRunnable runnable = new BukkitRunnable() {

private int countdown = 5;

@Override
public void run() {
DisplayLiveChatMessageMessage.Builder builder = DisplayLiveChatMessageMessage.newBuilder()
.setMessageId(13);

if (this.countdown > 0) {
builder.setAdventureJsonLines(AdventureUtil.toJson(
Component.text("Game starting in ", NamedTextColor.GREEN)
.append(Component.text(this.countdown, NamedTextColor.BLUE)))
);

ProtobufPacketUtil.broadcastPacket(builder.build());
this.countdown--;
} else {
builder.setAdventureJsonLines(AdventureUtil.toJson(
Component.text("Game started! ", NamedTextColor.GREEN))
);

ProtobufPacketUtil.broadcastPacket(builder.build());
this.cancel();
}
}
};

runnable.runTaskTimer(ApolloExamplePlugin.getPlugin(), 0L, 20L);
}
```

Expand All @@ -95,22 +123,38 @@ public void removeLiveChatMessageExample() {
### Displaying a Live Chat Message

```java
private int countdown = 5;

@Override
public void displayLiveChatMessageExample() {
JsonObject message = new JsonObject();
message.addProperty("@type", "type.googleapis.com/lunarclient.apollo.chat.v1.DisplayLiveChatMessageMessage");
message.addProperty("message_id", 13);
message.addProperty("adventure_json_lines", AdventureUtil.toJson(
Component.text("Game starting in ", NamedTextColor.GREEN)
.append(Component.text(this.countdown, NamedTextColor.BLUE))
));

if (--this.countdown == 0) {
this.countdown = 5;
}

JsonPacketUtil.broadcastPacket(message);
BukkitRunnable runnable = new BukkitRunnable() {

private int countdown = 5;

@Override
public void run() {
JsonObject message = new JsonObject();
message.addProperty("@type", "type.googleapis.com/lunarclient.apollo.chat.v1.DisplayLiveChatMessageMessage");
message.addProperty("message_id", 13);

if (this.countdown > 0) {
message.addProperty("adventure_json_lines", AdventureUtil.toJson(
Component.text("Game starting in ", NamedTextColor.GREEN)
.append(Component.text(this.countdown, NamedTextColor.BLUE))
));

JsonPacketUtil.broadcastPacket(message);
this.countdown--;
} else {
message.addProperty("adventure_json_lines", AdventureUtil.toJson(
Component.text("Game started! ", NamedTextColor.GREEN)
));

JsonPacketUtil.broadcastPacket(message);
this.cancel();
}
}
};

runnable.runTaskTimer(ApolloExamplePlugin.getPlugin(), 0L, 20L);
}
```

Expand Down

1 comment on commit 0e05008

@LunarClientBot
Copy link
Collaborator

@LunarClientBot LunarClientBot commented on 0e05008 Nov 27, 2024

Choose a reason for hiding this comment

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

📄 Documentation Deployment

Status:✅ Completed
Environment:preview
URL:https://c02ad92f.lunarclient-dev.pages.dev

Please sign in to comment.