generated from ybw0014/GuizhanSlimefunAddon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
546 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
files: | ||
- source: /src/main/resources/languages/en/*.yml | ||
translation: /src/main/resources/languages/%osx_locale%/%original_file_name% | ||
|
||
- source: /src/main/resources/translations/en/*.yml | ||
translation: /src/main/resources/translations/%osx_locale%/%original_file_name% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 110 additions & 2 deletions
112
src/main/java/net/guizhanss/slimefuntranslation/api/TranslationConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,133 @@ | ||
package net.guizhanss.slimefuntranslation.api; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.logging.Level; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
import net.guizhanss.slimefuntranslation.implementation.translations.FixedTranslation; | ||
import net.guizhanss.slimefuntranslation.utils.ConfigUtils; | ||
|
||
import org.bukkit.configuration.file.FileConfiguration; | ||
|
||
import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon; | ||
|
||
import net.guizhanss.slimefuntranslation.SlimefunTranslation; | ||
import net.guizhanss.slimefuntranslation.api.interfaces.Translation; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
|
||
/** | ||
* This class holds the information provided from a valid translations file, or from other addons. | ||
* | ||
* @author ybw0014 | ||
*/ | ||
@SuppressWarnings("ConstantConditions") | ||
@RequiredArgsConstructor | ||
@Getter | ||
public class TranslationConfiguration { | ||
private final String name; | ||
private final String author; | ||
private final boolean enabled; | ||
private final String lang; | ||
private final List<String> dependencies; | ||
private final Map<String, Translation> translations; | ||
|
||
@Setter(AccessLevel.PRIVATE) | ||
private State state = State.UNREGISTERED; | ||
private SlimefunAddon addon = null; | ||
|
||
/** | ||
* Creates a {@link TranslationConfiguration} from a {@link FileConfiguration}. | ||
* | ||
* @param config | ||
* the {@link FileConfiguration} to create the {@link TranslationConfiguration} from. | ||
* | ||
* @return an {@link Optional} of {@link TranslationConfiguration} if the config is valid, otherwise {@code null}. | ||
*/ | ||
@Nonnull | ||
public static Optional<TranslationConfiguration> fromFileConfiguration(@Nonnull FileConfiguration config) { | ||
Preconditions.checkArgument(config != null, "config cannot be null"); | ||
|
||
String name = config.getString("name", "Unnamed Translation"); | ||
String author = config.getString("author", "SlimefunTranslation"); | ||
String lang = SlimefunTranslation.getConfigService().getMappedLanguage(config.getString("lang", "en")); | ||
List<String> dependencies = config.getStringList("dependencies"); | ||
|
||
for (var dependency : dependencies) { | ||
if (!SlimefunTranslation.getInstance().getServer().getPluginManager().isPluginEnabled(dependency)) { | ||
SlimefunTranslation.log(Level.SEVERE, "Translation config \"{0}\" by {1} is missing dependency {2}.", name, author, dependency); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
var section = config.getConfigurationSection("translations"); | ||
if (section == null) { | ||
SlimefunTranslation.log(Level.WARNING, "No translations found in " + name + " by " + author); | ||
return Optional.empty(); | ||
} | ||
SlimefunTranslation.log(Level.INFO, "Loading translation configuration \"{0}\" by {1}, language: {2}", name, author, lang); | ||
Map<String, Translation> translations = new HashMap<>(); | ||
for (var itemId : section.getKeys(false)) { | ||
SlimefunTranslation.debug("Loading translation {0}", itemId); | ||
var itemSection = section.getConfigurationSection(itemId); | ||
// name | ||
String displayName = ""; | ||
if (itemSection.contains("name")) { | ||
displayName = itemSection.getString("name", ""); | ||
} | ||
|
||
// lore | ||
var lore = itemSection.getStringList("lore"); | ||
|
||
// lore replacements | ||
Map<Integer, String> replacementMap = new HashMap<>(); | ||
if (itemSection.contains("lore-replacements")) { | ||
try { | ||
Map<String, String> replacements = ConfigUtils.getMap(itemSection.getConfigurationSection("lore-replacements")); | ||
for (var entry : replacements.entrySet()) { | ||
replacementMap.put(Integer.parseInt(entry.getKey()), entry.getValue()); | ||
} | ||
} catch (NumberFormatException | NullPointerException ex) { | ||
SlimefunTranslation.log(Level.SEVERE, "Invalid lore replacements of item {0} in translation {1} by {2}", itemId, name, author); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
// check name | ||
boolean checkName = itemSection.getBoolean("check-name", false); | ||
|
||
var translation = new FixedTranslation(displayName, lore, replacementMap, checkName); | ||
translations.put(itemId, translation); | ||
} | ||
return Optional.of(new TranslationConfiguration(name, author, lang, dependencies, translations)); | ||
} | ||
|
||
public void register(@Nonnull SlimefunAddon addon) { | ||
if (state != State.UNREGISTERED) { | ||
throw new IllegalStateException("TranslationConfiguration is already registered"); | ||
} | ||
|
||
var allTranslations = SlimefunTranslation.getRegistry().getTranslations(); | ||
allTranslations.putIfAbsent(lang, new HashMap<>()); | ||
var currentTranslations = allTranslations.get(lang); | ||
currentTranslations.putAll(translations); | ||
|
||
this.addon = addon; | ||
setState(State.REGISTERED); | ||
} | ||
|
||
private final Map<String, Translation> translations = new HashMap<>(); | ||
public enum State { | ||
UNREGISTERED, | ||
REGISTERED | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
src/main/java/net/guizhanss/slimefuntranslation/api/translations/FixedTranslation.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/net/guizhanss/slimefuntranslation/core/services/ConfigurationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package net.guizhanss.slimefuntranslation.core.services; | ||
|
||
import java.util.Map; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
import net.guizhanss.guizhanlib.slimefun.addon.AddonConfig; | ||
import net.guizhanss.slimefuntranslation.SlimefunTranslation; | ||
import net.guizhanss.slimefuntranslation.utils.ConfigUtils; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
|
||
@SuppressWarnings("ConstantConditions") | ||
@Getter | ||
public final class ConfigurationService { | ||
@Getter(AccessLevel.NONE) | ||
private final AddonConfig config; | ||
private Map<String, String> languageMappings; | ||
private boolean autoUpdate; | ||
private boolean debug; | ||
|
||
public ConfigurationService(SlimefunTranslation plugin) { | ||
config = new AddonConfig(plugin, "config.yml"); | ||
reload(); | ||
} | ||
|
||
public void reload() { | ||
config.reload(); | ||
|
||
autoUpdate = config.getBoolean("auto-update", true); | ||
debug = config.getBoolean("debug", false); | ||
languageMappings = ConfigUtils.getMap(config.getConfigurationSection("language-mappings")); | ||
|
||
config.save(); | ||
} | ||
|
||
@Nonnull | ||
public String getMappedLanguage(@Nonnull String language) { | ||
Preconditions.checkArgument(language != null, "language cannot be null"); | ||
return SlimefunTranslation.getConfigService().getLanguageMappings().getOrDefault(language, language); | ||
} | ||
} |
Oops, something went wrong.