Skip to content

Commit

Permalink
feat: fixed translation
Browse files Browse the repository at this point in the history
  • Loading branch information
ybw0014 committed Dec 23, 2023
1 parent 768e2f4 commit 0a58f35
Show file tree
Hide file tree
Showing 19 changed files with 546 additions and 74 deletions.
5 changes: 2 additions & 3 deletions crowdin.yml
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%
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<filtering>true</filtering>
<includes>
<include>*</include>
<include>translations/**/*.yml</include>
</includes>
</resource>
</resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
import java.lang.reflect.Method;
import java.util.logging.Level;

import javax.annotation.Nonnull;

import com.google.common.base.Preconditions;

import org.bukkit.plugin.Plugin;

import io.github.thebusybiscuit.slimefun4.libraries.dough.updater.BlobBuildUpdater;

import net.guizhanss.guizhanlib.slimefun.addon.AbstractAddon;
import net.guizhanss.guizhanlib.updater.GuizhanBuildsUpdater;
import net.guizhanss.slimefuntranslation.core.Registry;
import net.guizhanss.slimefuntranslation.core.services.ConfigurationService;
import net.guizhanss.slimefuntranslation.implementation.managers.CommandManager;
import net.guizhanss.slimefuntranslation.implementation.managers.ListenerManager;
import net.guizhanss.slimefuntranslation.implementation.managers.PacketListenerManager;
Expand All @@ -21,9 +26,11 @@

public final class SlimefunTranslation extends AbstractAddon {

private ConfigurationService configService;
private Registry registry;
private UserManager userManager;
private TranslationManager translationManager;
private boolean debugEnabled = false;

public SlimefunTranslation() {
super("ybw0014", "SlimefunTranslation", "master", "auto-update");
Expand All @@ -33,37 +40,62 @@ private static SlimefunTranslation inst() {
return getInstance();
}

@Nonnull
public static ConfigurationService getConfigService() {
return inst().configService;
}

@Nonnull
public static Registry getRegistry() {
return inst().registry;
}

@Nonnull
public static UserManager getUserManager() {
return inst().userManager;
}

@Nonnull
public static TranslationManager getTranslationManager() {
return inst().translationManager;
}

public static void debug(@Nonnull String message, @Nonnull Object... args) {
Preconditions.checkNotNull(message, "message cannot be null");

if (inst().debugEnabled) {
inst().getLogger().log(Level.INFO, "[DEBUG] " + message, args);
}
}

@Override
public void enable() {
log(Level.INFO, "====================");
log(Level.INFO, "Slimefun Translation");
log(Level.INFO, " by ybw0014 ");
log(Level.INFO, "====================");

// config
configService = new ConfigurationService(this);

// registry
registry = new Registry();

// debug
debugEnabled = configService.isDebug();

// managers
userManager = new UserManager();
translationManager = new TranslationManager(this);
translationManager = new TranslationManager(this, getFile());
new CommandManager(this);
new ListenerManager(this);
new PacketListenerManager();

// metrics
setupMetrics();

getScheduler().runAsync(() -> {
translationManager.loadTranslations();
});
// delayed tasks
getScheduler().runAsync(() -> translationManager.loadTranslations());
}

@Override
Expand Down
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import net.guizhanss.slimefuntranslation.api.translations.FixedTranslation;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;

import net.guizhanss.slimefuntranslation.implementation.translations.FixedTranslation;

import org.bukkit.inventory.ItemStack;

/**
* This interface represents a translation.
Expand All @@ -13,8 +18,18 @@
*/
public interface Translation {
@Nonnull
String getDisplayName(String original);
String getDisplayName(@Nonnull String original);

@Nonnull
List<String> getLore(List<String> original);
List<String> getLore(@Nonnull List<String> original);

/**
* Override this method if you need extra check to make sure item can be translated.
* @param item The {@link ItemStack} to check.
* @return Whether the item can be translated.
*/
@ParametersAreNonnullByDefault
default boolean canTranslate(ItemStack item, SlimefunItem sfItem) {
return true;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
@Getter
public final class Registry {
private final Map<UUID, User> users = new HashMap<>();
private final Map<String, Translation> translations = new HashMap<>();
private final Map<String, Map<String, Translation>> translations = new HashMap<>();
}
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);
}
}
Loading

0 comments on commit 0a58f35

Please sign in to comment.