AlchemyPotionBrewer.java 8.7 KB

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