PageRenderTime 31ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/com/bukkit/BallisticBuddha/GoldStandard/GoldStandard.java

https://github.com/smickles/GoldStandard
Java | 690 lines | 635 code | 6 blank | 49 comment | 135 complexity | ee97c4d3da47d1c3d8ed71da8ed44f3c MD5 | raw file
  1. package com.bukkit.BallisticBuddha.GoldStandard;
  2. import com.bukkit.BallisticBuddha.GoldStandard.GSItem.GSType;
  3. import com.bukkit.BallisticBuddha.GoldStandard.Protect.*;
  4. import com.bukkit.BallisticBuddha.GoldStandard.Transactions.*;
  5. //import com.bukkit.BallisticBuddha.GoldStandard.griefcraft.*;
  6. import com.iConomy.*;
  7. import com.nijikokun.bukkit.Permissions.Permissions;
  8. import com.nijiko.permissions.PermissionHandler;
  9. import gnu.trove.list.array.TIntArrayList;
  10. import java.io.File;
  11. import java.util.ArrayList;
  12. import java.util.Collections;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.logging.Logger;
  17. import java.util.regex.Pattern;
  18. import org.bukkit.ChatColor;
  19. import org.bukkit.Material;
  20. import org.bukkit.Server;
  21. import org.bukkit.command.Command;
  22. import org.bukkit.command.CommandSender;
  23. import org.bukkit.entity.Player;
  24. import org.bukkit.event.Event;
  25. import org.bukkit.event.Event.Priority;
  26. import org.bukkit.event.Event.Type;
  27. import org.bukkit.inventory.Inventory;
  28. import org.bukkit.plugin.PluginDescriptionFile;
  29. import org.bukkit.plugin.java.JavaPlugin;
  30. import org.bukkit.plugin.Plugin;
  31. import org.bukkit.plugin.PluginManager;
  32. import org.bukkit.util.config.Configuration;
  33. /**
  34. @author BallisticBuddha
  35. @version 1.1.0
  36. Gold Standard plugin for bukkit
  37. */
  38. public class GoldStandard extends JavaPlugin{
  39. private final GSPlayerListener playerListener = new GSPlayerListener(this);
  40. private final GSBlockListener blockListener = new GSBlockListener(this);
  41. private static final Pattern positiveInt = Pattern.compile("^\\d+$");
  42. public iConomy iConomy = null;
  43. private static Logger log = Logger.getLogger("Minecraft");
  44. private static Server bukkitServer = null;
  45. private GSItem baseItem = null;
  46. private Map<Integer,GSItem> items = new HashMap<Integer,GSItem>();
  47. private int sellTool = 0;
  48. private boolean buybackEnabled;
  49. private boolean allowBlock;
  50. private int reloadInterval;
  51. private int buyCooldown;
  52. private int sellCooldown;
  53. private boolean opCools;
  54. private int defaultBuyItem;
  55. private int defaultBuyQty;
  56. private String defaultSellItems;
  57. private List<String> sellMethods = new ArrayList<String>();
  58. private GSCalc calc = null;
  59. private Configuration config = new Configuration(new File("plugins/GoldStandard/config.yml"));
  60. private Configuration itemConfig = new Configuration(new File("plugins/GoldStandard/items.yml"));
  61. private boolean usePermissions = false;
  62. private PermissionHandler Permissions = null;
  63. private int CleanseTask;
  64. private String protectSystem;
  65. private ContainerProtect protection;
  66. //private Updater updater;
  67. @Override
  68. public void onDisable() {
  69. stopCleanseThread();
  70. for (String pName : calc.getPlayers())
  71. getCalc().storePlayerND(pName);
  72. calc.closeDBSession();
  73. log.info("Gold Standard Disabled!");
  74. }
  75. @Override
  76. public void onEnable() {
  77. PluginManager pm = getServer().getPluginManager();
  78. bukkitServer = getServer();
  79. // updater = new Updater();
  80. // try {
  81. // updater.check();
  82. // updater.update();
  83. // } catch (Exception e) {
  84. // }
  85. pm.registerEvent(Event.Type.BLOCK_DAMAGE, blockListener, Priority.High, this);
  86. pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Normal, this);
  87. pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Normal, this);
  88. pm.registerEvent(Type.PLUGIN_ENABLE, new ServerICS(this), Priority.Monitor, this);
  89. pm.registerEvent(Type.PLUGIN_DISABLE, new ServerICS(this), Priority.Monitor, this);
  90. config.load();
  91. PluginDescriptionFile pdfFile = this.getDescription();
  92. usePermissions = config.getBoolean("Permissions", true);
  93. sellTool = config.getInt("SellTool", 283);
  94. buybackEnabled = config.getBoolean("Buyback", false);
  95. allowBlock = config.getBoolean("Allow Block", true);
  96. reloadInterval = config.getInt("Reload Interval", 60);
  97. sellCooldown = config.getInt("Cooldown.Sell", 0);
  98. buyCooldown = config.getInt("Cooldown.Buy", 0);
  99. opCools = config.getBoolean("Cooldown.Ops Obey", false);
  100. ArrayList<String> tempAR = (ArrayList<String>) config.getStringList("SellMethods", null);
  101. for (String item : tempAR){
  102. sellMethods.add(item.toLowerCase());
  103. }
  104. loadItems();
  105. calc = new GSCalc(this);
  106. startCleanseThread();
  107. setupPermissions();
  108. log.info( pdfFile.getName() + " version " + pdfFile.getVersion() + " enabled!" );
  109. setupProtection();
  110. }
  111. @Override
  112. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
  113. String commandName = command.getName().toLowerCase();
  114. Player player = (Player) sender;
  115. if(commandName.equalsIgnoreCase("gsworth")) {
  116. if (!(player instanceof Player)) //in-game only command
  117. return false;
  118. if (!hasPermissions(player, "goldstandard.worth")){
  119. player.sendMessage(ChatColor.RED.toString() +"You do not have permission to use that command.");
  120. return true;
  121. }
  122. if (args.length < 1)
  123. return false;
  124. String[] items = args[0].split(",");
  125. for (String itm : items){
  126. GSItem thisItem = parseGSItem(itm);
  127. if (thisItem != null){
  128. player.sendMessage(ChatColor.YELLOW.toString() +"The current sale price of "+
  129. thisItem.getNickname() +
  130. " is "+iConomy.format(this.calc.getWorth(thisItem)));
  131. player.sendMessage(ChatColor.YELLOW.toString() +"The buy price of "+
  132. thisItem.getNickname() +
  133. " is "+iConomy.format(this.calc.checkBuyPrice(thisItem)));
  134. }
  135. else
  136. player.sendMessage(ChatColor.RED.toString() + itm+" is not a valid item.");
  137. }
  138. return true;
  139. }
  140. else if (commandName.equalsIgnoreCase("gsclear")){
  141. if (!(player instanceof Player))
  142. return false;
  143. if (!hasPermissions(player, "goldstandard.clear")){
  144. player.sendMessage(ChatColor.RED.toString() +"You do not have permission to use that command.");
  145. return true;
  146. }
  147. this.getCalc().clear();
  148. player.sendMessage(ChatColor.RED.toString() +"All GS records cleared.");
  149. return true;
  150. }
  151. else if (commandName.equalsIgnoreCase("gssell")){
  152. if (!commandMode()){
  153. player.sendMessage(ChatColor.RED.toString() +"Selling on command is disabled.");
  154. return true;
  155. }
  156. if (args.length < 1)
  157. return false;
  158. Inventory stuff = player.getInventory();
  159. TIntArrayList itms = new TIntArrayList();
  160. TIntArrayList qtys = new TIntArrayList();
  161. ArrayList<Boolean> iBlocks = new ArrayList<Boolean>();
  162. ArrayList<Boolean> bChange = new ArrayList<Boolean>();
  163. String[] itemStrings = args[0].split(",");
  164. String[] qtyStrings = null;
  165. if (args.length == 2)
  166. qtyStrings = args[1].split(",");
  167. else
  168. return false;
  169. for (String itm : itemStrings){
  170. GSItem thisItem = parseGSItem(itm);
  171. if (thisItem != null)
  172. itms.add(thisItem.getTypeId());
  173. else
  174. player.sendMessage(itm+" is not a valid item or item number.");
  175. }
  176. for (int i=0;i<qtyStrings.length;i++){
  177. if (isPositiveInt(qtyStrings[i])){
  178. qtys.add(Integer.parseInt(qtyStrings[i]));
  179. iBlocks.add(false);
  180. bChange.add(false);
  181. }
  182. else if (qtyStrings[i].length() > 1){
  183. if (isPositiveInt(qtyStrings[i].substring(0, qtyStrings[i].length()-1))){
  184. if (qtyStrings[i].charAt(qtyStrings[i].length()-1) == '~'){
  185. if (itms.size()<i+1){
  186. player.sendMessage("Item-quantity mismatch, more quantities than items.");
  187. return true;
  188. }
  189. qtys.add(Integer.parseInt(qtyStrings[i].substring(0, qtyStrings[i].length()-1)));
  190. if (getGSItem(itms.get(i)).blockAllowed()){
  191. iBlocks.add(true);
  192. bChange.add(false);
  193. }
  194. else{
  195. iBlocks.add(false);
  196. bChange.add(false);
  197. }
  198. }
  199. else if (qtyStrings[i].charAt(qtyStrings[i].length()-1) == '*'){
  200. if (itms.size()<i+1){
  201. player.sendMessage("Item-quantity mismatch, more quantities than items.");
  202. return true;
  203. }
  204. qtys.add(Integer.parseInt(qtyStrings[i].substring(0, qtyStrings[i].length()-1)));
  205. if (getGSItem(itms.get(i)).blockAllowed()){
  206. iBlocks.add(true);
  207. bChange.add(true);
  208. }
  209. else{
  210. iBlocks.add(false);
  211. bChange.add(false);
  212. }
  213. }
  214. else{
  215. player.sendMessage(qtyStrings[i]+" is not a valid quantity.");
  216. continue;
  217. }
  218. }
  219. else{
  220. player.sendMessage(qtyStrings[i]+" is not a valid quantity.");
  221. continue;
  222. }
  223. }
  224. else{
  225. player.sendMessage(qtyStrings[i]+" is not a valid quantity.");
  226. continue;
  227. }
  228. //TODO: add option for selling/buying all items in inventory
  229. // if (qtyStrings[i].charAt(0) == '#'){
  230. // int totalInInventory = 0;
  231. // for (ItemStack is : stuff.getContents()){
  232. // if (is == null)
  233. // continue;
  234. // if (is.getTypeId() == getGSItem(itemStrings[i]))
  235. // totalInInventory += is.getAmount();
  236. // else if ((is.getTypeId() == gsi.getBlock()) && includeBlocks)
  237. // totalInInventory += is.getAmount()*9;
  238. // }
  239. // }
  240. }
  241. long timeSinceLast = getCalc().timeSinceSold(player);
  242. if (timeSinceLast < getSellCooldown()){
  243. if ((getSellCooldown()-timeSinceLast) >= 1000)
  244. player.sendMessage(ChatColor.RED.toString() +"You must wait "+
  245. (getSellCooldown()-timeSinceLast)/1000 +" seconds before you may sell again");
  246. else
  247. player.sendMessage(ChatColor.RED.toString() +"You must wait "+
  248. (getSellCooldown()-timeSinceLast) +" miliseconds before you may sell again");
  249. return true;
  250. }
  251. if (itms.size() != qtys.size()){
  252. player.sendMessage(ChatColor.RED.toString() +"You must specify a quantity for each item specified.");
  253. return true;
  254. }
  255. for (int i=0;i<itms.size();i++)
  256. new SellProcedure(this,player,stuff,iBlocks.get(i),bChange.get(i)).execute(itms.get(i), qtys.get(i));
  257. return true;
  258. }
  259. else if (commandName.equalsIgnoreCase("gsbuy")){
  260. if (!commandMode()){
  261. player.sendMessage(ChatColor.RED.toString() +"Buying on command is disabled.");
  262. return true;
  263. }
  264. if (args.length < 1)
  265. return false;
  266. Inventory stuff = player.getInventory();
  267. //TODO: Use TIntIntHashMap instead?
  268. //Is there a way to sanitize input through doing it that way?
  269. TIntArrayList itms = new TIntArrayList();
  270. TIntArrayList qtys = new TIntArrayList();
  271. String[] itemStrings = args[0].split(",");
  272. String[] qtyStrings = null;
  273. if (args.length == 2)
  274. qtyStrings = args[1].split(",");
  275. else
  276. return false;
  277. for (String itm : itemStrings){
  278. GSItem thisItem = parseGSItem(itm);
  279. if (thisItem != null)
  280. itms.add(thisItem.getTypeId());
  281. else
  282. player.sendMessage(itm+" is not a valid item or item number.");
  283. }
  284. for (int i=0;i<qtyStrings.length;i++){
  285. if (isPositiveInt(qtyStrings[i]))
  286. qtys.add(Integer.parseInt(qtyStrings[i]));
  287. else{
  288. player.sendMessage(qtyStrings[i]+" is not a valid quantity.");
  289. continue;
  290. }
  291. }
  292. if (itms.size() != qtys.size()){
  293. player.sendMessage(ChatColor.RED.toString() +"You must specify a quantity for each item specified.");
  294. return true;
  295. }
  296. long timeSinceLast = calc.timeSinceBought(player);
  297. if (timeSinceLast < getBuyCooldown()){
  298. if ((getBuyCooldown()-timeSinceLast) >= 1000)
  299. player.sendMessage(ChatColor.RED.toString() +"You must wait "+
  300. (getBuyCooldown()-timeSinceLast)/1000 +" seconds before you may buy again");
  301. else
  302. player.sendMessage(ChatColor.RED.toString() +"You must wait "+
  303. (getBuyCooldown()-timeSinceLast) +" miliseconds before you may buy again");
  304. return true;
  305. }
  306. for (int i=0;i<itms.size();i++)
  307. new BuyProcedure(this,player,stuff).execute(itms.get(i), qtys.get(i));
  308. return true;
  309. }
  310. else if (commandName.equalsIgnoreCase("gshistory")){
  311. if (!(player instanceof Player))
  312. return false;
  313. if (!hasPermissions(player, "goldstandard.history")){
  314. player.sendMessage(ChatColor.RED.toString() +
  315. "You do not have permission to use that command.");
  316. return true;
  317. }
  318. if (args.length < 1)
  319. return false;
  320. String itemID = args[0];
  321. String type = "";
  322. if (args.length < 2)
  323. type = "net";
  324. else
  325. type = args[1];
  326. player.sendMessage(ChatColor.YELLOW.toString() + calc.getTransactions(itemID, type));
  327. return true;
  328. }
  329. else if (commandName.equalsIgnoreCase("gsadd")){
  330. if (args.length != 1)
  331. return false;
  332. TIntArrayList itms = new TIntArrayList();
  333. ArrayList<Boolean> iBlocks = new ArrayList<Boolean>();
  334. String[] itemStrings = args[0].split(",");
  335. for (String itm : itemStrings){
  336. GSItem thisItem = parseGSItem(itm);
  337. if (thisItem != null){
  338. itms.add(thisItem.getTypeId());
  339. iBlocks.add(false);
  340. }
  341. else if (itm.length() > 1){
  342. GSItem truncItem = parseGSItem(itm.substring(0,itm.length()-1));
  343. if (truncItem != null && itm.charAt(itm.length()-1)=='~'){
  344. itms.add(truncItem.getTypeId());
  345. iBlocks.add(true);
  346. }
  347. else
  348. player.sendMessage(itm+" is not a valid item name or number.");
  349. }
  350. else
  351. player.sendMessage(itm+" is not a valid item name or number.");
  352. }
  353. for (int i=0;i<itms.size();i++)
  354. getCalc().getPlayer(player.getName()).addSellItem(itms.get(i), iBlocks.get(i));
  355. displayItemList(player, false);
  356. return true;
  357. }
  358. else if (commandName.equalsIgnoreCase("gsremove")){
  359. if (args.length != 1)
  360. return false;
  361. TIntArrayList itms = new TIntArrayList();
  362. String[] itemStrings = args[0].split(",");
  363. for (String itm : itemStrings){
  364. GSItem thisItem = parseGSItem(itm);
  365. if (thisItem != null)
  366. if (getCalc().getPlayer(player.getName()).isInSellList(thisItem.getTypeId())){
  367. itms.add(thisItem.getTypeId());
  368. player.sendMessage(ChatColor.YELLOW.toString() + thisItem.getNickname()+" was removed from you sell list.");
  369. }
  370. else
  371. player.sendMessage(ChatColor.RED.toString() + thisItem.getNickname()+" is not in your sell list.");
  372. else
  373. player.sendMessage(ChatColor.RED.toString() + itm+" is not a valid item name or number.");
  374. }
  375. for (int i=0;i<itms.size();i++)
  376. getCalc().getPlayer(player.getName()).removeSellItem(itms.get(i));
  377. displayItemList(player, false);
  378. return true;
  379. }
  380. else if (commandName.equals("gsset")){
  381. if (args.length < 2)
  382. return false;
  383. GSItem gsi = parseGSItem(args[0]);
  384. if (gsi != null){
  385. if (gsi.canBeBought()){
  386. if (isPositiveInt(args[1])){
  387. getCalc().getPlayer(player.getName()).setBuyItem(gsi.getTypeId());
  388. getCalc().getPlayer(player.getName()).setBuyQty(Integer.parseInt(args[1]));
  389. displayItemList(player, true);
  390. }
  391. else
  392. player.sendMessage(ChatColor.RED.toString() + args[1]+ " is not a valid quantity.");
  393. }
  394. else
  395. player.sendMessage(ChatColor.RED.toString() + gsi.getNickname()+ " cannot be bought.");
  396. }
  397. else
  398. player.sendMessage(ChatColor.RED.toString() + args[0] + " Is not a valid item.");
  399. return true;
  400. }
  401. else if (commandName.equalsIgnoreCase("gslist")){
  402. if (args.length < 1)
  403. return false;
  404. if (args[0].equalsIgnoreCase("buy"))
  405. displayItemList(player, true);
  406. else if (args[0].equalsIgnoreCase("sell"))
  407. displayItemList(player, false);
  408. return true;
  409. }
  410. return false;
  411. }
  412. private void setupPermissions() {
  413. if (!usePermissions)
  414. return;
  415. Plugin permissions = this.getServer().getPluginManager().getPlugin("Permissions");
  416. if (Permissions == null) {
  417. if (permissions != null)
  418. this.getServer().getPluginManager().enablePlugin(permissions);
  419. Permissions = ((Permissions) permissions).getHandler();
  420. }
  421. }
  422. public boolean hasPermissions(Player p, String s) {
  423. if (!usePermissions && !s.equalsIgnoreCase("goldstandard.clear"))
  424. return true;
  425. else if (Permissions != null){
  426. if (p.isOp())
  427. return true;
  428. else
  429. return Permissions.has(p, s);
  430. }
  431. return p.isOp();
  432. }
  433. private void setupProtection(){
  434. protectSystem = getConfig().getString("Protection","none");
  435. if (protectSystem.equalsIgnoreCase("none")){
  436. log.info("[GoldStandard] No protection system was specified, block protection is disabled.");
  437. protection = new NoneProtect(this);
  438. }
  439. else if (protectSystem.equalsIgnoreCase("LWC")){
  440. log.info("[GoldStandard] Block protection set to use LWC.");
  441. protection = new LWCProtect(this);
  442. }
  443. else{
  444. protection = new NoneProtect(this);
  445. log.info("[GoldStandard] An invalid protection system was specified, block protection is disabled.");
  446. protectSystem = "None";
  447. }
  448. }
  449. public void startCleanseThread(){
  450. CleanseTask = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
  451. @Override
  452. public void run() {
  453. if (calc.needsCleaning())
  454. calc.clearOld();
  455. if (getCalc().getSystem().equalsIgnoreCase("none") || getCalc().getSystem().equalsIgnoreCase("flatfiles"))
  456. getCalc().storeFlatfiles();
  457. else
  458. for (String pName : calc.getPlayers())
  459. getCalc().storePlayerND(pName);
  460. }
  461. }, 60 * 20L, reloadInterval*60 * 20L );
  462. }
  463. public void stopCleanseThread(){
  464. this.getServer().getScheduler().cancelTask(CleanseTask);
  465. }
  466. private void loadItems(){
  467. itemConfig.load();
  468. List<String> tempItems = itemConfig.getKeys("Items");
  469. for (String item : tempItems){
  470. String name = item;
  471. int id = itemConfig.getInt("Items."+item+".Id",0);
  472. boolean allowBlock = itemConfig.getBoolean("Items."+item+".Allow Block", this.allowBlock);
  473. boolean buyback = itemConfig.getBoolean("Items."+item+".Buyback", this.buybackEnabled);
  474. GSType gst = GSItem.GSType.valueOf(itemConfig.getString("Items."+item+".Type").toLowerCase());
  475. defaultBuyItem = itemConfig.getInt("Default.BuyItem", 0);
  476. defaultBuyQty = itemConfig.getInt("Default.BuyQty", 1);
  477. defaultSellItems = itemConfig.getString("Default.SellItems", "");
  478. double price;
  479. double min;
  480. double max;
  481. double ratio;
  482. double relation;
  483. GSItem gsi;
  484. switch(gst){
  485. case base:
  486. price = itemConfig.getDouble("Items."+item+".Price",0);
  487. min = itemConfig.getDouble("Items."+item+".Minimum",0);
  488. max = itemConfig.getDouble("Items."+item+".Maximum",0);
  489. ratio = itemConfig.getDouble("Items."+item+".Ratio",0);
  490. gsi = new GSItem(name,id,allowBlock,buyback,gst,price,min,max,ratio);
  491. items.put(id,gsi);
  492. baseItem = gsi;
  493. break;
  494. case relative:
  495. relation = itemConfig.getDouble("Items."+item+".Relation",0);
  496. //TODO: implement custom parent items
  497. gsi = new GSItem(name,id,allowBlock,buyback,gst,relation,this.getBaseItem().getNickname());
  498. items.put(id,gsi);
  499. break;
  500. case independent:
  501. price = itemConfig.getDouble("Items."+item+".Price",0);
  502. min = itemConfig.getDouble("Items."+item+".Minimum",0);
  503. max = itemConfig.getDouble("Items."+item+".Maximum",0);
  504. ratio = itemConfig.getDouble("Items."+item+".Ratio",0);
  505. gsi = new GSItem(name,id,allowBlock,buyback,gst,price,min,max,ratio);
  506. items.put(id,gsi);
  507. break;
  508. case fixed:
  509. price = itemConfig.getDouble("Items."+item+".Price",0);
  510. gsi = new GSItem(name,id,allowBlock,buyback,gst,price);
  511. items.put(id,gsi);
  512. break;
  513. default:
  514. log.warning("[GoldStandard] Error when loading item "+name+"\n"+
  515. " "+gst.toString()+" is an invalid item type.");
  516. break;
  517. }
  518. }
  519. log.info("[GoldStandard] "+items.size()+" Items loaded.");
  520. }
  521. //Re-inserted to display removed items to players
  522. public static String formatMaterialName(Material mat){
  523. String toOut = "";
  524. String oldString = mat.toString().toLowerCase();
  525. for (int i=0;i < oldString.length();i++){
  526. if (oldString.charAt(i) == '_')
  527. toOut += ' ';
  528. else
  529. toOut += oldString.charAt(i);
  530. }
  531. return toOut;
  532. }
  533. /** Gets a GSItem if the string input is valid (valid itemID or item nickname)
  534. * Returns null if invalid
  535. *
  536. * @param item - String to parse
  537. * @return the GSItem you're looking for (or null if not found)
  538. */
  539. public GSItem parseGSItem(String item){
  540. if (isPositiveInt(item)){
  541. int itemID = Integer.parseInt(item);
  542. if (validItem(itemID)){
  543. return getGSItem(itemID);
  544. }
  545. }
  546. return getGSItem(item);
  547. }
  548. /** Gets the GSItem of a valid sellable item by it's itemID. If the item is not valid, returns 0
  549. *
  550. * @param ItemID - the nickname to search for in the list of valid items
  551. * @return the itemID of a valid item's
  552. */
  553. public GSItem getGSItem(int itemID){
  554. return items.get(itemID);
  555. }
  556. /** Gets the GSItem of a valid sellable item by it's nickname. If the item is not valid, returns null
  557. *
  558. * @param nickname - the nickname to search for in the list of valid items
  559. * @return the itemID of a valid item's
  560. */
  561. public GSItem getGSItem(String nickname){
  562. for (GSItem gsi : items.values()){
  563. if (gsi.getNickname().equalsIgnoreCase(nickname))
  564. return gsi;
  565. }
  566. return null;
  567. }
  568. public boolean validItem(int itemID){
  569. return items.containsKey(itemID);
  570. }
  571. public boolean validBuy(Player player){
  572. GSItem myBuyItem = getGSItem(getCalc().getPlayer(player.getName()).getBuyItem());
  573. if (myBuyItem == null){
  574. player.sendMessage(ChatColor.RED.toString() +
  575. "The Item "+GoldStandard.formatMaterialName(Material.getMaterial(calc.getPlayer(player.getName()).getBuyItem())) +
  576. " is no longer valid.");
  577. player.sendMessage(ChatColor.RED.toString() +"Please reset it");
  578. return false;
  579. }
  580. else if (!myBuyItem.canBeBought()){
  581. player.sendMessage(ChatColor.RED.toString() +
  582. "The Item "+GoldStandard.formatMaterialName(Material.getMaterial(calc.getPlayer(player.getName()).getBuyItem())) +
  583. " can no longer be bought.");
  584. player.sendMessage(ChatColor.RED.toString() +"Please reset it");
  585. return false;
  586. }
  587. return true;
  588. }
  589. public boolean validSale(Player player, int itemID){
  590. if (!validItem(itemID)){
  591. player.sendMessage(ChatColor.RED.toString() +
  592. "The Item "+GoldStandard.formatMaterialName(Material.getMaterial(itemID)) +
  593. " is no longer valid.");
  594. player.sendMessage(ChatColor.RED.toString() +"It will be removed from your sell list.");
  595. getCalc().getPlayer(player.getName()).removeSellItem(itemID);
  596. return false;
  597. }
  598. return true;
  599. }
  600. private void displayItemList(Player player, boolean buyItem){
  601. if (buyItem){
  602. if (!validBuy(player))
  603. return;
  604. player.sendMessage(ChatColor.YELLOW.toString() + "You are currently buying:");
  605. player.sendMessage(ChatColor.YELLOW.toString() +
  606. getCalc().getPlayer(player.getName()).getBuyQty()+" "+
  607. getGSItem(getCalc().getPlayer(player.getName()).getBuyItem()).getNickname());
  608. }
  609. else{
  610. player.sendMessage(ChatColor.YELLOW.toString() + "You are currently selling:");
  611. if (getCalc().getPlayer(player.getName()).getSellList().keys().length == 0){
  612. player.sendMessage(ChatColor.RED.toString() +"NOTHING!");
  613. return;
  614. }
  615. for (int itemID : getCalc().getPlayer(player.getName()).getSellList().keys()){
  616. //System.out.println(itemID);
  617. if (!validSale(player,itemID))
  618. continue;
  619. player.sendMessage(ChatColor.YELLOW.toString() + getGSItem(itemID).getNickname()+
  620. (getCalc().getPlayer(player.getName()).itemCanBeBlock(itemID)?"~":""));
  621. }
  622. }
  623. }
  624. //Organization stuff goes here
  625. public GSCalc getCalc(){
  626. return this.calc;
  627. }
  628. public Configuration getConfig(){
  629. return this.config;
  630. }
  631. public GSItem getBaseItem(){
  632. return this.baseItem;
  633. }
  634. public int getSellTool(){
  635. return this.sellTool;
  636. }
  637. public boolean chestMode(){
  638. return sellMethods.containsAll(Collections.singleton("chest"));
  639. }
  640. public boolean furnaceMode(){
  641. return sellMethods.containsAll(Collections.singleton("furnace"));
  642. }
  643. public boolean dispenserMode(){
  644. return sellMethods.containsAll(Collections.singleton("dispenser"));
  645. }
  646. public boolean commandMode(){
  647. return sellMethods.containsAll(Collections.singleton("command"));
  648. }
  649. public static boolean isPositiveInt(String str){
  650. return positiveInt.matcher(str).matches();
  651. }
  652. public String getProtectionType(){
  653. return protectSystem;
  654. }
  655. public ContainerProtect getProtection(){
  656. return protection;
  657. }
  658. public static Server getBukkitServer() {
  659. return bukkitServer;
  660. }
  661. public Map<Integer,GSItem> getItems(){
  662. return this.items;
  663. }
  664. public int getBuyCooldown(){
  665. return this.buyCooldown;
  666. }
  667. public int getSellCooldown(){
  668. return this.sellCooldown;
  669. }
  670. public boolean opsObeyCooldown(){
  671. return this.opCools;
  672. }
  673. public int getDefaultBuyItem(){
  674. return this.defaultBuyItem;
  675. }
  676. public int getDefaultBuyQty(){
  677. return this.defaultBuyQty;
  678. }
  679. public String getDefaultSellItems(){
  680. return this.defaultSellItems;
  681. }
  682. }