Skip to content

Commit 2a0f034

Browse files
committed
add the json & proto examples
1 parent 86da3c1 commit 2a0f034

11 files changed

Lines changed: 268 additions & 26 deletions

File tree

docs/developers/lightweight/json/object-util.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ public static JsonObject createItemStackIconObject(@Nullable String itemName, in
9999
return iconObject;
100100
}
101101

102+
public static JsonObject createResourceLocationIconObject(@NotNull String resourceLocation) {
103+
JsonObject resourceIconObject = new JsonObject();
104+
resourceIconObject.addProperty("resource_location", resourceLocation);
105+
106+
JsonObject iconObject = new JsonObject();
107+
iconObject.add("resource_location", resourceIconObject);
108+
109+
return iconObject;
110+
}
111+
102112
public static JsonObject createSimpleResourceLocationIconObject(@NotNull String resourceLocation, int size) {
103113
JsonObject simpleIconObject = new JsonObject();
104114
simpleIconObject.addProperty("resource_location", resourceLocation);

docs/developers/lightweight/protobuf/object-util.mdx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static Location toBukkitPlayerLocation(JsonObject message) {
9797
Icon-related methods
9898

9999
```java
100-
public static Icon createItemStackIconProto(@Nullable String itemName, int itemId, int customModelData) {
100+
public static ItemStackIcon createItemStackIconProto(@Nullable String itemName, int itemId, int customModelData) {
101101
ItemStackIcon.Builder iconBuilder = ItemStackIcon.newBuilder()
102102
.setItemId(itemId)
103103
.setCustomModelData(customModelData);
@@ -106,21 +106,25 @@ public static Icon createItemStackIconProto(@Nullable String itemName, int itemI
106106
iconBuilder.setItemName(itemName);
107107
}
108108

109-
return Icon.newBuilder().setItemStack(iconBuilder.build()).build();
109+
return iconBuilder.build();
110110
}
111111

112-
public static Icon createSimpleResourceLocationIconProto(String resourceLocation, int size) {
113-
SimpleResourceLocationIcon icon = SimpleResourceLocationIcon.newBuilder()
112+
public static ResourceLocationIcon createResourceLocationIconProto(String resourceLocation) {
113+
return ResourceLocationIcon.newBuilder()
114114
.setResourceLocation(resourceLocation)
115-
.setSize(size)
116115
.build();
116+
}
117117

118-
return Icon.newBuilder().setSimpleResourceLocation(icon).build();
118+
public static SimpleResourceLocationIcon createSimpleResourceLocationIconProto(String resourceLocation, int size) {
119+
return SimpleResourceLocationIcon.newBuilder()
120+
.setResourceLocation(resourceLocation)
121+
.setSize(size)
122+
.build();
119123
}
120124

121-
public static Icon createAdvancedResourceLocationIconProto(String resourceLocation, float width, float height,
125+
public static AdvancedResourceLocationIcon createAdvancedResourceLocationIconProto(String resourceLocation, float width, float height,
122126
float minU, float maxU, float minV, float maxV) {
123-
AdvancedResourceLocationIcon icon = AdvancedResourceLocationIcon.newBuilder()
127+
return AdvancedResourceLocationIcon.newBuilder()
124128
.setResourceLocation(resourceLocation)
125129
.setWidth(width)
126130
.setHeight(height)
@@ -129,7 +133,5 @@ public static Icon createAdvancedResourceLocationIconProto(String resourceLocati
129133
.setMinV(minV)
130134
.setMaxV(maxV)
131135
.build();
132-
133-
return Icon.newBuilder().setAdvancedResourceLocation(icon).build();
134136
}
135137
```

docs/developers/modules/cooldown.mdx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ public void displayCooldownItemExample(Player viewer) {
121121
DisplayCooldownMessage message = DisplayCooldownMessage.newBuilder()
122122
.setName("enderpearl-cooldown")
123123
.setDuration(ProtobufUtil.createDurationProto(Duration.ofSeconds(15)))
124-
.setIcon(ProtobufUtil.createItemStackIconProto("ENDER_PEARL", 0, 0))
124+
.setIcon(Icon.newBuilder()
125+
.setItemStack(ProtobufUtil.createItemStackIconProto("ENDER_PEARL", 0, 0))
126+
.build())
125127
.build();
126128

127129
ProtobufPacketUtil.sendPacket(viewer, message);
@@ -135,7 +137,9 @@ public void displayCooldownResourceExample(Player viewer) {
135137
DisplayCooldownMessage message = DisplayCooldownMessage.newBuilder()
136138
.setName("lunar-cooldown")
137139
.setDuration(ProtobufUtil.createDurationProto(Duration.ofSeconds(15)))
138-
.setIcon(ProtobufUtil.createSimpleResourceLocationIconProto("lunar:logo/logo-200x182.svg", 12))
140+
.setIcon(Icon.newBuilder()
141+
.setSimpleResourceLocation(ProtobufUtil.createSimpleResourceLocationIconProto("lunar:logo/logo-200x182.svg", 12))
142+
.build())
139143
.build();
140144

141145
ProtobufPacketUtil.sendPacket(viewer, message);

example/bukkit/api/src/main/java/com/lunarclient/apollo/example/api/module/ServerLinkApiExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void addServerLinkExample(Player viewer) {
6363
apolloPlayerOpt.ifPresent(apolloPlayer -> {
6464
this.serverLinkModule.addServerLink(apolloPlayer, Lists.newArrayList(
6565
ServerLink.builder()
66-
.url("website")
66+
.id("website")
6767
.displayName(Component.text("Website", NamedTextColor.LIGHT_PURPLE))
6868
.url("https://www.lunarclient.com/")
6969
.build(),

example/bukkit/json/src/main/java/com/lunarclient/apollo/example/json/ApolloJsonExamplePlatform.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.lunarclient.apollo.example.json.module.NotificationJsonExample;
4444
import com.lunarclient.apollo.example.json.module.PayNowJsonExample;
4545
import com.lunarclient.apollo.example.json.module.RichPresenceJsonExample;
46+
import com.lunarclient.apollo.example.json.module.ServerLinkJsonExample;
4647
import com.lunarclient.apollo.example.json.module.ServerRuleJsonExample;
4748
import com.lunarclient.apollo.example.json.module.StaffModJsonExample;
4849
import com.lunarclient.apollo.example.json.module.StopwatchJsonExample;
@@ -85,6 +86,7 @@ public void registerModuleExamples() {
8586
this.setNotificationExample(new NotificationJsonExample());
8687
this.setPayNowExample(new PayNowJsonExample());
8788
this.setRichPresenceExample(new RichPresenceJsonExample());
89+
this.setServerLinkExample(new ServerLinkJsonExample());
8890
this.setServerRuleExample(new ServerRuleJsonExample());
8991
this.setStaffModExample(new StaffModJsonExample());
9092
this.setStopwatchExample(new StopwatchJsonExample());
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* This file is part of Apollo, licensed under the MIT License.
3+
*
4+
* Copyright (c) 2023 Moonsworth
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.lunarclient.apollo.example.json.module;
25+
26+
import com.google.common.collect.Lists;
27+
import com.google.gson.JsonArray;
28+
import com.google.gson.JsonObject;
29+
import com.lunarclient.apollo.example.json.util.AdventureUtil;
30+
import com.lunarclient.apollo.example.json.util.JsonPacketUtil;
31+
import com.lunarclient.apollo.example.json.util.JsonUtil;
32+
import com.lunarclient.apollo.example.module.impl.ServerLinkExample;
33+
import net.kyori.adventure.text.Component;
34+
import net.kyori.adventure.text.format.NamedTextColor;
35+
import org.bukkit.entity.Player;
36+
37+
public class ServerLinkJsonExample extends ServerLinkExample {
38+
39+
@Override
40+
public void overrideServerLinkResourceExample(Player viewer) {
41+
JsonObject message = new JsonObject();
42+
message.addProperty("@type", "type.googleapis.com/lunarclient.apollo.serverlink.v1.OverrideServerLinkResourceMessage");
43+
message.add("icon", JsonUtil.createResourceLocationIconObject("lunar:logo/logo-100x100.png"));
44+
45+
JsonPacketUtil.sendPacket(viewer, message);
46+
}
47+
48+
@Override
49+
public void resetServerLinkResourceExample(Player viewer) {
50+
JsonObject message = new JsonObject();
51+
message.addProperty("@type", "type.googleapis.com/lunarclient.apollo.serverlink.v1.ResetServerLinkResourceMessage");
52+
53+
JsonPacketUtil.sendPacket(viewer, message);
54+
}
55+
56+
@Override
57+
public void addServerLinkExample(Player viewer) {
58+
JsonArray serverLinks = Lists.newArrayList(
59+
this.createServerLinkObject("website", Component.text("Website", NamedTextColor.LIGHT_PURPLE), "https://www.lunarclient.com/"),
60+
this.createServerLinkObject("support", Component.text("Support", NamedTextColor.AQUA), "https://support.lunarclient.com/"),
61+
this.createServerLinkObject("status", Component.text("Status", NamedTextColor.RED), "https://status.lunarclient.com/")
62+
).stream().collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
63+
64+
JsonObject message = new JsonObject();
65+
message.addProperty("@type", "type.googleapis.com/lunarclient.apollo.serverlink.v1.AddServerLinkMessage");
66+
message.add("server_links", serverLinks);
67+
68+
JsonPacketUtil.sendPacket(viewer, message);
69+
}
70+
71+
private JsonObject createServerLinkObject(String id, Component displayName, String url) {
72+
JsonObject serverLinkObject = new JsonObject();
73+
serverLinkObject.addProperty("id", id);
74+
serverLinkObject.addProperty("display_name_adventure_json_lines", AdventureUtil.toJson(displayName));
75+
serverLinkObject.addProperty("url", url);
76+
return serverLinkObject;
77+
}
78+
79+
@Override
80+
public void removeServerLinkExample(Player viewer) {
81+
JsonArray serverLinkIds = Lists.newArrayList("website", "support", "status")
82+
.stream().collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
83+
84+
JsonObject message = new JsonObject();
85+
message.addProperty("@type", "type.googleapis.com/lunarclient.apollo.serverlink.v1.RemoveServerLinkMessage");
86+
message.add("server_link_ids", serverLinkIds);
87+
88+
JsonPacketUtil.sendPacket(viewer, message);
89+
}
90+
91+
@Override
92+
public void resetServerLinksExample(Player viewer) {
93+
JsonObject message = new JsonObject();
94+
message.addProperty("@type", "type.googleapis.com/lunarclient.apollo.serverlink.v1.ResetServerLinksMessage");
95+
96+
JsonPacketUtil.sendPacket(viewer, message);
97+
}
98+
99+
}

example/bukkit/json/src/main/java/com/lunarclient/apollo/example/json/util/JsonUtil.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ public static JsonObject createItemStackIconObject(@Nullable String itemName, in
150150
return iconObject;
151151
}
152152

153+
public static JsonObject createResourceLocationIconObject(@NotNull String resourceLocation) {
154+
JsonObject resourceIconObject = new JsonObject();
155+
resourceIconObject.addProperty("resource_location", resourceLocation);
156+
157+
JsonObject iconObject = new JsonObject();
158+
iconObject.add("resource_location", resourceIconObject);
159+
160+
return iconObject;
161+
}
162+
153163
public static JsonObject createSimpleResourceLocationIconObject(@NotNull String resourceLocation, int size) {
154164
JsonObject simpleIconObject = new JsonObject();
155165
simpleIconObject.addProperty("resource_location", resourceLocation);

example/bukkit/proto/src/main/java/com/lunarclient/apollo/example/proto/ApolloProtoExamplePlatform.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.lunarclient.apollo.example.proto.module.NotificationProtoExample;
4444
import com.lunarclient.apollo.example.proto.module.PayNowProtoExample;
4545
import com.lunarclient.apollo.example.proto.module.RichPresenceProtoExample;
46+
import com.lunarclient.apollo.example.proto.module.ServerLinkProtoExample;
4647
import com.lunarclient.apollo.example.proto.module.ServerRuleProtoExample;
4748
import com.lunarclient.apollo.example.proto.module.StaffModProtoExample;
4849
import com.lunarclient.apollo.example.proto.module.StopwatchProtoExample;
@@ -85,6 +86,7 @@ public void registerModuleExamples() {
8586
this.setNotificationExample(new NotificationProtoExample());
8687
this.setPayNowExample(new PayNowProtoExample());
8788
this.setRichPresenceExample(new RichPresenceProtoExample());
89+
this.setServerLinkExample(new ServerLinkProtoExample());
8890
this.setServerRuleExample(new ServerRuleProtoExample());
8991
this.setStaffModExample(new StaffModProtoExample());
9092
this.setStopwatchExample(new StopwatchProtoExample());

example/bukkit/proto/src/main/java/com/lunarclient/apollo/example/proto/module/CooldownProtoExample.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.lunarclient.apollo.example.proto.module;
2525

26+
import com.lunarclient.apollo.common.v1.Icon;
2627
import com.lunarclient.apollo.cooldown.v1.DisplayCooldownMessage;
2728
import com.lunarclient.apollo.cooldown.v1.RemoveCooldownMessage;
2829
import com.lunarclient.apollo.cooldown.v1.ResetCooldownsMessage;
@@ -39,7 +40,9 @@ public void displayCooldownItemExample(Player viewer) {
3940
DisplayCooldownMessage message = DisplayCooldownMessage.newBuilder()
4041
.setName("enderpearl-cooldown")
4142
.setDuration(ProtobufUtil.createDurationProto(Duration.ofSeconds(15)))
42-
.setIcon(ProtobufUtil.createItemStackIconProto("ENDER_PEARL", 0, 0))
43+
.setIcon(Icon.newBuilder()
44+
.setItemStack(ProtobufUtil.createItemStackIconProto("ENDER_PEARL", 0, 0))
45+
.build())
4346
.build();
4447

4548
ProtobufPacketUtil.sendPacket(viewer, message);
@@ -50,7 +53,9 @@ public void displayCooldownResourceExample(Player viewer) {
5053
DisplayCooldownMessage message = DisplayCooldownMessage.newBuilder()
5154
.setName("lunar-cooldown")
5255
.setDuration(ProtobufUtil.createDurationProto(Duration.ofSeconds(15)))
53-
.setIcon(ProtobufUtil.createSimpleResourceLocationIconProto("lunar:logo/logo-200x182.svg", 12))
56+
.setIcon(Icon.newBuilder()
57+
.setSimpleResourceLocation(ProtobufUtil.createSimpleResourceLocationIconProto("lunar:logo/logo-200x182.svg", 12))
58+
.build())
5459
.build();
5560

5661
ProtobufPacketUtil.sendPacket(viewer, message);
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* This file is part of Apollo, licensed under the MIT License.
3+
*
4+
* Copyright (c) 2023 Moonsworth
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.lunarclient.apollo.example.proto.module;
25+
26+
import com.google.common.collect.Lists;
27+
import com.lunarclient.apollo.example.module.impl.ServerLinkExample;
28+
import com.lunarclient.apollo.example.proto.util.AdventureUtil;
29+
import com.lunarclient.apollo.example.proto.util.ProtobufPacketUtil;
30+
import com.lunarclient.apollo.example.proto.util.ProtobufUtil;
31+
import com.lunarclient.apollo.serverlink.v1.AddServerLinkMessage;
32+
import com.lunarclient.apollo.serverlink.v1.OverrideServerLinkResourceMessage;
33+
import com.lunarclient.apollo.serverlink.v1.RemoveServerLinkMessage;
34+
import com.lunarclient.apollo.serverlink.v1.ResetServerLinkResourceMessage;
35+
import com.lunarclient.apollo.serverlink.v1.ResetServerLinksMessage;
36+
import com.lunarclient.apollo.serverlink.v1.ServerLink;
37+
import java.util.List;
38+
import net.kyori.adventure.text.Component;
39+
import net.kyori.adventure.text.format.NamedTextColor;
40+
import org.bukkit.entity.Player;
41+
42+
public class ServerLinkProtoExample extends ServerLinkExample {
43+
44+
@Override
45+
public void overrideServerLinkResourceExample(Player viewer) {
46+
OverrideServerLinkResourceMessage message = OverrideServerLinkResourceMessage.newBuilder()
47+
.setIcon(ProtobufUtil.createResourceLocationIconProto("lunar:logo/logo-100x100.png"))
48+
.build();
49+
50+
ProtobufPacketUtil.sendPacket(viewer, message);
51+
}
52+
53+
@Override
54+
public void resetServerLinkResourceExample(Player viewer) {
55+
ResetServerLinkResourceMessage message = ResetServerLinkResourceMessage.getDefaultInstance();
56+
ProtobufPacketUtil.sendPacket(viewer, message);
57+
}
58+
59+
@Override
60+
public void addServerLinkExample(Player viewer) {
61+
List<ServerLink> serverLinks = Lists.newArrayList(
62+
ServerLink.newBuilder()
63+
.setId("website")
64+
.setDisplayNameAdventureJsonLines(AdventureUtil.toJson(
65+
Component.text("Website", NamedTextColor.LIGHT_PURPLE)))
66+
.setUrl("https://www.lunarclient.com/")
67+
.build(),
68+
ServerLink.newBuilder()
69+
.setId("support")
70+
.setDisplayNameAdventureJsonLines(AdventureUtil.toJson(
71+
Component.text("Support", NamedTextColor.AQUA)))
72+
.setUrl("https://support.lunarclient.com/")
73+
.build(),
74+
ServerLink.newBuilder()
75+
.setId("status")
76+
.setDisplayNameAdventureJsonLines(AdventureUtil.toJson(
77+
Component.text("Status", NamedTextColor.RED)))
78+
.setUrl("https://status.lunarclient.com/")
79+
.build()
80+
);
81+
82+
AddServerLinkMessage message = AddServerLinkMessage.newBuilder()
83+
.addAllServerLinks(serverLinks)
84+
.build();
85+
86+
ProtobufPacketUtil.sendPacket(viewer, message);
87+
}
88+
89+
@Override
90+
public void removeServerLinkExample(Player viewer) {
91+
List<String> serverLinkIds = Lists.newArrayList("website", "support", "status");
92+
93+
RemoveServerLinkMessage message = RemoveServerLinkMessage.newBuilder()
94+
.addAllServerLinkIds(serverLinkIds)
95+
.build();
96+
97+
ProtobufPacketUtil.sendPacket(viewer, message);
98+
}
99+
100+
@Override
101+
public void resetServerLinksExample(Player viewer) {
102+
ResetServerLinksMessage message = ResetServerLinksMessage.getDefaultInstance();
103+
ProtobufPacketUtil.sendPacket(viewer, message);
104+
}
105+
106+
}

0 commit comments

Comments
 (0)