generated from TobiasDeBruijn/BaseBukkitPlugin
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added LanguageModel - Added dependency ClassValidator to validate the language model - Version bump to 1.4.0
- Loading branch information
1 parent
d6d02a9
commit 8ee4955
Showing
13 changed files
with
236 additions
and
30 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
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
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
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
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
97 changes: 97 additions & 0 deletions
97
src/main/java/nl/thedutchmc/SkinFixer/language/LangHandler.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,97 @@ | ||
package nl.thedutchmc.SkinFixer.language; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.util.LinkedHashMap; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.yaml.snakeyaml.Yaml; | ||
|
||
import com.google.common.io.Files; | ||
import com.google.gson.Gson; | ||
|
||
import dev.array21.classvalidator.ClassValidator; | ||
import dev.array21.classvalidator.Pair; | ||
import nl.thedutchmc.SkinFixer.SkinFixer; | ||
import nl.thedutchmc.SkinFixer.util.Utils; | ||
|
||
public class LangHandler { | ||
|
||
private SkinFixer plugin; | ||
|
||
public static LanguageModel model; | ||
|
||
public LangHandler(SkinFixer plugin) { | ||
this.plugin = plugin; | ||
|
||
File langFolder = new File(this.plugin.getDataFolder() + File.separator + "langs"); | ||
if(!langFolder.exists()) { | ||
langFolder.mkdirs(); | ||
} | ||
} | ||
|
||
public void loadLang(String lang) { | ||
File langFile = new File(this.plugin.getDataFolder() + File.separator + "langs", lang + ".yml"); | ||
if(!langFile.exists()) { | ||
SkinFixer.logWarn("Failed to load language model " + lang + ".yml!"); | ||
langFile = getEngModel(); | ||
} | ||
|
||
LanguageModel model = loadModel(langFile); | ||
LangHandler.model = model; | ||
} | ||
|
||
private File getEngModel() { | ||
File langFile = new File(this.plugin.getDataFolder() + File.separator + "langs", "en.yml"); | ||
if(!langFile.exists()) { | ||
SkinFixer.logInfo("Language model 'en.yml' does not exist. Saving from JAR."); | ||
this.plugin.saveResource("en.yml", false); | ||
|
||
try { | ||
Files.move(new File(this.plugin.getDataFolder(), "en.yml"), langFile); | ||
} catch(IOException e) { | ||
SkinFixer.logWarn("Failed to save language model 'en.yml': " + e.getMessage()); | ||
SkinFixer.logWarn(Utils.getStackTrace(e)); | ||
|
||
Bukkit.getPluginManager().disablePlugin(this.plugin); | ||
return null; | ||
} | ||
} | ||
|
||
return langFile; | ||
} | ||
|
||
private LanguageModel loadModel(File modelFile) { | ||
final Yaml yaml = new Yaml(); | ||
final Gson gson = new Gson(); | ||
|
||
Object yamlData; | ||
try { | ||
yamlData = yaml.load(new FileInputStream(modelFile)); | ||
} catch(FileNotFoundException e) { | ||
SkinFixer.logWarn(String.format("Failed to load LanguuageModel '%s'. It does not exist.", modelFile.getAbsolutePath())); | ||
Bukkit.getPluginManager().disablePlugin(this.plugin); | ||
return null; | ||
} | ||
|
||
String jsonData = gson.toJson(yamlData, LinkedHashMap.class); | ||
LanguageModel model = gson.fromJson(jsonData, LanguageModel.class); | ||
|
||
Pair<Boolean, String> validationResult = ClassValidator.validateType(model); | ||
if(validationResult.getA() == null) { | ||
SkinFixer.logWarn("Failed to validate language model: " + validationResult.getB()); | ||
Bukkit.getPluginManager().disablePlugin(this.plugin); | ||
return null; | ||
} | ||
|
||
if(!validationResult.getA() ) { | ||
SkinFixer.logWarn(String.format("LanguageModel '%s' failed validation: %s", modelFile.getAbsolutePath(), validationResult.getB())); | ||
Bukkit.getPluginManager().disablePlugin(this.plugin); | ||
return null; | ||
} | ||
|
||
return model; | ||
} | ||
} |
Oops, something went wrong.