Skip to content

Commit

Permalink
Fix github #7
Browse files Browse the repository at this point in the history
- Added LanguageModel
- Added dependency ClassValidator to validate the language model
- Version bump to 1.4.0
  • Loading branch information
TobiasDeBruijn committed May 26, 2021
1 parent d6d02a9 commit 8ee4955
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 30 deletions.
11 changes: 7 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ allprojects {
processResources {
from(sourceSets.main.resources.srcDirs) {
filter ReplaceTokens, tokens: [version: version]
filter ReplaceTokens, tokens: [name: pluginName]
filter ReplaceTokens, tokens: [groupId: pluginGroup]
filter ReplaceTokens, tokens: [author: pluginAuthor]
filter ReplaceTokens, tokens: [apiVersion: pluginApiVersion]
filter ReplaceTokens, tokens: [name: pluginName]
filter ReplaceTokens, tokens: [groupId: pluginGroup]
filter ReplaceTokens, tokens: [author: pluginAuthor]
filter ReplaceTokens, tokens: [apiVersion: pluginApiVersion]
}
}
}
Expand All @@ -52,6 +52,8 @@ dependencies {

//Spigot API
compileOnly 'org.spigotmc:spigot-api:1.16.1-R0.1-SNAPSHOT'

compile 'dev.array21:classvalidator:1.0.0'

}

Expand All @@ -61,6 +63,7 @@ shadowJar() {
relocate 'com.google.code.gson', 'nl.thedutchmc.skinfixer.libs.com.google.code.gson'
relocate 'nl.thedutchmc.httplib', 'nl.thedutchmc.skinfixer.libs.nl.thedutchmc.httplib'
relocate 'org.slf4j', 'nl.thedutchmc.skinfixer.libs.org.slf4j'
relocate 'dev.array21.classvalidator', 'nl.thedutchmc.skinfixer.libs.dev.array21.classvalidator'
}

