001package com.ericlam.mc.bungee.hnmc.builders; 002 003import com.ericlam.mc.bungee.hnmc.builders.function.ChatRunner; 004import com.ericlam.mc.bungee.hnmc.main.HyperNiteMC; 005import net.md_5.bungee.api.ChatColor; 006import net.md_5.bungee.api.CommandSender; 007import net.md_5.bungee.api.chat.*; 008 009import java.util.UUID; 010 011/** 012 * 訊息建造器 013 * 014 * @see AdvMessageBuilder 015 */ 016public class MessageBuilder { 017 private ComponentBuilder componentBuilder; 018 private UUID id; 019 private ChatRunner runner; 020 private int timeoutSeconds = -1; 021 private int timeoutClicks = -1; 022 023 /** 024 * 025 * @param msg 原始訊息 026 */ 027 public MessageBuilder(String msg) { 028 String amsg = ChatColor.translateAlternateColorCodes('&', msg); 029 componentBuilder = new ComponentBuilder(amsg); 030 } 031 032 public MessageBuilder() { 033 componentBuilder = new ComponentBuilder(""); 034 } 035 036 /** 037 * 038 * @param msgs 原始訊息串 039 */ 040 public MessageBuilder(String... msgs) { 041 componentBuilder = new ComponentBuilder(""); 042 for (int i = 0; i < msgs.length; i++) { 043 String msg = ChatColor.translateAlternateColorCodes('&', msgs[i]); 044 componentBuilder.append(TextComponent.fromLegacyText(msg)); 045 if (i != msgs.length - 1) { 046 componentBuilder.append("\n"); 047 } 048 } 049 } 050 051 /** 052 * 053 * @param sender 指令發送者 054 * @param message 訊息 055 */ 056 public static void sendMessage(CommandSender sender, String message) { 057 String msg = ChatColor.translateAlternateColorCodes('&', message); 058 sender.sendMessage(TextComponent.fromLegacyText(msg)); 059 } 060 061 /** 062 * 063 * @param msg 訊息 064 * @return this 065 */ 066 public MessageBuilder add(String msg) { 067 String amsg = ChatColor.translateAlternateColorCodes('&', msg); 068 componentBuilder.append(TextComponent.fromLegacyText(amsg)); 069 return this; 070 } 071 072 /** 073 * 074 * @param msgs 訊息串 075 * @return this 076 */ 077 public MessageBuilder add(String... msgs) { 078 for (int i = 0; i < msgs.length; i++) { 079 String msg = ChatColor.translateAlternateColorCodes('&', msgs[i]); 080 componentBuilder.append(TextComponent.fromLegacyText(msg)); 081 if (i != msgs.length - 1) { 082 componentBuilder.append("\n"); 083 } 084 } 085 return this; 086 } 087 088 /** 089 * 新增可點擊網址 090 * @param website 網址 091 * @return this 092 */ 093 public MessageBuilder url(String website) { 094 componentBuilder.event(new ClickEvent(ClickEvent.Action.OPEN_URL, website)); 095 return this; 096 } 097 098 /** 099 * 新增可點擊預輸入指令 100 * @param command 指令 101 * @return this 102 */ 103 public MessageBuilder suggest(String command) { 104 componentBuilder.event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, command)); 105 return this; 106 } 107 108 /** 109 * 新增可點擊運行指令 110 * @param command 指令 111 * @return this 112 */ 113 public MessageBuilder command(String command) { 114 componentBuilder.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command)); 115 return this; 116 } 117 118 /** 119 * 新增可點擊翻頁 120 * @param page 頁數 121 * @return this 122 */ 123 public MessageBuilder page(String page) { 124 componentBuilder.event(new ClickEvent(ClickEvent.Action.CHANGE_PAGE, page)); 125 return this; 126 } 127 128 /** 129 * 新增可見氣泡文字 130 * @param texts 文字串 131 * @return this 132 */ 133 public MessageBuilder hoverText(String... texts) { 134 ComponentBuilder builder = new ComponentBuilder(""); 135 for (int i = 0; i < texts.length; i++) { 136 String msg = ChatColor.translateAlternateColorCodes('&', texts[i]); 137 builder.append(msg); 138 if (i != texts.length - 1) { 139 builder.append("\n"); 140 } 141 } 142 componentBuilder.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, builder.create())); 143 return this; 144 } 145 146 /** 147 * 新增可見成就訊息 148 * @param achievementNode 成就節點 149 * @return this 150 */ 151 public MessageBuilder showAdvancement(String achievementNode) { 152 String value = "\"value\":" + achievementNode; 153 componentBuilder.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(value))); 154 return this; 155 } 156 157 /** 158 * 新增shift click 文字 159 * @param insert 文字 160 * @return this 161 */ 162 public MessageBuilder insertWhenShiftClick(String insert) { 163 componentBuilder.insertion(insert); 164 return this; 165 } 166 167 /** 168 * 換行 169 * @return this 170 */ 171 public MessageBuilder nextLine() { 172 componentBuilder.append("\n"); 173 return this; 174 } 175 176 /** 177 * 默認為 十分鐘 之後自動過期 178 * 179 * @param runner 運行函式 180 * @return this 181 */ 182 public MessageBuilder run(ChatRunner runner) { 183 this.id = UUID.randomUUID(); 184 this.runner = runner; 185 this.timeoutSeconds = 600; 186 this.timeoutClicks = -1; 187 return this; 188 } 189 190 /** 191 * @param timeoutClicks 點擊多少次後自動過期 192 * @param runner 運行函式 193 * @return this 194 */ 195 public MessageBuilder runClicks(int timeoutClicks, ChatRunner runner) { 196 this.id = UUID.randomUUID(); 197 this.runner = runner; 198 this.timeoutClicks = timeoutClicks; 199 this.timeoutSeconds = -1; 200 return this; 201 } 202 203 /** 204 * @param timeoutSeconds 多少秒後失效 205 * @param runner 運行函式 206 * @return this 207 */ 208 public MessageBuilder runTimeout(int timeoutSeconds, ChatRunner runner) { 209 this.id = UUID.randomUUID(); 210 this.runner = runner; 211 this.timeoutSeconds = timeoutSeconds; 212 this.timeoutClicks = -1; 213 return this; 214 } 215 216 /** 217 * @return 訊息 218 */ 219 public BaseComponent[] build() { 220 if (this.runner != null) { 221 this.command("/command-run-bungee_" + id.toString()); 222 if (timeoutSeconds > 0) { 223 HyperNiteMC.getAPI().getChatRunnerManager().registerTimeout(id, runner, timeoutSeconds); 224 } else if (timeoutClicks > 0) { 225 HyperNiteMC.getAPI().getChatRunnerManager().registerClicks(id, runner, timeoutClicks); 226 } 227 } 228 return componentBuilder.create(); 229 } 230 231 /** 232 * 233 * @param player 玩家 234 */ 235 public void sendPlayer(CommandSender player) { 236 player.sendMessage(this.build()); 237 } 238 239 240} 241//如何使用 242/* 243class MessageBuilderUse{ 244 void use(Player sender){ 245 BaseComponent[] msg = new MessageBuilder("&e[ 公告全世界你是女裝大佬 ]").hoverText("&a來啊點我啊").run(player -> { 246 Bukkit.broadcastMessage(net.md_5.bungee.api.ChatColor.AQUA + player.getName()+" 是女裝大佬!!"); 247 BaseComponent[] tp = new MessageBuilder("&e[ 點我傳送到「她」的位置 ]").hoverText("&a點啊快點啊").run(player1 -> { 248 player1.teleport(player); 249 }).build(); 250 Bukkit.getOnlinePlayers().forEach(p->p.sendMessage(tp)); 251 }).build(); 252 sender.sendMessage(msg); 253 } 254} 255 256 */