2015-08-18 23:20:11 +10:00
|
|
|
package com.intellectualcrafters.plot.util;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.StandardOpenOption;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
|
|
import com.intellectualcrafters.plot.PS;
|
|
|
|
import com.intellectualcrafters.plot.config.Settings;
|
2015-08-25 02:33:28 +10:00
|
|
|
import com.intellectualcrafters.plot.flag.FlagManager;
|
2015-08-18 23:20:11 +10:00
|
|
|
import com.intellectualcrafters.plot.generator.ClassicPlotWorld;
|
|
|
|
import com.intellectualcrafters.plot.object.BO3;
|
|
|
|
import com.intellectualcrafters.plot.object.ChunkLoc;
|
|
|
|
import com.intellectualcrafters.plot.object.Location;
|
|
|
|
import com.intellectualcrafters.plot.object.Plot;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotBlock;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotWorld;
|
|
|
|
|
2015-09-11 20:09:22 +10:00
|
|
|
public class BO3Handler
|
|
|
|
{
|
|
|
|
|
2015-08-18 23:20:11 +10:00
|
|
|
/**
|
|
|
|
* @see #saveBO3(null, Plot)
|
|
|
|
* @param plot
|
2015-09-11 20:09:22 +10:00
|
|
|
* @return if successfully exported
|
2015-08-18 23:20:11 +10:00
|
|
|
*/
|
2015-09-11 20:09:22 +10:00
|
|
|
public static boolean saveBO3(final Plot plot)
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
return saveBO3(null, plot);
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
|
|
|
|
public static boolean contains(final PlotBlock[] blocks, final PlotBlock block)
|
|
|
|
{
|
|
|
|
for (final PlotBlock item : blocks)
|
|
|
|
{
|
|
|
|
if (item.equals(block)) { return true; }
|
2015-09-03 15:57:12 +10:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
|
2015-08-18 23:20:11 +10:00
|
|
|
/**
|
|
|
|
* Save a plot as a BO3 file<br>
|
|
|
|
* - Use null for the player object if no player is applicable
|
|
|
|
* @param plr
|
|
|
|
* @param plot
|
|
|
|
* @return
|
|
|
|
*/
|
2015-09-11 20:09:22 +10:00
|
|
|
public static boolean saveBO3(final PlotPlayer plr, final Plot plot)
|
|
|
|
{
|
|
|
|
final PlotWorld plotworld = PS.get().getPlotWorld(plot.world);
|
|
|
|
if (!(plotworld instanceof ClassicPlotWorld) || (plotworld.TYPE != 0))
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
MainUtil.sendMessage(plr, "BO3 exporting only supports type 0 classic generation.");
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
final String alias = plot.toString();
|
|
|
|
final Location pos1 = plot.getBottom();
|
|
|
|
final Location pos2 = plot.getTop();
|
|
|
|
final ClassicPlotWorld cpw = (ClassicPlotWorld) plotworld;
|
|
|
|
final int height = cpw.PLOT_HEIGHT;
|
|
|
|
|
|
|
|
final int cx = (pos1.getX() + pos2.getX()) / 2;
|
|
|
|
final int cz = (pos1.getZ() + pos2.getZ()) / 2;
|
|
|
|
|
|
|
|
final HashMap<ChunkLoc, BO3> map = new HashMap<>();
|
|
|
|
|
2015-08-18 23:20:11 +10:00
|
|
|
boolean content = false;
|
2015-09-11 20:09:22 +10:00
|
|
|
for (int x = pos1.getX(); x <= pos2.getX(); x++)
|
|
|
|
{
|
|
|
|
final int X = ((x + 7) - cx) >> 4;
|
2015-09-13 01:19:39 +10:00
|
|
|
final int xx = (x - cx) % 16;
|
|
|
|
for (int z = pos1.getZ(); z <= pos2.getZ(); z++)
|
|
|
|
{
|
|
|
|
final int Z = ((z + 7) - cz) >> 4;
|
|
|
|
final int zz = (z - cz) % 16;
|
|
|
|
final ChunkLoc loc = new ChunkLoc(X, Z);
|
|
|
|
BO3 bo3 = map.get(loc);
|
|
|
|
for (int y = 1; y < height; y++)
|
|
|
|
{
|
|
|
|
final PlotBlock block = BlockManager.manager.getBlock(new Location(plot.world, x, y, z));
|
|
|
|
if ((block != null) && !contains(cpw.MAIN_BLOCK, block))
|
2015-09-11 20:09:22 +10:00
|
|
|
{
|
2015-09-13 01:19:39 +10:00
|
|
|
if (bo3 == null)
|
2015-09-11 20:09:22 +10:00
|
|
|
{
|
2015-09-13 01:19:39 +10:00
|
|
|
bo3 = new BO3(alias, loc);
|
|
|
|
map.put(loc, bo3);
|
|
|
|
content = true;
|
2015-09-03 15:57:12 +10:00
|
|
|
}
|
2015-09-13 01:19:39 +10:00
|
|
|
bo3.addBlock(xx, y - height - 1, zz, block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
final PlotBlock floor = BlockManager.manager.getBlock(new Location(plot.world, x, height, z));
|
|
|
|
if ((floor != null) && !contains(cpw.TOP_BLOCK, floor))
|
|
|
|
{
|
|
|
|
if (bo3 == null)
|
|
|
|
{
|
|
|
|
bo3 = new BO3(alias, loc);
|
|
|
|
map.put(loc, bo3);
|
|
|
|
content = true;
|
|
|
|
}
|
|
|
|
bo3.addBlock(xx, -1, zz, floor);
|
|
|
|
}
|
|
|
|
for (int y = height + 1; y < 256; y++)
|
|
|
|
{
|
|
|
|
final PlotBlock block = BlockManager.manager.getBlock(new Location(plot.world, x, y, z));
|
|
|
|
if ((block != null) && (block.id != 0))
|
|
|
|
{
|
|
|
|
if (bo3 == null)
|
2015-09-11 20:09:22 +10:00
|
|
|
{
|
2015-09-13 01:19:39 +10:00
|
|
|
bo3 = new BO3(alias, loc);
|
|
|
|
map.put(loc, bo3);
|
|
|
|
content = true;
|
2015-08-18 23:20:11 +10:00
|
|
|
}
|
2015-09-13 01:19:39 +10:00
|
|
|
bo3.addBlock(xx, y - height - 1, zz, block);
|
2015-08-18 23:20:11 +10:00
|
|
|
}
|
|
|
|
}
|
2015-09-13 01:19:39 +10:00
|
|
|
}
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
|
|
|
|
if (!content)
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
MainUtil.sendMessage(plr, "No content found!");
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
|
|
|
|
for (final Entry<ChunkLoc, BO3> entry : map.entrySet())
|
|
|
|
{
|
|
|
|
final ChunkLoc chunk = entry.getKey();
|
|
|
|
final BO3 bo3 = entry.getValue();
|
|
|
|
if ((chunk.x == 0) && (chunk.z == 0))
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int x = chunk.x;
|
|
|
|
int z = chunk.z;
|
2015-09-11 20:09:22 +10:00
|
|
|
if (Math.abs(chunk.x) > Math.abs(chunk.z))
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
x += chunk.x > 0 ? -1 : 1;
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
else
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
z += chunk.z > 0 ? -1 : 1;
|
|
|
|
}
|
|
|
|
ChunkLoc parentLoc = new ChunkLoc(x, z);
|
2015-09-11 20:09:22 +10:00
|
|
|
if (!map.containsKey(parentLoc))
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
parentLoc = null;
|
2015-09-11 20:09:22 +10:00
|
|
|
for (final Entry<ChunkLoc, BO3> entry2 : map.entrySet())
|
|
|
|
{
|
|
|
|
final ChunkLoc other = entry2.getKey();
|
|
|
|
if (((other.x == (chunk.x - 1)) && (other.z == chunk.z)) || ((other.z == (chunk.z - 1)) && (other.x == chunk.x)))
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
parentLoc = other;
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
if (parentLoc == null)
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
MainUtil.sendMessage(plr, "Exporting BO3 cancelled due to detached chunk: " + chunk + " - Make sure you only have one object per plot");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
map.get(parentLoc).addChild(bo3);
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
|
|
|
|
for (final Entry<ChunkLoc, BO3> entry : map.entrySet())
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
save(plot, entry.getValue());
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
|
2015-08-18 23:20:11 +10:00
|
|
|
MainUtil.sendMessage(plr, "BO3 exporting was successful!");
|
|
|
|
return true;
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
|
|
|
|
public static boolean save(final Plot plot, final BO3 bo3)
|
|
|
|
{
|
|
|
|
final File base = getBaseFile(plot.world);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
final List<String> lines = Files.readAllLines(base.toPath(), StandardCharsets.UTF_8);
|
|
|
|
for (int i = 0; i < lines.size(); i++)
|
|
|
|
{
|
|
|
|
final String line = lines.get(i).trim();
|
|
|
|
final String result = StringMan.replaceAll(line, "%owner%", MainUtil.getName(plot.owner), "%alias%", plot.toString(), "%blocks%", bo3.getBlocks(), "%branches%", bo3.getChildren(),
|
|
|
|
"%flags%", StringMan.join(FlagManager.getPlotFlags(plot).values(), ","));
|
|
|
|
if (!StringMan.isEqual(result, line))
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
lines.set(i, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
File bo3File;
|
2015-09-11 20:09:22 +10:00
|
|
|
if ((bo3.getLoc().x == 0) && (bo3.getLoc().z == 0))
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
bo3File = new File(base.getParentFile(), bo3.getName() + ".bo3");
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
else
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
bo3File = new File(base.getParentFile(), bo3.getName() + "_" + bo3.getLoc().x + "_" + bo3.getLoc().z + ".bo3");
|
|
|
|
}
|
|
|
|
bo3File.createNewFile();
|
|
|
|
Files.write(bo3File.toPath(), StringMan.join(lines, System.getProperty("line.separator")).getBytes(), StandardOpenOption.WRITE);
|
|
|
|
return true;
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
catch (final Exception e)
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 20:09:22 +10:00
|
|
|
|
|
|
|
public static File getBaseFile(final String category)
|
|
|
|
{
|
|
|
|
final File base = new File(PS.get().IMP.getDirectory(), Settings.BO3_SAVE_PATH + File.separator + category + File.separator + "base.yml");
|
|
|
|
if (!base.exists())
|
|
|
|
{
|
2015-08-18 23:20:11 +10:00
|
|
|
PS.get().copyFile("base.yml", Settings.BO3_SAVE_PATH + File.separator + category);
|
|
|
|
}
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
}
|