Fixed exportall command (spigot would attempt to restart because of lag)

This commit is contained in:
boy0001 2014-10-25 16:34:06 +11:00
parent 760921a43b
commit ed3003a2f6
3 changed files with 84 additions and 34 deletions

View File

@ -68,8 +68,7 @@ public enum C {
/* /*
* Schematic Stuff * Schematic Stuff
*/ */
SCHEMATIC_MISSING_ARG("&cYou need to specify an argument. Possible values: &6test <name>"), SCHEMATIC_MISSING_ARG("&cYou need to specify an argument. Possible values: &6test <name>&7 , &6save&7 , &6paste &7, &6exportall"),
SCHEMATIC_MISSING_ARG2("&cYou need to specify an argument. Possible values: &6save"),
SCHEMATIC_INVALID("&cThat is not a valid schematic. Reason: &c%s"), SCHEMATIC_INVALID("&cThat is not a valid schematic. Reason: &c%s"),
SCHEMATIC_VALID("&cThat is a valid schematic"), SCHEMATIC_VALID("&cThat is a valid schematic"),
SCHEMATIC_PASTE_FAILED("&cFailed to paste the schematic"), SCHEMATIC_PASTE_FAILED("&cFailed to paste the schematic"),

View File

@ -229,8 +229,11 @@ public class SchematicHandler {
// loading chunks // loading chunks
final Location pos1 = PlotHelper.getPlotBottomLoc(world, id).add(1, 0, 1); final Location pos1 = PlotHelper.getPlotBottomLoc(world, id).add(1, 0, 1);
final Location pos2 = PlotHelper.getPlotTopLoc(world, id); final Location pos2 = PlotHelper.getPlotTopLoc(world, id);
for (int i = (pos1.getBlockX() / 16) * 16; i < (16 + ((pos2.getBlockX() / 16) * 16)); i += 16) { int i = 0;
for (int j = (pos1.getBlockZ() / 16) * 16; j < (16 + ((pos2.getBlockZ() / 16) * 16)); j += 16) { int j = 0;
try {
for (i = (pos1.getBlockX() / 16) * 16; i < (16 + ((pos2.getBlockX() / 16) * 16)); i += 16) {
for (j = (pos1.getBlockZ() / 16) * 16; j < (16 + ((pos2.getBlockZ() / 16) * 16)); j += 16) {
Chunk chunk = world.getChunkAt(i, j); Chunk chunk = world.getChunkAt(i, j);
boolean result = chunk.load(false); boolean result = chunk.load(false);
if (!result) { if (!result) {
@ -241,6 +244,11 @@ public class SchematicHandler {
} }
} }
} }
}
catch (Exception e) {
PlotMain.sendConsoleSenderMessage("&7 - Cannot save: corrupt chunk at "+(i/16)+", "+(j/16));
return null;
}
int width = pos2.getBlockX()-pos1.getBlockX(); int width = pos2.getBlockX()-pos1.getBlockX();
int height = 256; int height = 256;
int length = pos2.getBlockZ()-pos1.getBlockZ(); int length = pos2.getBlockZ()-pos1.getBlockZ();

View File

@ -1,11 +1,13 @@
package com.intellectualcrafters.plot.commands; package com.intellectualcrafters.plot.commands;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import com.intellectualcrafters.jnbt.CompoundTag; import com.intellectualcrafters.jnbt.CompoundTag;
import com.intellectualcrafters.plot.C; import com.intellectualcrafters.plot.C;
@ -22,11 +24,19 @@ import com.sun.org.apache.xerces.internal.impl.xs.identity.ValueStore;
public class Schematic extends SubCommand { public class Schematic extends SubCommand {
public Schematic() { public Schematic() {
super("schematic", "plots.admin", "Schematic Command", "schematic {arg}", "sch", CommandCategory.ACTIONS, true); super("schematic", "plots.admin", "Schematic Command", "schematic {arg}", "sch", CommandCategory.ACTIONS, false);
// TODO command to fetch schematic from worldedit directory // TODO command to fetch schematic from worldedit directory
} }
private int counter = 0;
private boolean running = false;
private Plot[] plots;
private int task;
@Override @Override
public boolean execute(final Player plr, String... args) { public boolean execute(final Player plr, String... args) {
if (args.length < 1) { if (args.length < 1) {
@ -38,6 +48,10 @@ public class Schematic extends SubCommand {
SchematicHandler.Schematic schematic; SchematicHandler.Schematic schematic;
switch (arg) { switch (arg) {
case "paste": case "paste":
if (plr==null) {
PlotMain.sendConsoleSenderMessage(C.IS_CONSOLE);
return false;
}
if (!PlotMain.hasPermission(plr, "plots.schematic.save")) { if (!PlotMain.hasPermission(plr, "plots.schematic.save")) {
PlayerFunctions.sendMessage(plr, C.NO_PERMISSION, "plots.schematic.paste"); PlayerFunctions.sendMessage(plr, C.NO_PERMISSION, "plots.schematic.paste");
return false; return false;
@ -51,8 +65,8 @@ public class Schematic extends SubCommand {
break; break;
} }
file = args[1]; file = args[1];
schematic = new SchematicHandler().getSchematic(file); schematic = SchematicHandler.getSchematic(file);
boolean s = new SchematicHandler().paste(plr.getLocation(), schematic, PlayerFunctions.getCurrentPlot(plr)); boolean s = SchematicHandler.paste(plr.getLocation(), schematic, PlayerFunctions.getCurrentPlot(plr));
if (s) { if (s) {
sendMessage(plr, C.SCHEMATIC_PASTE_SUCCESS); sendMessage(plr, C.SCHEMATIC_PASTE_SUCCESS);
} }
@ -61,6 +75,10 @@ public class Schematic extends SubCommand {
} }
break; break;
case "test": case "test":
if (plr==null) {
PlotMain.sendConsoleSenderMessage(C.IS_CONSOLE);
return false;
}
if (!PlotMain.hasPermission(plr, "plots.schematic.save")) { if (!PlotMain.hasPermission(plr, "plots.schematic.save")) {
PlayerFunctions.sendMessage(plr, C.NO_PERMISSION, "plots.schematic.test"); PlayerFunctions.sendMessage(plr, C.NO_PERMISSION, "plots.schematic.test");
return false; return false;
@ -70,7 +88,7 @@ public class Schematic extends SubCommand {
break; break;
} }
file = args[1]; file = args[1];
schematic = new SchematicHandler().getSchematic(file); schematic = SchematicHandler.getSchematic(file);
if (schematic == null) { if (schematic == null) {
sendMessage(plr, C.SCHEMATIC_INVALID, "non-existent"); sendMessage(plr, C.SCHEMATIC_INVALID, "non-existent");
break; break;
@ -95,39 +113,65 @@ public class Schematic extends SubCommand {
return false; return false;
} }
if (args.length!=2) { if (args.length!=2) {
PlayerFunctions.sendMessage(plr, "&cNeed world arg. Use &7/plots schem exportall <world>"); PlayerFunctions.sendMessage(plr, "&cNeed world arg. Use &7/plots sch exportall <world>");
return false; return false;
} }
HashMap<PlotId, Plot> plotmap = PlotMain.getPlots(args[1]); HashMap<PlotId, Plot> plotmap = PlotMain.getPlots(args[1]);
if (plotmap==null || plotmap.size()==0) { if (plotmap==null || plotmap.size()==0) {
PlayerFunctions.sendMessage(plr, "&cInvalid world. Use &7/plots schem exportall <world>"); PlayerFunctions.sendMessage(plr, "&cInvalid world. Use &7/plots sch exportall <world>");
return false;
}
if (running) {
PlayerFunctions.sendMessage(plr, "&cTask is already running.");
return false; return false;
} }
PlotMain.sendConsoleSenderMessage("&3PlotSquared&8->&3Schemaitc&8: &7Mass export has started. This may take a while."); PlotMain.sendConsoleSenderMessage("&3PlotSquared&8->&3Schemaitc&8: &7Mass export has started. This may take a while.");
PlotMain.sendConsoleSenderMessage("&3PlotSquared&8->&3Schemaitc&8: &7Found &c"+plotmap.size()+"&7 plots..."); PlotMain.sendConsoleSenderMessage("&3PlotSquared&8->&3Schemaitc&8: &7Found &c"+plotmap.size()+"&7 plots...");
final Collection<Plot> plots = plotmap.values(); final World worldObj = Bukkit.getWorld(args[1]);
final World worldname = Bukkit.getWorld(args[1]); final String worldname = Bukkit.getWorld(args[1]).getName();
Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getServer().getPluginManager().getPlugin("PlotSquared"), new Runnable() {
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("PlotSquared");
this.plots = plotmap.values().toArray(new Plot[0]);
this.running = true;
this.counter = 0;
task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
if (counter>=plots.length) {
for (Plot plot:plots) { PlotMain.sendConsoleSenderMessage("&3PlotSquared&8->&3Schemaitc&8: &aFinished!");
CompoundTag schematic = SchematicHandler.getCompoundTag(worldname, plot.id); Bukkit.getScheduler().cancelTask(task);
if (schematic==null) { return;
PlayerFunctions.sendMessage(plr, "&7 - Skipped plot &c"+plot.id);
return;
}
boolean result = SchematicHandler.save(schematic, Settings.Web.PATH+"/"+plot.id.x+"-"+plot.id.y+"-"+worldname+".schematic");
if (!result) {
PlayerFunctions.sendMessage(plr, "&7 - Failed to save &c"+plot.id);
return;
}
PlayerFunctions.sendMessage(plr, "&7 - &aExport success: "+plot.id);
} }
final Plot plot = plots[counter];
final CompoundTag sch = SchematicHandler.getCompoundTag(worldObj, plot.id);
if (sch==null) {
PlayerFunctions.sendMessage(plr, "&7 - Skipped plot &c"+plot.id);
}
else {
Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getServer().getPluginManager().getPlugin("PlotSquared"), new Runnable() {
@Override
public void run() {
counter++;
PlayerFunctions.sendMessage(plr, "&6ID: "+plot.id);
boolean result = SchematicHandler.save(sch, Settings.Web.PATH+"/"+plot.id.x+","+plot.id.y+","+worldname+".schematic");
if (!result) {
PlayerFunctions.sendMessage(plr, "&7 - Failed to save &c"+plot.id);
}
else {
PlayerFunctions.sendMessage(plr, "&7 - &aExport success: "+plot.id);
}
}
});
}
counter++;
} }
}); }, 20, 20);
break;
case "export": case "export":
case "save": case "save":
if (!PlotMain.hasPermission(plr, "plots.schematic.save")) { if (!PlotMain.hasPermission(plr, "plots.schematic.save")) {
@ -156,18 +200,18 @@ public class Schematic extends SubCommand {
String[] split = args[2].split(";"); String[] split = args[2].split(";");
i = new PlotId(Integer.parseInt(split[0]),Integer.parseInt(split[1])); i = new PlotId(Integer.parseInt(split[0]),Integer.parseInt(split[1]));
if (PlotMain.getPlots(world)==null || PlotMain.getPlots(world).get(i) == null) { if (PlotMain.getPlots(world)==null || PlotMain.getPlots(world).get(i) == null) {
PlayerFunctions.sendMessage(plr, "&cInvalid world or id. Use &7/plots schem save <world> <id>"); PlayerFunctions.sendMessage(plr, "&cInvalid world or id. Use &7/plots sch save <world> <id>");
return false; return false;
} }
} }
catch (Exception e) { catch (Exception e) {
PlayerFunctions.sendMessage(plr, "&cInvalid world or id. Use &7/plots schem save <world> <id>"); PlayerFunctions.sendMessage(plr, "&cInvalid world or id. Use &7/plots sch save <world> <id>");
return false; return false;
} }
} }
else { else {
PlayerFunctions.sendMessage(plr, "&cInvalid world or id. Use &7/plots schem save <world> <id>"); PlayerFunctions.sendMessage(plr, "&cInvalid world or id. Use &7/plots sch save <world> <id>");
return false; return false;
} }
} }
@ -182,7 +226,7 @@ public class Schematic extends SubCommand {
PlayerFunctions.sendMessage(plr, "&cInvalid plot"); PlayerFunctions.sendMessage(plr, "&cInvalid plot");
return; return;
} }
boolean result = SchematicHandler.save(schematic, Settings.Web.PATH+"/"+i.x+"-"+i.y+"-"+world+".schematic"); boolean result = SchematicHandler.save(schematic, Settings.Web.PATH+"/"+i.x+","+i.y+","+world+".schematic");
if (!result) { if (!result) {
PlayerFunctions.sendMessage(plr, "&cFailed to save schematic"); PlayerFunctions.sendMessage(plr, "&cFailed to save schematic");
@ -191,7 +235,6 @@ public class Schematic extends SubCommand {
PlayerFunctions.sendMessage(plr, "&aSuccessfully saved schematic!"); PlayerFunctions.sendMessage(plr, "&aSuccessfully saved schematic!");
} }
}); });
break; break;
default: default:
sendMessage(plr, C.SCHEMATIC_MISSING_ARG); sendMessage(plr, C.SCHEMATIC_MISSING_ARG);