All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.legendofdragoon.modloader.ModContainer Maven / Gradle / Ivy

There is a newer version: 3.0.4
Show newest version
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:
   * 
    *
  1. Full locale code (see {@link Locale#toString()}
  2. *
  3. lang_COUNTRY (e.g. en_US)
  4. *
  5. lang (e.g. en)
  6. *
  7. en (fallback)
  8. *
  9. null (if en isn't found)
  10. *
*/ 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