diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Schematic.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Schematic.java index a219479ca..c18c907c5 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Schematic.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Schematic.java @@ -23,23 +23,21 @@ package com.intellectualcrafters.plot.commands; import java.util.Collection; 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.plot.PlotSquared; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; +import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.util.BlockManager; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.SchematicHandler; import com.intellectualcrafters.plot.util.SchematicHandler.DataCollection; 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.UUIDHandler; @@ -64,9 +62,9 @@ public class Schematic extends SubCommand { final String file; final SchematicHandler.Schematic schematic; switch (arg) { - case "paste": + case "paste": { if (plr == null) { - PlotSquared.log(C.IS_CONSOLE); + PlotSquared.log(C.IS_CONSOLE.s()); return false; } if (!Permissions.hasPermission(plr, "plots.schematic.paste")) { @@ -77,7 +75,9 @@ public class Schematic extends SubCommand { sendMessage(plr, C.SCHEMATIC_MISSING_ARG); break; } - if (!BukkitPlayerFunctions.isInPlot(plr)) { + final Location loc = plr.getLocation(); + Plot plot = MainUtil.getPlot(loc); + if (plot == null) { sendMessage(plr, C.NOT_IN_PLOT); break; } @@ -88,7 +88,7 @@ public class Schematic extends SubCommand { final String file2 = args[1]; this.running = true; this.counter = 0; - Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getServer().getPluginManager().getPlugin("PlotSquared"), new Runnable() { + TaskManager.runTaskAsync(new Runnable() { @Override public void run() { final SchematicHandler.Schematic schematic = SchematicHandler.getSchematic(file2); @@ -101,8 +101,8 @@ public class Schematic extends SubCommand { final int z; final Plot plot2 = MainUtil.getPlot(loc); final Dimension dem = schematic.getSchematicDimension(); - final Location bot = MainUtil.getPlotBottomLoc(plr.getWorld(), plot2.id).add(1, 0, 1); - final int length2 = MainUtil.getPlotWidth(plr.getWorld(), plot2.id); + final Location bot = MainUtil.getPlotBottomLoc(loc.getWorld(), plot2.id).add(1, 0, 1); + final int length2 = MainUtil.getPlotWidth(loc.getWorld(), plot2.id); 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)); Schematic.this.running = false; @@ -110,20 +110,19 @@ public class Schematic extends SubCommand { } if ((dem.getX() != length2) || (dem.getZ() != length2)) { final Location loc = plr.getLocation(); - x = Math.min(length2 - dem.getX(), loc.getBlockX() - bot.getBlockX()); - z = Math.min(length2 - dem.getZ(), loc.getBlockZ() - bot.getBlockZ()); + x = Math.min(length2 - dem.getX(), loc.getX() - bot.getX()); + z = Math.min(length2 - dem.getZ(), loc.getZ() - bot.getZ()); } else { x = 0; z = 0; } - final World w = plot2.getWorld(); 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 int WIDTH = schematic.getSchematicDimension().getX(); final int LENGTH = schematic.getSchematicDimension().getZ(); final int blen = b.length - 1; - Schematic.this.task = Bukkit.getScheduler().scheduleSyncRepeatingTask(PlotSquared.getMain(), new Runnable() { + Schematic.this.task = TaskManager.runTaskRepeat(new Runnable() { @Override public void run() { boolean result = false; @@ -133,21 +132,22 @@ public class Schematic extends SubCommand { sendMessage(plr, C.SCHEMATIC_PASTE_SUCCESS); MainUtil.update(plr.getLocation()); Schematic.this.running = false; - Bukkit.getScheduler().cancelTask(Schematic.this.task); + PlotSquared.TASK.cancelTask(Schematic.this.task); return; } 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++; } } - }, 1, 1); + }, 1); } }); break; - case "test": + } + case "test": { if (plr == null) { - PlotSquared.log(C.IS_CONSOLE); + PlotSquared.log(C.IS_CONSOLE.s()); return false; } if (!Permissions.hasPermission(plr, "plots.schematic.test")) { @@ -164,18 +164,20 @@ public class Schematic extends SubCommand { sendMessage(plr, C.SCHEMATIC_INVALID, "non-existent"); break; } + Location loc = plr.getLocation(); final int l1 = schematic.getSchematicDimension().getX(); final int l2 = schematic.getSchematicDimension().getZ(); 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)) { sendMessage(plr, C.SCHEMATIC_INVALID, String.format("Wrong size (x: %s, z: %d) vs %d ", l1, l2, length)); break; } sendMessage(plr, C.SCHEMATIC_VALID); break; + } case "saveall": - case "exportall": + case "exportall": { if (plr != null) { MainUtil.sendMessage(plr, C.NOT_CONSOLE); 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: &7Found &c" + plotmap.size() + "&7 plots..."); - final World worldObj = Bukkit.getWorld(args[1]); - final String worldname = Bukkit.getWorld(args[1]).getName(); - final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("PlotSquared"); + final String worldname = args[1]; final Collection values = plotmap.values(); this.plots = values.toArray(new Plot[values.size()]); this.running = true; this.counter = 0; - this.task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() { + this.task = TaskManager.runTaskRepeat(new Runnable() { @Override public void run() { if (Schematic.this.counter >= Schematic.this.plots.length) { PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &aFinished!"); Schematic.this.running = false; - Bukkit.getScheduler().cancelTask(Schematic.this.task); + PlotSquared.TASK.cancelTask(Schematic.this.task); return; } 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 owner = o == null ? "unknown" : o; if (sch == null) { MainUtil.sendMessage(null, "&7 - Skipped plot &c" + plot.id); } else { - Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getServer().getPluginManager().getPlugin("PlotSquared"), new Runnable() { + TaskManager.runTaskAsync(new Runnable() { @Override public void run() { MainUtil.sendMessage(null, "&6ID: " + plot.id); @@ -233,10 +233,12 @@ public class Schematic extends SubCommand { } Schematic.this.counter++; } - }, 20, 20); + }, 20); break; + } case "export": case "save": + { if (!Permissions.hasPermission(plr, "plots.schematic.save")) { MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.schematic.save"); return false; @@ -253,11 +255,11 @@ public class Schematic extends SubCommand { if (plot == null) { return !sendMessage(plr, C.NOT_IN_PLOT); } - if (!myplot.hasRights(plr)) { + if (!plot.isAdded(plr.getUUID())) { sendMessage(plr, C.NO_PLOT_PERMS); return false; } - p2 = myplot; + p2 = plot; world = loc.getWorld(); } else { if (args.length == 3) { @@ -279,27 +281,26 @@ public class Schematic extends SubCommand { return false; } } - final Plugin plugin2 = Bukkit.getServer().getPluginManager().getPlugin("PlotSquared"); this.plots = new Plot[] { p2 }; this.running = true; this.counter = 0; - this.task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin2, new Runnable() { + this.task = TaskManager.runTaskRepeat(new Runnable() { @Override public void run() { if (Schematic.this.counter >= Schematic.this.plots.length) { PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &aFinished!"); Schematic.this.running = false; - Bukkit.getScheduler().cancelTask(Schematic.this.task); + PlotSquared.TASK.cancelTask(Schematic.this.task); return; } 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 owner = o == null ? "unknown" : o; if (sch == null) { MainUtil.sendMessage(plr, "&7 - Skipped plot &c" + plot.id); } else { - Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getServer().getPluginManager().getPlugin("PlotSquared"), new Runnable() { + TaskManager.runTaskAsync(new Runnable() { @Override public void run() { MainUtil.sendMessage(plr, "&6ID: " + plot.id); @@ -314,11 +315,13 @@ public class Schematic extends SubCommand { } Schematic.this.counter++; } - }, 20, 60); + }, 60); break; - default: + } + default: { sendMessage(plr, C.SCHEMATIC_MISSING_ARG); break; + } } return true; } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/BlockManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/BlockManager.java index afed23651..2bfbe0b97 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/BlockManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/BlockManager.java @@ -29,6 +29,8 @@ public abstract class BlockManager { return (int) r; } + public abstract int getHeighestBlock(Location loc); + public abstract String getBiome(Location loc); public abstract Location getSpawn(String world); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/TaskManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/TaskManager.java index fb284e553..27b62fdf3 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/TaskManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/TaskManager.java @@ -7,43 +7,50 @@ import com.intellectualcrafters.plot.PlotSquared; public abstract class TaskManager { public static HashSet 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) { - 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) { - 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) { - 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) { - 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) { - PlotSquared.TASK.taskLaterAsync(r, delay); + return PlotSquared.TASK.taskLaterAsync(r, delay); } + return -1; } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitTaskManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitTaskManager.java index 1a796fb43..f4125b702 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitTaskManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitTaskManager.java @@ -1,31 +1,40 @@ package com.intellectualcrafters.plot.util.bukkit; +import org.bukkit.Bukkit; + import com.intellectualcrafters.plot.BukkitMain; import com.intellectualcrafters.plot.util.TaskManager; public class BukkitTaskManager extends TaskManager { @Override - public void taskRepeat(final Runnable r, final int interval) { - BukkitMain.THIS.getServer().getScheduler().scheduleSyncRepeatingTask(BukkitMain.THIS, r, interval, interval); + public int taskRepeat(final Runnable r, final int interval) { + return BukkitMain.THIS.getServer().getScheduler().scheduleSyncRepeatingTask(BukkitMain.THIS, r, interval, interval); } @Override - public void taskAsync(final Runnable r) { - BukkitMain.THIS.getServer().getScheduler().runTaskAsynchronously(BukkitMain.THIS, r); + public int taskAsync(final Runnable r) { + return BukkitMain.THIS.getServer().getScheduler().runTaskAsynchronously(BukkitMain.THIS, r).getTaskId(); } @Override - public void task(final Runnable r) { - BukkitMain.THIS.getServer().getScheduler().runTask(BukkitMain.THIS, r); + public int task(final Runnable r) { + return BukkitMain.THIS.getServer().getScheduler().runTask(BukkitMain.THIS, r).getTaskId(); } @Override - public void taskLater(final Runnable r, final int delay) { - BukkitMain.THIS.getServer().getScheduler().runTaskLater(BukkitMain.THIS, r, delay); + public int taskLater(final Runnable r, final int delay) { + return BukkitMain.THIS.getServer().getScheduler().runTaskLater(BukkitMain.THIS, r, delay).getTaskId(); } @Override - public void taskLaterAsync(final Runnable r, final int delay) { - BukkitMain.THIS.getServer().getScheduler().runTaskLaterAsynchronously(BukkitMain.THIS, r, delay); + public int taskLaterAsync(final Runnable r, final int 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); + } } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitUtil.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitUtil.java index ea44975a2..83d560647 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitUtil.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitUtil.java @@ -217,4 +217,9 @@ public class BukkitUtil extends BlockManager { org.bukkit.Location temp = getWorld(world).getSpawnLocation(); 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(); + } }