|
| 1 | +package com.froxynetwork.froxyapi; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | + |
| 5 | +import org.bukkit.plugin.java.JavaPlugin; |
| 6 | +import org.slf4j.Logger; |
| 7 | + |
| 8 | +import com.froxynetwork.froxyapi.language.LanguageManager; |
| 9 | +import com.froxynetwork.froxyapi.language.Languages; |
| 10 | + |
| 11 | +/** |
| 12 | + * MIT License |
| 13 | + * |
| 14 | + * Copyright (c) 2019 FroxyNetwork |
| 15 | + * |
| 16 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 17 | + * of this software and associated documentation files (the "Software"), to deal |
| 18 | + * in the Software without restriction, including without limitation the rights |
| 19 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 20 | + * copies of the Software, and to permit persons to whom the Software is |
| 21 | + * furnished to do so, subject to the following conditions: |
| 22 | + * |
| 23 | + * The above copyright notice and this permission notice shall be included in |
| 24 | + * all copies or substantial portions of the Software. |
| 25 | + * |
| 26 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 27 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 28 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 29 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 30 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 31 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 32 | + * SOFTWARE. |
| 33 | + * |
| 34 | + * @author 0ddlyoko |
| 35 | + */ |
| 36 | +/** |
| 37 | + * Represents the API core, for version and API singleton handling |
| 38 | + */ |
| 39 | +public final class Froxy { |
| 40 | + |
| 41 | + private static API api; |
| 42 | + |
| 43 | + private Froxy() { |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets the current {@link API} singleton |
| 48 | + * |
| 49 | + * @return API instance being ran |
| 50 | + */ |
| 51 | + public static API getAPI() { |
| 52 | + return api; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Attempts to set the {@link API} singleton. |
| 57 | + * <p> |
| 58 | + * This cannot be done if the API is already set. |
| 59 | + * |
| 60 | + * @param api API instance |
| 61 | + */ |
| 62 | + public static void setAPI(API api) { |
| 63 | + if (Froxy.api != null) |
| 64 | + throw new UnsupportedOperationException("Cannot redefine singleton API"); |
| 65 | + Froxy.api = api; |
| 66 | + api.getLogger().info(""); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @return The JavaPlugin implementation of the Core plugin |
| 71 | + */ |
| 72 | + public static JavaPlugin getCorePlugin() { |
| 73 | + return api.getCorePlugin(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @return The JavaPlugin implementation of the Game plugin |
| 78 | + */ |
| 79 | + public static JavaPlugin getGamePlugin() { |
| 80 | + return api.getGamePlugin(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return The version of the actual Core |
| 85 | + */ |
| 86 | + public static String getVersion() { |
| 87 | + return api.getVersion(); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @return The logger |
| 92 | + */ |
| 93 | + public static Logger getLogger() { |
| 94 | + return api.getLogger(); |
| 95 | + } |
| 96 | + |
| 97 | + // ----------------------------------------- |
| 98 | + // | | |
| 99 | + // | Language Manager | |
| 100 | + // | | |
| 101 | + // ----------------------------------------- |
| 102 | + |
| 103 | + public static LanguageManager getLanguageManager() { |
| 104 | + return api.getLanguageManager(); |
| 105 | + } |
| 106 | + |
| 107 | + public static Languages getDefaultLanguage() { |
| 108 | + return api.getDefaultLanguage(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Register a path as a language directory.<br /> |
| 113 | + * All language files MUST have the correct name to be loaded.<br /> |
| 114 | + * Files name MUST be of this form: "{name}.lang".<br /> |
| 115 | + * Example: <code>fr_FR.lang or en_US.lang</code> |
| 116 | + * |
| 117 | + * @param path The directory |
| 118 | + */ |
| 119 | + public static void register(File path) { |
| 120 | + api.register(path); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Get the default translate of specific message id.<br /> |
| 125 | + * Same as <code>$(id, getDefaultLanguage(), params)</code> |
| 126 | + * |
| 127 | + * @param id The id of the message |
| 128 | + * @param params The parameters |
| 129 | + * @return The message translated by default language, or the id if message id doesn't exist |
| 130 | + */ |
| 131 | + public static String $(String id, String... params) { |
| 132 | + return api.$(id, params); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Get the translation of specific message id with specific language. If message id not found, return the translation with DEFAULT language |
| 137 | + * |
| 138 | + * @param id The id of the message |
| 139 | + * @param lang The specific language |
| 140 | + * @param params The parameters |
| 141 | + * @return The message translated by specific language, or the message translated by default language, or the id if message id doesn't exist |
| 142 | + */ |
| 143 | + public static String $(String id, Languages lang, String... params) { |
| 144 | + return api.$(id, lang, params); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Get the translate of specific id with specific language |
| 149 | + * |
| 150 | + * @param id The id of the message |
| 151 | + * @param lang The specific language |
| 152 | + * @param params The parameters |
| 153 | + * @return The message translated by specific language, or the id if message id doesn't exist |
| 154 | + */ |
| 155 | + public static String $_(String id, Languages lang, String... params) { |
| 156 | + return api.$_(id, lang, params); |
| 157 | + } |
| 158 | + |
| 159 | + // ----------------------------------------- |
| 160 | + // | | |
| 161 | + // | Other | |
| 162 | + // | | |
| 163 | + // ----------------------------------------- |
| 164 | +} |
0 commit comments