Created
January 12, 2019 10:58
-
-
Save GravenilvecTV/c840343dcbbd44503ec6d5e528e6e715 to your computer and use it in GitHub Desktop.
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
package fr.gravendev.cyg.utils; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.io.UnsupportedEncodingException; | |
public class FileUtils { | |
public static void createAndWrite(File playerFile, String json) { | |
try { | |
// create default player json file | |
playerFile.createNewFile(); | |
updateDocument(playerFile, json); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@SuppressWarnings("resource") | |
public static String loadAndRead(File playerFile) { | |
// read account file writer using player file | |
try { | |
InputStreamReader reader = new InputStreamReader(new FileInputStream(playerFile), "UTF-8"); | |
BufferedReader br = new BufferedReader(reader); | |
StringBuilder stringBuilder = new StringBuilder(); | |
String line = br.readLine(); | |
while (line != null) { | |
// add line to builder | |
stringBuilder.append(line); | |
// read next line | |
line = br.readLine(); | |
} | |
return stringBuilder.toString(); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public static void updateDocument(File playerFile, String json) { | |
// init account file writer with json string | |
try { | |
PrintWriter writer = new PrintWriter(playerFile); | |
BufferedWriter bw = new BufferedWriter(writer); | |
bw.write(json); | |
bw.close(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
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
package fr.gravendev.cyg.utils; | |
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 java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.UUID; | |
import org.bukkit.World; | |
import org.bukkit.WorldCreator; | |
import org.bukkit.entity.Player; | |
public class WorldUtils { | |
public static String createWorld(Player player) { | |
// get player uuid | |
UUID playerUUID = player.getUniqueId(); | |
// generate world name | |
String worldName = playerUUID.toString(); | |
copyFileStructure(new File("plot_template"), new File(worldName)); | |
// loading world creator instance | |
WorldCreator wc = new WorldCreator(worldName); | |
// get world pattern by name | |
World worldPattern = player.getWorld(); | |
// copy this world pattern | |
wc.copy(worldPattern); | |
// generate the world | |
wc.createWorld(); | |
// return world name | |
return worldName; | |
} | |
public static void destroyWorld() { /* TO DO */ } | |
private static void copyFileStructure(File source, File target){ | |
try { | |
ArrayList<String> ignore = new ArrayList<>(Arrays.asList("uid.dat", "session.lock")); | |
if(!ignore.contains(source.getName())) { | |
if(source.isDirectory()) { | |
if(!target.exists()) | |
if (!target.mkdirs()) | |
throw new IOException("Couldn't create world directory!"); | |
String files[] = source.list(); | |
for (String file : files) { | |
File srcFile = new File(source, file); | |
File destFile = new File(target, file); | |
copyFileStructure(srcFile, destFile); | |
} | |
} else { | |
InputStream in = new FileInputStream(source); | |
OutputStream out = new FileOutputStream(target); | |
byte[] buffer = new byte[1024]; | |
int length; | |
while ((length = in.read(buffer)) > 0) | |
out.write(buffer, 0, length); | |
in.close(); | |
out.close(); | |
} | |
} | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment