2016-03-02 19:28:01 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.command;
|
2013-08-10 19:50:45 +00:00
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.rank.PlayerRank;
|
2013-08-10 19:50:45 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.DepreciationAggregator;
|
|
|
|
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
2013-08-10 19:50:45 +00:00
|
|
|
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;
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
@CommandPermissions(level = PlayerRank.SUPER_ADMIN, source = SourceType.ONLY_IN_GAME)
|
2013-08-10 19:50:45 +00:00
|
|
|
@CommandParameters(description = "Pretty rainbow trails.", usage = "/<command> [off]")
|
2015-10-19 17:43:46 +00:00
|
|
|
public class Command_trail extends FreedomCommand
|
2013-08-10 19:50:45 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2013-08-10 19:50:45 +00:00
|
|
|
private static Listener movementListener = null;
|
2016-03-02 19:28:01 +00:00
|
|
|
private static final List<Player> trailPlayers = new ArrayList<>();
|
2013-08-10 19:50:45 +00:00
|
|
|
private static final Random RANDOM = new Random();
|
|
|
|
|
|
|
|
@Override
|
2015-11-22 18:26:47 +00:00
|
|
|
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
|
2013-08-10 19:50:45 +00:00
|
|
|
{
|
2014-05-05 13:31:12 +00:00
|
|
|
if (args.length > 0 && "off".equals(args[0]))
|
2013-08-10 19:50:45 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
trailPlayers.remove(playerSender);
|
2013-08-10 19:50:45 +00:00
|
|
|
|
2016-03-02 19:28:01 +00:00
|
|
|
msg("Trail disabled.");
|
2013-08-10 19:50:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
if (!trailPlayers.contains(playerSender))
|
2013-08-10 19:50:45 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
trailPlayers.add(playerSender);
|
2013-08-10 19:50:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 19:28:01 +00:00
|
|
|
msg("Trail enabled. Use \"/trail off\" to disable.");
|
2013-08-10 19:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-10-19 17:43:46 +00:00
|
|
|
DepreciationAggregator.setData_Block(fromBlock, (byte) RANDOM.nextInt(16));
|
2013-08-10 19:50:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 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;
|
|
|
|
}
|
2013-08-28 19:40:14 +00:00
|
|
|
|
2013-08-28 17:11:27 +00:00
|
|
|
public static void startTrail(Player player)
|
|
|
|
{
|
|
|
|
if (!trailPlayers.contains(player))
|
|
|
|
{
|
|
|
|
trailPlayers.add(player);
|
|
|
|
}
|
2013-08-28 19:40:14 +00:00
|
|
|
|
2013-08-28 17:11:27 +00:00
|
|
|
if (!trailPlayers.isEmpty())
|
|
|
|
{
|
|
|
|
registerMovementHandler();
|
|
|
|
}
|
|
|
|
}
|
2013-08-28 19:40:14 +00:00
|
|
|
|
2013-08-28 17:11:27 +00:00
|
|
|
public static void stopTrail(Player player)
|
|
|
|
{
|
|
|
|
trailPlayers.remove(player);
|
2013-08-28 19:40:14 +00:00
|
|
|
|
2013-08-28 17:11:27 +00:00
|
|
|
if (trailPlayers.isEmpty())
|
|
|
|
{
|
|
|
|
unregisterMovementHandler();
|
|
|
|
}
|
|
|
|
}
|
2013-08-10 19:50:45 +00:00
|
|
|
}
|