Updated PlotMe converter (untested)

This commit is contained in:
boy0001 2015-01-10 01:05:10 +11:00
parent 791ce39ecc
commit 1d0d85a161
10 changed files with 787 additions and 389 deletions

View File

@ -176,23 +176,6 @@ import java.util.concurrent.TimeUnit;
return mySQL; return mySQL;
} }
/**
* Check for expired plots
*/
public static void checkForExpiredPlots() {
final JavaPlugin plugin = PlotMain.getMain();
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
@Override
public void run() {
try {
checkExpired(plugin, true);
} catch (final Exception e) {
e.printStackTrace();
}
}
}, 0l, 86_40_00L);
}
/** /**
* Check a range of permissions e.g. 'plots.plot.<0-100>'<br> Returns highest integer in range. * Check a range of permissions e.g. 'plots.plot.<0-100>'<br> Returns highest integer in range.
* *
@ -512,85 +495,6 @@ import java.util.concurrent.TimeUnit;
plots.get(world).put(plot.id, plot); plots.get(world).put(plot.id, plot);
} }
/**
* TODO: <b>Implement better system The whole point of this system is to recycle old plots</b> <br> So why not just
* allow users to claim old plots, and try to hide the fact that the are owned. <br> <br> Reduce amount of expired
* plots: <br> - On /plot <br> auto<br> - allow claiming of old plot, clear it so the user doesn't know<br> - On
* /plot info,<br> - show that the plot is expired and allowed to be claimed Have the task run less often:<br> - Run
* the task when there are very little, or no players online (great for small servers)<br> - Run the task at startup
* (also only useful for small servers)<br> Also, in terms of faster code:<br> - Have an array of plots, sorted by
* expiry time.<br> - Add new plots to the end.<br> - The task then only needs to go through the first few plots
*
* @param plugin Plugin
* @param async Call async?
*/
private static void checkExpired(final JavaPlugin plugin, final boolean async) {
if (async) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
for (final String world : getPlotWorldsString()) {
if (plots.containsKey(world)) {
final ArrayList<Plot> toDeletePlot = new ArrayList<>();
for (final Plot plot : plots.get(world).values()) {
if (plot.owner == null) {
continue;
}
final long lastPlayed = getLastPlayed(plot.owner);
if (lastPlayed == 0) {
continue;
}
final long compared = System.currentTimeMillis() - lastPlayed;
if (TimeUnit.MILLISECONDS.toDays(compared) >= Settings.AUTO_CLEAR_DAYS) {
final PlotDeleteEvent event = new PlotDeleteEvent(world, plot.id);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
event.setCancelled(true);
} else {
toDeletePlot.add(plot);
}
}
}
for (final Plot plot : toDeletePlot) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() {
final World worldobj = Bukkit.getWorld(world);
PlotHelper.clear(worldobj, plot, true);
PlotHelper.removeSign(worldobj, plot);
DBFunc.delete(world, plot);
removePlot(world, plot.id, true);
if ((Math.abs(plot.id.x) < Math.abs(Auto.lastPlot.x)) && (Math.abs(plot.id.y) < Math.abs(Auto.lastPlot.y))) {
Auto.lastPlot = plot.id;
}
}
});
}
}
}
}
});
} else {
for (final String world : getPlotWorldsString()) {
if (PlotMain.plots.containsKey(world)) {
for (final Plot plot : PlotMain.plots.get(world).values()) {
if (PlayerFunctions.hasExpired(plot)) {
final PlotDeleteEvent event = new PlotDeleteEvent(world, plot.id);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
event.setCancelled(true);
} else {
DBFunc.delete(world, plot);
}
}
}
}
}
}
}
/** /**
* Get the java version * Get the java version
* *
@ -1451,7 +1355,7 @@ import java.util.concurrent.TimeUnit;
// nor listeners, just run the converter? // nor listeners, just run the converter?
if (getServer().getPluginManager().getPlugin("PlotMe") != null) { if (getServer().getPluginManager().getPlugin("PlotMe") != null) {
try { try {
new PlotMeConverter(this).runAsync(); // new PlotMeConverter(this).runAsync();
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -1507,8 +1411,7 @@ import java.util.concurrent.TimeUnit;
} }
} }
if (Settings.AUTO_CLEAR) { if (Settings.AUTO_CLEAR) {
checkExpired(PlotMain.getMain(), true); ExpireManager.runTask();
checkForExpiredPlots();
} }
// Economy setup // Economy setup
{ {

View File

@ -127,8 +127,7 @@ public class Schematic extends SubCommand {
final int LENGTH = schematic.getSchematicDimension().getZ(); final int LENGTH = schematic.getSchematicDimension().getZ();
final int blen = b.length - 1; final int blen = b.length - 1;
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("PlotSquared"); Schematic.this.task = Bukkit.getScheduler().scheduleSyncRepeatingTask(PlotMain.getMain(), new Runnable() {
Schematic.this.task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
boolean result = false; boolean result = false;

View File

@ -46,8 +46,10 @@ import com.intellectualcrafters.plot.object.ChunkLoc;
import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.object.PlotManager; import com.intellectualcrafters.plot.object.PlotManager;
import com.intellectualcrafters.plot.util.ChunkManager;
import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.PlayerFunctions;
import com.intellectualcrafters.plot.util.PlotHelper; import com.intellectualcrafters.plot.util.PlotHelper;
import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.UUIDHandler; import com.intellectualcrafters.plot.util.UUIDHandler;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -154,7 +156,7 @@ import org.bukkit.entity.Player;
return false; return false;
} }
Trim.TASK = true; Trim.TASK = true;
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
final HybridPlotManager manager = (HybridPlotManager) PlotMain.getPlotManager(world); final HybridPlotManager manager = (HybridPlotManager) PlotMain.getPlotManager(world);
@ -231,7 +233,7 @@ import org.bukkit.entity.Player;
private void trimPlots(World world) { private void trimPlots(World world) {
String worldname = world.getName(); String worldname = world.getName();
ArrayList<ChunkLoc> chunks = getChunkChunks(world); ArrayList<ChunkLoc> chunks = ChunkManager.getChunkChunks(world);
for (ChunkLoc loc : chunks) { for (ChunkLoc loc : chunks) {
int sx = loc.x << 4; int sx = loc.x << 4;
int sz = loc.z << 4; int sz = loc.z << 4;
@ -242,74 +244,20 @@ import org.bukkit.entity.Player;
for (int x = sx; x < sx + 16; x++) { for (int x = sx; x < sx + 16; x++) {
for (int z = sz; z < sz + 16; z++) { for (int z = sz; z < sz + 16; z++) {
Chunk chunk = world.getChunkAt(x, z); Chunk chunk = world.getChunkAt(x, z);
if (hasPlot(world, chunk)) { if (ChunkManager.hasPlot(world, chunk)) {
delete = false; delete = false;
break loop; break loop;
} }
} }
} }
if (delete) { if (delete) {
deleteRegionFile(worldname, loc); ChunkManager.deleteRegionFile(worldname, loc);
} }
} }
} }
public void deleteRegionFile(final String world, final ChunkLoc loc) {
runTask(new Runnable() {
@Override
public void run() {
String directory = new File(".").getAbsolutePath() + File.separator + world + File.separator + "region" + File.separator + "r." + loc.x + "." + loc.z + ".mca";
File file = new File(directory);
PlotMain.sendConsoleSenderMessage("&6 - Deleted region "+file.getName()+" (max 256 chunks)");
if (file.exists()) {
file.delete();
}
}
});
}
public ArrayList<ChunkLoc> getChunkChunks(World world) {
File[] regionFiles = new File(new File(".").getAbsolutePath() + File.separator + world.getName() + File.separator + "region").listFiles();
ArrayList<ChunkLoc> chunks = new ArrayList<>();
for (File file : regionFiles) {
String name = file.getName();
if (name.endsWith("mca")) {
String[] split = name.split("\\.");
try {
chunks.add(new ChunkLoc(Integer.parseInt(split[1]), Integer.parseInt(split[2])));
} catch (Exception e) { }
}
}
return chunks;
}
private boolean hasPlot(World world, Chunk chunk) {
int x1 = chunk.getX() << 4;
int z1 = chunk.getZ() << 4;
int x2 = x1 + 15;
int z2 = z1 + 15;
Location bot = new Location(world, x1, 0, z1);
Plot plot;
plot = PlotHelper.getCurrentPlot(bot);
if (plot != null && plot.owner != null) {
return true;
}
Location top = new Location(world, x2, 0, z2);
plot = PlotHelper.getCurrentPlot(top);
if (plot != null && plot.owner != null) {
return true;
}
return false;
}
private void sendMessage(final String message) { private void sendMessage(final String message) {
PlotMain.sendConsoleSenderMessage("&3PlotSquared -> World trim&8: " + message); PlotMain.sendConsoleSenderMessage("&3PlotSquared -> World trim&8: " + message);
} }
private void runTask(final Runnable r) {
PlotMain.getMain().getServer().getScheduler().runTaskAsynchronously(PlotMain.getMain(), r);
}
} }

View File

@ -113,7 +113,9 @@ public class Settings {
/** /**
* Days until a plot gets cleared * Days until a plot gets cleared
*/ */
public static int AUTO_CLEAR_DAYS = 365; public static int AUTO_CLEAR_DAYS = -1;
public static int MIN_BLOCKS_CHANGED = -1;
/** /**
* API Location * API Location

View File

@ -21,24 +21,24 @@
package com.intellectualcrafters.plot.database; package com.intellectualcrafters.plot.database;
import com.google.common.base.Charsets;
import com.intellectualcrafters.plot.PlotMain; import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.generator.HybridGen; import com.intellectualcrafters.plot.generator.HybridGen;
import com.intellectualcrafters.plot.object.PlotHomePosition; import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotId;
import com.worldcretornica.plotme.PlayerList; import com.intellectualcrafters.plot.util.UUIDHandler;
import com.worldcretornica.plotme.Plot;
import com.worldcretornica.plotme.PlotManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.WorldCreator; import org.bukkit.WorldCreator;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*; import java.util.*;
/** /**
@ -64,7 +64,7 @@ public class PlotMeConverter {
} }
private void sendMessage(final String message) { private void sendMessage(final String message) {
PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8: " + message); PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8: &7" + message);
} }
public void runAsync() throws Exception { public void runAsync() throws Exception {
@ -73,160 +73,188 @@ public class PlotMeConverter {
@Override @Override
public void run() { public void run() {
sendMessage("&7Conversion has started"); try {
sendMessage("&7Caching playerdata..."); sendMessage("Conversion has started");
final ArrayList<com.intellectualcrafters.plot.object.Plot> createdPlots = new ArrayList<>(); sendMessage("Connecting to PlotMe DB");
// Online Mode final ArrayList<Plot> createdPlots = new ArrayList<>();
final boolean online = Bukkit.getServer().getOnlineMode() && !Settings.OFFLINE_MODE;
// PlotMe Plugin
final Plugin plotMePlugin = Bukkit.getPluginManager().getPlugin("PlotMe"); final Plugin plotMePlugin = Bukkit.getPluginManager().getPlugin("PlotMe");
// PlotMe Configuration
final FileConfiguration plotConfig = plotMePlugin.getConfig(); final FileConfiguration plotConfig = plotMePlugin.getConfig();
// Plot Worlds int count = 0;
final Set<String> worlds = new HashSet<>();
// Loop through the worlds Connection connection;
int duplicate; if (plotConfig.getBoolean("usemySQL")) {
HashMap<String, Plot> plots; String user = plotConfig.getString("mySQLuname");
for (World world : Bukkit.getWorlds()) { String password = plotConfig.getString("mySQLpass");
duplicate = 0; String con = plotConfig.getString("mySQLconn").replaceAll("jdbc:mysql://", "");
plots = PlotManager.getPlots(world); String host = con.split(":")[0];
if (plots != null) { String port = con.split(":")[1].split("/")[0];
worlds.add(world.getName()); String database = con.split(":")[1].split("/")[1];
sendMessage("&7Converting configuration for world '" + world.getName() + "'..."); MySQL mySQL = new MySQL(PlotMain.getMain(), host, port, database, user, password);
connection = mySQL.openConnection();
}
else {
connection = new SQLite(PlotMain.getMain(), plotMePlugin.getDataFolder() + File.separator +"plots.db").openConnection();
}
sendMessage("Collecting plot data");
ResultSet r;
Statement stmt;
HashMap<String, Integer> plotSize = new HashMap<>();
HashMap<String, HashMap<PlotId, Plot>> plots = new HashMap<>();
Set<String> worlds = plotConfig.getConfigurationSection("worlds").getKeys(false);
stmt = connection.createStatement();
r = stmt.executeQuery("SELECT * FROM `plotmePlots`");
while (r.next()) {
PlotId id = new PlotId(r.getInt("idX"), r.getInt("idZ"));
String name = r.getString("owner");
String world = r.getString("world");
if (!plotSize.containsKey(world)) {
int size = r.getInt("topZ") - r.getInt("botZ");
plotSize.put(world,size);
plots.put(world, new HashMap<PlotId, Plot>());
}
UUID owner = UUIDHandler.getUUID(name);
if (owner == null) {
if (name.equals("*")) {
owner = DBFunc.everyone;
}
else {
sendMessage("&cCould not identify owner for plot: "+id);
continue;
}
}
Plot plot = new Plot(id, owner, new ArrayList<UUID>() , new ArrayList<UUID>(), world);
plots.get(world).put(id, plot);
}
r = stmt.executeQuery("SELECT * FROM `plotmeAllowed`");
while (r.next()) {
count++;
PlotId id = new PlotId(r.getInt("idX"), r.getInt("idZ"));
String name = r.getString("player");
String world = r.getString("world");
UUID helper = UUIDHandler.getUUID(name);
if (helper == null) {
if (name.equals("*")) {
helper = DBFunc.everyone;
}
else {
sendMessage("&6Could not identify helper for plot: "+id);
continue;
}
}
if (plots.get(world).containsKey(id)) {
plots.get(world).get(id).helpers.add(helper);
}
}
r = stmt.executeQuery("SELECT * FROM `plotmeDenied`");
while (r.next()) {
PlotId id = new PlotId(r.getInt("idX"), r.getInt("idZ"));
String name = r.getString("player");
String world = r.getString("world");
UUID denied = UUIDHandler.getUUID(name);
if (denied == null) {
if (name.equals("*")) {
denied = DBFunc.everyone;
}
else {
sendMessage("&6Could not identify denied for plot: "+id);
continue;
}
}
if (plots.get(world).containsKey(id)) {
plots.get(world).get(id).denied.add(denied);
}
}
sendMessage("Collected " + count + "plots from PlotMe");
for (String world : plots.keySet()) {
sendMessage("Copying config for: "+world);
try { try {
final Integer pathwidth = plotConfig.getInt("worlds." + world.getName() + ".PathWidth"); // final Integer pathwidth = plotConfig.getInt("worlds." + world + ".PathWidth"); //
PlotMain.config.set("worlds." + world.getName() + ".road.width", pathwidth); PlotMain.config.set("worlds." + world + ".road.width", pathwidth);
final Integer plotsize = plotConfig.getInt("worlds." + world.getName() + ".PlotSize"); // final Integer plotsize = plotConfig.getInt("worlds." + world + ".PlotSize"); //
PlotMain.config.set("worlds." + world.getName() + ".plot.size", plotsize); PlotMain.config.set("worlds." + world + ".plot.size", plotsize);
final String wallblock = plotConfig.getString("worlds." + world.getName() + ".WallBlockId"); // final String wallblock = plotConfig.getString("worlds." + world + ".WallBlockId"); //
PlotMain.config.set("worlds." + world.getName() + ".wall.block", wallblock); PlotMain.config.set("worlds." + world + ".wall.block", wallblock);
final String floor = plotConfig.getString("worlds." + world.getName() + ".PlotFloorBlockId"); // final String floor = plotConfig.getString("worlds." + world + ".PlotFloorBlockId"); //
PlotMain.config.set("worlds." + world.getName() + ".plot.floor", Arrays.asList(floor)); PlotMain.config.set("worlds." + world + ".plot.floor", Arrays.asList(floor));
final String filling = plotConfig.getString("worlds." + world.getName() + ".PlotFillingBlockId"); // final String filling = plotConfig.getString("worlds." + world + ".PlotFillingBlockId"); //
PlotMain.config.set("worlds." + world.getName() + ".plot.filling", Arrays.asList(filling)); PlotMain.config.set("worlds." + world + ".plot.filling", Arrays.asList(filling));
final String road = plotConfig.getString("worlds." + world.getName() + ".RoadMainBlockId"); final String road = plotConfig.getString("worlds." + world + ".RoadMainBlockId");
PlotMain.config.set("worlds." + world.getName() + ".road.block", road); PlotMain.config.set("worlds." + world + ".road.block", road);
final String road_stripe = plotConfig.getString("worlds." + world.getName() + ".RoadStripeBlockId"); final Integer height = plotConfig.getInt("worlds." + world + ".RoadHeight"); //
PlotMain.config.set("worlds." + world.getName() + ".road.stripes", road_stripe); PlotMain.config.set("worlds." + world + ".road.height", height);
final Integer height = plotConfig.getInt("worlds." + world.getName() + ".RoadHeight"); //
PlotMain.config.set("worlds." + world.getName() + ".road.height", height);
final Boolean auto_link = plotConfig.getBoolean("worlds." + world.getName() + ".AutoLinkPlots"); //
PlotMain.config.set("worlds." + world.getName() + ".plot.auto_merge", auto_link);
} catch (final Exception e) { } catch (final Exception e) {
sendMessage("&c-- &lFailed to save configuration for world '" + world.getName() + "'\nThis will need to be done using the setup command, or manually"); sendMessage("&c-- &lFailed to save configuration for world '" + world + "'\nThis will need to be done using the setup command, or manually");
}
} }
sendMessage("&7Processing '" + plots.size() + "' plots for world '" + world.getName() + "'"); File PLOTME_DG_FILE = new File(plotMePlugin + File.separator + "PlotMe-DefaultGenerator" + File.separator + "config.yml");
ArrayList<UUID> psAdded, psTrusted, psDenied; if (PLOTME_DG_FILE.exists()) {
for (final Plot plot : plots.values()) { YamlConfiguration PLOTME_DG_YML = YamlConfiguration.loadConfiguration(PLOTME_DG_FILE);
psAdded = new ArrayList<>();
psTrusted = new ArrayList<>();
psDenied = new ArrayList<>();
if (world == null) {
world = Bukkit.getWorld("world");
}
try { try {
if (online) { for (String world : plots.keySet()) {
PlayerList denied; final Integer pathwidth = PLOTME_DG_YML.getInt("worlds." + world + ".PathWidth"); //
PlayerList added; PlotMain.config.set("worlds." + world + ".road.width", pathwidth);
final Field fAdded = plot.getClass().getDeclaredField("allowed");
final Field fDenied = plot.getClass().getDeclaredField("denied");
fAdded.setAccessible(true);
fDenied.setAccessible(true);
added = (PlayerList) fAdded.get(plot);
denied = (PlayerList) fDenied.get(plot);
for (final Map.Entry<String, UUID> set : added.getAllPlayers().entrySet()) {
if ((set.getValue() != null) || set.getKey().equals("*")) {
if (set.getKey().equalsIgnoreCase("*") || set.getValue().toString().equals("*")) {
psAdded.add(DBFunc.everyone);
continue;
}
}
if (set.getValue() != null) {
psAdded.add(set.getValue());
}
}
for (final Map.Entry<String, UUID> set : denied.getAllPlayers().entrySet()) {
if ((set.getValue() != null) || set.getKey().equals("*")) {
if (set.getKey().equals("*") || set.getValue().toString().equals("*")) {
psDenied.add(DBFunc.everyone);
continue;
}
}
if (set.getValue() != null) {
psDenied.add(set.getValue());
}
}
} else {
for (final String user : plot.getAllowed().split(",")) {
if (user.equals("*")) {
psAdded.add(DBFunc.everyone);
} else {
final UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + user).getBytes(Charsets.UTF_8));
psAdded.add(uuid);
}
}
try {
for (final String user : plot.getDenied().split(",")) {
if (user.equals("*")) {
psDenied.add(DBFunc.everyone);
} else {
final UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + user).getBytes(Charsets.UTF_8));
psDenied.add(uuid);
}
}
} catch (final Throwable e) {
// Okay, this is evil.
}
}
} catch (final Throwable e) {
e.printStackTrace();
}
final PlotId id = new PlotId(Integer.parseInt(plot.id.split(";")[0]), Integer.parseInt(plot.id.split(";")[1]));
com.intellectualcrafters.plot.object.Plot pl;
if (online) {
pl = new com.intellectualcrafters.plot.object.Plot(id, plot.getOwnerId(), psAdded, psTrusted, psDenied,
"", PlotHomePosition.DEFAULT, null, world.getName(), new boolean[]{false, false, false, false}); final Integer plotsize = PLOTME_DG_YML.getInt("worlds." + world + ".PlotSize"); //
} else { PlotMain.config.set("worlds." + world + ".plot.size", plotsize);
final String owner = plot.getOwner();
pl = new com.intellectualcrafters.plot.object.Plot(id, UUID.nameUUIDFromBytes(("OfflinePlayer:" + owner).getBytes(Charsets.UTF_8)), psAdded, psTrusted, psDenied,
"", PlotHomePosition.DEFAULT, null, world.getName(), new boolean[]{false, false, false, false}); final String wallblock = PLOTME_DG_YML.getString("worlds." + world + ".WallBlock"); //
PlotMain.config.set("worlds." + world + ".wall.block", wallblock);
final String floor = PLOTME_DG_YML.getString("worlds." + world + ".PlotFloorBlock"); //
PlotMain.config.set("worlds." + world + ".plot.floor", Arrays.asList(floor));
final String filling = PLOTME_DG_YML.getString("worlds." + world + ".FillBlock"); //
PlotMain.config.set("worlds." + world + ".plot.filling", Arrays.asList(filling));
final String road = PLOTME_DG_YML.getString("worlds." + world + ".RoadMainBlock");
PlotMain.config.set("worlds." + world + ".road.block", road);
final Integer height = PLOTME_DG_YML.getInt("worlds." + world + ".RoadHeight"); //
PlotMain.config.set("worlds." + world + ".road.height", height);
} }
}
catch (Exception e) {
if (pl != null) { }
if (!PlotMain.getPlots(world).containsKey(id)) { }
createdPlots.add(pl); for (String world : plots.keySet()) {
} else { int duplicate = 0;
for (Plot plot : plots.get(world).values()) {
if (!PlotMain.getPlots(world).containsKey(plot.id)) {
createdPlots.add(plot);
}
else {
duplicate++; duplicate++;
} }
} }
}
if (duplicate > 0) { if (duplicate > 0) {
PlotMain.sendConsoleSenderMessage("&c[WARNING] Found " + duplicate + " duplicate plots already in DB for world: '" + world.getName() + "'. Have you run the converter already?"); PlotMain.sendConsoleSenderMessage("&c[WARNING] Found " + duplicate + " duplicate plots already in DB for world: '" + world + "'. Have you run the converter already?");
} }
} }
}
PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8: &7Creating plot DB");
DBFunc.createPlots(createdPlots);
PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8: &7Creating settings/helpers DB");
// TODO createPlot doesn't add denied users
sendMessage("Creating plot DB");
DBFunc.createPlots(createdPlots);
sendMessage("Creating settings/helpers DB");
DBFunc.createAllSettingsAndHelpers(createdPlots); DBFunc.createAllSettingsAndHelpers(createdPlots);
PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8:&7 Saving configuration..."); sendMessage("Saving configuration...");
try { try {
PlotMain.config.save(PlotMain.configFile); PlotMain.config.save(PlotMain.configFile);
} catch (final IOException e) { } catch (final IOException e) {
PlotMain.sendConsoleSenderMessage(" - &cFailed to save configuration."); sendMessage(" - &cFailed to save configuration.");
} }
boolean MV = false; boolean MV = false;
@ -240,7 +268,7 @@ public class PlotMeConverter {
for (final String worldname : worlds) { for (final String worldname : worlds) {
final World world = Bukkit.getWorld(worldname); final World world = Bukkit.getWorld(worldname);
PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8:&7 Reloading generator for world: '" + worldname + "'..."); sendMessage("Reloading generator for world: '" + worldname + "'...");
PlotMain.removePlotWorld(worldname); PlotMain.removePlotWorld(worldname);
@ -272,11 +300,15 @@ public class PlotMeConverter {
} }
PlotMain.setAllPlotsRaw(DBFunc.getPlots()); PlotMain.setAllPlotsRaw(DBFunc.getPlots());
sendMessage("Disabling PlotMe...");
PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8:&7 Disabling PlotMe...");
Bukkit.getPluginManager().disablePlugin(plotMePlugin); Bukkit.getPluginManager().disablePlugin(plotMePlugin);
PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8:&7 Conversion has finished"); sendMessage("Conversion has finished");
PlotMain.sendConsoleSenderMessage("&cAlthough the server may be functional in it's current state, it is recommended that you restart the server and remove PlotMe to finalize the installation. Please make careful note of any warning messages that may have showed up during conversion."); PlotMain.sendConsoleSenderMessage("&cAlthough the server may be functional in it's current state, it is recommended that you restart the server and remove PlotMe to finalize the installation. Please make careful note of any warning messages that may have showed up during conversion.");
}
catch (Exception e) {
e.printStackTrace();
}
} }
}, 20); }, 20);
} }

View File

@ -28,6 +28,7 @@ import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotComment; import com.intellectualcrafters.plot.object.PlotComment;
import com.intellectualcrafters.plot.object.PlotHomePosition; import com.intellectualcrafters.plot.object.PlotHomePosition;
import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.UUIDHandler; import com.intellectualcrafters.plot.util.UUIDHandler;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -93,7 +94,7 @@ public class SQLManager implements AbstractDB {
// //
// public void setTimout() { // public void setTimout() {
// runTask(new Runnable() { // TaskManager.runTask(new Runnable() {
// @Override // @Override
// public void run() { // public void run() {
// try { // try {
@ -117,7 +118,7 @@ public class SQLManager implements AbstractDB {
*/ */
@Override @Override
public void setOwner(final Plot plot, final UUID uuid) { public void setOwner(final Plot plot, final UUID uuid) {
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -298,7 +299,7 @@ public class SQLManager implements AbstractDB {
*/ */
@Override @Override
public void createPlot(final Plot plot) { public void createPlot(final Plot plot) {
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -320,7 +321,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public void createPlotAndSettings(final Plot plot) { public void createPlotAndSettings(final Plot plot) {
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -391,7 +392,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public void delete(final String world, final Plot plot) { public void delete(final String world, final Plot plot) {
PlotMain.removePlot(world, plot.id, false); PlotMain.removePlot(world, plot.id, false);
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -432,7 +433,7 @@ public class SQLManager implements AbstractDB {
*/ */
@Override @Override
public void createPlotSettings(final int id, final Plot plot) { public void createPlotSettings(final int id, final Plot plot) {
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -509,7 +510,6 @@ public class SQLManager implements AbstractDB {
if (PlotMain.config.contains("worlds")) { if (PlotMain.config.contains("worlds")) {
worlds = PlotMain.config.getConfigurationSection("worlds").getKeys(false); worlds = PlotMain.config.getConfigurationSection("worlds").getKeys(false);
} }
final HashMap<String, UUID> uuids = new HashMap<String, UUID>(); final HashMap<String, UUID> uuids = new HashMap<String, UUID>();
final HashMap<String, Integer> noExist = new HashMap<String, Integer>(); final HashMap<String, Integer> noExist = new HashMap<String, Integer>();
@ -658,16 +658,23 @@ public class SQLManager implements AbstractDB {
if (myflags == null) { if (myflags == null) {
flags_string = new String[]{}; flags_string = new String[]{};
} else { } else {
if (myflags.length() > 0) {
flags_string = myflags.split(","); flags_string = myflags.split(",");
} }
else {
flags_string = new String[]{};
}
}
final Set<Flag> flags = new HashSet<Flag>(); final Set<Flag> flags = new HashSet<Flag>();
boolean exception = false; boolean exception = false;
for (final String element : flags_string) { for (final String element : flags_string) {
if (element.contains(":")) { if (element.contains(":")) {
final String[] split = element.split(":"); final String[] split = element.split(":");
try { try {
System.out.print("NEW FLAG] "+element);
flags.add(new Flag(FlagManager.getFlag(split[0], true), split[1].replaceAll("\u00AF", ":").replaceAll("<EFBFBD>", ","))); flags.add(new Flag(FlagManager.getFlag(split[0], true), split[1].replaceAll("\u00AF", ":").replaceAll("<EFBFBD>", ",")));
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace();
exception = true; exception = true;
} }
} else { } else {
@ -678,7 +685,7 @@ public class SQLManager implements AbstractDB {
PlotMain.sendConsoleSenderMessage("&cPlot " + id + " had an invalid flag. A fix has been attempted."); PlotMain.sendConsoleSenderMessage("&cPlot " + id + " had an invalid flag. A fix has been attempted.");
setFlags(id, flags.toArray(new Flag[0])); setFlags(id, flags.toArray(new Flag[0]));
} }
FlagManager.setPlotFlags(plot, flags); plot.settings.flags = flags;
} else { } else {
PlotMain.sendConsoleSenderMessage("&cPLOT " + id + " in plot_settings does not exist. Please create the plot or remove this entry."); PlotMain.sendConsoleSenderMessage("&cPLOT " + id + " in plot_settings does not exist. Please create the plot or remove this entry.");
} }
@ -709,7 +716,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public void setMerged(final String world, final Plot plot, final boolean[] merged) { public void setMerged(final String world, final Plot plot, final boolean[] merged) {
plot.settings.setMerged(merged); plot.settings.setMerged(merged);
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -741,13 +748,14 @@ public class SQLManager implements AbstractDB {
flag_string.append(flag.getKey() + ":" + flag.getValue().replaceAll(":", "\u00AF").replaceAll(",", "\u00B4")); flag_string.append(flag.getKey() + ":" + flag.getValue().replaceAll(":", "\u00AF").replaceAll(",", "\u00B4"));
i++; i++;
} }
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
final PreparedStatement stmt = SQLManager.this.connection.prepareStatement("UPDATE `" + SQLManager.this.prefix + "plot_settings` SET `flags` = ? WHERE `plot_plot_id` = ?"); final PreparedStatement stmt = SQLManager.this.connection.prepareStatement("UPDATE `" + SQLManager.this.prefix + "plot_settings` SET `flags` = ? WHERE `plot_plot_id` = ?");
stmt.setString(1, flag_string.toString()); stmt.setString(1, flag_string.toString());
stmt.setInt(2, getId(world, plot.id)); stmt.setInt(2, getId(world, plot.id));
System.out.print(stmt.toString());
stmt.execute(); stmt.execute();
stmt.close(); stmt.close();
} catch (final SQLException e) { } catch (final SQLException e) {
@ -766,7 +774,7 @@ public class SQLManager implements AbstractDB {
} }
} }
final String flag_string = StringUtils.join(newflags, ","); final String flag_string = StringUtils.join(newflags, ",");
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -790,7 +798,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public void setAlias(final String world, final Plot plot, final String alias) { public void setAlias(final String world, final Plot plot, final String alias) {
plot.settings.setAlias(alias); plot.settings.setAlias(alias);
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -810,12 +818,8 @@ public class SQLManager implements AbstractDB {
} }
/** /**
* @param r * Purge all plots with the f ollowing database IDs
*/ */
private void runTask(final Runnable r) {
PlotMain.getMain().getServer().getScheduler().runTaskAsynchronously(PlotMain.getMain(), r);
}
public void purgeIds(final String world, final Set<Integer> uniqueIds) { public void purgeIds(final String world, final Set<Integer> uniqueIds) {
if (uniqueIds.size() > 0) { if (uniqueIds.size() > 0) {
try { try {
@ -885,7 +889,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public void setPosition(final String world, final Plot plot, final String position) { public void setPosition(final String world, final Plot plot, final String position) {
plot.settings.setPosition(PlotHomePosition.valueOf(position)); plot.settings.setPosition(PlotHomePosition.valueOf(position));
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -957,7 +961,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public void removeComment(final String world, final Plot plot, final PlotComment comment) { public void removeComment(final String world, final Plot plot, final PlotComment comment) {
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -1001,7 +1005,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public void setComment(final String world, final Plot plot, final PlotComment comment) { public void setComment(final String world, final Plot plot, final PlotComment comment) {
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -1027,7 +1031,7 @@ public class SQLManager implements AbstractDB {
*/ */
@Override @Override
public void removeHelper(final String world, final Plot plot, final OfflinePlayer player) { public void removeHelper(final String world, final Plot plot, final OfflinePlayer player) {
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -1050,7 +1054,7 @@ public class SQLManager implements AbstractDB {
*/ */
@Override @Override
public void removeTrusted(final String world, final Plot plot, final OfflinePlayer player) { public void removeTrusted(final String world, final Plot plot, final OfflinePlayer player) {
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -1073,7 +1077,7 @@ public class SQLManager implements AbstractDB {
*/ */
@Override @Override
public void setHelper(final String world, final Plot plot, final OfflinePlayer player) { public void setHelper(final String world, final Plot plot, final OfflinePlayer player) {
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -1091,7 +1095,7 @@ public class SQLManager implements AbstractDB {
} }
public void setHelper(final int id, final UUID uuid) { public void setHelper(final int id, final UUID uuid) {
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -1114,7 +1118,7 @@ public class SQLManager implements AbstractDB {
*/ */
@Override @Override
public void setTrusted(final String world, final Plot plot, final OfflinePlayer player) { public void setTrusted(final String world, final Plot plot, final OfflinePlayer player) {
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -1137,7 +1141,7 @@ public class SQLManager implements AbstractDB {
*/ */
@Override @Override
public void removeDenied(final String world, final Plot plot, final OfflinePlayer player) { public void removeDenied(final String world, final Plot plot, final OfflinePlayer player) {
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -1160,7 +1164,7 @@ public class SQLManager implements AbstractDB {
*/ */
@Override @Override
public void setDenied(final String world, final Plot plot, final OfflinePlayer player) { public void setDenied(final String world, final Plot plot, final OfflinePlayer player) {
runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {

View File

@ -0,0 +1,64 @@
package com.intellectualcrafters.plot.util;
import java.io.File;
import java.util.ArrayList;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.object.ChunkLoc;
import com.intellectualcrafters.plot.object.Plot;
public class ChunkManager {
public static ArrayList<ChunkLoc> getChunkChunks(World world) {
File[] regionFiles = new File(new File(".").getAbsolutePath() + File.separator + world.getName() + File.separator + "region").listFiles();
ArrayList<ChunkLoc> chunks = new ArrayList<>();
for (File file : regionFiles) {
String name = file.getName();
if (name.endsWith("mca")) {
String[] split = name.split("\\.");
try {
chunks.add(new ChunkLoc(Integer.parseInt(split[1]), Integer.parseInt(split[2])));
} catch (Exception e) { }
}
}
return chunks;
}
public static void deleteRegionFile(final String world, final ChunkLoc loc) {
TaskManager.runTask(new Runnable() {
@Override
public void run() {
String directory = new File(".").getAbsolutePath() + File.separator + world + File.separator + "region" + File.separator + "r." + loc.x + "." + loc.z + ".mca";
File file = new File(directory);
PlotMain.sendConsoleSenderMessage("&6 - Deleted region "+file.getName()+" (max 256 chunks)");
if (file.exists()) {
file.delete();
}
}
});
}
public static boolean hasPlot(World world, Chunk chunk) {
int x1 = chunk.getX() << 4;
int z1 = chunk.getZ() << 4;
int x2 = x1 + 15;
int z2 = z1 + 15;
Location bot = new Location(world, x1, 0, z1);
Plot plot;
plot = PlotHelper.getCurrentPlot(bot);
if (plot != null && plot.owner != null) {
return true;
}
Location top = new Location(world, x2, 0, z2);
plot = PlotHelper.getCurrentPlot(top);
if (plot != null && plot.owner != null) {
return true;
}
return false;
}
}

View File

@ -0,0 +1,153 @@
package com.intellectualcrafters.plot.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.commands.Auto;
import com.intellectualcrafters.plot.commands.Schematic;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.events.PlotDeleteEvent;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotManager;
public class ExpireManager {
private static long timestamp = 0;
public static ConcurrentHashMap<String, ArrayList<Plot>> expiredPlots = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, Boolean> updatingPlots = new ConcurrentHashMap<>();
public static int task;
public static void updateExpired(final String world) {
updatingPlots.put(world, true);
long now = System.currentTimeMillis();
if (now > timestamp) {
timestamp = now + 86400000;
TaskManager.runTask(new Runnable() {
@Override
public void run() {
ArrayList<Plot> plots = getOldPlots(world);
expiredPlots.put(world, plots);
updatingPlots.put(world, false);
}
});
}
else {
updatingPlots.put(world, false);
}
}
public static void runTask() {
ExpireManager.task = Bukkit.getScheduler().scheduleSyncRepeatingTask(PlotMain.getMain(), new Runnable() {
@Override
public void run() {
System.out.print("RUNNING TASK");
for (String world : PlotMain.getPlotWorldsString()) {
System.out.print(" - check world");
if (!ExpireManager.updatingPlots.contains(world)) {
ExpireManager.updatingPlots.put(world, false);
}
Boolean updating = ExpireManager.updatingPlots.get(world);
if (updating) {
System.out.print(" - ERR UPDATING");
return;
}
ArrayList<Plot> plots = expiredPlots.get(world);
if (plots == null || plots.size() == 0) {
updateExpired(world);
return;
}
Plot plot = plots.get(0);
if (plot.owner != null) {
if (UUIDHandler.uuidWrapper.getPlayer(plot.owner) != null) {
expiredPlots.get(world).remove(0);
return;
}
}
if (!isExpired(plot.owner)) {
expiredPlots.get(world).remove(0);
return;
}
final PlotDeleteEvent event = new PlotDeleteEvent(world, plot.id);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
event.setCancelled(true);
return;
}
final World worldobj = Bukkit.getWorld(world);
final PlotManager manager = PlotMain.getPlotManager(world);
manager.clearPlot(worldobj, plot, false);
PlotHelper.clear(worldobj, plot, true);
PlotHelper.removeSign(worldobj, plot);
DBFunc.delete(world, plot);
PlotMain.removePlot(world, plot.id, true);
expiredPlots.get(world).remove(0);
PlotMain.sendConsoleSenderMessage("&cDeleted expired plot: " + plot.id);
if ((Math.abs(plot.id.x) < Math.abs(Auto.lastPlot.x)) && (Math.abs(plot.id.y) < Math.abs(Auto.lastPlot.y))) {
Auto.lastPlot = plot.id;
}
return;
}
}
}, 1, 20);
}
public static boolean isExpired(UUID uuid) {
OfflinePlayer op = UUIDHandler.uuidWrapper.getOfflinePlayer(uuid);
if (!op.hasPlayedBefore()) {
return true;
}
long last = op.getLastPlayed();
long compared = System.currentTimeMillis() - last;
if (compared >= 86400000 * Settings.AUTO_CLEAR_DAYS) {
return true;
}
return false;
}
public static ArrayList<Plot> getOldPlots(String world) {
final Collection<Plot> plots = PlotMain.getPlots(world).values();
final ArrayList<Plot> toRemove = new ArrayList<>();
Set<UUID> remove = new HashSet<>();
Set<UUID> keep = new HashSet<>();
for (Plot plot : plots) {
UUID uuid = plot.owner;
if (uuid == null || remove.contains(uuid)) {
toRemove.add(plot);
continue;
}
if (keep.contains(uuid)) {
continue;
}
OfflinePlayer op = UUIDHandler.uuidWrapper.getOfflinePlayer(uuid);
if (!op.hasPlayedBefore()) {
toRemove.add(plot);
PlotMain.removePlot(plot.world, plot.id, true);
continue;
}
long last = op.getLastPlayed();
long compared = System.currentTimeMillis() - last;
if (compared >= 86400000 * Settings.AUTO_CLEAR_DAYS) {
toRemove.add(plot);
remove.add(uuid);
}
keep.add(uuid);
}
return toRemove;
}
}

View File

@ -0,0 +1,9 @@
package com.intellectualcrafters.plot.util;
import com.intellectualcrafters.plot.PlotMain;
public class TaskManager {
public static void runTask(final Runnable r) {
PlotMain.getMain().getServer().getScheduler().runTaskAsynchronously(PlotMain.getMain(), r);
}
}

View File

@ -0,0 +1,284 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.unused;
import com.google.common.base.Charsets;
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.generator.HybridGen;
import com.intellectualcrafters.plot.object.PlotHomePosition;
import com.intellectualcrafters.plot.object.PlotId;
import com.worldcretornica.plotme.PlayerList;
import com.worldcretornica.plotme.Plot;
import com.worldcretornica.plotme.PlotManager;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
/**
* Created 2014-08-17 for PlotSquared
*
* @author Citymonstret
* @author Empire92
*/
public class PlotMeConverter_0_13 {
/**
* PlotMain Object
*/
private final PlotMain plugin;
/**
* Constructor
*
* @param plugin Plugin Used to run the converter
*/
public PlotMeConverter_0_13(final PlotMain plugin) {
this.plugin = plugin;
}
private void sendMessage(final String message) {
PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8: " + message);
}
public void runAsync() throws Exception {
// We have to make it wait a couple of seconds
Bukkit.getScheduler().runTaskLater(this.plugin, new Runnable() {
@Override
public void run() {
sendMessage("&7Conversion has started");
sendMessage("&7Caching playerdata...");
final ArrayList<com.intellectualcrafters.plot.object.Plot> createdPlots = new ArrayList<>();
// Online Mode
final boolean online = Bukkit.getServer().getOnlineMode() && !Settings.OFFLINE_MODE;
// PlotMe Plugin
final Plugin plotMePlugin = Bukkit.getPluginManager().getPlugin("PlotMe");
// PlotMe Configuration
final FileConfiguration plotConfig = plotMePlugin.getConfig();
// Plot Worlds
final Set<String> worlds = new HashSet<>();
// Loop through the worlds
int duplicate;
HashMap<String, Plot> plots;
for (World world : Bukkit.getWorlds()) {
duplicate = 0;
plots = PlotManager.getPlots(world);
if (plots != null) {
worlds.add(world.getName());
sendMessage("&7Converting configuration for world '" + world.getName() + "'...");
try {
final Integer pathwidth = plotConfig.getInt("worlds." + world.getName() + ".PathWidth"); //
PlotMain.config.set("worlds." + world.getName() + ".road.width", pathwidth);
final Integer plotsize = plotConfig.getInt("worlds." + world.getName() + ".PlotSize"); //
PlotMain.config.set("worlds." + world.getName() + ".plot.size", plotsize);
final String wallblock = plotConfig.getString("worlds." + world.getName() + ".WallBlockId"); //
PlotMain.config.set("worlds." + world.getName() + ".wall.block", wallblock);
final String floor = plotConfig.getString("worlds." + world.getName() + ".PlotFloorBlockId"); //
PlotMain.config.set("worlds." + world.getName() + ".plot.floor", Arrays.asList(floor));
final String filling = plotConfig.getString("worlds." + world.getName() + ".PlotFillingBlockId"); //
PlotMain.config.set("worlds." + world.getName() + ".plot.filling", Arrays.asList(filling));
final String road = plotConfig.getString("worlds." + world.getName() + ".RoadMainBlockId");
PlotMain.config.set("worlds." + world.getName() + ".road.block", road);
final String road_stripe = plotConfig.getString("worlds." + world.getName() + ".RoadStripeBlockId");
PlotMain.config.set("worlds." + world.getName() + ".road.stripes", road_stripe);
final Integer height = plotConfig.getInt("worlds." + world.getName() + ".RoadHeight"); //
PlotMain.config.set("worlds." + world.getName() + ".road.height", height);
final Boolean auto_link = plotConfig.getBoolean("worlds." + world.getName() + ".AutoLinkPlots"); //
PlotMain.config.set("worlds." + world.getName() + ".plot.auto_merge", auto_link);
} catch (final Exception e) {
sendMessage("&c-- &lFailed to save configuration for world '" + world.getName() + "'\nThis will need to be done using the setup command, or manually");
}
sendMessage("&7Processing '" + plots.size() + "' plots for world '" + world.getName() + "'");
ArrayList<UUID> psAdded, psTrusted, psDenied;
for (final Plot plot : plots.values()) {
psAdded = new ArrayList<>();
psTrusted = new ArrayList<>();
psDenied = new ArrayList<>();
if (world == null) {
world = Bukkit.getWorld("world");
}
try {
if (online) {
PlayerList denied;
PlayerList added;
final Field fAdded = plot.getClass().getDeclaredField("allowed");
final Field fDenied = plot.getClass().getDeclaredField("denied");
fAdded.setAccessible(true);
fDenied.setAccessible(true);
added = (PlayerList) fAdded.get(plot);
denied = (PlayerList) fDenied.get(plot);
for (final Map.Entry<String, UUID> set : added.getAllPlayers().entrySet()) {
if ((set.getValue() != null) || set.getKey().equals("*")) {
if (set.getKey().equalsIgnoreCase("*") || set.getValue().toString().equals("*")) {
psAdded.add(DBFunc.everyone);
continue;
}
}
if (set.getValue() != null) {
psAdded.add(set.getValue());
}
}
for (final Map.Entry<String, UUID> set : denied.getAllPlayers().entrySet()) {
if ((set.getValue() != null) || set.getKey().equals("*")) {
if (set.getKey().equals("*") || set.getValue().toString().equals("*")) {
psDenied.add(DBFunc.everyone);
continue;
}
}
if (set.getValue() != null) {
psDenied.add(set.getValue());
}
}
} else {
for (final String user : plot.getAllowed().split(",")) {
if (user.equals("*")) {
psAdded.add(DBFunc.everyone);
} else {
final UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + user).getBytes(Charsets.UTF_8));
psAdded.add(uuid);
}
}
try {
for (final String user : plot.getDenied().split(",")) {
if (user.equals("*")) {
psDenied.add(DBFunc.everyone);
} else {
final UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + user).getBytes(Charsets.UTF_8));
psDenied.add(uuid);
}
}
} catch (final Throwable e) {
// Okay, this is evil.
}
}
} catch (final Throwable e) {
e.printStackTrace();
}
final PlotId id = new PlotId(Integer.parseInt(plot.id.split(";")[0]), Integer.parseInt(plot.id.split(";")[1]));
com.intellectualcrafters.plot.object.Plot pl;
if (online) {
pl = new com.intellectualcrafters.plot.object.Plot(id, plot.getOwnerId(), psAdded, psTrusted, psDenied,
"", PlotHomePosition.DEFAULT, null, world.getName(), new boolean[]{false, false, false, false});
} else {
final String owner = plot.getOwner();
pl = new com.intellectualcrafters.plot.object.Plot(id, UUID.nameUUIDFromBytes(("OfflinePlayer:" + owner).getBytes(Charsets.UTF_8)), psAdded, psTrusted, psDenied,
"", PlotHomePosition.DEFAULT, null, world.getName(), new boolean[]{false, false, false, false});
}
if (pl != null) {
if (!PlotMain.getPlots(world).containsKey(id)) {
createdPlots.add(pl);
} else {
duplicate++;
}
}
}
if (duplicate > 0) {
PlotMain.sendConsoleSenderMessage("&c[WARNING] Found " + duplicate + " duplicate plots already in DB for world: '" + world.getName() + "'. Have you run the converter already?");
}
}
}
PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8: &7Creating plot DB");
DBFunc.createPlots(createdPlots);
PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8: &7Creating settings/helpers DB");
// TODO createPlot doesn't add denied users
DBFunc.createAllSettingsAndHelpers(createdPlots);
PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8:&7 Saving configuration...");
try {
PlotMain.config.save(PlotMain.configFile);
} catch (final IOException e) {
PlotMain.sendConsoleSenderMessage(" - &cFailed to save configuration.");
}
boolean MV = false;
boolean MW = false;
if ((Bukkit.getPluginManager().getPlugin("Multiverse-Core") != null) && Bukkit.getPluginManager().getPlugin("Multiverse-Core").isEnabled()) {
MV = true;
} else if ((Bukkit.getPluginManager().getPlugin("MultiWorld") != null) && Bukkit.getPluginManager().getPlugin("MultiWorld").isEnabled()) {
MW = true;
}
for (final String worldname : worlds) {
final World world = Bukkit.getWorld(worldname);
PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8:&7 Reloading generator for world: '" + worldname + "'...");
PlotMain.removePlotWorld(worldname);
if (MV) {
// unload
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mv unload " + worldname);
try {
Thread.sleep(1000);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
}
// load
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mv import " + worldname + " normal -g PlotSquared");
} else if (MW) {
// unload
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mw unload " + worldname);
try {
Thread.sleep(1000);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
}
// load
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mw create " + worldname + " plugin:PlotSquared");
} else {
Bukkit.getServer().unloadWorld(world, true);
final World myworld = WorldCreator.name(worldname).generator(new HybridGen(worldname)).createWorld();
myworld.save();
}
}
PlotMain.setAllPlotsRaw(DBFunc.getPlots());
PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8:&7 Disabling PlotMe...");
Bukkit.getPluginManager().disablePlugin(plotMePlugin);
PlotMain.sendConsoleSenderMessage("&3PlotMe&8->&3PlotSquared&8:&7 Conversion has finished");
PlotMain.sendConsoleSenderMessage("&cAlthough the server may be functional in it's current state, it is recommended that you restart the server and remove PlotMe to finalize the installation. Please make careful note of any warning messages that may have showed up during conversion.");
}
}, 20);
}
}