Current work
This commit is contained in:
@@ -36,8 +36,8 @@ public class DisguiseParser {
|
||||
super();
|
||||
}
|
||||
|
||||
public DisguiseParseException(String string) {
|
||||
super(string);
|
||||
public DisguiseParseException(LibsMessages message, String... params) {
|
||||
super(message.get(params));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -467,11 +467,12 @@ public class DisguiseParser {
|
||||
public static Disguise parseDisguise(CommandSender sender, String permNode, String[] args,
|
||||
HashMap<DisguisePerm, HashMap<ArrayList<String>, Boolean>> permissionMap) throws DisguiseParseException, IllegalAccessException, InvocationTargetException {
|
||||
if (permissionMap.isEmpty()) {
|
||||
throw new DisguiseParseException(ChatColor.RED + "You are forbidden to use this command.");
|
||||
throw new DisguiseParseException(
|
||||
TranslateType.MESSAGE.get(ChatColor.RED + "You are forbidden to use this " + "command."));
|
||||
}
|
||||
|
||||
if (args.length == 0) {
|
||||
throw new DisguiseParseException("No arguments defined");
|
||||
throw new DisguiseParseException(TranslateType.MESSAGE.get("No arguments defined"));
|
||||
}
|
||||
|
||||
// How many args to skip due to the disugise being constructed
|
||||
@@ -487,15 +488,17 @@ public class DisguiseParser {
|
||||
disguise = DisguiseUtilities.getClonedDisguise(args[0].toLowerCase());
|
||||
|
||||
if (disguise == null) {
|
||||
throw new DisguiseParseException(
|
||||
ChatColor.RED + "Cannot find a disguise under the reference " + args[0]);
|
||||
throw new DisguiseParseException(String.format(
|
||||
TranslateType.MESSAGE.get(ChatColor.RED + "Cannot find a disguise under the reference %s"),
|
||||
args[0]));
|
||||
}
|
||||
} else {
|
||||
throw new DisguiseParseException(
|
||||
ChatColor.RED + "You do not have perimssion to use disguise references!");
|
||||
throw new DisguiseParseException(TranslateType.MESSAGE.get(
|
||||
ChatColor.RED + "You do not have perimssion to use disguise references!"));
|
||||
}
|
||||
|
||||
optionPermissions = (permissionMap.containsKey(disguise.getType()) ? permissionMap.get(disguise.getType()) :
|
||||
optionPermissions = (permissionMap.containsKey(new DisguisePerm(disguise.getType())) ?
|
||||
permissionMap.get(new DisguisePerm(disguise.getType())) :
|
||||
new HashMap<ArrayList<String>, Boolean>());
|
||||
} else {
|
||||
DisguisePerm disguisePerm = getDisguisePerm(args[0]);
|
||||
@@ -506,21 +509,24 @@ public class DisguiseParser {
|
||||
}
|
||||
|
||||
if (disguisePerm == null) {
|
||||
throw new DisguiseParseException(
|
||||
ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + args[0] + ChatColor.RED + " doesn't exist!");
|
||||
throw new DisguiseParseException(String.format(TranslateType.MESSAGE.get(
|
||||
ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + "%s" + ChatColor.RED + " " + "doesn't exist!"),
|
||||
args[0]));
|
||||
}
|
||||
|
||||
if (disguisePerm.isUnknown()) {
|
||||
throw new DisguiseParseException(
|
||||
ChatColor.RED + "Error! You cannot disguise as " + ChatColor.GREEN + "Unknown!");
|
||||
throw new DisguiseParseException(TranslateType.MESSAGE.get(
|
||||
ChatColor.RED + "Error! You cannot disguise as " + ChatColor.GREEN + "Unknown!"));
|
||||
}
|
||||
|
||||
if (disguisePerm.getEntityType() == null) {
|
||||
throw new DisguiseParseException(ChatColor.RED + "Error! This disguise couldn't be loaded!");
|
||||
throw new DisguiseParseException(
|
||||
TranslateType.MESSAGE.get(ChatColor.RED + "Error! This disguise " + "couldn't be loaded!"));
|
||||
}
|
||||
|
||||
if (!permissionMap.containsKey(disguisePerm)) {
|
||||
throw new DisguiseParseException(ChatColor.RED + "You are forbidden to use this disguise.");
|
||||
throw new DisguiseParseException(
|
||||
TranslateType.MESSAGE.get(ChatColor.RED + "You are forbidden to use " + "this disguise."));
|
||||
}
|
||||
|
||||
optionPermissions = permissionMap.get(disguisePerm);
|
||||
@@ -532,12 +538,13 @@ public class DisguiseParser {
|
||||
// If he is doing a player disguise
|
||||
if (args.length == 1) {
|
||||
// He needs to give the player name
|
||||
throw new DisguiseParseException(ChatColor.RED + "Error! You need to give a player name!");
|
||||
throw new DisguiseParseException(TranslateType.MESSAGE.get(
|
||||
ChatColor.RED + "Error! You need " + "to give a player name!"));
|
||||
} else {
|
||||
if (!disguiseOptions.isEmpty() && (!disguiseOptions.containsKey(
|
||||
args[1].toLowerCase()) || !disguiseOptions.get(args[1].toLowerCase()))) {
|
||||
throw new DisguiseParseException(
|
||||
ChatColor.RED + "Error! You don't have permission to use that name!");
|
||||
throw new DisguiseParseException(TranslateType.MESSAGE.get(
|
||||
ChatColor.RED + "Error! You don't have permission to use that name!"));
|
||||
}
|
||||
|
||||
args[1] = args[1].replace("\\_", " ");
|
||||
@@ -604,8 +611,9 @@ public class DisguiseParser {
|
||||
case WITHER_SKULL:
|
||||
break;
|
||||
default:
|
||||
throw new DisguiseParseException(
|
||||
ChatColor.RED + "Error! " + disguisePerm.toReadable() + " doesn't know what to do with " + args[1] + "!");
|
||||
throw new DisguiseParseException(String.format(TranslateType.MESSAGE.get(
|
||||
ChatColor.RED + "Error! %s doesn't know" + " " + "what to do with %s!"),
|
||||
disguisePerm.toReadable(), args[1]));
|
||||
}
|
||||
toSkip++;
|
||||
// If they also defined a data value
|
||||
@@ -615,8 +623,10 @@ public class DisguiseParser {
|
||||
}
|
||||
if (secondArg != null) {
|
||||
if (disguisePerm.getType() != DisguiseType.FALLING_BLOCK && disguisePerm.getType() != DisguiseType.DROPPED_ITEM) {
|
||||
throw new DisguiseParseException(
|
||||
ChatColor.RED + "Error! Only the disguises " + DisguiseType.FALLING_BLOCK.toReadable() + " and " + DisguiseType.DROPPED_ITEM.toReadable() + " uses a second number!");
|
||||
throw new DisguiseParseException(String.format(TranslateType.MESSAGE.get(
|
||||
ChatColor.RED + "Error! Only the disguises " + DisguiseType.FALLING_BLOCK.toReadable() + " and " + DisguiseType.DROPPED_ITEM.toReadable() + " uses a second number!"),
|
||||
DisguiseType.FALLING_BLOCK.toReadable(),
|
||||
DisguiseType.DROPPED_ITEM.toReadable()));
|
||||
}
|
||||
miscData = Integer.parseInt(secondArg);
|
||||
}
|
||||
@@ -635,8 +645,9 @@ public class DisguiseParser {
|
||||
}
|
||||
|
||||
if (!disguiseOptions.containsKey(toCheck) || !disguiseOptions.get(toCheck)) {
|
||||
throw new DisguiseParseException(
|
||||
ChatColor.RED + "Error! You do not have permission to use the parameter " + toCheck + " on the " + disguisePerm.toReadable() + " disguise!");
|
||||
throw new DisguiseParseException(String.format(TranslateType.MESSAGE.get(
|
||||
ChatColor.RED + "Error! You do not have permission to use the parameter %s on the" + " %s disguise!"),
|
||||
toCheck, disguisePerm.toReadable()));
|
||||
}
|
||||
}
|
||||
|
||||
|
22
src/me/libraryaddict/disguise/utilities/LibsMessages.java
Normal file
22
src/me/libraryaddict/disguise/utilities/LibsMessages.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package me.libraryaddict.disguise.utilities;
|
||||
|
||||
/**
|
||||
* Created by libraryaddict on 15/06/2017.
|
||||
*/
|
||||
public enum LibsMessages {
|
||||
// Format being CLASS_STRING. So no perm = DISG_COMMAND_NO_PERM. Or DISG_PARSE_NO_PERM_OPTION
|
||||
TEST("This is a test string");
|
||||
|
||||
private String string;
|
||||
|
||||
LibsMessages(String string) {
|
||||
this.string = string;
|
||||
}
|
||||
|
||||
public String get(String... strings) {
|
||||
if (strings.length == 0)
|
||||
return TranslateType.MESSAGE.get(string);
|
||||
|
||||
return String.format(TranslateType.MESSAGE.get(string), (Object[]) strings);
|
||||
}
|
||||
}
|
@@ -149,7 +149,7 @@ public class ReflectionFlagWatchers {
|
||||
new ParamInfo(Ocelot.Type.class, "Ocelot Type", "View all the ocelot types you can use for ocelots");
|
||||
new ParamInfo(Villager.Profession.class, "Villager Profession",
|
||||
"View all the professions you can set on a villager");
|
||||
new ParamInfo(BlockFace.class, Arrays.copyOf(BlockFace.values(), 5), "Direction",
|
||||
new ParamInfo(BlockFace.class, Arrays.copyOf(BlockFace.values(), 4), "Direction",
|
||||
"View the five directions usable on player setSleeping disguise");
|
||||
new ParamInfo(Rabbit.Type.class, "Rabbit Type", "View the kinds of rabbits you can turn into");
|
||||
new ParamInfo(TreeSpecies.class, "Tree Species", "View the different types of tree species");
|
||||
@@ -178,7 +178,7 @@ public class ReflectionFlagWatchers {
|
||||
|
||||
new ParamInfo(ItemStack.class, "Item (id:damage)", "An ItemStack compromised of ID:Durability", materials);
|
||||
|
||||
new ParamInfo(ItemStack[].class, "Four ItemStacks (id:damage,id:damage..)", "Four ItemStacks seperated by an ,",
|
||||
new ParamInfo(ItemStack[].class, "Four ItemStacks (id:damage,id:damage..)", "Four ItemStacks separated by an ,",
|
||||
materials) {
|
||||
@Override
|
||||
public String[] getEnums(String tabComplete) {
|
||||
@@ -203,14 +203,14 @@ public class ReflectionFlagWatchers {
|
||||
potionEnums.toArray(new String[0]));
|
||||
new ParamInfo(String.class, "Text", "A line of text");
|
||||
new ParamInfo(boolean.class, "True/False", "True or False", new String[]{"true", "false"});
|
||||
new ParamInfo(int.class, "Number", "A whole number, no decimcals");
|
||||
new ParamInfo(int.class, "Number", "A whole number, no decimals");
|
||||
new ParamInfo(double.class, "Number", "A number which can have decimals");
|
||||
new ParamInfo(float.class, "Number", "A number which can have decimals");
|
||||
|
||||
new ParamInfo(Horse.Style.class, "Horse Style", "Horse style which is the patterns on the horse");
|
||||
new ParamInfo(int[].class, "number,number,number...", "Numbers seperated by an ,");
|
||||
new ParamInfo(int[].class, "number,number,number..", "Numbers separated by an ,");
|
||||
|
||||
new ParamInfo(BlockPosition.class, "Block Position (num,num,num)", "Three numbers seperated by an ,");
|
||||
new ParamInfo(BlockPosition.class, "Block Position (num,num,num)", "Three numbers separated by a ,");
|
||||
new ParamInfo(GameProfile.class, "GameProfile",
|
||||
"Get the gameprofile here https://sessionserver.mojang.com/session/minecraft/profile/PLAYER_UUID_GOES_HERE?unsigned=false");
|
||||
|
||||
@@ -223,7 +223,7 @@ public class ReflectionFlagWatchers {
|
||||
}
|
||||
|
||||
public static Method[] getDisguiseWatcherMethods(Class<? extends FlagWatcher> watcherClass) {
|
||||
ArrayList<Method> methods = new ArrayList<Method>(Arrays.asList(watcherClass.getMethods()));
|
||||
ArrayList<Method> methods = new ArrayList<>(Arrays.asList(watcherClass.getMethods()));
|
||||
|
||||
Iterator<Method> itel = methods.iterator();
|
||||
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package me.libraryaddict.disguise.utilities;
|
||||
|
||||
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -15,8 +16,11 @@ public class TranslateFiller {
|
||||
if (!info.isEnums())
|
||||
continue;
|
||||
|
||||
if (info.getParamClass() == ItemStack.class || info.getParamClass() == ItemStack[].class)
|
||||
continue;
|
||||
|
||||
for (String e : info.getEnums("")) {
|
||||
TranslateType.METHOD_PARAM.get(e, "Name for the param for " + info.getName());
|
||||
TranslateType.METHOD_PARAM.get(e, "Used as a disguise option for " + info.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,10 +28,23 @@ public class TranslateFiller {
|
||||
type.toReadable();
|
||||
|
||||
for (Method method : ReflectionFlagWatchers.getDisguiseWatcherMethods(type.getWatcherClass())) {
|
||||
Class para = method.getParameterTypes()[0];
|
||||
String className = method.getDeclaringClass().getSimpleName().replace("Watcher", "");
|
||||
|
||||
if (className.equals("Flag") || className.equals("Disguise"))
|
||||
className = "Entity";
|
||||
else if (className.equals("Living"))
|
||||
className = "Living Entity";
|
||||
else if (className.equals("AbstractHorse"))
|
||||
className = "Horse";
|
||||
else if (className.equals("DroppedItem"))
|
||||
className = "Item";
|
||||
else if (className.equals("IllagerWizard"))
|
||||
className = "Illager";
|
||||
|
||||
TranslateType.METHOD.get(method.getName(),
|
||||
"Found in " + method.getDeclaringClass().getSimpleName().replace("Watcher",
|
||||
"") + " and accepts as a parameter " + TranslateType.METHOD_PARAM.get(
|
||||
method.getParameterTypes()[0].getSimpleName()));
|
||||
"Found in the disguise options for " + className + " and uses " + (para.isArray() ?
|
||||
"multiple" + " " : "a ") + para.getSimpleName().replace("[]", "s"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,11 +1,15 @@
|
||||
package me.libraryaddict.disguise.utilities;
|
||||
|
||||
import me.libraryaddict.disguise.DisguiseConfig;
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -13,46 +17,80 @@ import java.util.Objects;
|
||||
* Created by libraryaddict on 10/06/2017.
|
||||
*/
|
||||
public enum TranslateType {
|
||||
DISGUISE("disguise_names"), MESSAGE("messages"), METHOD_PARAM("option_names"), METHOD("disguise_options");
|
||||
DISGUISE("disguises"), MESSAGE("messages"), METHOD_PARAM("disguise_options"), METHOD("disguise_option_parameters");
|
||||
private File file;
|
||||
private YamlConfiguration config;
|
||||
private HashMap<String, String> translated = new HashMap<>();
|
||||
|
||||
TranslateType(String fileName) {
|
||||
file = new File("translate", fileName + ".yml");
|
||||
file = new File("plugins/LibsDisguises/Translations", fileName + ".yml");
|
||||
reload();
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
if (!file.exists())
|
||||
file.getParentFile().mkdirs();
|
||||
|
||||
try {
|
||||
file.createNewFile();
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
public static void reloadTranslations() {
|
||||
for (TranslateType type : values()) {
|
||||
type.reload();
|
||||
}
|
||||
|
||||
config = YamlConfiguration.loadConfiguration(file);
|
||||
TranslateFiller.fillConfigs();
|
||||
}
|
||||
|
||||
private YamlConfiguration getConfig() {
|
||||
return config;
|
||||
private void reload() {
|
||||
if (!LibsPremium.isPremium() || !DisguiseConfig.isUseTranslations())
|
||||
return;
|
||||
|
||||
translated.clear();
|
||||
|
||||
if (!file.exists())
|
||||
return;
|
||||
|
||||
System.out.println("[LibsDisguises] Loading translations: " + name());
|
||||
YamlConfiguration config = new YamlConfiguration();
|
||||
config.options().pathSeparator(Character.toChars(0)[0]);
|
||||
|
||||
try {
|
||||
config.load(file);
|
||||
|
||||
for (String key : config.getKeys(false)) {
|
||||
String value = config.getString(key);
|
||||
|
||||
if (value == null)
|
||||
System.err.println("Translation for " + name() + " has a null value for the key '" + key + "'");
|
||||
else
|
||||
translated.put(key, value);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public void save(String message, String comment) {
|
||||
message = StringEscapeUtils.escapeJson(message);
|
||||
|
||||
if (getConfig().contains(message))
|
||||
private void save(String message, String comment) {
|
||||
if (translated.containsKey(message))
|
||||
return;
|
||||
|
||||
translated.put(message, message);
|
||||
|
||||
message = StringEscapeUtils.escapeJava(message);
|
||||
|
||||
try {
|
||||
PrintWriter writer = new PrintWriter(getFile());
|
||||
writer.write((comment != null ? "# " + comment + "\n" : "") + message + ": " + message + "\n");
|
||||
boolean exists = file.exists();
|
||||
|
||||
if (!exists) {
|
||||
file.getParentFile().mkdirs();
|
||||
file.createNewFile();
|
||||
}
|
||||
|
||||
FileWriter writer = new FileWriter(getFile(), true);
|
||||
|
||||
if (!exists)
|
||||
writer.write("# To use translations in Lib's Disguises, you must have the purchased plugin\n");
|
||||
|
||||
writer.write("\n" + (comment != null ? "# " + comment + "\n" :
|
||||
"") + "\"" + message + "\": \"" + message + "\"\n");
|
||||
|
||||
writer.close();
|
||||
}
|
||||
@@ -62,10 +100,10 @@ public enum TranslateType {
|
||||
}
|
||||
|
||||
public String reverseGet(String translated) {
|
||||
translated = StringEscapeUtils.unescapeJson(translated).toLowerCase();
|
||||
translated = translated.toLowerCase();
|
||||
|
||||
for (Map.Entry<String, Object> entry : getConfig().getValues(false).entrySet()) {
|
||||
if (!Objects.equals(entry.getValue().toString().toLowerCase(), translated))
|
||||
for (Map.Entry<String, String> entry : this.translated.entrySet()) {
|
||||
if (!Objects.equals(entry.getValue().toLowerCase(), translated))
|
||||
continue;
|
||||
|
||||
return entry.getKey();
|
||||
@@ -82,10 +120,15 @@ public enum TranslateType {
|
||||
}
|
||||
|
||||
public String get(String message, String comment) {
|
||||
String msg = getConfig().getString(StringEscapeUtils.escapeJson(message));
|
||||
if (!LibsPremium.isPremium() || !DisguiseConfig.isUseTranslations())
|
||||
return message;
|
||||
System.out.println("1");
|
||||
|
||||
String msg = translated.get(message);
|
||||
|
||||
if (msg != null)
|
||||
return msg;
|
||||
System.out.println("2");
|
||||
|
||||
save(message, comment);
|
||||
|
||||
|
@@ -13,7 +13,6 @@ public class SerializerGameProfile implements JsonSerializer<WrappedGameProfile>
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(WrappedGameProfile src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
System.out.println(src.getHandle().toString());
|
||||
return context.serialize(src.getHandle(), GameProfile.class);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user