Added the nearby command. Issue #12

This commit is contained in:
Tobias Ottenweller 2013-07-24 10:39:37 +02:00
parent f4111fb34f
commit efc30d0ae0
6 changed files with 93 additions and 1 deletions

View File

@ -1,5 +1,6 @@
maxGateBlocks: 50
playerGateBlockUpdateRadius: 64
highlightDuration: 5
saveOnChanges: true
checkForBrokenGateFrames: true
gateTeleportMessage: "Thank you for traveling with Craft Inc. Gates."

View File

@ -51,6 +51,7 @@ public class Plugin extends JavaPlugin
public static final String confGateTeleportNoPermissionMessageKey = "gateTeleportNoPermissionMessage";
public static final String confShowTeleportNoPermissionMessageKey = "showTeleportNoPermissionMessage";
public static final String confSaveOnChangesKey = "saveOnChanges";
public static final String confHighlightDurationKey = "highlightDuration";
private static Plugin instance;
private static Permission permission;
@ -154,6 +155,7 @@ public class Plugin extends JavaPlugin
commands.add(new CommandHide());
commands.add(new CommandUnhide());
commands.add(new CommandExitOpen());
commands.add(new CommandNearby());
// Register events

View File

@ -45,6 +45,7 @@ public class CommandHelp extends BaseCommand
allUsageStrings.add( new CommandHide().getUsageTemplate(true, true) );
allUsageStrings.add( new CommandUnhide().getUsageTemplate(true, true) );
allUsageStrings.add( new CommandExitOpen().getUsageTemplate(true, true) );
allUsageStrings.add( new CommandNearby().getUsageTemplate(true, true) );
Collections.sort(allUsageStrings);

View File

@ -29,7 +29,6 @@ import org.bukkit.block.Block;
public class CommandLocation extends BaseLocationCommand
{
public CommandLocation()
{
aliases.add("location");

View File

@ -0,0 +1,29 @@
package de.craftinc.gates.commands;
import de.craftinc.gates.Plugin;
import de.craftinc.gates.util.GateBlockChangeSender;
public class CommandNearby extends BaseLocationCommand
{
public CommandNearby()
{
aliases.add("nearby");
aliases.add("nb");
helpDescription = "Highlight nearby gates";
requiredPermission = Plugin.permissionInfo;
needsPermissionAtCurrentLocation = true;
shouldPersistToDisk = false;
senderMustBePlayer = true;
hasGateParam = false;
}
public void perform()
{
GateBlockChangeSender.temporaryHighlightGatesFrames(player);
}
}

View File

@ -22,6 +22,7 @@ import de.craftinc.gates.Gate;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import java.util.ArrayList;
@ -30,6 +31,65 @@ import java.util.Set;
public class GateBlockChangeSender
{
/**
* Replaces gate frame blocks with glowstone for a short period of time.
* Uses the value stored in 'highlightDuration' inside the config file
* for determining when to de-highlight the frames.
* @param player The player for whom the frame should be highlighted.
* Must not be null!
*/
public static void temporaryHighlightGatesFrames(final Player player)
{
if (player == null) {
throw new IllegalArgumentException("'player' must not be 'null'!");
}
Location location = player.getLocation();
final Set<Gate> gatesNearby = Plugin.getPlugin().getGatesManager().getNearbyGates(location.getChunk());
if (gatesNearby == null) {
return; // no gates nearby
}
highlightGatesFrames(player, gatesNearby);
Plugin plugin = Plugin.getPlugin();
long highlightDuration = 20 * plugin.getConfig().getLong(Plugin.confHighlightDurationKey);
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() {
dehighlightGateFrame(player, gatesNearby);
}
}, highlightDuration);
}
protected static void highlightGatesFrames(final Player player, final Set<Gate> gates)
{
for (Gate g : gates) {
Set<Block> frameBlocks = g.getGateFrameBlocks();
for (Block b : frameBlocks) {
player.sendBlockChange(b.getLocation(), Material.GLOWSTONE, (byte)0);
}
}
}
protected static void dehighlightGateFrame(final Player player, final Set<Gate> gates)
{
for (Gate g : gates) {
Set<Block> frameBlocks = g.getGateFrameBlocks();
for (Block b : frameBlocks) {
player.sendBlockChange(b.getLocation(), b.getType(), (byte)0);
}
}
}
/**
* Sends gate blocks to player at a given location. Will send the updates either immediately or
* immediately and after a short delay.