/src/main/java/com/edoxile/bukkit/bettermechanics/utils/BlockbagUtil.java

https://github.com/Retinue/BetterMechanics · Java · 147 lines · 136 code · 8 blank · 3 comment · 57 complexity · 94979d1077f6701c6238edb35d9003d1 MD5 · raw file

  1. package com.edoxile.bukkit.bettermechanics.utils;
  2. import java.util.logging.Logger;
  3. import org.bukkit.Material;
  4. import org.bukkit.block.Block;
  5. import org.bukkit.block.BlockState;
  6. import org.bukkit.block.Chest;
  7. import org.bukkit.inventory.ItemStack;
  8. import com.edoxile.bukkit.bettermechanics.exceptions.OutOfMaterialException;
  9. import com.edoxile.bukkit.bettermechanics.exceptions.OutOfSpaceException;
  10. /**
  11. * Created by IntelliJ IDEA. User: Edoxile
  12. */
  13. public class BlockbagUtil {
  14. private static final Logger log = Logger.getLogger("Minecraft");
  15. public static boolean safeRemoveItems(Chest chest, ItemStack itemStack)
  16. throws OutOfMaterialException {
  17. boolean checkData = true;
  18. if (itemStack.getData() == null) {
  19. checkData = false;
  20. }
  21. ItemStack[] stacks = chest.getInventory().getContents();
  22. ItemStack tempStack;
  23. for (int i = 0; i < stacks.length; i++) {
  24. tempStack = stacks[i];
  25. if (tempStack == null)
  26. continue;
  27. if (checkData) {
  28. if (tempStack.getData() == null) {
  29. continue;
  30. }
  31. if (!tempStack.getData().equals(itemStack.getData())) {
  32. continue;
  33. }
  34. } else {
  35. if (tempStack.getType() != itemStack.getType()) {
  36. continue;
  37. }
  38. }
  39. if (tempStack.getAmount() > itemStack.getAmount()) {
  40. tempStack.setAmount(tempStack.getAmount()
  41. - itemStack.getAmount());
  42. itemStack.setAmount(0);
  43. stacks[i] = tempStack;
  44. break;
  45. } else if (tempStack.getAmount() < itemStack.getAmount()) {
  46. stacks[i] = null;
  47. itemStack.setAmount(itemStack.getAmount()
  48. - tempStack.getAmount());
  49. continue;
  50. } else {
  51. stacks[i] = null;
  52. itemStack.setAmount(0);
  53. break;
  54. }
  55. }
  56. if (itemStack.getAmount() > 0) {
  57. log.warning("[BetterMechanics] Not enough items in chest, no changes were made.");
  58. log.warning("[BetterMechanics] Chest location: "
  59. + chest.getBlock().getLocation().toString() + ".");
  60. throw new OutOfMaterialException(itemStack.getAmount());
  61. } else {
  62. chest.getInventory().setContents(stacks);
  63. return true;
  64. }
  65. }
  66. public static boolean safeAddItems(Chest chest, ItemStack itemStack)
  67. throws OutOfSpaceException {
  68. int amount = itemStack.getAmount();
  69. ItemStack[] stacks = chest.getInventory().getContents();
  70. for (int i = 0; i < stacks.length; i++) {
  71. if (stacks[i] == null) {
  72. if (amount > 64) {
  73. if (itemStack.getData() == null) {
  74. stacks[i] = new ItemStack(itemStack.getType(), 64);
  75. } else {
  76. stacks[i] = itemStack.getData().toItemStack(64);
  77. }
  78. amount -= 64;
  79. } else {
  80. if (itemStack.getData() == null) {
  81. stacks[i] = new ItemStack(itemStack.getType(), amount);
  82. } else {
  83. stacks[i] = itemStack.getData().toItemStack(amount);
  84. }
  85. amount = 0;
  86. }
  87. } else {
  88. if (stacks[i].getType() != itemStack.getType()
  89. || stacks[i].getAmount() == stacks[i].getMaxStackSize()) {
  90. continue;
  91. } else {
  92. if (stacks[i].getData() == null
  93. && itemStack.getData() == null
  94. || stacks[i].getData() != null
  95. && stacks[i].getData().equals(itemStack.getData())) {
  96. if (stacks[i].getAmount() + amount > 64) {
  97. if (itemStack.getData() == null) {
  98. stacks[i].setAmount(64);
  99. } else {
  100. stacks[i] = itemStack.getData().toItemStack(64);
  101. }
  102. amount = stacks[i].getAmount() + amount - 64;
  103. } else {
  104. if (itemStack.getData() == null) {
  105. stacks[i].setAmount(stacks[i].getAmount()
  106. + amount);
  107. } else {
  108. stacks[i].setAmount(stacks[i].getAmount()
  109. + amount);
  110. }
  111. amount = 0;
  112. }
  113. }
  114. }
  115. }
  116. if (amount == 0) {
  117. break;
  118. }
  119. }
  120. if (amount > 0) {
  121. log.warning("[BetterMechanics] Not enough space in chest, no changes were made.");
  122. log.warning("[BetterMechanics] Chest location: "
  123. + chest.getBlock().getLocation().toString() + ".");
  124. throw new OutOfSpaceException();
  125. } else {
  126. chest.getInventory().setContents(stacks);
  127. return true;
  128. }
  129. }
  130. public static Chest getChest(Block block) {
  131. if (block.getType() == Material.CHEST) {
  132. BlockState s = block.getState();
  133. if (s instanceof Chest) {
  134. return (Chest) s;
  135. }
  136. }
  137. return null;
  138. }
  139. }