Skip to content

Commit

Permalink
Refactored all constant strings into the Names class and added feedba…
Browse files Browse the repository at this point in the history
…ck messages to reload/load/save.
Lunatrius committed Sep 4, 2014
1 parent 4be95e1 commit b4da690
Showing 6 changed files with 112 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
import com.github.lunatrius.ingameinfo.printer.json.JsonPrinter;
import com.github.lunatrius.ingameinfo.printer.text.TextPrinter;
import com.github.lunatrius.ingameinfo.printer.xml.XmlPrinter;
import com.github.lunatrius.ingameinfo.reference.Names;
import com.github.lunatrius.ingameinfo.reference.Reference;
import com.github.lunatrius.ingameinfo.tag.Tag;
import com.github.lunatrius.ingameinfo.value.Value;
@@ -66,15 +67,15 @@ public File getConfigDirectory() {
public boolean setConfigFile(String filename) {
File file = new File(this.configDirectory, filename);
if (file.exists()) {
if (filename.endsWith(".xml")) {
if (filename.endsWith(Names.Files.EXT_XML)) {
this.configFile = file;
this.parser = new XmlParser();
return true;
} else if (filename.endsWith(".json")) {
} else if (filename.endsWith(Names.Files.EXT_JSON)) {
this.configFile = file;
this.parser = new JsonParser();
return true;
} else if (filename.endsWith(".txt")) {
} else if (filename.endsWith(Names.Files.EXT_TXT)) {
this.configFile = file;
this.parser = new TextParser();
return true;
@@ -213,7 +214,7 @@ private InputStream getInputStream() {
inputStream = new FileInputStream(this.configFile);
} else {
Reference.logger.debug("Loading default config...");
ResourceLocation resourceLocation = new ResourceLocation("ingameinfo", "InGameInfo.xml");
ResourceLocation resourceLocation = new ResourceLocation("ingameinfo", Names.Files.FILE_XML);
IResource resource = this.minecraft.getResourceManager().getResource(resourceLocation);
inputStream = resource.getInputStream();
}
@@ -227,11 +228,11 @@ private InputStream getInputStream() {
public boolean saveConfig(String filename) {
IPrinter printer = null;
File file = new File(this.configDirectory, filename);
if (filename.endsWith(".xml")) {
if (filename.endsWith(Names.Files.EXT_XML)) {
printer = new XmlPrinter();
} else if (filename.endsWith(".json")) {
} else if (filename.endsWith(Names.Files.EXT_JSON)) {
printer = new JsonPrinter();
} else if (filename.endsWith(".txt")) {
} else if (filename.endsWith(Names.Files.EXT_TXT)) {
printer = new TextPrinter();
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.lunatrius.ingameinfo.client.gui;

import com.github.lunatrius.ingameinfo.handler.ConfigurationHandler;
import com.github.lunatrius.ingameinfo.reference.Names;
import com.github.lunatrius.ingameinfo.reference.Reference;
import cpw.mods.fml.client.config.GuiConfig;
import cpw.mods.fml.client.config.IConfigElement;
@@ -18,7 +19,7 @@ public GuiModConfig(GuiScreen guiScreen) {
private static List<IConfigElement> getConfigElements() {
List<IConfigElement> elements = new ArrayList<IConfigElement>();
for (String name : ConfigurationHandler.configuration.getCategoryNames()) {
elements.add(new ConfigElement(ConfigurationHandler.configuration.getCategory(name).setLanguageKey(String.format("%s.category.%s", ConfigurationHandler.LANG_PREFIX, name))));
elements.add(new ConfigElement(ConfigurationHandler.configuration.getCategory(name).setLanguageKey(Names.Config.LANG_PREFIX + ".category." + name)));
}
return elements;
}
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import com.github.lunatrius.ingameinfo.handler.ConfigurationHandler;
import com.github.lunatrius.ingameinfo.handler.DelayedGuiDisplayTicker;
import com.github.lunatrius.ingameinfo.handler.Ticker;
import com.github.lunatrius.ingameinfo.reference.Names;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
@@ -20,12 +21,12 @@ public class InGameInfoCommand extends CommandBase {

@Override
public String getCommandName() {
return "igi";
return Names.Command.NAME;
}

@Override
public String getCommandUsage(ICommandSender commandSender) {
return "commands.ingameinfoxml.usage";
return Names.Command.Message.USAGE;
}

@Override
@@ -36,12 +37,12 @@ public boolean canCommandSenderUseCommand(ICommandSender par1ICommandSender) {
@Override
public List addTabCompletionOptions(ICommandSender commandSender, String[] args) {
if (args.length == 1) {
return getListOfStringsMatchingLastWord(args, "reload", "load", "save", "enable", "disable", "taglist");
return getListOfStringsMatchingLastWord(args, Names.Command.RELOAD, Names.Command.LOAD, Names.Command.SAVE, Names.Command.ENABLE, Names.Command.DISABLE, Names.Command.TAGLIST);
} else if (args.length == 2) {
if (args[0].equalsIgnoreCase("load")) {
if (args[0].equalsIgnoreCase(Names.Command.LOAD)) {
return getListOfStringsFromIterableMatchingLastWord(args, getFilenames());
} else if (args[0].equalsIgnoreCase("save")) {
return CommandBase.getListOfStringsMatchingLastWord(args, "InGameInfo.xml", "InGameInfo.json", "InGameInfo.txt");
} else if (args[0].equalsIgnoreCase(Names.Command.SAVE)) {
return CommandBase.getListOfStringsMatchingLastWord(args, Names.Files.FILE_XML, Names.Files.FILE_JSON, Names.Files.FILE_TXT);
}
}

@@ -52,7 +53,7 @@ private List<String> getFilenames() {
File[] files = this.core.getConfigDirectory().listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.startsWith("InGameInfo") && (name.endsWith(".xml") || name.endsWith(".json") || name.endsWith(".txt"));
return name.startsWith(Names.Files.NAME) && (name.endsWith(Names.Files.EXT_XML) || name.endsWith(Names.Files.EXT_JSON) || name.endsWith(Names.Files.EXT_TXT));
}
});

@@ -67,31 +68,35 @@ public boolean accept(File dir, String name) {
@Override
public void processCommand(ICommandSender commandSender, String[] args) {
if (args.length > 0) {
if (args[0].equalsIgnoreCase("reload")) {
commandSender.addChatMessage(new ChatComponentTranslation("commands.ingameinfoxml.reload"));
if (args[0].equalsIgnoreCase(Names.Command.RELOAD)) {
commandSender.addChatMessage(new ChatComponentTranslation(Names.Command.Message.RELOAD));
ConfigurationHandler.reload();
this.core.reloadConfig();
final boolean success = this.core.reloadConfig();
commandSender.addChatMessage(new ChatComponentTranslation(success ? Names.Command.Message.SUCCESS : Names.Command.Message.FAILURE));
return;
} else if (args[0].equalsIgnoreCase("load")) {
commandSender.addChatMessage(new ChatComponentTranslation("commands.ingameinfoxml.load", args[1]));
if (this.core.loadConfig(args[1])) {
} else if (args[0].equalsIgnoreCase(Names.Command.LOAD)) {
commandSender.addChatMessage(new ChatComponentTranslation(Names.Command.Message.LOAD, args[1]));
final boolean success = this.core.loadConfig(args[1]);
commandSender.addChatMessage(new ChatComponentTranslation(success ? Names.Command.Message.SUCCESS : Names.Command.Message.FAILURE));
if (success) {
ConfigurationHandler.setConfigName(args[1]);
ConfigurationHandler.save();
}
return;
} else if (args[0].equalsIgnoreCase("save")) {
commandSender.addChatMessage(new ChatComponentTranslation("commands.ingameinfoxml.save", args[1]));
this.core.saveConfig(args[1]);
} else if (args[0].equalsIgnoreCase(Names.Command.SAVE)) {
commandSender.addChatMessage(new ChatComponentTranslation(Names.Command.Message.SAVE, args[1]));
final boolean success = this.core.saveConfig(args[1]);
commandSender.addChatMessage(new ChatComponentTranslation(success ? Names.Command.Message.SUCCESS : Names.Command.Message.FAILURE));
return;
} else if (args[0].equalsIgnoreCase("enable")) {
commandSender.addChatMessage(new ChatComponentTranslation("commands.ingameinfoxml.enable"));
} else if (args[0].equalsIgnoreCase(Names.Command.ENABLE)) {
commandSender.addChatMessage(new ChatComponentTranslation(Names.Command.Message.ENABLE));
Ticker.enabled = true;
return;
} else if (args[0].equalsIgnoreCase("disable")) {
commandSender.addChatMessage(new ChatComponentTranslation("commands.ingameinfoxml.disable"));
} else if (args[0].equalsIgnoreCase(Names.Command.DISABLE)) {
commandSender.addChatMessage(new ChatComponentTranslation(Names.Command.Message.DISABLE));
Ticker.enabled = false;
return;
} else if (args[0].equalsIgnoreCase("taglist")) {
} else if (args[0].equalsIgnoreCase(Names.Command.TAGLIST)) {
DelayedGuiDisplayTicker.create(new GuiTags(), 10);
return;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.lunatrius.ingameinfo.handler;

import com.github.lunatrius.ingameinfo.Alignment;
import com.github.lunatrius.ingameinfo.reference.Names;
import com.github.lunatrius.ingameinfo.reference.Reference;
import cpw.mods.fml.client.event.ConfigChangedEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
@@ -13,30 +14,9 @@
import java.util.regex.Pattern;

public class ConfigurationHandler {
public static final String CATEGORY_GENERAL = "general";
public static final String CATEGORY_ALIGNMENT = "alignment";

public static final String FILENAME = "filename";
public static final String FILENAME_DESC = "The configuration that that should be loaded on startup.";

public static final String REPLACEDEBUG = "replaceDebug";
public static final String REPLACEDEBUG_DESC = "Replace the debug overlay (F3) with the InGameInfoXML overlay.";

public static final String SHOWINCHAT = "showInChat";
public static final String SHOWINCHAT_DESC = "Display the overlay in chat.";

public static final String SHOWONPLAYERLIST = "showOnPlayerList";
public static final String SHOWONPLAYERLIST_DESC = "Display the overlay on the player list.";

public static final String SCALE = "scale";
public static final String SCALE_DESC = "The overlay will be scaled by this amount.";

public static final String ALIGNMENT_DESC = "Offsets for %s (X<space>Y).";
public static final String LANG_PREFIX = Reference.MODID.toLowerCase() + ".config";

public static Configuration configuration;

public static final String CONFIGNAME_DEFAULT = "InGameInfo.xml";
public static final String CONFIGNAME_DEFAULT = Names.Files.FILE_XML;
// TODO: 1.8 - flip the default to true
public static final boolean REPLACEDEBUG_DEFAULT = false;
public static final boolean SHOWINCHAT_DEFAULT = true;
@@ -64,31 +44,31 @@ public static void init(File configFile) {
}

private static void loadConfiguration() {
propConfigName = configuration.get(CATEGORY_GENERAL, FILENAME, CONFIGNAME_DEFAULT, FILENAME_DESC);
propConfigName.setLanguageKey(String.format("%s.%s", LANG_PREFIX, FILENAME));
propConfigName = configuration.get(Names.Config.Category.GENERAL, Names.Config.FILENAME, CONFIGNAME_DEFAULT, Names.Config.FILENAME_DESC);
propConfigName.setLanguageKey(Names.Config.LANG_PREFIX + "." + Names.Config.FILENAME);
propConfigName.setRequiresMcRestart(true);
configName = propConfigName.getString();

propReplaceDebug = configuration.get(CATEGORY_GENERAL, REPLACEDEBUG, REPLACEDEBUG_DEFAULT, REPLACEDEBUG_DESC);
propReplaceDebug.setLanguageKey(String.format("%s.%s", LANG_PREFIX, REPLACEDEBUG));
propReplaceDebug = configuration.get(Names.Config.Category.GENERAL, Names.Config.REPLACEDEBUG, REPLACEDEBUG_DEFAULT, Names.Config.REPLACEDEBUG_DESC);
propReplaceDebug.setLanguageKey(Names.Config.LANG_PREFIX + "." + Names.Config.REPLACEDEBUG);
replaceDebug = propReplaceDebug.getBoolean(REPLACEDEBUG_DEFAULT);

propShowInChat = configuration.get(CATEGORY_GENERAL, SHOWINCHAT, SHOWINCHAT_DEFAULT, SHOWINCHAT_DESC);
propShowInChat.setLanguageKey(String.format("%s.%s", LANG_PREFIX, SHOWINCHAT));
propShowInChat = configuration.get(Names.Config.Category.GENERAL, Names.Config.SHOWINCHAT, SHOWINCHAT_DEFAULT, Names.Config.SHOWINCHAT_DESC);
propShowInChat.setLanguageKey(Names.Config.LANG_PREFIX + "." + Names.Config.SHOWINCHAT);
showInChat = propShowInChat.getBoolean(SHOWINCHAT_DEFAULT);

propShowOnPlayerList = configuration.get(CATEGORY_GENERAL, SHOWONPLAYERLIST, SHOWONPLAYERLIST_DEFAULT, SHOWONPLAYERLIST_DESC);
propShowOnPlayerList.setLanguageKey(String.format("%s.%s", LANG_PREFIX, SHOWONPLAYERLIST));
propShowOnPlayerList = configuration.get(Names.Config.Category.GENERAL, Names.Config.SHOWONPLAYERLIST, SHOWONPLAYERLIST_DEFAULT, Names.Config.SHOWONPLAYERLIST_DESC);
propShowOnPlayerList.setLanguageKey(Names.Config.LANG_PREFIX + "." + Names.Config.SHOWONPLAYERLIST);
showOnPlayerList = propShowOnPlayerList.getBoolean(SHOWONPLAYERLIST_DEFAULT);

propScale = configuration.get(CATEGORY_GENERAL, SCALE, String.valueOf(SCALE_DEFAULT), SCALE_DESC);
propScale.setLanguageKey(String.format("%s.%s", LANG_PREFIX, SCALE));
propScale = configuration.get(Names.Config.Category.GENERAL, Names.Config.SCALE, String.valueOf(SCALE_DEFAULT), Names.Config.SCALE_DESC);
propScale.setLanguageKey(Names.Config.LANG_PREFIX + "." + Names.Config.SCALE);
propScale.setValidValues(new String[] { "0.5", "1.0", "1.5", "2.0" });
scale = (float) propScale.getDouble(SCALE_DEFAULT);

for (Alignment alignment : Alignment.values()) {
Property property = configuration.get(CATEGORY_ALIGNMENT, alignment.toString().toLowerCase(), alignment.getXY(), String.format(ALIGNMENT_DESC, alignment.toString()));
property.setLanguageKey(String.format("%s.%s", LANG_PREFIX, alignment.toString().toLowerCase()));
Property property = configuration.get(Names.Config.Category.ALIGNMENT, alignment.toString().toLowerCase(), alignment.getXY(), String.format(Names.Config.ALIGNMENT_DESC, alignment.toString()));
property.setLanguageKey(Names.Config.LANG_PREFIX + "." + alignment.toString().toLowerCase());
property.setValidationPattern(Pattern.compile("-?\\d+ -?\\d+"));
propAlignments.put(alignment, property);
alignment.setXY(property.getString());
59 changes: 59 additions & 0 deletions src/main/java/com/github/lunatrius/ingameinfo/reference/Names.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.github.lunatrius.ingameinfo.reference;

@SuppressWarnings("HardCodedStringLiteral")
public final class Names {
public static final class Command {
public static final class Message {
public static final String USAGE = "commands.ingameinfoxml.usage";
public static final String RELOAD = "commands.ingameinfoxml.reload";
public static final String LOAD = "commands.ingameinfoxml.load";
public static final String SAVE = "commands.ingameinfoxml.save";
public static final String SUCCESS = "commands.ingameinfoxml.success";
public static final String FAILURE = "commands.ingameinfoxml.failure";
public static final String ENABLE = "commands.ingameinfoxml.enable";
public static final String DISABLE = "commands.ingameinfoxml.disable";
}

public static final String NAME = "igi";
public static final String RELOAD = "reload";
public static final String LOAD = "load";
public static final String SAVE = "save";
public static final String ENABLE = "enable";
public static final String DISABLE = "disable";
public static final String TAGLIST = "taglist";
}

public static final class Config {
public static final class Category {
public static final String GENERAL = "general";
public static final String ALIGNMENT = "alignment";
}

public static final String FILENAME = "filename";
public static final String FILENAME_DESC = "The configuration that that should be loaded on startup.";
public static final String REPLACEDEBUG = "replaceDebug";
public static final String REPLACEDEBUG_DESC = "Replace the debug overlay (F3) with the InGameInfoXML overlay.";
public static final String SHOWINCHAT = "showInChat";
public static final String SHOWINCHAT_DESC = "Display the overlay in chat.";
public static final String SHOWONPLAYERLIST = "showOnPlayerList";
public static final String SHOWONPLAYERLIST_DESC = "Display the overlay on the player list.";
public static final String SCALE = "scale";
public static final String SCALE_DESC = "The overlay will be scaled by this amount.";

public static final String ALIGNMENT_DESC = "Offsets for %s (X<space>Y).";

public static final String LANG_PREFIX = Reference.MODID.toLowerCase() + ".config";
}

public static final class Files {
public static final String NAME = "InGameInfo";

public static final String FILE_XML = "InGameInfo.xml";
public static final String FILE_JSON = "InGameInfo.json";
public static final String FILE_TXT = "InGameInfo.txt";

public static final String EXT_XML = ".xml";
public static final String EXT_JSON = ".json";
public static final String EXT_TXT = ".txt";
}
}
4 changes: 3 additions & 1 deletion src/main/resources/assets/ingameinfo/lang/en_US.lang
Original file line number Diff line number Diff line change
@@ -240,9 +240,11 @@ ingameinfoxml.tag.temperature.desc=Biome temperature.
ingameinfoxml.tag.humidity.desc=Biome humidity.

# commands
commands.ingameinfoxml.usage=/igi <reload|load|save|enable|disable> [filename]
commands.ingameinfoxml.usage=/igi <reload|load|save|enable|disable|taglist> [filename]
commands.ingameinfoxml.reload=Reloading...
commands.ingameinfoxml.load=Loading %s...
commands.ingameinfoxml.save=Saving %s...
commands.ingameinfoxml.success=Done!
commands.ingameinfoxml.failure=Something went wrong!
commands.ingameinfoxml.enable=InGame Info XML overlay is now enabled.
commands.ingameinfoxml.disable=InGame Info XML overlay is now disabled.

0 comments on commit b4da690

Please sign in to comment.