This repository has been archived by the owner on Dec 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/lb_stuff/mcmodify/minecraft/ServerList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
|
||
// | ||
} |