task testJar(type: ShadowJar) {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pluginVersion = 1.3.1
pluginVersion = 1.4.0
pluginGroup = nl.thedutchmc
pluginName = SkinFixer
pluginAuthor = TheDutchMC
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/nl/thedutchmc/SkinFixer/SkinFixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import nl.thedutchmc.SkinFixer.commandexecutors.SkinFixerCommandExecutor;
import nl.thedutchmc.SkinFixer.fileHandlers.ConfigurationHandler;
import nl.thedutchmc.SkinFixer.fileHandlers.StorageHandler;
import nl.thedutchmc.SkinFixer.language.LangHandler;
import nl.thedutchmc.SkinFixer.minecraftevents.PlayerJoinEventListener;

public class SkinFixer extends JavaPlugin {
Expand All @@ -35,12 +36,18 @@ public void onEnable() {
ConfigurationHandler configHandler = new ConfigurationHandler();
configHandler.loadConfig();

LangHandler langHandler = new LangHandler(this);
if(ConfigurationHandler.language != null) {
langHandler.loadLang(ConfigurationHandler.language);
} else {
SkinFixer.logWarn("Configuration entry 'language' is missing. Using English as default.");
langHandler.loadLang("en");
}

//Storage of skins and pending keys
STORAGE = new StorageHandler();
STORAGE.loadConfig();

if(Bukkit.getOnlineMode() == true) logInfo("This plugin is not needed on servers running in online mode!");


//Register command executors
this.getCommand("setskin").setExecutor(new SetSkinCommandExecutor());
this.getCommand("getcode").setExecutor(new GetCodeCommandExecutor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import nl.thedutchmc.SkinFixer.apis.MineskinApi;
import nl.thedutchmc.SkinFixer.fileHandlers.StorageHandler;
import nl.thedutchmc.SkinFixer.gson.GetSkinResponse;
import nl.thedutchmc.SkinFixer.language.LangHandler;
import nl.thedutchmc.SkinFixer.util.ReflectionUtil;
import nl.thedutchmc.SkinFixer.util.Triple;

Expand All @@ -35,7 +36,8 @@ public static void changeSkinJson(String skinUrl, UUID internalUuid, UUID extern
public void run() {

Player player = Bukkit.getPlayer(internalUuid);
player.sendMessage(ChatColor.GOLD + "Fetching skin value and signature...");
//player.sendMessage(ChatColor.GOLD + "Fetching skin value and signature...");
player.sendMessage(ChatColor.GOLD + LangHandler.model.skinFetching);

//Fetch the skin from Mineskin.org's API
Triple<Boolean, GetSkinResponse, String> apiResponse;
Expand All @@ -46,7 +48,8 @@ public void run() {
}

if(!apiResponse.getA()) {
player.sendMessage(ChatColor.RED + "Something went wrong applying your skin:\n" + ChatColor.GRAY + apiResponse.getC());
//player.sendMessage(ChatColor.RED + "Something went wrong applying your skin:\n" + ChatColor.GRAY + apiResponse.getC());
player.sendMessage(ChatColor.RED + LangHandler.model.skinApplyFailed.replaceAll("%ERROR%", ChatColor.GRAY + apiResponse.getC() + ChatColor.RED));
return;
}

Expand All @@ -63,7 +66,6 @@ public static void changeSkinFromObject(SkinObject skin) {
private static void changeSkin(String skinValue, String skinSignature, UUID caller, boolean slim ) {
Player player = Bukkit.getPlayer(caller);

//Store the skin to the storage file, so it can be reapplied when they join.
if(StorageHandler.skins.containsKey(caller)) {
SkinObject skin = StorageHandler.skins.get(caller);
if(slim) skin.setSlim(true);
Expand All @@ -74,13 +76,13 @@ private static void changeSkin(String skinValue, String skinSignature, UUID call
StorageHandler.skins.put(caller, skin);
}

player.sendMessage(ChatColor.GOLD + "Applying skin...");
player.sendMessage(ChatColor.GOLD + LangHandler.model.skinApplying);

applySkin(player, skinValue, skinSignature);
reloadPlayer(player);

//Inform the player that we're done
player.sendMessage(ChatColor.GOLD + "Done.");
player.sendMessage(ChatColor.GOLD + LangHandler.model.skinApplied);
}

private static void applySkin(Player player, String skinValue, String skinSignature) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,33 @@

import net.md_5.bungee.api.ChatColor;
import nl.thedutchmc.SkinFixer.common.AddNewSkin;
import nl.thedutchmc.SkinFixer.language.LangHandler;

public class GetCodeCommandExecutor implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

if(!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "This command is for players only!");
sender.sendMessage(ChatColor.RED + LangHandler.model.commandPlayerOnly);
return true;
}

if(!sender.hasPermission("skinfixer.getcode") ) {
sender.sendMessage(ChatColor.RED + "You don't have permission to use this command!");
//sender.sendMessage(ChatColor.RED + "You don't have permission to use this command!");
sender.sendMessage(ChatColor.RED + LangHandler.model.commandNoPermission);
return true;
}

if(args.length == 0) {
sender.sendMessage(ChatColor.RED + "You need to provide a URL to your skin!");
//sender.sendMessage(ChatColor.RED + "You need to provide a URL to your skin!");
sender.sendMessage(ChatColor.RED + LangHandler.model.getCodeUrlRequired);
return true;
}

int code = AddNewSkin.add(args[0]);
sender.sendMessage(ChatColor.GOLD + "Your skin has been added! You can apply it with /setskin " + code);

//sender.sendMessage(ChatColor.GOLD + "Your skin has been added! You can apply it with /setskin " + code);
sender.sendMessage(ChatColor.GOLD + LangHandler.model.getCodeSkinAdded.replaceAll("%CODE%", ChatColor.RED + String.valueOf(code) + ChatColor.GOLD));
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,39 @@
import nl.thedutchmc.SkinFixer.SkinFixer;
import nl.thedutchmc.SkinFixer.changeSkin.SkinChangeHandler;
import nl.thedutchmc.SkinFixer.fileHandlers.StorageHandler;
import nl.thedutchmc.SkinFixer.language.LangHandler;

public class SetSkinCommandExecutor implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

if(!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "This command is for players only!");
sender.sendMessage(ChatColor.RED + LangHandler.model.commandPlayerOnly);
return true;
}

if(!sender.hasPermission("skinfixer.setskin") ) {
sender.sendMessage(ChatColor.RED + "You don't have permission to use this command!");
sender.sendMessage(ChatColor.RED + LangHandler.model.commandNoPermission);
return true;
}

if(args.length == 0) {
sender.sendMessage(ChatColor.RED + "You need to provide a code!");
sender.sendMessage(ChatColor.RED + LangHandler.model.setSkinCodeRequired);
return true;
}

int code = 0;
try {
code = getIntFromString(args[0]);
} catch(NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "The code you entered is not a number!");
sender.sendMessage(ChatColor.RED + LangHandler.model.setSkinCodeNotANumber);
return true;
}

if(!StorageHandler.pendingLinks.containsKey(code)) {
sender.sendMessage(ChatColor.RED + "Unkown code!");
//sender.sendMessage(ChatColor.RED + "Unkown code!");
sender.sendMessage(ChatColor.RED + LangHandler.model.setSkinCodeUnknown);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import net.md_5.bungee.api.ChatColor;
import nl.thedutchmc.SkinFixer.SkinFixer;
import nl.thedutchmc.SkinFixer.language.LangHandler;

public class SkinFixerCommandExecutor implements CommandExecutor {

Expand All @@ -16,23 +17,23 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
final ChatColor cw = ChatColor.WHITE;

if(args.length == 0) {
sender.sendMessage(cg + "No option provided. See " + ChatColor.RED + "/skinfixer help " + cg + "for help!");
sender.sendMessage(ChatColor.GOLD + LangHandler.model.skinFixerNoOptionProvided);
return true;
}

if(args[0].equals("help")) {
sender.sendMessage(cg + "SkinFixer help");
sender.sendMessage(cg + "------------");
sender.sendMessage("- " + cg + "/setskin <code> [slim true/false]" + cw + " Set your skin from a code.");
sender.sendMessage("- " + cg + "/getcode <url>" + cw + " Generate a code from a Skin url. URL must be the skinfie itself.");
sender.sendMessage("- " + cg + "/skinfixer help" + cw + " Shows this page");
sender.sendMessage("- " + cg + "/skinfixer version" + cw + " Returns the version of SkinFixer you are using");
sender.sendMessage("- " + cg + "/setskin <code> [slim true/false]" + cw + " " + LangHandler.model.skinFixerSetSkinHelp);
sender.sendMessage("- " + cg + "/getcode <url>" + cw + " " + LangHandler.model.skinFixerGetCodeHelp);
sender.sendMessage("- " + cg + "/skinfixer help" + cw + " " + LangHandler.model.skinFixerShowHelp);
sender.sendMessage("- " + cg + "/skinfixer version" + cw + " " + LangHandler.model.skinFixerVersionHelp);

return true;
}

if(args[0].equals("version")) {
sender.sendMessage(cg + "You are using SkinFixer version " + ChatColor.RED + SkinFixer.PLUGIN_VERSION);
sender.sendMessage(cg + LangHandler.model.skinFixerVersion.replaceAll("%VERSION%", ChatColor.RED + SkinFixer.PLUGIN_VERSION + ChatColor.GOLD));

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import nl.thedutchmc.SkinFixer.JdaHandler;
import nl.thedutchmc.SkinFixer.common.AddNewSkin;
import nl.thedutchmc.SkinFixer.language.LangHandler;

public class MessageReceivedEventListener extends ListenerAdapter {

Expand All @@ -25,7 +26,7 @@ public void onMessageReceived(MessageReceivedEvent event) {
for(Attachment a : attachments) {
int n = AddNewSkin.add(a.getUrl());

msgChannel.sendMessage("You can set this as your skin in-game using /setskin " + n).queue();
msgChannel.sendMessage(LangHandler.model.discordSetSkin.replaceAll("%CODE%", String.valueOf(n))).queue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public class ConfigurationHandler {
public static boolean useDiscord;
public static String token, channel;

/**
* @since 1.4.0
*/
public static String language;

private File file;
private FileConfiguration config;

Expand Down Expand Up @@ -53,5 +58,7 @@ public void readConfig() {
useDiscord = false;
}
}

language = this.getConfig().getString("language");
}
}
97 changes: 97 additions & 0 deletions src/main/java/nl/thedutchmc/SkinFixer/language/LangHandler.java
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;
}
}
Loading

0 comments on commit 8ee4955

Please sign in to comment.