/src/net/edoxile/bettermechanics/utils/BlockbagUtil.java

https://github.com/GuntherDW/BetterMechanics · Java · 134 lines · 124 code · 6 blank · 4 comment · 57 complexity · 478090db2fa81daf72f2504495545a0d MD5 · raw file

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