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

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

https://github.com/smickles/GoldStandard
Java | 151 lines | 140 code | 5 blank | 6 comment | 57 complexity | df678b98a6dca4c8f187455d87e9b31d MD5 | raw file
  1. package com.bukkit.BallisticBuddha.GoldStandard;
  2. import java.util.logging.Logger;
  3. import org.bukkit.Material;
  4. import org.bukkit.block.Chest;
  5. import org.bukkit.block.ContainerBlock;
  6. import org.bukkit.block.Dispenser;
  7. import org.bukkit.block.Furnace;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.event.block.BlockDamageEvent;
  10. import org.bukkit.event.block.BlockListener;
  11. import org.bukkit.inventory.Inventory;
  12. import org.bukkit.inventory.ItemStack;
  13. import org.bukkit.ChatColor;
  14. import com.bukkit.BallisticBuddha.GoldStandard.Transactions.*;
  15. import gnu.trove.map.hash.TIntIntHashMap;
  16. import gnu.trove.map.hash.TIntShortHashMap;
  17. /** A very, very, very ugly listener that sells & buys items from container blocks
  18. *
  19. * @author BallisticBuddha
  20. *
  21. */
  22. public class GSBlockListener extends BlockListener{
  23. private static GoldStandard plugin = null;
  24. public GSBlockListener(GoldStandard instance){
  25. plugin = instance;
  26. }
  27. public void onBlockDamage(BlockDamageEvent event) {
  28. if (!(event.getPlayer() instanceof Player))
  29. return;
  30. if ((event.getBlock().getTypeId() == Material.FURNACE.getId()
  31. || event.getBlock().getTypeId() == Material.BURNING_FURNACE.getId()) && plugin.furnaceMode())
  32. sellIt(event, "Furnace");
  33. else if (event.getBlock().getTypeId() == Material.CHEST.getId() && plugin.chestMode())
  34. sellIt(event, "Chest");
  35. else if (event.getBlock().getTypeId() == Material.DISPENSER.getId() && plugin.dispenserMode())
  36. sellIt(event, "Dispenser");
  37. }
  38. private void sellIt(BlockDamageEvent event, String SellObject){
  39. Player player = event.getPlayer();
  40. if (player.getItemInHand().getTypeId() == plugin.getSellTool()){
  41. Inventory stuff = null;
  42. GSTransaction action;
  43. if (SellObject.equalsIgnoreCase("Furnace"))
  44. stuff = ((Furnace) ((ContainerBlock) event.getBlock().getState())).getInventory();
  45. else if (SellObject.equalsIgnoreCase("Chest"))
  46. stuff = ((Chest) ((ContainerBlock) event.getBlock().getState())).getInventory();
  47. else if (SellObject.equalsIgnoreCase("Dispenser"))
  48. stuff = ((Dispenser) ((ContainerBlock) event.getBlock().getState())).getInventory();
  49. else
  50. return;
  51. if (stuff == null)
  52. return;
  53. if (isEmpty(stuff)){
  54. long timeSinceLast = plugin.getCalc().timeSinceBought(player);
  55. if (timeSinceLast < plugin.getBuyCooldown()){
  56. if ((plugin.getBuyCooldown()-timeSinceLast) >= 1000)
  57. player.sendMessage(ChatColor.RED.toString() +"You must wait "+
  58. (plugin.getBuyCooldown()-timeSinceLast)/1000 +" seconds before you may buy again");
  59. else
  60. player.sendMessage(ChatColor.RED.toString() +"You must wait "+
  61. (plugin.getBuyCooldown()-timeSinceLast) +" miliseconds before you may buy again");
  62. return;
  63. }
  64. GSPlayer gsp = plugin.getCalc().getPlayer(player.getName());
  65. if ((gsp.getBuyItem() <= 0) || (gsp.getBuyQty()) <= 0){
  66. player.sendMessage(ChatColor.RED.toString() +"You must first set an item and quantity to buy with /gsset.");
  67. return;
  68. }
  69. if (!plugin.validBuy(player))
  70. return;
  71. action = new BuyProcedure(plugin,player,stuff);
  72. action.execute(gsp.getBuyItem(), gsp.getBuyQty());
  73. return;
  74. }
  75. long timeSinceLast = plugin.getCalc().timeSinceSold(player);
  76. if (timeSinceLast < plugin.getSellCooldown()){
  77. if ((plugin.getSellCooldown()-timeSinceLast) >= 1000)
  78. player.sendMessage(ChatColor.RED.toString() +"You must wait "+
  79. (plugin.getSellCooldown()-timeSinceLast)/1000 +" seconds before you may sell again");
  80. else
  81. player.sendMessage(ChatColor.RED.toString() +"You must wait "+
  82. (plugin.getSellCooldown()-timeSinceLast) +" miliseconds before you may sell again");
  83. return;
  84. }
  85. if (containsAGSItem(player,stuff)){
  86. if (!plugin.hasPermissions(player, "goldstandard.sell")){
  87. player.sendMessage(ChatColor.RED.toString() +"You do not have permission to sell.");
  88. return;
  89. }
  90. if (!plugin.getProtection().canSellFrom(player, event.getBlock())){
  91. player.sendMessage(ChatColor.RED.toString() +"You do not have access to this "+SellObject+".");
  92. return;
  93. }
  94. //start selling
  95. TIntIntHashMap itemsSold = new TIntIntHashMap();
  96. TIntShortHashMap blockOptions = new TIntShortHashMap();
  97. GSPlayer gsp = plugin.getCalc().getPlayer(player.getName());
  98. if (gsp == null){
  99. Logger.getLogger("Minecraft").info("[GoldStandard] Null player tried to sell!");
  100. return;
  101. }
  102. for (ItemStack is : stuff.getContents()){
  103. if (is != null){
  104. if (gsp.isInSellList(is.getTypeId())){
  105. if (!plugin.validSale(player, is.getTypeId()))
  106. continue;
  107. itemsSold.putIfAbsent(is.getTypeId(), 0);
  108. itemsSold.adjustValue(is.getTypeId(),is.getAmount());
  109. blockOptions.putIfAbsent(is.getTypeId(), (short) 0);
  110. }
  111. else if (gsp.itemCanBeBlock(GSItem.reverseGetBlock(is.getTypeId()))){
  112. if (!plugin.validSale(player, GSItem.reverseGetBlock(is.getTypeId())))
  113. continue;
  114. if (!plugin.getGSItem(GSItem.reverseGetBlock(is.getTypeId())).blockAllowed())
  115. continue;
  116. itemsSold.putIfAbsent(GSItem.reverseGetBlock(is.getTypeId()), 0);
  117. itemsSold.adjustValue(GSItem.reverseGetBlock(is.getTypeId()),is.getAmount()*9);
  118. blockOptions.putIfAbsent(GSItem.reverseGetBlock(is.getTypeId()), (short) 1);
  119. }
  120. }
  121. }
  122. for (int i : blockOptions.keys()){
  123. new SellProcedure(plugin,player,stuff,blockOptions.get(i)!=0,false).execute(i, itemsSold.get(i));
  124. }
  125. }
  126. }
  127. }
  128. private boolean isEmpty(Inventory i){
  129. for (ItemStack is : i.getContents())
  130. if (is != null)
  131. return false;
  132. return true;
  133. }
  134. private boolean containsAGSItem(Player player, Inventory i){
  135. for (ItemStack is : i.getContents()){
  136. if (is == null)
  137. continue;
  138. else if (plugin.getItems().containsKey(is.getTypeId()))
  139. return true;
  140. else if (plugin.getItems().containsKey(GSItem.reverseGetBlock(is.getTypeId())))
  141. if (plugin.getGSItem(GSItem.reverseGetBlock(is.getTypeId())).blockAllowed())
  142. return true;
  143. }
  144. return false;
  145. }
  146. }