From 3d87cb3df14bc7977fa06508c9478df0fa952d64 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Sun, 2 Jan 2022 12:47:22 +0100 Subject: [PATCH] Fixed #3400 --- CHANGELOG.md | 8 +++++ .../core/services/LocalizationService.java | 33 ++++++++++++------- .../core/services/github/GitHubService.java | 3 +- .../localization/SlimefunLocalization.java | 19 +++++++---- .../slimefun4/utils/PatternUtils.java | 2 ++ 5 files changed, 47 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 759e28747..26345eee9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,12 @@ #### Additions * Added Armored Jetpack +* Added Cocoa Beans as a fuel type for the Bio-Generator +* Added Beetroots and Beetroot seeds as fuel types for the Bio-Generator +* Added small and big dripleaves as fuel types for the Bio-Generator +* Added Glow Berries as a fuel type for the Bio-Generator +* Added Glow Lichen as a fuel type for the Bio-Generator +* Added Spore Blossom as a fuel type for the Bio-Generator #### Changes @@ -43,6 +49,8 @@ * Fixed research issues for vanilla items, e.g. Trident or Totem of Undying * Fixed #3368 * Fixed #1315 +* Fixed #3400 +* Fixed rare issue where Slimefun would not load at all ## Release Candidate 30 (31 Dec 2021) https://thebusybiscuit.github.io/builds/TheBusyBiscuit/Slimefun4/stable/#30 diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/LocalizationService.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/LocalizationService.java index 75c9e5691..39b533335 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/LocalizationService.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/LocalizationService.java @@ -10,6 +10,7 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import java.util.logging.Level; +import java.util.stream.Collectors; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -18,6 +19,7 @@ import javax.annotation.ParametersAreNonnullByDefault; import org.apache.commons.lang.Validate; import org.bukkit.NamespacedKey; import org.bukkit.Server; +import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; @@ -29,6 +31,7 @@ import io.github.thebusybiscuit.slimefun4.core.services.localization.LanguageFil import io.github.thebusybiscuit.slimefun4.core.services.localization.SlimefunLocalization; import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; import io.github.thebusybiscuit.slimefun4.utils.NumberUtils; +import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; /** * As the name suggests, this Service is responsible for Localization. @@ -97,7 +100,7 @@ public class LocalizationService extends SlimefunLocalization { } @Override - public String getPrefix() { + public String getChatPrefix() { return prefix; } @@ -126,7 +129,7 @@ public class LocalizationService extends SlimefunLocalization { // Checks if our jar files contains a messages.yml file for that language String file = LanguageFile.MESSAGES.getFilePath(id); - return !streamConfigFile(file, null).getKeys(false).isEmpty(); + return !getConfigurationFromStream(file, null).getKeys(false).isEmpty(); } /** @@ -196,7 +199,7 @@ public class LocalizationService extends SlimefunLocalization { @ParametersAreNonnullByDefault private void copyToDefaultLanguage(String language, LanguageFile file) { - FileConfiguration config = streamConfigFile(file.getFilePath(language), null); + FileConfiguration config = getConfigurationFromStream(file.getFilePath(language), null); defaultLanguage.setFile(file, config); } @@ -210,7 +213,7 @@ public class LocalizationService extends SlimefunLocalization { for (LanguageFile file : LanguageFile.values()) { FileConfiguration defaults = file == LanguageFile.MESSAGES ? getConfig().getConfiguration() : null; - FileConfiguration config = streamConfigFile(file.getFilePath(language), defaults); + FileConfiguration config = getConfigurationFromStream(file.getFilePath(language), defaults); language.setFile(file, config); } @@ -249,8 +252,7 @@ public class LocalizationService extends SlimefunLocalization { return Math.min(NumberUtils.reparseDouble(100.0 * (matches / (double) defaultKeys.size())), 100.0); } - @Nonnull - private FileConfiguration streamConfigFile(@Nonnull String file, @Nullable FileConfiguration defaults) { + private @Nonnull FileConfiguration getConfigurationFromStream(@Nonnull String file, @Nullable FileConfiguration defaults) { InputStream inputStream = plugin.getClass().getResourceAsStream(file); if (inputStream == null) { @@ -258,15 +260,24 @@ public class LocalizationService extends SlimefunLocalization { } try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { - FileConfiguration config = YamlConfiguration.loadConfiguration(reader); + String content = reader.lines().collect(Collectors.joining("\n")); + YamlConfiguration config = new YamlConfiguration(); - if (defaults != null) { - config.setDefaults(defaults); + /* + * Fixes #3400 - Only attempt to load yaml files that contain data. + * This is not a perfect fix but should be sufficient to circumvent this issue. + */ + if (PatternUtils.YAML_ENTRY.matcher(content).find()) { + config.loadFromString(content); + + if (defaults != null) { + config.setDefaults(defaults); + } } return config; - } catch (IOException e) { - Slimefun.logger().log(Level.SEVERE, e, () -> "Failed to load language file into memory: \"" + file + "\""); + } catch (IOException | InvalidConfigurationException e) { + Slimefun.logger().log(Level.WARNING, e, () -> "Failed to load language file into memory: \"" + file + "\""); return new YamlConfiguration(); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/github/GitHubService.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/github/GitHubService.java index 56e978028..17d158d27 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/github/GitHubService.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/github/GitHubService.java @@ -57,7 +57,6 @@ public class GitHubService { connectors = new HashSet<>(); contributors = new ConcurrentHashMap<>(); - loadConnectors(false); } /** @@ -68,6 +67,8 @@ public class GitHubService { * Our instance of {@link Slimefun} */ public void start(@Nonnull Slimefun plugin) { + loadConnectors(false); + long period = TimeUnit.HOURS.toMillis(1); GitHubTask task = new GitHubTask(this); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/localization/SlimefunLocalization.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/localization/SlimefunLocalization.java index cbc29189b..185d803e2 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/localization/SlimefunLocalization.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/localization/SlimefunLocalization.java @@ -64,7 +64,14 @@ public abstract class SlimefunLocalization implements Keyed { defaultConfig.save(); } - public @Nonnull String getPrefix() { + /** + * This returns the chat prefix for our messages. + * Every message (unless explicitly omitted) will have this + * prefix prepended. + * + * @return The chat prefix + */ + public @Nonnull String getChatPrefix() { return getMessage("prefix"); } @@ -339,7 +346,7 @@ public abstract class SlimefunLocalization implements Keyed { Validate.notNull(recipient, "Recipient cannot be null!"); Validate.notNull(key, "Message key cannot be null!"); - String prefix = addPrefix ? getPrefix() : ""; + String prefix = addPrefix ? getChatPrefix() : ""; if (recipient instanceof Player) { recipient.sendMessage(ChatColors.color(prefix + getMessage((Player) recipient, key))); @@ -352,7 +359,7 @@ public abstract class SlimefunLocalization implements Keyed { Validate.notNull(player, "Player cannot be null!"); Validate.notNull(key, "Message key cannot be null!"); - String prefix = addPrefix ? getPrefix() : ""; + String prefix = addPrefix ? getChatPrefix() : ""; String message = ChatColors.color(prefix + getMessage(player, key)); BaseComponent[] components = TextComponent.fromLegacyText(message); @@ -374,7 +381,7 @@ public abstract class SlimefunLocalization implements Keyed { return; } - String prefix = addPrefix ? getPrefix() : ""; + String prefix = addPrefix ? getChatPrefix() : ""; if (recipient instanceof Player) { recipient.sendMessage(ChatColors.color(prefix + function.apply(getMessage((Player) recipient, key)))); @@ -384,7 +391,7 @@ public abstract class SlimefunLocalization implements Keyed { } public void sendMessages(@Nonnull CommandSender recipient, @Nonnull String key) { - String prefix = getPrefix(); + String prefix = getChatPrefix(); if (recipient instanceof Player) { for (String translation : getMessages((Player) recipient, key)) { @@ -401,7 +408,7 @@ public abstract class SlimefunLocalization implements Keyed { @ParametersAreNonnullByDefault public void sendMessages(CommandSender recipient, String key, boolean addPrefix, UnaryOperator function) { - String prefix = addPrefix ? getPrefix() : ""; + String prefix = addPrefix ? getChatPrefix() : ""; if (recipient instanceof Player) { for (String translation : getMessages((Player) recipient, key)) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/PatternUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/PatternUtils.java index 96b8bd1c3..b6332af6f 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/PatternUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/PatternUtils.java @@ -19,6 +19,8 @@ public final class PatternUtils { private PatternUtils() {} public static final Pattern SLASH_SEPARATOR = Pattern.compile(" / "); + + public static final Pattern YAML_ENTRY = Pattern.compile("[a-z0-9_-]+:.*"); public static final Pattern MINECRAFT_NAMESPACEDKEY = Pattern.compile("minecraft:[a-z0-9/._-]+");