package net.wiskr.simplefreeforall.commands;
import java.util.logging.Logger;
import net.wiskr.simplefreeforall.CommandManager;
import net.wiskr.simplefreeforall.configuration.Configuration;
import net.wiskr.simplefreeforall.configuration.Language;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Util
{
/**
* Checks if the command sender has a permission.
* @param node The permission node
* @return true is the sender has a permission, false if he does not
*/
public static boolean senderHasPermission(String node)
{
CommandSender sender = CommandManager.getSender();
boolean usePermissions = Configuration.getBoolean("configuration.use-permissions");
// Console always has the permissions
Player player;
if (isPlayer()) player = (Player) sender;
else return true;
// If permissions are not used, check for op
if(!usePermissions)
{
if(player.isOp()) return true;
else return false;
}
if(player.hasPermission("minereset." + node))
return true;
return false;
}
/**
* Checks if the command sender has a permission.
* @param node The permission node
* @return true is the sender has a permission, false if he does not
*/
public static boolean playerHasPermission(Player player, String node)
{
boolean usePermissions = Configuration.getBoolean("configuration.use-permissions");
if(!usePermissions)
{
if(player.isOp()) return true;
else return false;
}
if(player.hasPermission("minereset." + node))
return true;
return false;
}
/**
* Checks if the command sender is a player or a console
* @return true if sender is a player, false if not
*/
public static boolean isPlayer()
{
CommandSender sender = CommandManager.getSender();
if(sender instanceof Player) return true;
else return false;
}
/**
* Checks if debug mode is enabled in the config
* @return true if debug is enabled, false if it's not
*/
public static boolean debugEnabled()
{
if(Configuration.getBoolean("configuration.debug-mode")) return true;
else return false;
}
/**
* Sends a message back to the command sender
* @param message A message to be sent
*/
public static void sendMessage(String message)
{
CommandSender sender = CommandManager.getSender();
sender.sendMessage(message);
}
/**
* Sends a green-titled message to the command sender
* @param message A message to be sent
*/
public static void sendSuccess(String message)
{
CommandSender sender = CommandManager.getSender();
String title = Language.getString("general.title");
sender.sendMessage(ChatColor.GREEN + "[" + title + "] " + ChatColor.WHITE + message);
}
/**
* Sends a green-titled message to a player
* @param message A message to be sent
*/
public static void sendPlayerSuccess(Player player, String message)
{
String title = Language.getString("general.title");
player.sendMessage(ChatColor.GREEN + "[" + title + "] " + ChatColor.WHITE + message);
}
/**
* Sends a red-titled message to the command sender
* @param message A message to be sent
*/
public static void sendError(String message)
{
CommandSender sender = CommandManager.getSender();
String title = Language.getString("general.title");
sender.sendMessage(ChatColor.RED + "[" + title + "] " + ChatColor.WHITE + message);
}
/**
* Sends a red-titled message to a player
* @param message A message to be sent
*/
public static void sendPlayerError(Player player, String message)
{
String title = Language.getString("general.title");
player.sendMessage(ChatColor.RED + "[" + title + "] " + ChatColor.WHITE + message);
}
/**
* Message thrown when the command used by a player is invalid
* Also sends a command into the log
* @param command Command used
*/
public static void sendInvalid(Command cmd, String[] args)
{
CommandSender sender = CommandManager.getSender();
String title = Language.getString("general.title");
String message = Language.getString("general.invalid-command");
String command = cmd.getName();
for(int i = 0; i < args.length; i++)
command = command + " " + args[i];
log(sender.getName() + " sent an invalid command (/" + command + ")");
sender.sendMessage(ChatColor.RED + "[" + title + "] " + ChatColor.WHITE + message);
}
/**
* Message thrown when the user is denied of an action
* @param command Command used
*/
public static void sendDenied(String[] args)
{
CommandSender sender = CommandManager.getSender();
String title = Language.getString("general.title");
String message = Language.getString("general.access-denied");
String command = "";
for(int i = 0; i < args.length; i++)
command = command + " " + args[i];
log(sender.getName() + " was denied to use a command (/mine" + command + ")");
sender.sendMessage(ChatColor.RED + "[" + title + "] " + ChatColor.WHITE + message);
}
/**
* Sends a message into the server log
* @param message A message to be sent
*/
public static void log(String message)
{
Logger log = Bukkit.getServer().getLogger();
log.info(message);
}
/**
* Broadcasts a green-titled message to all players
* @param message A message to be sent
*/
public static void broadcastMessage(String message)
{
Bukkit.getServer().broadcastMessage(message);
return;
}
/**
* Broadcasts a green-titled message to all players
* This should be normally used just for the mine reset warnings
* @param message A message to be sent
*/
public static void broadcastSuccess(String message)
{
String title = Language.getString("general.title");
Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "[" + title + "] " + ChatColor.WHITE + message);
}
/**
* Broadcasts a red-titled message to all players
* This should not be used normally
* @param message A message to be sent
*/
public static void broadcastError(String message)
{
String title = Language.getString("general.title");
Bukkit.getServer().broadcastMessage(ChatColor.RED + "[" + title + "] " + ChatColor.WHITE + message);
}
/**
* Parses a block specified for a material
* @param blockName Name of a block
* @return Block material if it exists, null if it does not.
*/
public static int getBlockId(String blockName)
{
try
{
Material material;
if(isNumeric(blockName))
{
material = Material.getMaterial(Integer.parseInt(blockName));
}
else
{
material = Material.matchMaterial(blockName);
}
return material.getId();
}
catch(NumberFormatException nfe) { return -1; }
}
/**
* Checks if a string is numeric
* @param str String String to be checked
* @return boolean True if a string is numeric
*/
@SuppressWarnings("unused")
public static boolean isNumeric(String str)
{
try
{ double d = Double.parseDouble(str); }
catch(NumberFormatException nfe)
{ return false; }
return true;
}
/**
* Checks if the mine exists
* @param name Name of the mine being checked
* @return True if the mine exists, False if it does not
*/
public static boolean arenaExists(String name)
{
return true;
//List<String> mineList = Data.getList("data.list-of-arenas");
//if(mineList.indexOf(name) == -1) return false;
//else return true;
}
/**
* Replaces the specified value in the string provided with the new value
* @param str String to parse
* @param replaceFrom Value to be replaced
* @param replaceTo Value to be substituted
* @return A new String with necessary values substituted
*/
public static String parseString(String str, String replaceFrom, String replaceTo)
{
str = str.replaceAll(replaceFrom, replaceTo);
return str;
}
}