Added Command_trail.

+1 points for no external changes.
This commit is contained in:
Steven Lawson 2013-08-10 15:50:45 -04:00
parent 1ae6eb8a5a
commit 38b1035020
3 changed files with 113 additions and 5 deletions

View File

@ -1,5 +1,5 @@
#Sat, 10 Aug 2013 12:37:32 -0400 #Sat, 10 Aug 2013 15:43:16 -0400
program.VERSION=2.22 program.VERSION=2.22
program.BUILDNUM=386 program.BUILDNUM=392
program.BUILDDATE=08/10/2013 12\:37 PM program.BUILDDATE=08/10/2013 03\:43 PM

View File

@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit! #Build Number for ANT. Do not edit!
#Sat Aug 10 12:37:32 EDT 2013 #Sat Aug 10 15:43:16 EDT 2013
build.number=387 build.number=393

View File

@ -0,0 +1,108 @@
package me.StevenLawson.TotalFreedomMod.Commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import me.StevenLawson.TotalFreedomMod.TFM_Util;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.RegisteredListener;
@CommandPermissions(level = AdminLevel.SUPER, source = SourceType.ONLY_IN_GAME)
@CommandParameters(description = "Pretty rainbow trails.", usage = "/<command> [off]")
public class Command_trail extends TFM_Command
{
private static Listener movementListener = null;
private static final List<Player> trailPlayers = new ArrayList<Player>();
private static final Random RANDOM = new Random();
@Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
if (args.length > 0 && TFM_Util.isStopCommand(args[0]))
{
trailPlayers.remove(sender_p);
playerMsg("Trail disabled.");
}
else
{
if (!trailPlayers.contains(sender_p))
{
trailPlayers.add(sender_p);
}
playerMsg("Trail enabled!");
}
if (!trailPlayers.isEmpty())
{
registerMovementHandler();
}
else
{
unregisterMovementHandler();
}
return true;
}
private static void registerMovementHandler()
{
if (getRegisteredListener(movementListener) == null)
{
Bukkit.getPluginManager().registerEvents(movementListener = new Listener()
{
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerMove(PlayerMoveEvent event)
{
Player player = event.getPlayer();
if (trailPlayers.contains(player))
{
Block fromBlock = event.getFrom().getBlock();
if (fromBlock.isEmpty())
{
Block toBlock = event.getTo().getBlock();
if (!fromBlock.equals(toBlock))
{
fromBlock.setType(Material.WOOL);
fromBlock.setData((byte) RANDOM.nextInt(16));
}
}
}
}
}, TotalFreedomMod.plugin);
}
}
private static void unregisterMovementHandler()
{
Listener registeredListener = getRegisteredListener(movementListener);
if (registeredListener != null)
{
PlayerMoveEvent.getHandlerList().unregister(registeredListener);
}
}
private static Listener getRegisteredListener(Listener listener)
{
RegisteredListener[] registeredListeners = PlayerMoveEvent.getHandlerList().getRegisteredListeners();
for (RegisteredListener registeredListener : registeredListeners)
{
if (registeredListener.getListener() == listener)
{
return listener;
}
}
return null;
}
}