PageRenderTime 70ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/oberonserver/timerank/timerank.java

https://github.com/Specops343/Time-Rank
Java | 1404 lines | 1242 code | 106 blank | 56 comment | 231 complexity | 21afed40578de97327bfadf34aad508f MD5 | raw file
  1. package com.oberonserver.timerank;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.io.PrintWriter;
  10. import java.io.StringWriter;
  11. import java.lang.reflect.Field;
  12. import java.text.ParseException;
  13. import java.text.SimpleDateFormat;
  14. import java.util.Date;
  15. import java.util.HashMap;
  16. import java.util.HashSet;
  17. import java.util.Iterator;
  18. import java.util.LinkedHashMap;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.Properties;
  23. import java.util.logging.Logger;
  24. import org.bukkit.Material;
  25. import org.bukkit.command.Command;
  26. import org.bukkit.command.CommandSender;
  27. import org.bukkit.entity.Player;
  28. import org.bukkit.event.Event;
  29. import org.bukkit.inventory.ItemStack;
  30. import org.bukkit.plugin.Plugin;
  31. import org.bukkit.plugin.PluginManager;
  32. import org.bukkit.plugin.java.JavaPlugin;
  33. import org.bukkit.util.config.Configuration;
  34. import com.nijiko.permissions.PermissionHandler;
  35. import com.nijikokun.bukkit.Permissions.Permissions;
  36. import com.nijikokun.timerank.register.payment.Method;
  37. import com.oberonserver.perms.PermMethod;
  38. public class timerank extends JavaPlugin
  39. {
  40. private boolean debug = false;
  41. static String mainDirectory = "plugins/TimeRank";
  42. static File times = new File(mainDirectory+File.separator+"Time.dat");
  43. public Logger log = Logger.getLogger("Minecraft");
  44. public PermissionHandler permissionHandler;
  45. boolean UsePermissions = false;
  46. private final TimeRankPlayerListener playerListener = new TimeRankPlayerListener(this);
  47. private final TimeRankWorldListener worldListener = new TimeRankWorldListener(this);
  48. private final TimeRankServerListener serverListener = new TimeRankServerListener(this);
  49. public Map<String, Long> StartTime = new HashMap<String, Long>();
  50. public Map<String, Long> PlayTime = new HashMap<String, Long>();
  51. public List<PurchasedAbility> RentedAbilities;
  52. public List<PurchasedRank> RentedRanks;
  53. public Map<Rank, Long> Ranks = new LinkedHashMap<Rank, Long>();
  54. public Map<Ability, Long> Abilities = new LinkedHashMap<Ability,Long>();
  55. public Method Method = null;
  56. public PermMethod perms;
  57. public String permissions = "Permissions3";
  58. private boolean hideUnavaible=false;
  59. private TimeRankChecker checker;
  60. private int checkDelay=5*20;
  61. private int checkInterval=10*20;
  62. public Configuration config;
  63. public void onEnable(){
  64. //Get the plugin managor
  65. PluginManager pm = this.getServer().getPluginManager();
  66. //register our events
  67. pm.registerEvent(Event.Type.PLAYER_JOIN , playerListener, Event.Priority.Monitor , this);
  68. pm.registerEvent(Event.Type.PLAYER_QUIT , playerListener, Event.Priority.Monitor , this);
  69. pm.registerEvent(Event.Type.WORLD_SAVE, worldListener, Event.Priority.Monitor, this);
  70. pm.registerEvent(Event.Type.PLUGIN_ENABLE, serverListener, Event.Priority.Monitor, this);
  71. pm.registerEvent(Event.Type.PLUGIN_DISABLE, serverListener, Event.Priority.Monitor, this);
  72. //Make our data folder and try and prepare the config file.
  73. new File(mainDirectory).mkdir();
  74. new File(mainDirectory+File.separator+"data").mkdir();
  75. //loadPlaytime();
  76. config = getConfiguration();
  77. loadConfig();
  78. //setupPermissions();
  79. //Get all players online and give them a start time. This is in case we are disabled/enabled after the server starts
  80. for(Player p : getServer().getOnlinePlayers())
  81. {
  82. long now = System.currentTimeMillis();
  83. StartTime.put(p.getName(),now);
  84. loadPlaytime(p.getName());
  85. }
  86. //Schedule our check event
  87. checker = new TimeRankChecker(this, checkInterval);
  88. getServer().getScheduler().scheduleSyncRepeatingTask(this, checker, checkDelay, checkInterval);
  89. //Load what abilites were rented last time we were closed
  90. RentedAbilities = new LinkedList<PurchasedAbility>();
  91. RentedRanks = new LinkedList<PurchasedRank>();
  92. loadRent();
  93. //All went well.
  94. log.info("[Time Rank] Version " + this.getDescription().getVersion() + " Enabled.");
  95. }
  96. public void onDisable(){
  97. //Save play time and clear variables
  98. savePlaytime();
  99. saveRent();
  100. permissionHandler=null;
  101. Ranks.clear();
  102. Abilities.clear();
  103. StartTime.clear();
  104. PlayTime.clear();
  105. RentedAbilities.clear();
  106. RentedRanks.clear();
  107. //stop our scheduled rank checker
  108. getServer().getScheduler().cancelTasks(this);
  109. log.info("[Time Rank] Disabled.");
  110. }
  111. private void loadConfig()
  112. {
  113. try
  114. {
  115. config.load();
  116. //check for old config.
  117. int cv=config.getInt("settings.configVersion", 1);
  118. if (cv==1)
  119. {
  120. log.info("[TimeRank] Old config version " + cv + " detected. Updating to new config format.");
  121. updateConfig();
  122. config.load();
  123. Ranks.clear();
  124. }
  125. debug = config.getBoolean("settings.debug",false);
  126. hideUnavaible = config.getBoolean("settings.hideUnavaible",false);
  127. List<String> keys = config.getKeys("ranks");
  128. DebugPrint("Ranks key size "+Integer.toString(keys.size()));
  129. //load config
  130. for(String key : keys)
  131. {
  132. String node="ranks."+key;
  133. String sGroup = config.getString(node+".group");
  134. String sOldGroup = config.getString(node+".oldgroup","");
  135. String sWorld = config.getString(node+".world","*");
  136. boolean remove = config.getBoolean(node+".remove", false);
  137. int iTime = config.getInt(node+".time",-1);
  138. long lTime = (long)iTime * 60 * 1000;
  139. GenericGroup group = new GenericGroup(sWorld,sGroup);;
  140. GenericGroup gOldGroup=null;
  141. if (sOldGroup != "")
  142. gOldGroup = new GenericGroup(sWorld,sOldGroup);
  143. Rank rank = new Rank(key, group, gOldGroup, remove);
  144. rank.name=key;
  145. rank.time = lTime;
  146. if (config.getString(node+".buy.cost","").equalsIgnoreCase("money"))
  147. rank.cost=0;
  148. else
  149. rank.cost = config.getInt(node+".buy.cost", -1);
  150. if (rank.cost != -1)
  151. {
  152. rank.amount = config.getDouble(node+".buy.amount", 1);
  153. rank.minTime = config.getInt(node+".buy.minTime", -1)*60*1000;
  154. rank.broadcast = config.getBoolean(node+".buy.broadcast", true);
  155. rank.msg = config.getString(node+".buy.msg", "&B<player.name> &Ehas been promoted to &B<rank.group>");
  156. } else {
  157. config.removeProperty(node+".buy.cost");
  158. config.removeProperty(node+".buy");
  159. }
  160. //Rent stuff
  161. if (config.getString(node+".rent.cost","").equalsIgnoreCase("money"))
  162. rank.rentCost=0;
  163. else
  164. rank.rentCost = config.getInt(node+".rent.cost", -1);
  165. if (rank.rentCost != -1)
  166. {
  167. rank.rentMinTime = config.getInt(node+".rent.minTime", -1)*60*1000;
  168. rank.rentAmount = config.getDouble(node+".rent.amount", 1);
  169. rank.rentBroadcast = config.getBoolean(node+".rent.broadcast", true);
  170. rank.rentGainedMsg = config.getString(node+".rent.gainedMsg", "&B<player.name> &Ehas been promoted to &B<rank.group>");
  171. rank.rentLostMsg = config.getString(node+".rent.lostMsg", "&B<player.name> &Ehas been demoted from &B<rank.group> &Eto &B<rank.oldgroup>.");
  172. iTime = config.getInt(node+".rent.time",-1);
  173. lTime = (long)iTime * 60 * 1000;
  174. rank.rentTime = lTime;
  175. } else {
  176. config.removeProperty(node+".rent.cost");
  177. config.removeProperty(node+".rent");
  178. }
  179. rank.desc = config.getString(node+".description", "");
  180. Ranks.put(rank, lTime);
  181. DebugPrint("Loaded " + rank.name + " with group " + rank.GetGroup().getName() + " in world " + rank.GetGroup().getWorld());
  182. }
  183. //Load abilities node
  184. keys = config.getKeys("abilities");
  185. if (keys != null)
  186. {
  187. DebugPrint("Abilities key size "+Integer.toString(keys.size()));
  188. List<String> emptyNodes = new LinkedList<String>();
  189. emptyNodes.add("timerank.none");
  190. List<String> emptyCat = new LinkedList<String>();
  191. emptyCat.add("Uncategorized");
  192. for(String key : keys)
  193. {
  194. String node="abilities."+key;
  195. String sWorld = config.getString(node+".world","*");
  196. int iTime = config.getInt(node+".time",-1);
  197. long lTime = (long)iTime * 60 * 1000;
  198. Ability ability = new Ability();
  199. ability.world=sWorld;
  200. ability.name=key;
  201. ability.permission = config.getString(node+".permission", "timerank.ab");
  202. ability.Nodes = config.getStringList(node+".nodes", emptyNodes);
  203. ability.Categories = config.getStringList(node+".categories", emptyCat);
  204. ability.time = lTime;
  205. if (config.getString(node+".buy.cost","").equalsIgnoreCase("money"))
  206. ability.cost=0;
  207. else
  208. ability.cost = config.getInt(node+".buy.cost", -1);
  209. if (ability.cost != -1)
  210. {
  211. ability.amount = config.getDouble(node+".buy.amount", 1);
  212. ability.minTime = config.getInt(node+".buy.minTime", -1)*60*1000;
  213. ability.broadcast = config.getBoolean(node+".buy.broadcast", true);
  214. ability.msg = config.getString(node+".buy.msg", "&B<player.name> &Ehas bought &B<ability.name>");
  215. } else {
  216. config.removeProperty(node+".buy.cost");
  217. config.removeProperty(node+".buy");
  218. }
  219. //Rent stuff
  220. if (config.getString(node+".rent.cost","").equalsIgnoreCase("money"))
  221. ability.rentCost=0;
  222. else
  223. ability.rentCost = config.getInt(node+".rent.cost", -1);
  224. if (ability.rentCost != -1)
  225. {
  226. ability.rentMinTime = config.getInt(node+".rent.minTime", -1)*60*1000;
  227. ability.rentAmount = config.getDouble(node+".rent.amount", 1);
  228. ability.rentBroadcast = config.getBoolean(node+".rent.broadcast", true);
  229. ability.rentGainedMsg = config.getString(node+".rent.gainedMsg", "&B<player.name> &Ehas rented &B<ability.name>");
  230. ability.rentLostMsg = config.getString(node+".rent.lostMsg", "&B<player.name> &Ehas lost the &B<ability.name> ability");
  231. iTime = config.getInt(node+".rent.time",-1);
  232. lTime = (long)iTime * 60 * 1000;
  233. ability.rentTime = lTime;
  234. } else {
  235. config.removeProperty(node+".rent.cost");
  236. config.removeProperty(node+".rent");
  237. }
  238. ability.desc = config.getString(node+".description", "");
  239. Abilities.put(ability, lTime);
  240. DebugPrint("Loaded " + ability.name + " with permissions " + "" + " in world " + ability.world);
  241. }
  242. }else
  243. {
  244. DebugPrint("No abilities found.");
  245. }
  246. }catch(Exception e){
  247. ThrowSimpleError(e);
  248. }
  249. }
  250. private void updateConfig()
  251. {//read in the old config and output a new one.
  252. config.load();
  253. debug = config.getBoolean("settings.debug",false);
  254. hideUnavaible = config.getBoolean("settings.hideUnavaible",false);
  255. List<String> keys = config.getKeys("ranks");
  256. DebugPrint("Keys size "+Integer.toString(keys.size()));
  257. //load old config
  258. for(String key : keys)
  259. {
  260. String node="ranks."+key;
  261. String sGroup = config.getString(node+".group");
  262. String sOldGroup = config.getString(node+".oldgroup","");
  263. String sWorld = config.getString(node+".world","*");
  264. boolean remove = config.getBoolean(node+".remove", false);
  265. int iTime = config.getInt(node+".time",-1);
  266. long lTime = (long)iTime * 60 * 1000;
  267. GenericGroup group = new GenericGroup(sWorld,sGroup);;
  268. GenericGroup gOldGroup=null;
  269. if (sOldGroup != "")
  270. gOldGroup = new GenericGroup(sWorld,sOldGroup);
  271. Rank rank = new Rank(key, group, gOldGroup, remove);
  272. rank.name=key;
  273. rank.time = lTime;
  274. if (config.getString(node+".cost","").equalsIgnoreCase("money"))
  275. rank.cost=0;
  276. else
  277. rank.cost = config.getInt(node+".cost", -1);
  278. if (rank.cost != -1)
  279. {
  280. rank.amount = config.getDouble(node+".amount", 1);
  281. rank.minTime = config.getInt(node+".minTime", -1)*60*1000;
  282. rank.broadcast = config.getBoolean(node+".broadcast", true);
  283. rank.msg = config.getString(node+".msg", "&B<player.name> &Ehas been promoted to &B<rank.group>");
  284. } else {
  285. config.removeProperty(node+".cost");
  286. }
  287. //Rent stuff
  288. if (config.getString(node+".rentCost","").equalsIgnoreCase("money"))
  289. rank.rentCost=0;
  290. else
  291. rank.rentCost = config.getInt(node+".rentCost", -1);
  292. if (rank.rentCost != -1)
  293. {
  294. rank.rentMinTime = config.getInt(node+".rentMinTime", -1)*60*1000;
  295. rank.rentAmount = config.getDouble(node+".rentAmount", 1);
  296. rank.rentBroadcast = config.getBoolean(node+".rentBroadcast", true);
  297. rank.rentGainedMsg = config.getString(node+".rentGainedMsg", "&B<player.name> &Ehas been promoted to &B<rank.group>");
  298. rank.rentLostMsg = config.getString(node+".rentLostMsg", "&B<player.name> &Ehas been demoted from &B<rank.group> &Eto &B<rank.oldgroup>.");
  299. }
  300. else
  301. {
  302. config.removeProperty(node+".rentCost");
  303. }
  304. iTime = config.getInt(node+".rentTime",-1);
  305. lTime = (long)iTime * 60 * 1000;
  306. rank.rentTime = lTime;
  307. rank.desc = config.getString(node+".description", "");
  308. Ranks.put(rank, lTime);
  309. DebugPrint("Loaded " + rank.name + " with group " + rank.GetGroup().getName() + " in world " + rank.GetGroup().getWorld());
  310. //Remove old nods
  311. for(String remNode : config.getKeys(node))
  312. {//loop though all the nodes.
  313. DebugPrint("Removing old node: "+node+"."+remNode);
  314. config.removeProperty(node+"."+remNode);
  315. }
  316. }
  317. saveConfig();
  318. }
  319. private void saveConfig()
  320. {//save all our settings to the config file.
  321. config.setProperty("settings.debug", debug);
  322. config.setProperty("settings.hideUnavaible", hideUnavaible);
  323. config.setProperty("settings.configVersion", 2);
  324. for(Rank rank : Ranks.keySet())
  325. {
  326. String node="ranks."+rank.name;
  327. config.setProperty(node+".group", rank.GetGroup().getName());
  328. config.setProperty(node+".world", rank.GetGroup().getWorld());
  329. config.setProperty(node+".oldgroup", rank.GetOldGroup().getName());
  330. config.setProperty(node+".time", rank.time/60/1000);
  331. if (rank.cost > -1)
  332. {
  333. if (rank.cost>0)
  334. config.setProperty(node+".buy.cost", rank.cost);
  335. else if (rank.cost==0)
  336. config.setProperty(node+".buy.cost", "Money");
  337. config.setProperty(node+".buy.amount", rank.amount);
  338. if (rank.minTime>0) config.setProperty(node+".buy.minTime", rank.minTime/60/1000);
  339. config.setProperty(node+".buy.broadcast", rank.broadcast);
  340. config.setProperty(node+".buy.msg", rank.msg);
  341. }
  342. if (rank.rentCost > -1)
  343. {
  344. DebugPrint("rentCost is "+rank.rentCost+" for "+rank.name);
  345. if (rank.rentCost>0)
  346. config.setProperty(node+".rent.cost", rank.rentCost);
  347. else if (rank.rentCost==0)
  348. config.setProperty(node+".rent.cost", "Money");
  349. if (rank.rentMinTime>0) config.setProperty(node+".rent.minTime", rank.rentMinTime/60/1000);
  350. config.setProperty(node+".rent.amount", rank.rentAmount);
  351. config.setProperty(node+".rent.broadcast", rank.rentBroadcast);
  352. config.setProperty(node+".rent.gainedMessage", rank.rentGainedMsg);
  353. config.setProperty(node+".rent.lostMessage", rank.rentLostMsg);
  354. config.setProperty(node+".rent.time", rank.rentTime/60/1000);
  355. }
  356. config.setProperty(node+".description", rank.desc);
  357. }
  358. config.save();
  359. }
  360. public void setupPermissions()
  361. {
  362. //Get the permissions plugin.
  363. Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
  364. if (permissionHandler == null) {//check if we already have a handler
  365. if (permissionsPlugin != null) {//Make sure we found a permissions plugin
  366. permissionHandler = ((Permissions) permissionsPlugin).getHandler();
  367. UsePermissions = true;
  368. if (permissions.equalsIgnoreCase("Permissions3"))
  369. {//Use permissions 3.x
  370. try
  371. {
  372. perms = new com.oberonserver.perms.methods.Perm3(this);
  373. log.info("[Time Rank] Using Permissions 3.x");
  374. }catch(Exception e){
  375. PluginManager pm = this.getServer().getPluginManager();
  376. Map<String,String>ErrorInfo = new LinkedHashMap<String,String>();
  377. ErrorInfo.put("Error message:", "Set to use Permissions 3.x but something went wrong.");
  378. ErrorInfo.put("Depend", pm.getPlugin("Permissions").getDescription().getDepend().toString());
  379. ErrorInfo.put("Permissions version", pm.getPlugin("Permissions").getDescription().getVersion().toString());
  380. ErrorLog(ErrorInfo);
  381. }
  382. }
  383. else if (permissions.equalsIgnoreCase("GroupManager"))
  384. {//Use group manager
  385. Plugin gm = this.getServer().getPluginManager().getPlugin("GroupManager");
  386. perms = new com.oberonserver.perms.methods.GM(this,gm);
  387. log.info("[Time Rank] Using permissions GroupManger");
  388. }
  389. } else {
  390. System.out.println("[Time Rank] Permission system not detected. Something went wrong.");
  391. System.out.println("[Time Rank] Make sure you are using Permisions 3.x or GroupManager.");
  392. }
  393. }
  394. }
  395. @Override
  396. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
  397. {
  398. try
  399. {
  400. Player player = null;
  401. //String[] split = args;
  402. if(sender instanceof Player)
  403. {
  404. player = (Player) sender;
  405. //Check permissions. Node should be timerank.<command name>
  406. if(!perms.HasPermission(player,"timerank."+cmd.getName()))
  407. {
  408. if(sender instanceof Player)
  409. sender.sendMessage("§4You do not have permission to run this command");
  410. return true;
  411. }
  412. }
  413. //We have permission to run the command.
  414. if (cmd.getName().equalsIgnoreCase("playtime")) //Check which command we ran.
  415. {
  416. String playername="";
  417. if(sender instanceof Player)
  418. playername = player.getDisplayName();
  419. if (args.length > 0)
  420. playername = args[0];
  421. for(String p : PlayTime.keySet())
  422. {
  423. if (p.equalsIgnoreCase(playername))
  424. {//Name or display name matches
  425. if(sender instanceof Player)
  426. player.sendMessage(p + " has been playing for " + Mills2Time(GetPlaytime(p)));
  427. else
  428. log.info(p + " has been playing for " + Mills2Time(GetPlaytime(p)));
  429. return true;
  430. }
  431. }
  432. //Player not found in list see if it is there first connect.
  433. for(String p : StartTime.keySet())
  434. {
  435. if (p.equalsIgnoreCase(playername))
  436. {//Name or display name matches
  437. sender.sendMessage(p + " has been playing for " + Mills2Time(GetPlaytime(p)));
  438. return true;
  439. }
  440. }
  441. //see if they are just not loaded yet.
  442. File path = new File(mainDirectory+File.separator+"data"+File.separator+playername);
  443. if (path.exists())
  444. {
  445. loadPlaytime(playername);
  446. sender.sendMessage(playername + " has been playing for " + Mills2Time(GetPlaytime(playername)));
  447. }
  448. sender.sendMessage(playername + " could not be found");
  449. }
  450. else if (cmd.getName().equalsIgnoreCase("checkranks"))
  451. {
  452. sender.sendMessage("Promoted " + CheckRanks(getServer().getOnlinePlayers()) + " people.");
  453. return true;
  454. }
  455. else if (cmd.getName().equalsIgnoreCase("buyrank"))
  456. {
  457. //Make sure a player is running the command.
  458. if(!(sender instanceof Player))
  459. {
  460. log.info("This command must be run in game.");
  461. return false;
  462. }
  463. if (args.length < 1)
  464. return false;
  465. //Buy the rank
  466. String rankname = args[0];
  467. DebugPrint(player.getName() + " is trying to buy "+rankname);
  468. BuyRank(player,rankname);
  469. return true;
  470. }
  471. else if (cmd.getName().equalsIgnoreCase("rentrank"))
  472. {
  473. //Make sure it is a player
  474. if(!(sender instanceof Player))
  475. {
  476. log.info("This command must be run in game.");
  477. return false;
  478. }
  479. if (args.length < 1)
  480. return false;
  481. //Rent out the rank
  482. String rankname = args[0];
  483. DebugPrint(player.getName() + " is trying to rent "+rankname);
  484. RentRank(player,rankname);
  485. return true;
  486. }
  487. else if (cmd.getName().equalsIgnoreCase("listranks"))
  488. {
  489. //Set up the filter and page info
  490. String sCmd ="";
  491. int iPage=-1;
  492. if (args.length > 0)
  493. if (!isParsableToInt(args[0]))
  494. sCmd = args[0];
  495. else
  496. iPage = Integer.parseInt(args[0]);
  497. if ((args.length > 1) && (isParsableToInt(args[1])))
  498. iPage = Integer.parseInt(args[1]);
  499. //iPage -= 1;
  500. int perPage = 5;
  501. int curItem = -1;
  502. int startItem = ((iPage-1) * perPage);
  503. sender.sendMessage("§B---------------Rank List---------------");
  504. for(Rank r : Ranks.keySet())
  505. {
  506. //check to see if we hide groups we can't get.
  507. if ( hideUnavaible )
  508. if (!perms.inGroup(player,r.GetOldGroup().getWorld(), r.GetOldGroup().getName())&& !r.GetOldGroup().getName().equals(""))
  509. {
  510. DebugPrint("Hidding " + r.name + " from " + player.getName());
  511. continue;
  512. }
  513. //Check filters
  514. if (sCmd.equalsIgnoreCase("time"))
  515. if (r.time<=0)
  516. continue;
  517. if (sCmd.equalsIgnoreCase("buy"))
  518. if (r.cost<0)
  519. continue;
  520. if (sCmd.equalsIgnoreCase("rent"))
  521. if (r.rentCost<0)
  522. continue;
  523. //We are showing this one, so update page info
  524. curItem +=1;
  525. if (iPage >= 0)
  526. {
  527. if (curItem <= startItem)
  528. continue;
  529. if (curItem > iPage * perPage)
  530. continue;
  531. }
  532. //Build the rank info string and display it
  533. String msg="§A"+r.name + " - ";
  534. if (r.time>0)
  535. msg+="§BTime: §A" + Mills2Time(r.time) + " ";
  536. if (r.cost>0)
  537. msg+="§BBuy: §A" + r.amount+ " " + Material.getMaterial(r.cost)+ " ";
  538. if (r.cost==0)
  539. msg+="§BBuy: §A" + Method.format(r.amount)+ " ";
  540. if (r.rentCost>0)
  541. msg+="§BRent: §A" + r.rentAmount+ " " + Material.getMaterial(r.rentCost)+ " ";
  542. if (r.rentCost==0)
  543. msg+="§BRent: §A" + Method.format(r.rentAmount)+ " ";
  544. if (r.GetOldGroup() != null)
  545. msg+="§BRequires group: §A" + r.GetOldGroup().getName()+ " ";
  546. sender.sendMessage(msg);
  547. if (r.desc != "")
  548. sender.sendMessage("§BDescription: §A"+r.desc);
  549. }
  550. sender.sendMessage("§B-----------------------------------------");
  551. return true;
  552. }
  553. else if (cmd.getName().equalsIgnoreCase("buyab"))
  554. {
  555. //Make sure a player is running the command.
  556. if(!(sender instanceof Player))
  557. {
  558. log.info("This command must be run in game.");
  559. return false;
  560. }
  561. if (args.length < 1)
  562. return false;
  563. //Buy the ability
  564. String abilityname = args[0];
  565. DebugPrint(player.getName() + " is trying to buy "+abilityname);
  566. BuyAbility(player,abilityname);
  567. return true;
  568. }
  569. else if (cmd.getName().equalsIgnoreCase("rentab"))
  570. {
  571. //Make sure it is a player
  572. if(!(sender instanceof Player))
  573. {
  574. log.info("This command must be run in game.");
  575. return false;
  576. }
  577. if (args.length < 1)
  578. return false;
  579. //Rent out the ability
  580. String abilityname = args[0];
  581. DebugPrint(player.getName() + " is trying to rent "+abilityname);
  582. RentAbility(player,abilityname);
  583. return true;
  584. }
  585. else if (cmd.getName().equalsIgnoreCase("listabs"))
  586. {
  587. //Set up the filter and page info
  588. String sCmd ="";
  589. int iPage=-1;
  590. if (args.length > 0)
  591. if (!isParsableToInt(args[0]))
  592. sCmd = args[0];
  593. else
  594. iPage = Integer.parseInt(args[0]);
  595. if ((args.length > 1) && (isParsableToInt(args[1])))
  596. iPage = Integer.parseInt(args[1]);
  597. //iPage -= 1;
  598. int perPage = 5;
  599. int curItem = -1;
  600. int startItem = ((iPage-1) * perPage);
  601. String catFilter="";
  602. //Build a list of categories.
  603. HashSet<String> cats =new HashSet<String>();
  604. for(Ability ab : Abilities.keySet())
  605. {
  606. for(String cat : ab.Categories)
  607. {
  608. cats.add(cat);
  609. if (sCmd.equalsIgnoreCase(cat))
  610. catFilter=cat;
  611. }
  612. }
  613. if (sCmd.equalsIgnoreCase("cats"))
  614. {//special case. We only want the categories.
  615. HashSet<String> catlist =new HashSet<String>();
  616. for(String cat : cats)
  617. {//color the category list before we display it.
  618. catlist.add("§A"+cat+"§F");
  619. }
  620. sender.sendMessage("§B--------Abilities Categories--------");
  621. sender.sendMessage("Categories: "+catlist.toString());
  622. sender.sendMessage("§B-----------------------------------------");
  623. return true;
  624. }
  625. sender.sendMessage("§B---------------Abilities List---------------");
  626. for(Ability ab : Abilities.keySet())
  627. {
  628. //check to see if we hide groups we can't get.
  629. if ( hideUnavaible )
  630. if (!perms.HasPermission(player, ab.permission, player.getWorld().getName()))
  631. {
  632. DebugPrint("Hidding " + ab.name + " from " + player.getName());
  633. continue;
  634. }
  635. //Check filters
  636. if (sCmd.equalsIgnoreCase("time"))
  637. if (ab.time<=0)
  638. continue;
  639. if (sCmd.equalsIgnoreCase("buy"))
  640. if (ab.cost<0)
  641. continue;
  642. if (sCmd.equalsIgnoreCase("rent"))
  643. if (ab.rentCost<0)
  644. continue;
  645. if (catFilter!="") //we are filtering by category.
  646. if (!ab.Categories.contains(catFilter))
  647. continue;
  648. //We are showing this one, so update page info
  649. curItem +=1;
  650. if (iPage >= 0)
  651. {
  652. if (curItem <= startItem)
  653. continue;
  654. if (curItem > iPage * perPage)
  655. continue;
  656. }
  657. //Build the rank info string and display it
  658. String msg="§A"+ab.name + " - ";
  659. if (ab.time>0)
  660. msg+="§BTime: §A" + Mills2Time(ab.time) + " ";
  661. if (ab.cost>0)
  662. msg+="§BBuy: §A" + ab.amount+ " " + Material.getMaterial(ab.cost)+ " ";
  663. if (ab.cost==0)
  664. msg+="§BBuy: §A" + Method.format(ab.amount)+ " ";
  665. if (ab.rentCost>0)
  666. msg+="§BRent: §A" + ab.rentAmount+ " " + Material.getMaterial(ab.rentCost)+ " ";
  667. if (ab.rentCost==0)
  668. msg+="§BRent: §A" + Method.format(ab.rentAmount)+ " ";
  669. sender.sendMessage(msg);
  670. if (ab.desc != "")
  671. sender.sendMessage("§BDescription: §A"+ab.desc);
  672. msg="";
  673. HashSet<String> catlist =new HashSet<String>();
  674. for(String cat : ab.Categories )
  675. {//color the category list before we display it.
  676. catlist.add("§A"+cat+"§B");
  677. }
  678. sender.sendMessage("§BCategories: "+catlist.toString());
  679. }
  680. sender.sendMessage("§B-----------------------------------------");
  681. return true;
  682. }
  683. else if (cmd.getName().equalsIgnoreCase("timerank"))
  684. {
  685. //Check for no args. If no args, display basic info
  686. if (args.length<1)
  687. {
  688. sender.sendMessage("§B---------------Time Rank---------------");
  689. sender.sendMessage("§BVersion: §A"+this.getDescription().getVersion());
  690. sender.sendMessage("§BDebug: §A"+this.debug);
  691. sender.sendMessage("§BPermissions: §A"+this.permissions);
  692. sender.sendMessage("§BHide Unavaible: §A"+this.hideUnavaible );
  693. sender.sendMessage("§B-----------------------------------------");
  694. return true;
  695. }
  696. else if (args[0].equalsIgnoreCase("reload"))
  697. {//Reload the config file.
  698. Ranks.clear();
  699. Ranks = new HashMap<Rank, Long>();
  700. loadConfig();
  701. sender.sendMessage("§B[TimeRank] Timerank has been reloaded.");
  702. return true;
  703. }
  704. else if (args[0].equalsIgnoreCase("groups"))
  705. {//Show advanced listing of the groups.
  706. String sCmd ="";
  707. if (args.length > 1)
  708. sCmd = args[1];
  709. sender.sendMessage("§B-----------Advanced Rank List-----------");
  710. for(Rank r : Ranks.keySet())
  711. {
  712. if (sCmd.equalsIgnoreCase("time"))
  713. if (r.time<=0)
  714. continue;
  715. if (sCmd.equalsIgnoreCase("buy"))
  716. if (r.cost<0)
  717. continue;
  718. if (sCmd.equalsIgnoreCase("rent"))
  719. if (r.rentCost<0)
  720. continue;
  721. String msg="§A"+r.name + " - ";
  722. if (r.time>0)
  723. msg+="§BTime: §A" +Mills2Time(r.time) + " ";
  724. if (r.cost>0)
  725. msg+="§BCost: §A" + r.amount+ " " + Material.getMaterial(r.cost)+ " ";
  726. if (r.cost==0)
  727. msg+="§BCost: §A" + Method.format(r.amount)+ " ";
  728. msg+="§BGroup: §A" + r.GetGroup().getName()+ " ";
  729. if (r.GetOldGroup() != null)
  730. msg+="§BRequires group: §A" + r.GetOldGroup().getName()+ " ";
  731. sender.sendMessage(msg);
  732. }
  733. sender.sendMessage("§B-----------------------------------------");
  734. return true;
  735. }
  736. else if (args[0].equalsIgnoreCase("group"))
  737. {//show a lot of info about 1 group.
  738. if (args.length < 2)
  739. {
  740. sender.sendMessage("§CUseage: /timerank group <name>");
  741. return true;
  742. }
  743. sender.sendMessage("§B-----------Advanced Rank List-----------");
  744. for(Rank r : Ranks.keySet())
  745. {
  746. if (r.name.equalsIgnoreCase(args[1]))
  747. {
  748. String msg="§A"+r.name + " - ";
  749. if (r.time>0)
  750. msg+="§BTime: §A" + Mills2Time(r.time) + " ";
  751. if (r.cost>0)
  752. msg+="§BCost: §A" + r.amount+ " " + Material.getMaterial(r.cost)+ " ";
  753. if (r.cost==0)
  754. msg+="§BCost: §A" + Method.format(r.amount)+ " ";
  755. msg+="§BGroup: §A" + r.GetGroup().getName()+ " ";
  756. if (r.GetOldGroup() != null)
  757. msg+="§BRequires group: §A" + r.GetOldGroup().getName()+ " ";
  758. sender.sendMessage(msg);
  759. }
  760. sender.sendMessage("§B-----------------------------------------");
  761. return true;
  762. }
  763. }
  764. else if (args[0].equalsIgnoreCase("set"))
  765. {//change a config variable
  766. if (args.length < 3)
  767. {//make sure we have at least 3 args
  768. sender.sendMessage("§CUseage: /timerank set <setting> <value>");
  769. return true;
  770. }
  771. if (args[1].equalsIgnoreCase("debug"))
  772. {//Set debug value
  773. if (args[2].equalsIgnoreCase("true"))
  774. debug=true;
  775. else
  776. debug=false;
  777. sender.sendMessage("§AConfig file updated.");
  778. saveConfig();
  779. }
  780. return true;
  781. }
  782. else if (args[0].equalsIgnoreCase("test"))
  783. {//Test command. This changes A LOT and can/will do whatever I'm trying to figure out at the moment.
  784. Class<Rank> rClass = Rank.class;
  785. Field[] methods = rClass.getFields();
  786. for(Field f : methods)
  787. {
  788. for(Rank r : Ranks.keySet())
  789. {
  790. try {
  791. DebugPrint(r.name + ":" + f.getName() + " = " + f.get(r));
  792. } catch (IllegalArgumentException e) {
  793. ThrowSimpleError(e);
  794. } catch (IllegalAccessException e) {
  795. ThrowSimpleError(e);
  796. }
  797. }
  798. }
  799. return true;
  800. }
  801. }
  802. }catch(Exception e)
  803. {//Oops. Dump a bunch of data so we can figure out what caused the error.
  804. Map<String,String>ErrorInfo = new LinkedHashMap<String,String>();
  805. //CommandSender sender, Command cmd, String commandLabel, String[] args
  806. ErrorInfo.put("Msg", "Error running command.");
  807. ErrorInfo.put("CMD", cmd.getName());
  808. ErrorInfo.put("Label", commandLabel);;
  809. ErrorInfo.put("Arguments",Integer.toString(args.length));
  810. ErrorInfo.put("Args",arrayToString(args, " "));
  811. ErrorInfo.put("Trace",StracktraceToString(e));
  812. ErrorLog(ErrorInfo);
  813. }
  814. return false;
  815. }
  816. public long GetPlaytime(String player)
  817. {//Get the play time of a single player and return it in milliseconds
  818. long now = System.currentTimeMillis();
  819. long login=0;
  820. long total=0;
  821. if (StartTime.containsKey(player))
  822. login=now - StartTime.get(player);
  823. if (PlayTime.containsKey(player))
  824. total=PlayTime.get(player);
  825. if (total==0)
  826. {
  827. loadPlaytime(player);
  828. if (PlayTime.containsKey(player))
  829. total=PlayTime.get(player);
  830. }
  831. return login + total;
  832. }
  833. public void saveRent()
  834. {//Save our rented ranks and abilities to disk so we can load them later.
  835. try {
  836. File path = new File(mainDirectory+File.separator+"data"+File.separator+"rent.data");
  837. ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream(path.getPath()));
  838. obj.writeObject(RentedRanks);
  839. obj.close();
  840. } catch (FileNotFoundException e) {
  841. ThrowSimpleError(e);
  842. } catch (IOException e) {
  843. ThrowSimpleError(e);
  844. }
  845. try {
  846. File path = new File(mainDirectory+File.separator+"data"+File.separator+"rentability.data");
  847. ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream(path.getPath()));
  848. obj.writeObject(RentedAbilities);
  849. obj.close();
  850. } catch (FileNotFoundException e) {
  851. ThrowSimpleError(e);
  852. } catch (IOException e) {
  853. ThrowSimpleError(e);
  854. }
  855. }
  856. @SuppressWarnings("unchecked")
  857. public void loadRent()
  858. {//Load who has rented what.
  859. File path = new File(mainDirectory+File.separator+"data"+File.separator+"rent.data");
  860. if (path.exists())
  861. {
  862. try {
  863. ObjectInputStream obj = new ObjectInputStream(new FileInputStream(path.getPath()));
  864. RentedRanks = (List<PurchasedRank>)obj.readObject();
  865. } catch (FileNotFoundException e) {
  866. ThrowSimpleError(e);
  867. } catch (IOException e) {
  868. ThrowSimpleError(e);
  869. } catch (ClassNotFoundException e) {
  870. ThrowSimpleError(e);
  871. }
  872. }
  873. path = new File(mainDirectory+File.separator+"data"+File.separator+"rentability.data");
  874. if (path.exists())
  875. {
  876. try {
  877. ObjectInputStream obj = new ObjectInputStream(new FileInputStream(path.getPath()));
  878. RentedAbilities = (List<PurchasedAbility>)obj.readObject();
  879. } catch (FileNotFoundException e) {
  880. ThrowSimpleError(e);
  881. } catch (IOException e) {
  882. ThrowSimpleError(e);
  883. } catch (ClassNotFoundException e) {
  884. ThrowSimpleError(e);
  885. }
  886. }
  887. }
  888. public void savePlaytime()
  889. {//Save play time for everyone
  890. for(String p : PlayTime.keySet())
  891. {
  892. savePlaytime(p);
  893. }
  894. }
  895. public void savePlaytime(Player p)
  896. {//Save playtime for a single player.
  897. savePlaytime(p.getName());
  898. }
  899. public void savePlaytime(String name)
  900. {//Save play time for a single players name.
  901. String path;
  902. Properties prop = new Properties(); //creates a new properties file
  903. try {
  904. //Update play time
  905. long playtime =GetPlaytime(name);
  906. PlayTime.put(name,playtime);
  907. long now = System.currentTimeMillis();
  908. StartTime.put(name, now);
  909. path = mainDirectory+File.separator+"data"+File.separator+name;
  910. ObjectOutputStream hashfile = new ObjectOutputStream(new FileOutputStream(path));
  911. prop.put("time", PlayTime.get(name).toString());
  912. prop.store(hashfile, "Do not edit");
  913. hashfile.flush();
  914. hashfile.close();
  915. //Something went wrong
  916. }catch(Exception e){
  917. ThrowSimpleError(e);
  918. }
  919. }
  920. public void loadPlaytime(Player p)
  921. {//Load play time for a player.
  922. loadPlaytime(p.getName());
  923. }
  924. public void loadPlaytime(String name)
  925. { //Load play time for a single players name
  926. Properties prop = new Properties(); //creates a new properties file
  927. File path = new File(mainDirectory+File.separator+"data"+File.separator+name);
  928. if (path.exists())
  929. {
  930. try{
  931. ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path.getPath()));
  932. prop.load(ois);
  933. if (prop.getProperty("time") != null)
  934. PlayTime.put(name,Long.parseLong(prop.getProperty("time")) );
  935. else
  936. {
  937. DebugPrint("Problem loading playtime, returned null for "+name+". Setting it to 0");
  938. DebugPrint("Problem loading playtime, returned null for "+name+". Setting it to 0");
  939. }
  940. }catch(Exception e){
  941. DebugPrint("Error loading playtime. Setting it to 0");
  942. PlayTime.put(name,(long) 0);
  943. ThrowSimpleError(e);
  944. }
  945. }
  946. else
  947. {
  948. DebugPrint("Playtime not found for "+name+". Setting it to 0.");
  949. PlayTime.put(name,(long) 0);
  950. }
  951. }
  952. public void ThrowSimpleError(Exception e,String msg)
  953. {//Throw a simple error message using our nice formated error system with a single bit of txt to help describe it.
  954. Map<String, String>ErrorInfo= new LinkedHashMap<String,String>();
  955. ErrorInfo.put("Msg", msg);
  956. ErrorInfo.put("Trace", StracktraceToString(e));
  957. ErrorLog(ErrorInfo);
  958. }
  959. public void ThrowSimpleError(Exception e)
  960. {//throw a basic error in our nice formated error system.
  961. Map<String, String>ErrorInfo = new LinkedHashMap<String,String>();
  962. ErrorInfo.put("Trace", StracktraceToString(e));
  963. ErrorLog(ErrorInfo);
  964. }
  965. public String StracktraceToString(Exception e)
  966. {//Take a stack trace and return it in a string we can work with.
  967. StringWriter sw = new StringWriter();
  968. e.printStackTrace(new PrintWriter(sw));
  969. return sw.toString();
  970. }
  971. public void DebugPrint(String msg)
  972. {//Print debug messages if debug is turned on.
  973. if (debug)
  974. log.info("[Time Rank] " + msg);
  975. }
  976. public void ErrorLog(Map<String, String> ErrorList)
  977. {//A nicely formated error message for the console.
  978. log.severe("===================================================");
  979. log.severe("= ERROR REPORT START =");
  980. log.severe("===================================================");
  981. log.severe("= TIME RANK ERROR =");
  982. log.severe("= INCLUDE WHEN ASKING FOR HELP =");
  983. log.severe("===================================================");
  984. log.severe("Version: "+this.getDescription().getVersion());
  985. log.severe("Permissions: "+this.permissions);
  986. log.severe("Ranks Loaded: "+Ranks.size());
  987. if (ErrorList != null)
  988. {
  989. log.severe("===================ERROR INFO===================");
  990. for (String key:ErrorList.keySet())
  991. {
  992. log.severe(key + ": " + ErrorList.get(key));
  993. }
  994. }
  995. log.severe("===================================================");
  996. log.severe("= ERROR REPORT ENDED =");
  997. log.severe("===================================================");
  998. }
  999. public static String arrayToString(String[] a, String separator) {
  1000. StringBuffer result = new StringBuffer();
  1001. if (a.length > 0) {
  1002. result.append(a[0]);
  1003. for (int i=1; i<a.length; i++) {
  1004. result.append(separator);
  1005. result.append(a[i]);
  1006. }
  1007. }
  1008. return result.toString();
  1009. }
  1010. public void BuyRank(Player player, String rankname)
  1011. { //Given a player and a rank. Try to have that player buy that rank.
  1012. DebugPrint("Looking for " + rankname);
  1013. for(Rank r : Ranks.keySet())
  1014. {
  1015. DebugPrint("Checking "+rankname + "=" + r.name);
  1016. if (r.name.equalsIgnoreCase(rankname))
  1017. {//found the rank we are looking for. See if it is for sale
  1018. DebugPrint(rankname + " found. Checking cost: " +r.cost);
  1019. //Check if we have passed the minimum time
  1020. if (r.cost>=0 && r.minTime < GetPlaytime(player.getName()))
  1021. {
  1022. if (r.cost==0)
  1023. {//use money
  1024. DebugPrint(rankname + " Using money for cost");
  1025. if (Method.getAccount(player.getName()).hasEnough(r.amount))
  1026. {
  1027. DebugPrint("You have the required money");
  1028. switch(PromotePlayer(player,r))
  1029. {
  1030. case 0://Everything went fine
  1031. Method.getAccount(player.getName()).subtract(r.amount); //Consume money.
  1032. Map<String, String> replace = new HashMap<String, String>();
  1033. replace.putAll(ProcessMsgVars(player));
  1034. replace.putAll(ProcessMsgVars(r));
  1035. String msg = ProcessMsg(r.msg, replace);
  1036. if (r.broadcast)
  1037. getServer().broadcastMessage(msg);
  1038. else
  1039. player.sendMessage(msg);
  1040. break;
  1041. case 1://Not in old group
  1042. player.sendMessage("You need to be in " + r.GetOldGroup().getName() + " to be buy the rank "+r.name);
  1043. break;
  1044. case 2:
  1045. player.sendMessage("You are already in " + r.GetGroup().getName() +" which " + r.name + " grants.");
  1046. break;
  1047. }//end switch
  1048. }
  1049. else
  1050. {
  1051. player.sendMessage("You don't have enough items. You need at least " + Method.format(r.amount));
  1052. }
  1053. }
  1054. else
  1055. {//use block id
  1056. DebugPrint(rankname + " Using block "+ r.cost +" for cost");
  1057. ItemStack item = new ItemStack(r.cost, (int) r.amount);
  1058. //right now the player must have exactly that item.
  1059. //Example: a stack of 5 gold bars instead of a stack of 10 gold bars.
  1060. if (CheckItems(player,item))
  1061. {
  1062. DebugPrint("You have the required items");
  1063. switch(PromotePlayer(player,r))
  1064. {
  1065. case 0://Everything went fine
  1066. ConsumeItems(player,item);
  1067. Map<String, String> replace = new HashMap<String, String>();
  1068. replace.putAll(ProcessMsgVars(player));
  1069. replace.putAll(ProcessMsgVars(r));
  1070. String msg = ProcessMsg(r.msg, replace);
  1071. if (r.broadcast)
  1072. getServer().broadcastMessage(msg);
  1073. else
  1074. player.sendMessage(msg);
  1075. break;
  1076. case 1://Not in old group
  1077. player.sendMessage("You need to be in " + r.GetOldGroup().getName() + " to be buy the rank "+r.name);
  1078. break;
  1079. case 2:
  1080. player.sendMessage("You are already in " + r.GetGroup().getName() +" which " + r.name + " grants.");
  1081. break;
  1082. }//end switch
  1083. }//end player has items check
  1084. else
  1085. {
  1086. player.sendMessage("You don't have enough items. You need at least " + r.amount + " of " + Material.getMaterial(r.cost));
  1087. }
  1088. }//end check to see if we are using money/block
  1089. }
  1090. }//end check to see if we can buy this
  1091. }//end check of rank name
  1092. }
  1093. public void RentRank(Player player,String rankname)
  1094. {//RentedGroups
  1095. DebugPrint("Looking for " + rankname);
  1096. for(Rank r : Ranks.keySet())
  1097. {
  1098. DebugPrint("Checking "+rankname + "=" + r.name);
  1099. if (r.name.equalsIgnoreCase(rankname))
  1100. {//found the rank we are looking for. See if it is for sale
  1101. DebugPrint(rankname + " found. Checking cost: " +r.rentCost);
  1102. if (r.rentCost>=0)
  1103. {
  1104. if (r.rentCost==0)
  1105. {//use money
  1106. DebugPrint(rankname + " Using money for cost");
  1107. if (Method.getAccount(player.getName()).hasEnough(r.rentAmount))
  1108. {
  1109. DebugPrint("You have the required items");
  1110. switch(PromotePlayer(player,r))
  1111. {
  1112. case 0://Everything went fine
  1113. Method.getAccount(player.getName()).subtract(r.rentAmount); //Consume money.
  1114. RentedRanks.add(new PurchasedRank(player.getName(), r));
  1115. Map<String, String> replace = new HashMap<String, String>();
  1116. replace.putAll(ProcessMsgVars(player));
  1117. replace.putAll(ProcessMsgVars(r));
  1118. String msg = ProcessMsg(r.rentGainedMsg, replace);
  1119. if (r.broadcast)
  1120. getServer().broadcastMessage(msg);
  1121. else
  1122. player.sendMessage(msg);
  1123. break;
  1124. case 1://Not in old group
  1125. player.sendMessage("You need to be in " + r.GetOldGroup().getName() + " to be buy the rank "+r.name);
  1126. break;
  1127. case 2:
  1128. player.sendMessage("You are already in " + r.GetGroup().getName() +" which " + r.name + " grants.");
  1129. break;
  1130. }//end switch
  1131. }
  1132. else
  1133. {
  1134. player.sendMessage("You don't have enough items. You need at least " + Method.format(r.rentAmount));
  1135. }
  1136. }
  1137. else
  1138. {//use block id
  1139. DebugPrint(rankname + " Using block "+ r.rentCost +" for cost");
  1140. ItemStack item = new ItemStack(r.rentCost, (int) r.rentAmount);
  1141. if (CheckItems(player,item))
  1142. {
  1143. DebugPrint("You have the required items");
  1144. switch(PromotePlayer(player,r))
  1145. {
  1146. case 0://Everything went fine
  1147. ConsumeItems(player,item); //Consume items.
  1148. Map<String, String> replace = new HashMap<String, String>();
  1149. replace.putAll(ProcessMsgVars(player));
  1150. replace.putAll(ProcessMsgVars(r));
  1151. String msg = ProcessMsg(r.rentGainedMsg, replace);
  1152. if (r.broadcast)
  1153. getServer().broadcastMessage(msg);
  1154. else
  1155. player.sendMessage(msg);
  1156. break;
  1157. case 1://Not in old group
  1158. player.sendMessage("You need to be in " + r.GetOldGroup().getName() + " to be buy the rank "+r.name);
  1159. break;
  1160. case 2:
  1161. player.sendMessage("You are already in " + r.GetGroup() +" which " + r.name + " grants.");
  1162. break;
  1163. }//end switch
  1164. }//end player has items check
  1165. else
  1166. {
  1167. player.sendMessage("You don't have enough items. You need at least " + r.rentAmount + " of " + Material.getMaterial(r.rentCost));
  1168. }
  1169. }//end check to see if we are using money
  1170. }//end check to see if we can buy this
  1171. }//end check of rank name
  1172. }
  1173. }
  1174. public void BuyAbility(Player player, String abilityname)
  1175. { //Given a player and a rank. Try to have that player buy that rank.
  1176. DebugPrint("Looking for " + abilityname);
  1177. for(Ability ab : Abilities.keySet())
  1178. {
  1179. DebugPrint("Checking "+abilityname + "=" + ab.name);
  1180. if (ab.name.equalsIgnoreCase(abilityname))
  1181. {//found the ability we are looking for. See if it is for sale
  1182. DebugPrint(abilityname + " found. Checking cost: " +ab.cost);
  1183. //Check if we have passed the minimum time
  1184. if (ab.cost>=0 && ab.minTime < GetPlaytime(player.getName()))
  1185. {
  1186. if (ab.cost==0)
  1187. {//use money
  1188. DebugPrint(abilityname + " Using money for cost");
  1189. if (Method.getAccount(player.getName()).hasEnough(ab.amount))
  1190. {
  1191. DebugPrint("You have the required money");
  1192. switch(AddPlayerNode(player,ab))
  1193. {
  1194. case 0://Everything went fine
  1195. Method.getAccount(player.getName()).subtract(ab.amount); //Consume money.
  1196. Map<String, String> replace = new HashMap<String, String>();
  1197. replace.putAll(ProcessMsgVars(player));
  1198. replace.putAll(ProcessMsgVars(ab));
  1199. String msg = ProcessMsg(ab.msg, replace);
  1200. if (ab.broadcast)
  1201. getServer().broadcastMessage(msg);
  1202. else
  1203. player.sendMessage(msg);
  1204. break;
  1205. case 1://Don't have the permissions
  1206. player.sendMessage("You don't have the permissions to buy " + ab.name);
  1207. break;
  1208. case 2:
  1209. player.sendMessage("You are already all the permissions " + ab.name + " grants.");
  1210. break;
  1211. }//end switch
  1212. }
  1213. else
  1214. {
  1215. player.sendMessage("You don't have enough items. You need at least " + Method.format(ab.amount));
  1216. }
  1217. }
  1218. else
  1219. {//use block id
  1220. DebugPrint(abilityname + " Using block "+ ab.cost +" for cost");
  1221. ItemStack item = new ItemStack(ab.cost, (int) ab.amount);
  1222. //right now the player must have exactly that item.
  1223. //Example: a stack of 5 gold bars instead of a stack of 10 gold bars.
  1224. if (CheckItems(player,item))
  1225. {
  1226. DebugPrint("You have the required items");
  1227. switch(AddPlayerNode(player,ab))
  1228. {
  1229. case 0://Everything went fine
  1230. ConsumeItems(player,item);
  1231. Map<String, String> replace = new HashMap<String, String>();
  1232. replace.putAll(ProcessMsgVars(player));
  1233. replace.putAll(ProcessMsgVars(ab));
  1234. String msg = ProcessMsg(ab.msg, replace);
  1235. if (ab.broadcast)
  1236. getServer().broadcastMessage(msg);
  1237. else
  1238. player.sendMessage(msg);
  1239. break;
  1240. case 1://Don't have the permissions
  1241. player.sendMessage("You don't have the permissions to buy " + ab.name);
  1242. break;
  1243. case 2:
  1244. player.sendMessage("You are already all the permissions " + ab.name + " grants.");
  1245. break;
  1246. }//end switch
  1247. }//end player has items check
  1248. else
  1249. {
  1250. player.sendMessage("You don't have enough items. You need at least " + ab.amount + " of " + Material.getMaterial(ab.cost));
  1251. }
  1252. }//end check to see if we are using money/block
  1253. }
  1254. }//end check to see if we can buy this
  1255. }//end check of rank name
  1256. }
  1257. public void RentAbility(Player player,String abilityname)
  1258. {//RentedGroups
  1259. DebugPrint("Looking for " + abilityname);
  1260. for(Ability ab : Abilities.keySet())
  1261. {
  1262. DebugPrint("Checking "+ abilityname + "=" + ab.name);
  1263. if (ab.name.equalsIgnoreCase(abilityname))
  1264. {//found the rank we are looking for. See if it is for sale
  1265. DebugPrint(abilityname + " found. Checking cost: " +ab.rentCost);
  1266. if (ab.rentCost>=0)
  1267. {
  1268. if (ab.rentCost==0)
  1269. {//use money
  1270. DebugPrint(abilityname + " Using money for cost");
  1271. if (Method.getAccount(player.getName()).hasEnough(ab.rentAmount))
  1272. {
  1273. DebugPrint("You have the required items");
  1274. switch(AddPlayerNode(player,ab))
  1275. {
  1276. case 0://Everything went fine
  1277. Method.getAccount(player.getName()).subtract(ab.rentAmount); //Consume money.
  1278. RentedAbilities.add(new PurchasedAbility(player.getName(), ab));
  1279. Map<String, String> replace = new HashMap<String, String>();
  1280. replace.putAll(ProcessMsgVars(player));
  1281. replace.putAll(ProcessMsgVars(ab));
  1282. String msg = ProcessMsg(ab.rentGainedMsg, replace);
  1283. if (ab.broadcast)
  1284. getServer().broadcastMessage(msg);
  1285. else
  1286. player.sendMessage(msg);
  1287. break;
  1288. case 1://Don't have the permissions
  1289. player.sendMessage("You don't have the permissions to buy " + ab.name);
  1290. break;
  1291. case 2:
  1292. player.sendMessage("You are already all the permissions " + ab.name + " grants.");
  1293. break;
  1294. }//end switch
  1295. }
  1296. else
  1297. {
  1298. player.sendMessage("You don't have enough