|
| 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 | +} |
0 commit comments