Velocity queueing solution
at master 4.2 kB view raw
1/* 2 * ProxyQueues, a Velocity queueing solution 3 * 4 * Copyright (c) 2021 James Lyne 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 SOFTWARE. 22 */ 23 24package uk.co.notnull.proxyqueues; 25 26import net.kyori.adventure.text.Component; 27import net.kyori.adventure.text.ComponentLike; 28import net.kyori.adventure.text.minimessage.MiniMessage; 29import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; 30import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; 31import org.spongepowered.configurate.ConfigurationNode; 32import uk.co.notnull.proxyqueues.api.MessageType; 33 34import java.util.Collections; 35import java.util.Map; 36 37public class Messages { 38 private static ConfigurationNode messages; 39 private static final MiniMessage miniMessage = MiniMessage.miniMessage(); 40 41 public static void set(ConfigurationNode messages) { 42 Messages.messages = messages; 43 } 44 45 public static String get(String id) { 46 return get(id, Collections.emptyMap()); 47 } 48 49 public static String getPrefixed(String id, MessageType messageType) { 50 return getPrefixed(id, messageType, Collections.emptyMap()); 51 } 52 53 public static String get(String id, Map<String, String> replacements) { 54 if(messages == null) { 55 return ""; 56 } 57 58 String message = messages.node((Object[]) id.split("\\.")) 59 .getString("Message " + id + " does not exist"); 60 61 for (Map.Entry<String, String> entry : replacements.entrySet()) { 62 message = message.replace("<" + entry.getKey() + ">", entry.getValue()); 63 } 64 65 return message; 66 } 67 68 public static String getPrefixed(String id, MessageType messageType, Map<String, String> replacements) { 69 return getPrefix(messageType) + get(id, replacements); 70 } 71 72 public static Component getComponent(String id) { 73 return getComponent(id, Collections.emptyMap(), Collections.emptyMap()); 74 } 75 76 public static Component getComponent(String id, Map<String, String> stringReplacements, Map<String, ComponentLike> componentReplacmenets) { 77 if(messages == null) { 78 return Component.empty(); 79 } 80 81 String message = messages.node((Object[]) id.split("\\.")) 82 .getString("Message " + id + " does not exist"); 83 84 TagResolver.Builder placeholders = TagResolver.builder(); 85 86 for (Map.Entry<String, String> entry : stringReplacements.entrySet()) { 87 placeholders.resolver(Placeholder.parsed(entry.getKey(), entry.getValue())); 88 } 89 90 for (Map.Entry<String, ComponentLike> entry : componentReplacmenets.entrySet()) { 91 placeholders.resolver(Placeholder.component(entry.getKey(), entry.getValue())); 92 } 93 94 return miniMessage.deserialize(message, placeholders.build()); 95 } 96 97 public static String getPrefix(MessageType messageType) { 98 if(messageType.equals(MessageType.ERROR)) { 99 return Messages.get("prefix.error"); 100 } else if(messageType.equals(MessageType.WARNING)) { 101 return Messages.get("prefix.warning"); 102 } else { 103 return Messages.get("prefix.info"); 104 } 105 } 106}