summaryrefslogtreewikicommitdiff
path: root/src/main/java/net/tylermurphy/hideAndSeek/world/WorldLoader.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/net/tylermurphy/hideAndSeek/world/WorldLoader.java')
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/world/WorldLoader.java126
1 files changed, 63 insertions, 63 deletions
diff --git a/src/main/java/net/tylermurphy/hideAndSeek/world/WorldLoader.java b/src/main/java/net/tylermurphy/hideAndSeek/world/WorldLoader.java
index 011d334..1d4ff71 100644
--- a/src/main/java/net/tylermurphy/hideAndSeek/world/WorldLoader.java
+++ b/src/main/java/net/tylermurphy/hideAndSeek/world/WorldLoader.java
@@ -19,85 +19,89 @@
package net.tylermurphy.hideAndSeek.world;
-import static net.tylermurphy.hideAndSeek.configuration.Config.*;
-import static net.tylermurphy.hideAndSeek.configuration.Localization.message;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-
-import net.tylermurphy.hideAndSeek.util.Version;
+import net.tylermurphy.hideAndSeek.Main;
import org.bukkit.Bukkit;
+import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.WorldCreator;
-import net.tylermurphy.hideAndSeek.Main;
+import java.io.*;
+import java.nio.file.Files;
+
+import static net.tylermurphy.hideAndSeek.configuration.Config.*;
+import static net.tylermurphy.hideAndSeek.configuration.Localization.message;
public class WorldLoader {
- String mapname;
- String savename;
+ private String mapName;
+ private String saveName;
- public WorldLoader(String mapname) {
- this.mapname = mapname;
- this.savename = "hideandseek_"+mapname;
+ public WorldLoader(String mapName) {
+ this.mapName = mapName;
+ this.saveName = "hideandseek_"+ mapName;
}
- public World getWorld(){
- return Bukkit.getServer().getWorld(savename);
+ public void setNewMap(String mapName){
+ this.mapName = mapName;
+ this.saveName = "hideandseek_"+ mapName;
}
- public void unloadMap(){
- World world = Bukkit.getServer().getWorld(savename);
- if(world == null){
- Main.plugin.getLogger().warning(savename + " already unloaded.");
+ public World getWorld() {
+ return Bukkit.getServer().getWorld(saveName);
+ }
+
+ public void unloadMap() {
+ World world = Bukkit.getServer().getWorld(saveName);
+ if (world == null) {
+ Main.getInstance().getLogger().warning(saveName + " already unloaded.");
return;
}
- if(Bukkit.getServer().unloadWorld(world, false)){
- Main.plugin.getLogger().info("Successfully unloaded " + savename);
+ world.getPlayers().forEach(player -> player.teleport(new Location(Bukkit.getWorld(exitWorld), exitPosition.getX(), exitPosition.getY(), exitPosition.getZ())));
+ if (Bukkit.getServer().unloadWorld(world, false)) {
+ Main.getInstance().getLogger().info("Successfully unloaded " + saveName);
}else{
- Main.plugin.getLogger().severe("COULD NOT UNLOAD " + savename);
+ Main.getInstance().getLogger().severe("COULD NOT UNLOAD " + saveName);
}
}
- public void loadMap(){
- Bukkit.getServer().createWorld(new WorldCreator(savename).generator(new VoidGenerator()));
- World world = Bukkit.getServer().getWorld(savename);
- if(world == null){
- Main.plugin.getLogger().severe("COULD NOT LOAD " + savename);
+ public void loadMap() {
+ Bukkit.getServer().createWorld(new WorldCreator(saveName).generator(new VoidGenerator()));
+ World world = Bukkit.getServer().getWorld(saveName);
+ if (world == null) {
+ Main.getInstance().getLogger().severe("COULD NOT LOAD " + saveName);
return;
}
world.setAutoSave(false);
}
- public void rollback(){
+ public void rollback() {
unloadMap();
loadMap();
}
public String save() {
- File current = new File(Main.root+File.separator+mapname);
- if(current.exists()) {
+ World world = Bukkit.getServer().getWorld(mapName);
+ if(world == null){
+ throw new RuntimeException("Invalid world to save: " + mapName);
+ }
+ File current = new File(Main.getInstance().getWorldContainer()+File.separator+ mapName);
+ if (current.exists()) {
try {
- File destenation = new File(Main.root+File.separator+savename);
- File temp_destenation = new File(Main.root+File.separator+"temp_"+savename);
+ File destination = new File(Main.getInstance().getWorldContainer()+File.separator+ saveName);
+ File temp_destination = new File(Main.getInstance().getWorldContainer()+File.separator+"temp_"+ saveName);
copyFileFolder("region",true);
copyFileFolder("entities",true);
copyFileFolder("datapacks",false);
+ copyFileFolder("data",false);
File srcFile = new File(current, "level.dat");
- File destFile = new File(temp_destenation, "level.dat");
+ File destFile = new File(temp_destination, "level.dat");
copyFile(srcFile,destFile);
- if(destenation.exists()) {
- deleteDirectory(destenation);
- if(!destenation.mkdir()){
- throw new RuntimeException("Failed to create directory: "+destenation.getPath());
- }
+ if (destination.exists()) {
+ deleteDirectory(destination);
}
- if(!temp_destenation.renameTo(destenation)){
- throw new RuntimeException("Failed to rename directory: "+temp_destenation.getPath());
+
+ if (!temp_destination.renameTo(destination)) {
+ throw new RuntimeException("Failed to rename directory: "+temp_destination.getPath());
}
} catch(IOException e) {
e.printStackTrace();
@@ -110,38 +114,34 @@ public class WorldLoader {
}
private void copyFileFolder(String name, Boolean isMca) throws IOException {
- File region = new File(Main.root+File.separator+mapname+File.separator+name);
- File temp = new File(Main.root+File.separator+"temp_"+savename+File.separator+name);
- System.out.println(region.getAbsolutePath());
- System.out.println(temp.getAbsolutePath());
- if(region.exists() && region.isDirectory()) {
- System.out.println("passed");
- if(!temp.exists())
- if(!temp.mkdirs())
+ File region = new File(Main.getInstance().getWorldContainer()+File.separator+ mapName +File.separator+name);
+ File temp = new File(Main.getInstance().getWorldContainer()+File.separator+"temp_"+ saveName +File.separator+name);
+ if (region.exists() && region.isDirectory()) {
+ if (!temp.exists())
+ if (!temp.mkdirs())
throw new IOException("Couldn't create region directory!");
String[] files = region.list();
- if(files == null){
- Main.plugin.getLogger().severe("Region directory is null or cannot be accessed");
+ if (files == null) {
+ Main.getInstance().getLogger().severe("Region directory is null or cannot be accessed");
return;
}
for (String file : files) {
- System.out.println("Testing file "+ file);
- if(isMca) {
+ if (isMca) {
int minX = (int)Math.floor(saveMinX / 512.0);
int minZ = (int)Math.floor(saveMinZ / 512.0);
int maxX = (int)Math.floor(saveMaxX / 512.0);
int maxZ = (int)Math.floor(saveMaxZ / 512.0);
String[] parts = file.split("\\.");
- if(parts.length > 1) {
- Main.plugin.getLogger().info(file);
- if( Integer.parseInt(parts[1]) < minX || Integer.parseInt(parts[1]) > maxX || Integer.parseInt(parts[2]) < minZ || Integer.parseInt(parts[2]) > maxZ )
+ if (parts.length > 1) {
+ Main.getInstance().getLogger().info(file);
+ if ( Integer.parseInt(parts[1]) < minX || Integer.parseInt(parts[1]) > maxX || Integer.parseInt(parts[2]) < minZ || Integer.parseInt(parts[2]) > maxZ )
continue;
}
}
File srcFile = new File(region, file);
- if(srcFile.isDirectory()) {
+ if (srcFile.isDirectory()) {
copyFileFolder(name+File.separator+file, false);
} else {
File destFile = new File(temp, file);
@@ -152,8 +152,8 @@ public class WorldLoader {
}
private void copyFile(File source, File target) throws IOException {
- InputStream in = new FileInputStream(source);
- OutputStream out = new FileOutputStream(target);
+ InputStream in = Files.newInputStream(source.toPath());
+ OutputStream out = Files.newOutputStream(target.toPath());
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0)
@@ -169,7 +169,7 @@ public class WorldLoader {
deleteDirectory(file);
}
}
- if(!directoryToBeDeleted.delete()){
+ if (!directoryToBeDeleted.delete()) {
throw new RuntimeException("Failed to delete directory: "+directoryToBeDeleted.getPath());
}
}