/* * ProxyQueues, a Velocity queueing solution * * Copyright (c) 2021 James Lyne * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package uk.co.notnull.proxyqueues; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.spongepowered.configurate.ConfigurationNode; import uk.co.notnull.proxyqueues.api.MessageType; import java.util.Collections; import java.util.Map; public class Messages { private static ConfigurationNode messages; private static final MiniMessage miniMessage = MiniMessage.miniMessage(); public static void set(ConfigurationNode messages) { Messages.messages = messages; } public static String get(String id) { return get(id, Collections.emptyMap()); } public static String getPrefixed(String id, MessageType messageType) { return getPrefixed(id, messageType, Collections.emptyMap()); } public static String get(String id, Map replacements) { if(messages == null) { return ""; } String message = messages.node((Object[]) id.split("\\.")) .getString("Message " + id + " does not exist"); for (Map.Entry entry : replacements.entrySet()) { message = message.replace("<" + entry.getKey() + ">", entry.getValue()); } return message; } public static String getPrefixed(String id, MessageType messageType, Map replacements) { return getPrefix(messageType) + get(id, replacements); } public static Component getComponent(String id) { return getComponent(id, Collections.emptyMap(), Collections.emptyMap()); } public static Component getComponent(String id, Map stringReplacements, Map componentReplacmenets) { if(messages == null) { return Component.empty(); } String message = messages.node((Object[]) id.split("\\.")) .getString("Message " + id + " does not exist"); TagResolver.Builder placeholders = TagResolver.builder(); for (Map.Entry entry : stringReplacements.entrySet()) { placeholders.resolver(Placeholder.parsed(entry.getKey(), entry.getValue())); } for (Map.Entry entry : componentReplacmenets.entrySet()) { placeholders.resolver(Placeholder.component(entry.getKey(), entry.getValue())); } return miniMessage.deserialize(message, placeholders.build()); } public static String getPrefix(MessageType messageType) { if(messageType.equals(MessageType.ERROR)) { return Messages.get("prefix.error"); } else if(messageType.equals(MessageType.WARNING)) { return Messages.get("prefix.warning"); } else { return Messages.get("prefix.info"); } } }