Skip to content

Commit

Permalink
Added NBT handling in the same way its done by Pay2Spawn. Not that us…
Browse files Browse the repository at this point in the history
…er friendly, but it can be used in case of emergency.

Added special cases for some JSON files (op, ban, ban-ip, whitelist)
  • Loading branch information
dries007 committed Oct 14, 2014
1 parent f2ca388 commit 86b9f81
Show file tree
Hide file tree
Showing 12 changed files with 883 additions and 126 deletions.
51 changes: 42 additions & 9 deletions src/main/java/net/doubledoordev/backend/server/FileManager.java
Expand Up @@ -40,24 +40,31 @@

package net.doubledoordev.backend.server;

import net.doubledoordev.backend.util.Constants;
import net.doubledoordev.backend.util.Helper;
import net.doubledoordev.backend.util.JsonNBTHelper;
import net.doubledoordev.backend.webserver.NanoHTTPD;
import net.doubledoordev.backend.webserver.SimpleWebServer;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.logging.log4j.util.Strings;
import org.apache.commons.codec.binary.Base64;
import org.spout.nbt.Tag;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;

import static net.doubledoordev.backend.webserver.NanoHTTPD.MIME_PLAINTEXT;
import static net.doubledoordev.backend.webserver.NanoHTTPD.Response.Status.FORBIDDEN;
import static net.doubledoordev.backend.webserver.NanoHTTPD.Response.Status.INTERNAL_ERROR;
import static net.doubledoordev.backend.webserver.NanoHTTPD.Response.Status.OK;
import static net.doubledoordev.backend.webserver.NanoHTTPD.Response.Status.*;

/**
* Current limitations:
* You can't make a new NBT file, as it will not be able to parse the compression state.
*
* @author Dries007
*/
@SuppressWarnings("UnusedDeclaration")
Expand Down Expand Up @@ -128,7 +135,7 @@ public Collection<File> makeBreadcrumbs()
public String stripServer(File file)
{
if (file.equals(serverFolder)) return serverFolder.getName();
return file.toString().substring(serverFolder.toString().length() + 1);
return file.toString().substring(serverFolder.toString().length() + 1).replace('\\', '/');
}

public boolean canEdit(File file)
Expand All @@ -147,6 +154,10 @@ public boolean canEdit(File file)

public String getEditor()
{
if (file.getName().equals("ops.json")) return "ops.ftl";
if (file.getName().equals("whitelist.json")) return "whitelist.ftl";
if (file.getName().equals("banned-players.json")) return "banned-players.ftl";
if (file.getName().equals("banned-ips.json")) return "banned-ips.ftl";
switch (getExtension())
{
case "jar":
Expand All @@ -155,6 +166,7 @@ public String getEditor()
return null;

case "json":
case "dat":
return "json.ftl";

case "jpg":
Expand All @@ -174,6 +186,7 @@ public String getIcon(File file)
{
case "html":
case "json":
case "dat":
return "file-code-o";

case "txt":
Expand All @@ -193,9 +206,19 @@ public String getIcon(File file)
}
}

public String getRawFileContents() throws IOException
public String getFileContentsAsJson() throws IOException
{
return FileUtils.readFileToString(file);
switch (getExtension())
{
case "json":
return FileUtils.readFileToString(file);
case "dat":
Tag tag = Helper.readRawNBT(file, true);
if (tag == null) tag = Helper.readRawNBT(file, false);
if (tag != null) return JsonNBTHelper.parseNBT(tag).toString();
default:
return null;
}
}

public String getFileContentsAsString() throws IOException
Expand All @@ -213,12 +236,22 @@ public NanoHTTPD.Response set(String contents)
if (!file.canWrite()) return new NanoHTTPD.Response(FORBIDDEN, MIME_PLAINTEXT, "File is write protected.");
try
{
FileUtils.writeStringToFile(file, contents);
switch (getExtension())
{
case "dat":
Helper.writeRawNBT(file, Helper.readRawNBT(file, true) != null, JsonNBTHelper.parseJSON(Constants.JSONPARSER.parse(contents)));
break;
default:
FileUtils.writeStringToFile(file, contents);
break;
}
}
catch (IOException e)
{
e.printStackTrace();
return new NanoHTTPD.Response(INTERNAL_ERROR, MIME_PLAINTEXT, e.toString());
}

return new NanoHTTPD.Response(OK, MIME_PLAINTEXT, "OK");
}

Expand Down
72 changes: 71 additions & 1 deletion src/main/java/net/doubledoordev/backend/util/Helper.java
Expand Up @@ -41,11 +41,16 @@
package net.doubledoordev.backend.util;

import net.doubledoordev.backend.server.Server;
import org.spout.nbt.Tag;
import org.spout.nbt.stream.NBTInputStream;
import org.spout.nbt.stream.NBTOutputStream;

import java.io.IOException;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

import static net.doubledoordev.backend.util.Constants.RANDOM;
import static net.doubledoordev.backend.util.Constants.symbols;
Expand Down Expand Up @@ -131,4 +136,69 @@ public static int getTotalDiskspaceUsed()
for (Server server : Settings.SETTINGS.getServers()) total += server.getDiskspaceUse();
return total;
}

private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");

public static String getNowInBanFormat()
{
return dateFormat.format(new Date());
}

public static Tag<?> readRawNBT(File file, boolean compressed)
{
Tag<?> tag = null;
try
{
InputStream is = new FileInputStream(file);
NBTInputStream ns = new NBTInputStream(is, compressed);
try
{
tag = ns.readTag();
}
finally
{
try
{
ns.close();
}
catch (IOException ignored)
{

}
}
}
catch (Exception ignored)
{

}
return tag;
}

public static void writeRawNBT(File file, boolean compressed, Tag<?> tag)
{
try
{
OutputStream is = new FileOutputStream(file);
NBTOutputStream ns = new NBTOutputStream(is, compressed);
try
{
ns.writeTag(tag);
}
finally
{
try
{
ns.close();
}
catch (IOException ignored)
{

}
}
}
catch (Exception ignored)
{

}
}
}

0 comments on commit 86b9f81

Please sign in to comment.