Skip to content
This repository has been archived by the owner on Dec 28, 2020. It is now read-only.

Commit

Permalink
#11 - Partial work
Browse files Browse the repository at this point in the history
  • Loading branch information
LB-- committed Aug 29, 2014
1 parent 484eeee commit 3c7ccbc
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/com/lb_stuff/mcmodify/minecraft/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.lang.reflect.InvocationTargetException;

/**
*
* @see <a href="http://minecraft.gamepedia.com/Chunk_format">Chunk format</a> on the Minecraft Wiki
*/
public class Chunk
{
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/lb_stuff/mcmodify/minecraft/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

/**
* Region file reader/writer
* @see <a href="http://minecraft.gamepedia.com/Region_file_format">Region file format</a> on the Minecraft Wiki
*/
public final class Region
{
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/lb_stuff/mcmodify/minecraft/ServerList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.lb_stuff.mcmodify.minecraft;

import java.io.File;

/**
* @see <a href="http://minecraft.gamepedia.com/Servers.dat_format">Servers.dat format</a> on the Minecraft Wiki
*/
public class ServerList
{
private final File dir;
public ServerList(File mcdir)
{
if(mcdir.isDirectory())
{
dir = mcdir;
return;
}
else if(mcdir.isFile() && mcdir.getName().equalsIgnoreCase("servers.dat"))
{
dir = mcdir.getParentFile();
return;
}
throw new IllegalArgumentException("\"mcdir\" must be the Minecraft directory or servers.dat file");
}

//
}
75 changes: 75 additions & 0 deletions src/main/java/com/lb_stuff/mcmodify/minecraft/World.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.lb_stuff.mcmodify.minecraft;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;

/**
* @see <a href="http://minecraft.gamepedia.com/Level_format">Level format</a> on the Minecraft Wiki
*/
public class World
{
private final File dir;
public World(File level)
{
if(level.isDirectory())
{
dir = level;
return;
}
else if(level.isFile() && level.getName().equalsIgnoreCase("level.dat"))
{
dir = level.getParentFile();
return;
}
throw new IllegalArgumentException("\"level\" must be the world directory or level.dat file");
}

private File getLockFile()
{
return new File(dir, "session.lock");
}
private Long locktimestamp = null;
public void lock() throws IOException
{
File f = getLockFile();
if(!f.delete() || !f.createNewFile())
{
throw new IOException("Cannot recreate session.lock");
}
Long t;
try(PrintWriter pw = new PrintWriter(f))
{
t = System.currentTimeMillis();
pw.print((long)t);
}
locktimestamp = t;
}
public boolean isLocked()
{
if(locktimestamp == null)
{
return false;
}
try(Scanner s = new Scanner(getLockFile()))
{
return locktimestamp.equals(s.nextLong());
}
catch(IOException|NoSuchElementException e)
{
return false;
}
}
public void throwIfNotLocked() throws IllegalStateException
{
if(!isLocked())
{
throw new IllegalStateException("World is no longer locked by this instance/program");
}
}

//
}

0 comments on commit 3c7ccbc

Please sign in to comment.