PlotSquared/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/WorldUtil.java

152 lines
6.4 KiB
Java
Raw Normal View History

package com.github.intellectualsites.plotsquared.plot.util;
2016-02-10 19:59:51 +01:00
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.object.*;
import com.github.intellectualsites.plotsquared.plot.object.schematic.PlotItem;
import com.sk89q.jnbt.*;
2018-08-10 17:01:10 +02:00
import java.io.*;
import java.net.URL;
import java.util.Map;
2017-03-23 01:10:29 +01:00
import java.util.Set;
import java.util.UUID;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
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-02-10 19:59:51 +01:00
public abstract Location getSpawn(String world);
public abstract Location getSpawn(PlotPlayer pp);
2016-03-23 02:41:37 +01:00
public abstract void setSpawn(Location location);
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);
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) {
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) {
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];
if (dat != null) {
2016-03-23 02:41:37 +01:00
ZipEntry ze = new ZipEntry("world" + File.separator + dat.getName());
zos.putNextEntry(ze);
2018-08-10 17:01:10 +02:00
try (NBTInputStream nis = new NBTInputStream(
new GZIPInputStream(new FileInputStream(dat)))) {
CompoundTag tag = (CompoundTag) nis.readNamedTag().getTag();
CompoundTag data = (CompoundTag) tag.getValue().get("Data");
Map<String, Tag> map = ReflectionUtils.getMap(data.getValue());
map.put("SpawnX", new IntTag(home.getX()));
map.put("SpawnY", new IntTag(home.getY()));
map.put("SpawnZ", new IntTag(home.getZ()));
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());
}
}
}
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);
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);
zos.putNextEntry(ze);
2017-03-23 01:10:29 +01:00
final FileInputStream in = new FileInputStream(file);
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();
}
}
}
}
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(
PlotSquared.get().IMP.getWorldContainer() + File.separator + world + File.separator
2018-08-10 17:01:10 +02:00
+ "level.dat");
if (file.exists()) {
return file;
}
return null;
}
2016-03-23 02:41:37 +01:00
public File getMcr(String world, int x, int z) {
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");
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
}