org.legendofdragoon.modloader.ModContainer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mod-loader Show documentation
Show all versions of mod-loader Show documentation
The mod loader used by various Severed Chains projects
package org.legendofdragoon.modloader;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
public class ModContainer {
public final String modId;
public final Object mod;
ModState state = ModState.INITIALIZED;
public ModContainer(final String modId, final Object mod) {
this.modId = modId;
this.mod = mod;
}
public URL getResource(final String path) {
return this.mod.getClass().getClassLoader().getResource(this.modId + '/' + path);
}
/**
* Searches for a lang file in the following order:
*
* - Full locale code (see {@link Locale#toString()}
* - lang_COUNTRY (e.g. en_US)
* - lang (e.g. en)
* - en (fallback)
* - null (if en isn't found)
*
*/
private URL getLangResource(final Locale locale) {
final URL specific = this.getResource("lang/%s.lang".formatted(locale));
if(specific != null) {
return specific;
}
final URL langCountry = this.getResource("lang/%s_%s.lang".formatted(locale.getLanguage(), locale.getCountry()));
if(langCountry != null) {
return langCountry;
}
final URL lang = this.getResource("lang/%s.lang".formatted(locale.getLanguage()));
if(lang != null) {
return lang;
}
return this.getResource("lang/en.lang");
}
public Map loadLang(final Locale locale) throws IOException {
final URL langUrl = this.getLangResource(locale);
if(langUrl == null) {
return Map.of();
}
final Map lang = new HashMap<>();
final Properties properties = new Properties();
properties.load(langUrl.openStream());
for(final Object key : properties.keySet()) {
lang.put((String)key, properties.getProperty((String)key));
}
return lang;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy