package me.ashtheking.currency;
import java.util.logging.Logger;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.event.*;
import org.bukkit.event.player.*;
import org.bukkit.entity.*;
import org.bukkit.*;
import org.bukkit.inventory.*;
import java.util.Random;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
import org.bukkit.plugin.Plugin;
import org.bukkit.command.*;
import java.io.*;
/**
@author ashtheking
*/
public class Currency extends JavaPlugin
{
public static PermissionHandler permissionHandler = null;
Logger log = Logger.getLogger("Minecraft");
public PropertyManager config = new PropertyManager("config.properties", this, "MultiCurrency: Config");
public String noPerms;
public void onEnable(){
PluginDescriptionFile descrip = getDescription();
log.info(descrip.getFullName() + " by " + descrip.getAuthors().toString());
CurrencyList.setPlugin(this);
PluginManager pm = getServer().getPluginManager();
MoneyListener money = new MoneyListener();
//pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, money, Event.Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_LOGIN, money, Event.Priority.Normal, this);
CurrencyList.loadAll();
setupPermissions();
noPerms = config.getStringProperty("noPermsMessage", "You don't have permissions to do this.");
}
public void onDisable(){
log.info("MultiCurrency Plugin deactivating.");
}
private void setupPermissions() {
if(! config.getBooleanProperty("usePermissions", true))
return;
if (permissionHandler != null) {
return;
}
Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
if (permissionsPlugin == null) {
log.warning("MultiCurrency has not found Permissions, and will now use OP.");
return;
}
permissionHandler = ((Permissions) permissionsPlugin).getHandler();
log.info("MultiCurrency has located Permissions and will use it.");
}
public static boolean checkPermission(Player player, String command, boolean op) {
if(permissionHandler == null)
{
if(op)
return player.isOp();
else
return true;
}
else
return permissionHandler.has(player, "ashtheking.multicurrency."+command);
}
public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] split){
String cmd = command.getName();
if(cmd.equalsIgnoreCase("create") && split.length > 1)
{
if(sender instanceof Player)
if(!(checkPermission((Player)sender, "create", true)))
{
sender.sendMessage(noPerms);
return true;
}
try
{
String currency = split[0].toLowerCase();
Material m = Material.matchMaterial(split[1].toUpperCase());
if(sender instanceof Player)
System.out.println("Created Currency: " + currency + " with item: " + m.name());
sender.sendMessage(ChatColor.BLUE + "Created Currency: " + currency + " with item: " + m.name());
if(m != null)
{
CurrencyList.addCurrency(currency, m.name());
return true;
}
else
sender.sendMessage(ChatColor.RED + "There is no material by the name of " + split[1] + ".");
}
catch(Exception e)
{
sender.sendMessage("Some error occurred. Please check your material name.");
}
}
if(cmd.equalsIgnoreCase("exchange") && split.length > 1)
{
if(sender instanceof Player)
if(!(checkPermission((Player)sender,"exchange", true)))
{
sender.sendMessage(noPerms);
return true;
}
String currency = split[0].toLowerCase();
int amount = Integer.parseInt(split[1]);
CurrencyList.setExchangeRate(currency, amount);
if(sender instanceof Player)
System.out.println("Set " + currency + " exchange rate to " + amount);
sender.sendMessage("Set " + currency + " exchange rate to " + amount);
return true;
}
if(cmd.equalsIgnoreCase("remove"))
{
if(sender instanceof Player)
if(!(checkPermission((Player)sender,"remove", true)))
{
sender.sendMessage(noPerms);
return true;
}
if(split.length > 1)
{
String currency = split[0].toLowerCase();
Player other = getServer().getPlayer(split[1]);
if(other != null)
{
CurrencyList.remove(other.getName(),currency);
sender.sendMessage("Nulled " + other.getName() + "'s account for currency: " + currency);
return true;
}
}
else if(split.length > 0)
{
Player other = getServer().getPlayer(split[0]);
String currency = (String)CurrencyList.maxCurrency(other.getName())[0];
if(other != null)
{
CurrencyList.remove(other.getName());
sender.sendMessage("Nulled " + other.getName() + "'s account for his maximum currency, which was " + currency);
return true;
}
}
}
//THIS IS THE END OF THE CONSOLE USABLE COMMANDS.
if(!(sender instanceof Player))
{
sender.sendMessage("You must be ingame to use these commands.");
return true;
}
Player player = (Player)sender;
if(cmd.equalsIgnoreCase("pay") && split.length > 2){
if(!(checkPermission(player,"pay", false)))
{
player.sendMessage(noPerms);
return true;
}
String playerName = split[0], currency = split[2].toLowerCase();
double amount = Double.parseDouble(split[1]);
if(player.getServer().getPlayer(playerName) != null)
if(CurrencyList.hasEnough(player.getName(), amount, currency))
{
Player other = player.getServer().getPlayer(playerName);
if(config.getBooleanProperty("requirePermsForEachCurrency", false))
if(!(checkPermission(other, "obtain."+currency, false)))
{
player.sendMessage(ChatColor.RED + "He is unable to recieve " + currency + ", due to " +
config.getStringProperty("unableToGetCurrencyMessage", "not having permissions for this currency."));
other.sendMessage(ChatColor.RED + "You are unable to recieve " + currency + ", due to " +
config.getStringProperty("unableToGetCurrencyMessage", "not having permissions for this currency."));
return true;
}
CurrencyList.subtract(player.getName(), amount, currency);
CurrencyList.add(other.getName(), amount, currency);
player.sendMessage(ChatColor.YELLOW + "You paid " + playerName + " " + amount + " " + currency);
other.sendMessage(ChatColor.YELLOW + "You recieved " + amount + " " + currency + " from " + player.getName());
return true;
}
}
if(cmd.equalsIgnoreCase("money") && split.length > 0)
{
if(!(checkPermission(player,"info", false)))
{
player.sendMessage(noPerms);
return true;
}
try
{
String currency = split[0].toLowerCase();
player.sendMessage(ChatColor.GREEN + "You have " + CurrencyList.getValue(currency,player.getName()) + " " + currency + ". " +
currency + " is traded in " + CurrencyList.getConvertMaterial(currency).toUpperCase() + " at a 1 : " + CurrencyList.getExchangeRate(currency) + " exchange rate.");
return true;
}
catch(NullPointerException npe)
{}
}
if(cmd.equalsIgnoreCase("convert") && split.length > 0)
{
if(!(checkPermission(player,"convert", false)))
{
player.sendMessage(noPerms);
return true;
}
String currency = split[0].toLowerCase();
int amount = 1;
if(split.length > 1)
try
{amount = Integer.parseInt(split[1]);}
catch(NumberFormatException nfe){}
if(!(CurrencyList.getConvertMaterial(currency).equals("NONE")))
{
Material m = Material.matchMaterial(CurrencyList.getConvertMaterial(currency).toUpperCase());
if(player.getInventory().contains(m,amount))
{
player.getInventory().removeItem(new ItemStack(m.getId(),amount));
CurrencyList.setValue(currency, player.getName(), CurrencyList.getValue(currency, player.getName()) + (amount * CurrencyList.getExchangeRate(currency)));
return true;
}
}
}
return false;
}
public class MoneyListener extends PlayerListener
{
public void onPlayerLogin(PlayerLoginEvent event) {
try
{
for(String key : CurrencyList.getAllCurrencys())
CurrencyList.getValue(key, event.getPlayer().getName());
}
catch(NullPointerException npe)
{}
}
}
}