AlchemyPotionBrewer.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package com.gmail.nossr50.skills.alchemy;
  2. import com.gmail.nossr50.config.skills.alchemy.PotionConfig;
  3. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  4. import com.gmail.nossr50.datatypes.skills.alchemy.AlchemyPotion;
  5. import com.gmail.nossr50.datatypes.skills.alchemy.PotionStage;
  6. import com.gmail.nossr50.events.fake.FakeBrewEvent;
  7. import com.gmail.nossr50.mcMMO;
  8. import com.gmail.nossr50.runnables.player.PlayerUpdateInventoryTask;
  9. import com.gmail.nossr50.runnables.skills.AlchemyBrewCheckTask;
  10. import com.gmail.nossr50.util.Permissions;
  11. import org.bukkit.Material;
  12. import org.bukkit.block.BlockState;
  13. import org.bukkit.block.BrewingStand;
  14. import org.bukkit.entity.HumanEntity;
  15. import org.bukkit.entity.Player;
  16. import org.bukkit.event.inventory.ClickType;
  17. import org.bukkit.inventory.BrewerInventory;
  18. import org.bukkit.inventory.Inventory;
  19. import org.bukkit.inventory.InventoryView;
  20. import org.bukkit.inventory.ItemStack;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. public final class AlchemyPotionBrewer {
  24. public static boolean isValidBrew(Player player, ItemStack[] contents) {
  25. if (!isValidIngredient(player, contents[Alchemy.INGREDIENT_SLOT])) {
  26. return false;
  27. }
  28. for (int i = 0; i < 3; i++) {
  29. if (contents[i] == null || contents[i].getType() != Material.POTION && contents[i].getType() != Material.SPLASH_POTION && contents[i].getType() != Material.LINGERING_POTION) {
  30. continue;
  31. }
  32. if (getChildPotion(PotionConfig.getInstance().getPotion(contents[i]), contents[Alchemy.INGREDIENT_SLOT]) != null) {
  33. return true;
  34. }
  35. }
  36. return false;
  37. }
  38. private static AlchemyPotion getChildPotion(AlchemyPotion potion, ItemStack ingredient) {
  39. if (potion != null) {
  40. return potion.getChild(ingredient);
  41. }
  42. return null;
  43. }
  44. public static boolean isEmpty(ItemStack item) {
  45. return item == null || item.getType() == Material.AIR || item.getAmount() == 0;
  46. }
  47. private static void removeIngredient(BrewerInventory inventory, Player player) {
  48. if(inventory.getIngredient() == null)
  49. return;
  50. ItemStack ingredient = inventory.getIngredient().clone();
  51. if (!isEmpty(ingredient) && isValidIngredient(player, ingredient)) {
  52. if (ingredient.getAmount() <= 1) {
  53. inventory.setIngredient(null);
  54. }
  55. else {
  56. ingredient.setAmount(ingredient.getAmount() - 1);
  57. inventory.setIngredient(ingredient);
  58. }
  59. }
  60. }
  61. private static boolean hasIngredient(BrewerInventory inventory, Player player) {
  62. ItemStack ingredient = inventory.getIngredient() == null ? null : inventory.getIngredient().clone();
  63. return !isEmpty(ingredient) && isValidIngredient(player, ingredient);
  64. }
  65. public static boolean isValidIngredient(Player player, ItemStack item) {
  66. if (isEmpty(item)) {
  67. return false;
  68. }
  69. for (ItemStack ingredient : getValidIngredients(player)) {
  70. if (item.isSimilar(ingredient)) {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. private static List<ItemStack> getValidIngredients(Player player) {
  77. if(player == null || mcMMO.getUserManager().queryPlayer(player) == null)
  78. {
  79. return PotionConfig.getInstance().getIngredients(1);
  80. }
  81. return PotionConfig.getInstance().getIngredients(!Permissions.isSubSkillEnabled(player, SubSkillType.ALCHEMY_CONCOCTIONS) ? 1 : mcMMO.getUserManager().queryPlayer(player).getAlchemyManager().getTier());
  82. }
  83. public static void finishBrewing(BlockState brewingStand, Player player, boolean forced) {
  84. if (!(brewingStand instanceof BrewingStand)) {
  85. return;
  86. }
  87. BrewerInventory inventory = ((BrewingStand) brewingStand).getInventory();
  88. ItemStack ingredient = inventory.getIngredient() == null ? null : inventory.getIngredient().clone();
  89. if (!hasIngredient(inventory, player)) {
  90. return;
  91. }
  92. List<AlchemyPotion> inputList = new ArrayList<>();
  93. ItemStack[] outputList = new ItemStack[3];
  94. for (int i = 0; i < 3; i++) {
  95. ItemStack item = inventory.getItem(i);
  96. if (isEmpty(item) || item.getType() == Material.GLASS_BOTTLE || !PotionConfig.getInstance().isValidPotion(item)) {
  97. continue;
  98. }
  99. AlchemyPotion input = PotionConfig.getInstance().getPotion(item);
  100. AlchemyPotion output = input.getChild(ingredient);
  101. inputList.add(input);
  102. if (output != null) {
  103. outputList[i] = output.toItemStack(item.getAmount()).clone();
  104. }
  105. }
  106. FakeBrewEvent event = new FakeBrewEvent(brewingStand.getBlock(), inventory, ((BrewingStand) brewingStand).getFuelLevel());
  107. mcMMO.p.getServer().getPluginManager().callEvent(event);
  108. if (event.isCancelled() || inputList.isEmpty()) {
  109. return;
  110. }
  111. for (int i = 0; i < 3; i++) {
  112. if(outputList[i] != null) {
  113. inventory.setItem(i, outputList[i]);
  114. }
  115. }
  116. removeIngredient(inventory, player);
  117. for (AlchemyPotion input : inputList) {
  118. AlchemyPotion output = input.getChild(ingredient);
  119. if (output != null && player != null) {
  120. PotionStage potionStage = PotionStage.getPotionStage(input, output);
  121. //TODO: hmm
  122. if (mcMMO.getUserManager().hasPlayerDataKey(player)) {
  123. mcMMO.getUserManager().queryPlayer(player).getAlchemyManager().handlePotionBrewSuccesses(potionStage, 1);
  124. }
  125. }
  126. }
  127. if (!forced) {
  128. scheduleUpdate(inventory);
  129. }
  130. }
  131. public static boolean transferItems(InventoryView view, int fromSlot, ClickType click) {
  132. boolean success = false;
  133. if (click.isLeftClick()) {
  134. success = transferItems(view, fromSlot);
  135. }
  136. else if (click.isRightClick()) {
  137. success = transferOneItem(view, fromSlot);
  138. }
  139. return success;
  140. }
  141. private static boolean transferOneItem(InventoryView view, int fromSlot) {
  142. ItemStack from = view.getItem(fromSlot).clone();
  143. ItemStack to = view.getItem(Alchemy.INGREDIENT_SLOT).clone();
  144. if (isEmpty(from)) {
  145. return false;
  146. }
  147. boolean emptyTo = isEmpty(to);
  148. int fromAmount = from.getAmount();
  149. if (!emptyTo && fromAmount >= from.getType().getMaxStackSize()) {
  150. return false;
  151. }
  152. else if (emptyTo || from.isSimilar(to)) {
  153. if (emptyTo) {
  154. to = from.clone();
  155. to.setAmount(1);
  156. }
  157. else {
  158. to.setAmount(to.getAmount() + 1);
  159. }
  160. from.setAmount(fromAmount - 1);
  161. view.setItem(Alchemy.INGREDIENT_SLOT, to);
  162. view.setItem(fromSlot, from);
  163. return true;
  164. }
  165. return false;
  166. }
  167. /**
  168. * Transfer items between two ItemStacks, returning the leftover status
  169. */
  170. private static boolean transferItems(InventoryView view, int fromSlot) {
  171. ItemStack from = view.getItem(fromSlot).clone();
  172. ItemStack to = view.getItem(Alchemy.INGREDIENT_SLOT).clone();
  173. if (isEmpty(from)) {
  174. return false;
  175. }
  176. else if (isEmpty(to)) {
  177. view.setItem(Alchemy.INGREDIENT_SLOT, from);
  178. view.setItem(fromSlot, null);
  179. return true;
  180. }
  181. else if (from.isSimilar(to)) {
  182. int fromAmount = from.getAmount();
  183. int toAmount = to.getAmount();
  184. int maxSize = to.getType().getMaxStackSize();
  185. if (fromAmount + toAmount > maxSize) {
  186. int left = fromAmount + toAmount - maxSize;
  187. to.setAmount(maxSize);
  188. view.setItem(Alchemy.INGREDIENT_SLOT, to);
  189. from.setAmount(left);
  190. view.setItem(fromSlot, from);
  191. return true;
  192. }
  193. to.setAmount(fromAmount + toAmount);
  194. view.setItem(fromSlot, null);
  195. view.setItem(Alchemy.INGREDIENT_SLOT, to);
  196. return true;
  197. }
  198. return false;
  199. }
  200. public static void scheduleCheck(Player player, BrewingStand brewingStand) {
  201. new AlchemyBrewCheckTask(player, brewingStand).runTask(mcMMO.p);
  202. }
  203. public static void scheduleUpdate(Inventory inventory) {
  204. for (HumanEntity humanEntity : inventory.getViewers()) {
  205. if (humanEntity instanceof Player) {
  206. new PlayerUpdateInventoryTask((Player) humanEntity).runTask(mcMMO.p);
  207. }
  208. }
  209. }
  210. }