From adb8ee16688599b5c58af3e6c467db7927f3a497 Mon Sep 17 00:00:00 2001 From: garbagemule Date: Mon, 24 Jun 2013 22:10:07 +0200 Subject: [PATCH] Add checkspawns command. --- .../MobArena/commands/CommandHandler.java | 1 + .../commands/setup/CheckSpawnsCommand.java | 61 ++++++++++++++ .../commands/setup/ShowRegionCommand.java | 2 +- .../MobArena/region/ArenaRegion.java | 79 ++++++++++++++----- 4 files changed, 123 insertions(+), 20 deletions(-) create mode 100644 src/com/garbagemule/MobArena/commands/setup/CheckSpawnsCommand.java diff --git a/src/com/garbagemule/MobArena/commands/CommandHandler.java b/src/com/garbagemule/MobArena/commands/CommandHandler.java index effbef1..b8983c8 100644 --- a/src/com/garbagemule/MobArena/commands/CommandHandler.java +++ b/src/com/garbagemule/MobArena/commands/CommandHandler.java @@ -173,6 +173,7 @@ public class CommandHandler implements CommandExecutor register(AddSpawnpointCommand.class); register(ArenaCommand.class); register(CheckDataCommand.class); + register(CheckSpawnsCommand.class); register(ConfigCommand.class); register(ContainersCommand.class); register(EditArenaCommand.class); diff --git a/src/com/garbagemule/MobArena/commands/setup/CheckSpawnsCommand.java b/src/com/garbagemule/MobArena/commands/setup/CheckSpawnsCommand.java new file mode 100644 index 0000000..7609d76 --- /dev/null +++ b/src/com/garbagemule/MobArena/commands/setup/CheckSpawnsCommand.java @@ -0,0 +1,61 @@ +package com.garbagemule.MobArena.commands.setup; + +import com.garbagemule.MobArena.Messenger; +import com.garbagemule.MobArena.Msg; +import com.garbagemule.MobArena.commands.Command; +import com.garbagemule.MobArena.commands.CommandInfo; +import com.garbagemule.MobArena.commands.Commands; +import com.garbagemule.MobArena.framework.Arena; +import com.garbagemule.MobArena.framework.ArenaMaster; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +@CommandInfo( + name = "checkspawns", + pattern = "checkspawn(point)?s", + usage = "/ma checkspawns ()", + desc = "show spawnpoints that cover your location", + permission = "mobarena.setup.checkspawns" +) +public class CheckSpawnsCommand implements Command +{ + @Override + public boolean execute(ArenaMaster am, CommandSender sender, String... args) { + if (!Commands.isPlayer(sender)) { + Messenger.tellPlayer(sender, Msg.MISC_NOT_FROM_CONSOLE); + return false; + } + + // Grab the argument, if any. + String arg1 = (args.length > 0 ? args[0] : ""); + + // Cast the sender. + Player p = (Player) sender; + + Arena arena; + + if (arg1.equals("")) { + arena = am.getArenaAtLocation(p.getLocation()); + if (arena == null) { + arena = am.getSelectedArena(); + } + + if (arena.getRegion().getSpawnpoints().isEmpty()) { + Messenger.tellPlayer(sender, "There are no spawnpoints in the selected arena."); + return false; + } + } + else { + arena = am.getArenaWithName(arg1); + + if (arena == null) { + Messenger.tellPlayer(sender, Msg.ARENA_DOES_NOT_EXIST); + return false; + } + } + + arena.getRegion().checkSpawns(p); + + return true; + } +} diff --git a/src/com/garbagemule/MobArena/commands/setup/ShowRegionCommand.java b/src/com/garbagemule/MobArena/commands/setup/ShowRegionCommand.java index 74c2fb0..d016097 100644 --- a/src/com/garbagemule/MobArena/commands/setup/ShowRegionCommand.java +++ b/src/com/garbagemule/MobArena/commands/setup/ShowRegionCommand.java @@ -10,7 +10,7 @@ import com.garbagemule.MobArena.framework.ArenaMaster; @CommandInfo( name = "showregion", - pattern = "show(region)?", + pattern = "showregion", usage = "/ma showregion ()", desc = "show an arena region", permission = "mobarena.setup.showregion" diff --git a/src/com/garbagemule/MobArena/region/ArenaRegion.java b/src/com/garbagemule/MobArena/region/ArenaRegion.java index 1adc7eb..e719aa8 100644 --- a/src/com/garbagemule/MobArena/region/ArenaRegion.java +++ b/src/com/garbagemule/MobArena/region/ArenaRegion.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.bukkit.ChatColor; import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.World; @@ -458,33 +459,73 @@ public class ArenaRegion if (!isDefined()) { return; } - + showBlocks(p, getFramePoints()); + } + + public void showSpawns(final Player p) { + if (spawnpoints.isEmpty()) { + return; + } + showBlocks(p, spawnpoints.values()); + } + + public void checkSpawns(Player p) { + if (spawnpoints.isEmpty()) { + return; + } + + // Find all the spawnpoints that cover the location + Map map = new HashMap(); + for (Map.Entry entry : spawnpoints.entrySet()) { + if (p.getLocation().distanceSquared(entry.getValue()) < MobArena.MIN_PLAYER_DISTANCE_SQUARED) { + map.put(entry.getKey(), entry.getValue()); + } + } + + if (map.isEmpty()) { + Messenger.tellPlayer(p, "No spawnpoints cover your location!"); + return; + } + + // Notify the player + Messenger.tellPlayer(p, "The following points cover your location:"); + for (Map.Entry entry : map.entrySet()) { + Location l = entry.getValue(); + String coords = l.getBlockX() + "," + l.getBlockY() + "," + l.getBlockZ(); + p.sendMessage(ChatColor.AQUA + entry.getKey() + ChatColor.WHITE + " : " + coords); + } + + // And show the blocks + showBlocks(p, map.values()); + } + + private void showBlocks(final Player p, Collection points) { // Grab all the blocks, and send block change events. final Map blocks = new HashMap(); - for (Location l : getFramePoints()) { + for (Location l : points) { Block b = l.getBlock(); blocks.put(l, b.getState()); p.sendBlockChange(l, 35, (byte) 14); } - + arena.scheduleTask(new Runnable() { - public void run() { - // If the player isn't online, just forget it. - if (!p.isOnline()) { - return; - } - - // Send block "restore" events. - for (Map.Entry entry : blocks.entrySet()) { - Location l = entry.getKey(); - BlockState b = entry.getValue(); - int id = b.getTypeId(); - byte data = b.getRawData(); - - p.sendBlockChange(l, id, data); - } + public void run() { + // If the player isn't online, just forget it. + if (!p.isOnline()) { + return; } - }, 100); + + // Send block "restore" events. + for (Map.Entry entry : blocks.entrySet()) { + Location l = entry.getKey(); + BlockState b = entry.getValue(); + int id = b.getTypeId(); + byte data = b.getRawData(); + + p.sendBlockChange(l, id, data); + } + } + }, 100); } private List getFramePoints() {