|
| 1 | +/* |
| 2 | + * This file is a part of project QuickShop, the name is Lang.java |
| 3 | + * Copyright (C) PotatoCraft Studio and contributors |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License as published by the |
| 7 | + * Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +package org.maxgamer.quickshop.util.language; |
| 21 | + |
| 22 | +import com.google.common.collect.ImmutableList; |
| 23 | +import lombok.Getter; |
| 24 | +import org.apache.commons.lang.StringUtils; |
| 25 | +import org.bukkit.command.CommandSender; |
| 26 | +import org.bukkit.configuration.file.FileConfiguration; |
| 27 | +import org.bukkit.configuration.file.YamlConfiguration; |
| 28 | +import org.jetbrains.annotations.NotNull; |
| 29 | +import org.jetbrains.annotations.Nullable; |
| 30 | +import org.maxgamer.quickshop.util.language.formatter.FilledFormatter; |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.function.Consumer; |
| 35 | + |
| 36 | +public class Lang { |
| 37 | + private final File file; |
| 38 | + private final Consumer<FileConfiguration> upgrading; |
| 39 | + private final ImmutableList<Formatter> formatters; |
| 40 | + @Getter |
| 41 | + private FileConfiguration map; |
| 42 | + |
| 43 | + /** |
| 44 | + * Creating Language utils from a file |
| 45 | + * |
| 46 | + * @param file The Yaml language file |
| 47 | + * @param upgrading The upgrading method, executing everytime and allows devs to pre-processing or upgrading their |
| 48 | + * language files. |
| 49 | + * @param formatters The formatters used to formatting texts passthroughs this utils.. |
| 50 | + */ |
| 51 | + public Lang(@NotNull File file, @NotNull Consumer<FileConfiguration> upgrading, @Nullable Formatter... formatters) { |
| 52 | + this.file = file; |
| 53 | + this.upgrading = upgrading; |
| 54 | + if (formatters != null) { |
| 55 | + this.formatters = ImmutableList.copyOf(formatters); |
| 56 | + } else { |
| 57 | + this.formatters = ImmutableList.of(new FilledFormatter()); |
| 58 | + } |
| 59 | + this.reload(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Clean up and reload the data from drive |
| 64 | + */ |
| 65 | + public void reload() { |
| 66 | + if (!this.file.exists()) { |
| 67 | + try { |
| 68 | + //noinspection ResultOfMethodCallIgnored |
| 69 | + this.file.createNewFile(); |
| 70 | + } catch (IOException e) { |
| 71 | + e.printStackTrace(); |
| 72 | + } |
| 73 | + } |
| 74 | + this.map = YamlConfiguration.loadConfiguration(this.file); |
| 75 | + this.upgrading.accept(map); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Save the changes of the language file |
| 80 | + */ |
| 81 | + public void save() { |
| 82 | + try { |
| 83 | + this.map.save(this.file); |
| 84 | + } catch (IOException e) { |
| 85 | + e.printStackTrace(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Set the data |
| 91 | + * |
| 92 | + * @param path The data path |
| 93 | + * @param data The data |
| 94 | + */ |
| 95 | + public void set(@NotNull String path, @Nullable Object data) { |
| 96 | + this.map.set(path, data); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Formatting the phrase by formatters |
| 101 | + * |
| 102 | + * @param raw Original text |
| 103 | + * @param sender (optional) The text sender |
| 104 | + * @param args (optional) The args used for formatting |
| 105 | + * @return Formatted text |
| 106 | + */ |
| 107 | + public String format(@Nullable String raw, @Nullable CommandSender sender, @Nullable String... args) { |
| 108 | + if (StringUtils.isNotEmpty(raw)) { |
| 109 | + for (Formatter formatter : formatters) { |
| 110 | + raw = formatter.format(raw, sender, args); |
| 111 | + } |
| 112 | + } |
| 113 | + return raw; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Formatting the phrase by formatters |
| 118 | + * |
| 119 | + * @param raw Original text |
| 120 | + * @param args (optional) The args used for formatting |
| 121 | + * @return Formatted text |
| 122 | + */ |
| 123 | + public String format(@Nullable String raw, @Nullable String... args) { |
| 124 | + return format(raw, null, args); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Creating a Phrase from location |
| 129 | + * Phrase object allows developer to advanced and faster controlling phrase |
| 130 | + * |
| 131 | + * @param path The text path |
| 132 | + * @return A Phrase |
| 133 | + */ |
| 134 | + @NotNull |
| 135 | + public Phrase create(@NotNull String path) { |
| 136 | + return new Phrase(this, path); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Getting text from language file |
| 141 | + * |
| 142 | + * @param path The phrase location |
| 143 | + * @param args (optional) The args used for formatting |
| 144 | + * @return The formatted text |
| 145 | + */ |
| 146 | + @NotNull |
| 147 | + public String getString(@NotNull String path, @Nullable String... args) { |
| 148 | + return new Phrase(this, path).get(args); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Getting text from language file |
| 153 | + * |
| 154 | + * @param path The phrase location |
| 155 | + * @param sender (optional) The text sender |
| 156 | + * @param args (optional) The args used for formatting |
| 157 | + * @return The formatted text |
| 158 | + */ |
| 159 | + @NotNull |
| 160 | + public String getString(@NotNull String path, @NotNull CommandSender sender, @Nullable String... args) { |
| 161 | + return new Phrase(this, path).get(sender, args); |
| 162 | + } |
| 163 | + |
| 164 | +} |
0 commit comments