diff --git a/ci-pom.xml b/ci-pom.xml index 2510b4f8..a9a6c219 100644 --- a/ci-pom.xml +++ b/ci-pom.xml @@ -7,7 +7,7 @@ us.mytheria BlobLib - 1.696.8 + 1.696.9 pom.xml bloblib diff --git a/local-pom.xml b/local-pom.xml index fe84be76..b734eb39 100644 --- a/local-pom.xml +++ b/local-pom.xml @@ -5,7 +5,7 @@ us.mytheria BlobLib - 1.696.8 + 1.696.9 pom.xml bloblib diff --git a/pom.xml b/pom.xml index 8976adde..0beee462 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 us.mytheria BlobLib - 1.696.8 + 1.696.10 pom diff --git a/src/main/java/us/mytheria/bloblib/api/BlobLibMessageAPI.java b/src/main/java/us/mytheria/bloblib/api/BlobLibMessageAPI.java index df2cd029..adb8e682 100644 --- a/src/main/java/us/mytheria/bloblib/api/BlobLibMessageAPI.java +++ b/src/main/java/us/mytheria/bloblib/api/BlobLibMessageAPI.java @@ -8,6 +8,7 @@ import us.mytheria.bloblib.managers.MessageManager; import java.io.File; +import java.util.Objects; public class BlobLibMessageAPI { private static BlobLibMessageAPI instance; @@ -54,24 +55,46 @@ public String getMessagesFilePath() { } /** + * Gets the message from the default locale. + * * @param key The key of the message * @return The message */ @Nullable - public ReferenceBlobMessage getMessage(String key) { + public ReferenceBlobMessage getMessage(@NotNull String key) { + Objects.requireNonNull(key); return getMessageManager().getMessage(key); } /** + * Gets the message from the locale. + * If message is not available in the locale, returns the default message. + * * @param key The key of the message * @param locale The locale of the message * @return The message */ @Nullable - public ReferenceBlobMessage getMessage(String key, String locale) { + public ReferenceBlobMessage getMessage(@NotNull String key, @NotNull String locale) { + Objects.requireNonNull(key); + Objects.requireNonNull(locale); return getMessageManager().getMessage(key, locale); } + /** + * Gets the locale of the player and returns the message. + * If message is not available in the locale, returns the default message. + * + * @param key The key of the message + * @param player The player to get the locale from + * @return The message + */ + @Nullable + public ReferenceBlobMessage getMessage(@NotNull String key, @NotNull Player player) { + Objects.requireNonNull(player); + return getMessageManager().getMessage(key, player.getLocale()); + } + /** * @param key The key of the message * @param locale The locale of the message diff --git a/src/main/java/us/mytheria/bloblib/entities/BlobMessageReader.java b/src/main/java/us/mytheria/bloblib/entities/BlobMessageReader.java index 720a50d8..05825e55 100644 --- a/src/main/java/us/mytheria/bloblib/entities/BlobMessageReader.java +++ b/src/main/java/us/mytheria/bloblib/entities/BlobMessageReader.java @@ -1,7 +1,7 @@ package us.mytheria.bloblib.entities; import org.bukkit.configuration.ConfigurationSection; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.NotNull; import us.mytheria.bloblib.api.BlobLibMessageAPI; import us.mytheria.bloblib.entities.message.*; import us.mytheria.bloblib.utilities.TextColor; @@ -15,15 +15,18 @@ * Recommended method is parse(ConfigurationSection section) */ public class BlobMessageReader { + public static SerialBlobMessage read(ConfigurationSection section) { + return read(section, "en_us"); + } + /** * Will read a BlobMessage from a ConfigurationSection * * @param section The section to read from * @return The BlobMessage */ - public static SerialBlobMessage read(ConfigurationSection section) { + public static SerialBlobMessage read(ConfigurationSection section, @NotNull String locale) { String type = section.getString("Type"); - @Nullable String locale = section.getString("Locale", null); Optional sound = section.contains("BlobSound") ? BlobSoundReader.parse(section) : Optional.empty(); switch (type) { diff --git a/src/main/java/us/mytheria/bloblib/entities/Rep.java b/src/main/java/us/mytheria/bloblib/entities/Rep.java deleted file mode 100644 index d86ac5d1..00000000 --- a/src/main/java/us/mytheria/bloblib/entities/Rep.java +++ /dev/null @@ -1,53 +0,0 @@ -package us.mytheria.bloblib.entities; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -public final class Rep { - private String check; - private String replace; - - public static Rep lace(String check, String replace) { - return new Rep(check, replace); - } - - private Rep(String check, String replace) { - this.check = check; - this.replace = replace; - } - - public String getCheck() { - return check; - } - - public String getReplace() { - return replace; - } - - public static List lace(List lore, Rep... reps) { - if (lore == null) - return null; - List newLore = new ArrayList<>(); - for (String string : lore) { - for (Rep rep : reps) { - string = string.replace(rep.getCheck(), rep.getReplace()); - } - newLore.add(string); - } - return newLore; - } - - public static List lace(List lore, Collection reps) { - if (lore == null) - return null; - List newLore = new ArrayList<>(); - for (String string : lore) { - for (Rep rep : reps) { - string = string.replace(rep.getCheck(), rep.getReplace()); - } - newLore.add(string); - } - return newLore; - } -} diff --git a/src/main/java/us/mytheria/bloblib/itemstack/ItemStackModder.java b/src/main/java/us/mytheria/bloblib/itemstack/ItemStackModder.java index 000b68ed..eadcba99 100644 --- a/src/main/java/us/mytheria/bloblib/itemstack/ItemStackModder.java +++ b/src/main/java/us/mytheria/bloblib/itemstack/ItemStackModder.java @@ -9,11 +9,11 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.LeatherArmorMeta; -import us.mytheria.bloblib.entities.Rep; +import us.mytheria.bloblib.utilities.TextColor; import java.util.*; import java.util.function.Consumer; -import java.util.function.Function; +import java.util.stream.Stream; public final class ItemStackModder { private final ItemStack itemStack; @@ -73,28 +73,38 @@ public ItemStackModder unflag(Collection flags) { return unflag(flags.toArray(new ItemFlag[0])); } + public ItemStackModder displayName(String name, char colorChar) { + return itemMeta(itemMeta -> itemMeta.setDisplayName(TextColor.CUSTOM_PARSE(colorChar, name))); + } + public ItemStackModder displayName(String name) { - return itemMeta(itemMeta -> itemMeta.setDisplayName(name)); + return displayName(name, '&'); + } + + public ItemStackModder lore(String line, char colorChar) { + return lore(List.of(line), colorChar); } public ItemStackModder lore(String line) { - return lore(List.of(line)); + return lore(line, '&'); } - public ItemStackModder lore(String... lore) { - return itemMeta(itemMeta -> itemMeta.setLore(List.of(lore))); + public ItemStackModder lore(char colorChar, String... lore) { + List list = Stream.of(lore).map(line -> TextColor.CUSTOM_PARSE(colorChar, line)).toList(); + return itemMeta(itemMeta -> itemMeta.setLore(list)); } - public ItemStackModder lore(List lore) { - return itemMeta(itemMeta -> itemMeta.setLore(lore)); + public ItemStackModder lore(String... lore) { + return lore('&', lore); } - public ItemStackModder lore(Function, List> lore) { - return itemMeta(itemMeta -> itemMeta.setLore(lore.apply(itemMeta.getLore()))); + public ItemStackModder lore(List lore, char colorChar) { + List list = lore.stream().map(line -> TextColor.CUSTOM_PARSE(colorChar, line)).toList(); + return itemMeta(itemMeta -> itemMeta.setLore(list)); } - public ItemStackModder lore(List lore, Rep... reps) { - return lore(Rep.lace(lore, reps)); + public ItemStackModder lore(List lore) { + return lore(lore, '&'); } private List getLore() { @@ -103,18 +113,6 @@ private List getLore() { return meta.getLore(); } - public ItemStackModder lore(Rep... reps) { - List lore = getLore(); - if (lore == null) return this; - return lore(Rep.lace(lore, reps)); - } - - public ItemStackModder lore(Collection reps) { - List lore = getLore(); - if (lore == null) return this; - return lore(Rep.lace(lore, reps)); - } - /** * Clears the lore of the item. */ @@ -252,18 +250,4 @@ public ItemStackModder replace(String regex, String replacement) { } }); } - - public ItemStackModder replace(Rep... reps) { - for (Rep rep : reps) { - replace(rep.getCheck(), rep.getReplace()); - } - return this; - } - - public ItemStackModder replace(Collection reps) { - for (Rep rep : reps) { - replace(rep.getCheck(), rep.getReplace()); - } - return this; - } } diff --git a/src/main/java/us/mytheria/bloblib/managers/MessageManager.java b/src/main/java/us/mytheria/bloblib/managers/MessageManager.java index c7544e4a..ed4e8f84 100644 --- a/src/main/java/us/mytheria/bloblib/managers/MessageManager.java +++ b/src/main/java/us/mytheria/bloblib/managers/MessageManager.java @@ -14,7 +14,6 @@ public class MessageManager { private final BlobLib main; - private HashMap messages; private HashMap> pluginMessages; private HashMap duplicates; private HashMap> locales; @@ -28,7 +27,6 @@ public void reload() { } public void load() { - messages = new HashMap<>(); locales = new HashMap<>(); pluginMessages = new HashMap<>(); duplicates = new HashMap<>(); @@ -57,7 +55,7 @@ public void unload(BlobPlugin plugin) { Iterator iterator = messages.iterator(); while (iterator.hasNext()) { String inventoryName = iterator.next(); - this.messages.remove(inventoryName); + this.locales.forEach((locale, map) -> map.remove(inventoryName)); iterator.remove(); } pluginMessages.remove(pluginName); @@ -91,42 +89,42 @@ private void loadFiles(File path) { private void loadYamlConfiguration(File file) { YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file); + String locale = yamlConfiguration.getString("Locale", "en_us"); yamlConfiguration.getKeys(true).forEach(reference -> { if (!yamlConfiguration.isConfigurationSection(reference)) return; ConfigurationSection section = yamlConfiguration.getConfigurationSection(reference); if (!section.contains("Type") && !section.isString("Type")) return; - if (messages.containsKey(reference)) { - addDuplicate(reference); - return; - } - SerialBlobMessage message = BlobMessageReader.read(section); - messages.put(reference, message); + SerialBlobMessage message = BlobMessageReader.read(section, locale); addOrCreateLocale(message, reference); }); } private void loadYamlConfiguration(File file, BlobPlugin plugin) { YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file); + String locale = yamlConfiguration.getString("Locale", "en_us"); yamlConfiguration.getKeys(true).forEach(reference -> { if (!yamlConfiguration.isConfigurationSection(reference)) return; ConfigurationSection section = yamlConfiguration.getConfigurationSection(reference); if (!section.contains("Type") && !section.isString("Type")) return; - if (messages.containsKey(reference)) { - addDuplicate(reference); + if (!addOrCreateLocale(BlobMessageReader.read(section, locale), reference)) return; - } - messages.put(reference, BlobMessageReader.read(section)); pluginMessages.get(plugin.getName()).add(reference); }); } - private void addOrCreateLocale(SerialBlobMessage message, String reference) { + private boolean addOrCreateLocale(SerialBlobMessage message, String reference) { String locale = message.getLocale(); - locales.computeIfAbsent(locale, k -> new HashMap<>()).put(reference, message); + Map localeMap = locales.computeIfAbsent(locale, k -> new HashMap<>()); + if (localeMap.containsKey(reference)) { + addDuplicate(reference); + return false; + } + localeMap.put(reference, message); + return true; } /** @@ -157,12 +155,12 @@ private void addDuplicate(String key) { } public void noPermission(Player player) { - messages.get("System.No-Permission").handle(player); + locales.get("en_us").get("System.No-Permission").handle(player); } @Nullable public ReferenceBlobMessage getMessage(String key) { - return new ReferenceBlobMessage(messages.get(key), key); + return new ReferenceBlobMessage(locales.get("en_us").get(key), key); } @Nullable @@ -174,14 +172,20 @@ public ReferenceBlobMessage getMessage(String key, String locale) { } public void playAndSend(Player player, String key) { - SerialBlobMessage message = messages.get(key); + ReferenceBlobMessage message = getMessage(key, player.getLocale()); if (message == null) throw new NullPointerException("Message '" + key + "' does not exist!"); message.handle(player); } + /** + * @param player The player to send the message to + * @param key The key of the message + * @deprecated Use {@link #playAndSend(Player, String)} instead + */ + @Deprecated public void send(Player player, String key) { - SerialBlobMessage message = messages.get(key); + ReferenceBlobMessage message = getMessage(key, player.getLocale()); if (message == null) throw new NullPointerException("Message '" + key + "' does not exist!"); message.send(player);