Skip to content

Commit

Permalink
added config
Browse files Browse the repository at this point in the history
  • Loading branch information
palechip committed Apr 18, 2013
1 parent 0723611 commit 3f245f0
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 4 deletions.
2 changes: 1 addition & 1 deletion vanilla/ServerList.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void saveServerList()
try
{
// MODIFIED
ServerListFolder.ManageUpwardsFolders(this); // ... server stuff
ServerListFolder.manageUpwardsFolders(this); // ... server stuff
// MODIFIED END

NBTTagList var1 = new NBTTagList();
Expand Down
22 changes: 21 additions & 1 deletion vanilla/ServerListFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
import java.util.ArrayList;
import java.util.Calendar;

import net.minecraft.client.Minecraft;

public class ServerListFolder {
// saves the opened hierarchy
private static ArrayList<String> openedFolders;
private static int index;
private static ServerListFolderClipboard clipboard;
public static ServerListFolderConfig config;
private static boolean april1st;
// makes the refresh button not changing to the main servers
private static Boolean lockNextReset;

static {
info("Loading the mod...");
openedFolders = new ArrayList<String>(3);
openedFolders.add(0, "servers");
openedFolders.add(1, "");
Expand All @@ -28,6 +32,8 @@ public class ServerListFolder {

clipboard = new ServerListFolderClipboard();

config = new ServerListFolderConfig();

Calendar cal = Calendar.getInstance();

if(cal.get(2) == 3 && cal.get(5) == 1) {
Expand Down Expand Up @@ -90,7 +96,7 @@ public static Boolean checkIfFolder(ServerData serverToCheck){
/**
* this function adds a ... folder if there is no and makes sure that the ... folder is always the last one.
*/
public static void ManageUpwardsFolders(ServerList list) {
public static void manageUpwardsFolders(ServerList list) {
if(index != 0) { // index == 0 means that you are in the main server list, so no ... server will be added
ServerData upwardsServer = new ServerData("...", "dir:...");
upwardsServer.setHideAddress(true);
Expand Down Expand Up @@ -147,4 +153,18 @@ public static ServerListFolderClipboard getClipboard() {
public static boolean isApril1st() {
return april1st;
}

/**
* Prints a message to the console
*/
public static void info(String message) {
Minecraft.getMinecraft().getLogAgent().logInfo("[ServerListFolder] " + message);
}

/**
* Prints a message as a warning to the console
*/
public static void warn(String message) {
Minecraft.getMinecraft().getLogAgent().logWarning("[ServerListFolder] " + message);
}
}
239 changes: 239 additions & 0 deletions vanilla/ServerListFolderConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
// part of the Server List Folder mod by palechip
// license GPLv3 (http://www.gnu.org/licenses/quick-guide-gplv3.html)
// to extend the mod, you need to decompile Minecraft and copy the modifyed code to the given function.
// that's because it isn't allowed to release decompiled minecraft code.

// config originally written by lbjdaking23 for https://github.com/UnofficalProjectAresTeam/mod_Ares/

package net.minecraft.src;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import net.minecraft.client.Minecraft;

public class ServerListFolderConfig {
private static Properties defaults = new Properties();
private String configPath;
private Properties config;

// update this value to change the config version.
private static int version = 1;

// main variables
public static int configVersion;
public static EnumChatFormatting FolderMotdColorCode;
private static String FolderMotdColor;
public static String FolderMotdText;

/**
* Default values created when class is first referenced
*/
static {
// if the value is missing, it should force an update. Don't change it.
defaults.setProperty("configVersion", "0");
defaults.setProperty("FolderMotdColorCode", "DARK_PURPLE");
defaults.setProperty("FolderMotdColor", "purple");
defaults.setProperty("FolderMotdText", "Folder");
}

public ServerListFolderConfig() {
ServerListFolder.info("Attempting to load/create the configuration.");
loadConfig();
loadConfigData();
}

/**
* Attempts to find a config
* If there is one load it
* If there is not one create one
*/
private void loadConfig() {
config = new Properties(defaults);

try {
configPath = Minecraft.getMinecraft().getMinecraftDir().getCanonicalPath() + File.separatorChar + "config" + File.separatorChar + "ServerListFolder" + File.separatorChar;

File cfg = new File(configPath + "ServerListFolder.cfg");

if(cfg.exists()) {
ServerListFolder.info("Config file found, loading...");
config.load(new FileInputStream(configPath + "ServerListFolder.cfg"));
} else {
ServerListFolder.info("No config file found, creating...");
createConfig(cfg);
}
} catch(Exception e) {
ServerListFolder.warn(e.toString());
}
}

/**
* Creates a config properties of default values
* Then saves the config to the config location
*
* @param cfg config file
*/
private void createConfig(File cfg) {
File folder = new File(configPath);
if(!folder.exists()) {
ServerListFolder.info("No folder found, creating...");
folder.mkdirs();
}

try {
cfg.createNewFile();
config.setProperty("configVersion", ""+version);
config.setProperty("FolderMotdColor", "purple");
config.setProperty("FolderMotdText", "Folder");
config.store(new FileOutputStream(configPath +"ServerListFolder.cfg"),"Server List Folder Mod Config" + "\nUse the color names(without spaces!) form: http://www.minecraftwiki.net/wiki/Color_Codes");
} catch (Exception e) {
ServerListFolder.warn(e.toString());
}
}

/**
* Loads the property data into the local data
*/
public void loadConfigData() {
ServerListFolder.info("Loading Config to Local Data");
configVersion = this.getIntProperty("configVersion");
FolderMotdColor = this.getStringProperty("FolderMotdColor");
parseColor();
FolderMotdText = this.getStringProperty("FolderMotdText");
checkForConfigUpdate();
}

public void setProperty(String prop, String value) {
config.setProperty(prop, value);
saveConfig();
}

public void setProperty(String prop, float value) {
String s = String.valueOf(value);
config.setProperty(prop, s);
saveConfig();
}

public void setProperty(String prop, int value) {
String s = String.valueOf(value);
config.setProperty(prop, s);
saveConfig();
}

public void setProperty(String prop, boolean value) {
String s = String.valueOf(value);
config.setProperty(prop, s);
saveConfig();
}

public String getStringProperty(String prop) {
return config.getProperty(prop);
}

public float getFloatProperty(String prop) {
String s = config.getProperty(prop);
return Float.parseFloat(s);
}

public int getIntProperty(String prop) {
String s = config.getProperty(prop);
return Integer.parseInt(s);
}

public boolean getBoolProperty(String prop) {
String s = config.getProperty(prop);
return Boolean.parseBoolean(s);
}

public static String getDefaultPropertyValue(String prop) {
return defaults.getProperty(prop);
}

public static float getDefaultFloatProperty(String prop) {
String s = defaults.getProperty(prop);
return Float.parseFloat(s);
}

public static int getDefaultIntProperty(String prop) {
String s = defaults.getProperty(prop);
return Integer.parseInt(s);
}

public static boolean getDefaultBoolProperty(String prop) {
String s = defaults.getProperty(prop);
return Boolean.parseBoolean(s);
}

public void saveConfig() {
try {
config.store(new FileOutputStream(configPath + "ServerListFolder.cfg"), null);
config.load(new FileInputStream(configPath + "ServerListFolder.cfg"));
} catch (Exception e) {
ServerListFolder.warn(e.toString());
}
}

/**
* Checks if the config version has changed and adds the options which are new.
*/
private void checkForConfigUpdate() {
if(version != configVersion) {
ServerListFolder.info("Updating the config...");
switch(configVersion) {
case 0:
if(FolderMotdColor.equals(defaults.getProperty("FolderMotdColor"))) {
config.setProperty("FolderMotdColor", defaults.getProperty("FolderMotdColor"));
}
if(FolderMotdText.equals(defaults.getProperty("FolderMotdText"))) {
config.setProperty("FolderMotdText", defaults.getProperty("FolderMotdText"));
}
case 1:
// for the next version.
}
config.setProperty("configVersion", ""+version);
saveConfig();
}
}

private void parseColor() {
if(FolderMotdColor.equalsIgnoreCase("black")){
FolderMotdColorCode = EnumChatFormatting.BLACK;
} else if(FolderMotdColor.equalsIgnoreCase("white")) {
FolderMotdColorCode = EnumChatFormatting.WHITE;
} else if(FolderMotdColor.equalsIgnoreCase("red")) {
FolderMotdColorCode = EnumChatFormatting.RED;
} else if(FolderMotdColor.equalsIgnoreCase("blue") || FolderMotdColor.equalsIgnoreCase("indigo")) {
FolderMotdColorCode = EnumChatFormatting.BLUE;
} else if (FolderMotdColor.equals("darkblue") || FolderMotdColor.equalsIgnoreCase("navy")) {
FolderMotdColorCode = EnumChatFormatting.DARK_BLUE;
} else if(FolderMotdColor.equalsIgnoreCase("darkgreen") || FolderMotdColor.equalsIgnoreCase("green")) {
FolderMotdColorCode = EnumChatFormatting.DARK_GREEN;
} else if(FolderMotdColor.equalsIgnoreCase("darkaqua") || FolderMotdColor.equalsIgnoreCase("teal")) {
FolderMotdColorCode = EnumChatFormatting.DARK_AQUA;
} else if(FolderMotdColor.equalsIgnoreCase("yellow")) {
FolderMotdColorCode = EnumChatFormatting.YELLOW;
} else if(FolderMotdColor.equalsIgnoreCase("grey") || FolderMotdColor.equalsIgnoreCase("silver")) {
FolderMotdColorCode = EnumChatFormatting.GRAY;
} else if(FolderMotdColor.equalsIgnoreCase("aqua")) {
FolderMotdColorCode = EnumChatFormatting.AQUA;
} else if(FolderMotdColor.equalsIgnoreCase("pink")) {
FolderMotdColorCode = EnumChatFormatting.LIGHT_PURPLE;
} else if (FolderMotdColor.equalsIgnoreCase("gold") || FolderMotdColor.equalsIgnoreCase("orange")) {
FolderMotdColorCode = EnumChatFormatting.GOLD;
} else if(FolderMotdColor.equalsIgnoreCase("lime") || FolderMotdColor.equalsIgnoreCase("brightgreen")) {
FolderMotdColorCode = EnumChatFormatting.GREEN;
} else if(FolderMotdColor.equalsIgnoreCase("darkgrey")) {
FolderMotdColorCode = EnumChatFormatting.DARK_GRAY;
} else if(FolderMotdColor.equalsIgnoreCase("darkred") || FolderMotdColor.equalsIgnoreCase("maroon")) {
FolderMotdColorCode = EnumChatFormatting.DARK_RED;
} else if(FolderMotdColor.equalsIgnoreCase("purple")) {
FolderMotdColorCode = EnumChatFormatting.DARK_PURPLE;
} else {
ServerListFolder.warn("Failed to parse the Color Setting. Using Purple");
FolderMotdColorCode = EnumChatFormatting.DARK_PURPLE;
}
}
}

4 changes: 2 additions & 2 deletions vanilla/ThreadPollServers.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public void run()
if(this.pollServersServerData.serverIP.equals("") || this.pollServersServerData.serverIP.startsWith("dir:")) {
this.pollServersServerData.pingToServer = -1L; // "(no-connection)"
if(ServerListFolder.isApril1st()) {
this.pollServersServerData.serverMOTD = EnumChatFormatting.OBFUSCATED + "Folder";
this.pollServersServerData.serverMOTD = EnumChatFormatting.OBFUSCATED + ServerListFolder.config.FolderMotdText;
} else {
this.pollServersServerData.serverMOTD = EnumChatFormatting.DARK_PURPLE + "Folder";
this.pollServersServerData.serverMOTD = ServerListFolder.config.FolderMotdColorCode + ServerListFolder.config.FolderMotdText;
}
return;
}
Expand Down

0 comments on commit 3f245f0

Please sign in to comment.