From fafad0f59068a7e0a0469ab9f2b354039f980b0c Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sat, 28 Jan 2012 19:49:01 +0100 Subject: [PATCH] Improved the seechunk command. --- plugin.yml | 3 + .../massivecraft/factions/cmd/CmdHelp.java | 1 + .../factions/cmd/CmdSeeChunk.java | 41 +------- .../listeners/FactionsPlayerListener.java | 24 +++++ .../factions/struct/Permission.java | 1 + .../factions/util/VisualizeUtil.java | 96 +++++++++++++++++++ 6 files changed, 129 insertions(+), 37 deletions(-) create mode 100644 src/com/massivecraft/factions/util/VisualizeUtil.java diff --git a/plugin.yml b/plugin.yml index 3ec8db7e..adbccba8 100644 --- a/plugin.yml +++ b/plugin.yml @@ -67,6 +67,7 @@ permissions: factions.power: true factions.power.any: true factions.relation: true + factions.seechunk: true factions.sethome: true factions.show: true factions.tag: true @@ -165,6 +166,8 @@ permissions: description: reload data file(s) from disk factions.save: description: save all data to disk + factions.seechunk: + description: see the chunk you stand in factions.sethome: description: set the faction home factions.show: diff --git a/src/com/massivecraft/factions/cmd/CmdHelp.java b/src/com/massivecraft/factions/cmd/CmdHelp.java index ecc96f0e..1601d032 100644 --- a/src/com/massivecraft/factions/cmd/CmdHelp.java +++ b/src/com/massivecraft/factions/cmd/CmdHelp.java @@ -112,6 +112,7 @@ public class CmdHelp extends FCommand pageLines = new ArrayList(); pageLines.add( p.cmdBase.cmdMap.getUseageTemplate(true) ); + pageLines.add( p.cmdBase.cmdSeeChunks.getUseageTemplate(true) ); pageLines.add(p.txt.parse("Claimed land with ownership set is further protected so")); pageLines.add(p.txt.parse("that only the owner(s), faction admin, and possibly the")); pageLines.add(p.txt.parse("faction moderators have full access.")); diff --git a/src/com/massivecraft/factions/cmd/CmdSeeChunk.java b/src/com/massivecraft/factions/cmd/CmdSeeChunk.java index 86b6105b..914edc37 100644 --- a/src/com/massivecraft/factions/cmd/CmdSeeChunk.java +++ b/src/com/massivecraft/factions/cmd/CmdSeeChunk.java @@ -1,28 +1,21 @@ package com.massivecraft.factions.cmd; -import java.util.ArrayList; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; -import org.bukkit.block.Block; import org.bukkit.entity.Player; - import com.massivecraft.factions.struct.Permission; +import com.massivecraft.factions.util.VisualizeUtil; -// !!!! This is just an experiment. -// Proof of concept. We could use fake block updates to visualize the territories. public class CmdSeeChunk extends FCommand { public CmdSeeChunk() { super(); this.aliases.add("sc"); - this.aliases.add("seechunks"); + this.aliases.add("seechunk"); - //this.requiredArgs.add(""); - //this.optionalArgs.put("", ""); - - this.permission = Permission.ADMIN.node; + this.permission = Permission.SEE_CHUNK.node; this.disableOnLock = false; senderMustBePlayer = true; @@ -64,38 +57,12 @@ public class CmdSeeChunk extends FCommand public void showPillar(Player player, World world, int blockX, int blockZ) { Location loc = new Location(world, blockX, 0, blockZ); - //Block block = loc.getBlock(); for (int blockY = 0; blockY <=127; blockY++) { loc.setY(blockY); if (loc.getBlock().getTypeId() != 0) continue; - player.sendBlockChange(loc, blockY % 5 == 0 ? Material.GLOWSTONE : Material.GLASS, (byte) 0); + VisualizeUtil.addLocation(player, loc.clone(), blockY % 5 == 0 ? Material.GLOWSTONE.getId() : Material.GLASS.getId()); } } - // DEV DIRT BELOW... - - public ArrayList getChunkPillarLocations(int chunkX, int chunkZ) - { - ArrayList ret = new ArrayList(); - - - - return ret; - } - - public ArrayList getPillar(Block block) - { - ArrayList ret = new ArrayList(); - - // y 0-127 - - for (int i = 0; i <=127; i++) - { - - } - - return ret; - } - } diff --git a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java index 41598686..bd19d13c 100644 --- a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -21,6 +21,7 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerKickEvent; import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerPreLoginEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerRespawnEvent; @@ -34,6 +35,7 @@ import com.massivecraft.factions.integration.SpoutFeatures; import com.massivecraft.factions.struct.FFlag; import com.massivecraft.factions.struct.FPerm; import com.massivecraft.factions.struct.Rel; +import com.massivecraft.factions.util.VisualizeUtil; import java.util.logging.Level; @@ -452,4 +454,26 @@ public class FactionsPlayerListener implements Listener badGuy.detach(); } } + + // -------------------------------------------- // + // VisualizeUtil + // -------------------------------------------- // + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerMoveClearVisualizations(PlayerMoveEvent event) + { + if (event.isCancelled()) return; + + Block blockFrom = event.getFrom().getBlock(); + Block blockTo = event.getTo().getBlock(); + if (blockFrom.equals(blockTo)) return; + + VisualizeUtil.clear(event.getPlayer()); + } + + @EventHandler(priority = EventPriority.LOWEST) + public void onPlayerPreLogin(PlayerPreLoginEvent event) + { + VisualizeUtil.onPlayerPreLogin(event.getName()); + } } diff --git a/src/com/massivecraft/factions/struct/Permission.java b/src/com/massivecraft/factions/struct/Permission.java index f04a7944..9c31b226 100644 --- a/src/com/massivecraft/factions/struct/Permission.java +++ b/src/com/massivecraft/factions/struct/Permission.java @@ -46,6 +46,7 @@ public enum Permission RELATION("relation"), RELOAD("reload"), SAVE("save"), + SEE_CHUNK("seechunk"), SETHOME("sethome"), SHOW("show"), TAG("tag"), diff --git a/src/com/massivecraft/factions/util/VisualizeUtil.java b/src/com/massivecraft/factions/util/VisualizeUtil.java new file mode 100644 index 00000000..b6ba0dd9 --- /dev/null +++ b/src/com/massivecraft/factions/util/VisualizeUtil.java @@ -0,0 +1,96 @@ +package com.massivecraft.factions.util; + +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; + +import com.massivecraft.factions.P; + +// TODO: Only send blocks in visual range +// TODO: Only send blocks that where changed when clearing? +// TODO: Create packed queue to avoid freezes. + +public class VisualizeUtil +{ + protected static Map> playerLocations = new HashMap>(); + + public static void onPlayerPreLogin(String name) + { + playerLocations.put(name, new HashSet()); + } + + // -------------------------------------------- // + // SINGLE + // -------------------------------------------- // + + public static void addLocation(Player player, Location location, int typeId, byte data) + { + playerLocations.get(player.getName()).add(location); + player.sendBlockChange(location, typeId, data); + } + + public static void addLocation(Player player, Location location, int typeId) + { + playerLocations.get(player.getName()).add(location); + player.sendBlockChange(location, typeId, (byte) 0); + } + + // -------------------------------------------- // + // MANY + // -------------------------------------------- // + + public static void addLocations(Player player, Map locationMaterialIds) + { + Set ploc = playerLocations.get(player.getName()); + for (Entry entry : locationMaterialIds.entrySet()) + { + ploc.add(entry.getKey()); + player.sendBlockChange(entry.getKey(), entry.getValue(), (byte) 0); + } + } + + public static void addLocations(Player player, Collection locations, int typeId) + { + Set ploc = playerLocations.get(player.getName()); + for (Location location : locations) + { + ploc.add(location); + player.sendBlockChange(location, typeId, (byte) 0); + } + } + + public static void addBlocks(Player player, Collection blocks, int typeId) + { + Set ploc = playerLocations.get(player.getName()); + for (Block block : blocks) + { + Location location = block.getLocation(); + ploc.add(location); + player.sendBlockChange(location, typeId, (byte) 0); + } + } + + // -------------------------------------------- // + // CLEAR + // -------------------------------------------- // + + public static void clear(Player player) + { + Set locations = playerLocations.get(player.getName()); + if (locations == null) return; + for (Location location : locations) + { + Block block = location.getWorld().getBlockAt(location); + player.sendBlockChange(location, block.getTypeId(), block.getData()); + } + playerLocations.remove(player); + } + +}