schematic

This commit is contained in:
boy0001 2015-02-22 22:30:46 +11:00
parent d62841de3e
commit 79a01e722f
5 changed files with 91 additions and 65 deletions

View File

@ -23,23 +23,21 @@ package com.intellectualcrafters.plot.commands;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.plugin.Plugin;
import com.intellectualcrafters.jnbt.CompoundTag; import com.intellectualcrafters.jnbt.CompoundTag;
import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.PlotSquared;
import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.object.Location;
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.PlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.BlockManager;
import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualcrafters.plot.util.SchematicHandler; import com.intellectualcrafters.plot.util.SchematicHandler;
import com.intellectualcrafters.plot.util.SchematicHandler.DataCollection; import com.intellectualcrafters.plot.util.SchematicHandler.DataCollection;
import com.intellectualcrafters.plot.util.SchematicHandler.Dimension; import com.intellectualcrafters.plot.util.SchematicHandler.Dimension;
import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.bukkit.BukkitPlayerFunctions; import com.intellectualcrafters.plot.util.bukkit.BukkitPlayerFunctions;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
@ -64,9 +62,9 @@ public class Schematic extends SubCommand {
final String file; final String file;
final SchematicHandler.Schematic schematic; final SchematicHandler.Schematic schematic;
switch (arg) { switch (arg) {
case "paste": case "paste": {
if (plr == null) { if (plr == null) {
PlotSquared.log(C.IS_CONSOLE); PlotSquared.log(C.IS_CONSOLE.s());
return false; return false;
} }
if (!Permissions.hasPermission(plr, "plots.schematic.paste")) { if (!Permissions.hasPermission(plr, "plots.schematic.paste")) {
@ -77,7 +75,9 @@ public class Schematic extends SubCommand {
sendMessage(plr, C.SCHEMATIC_MISSING_ARG); sendMessage(plr, C.SCHEMATIC_MISSING_ARG);
break; break;
} }
if (!BukkitPlayerFunctions.isInPlot(plr)) { final Location loc = plr.getLocation();
Plot plot = MainUtil.getPlot(loc);
if (plot == null) {
sendMessage(plr, C.NOT_IN_PLOT); sendMessage(plr, C.NOT_IN_PLOT);
break; break;
} }
@ -88,7 +88,7 @@ public class Schematic extends SubCommand {
final String file2 = args[1]; final String file2 = args[1];
this.running = true; this.running = true;
this.counter = 0; this.counter = 0;
Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getServer().getPluginManager().getPlugin("PlotSquared"), new Runnable() { TaskManager.runTaskAsync(new Runnable() {
@Override @Override
public void run() { public void run() {
final SchematicHandler.Schematic schematic = SchematicHandler.getSchematic(file2); final SchematicHandler.Schematic schematic = SchematicHandler.getSchematic(file2);
@ -101,8 +101,8 @@ public class Schematic extends SubCommand {
final int z; final int z;
final Plot plot2 = MainUtil.getPlot(loc); final Plot plot2 = MainUtil.getPlot(loc);
final Dimension dem = schematic.getSchematicDimension(); final Dimension dem = schematic.getSchematicDimension();
final Location bot = MainUtil.getPlotBottomLoc(plr.getWorld(), plot2.id).add(1, 0, 1); final Location bot = MainUtil.getPlotBottomLoc(loc.getWorld(), plot2.id).add(1, 0, 1);
final int length2 = MainUtil.getPlotWidth(plr.getWorld(), plot2.id); final int length2 = MainUtil.getPlotWidth(loc.getWorld(), plot2.id);
if ((dem.getX() > length2) || (dem.getZ() > length2)) { if ((dem.getX() > length2) || (dem.getZ() > length2)) {
sendMessage(plr, C.SCHEMATIC_INVALID, String.format("Wrong size (x: %s, z: %d) vs %d ", dem.getX(), dem.getZ(), length2)); sendMessage(plr, C.SCHEMATIC_INVALID, String.format("Wrong size (x: %s, z: %d) vs %d ", dem.getX(), dem.getZ(), length2));
Schematic.this.running = false; Schematic.this.running = false;
@ -110,20 +110,19 @@ public class Schematic extends SubCommand {
} }
if ((dem.getX() != length2) || (dem.getZ() != length2)) { if ((dem.getX() != length2) || (dem.getZ() != length2)) {
final Location loc = plr.getLocation(); final Location loc = plr.getLocation();
x = Math.min(length2 - dem.getX(), loc.getBlockX() - bot.getBlockX()); x = Math.min(length2 - dem.getX(), loc.getX() - bot.getX());
z = Math.min(length2 - dem.getZ(), loc.getBlockZ() - bot.getBlockZ()); z = Math.min(length2 - dem.getZ(), loc.getZ() - bot.getZ());
} else { } else {
x = 0; x = 0;
z = 0; z = 0;
} }
final World w = plot2.getWorld();
final DataCollection[] b = schematic.getBlockCollection(); final DataCollection[] b = schematic.getBlockCollection();
final int sy = w.getHighestBlockYAt(bot.getBlockX(), bot.getBlockZ()); final int sy = BlockManager.manager.getHeighestBlock(bot);
final Location l1 = bot.add(0, sy - 1, 0); final Location l1 = bot.add(0, sy - 1, 0);
final int WIDTH = schematic.getSchematicDimension().getX(); final int WIDTH = schematic.getSchematicDimension().getX();
final int LENGTH = schematic.getSchematicDimension().getZ(); final int LENGTH = schematic.getSchematicDimension().getZ();
final int blen = b.length - 1; final int blen = b.length - 1;
Schematic.this.task = Bukkit.getScheduler().scheduleSyncRepeatingTask(PlotSquared.getMain(), new Runnable() { Schematic.this.task = TaskManager.runTaskRepeat(new Runnable() {
@Override @Override
public void run() { public void run() {
boolean result = false; boolean result = false;
@ -133,21 +132,22 @@ public class Schematic extends SubCommand {
sendMessage(plr, C.SCHEMATIC_PASTE_SUCCESS); sendMessage(plr, C.SCHEMATIC_PASTE_SUCCESS);
MainUtil.update(plr.getLocation()); MainUtil.update(plr.getLocation());
Schematic.this.running = false; Schematic.this.running = false;
Bukkit.getScheduler().cancelTask(Schematic.this.task); PlotSquared.TASK.cancelTask(Schematic.this.task);
return; return;
} }
final int end = Math.min(start + 5000, blen); final int end = Math.min(start + 5000, blen);
result = SchematicHandler.pastePart(w, b, l1, x, z, start, end, WIDTH, LENGTH); result = SchematicHandler.pastePart(loc.getWorld(), b, l1, x, z, start, end, WIDTH, LENGTH);
Schematic.this.counter++; Schematic.this.counter++;
} }
} }
}, 1, 1); }, 1);
} }
}); });
break; break;
case "test": }
case "test": {
if (plr == null) { if (plr == null) {
PlotSquared.log(C.IS_CONSOLE); PlotSquared.log(C.IS_CONSOLE.s());
return false; return false;
} }
if (!Permissions.hasPermission(plr, "plots.schematic.test")) { if (!Permissions.hasPermission(plr, "plots.schematic.test")) {
@ -164,18 +164,20 @@ public class Schematic extends SubCommand {
sendMessage(plr, C.SCHEMATIC_INVALID, "non-existent"); sendMessage(plr, C.SCHEMATIC_INVALID, "non-existent");
break; break;
} }
Location loc = plr.getLocation();
final int l1 = schematic.getSchematicDimension().getX(); final int l1 = schematic.getSchematicDimension().getX();
final int l2 = schematic.getSchematicDimension().getZ(); final int l2 = schematic.getSchematicDimension().getZ();
final Plot plot = MainUtil.getPlot(loc); final Plot plot = MainUtil.getPlot(loc);
final int length = MainUtil.getPlotWidth(plr.getWorld(), plot.id); final int length = MainUtil.getPlotWidth(loc.getWorld(), plot.id);
if ((l1 < length) || (l2 < length)) { if ((l1 < length) || (l2 < length)) {
sendMessage(plr, C.SCHEMATIC_INVALID, String.format("Wrong size (x: %s, z: %d) vs %d ", l1, l2, length)); sendMessage(plr, C.SCHEMATIC_INVALID, String.format("Wrong size (x: %s, z: %d) vs %d ", l1, l2, length));
break; break;
} }
sendMessage(plr, C.SCHEMATIC_VALID); sendMessage(plr, C.SCHEMATIC_VALID);
break; break;
}
case "saveall": case "saveall":
case "exportall": case "exportall": {
if (plr != null) { if (plr != null) {
MainUtil.sendMessage(plr, C.NOT_CONSOLE); MainUtil.sendMessage(plr, C.NOT_CONSOLE);
return false; return false;
@ -195,30 +197,28 @@ public class Schematic extends SubCommand {
} }
PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &7Mass export has started. This may take a while."); PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &7Mass export has started. This may take a while.");
PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &7Found &c" + plotmap.size() + "&7 plots..."); PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &7Found &c" + plotmap.size() + "&7 plots...");
final World worldObj = Bukkit.getWorld(args[1]); final String worldname = args[1];
final String worldname = Bukkit.getWorld(args[1]).getName();
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("PlotSquared");
final Collection<Plot> values = plotmap.values(); final Collection<Plot> values = plotmap.values();
this.plots = values.toArray(new Plot[values.size()]); this.plots = values.toArray(new Plot[values.size()]);
this.running = true; this.running = true;
this.counter = 0; this.counter = 0;
this.task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() { this.task = TaskManager.runTaskRepeat(new Runnable() {
@Override @Override
public void run() { public void run() {
if (Schematic.this.counter >= Schematic.this.plots.length) { if (Schematic.this.counter >= Schematic.this.plots.length) {
PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &aFinished!"); PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &aFinished!");
Schematic.this.running = false; Schematic.this.running = false;
Bukkit.getScheduler().cancelTask(Schematic.this.task); PlotSquared.TASK.cancelTask(Schematic.this.task);
return; return;
} }
final Plot plot = Schematic.this.plots[Schematic.this.counter]; final Plot plot = Schematic.this.plots[Schematic.this.counter];
final CompoundTag sch = SchematicHandler.getCompoundTag(worldObj, plot.id); final CompoundTag sch = SchematicHandler.getCompoundTag(worldname, plot.id);
final String o = UUIDHandler.getName(plot.owner); final String o = UUIDHandler.getName(plot.owner);
final String owner = o == null ? "unknown" : o; final String owner = o == null ? "unknown" : o;
if (sch == null) { if (sch == null) {
MainUtil.sendMessage(null, "&7 - Skipped plot &c" + plot.id); MainUtil.sendMessage(null, "&7 - Skipped plot &c" + plot.id);
} else { } else {
Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getServer().getPluginManager().getPlugin("PlotSquared"), new Runnable() { TaskManager.runTaskAsync(new Runnable() {
@Override @Override
public void run() { public void run() {
MainUtil.sendMessage(null, "&6ID: " + plot.id); MainUtil.sendMessage(null, "&6ID: " + plot.id);
@ -233,10 +233,12 @@ public class Schematic extends SubCommand {
} }
Schematic.this.counter++; Schematic.this.counter++;
} }
}, 20, 20); }, 20);
break; break;
}
case "export": case "export":
case "save": case "save":
{
if (!Permissions.hasPermission(plr, "plots.schematic.save")) { if (!Permissions.hasPermission(plr, "plots.schematic.save")) {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.schematic.save"); MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.schematic.save");
return false; return false;
@ -253,11 +255,11 @@ public class Schematic extends SubCommand {
if (plot == null) { if (plot == null) {
return !sendMessage(plr, C.NOT_IN_PLOT); return !sendMessage(plr, C.NOT_IN_PLOT);
} }
if (!myplot.hasRights(plr)) { if (!plot.isAdded(plr.getUUID())) {
sendMessage(plr, C.NO_PLOT_PERMS); sendMessage(plr, C.NO_PLOT_PERMS);
return false; return false;
} }
p2 = myplot; p2 = plot;
world = loc.getWorld(); world = loc.getWorld();
} else { } else {
if (args.length == 3) { if (args.length == 3) {
@ -279,27 +281,26 @@ public class Schematic extends SubCommand {
return false; return false;
} }
} }
final Plugin plugin2 = Bukkit.getServer().getPluginManager().getPlugin("PlotSquared");
this.plots = new Plot[] { p2 }; this.plots = new Plot[] { p2 };
this.running = true; this.running = true;
this.counter = 0; this.counter = 0;
this.task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin2, new Runnable() { this.task = TaskManager.runTaskRepeat(new Runnable() {
@Override @Override
public void run() { public void run() {
if (Schematic.this.counter >= Schematic.this.plots.length) { if (Schematic.this.counter >= Schematic.this.plots.length) {
PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &aFinished!"); PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &aFinished!");
Schematic.this.running = false; Schematic.this.running = false;
Bukkit.getScheduler().cancelTask(Schematic.this.task); PlotSquared.TASK.cancelTask(Schematic.this.task);
return; return;
} }
final Plot plot = Schematic.this.plots[Schematic.this.counter]; final Plot plot = Schematic.this.plots[Schematic.this.counter];
final CompoundTag sch = SchematicHandler.getCompoundTag(Bukkit.getWorld(world), plot.id); final CompoundTag sch = SchematicHandler.getCompoundTag(world, plot.id);
final String o = UUIDHandler.getName(plot.owner); final String o = UUIDHandler.getName(plot.owner);
final String owner = o == null ? "unknown" : o; final String owner = o == null ? "unknown" : o;
if (sch == null) { if (sch == null) {
MainUtil.sendMessage(plr, "&7 - Skipped plot &c" + plot.id); MainUtil.sendMessage(plr, "&7 - Skipped plot &c" + plot.id);
} else { } else {
Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getServer().getPluginManager().getPlugin("PlotSquared"), new Runnable() { TaskManager.runTaskAsync(new Runnable() {
@Override @Override
public void run() { public void run() {
MainUtil.sendMessage(plr, "&6ID: " + plot.id); MainUtil.sendMessage(plr, "&6ID: " + plot.id);
@ -314,11 +315,13 @@ public class Schematic extends SubCommand {
} }
Schematic.this.counter++; Schematic.this.counter++;
} }
}, 20, 60); }, 60);
break; break;
default: }
default: {
sendMessage(plr, C.SCHEMATIC_MISSING_ARG); sendMessage(plr, C.SCHEMATIC_MISSING_ARG);
break; break;
}
} }
return true; return true;
} }

View File

@ -29,6 +29,8 @@ public abstract class BlockManager {
return (int) r; return (int) r;
} }
public abstract int getHeighestBlock(Location loc);
public abstract String getBiome(Location loc); public abstract String getBiome(Location loc);
public abstract Location getSpawn(String world); public abstract Location getSpawn(String world);

View File

@ -7,43 +7,50 @@ import com.intellectualcrafters.plot.PlotSquared;
public abstract class TaskManager { public abstract class TaskManager {
public static HashSet<String> TELEPORT_QUEUE = new HashSet<>(); public static HashSet<String> TELEPORT_QUEUE = new HashSet<>();
public abstract void taskRepeat(final Runnable r, int interval); public abstract int taskRepeat(final Runnable r, int interval);
public abstract void taskAsync(final Runnable r); public abstract int taskAsync(final Runnable r);
public abstract void task(final Runnable r); public abstract int task(final Runnable r);
public abstract void taskLater(final Runnable r, int delay); public abstract int taskLater(final Runnable r, int delay);
public abstract void taskLaterAsync(final Runnable r, int delay); public abstract int taskLaterAsync(final Runnable r, int delay);
public static void runTaskRepeat(final Runnable r, final int interval) { public abstract void cancelTask(int task);
public static int runTaskRepeat(final Runnable r, final int interval) {
if (r != null) { if (r != null) {
PlotSquared.TASK.taskRepeat(r, interval); return PlotSquared.TASK.taskRepeat(r, interval);
} }
return -1;
} }
public static void runTaskAsync(final Runnable r) { public static int runTaskAsync(final Runnable r) {
if (r != null) { if (r != null) {
PlotSquared.TASK.taskAsync(r); return PlotSquared.TASK.taskAsync(r);
} }
return -1;
} }
public static void runTask(final Runnable r) { public static int runTask(final Runnable r) {
if (r != null) { if (r != null) {
PlotSquared.TASK.task(r); return PlotSquared.TASK.task(r);
} }
return -1;
} }
public static void runTaskLater(final Runnable r, final int delay) { public static int runTaskLater(final Runnable r, final int delay) {
if (r != null) { if (r != null) {
PlotSquared.TASK.taskLater(r, delay); return PlotSquared.TASK.taskLater(r, delay);
} }
return -1;
} }
public static void runTaskLaterAsync(final Runnable r, final int delay) { public static int runTaskLaterAsync(final Runnable r, final int delay) {
if (r != null) { if (r != null) {
PlotSquared.TASK.taskLaterAsync(r, delay); return PlotSquared.TASK.taskLaterAsync(r, delay);
} }
return -1;
} }
} }

View File

@ -1,31 +1,40 @@
package com.intellectualcrafters.plot.util.bukkit; package com.intellectualcrafters.plot.util.bukkit;
import org.bukkit.Bukkit;
import com.intellectualcrafters.plot.BukkitMain; import com.intellectualcrafters.plot.BukkitMain;
import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.TaskManager;
public class BukkitTaskManager extends TaskManager { public class BukkitTaskManager extends TaskManager {
@Override @Override
public void taskRepeat(final Runnable r, final int interval) { public int taskRepeat(final Runnable r, final int interval) {
BukkitMain.THIS.getServer().getScheduler().scheduleSyncRepeatingTask(BukkitMain.THIS, r, interval, interval); return BukkitMain.THIS.getServer().getScheduler().scheduleSyncRepeatingTask(BukkitMain.THIS, r, interval, interval);
} }
@Override @Override
public void taskAsync(final Runnable r) { public int taskAsync(final Runnable r) {
BukkitMain.THIS.getServer().getScheduler().runTaskAsynchronously(BukkitMain.THIS, r); return BukkitMain.THIS.getServer().getScheduler().runTaskAsynchronously(BukkitMain.THIS, r).getTaskId();
} }
@Override @Override
public void task(final Runnable r) { public int task(final Runnable r) {
BukkitMain.THIS.getServer().getScheduler().runTask(BukkitMain.THIS, r); return BukkitMain.THIS.getServer().getScheduler().runTask(BukkitMain.THIS, r).getTaskId();
} }
@Override @Override
public void taskLater(final Runnable r, final int delay) { public int taskLater(final Runnable r, final int delay) {
BukkitMain.THIS.getServer().getScheduler().runTaskLater(BukkitMain.THIS, r, delay); return BukkitMain.THIS.getServer().getScheduler().runTaskLater(BukkitMain.THIS, r, delay).getTaskId();
} }
@Override @Override
public void taskLaterAsync(final Runnable r, final int delay) { public int taskLaterAsync(final Runnable r, final int delay) {
BukkitMain.THIS.getServer().getScheduler().runTaskLaterAsynchronously(BukkitMain.THIS, r, delay); return BukkitMain.THIS.getServer().getScheduler().runTaskLaterAsynchronously(BukkitMain.THIS, r, delay).getTaskId();
}
@Override
public void cancelTask(int task) {
if (task != -1) {
Bukkit.getScheduler().cancelTask(task);
}
} }
} }

View File

@ -217,4 +217,9 @@ public class BukkitUtil extends BlockManager {
org.bukkit.Location temp = getWorld(world).getSpawnLocation(); org.bukkit.Location temp = getWorld(world).getSpawnLocation();
return new Location(world, temp.getBlockX(), temp.getBlockY(), temp.getBlockZ()); return new Location(world, temp.getBlockX(), temp.getBlockY(), temp.getBlockZ());
} }
@Override
public int getHeighestBlock(Location loc) {
return getWorld(loc.getWorld()).getHighestBlockAt(loc.getX(), loc.getZ()).getY();
}
} }