Skip to content

Commit

Permalink
var fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dries007 committed Sep 22, 2014
1 parent 5c9821f commit c6a261a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 24 deletions.
7 changes: 7 additions & 0 deletions src/main/java/net/doubledoordev/backend/Main.java
Expand Up @@ -42,6 +42,8 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.HashMap;

import static net.doubledoordev.backend.util.Constants.NAME;
Expand All @@ -59,6 +61,11 @@ private Main()

public static void main(String[] args) throws Exception
{
System.setProperty("file.encoding","UTF-8");
Field charset = Charset.class.getDeclaredField("defaultCharset");
charset.setAccessible(true);
charset.set(null,null);

LOGGER.info("+-------------------------------------------------------+");
LOGGER.info("| ...................... Loading ...................... |");
LOGGER.info("+-------------------------------------------------------+");
Expand Down
41 changes: 23 additions & 18 deletions src/main/java/net/doubledoordev/backend/server/Server.java
Expand Up @@ -121,7 +121,7 @@ public Server(ServerData data)
this.propertiesFile = new File(folder, SERVER_PROPERTIES);

if (!folder.exists()) folder.mkdir();
saveProperties();
normalizeProperties();

// Check to see if the server is running outside the backend, if so reboot please!
if (getRCon() != null)
Expand Down Expand Up @@ -222,6 +222,24 @@ public void saveProperties()
{
if (!propertiesLoaded) getProperties();

normalizeProperties();

try
{
if (!propertiesFile.exists()) //noinspection ResultOfMethodCallIgnored
propertiesFile.createNewFile();
FileWriter fileWriter = new FileWriter(propertiesFile);
properties.store(fileWriter, "Minecraft server properties\nModified by D3Backend");
fileWriter.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

private void normalizeProperties()
{
if (!Settings.SETTINGS.fixedPorts)
{
properties.setProperty(SERVER_PORT, String.valueOf(data.serverPort));
Expand All @@ -242,19 +260,6 @@ public void saveProperties()
properties.put(RCON_ENABLE, "true");
properties.put(QUERY_ENABLE, "true");
properties.put(RCON_PASSWORD, data.rconPswd);

try
{
if (!propertiesFile.exists()) //noinspection ResultOfMethodCallIgnored
propertiesFile.createNewFile();
FileWriter fileWriter = new FileWriter(propertiesFile);
properties.store(fileWriter, "Minecraft server properties\nModified by D3Backend");
fileWriter.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

/**
Expand Down Expand Up @@ -330,7 +335,7 @@ public void setProperty(String key, String value) throws ServerOnlineException
{
if (getOnline()) throw new ServerOnlineException();
properties.put(key, value);
saveProperties();
normalizeProperties();
}

/**
Expand Down Expand Up @@ -562,7 +567,6 @@ public List<String> getAdmins()
@SuppressWarnings("UnusedDeclaration")
public String getPropertiesAsText()
{
getProperties();
StringWriter stringWriter = new StringWriter();
try
{
Expand All @@ -575,13 +579,13 @@ public String getPropertiesAsText()
return stringWriter.toString();
}

@SuppressWarnings("UnusedDeclaration")
public void setPropertiesAsText(String urlEncodedText)
{
try
{
properties.clear();
properties.load(new StringReader(urlEncodedText));
saveProperties();
normalizeProperties();
}
catch (IOException e)
{
Expand Down Expand Up @@ -623,6 +627,7 @@ public void startServer() throws Exception
if (getOnline()) throw new ServerOnlineException();
if (new File(folder, data.jarName + ".tmp").exists()) throw new Exception("Minecraft server jar still downloading...");
if (!new File(folder, data.jarName).exists()) throw new FileNotFoundException(data.jarName + " not found.");
saveProperties();
new Thread(new Runnable()
{
@Override
Expand Down
Expand Up @@ -345,7 +345,7 @@ protected String decodePercent(String str)
String decoded = null;
try
{
decoded = URLDecoder.decode(str, "UTF8");
decoded = URLDecoder.decode(str, "UTF-8");
}
catch (UnsupportedEncodingException ignored)
{
Expand Down
Expand Up @@ -79,6 +79,8 @@ public static void handlePost(HashMap<String, Object> dataObject, IHTTPSession s
{
session.parseBody(new HashMap<String, String>());
Map<String, String> map = session.getParms();
Main.printdebug(session, dataObject);

String split[] = session.getUri().substring(1).split("/");
switch (split[0]) // 0 => type id
{
Expand All @@ -99,6 +101,9 @@ public static void handlePost(HashMap<String, Object> dataObject, IHTTPSession s
dataObject.put("message", e.getLocalizedMessage());
}
break;
case "servers":
Settings.getServerByName(split[1]).setPropertiesAsText(map.get("serverProperties"));
break;
}
}
catch (IOException | ResponseException e)
Expand All @@ -114,7 +119,7 @@ private static void handleNewServer(HashMap<String, Object> dataObject, IHTTPSes
{
User user = (User) dataObject.get("user");
if (user == null) throw new Exception("Not logged in.");
if (user.getMaxServers() != -1 && user.getServerCount() + 1 >= user.getMaxServers()) throw new Exception("Max server count reached.");
if (user.getMaxServers() != -1 && user.getServerCount() >= user.getMaxServers()) throw new Exception("Max server count reached.");
ServerData data = new ServerData();

data.name = user.getUsername() + "_" + map.get("name");
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/templates/server.ftl
Expand Up @@ -108,10 +108,10 @@
</div>
<div class="panel-body" style="text-align: center;">
<p>No checks are preformed when submitting. Be careful!</p>
<form class="form-horizontal" role="form" style="padding-left: 10px; padding-right: 10px;">
<textarea id="serverProperties" class="form-control" rows="25" <#if allowModify && server.online>disabled</#if>>${server.propertiesAsText}</textarea>
<form class="form-horizontal" role="form" style="padding-left: 10px; padding-right: 10px;" method="post">
<textarea id="serverProperties" name="serverProperties" class="form-control" rows="25" <#if allowModify && server.online>disabled</#if>>${server.propertiesAsText}</textarea>
<br>
<button class="btn btn-primary btn-lg btn-block" <#if allowModify && !server.online>onclick="call('server', '${server.name}', 'setPropertiesAsText', encodeURIComponent(document.getElementById('serverProperties').value))" <#else>disabled</#if>>Send!
<button class="btn btn-primary btn-lg btn-block" <#if allowModify && !server.online>type="submit" <#else>disabled</#if>>Send!
</button>
</form>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/userlist.ftl
Expand Up @@ -24,7 +24,7 @@
<td><input type="number" min="-1" class="form-control" placeholder="-1 is infinite"
value="${user.maxServers?c}"
onchange="call('users', '${user.username}', 'setMaxServers', this.value)"></td>
<td><input type="number" min="-1" class="form-control" placeholder="0 is infinite" value="${user.maxRam?c}"
<td><input type="number" min="-1" class="form-control" placeholder="-1 is infinite" value="${user.maxRam?c}"
onchange="call('users', '${user.username}', 'setMaxRam', this.value)"></td>
<#else>
<td>${user.group}</td>
Expand Down

0 comments on commit c6a261a

Please sign in to comment.