2018-08-10 17:53:14 +02:00
|
|
|
package com.github.intellectualsites.plotsquared.plot.util;
|
2016-02-10 19:59:51 +01:00
|
|
|
|
2018-11-14 14:19:56 +01:00
|
|
|
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
|
2018-08-10 17:53:14 +02:00
|
|
|
import com.github.intellectualsites.plotsquared.plot.object.*;
|
|
|
|
import com.github.intellectualsites.plotsquared.plot.object.schematic.PlotItem;
|
2018-12-20 00:18:57 +01:00
|
|
|
import com.sk89q.jnbt.*;
|
2018-08-10 17:01:10 +02:00
|
|
|
|
|
|
|
import java.io.*;
|
2016-03-17 10:11:07 +01:00
|
|
|
import java.net.URL;
|
2017-09-19 16:33:29 +02:00
|
|
|
import java.util.Map;
|
2017-03-23 01:10:29 +01:00
|
|
|
import java.util.Set;
|
2016-03-17 10:11:07 +01:00
|
|
|
import java.util.UUID;
|
2017-09-19 16:33:29 +02:00
|
|
|
import java.util.zip.GZIPInputStream;
|
|
|
|
import java.util.zip.GZIPOutputStream;
|
2016-03-17 10:11:07 +01:00
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipOutputStream;
|
2016-02-10 19:59:51 +01:00
|
|
|
|
|
|
|
public abstract class WorldUtil {
|
|
|
|
public static WorldUtil IMP;
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public abstract int getBiomeFromString(String value);
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public abstract String[] getBiomeList();
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public abstract String getMainWorld();
|
|
|
|
|
2016-03-29 21:47:59 +02:00
|
|
|
public abstract boolean isWorld(String worldName);
|
2016-03-23 02:41:37 +01:00
|
|
|
|
|
|
|
public abstract String[] getSign(Location location);
|
2016-03-31 11:48:46 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public abstract Location getSpawn(String world);
|
2016-03-17 10:11:07 +01:00
|
|
|
|
2016-03-31 11:48:46 +02:00
|
|
|
public abstract Location getSpawn(PlotPlayer pp);
|
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public abstract void setSpawn(Location location);
|
2016-03-17 10:11:07 +01:00
|
|
|
|
|
|
|
public abstract void saveWorld(String world);
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public abstract String getClosestMatchingName(PlotBlock plotBlock);
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public abstract boolean isBlockSolid(PlotBlock block);
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public abstract StringComparison<PlotBlock>.ComparisonResult getClosestBlock(String name);
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public abstract String getBiome(String world, int x, int z);
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public abstract PlotBlock getBlock(Location location);
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-19 17:55:00 +01:00
|
|
|
public abstract int getHighestBlock(String world, int x, int z);
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public abstract boolean addItems(String world, PlotItem item);
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public abstract void setSign(String world, int x, int y, int z, String[] lines);
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public abstract void setBiomes(String world, RegionWrapper region, String biome);
|
2016-03-17 10:11:07 +01:00
|
|
|
|
2018-12-20 00:18:57 +01:00
|
|
|
public abstract com.sk89q.worldedit.world.World getWeWorld(String world);
|
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public void upload(final Plot plot, UUID uuid, String file, RunnableVal<URL> whenDone) {
|
2016-03-17 10:11:07 +01:00
|
|
|
if (plot == null) {
|
|
|
|
throw new IllegalArgumentException("Plot may not be null!");
|
|
|
|
}
|
|
|
|
final Location home = plot.getHome();
|
|
|
|
MainUtil.upload(uuid, file, "zip", new RunnableVal<OutputStream>() {
|
2018-08-10 17:01:10 +02:00
|
|
|
@Override public void run(OutputStream output) {
|
2016-03-17 10:11:07 +01:00
|
|
|
try (final ZipOutputStream zos = new ZipOutputStream(output)) {
|
2017-03-23 01:10:29 +01:00
|
|
|
File dat = getDat(plot.getWorldName());
|
|
|
|
Location spawn = getSpawn(plot.getWorldName());
|
2016-03-23 18:09:13 +01:00
|
|
|
byte[] buffer = new byte[1024];
|
2016-03-17 10:11:07 +01:00
|
|
|
if (dat != null) {
|
2016-03-23 02:41:37 +01:00
|
|
|
ZipEntry ze = new ZipEntry("world" + File.separator + dat.getName());
|
2016-03-17 10:11:07 +01:00
|
|
|
zos.putNextEntry(ze);
|
2018-08-10 17:01:10 +02:00
|
|
|
try (NBTInputStream nis = new NBTInputStream(
|
|
|
|
new GZIPInputStream(new FileInputStream(dat)))) {
|
2018-12-20 00:18:57 +01:00
|
|
|
CompoundTag tag = (CompoundTag) nis.readNamedTag().getTag();
|
2017-09-19 16:33:29 +02:00
|
|
|
CompoundTag data = (CompoundTag) tag.getValue().get("Data");
|
|
|
|
Map<String, Tag> map = ReflectionUtils.getMap(data.getValue());
|
2018-12-20 00:18:57 +01:00
|
|
|
map.put("SpawnX", new IntTag(home.getX()));
|
|
|
|
map.put("SpawnY", new IntTag(home.getY()));
|
|
|
|
map.put("SpawnZ", new IntTag(home.getZ()));
|
2019-02-15 18:50:43 +01:00
|
|
|
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
|
|
|
try (NBTOutputStream out = new NBTOutputStream(
|
|
|
|
new GZIPOutputStream(baos, true))) {
|
|
|
|
//TODO Find what this should be called
|
|
|
|
out.writeNamedTag("Schematic????", tag);
|
|
|
|
}
|
|
|
|
zos.write(baos.toByteArray());
|
2017-09-19 16:33:29 +02:00
|
|
|
}
|
2016-03-17 10:11:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
setSpawn(spawn);
|
|
|
|
for (Plot current : plot.getConnectedPlots()) {
|
2016-03-23 02:41:37 +01:00
|
|
|
Location bot = current.getBottomAbs();
|
|
|
|
Location top = current.getTopAbs();
|
|
|
|
int brx = bot.getX() >> 9;
|
|
|
|
int brz = bot.getZ() >> 9;
|
|
|
|
int trx = top.getX() >> 9;
|
|
|
|
int trz = top.getZ() >> 9;
|
2017-03-23 01:10:29 +01:00
|
|
|
Set<ChunkLoc> files = ChunkManager.manager.getChunkChunks(bot.getWorld());
|
|
|
|
for (ChunkLoc mca : files) {
|
|
|
|
if (mca.x >= brx && mca.x <= trx && mca.z >= brz && mca.z <= trz) {
|
|
|
|
final File file = getMcr(plot.getWorldName(), mca.x, mca.z);
|
2016-03-17 10:11:07 +01:00
|
|
|
if (file != null) {
|
|
|
|
//final String name = "r." + (x - cx) + "." + (z - cz) + ".mca";
|
|
|
|
String name = file.getName();
|
2018-08-10 17:01:10 +02:00
|
|
|
final ZipEntry ze = new ZipEntry(
|
|
|
|
"world" + File.separator + "region" + File.separator
|
|
|
|
+ name);
|
2016-03-17 10:11:07 +01:00
|
|
|
zos.putNextEntry(ze);
|
2017-03-23 01:10:29 +01:00
|
|
|
final FileInputStream in = new FileInputStream(file);
|
2016-03-17 10:11:07 +01:00
|
|
|
int len;
|
|
|
|
while ((len = in.read(buffer)) > 0) {
|
|
|
|
zos.write(buffer, 0, len);
|
|
|
|
}
|
|
|
|
in.close();
|
2017-03-23 01:10:29 +01:00
|
|
|
zos.closeEntry();
|
2016-03-17 10:11:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
zos.closeEntry();
|
|
|
|
zos.flush();
|
|
|
|
zos.finish();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, whenDone);
|
|
|
|
}
|
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public File getDat(String world) {
|
2018-08-10 17:01:10 +02:00
|
|
|
File file = new File(
|
2018-11-14 14:19:56 +01:00
|
|
|
PlotSquared.get().IMP.getWorldContainer() + File.separator + world + File.separator
|
2018-08-10 17:01:10 +02:00
|
|
|
+ "level.dat");
|
2016-03-17 10:11:07 +01:00
|
|
|
if (file.exists()) {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public File getMcr(String world, int x, int z) {
|
2018-11-14 14:19:56 +01:00
|
|
|
File file = new File(PlotSquared.get().IMP.getWorldContainer(),
|
2018-08-10 17:01:10 +02:00
|
|
|
world + File.separator + "region" + File.separator + "r." + x + '.' + z + ".mca");
|
2016-03-17 10:11:07 +01:00
|
|
|
if (file.exists()) {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2018-12-17 20:57:21 +01:00
|
|
|
|
|
|
|
public abstract boolean isBlockSame(PlotBlock block1, PlotBlock block2);
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|