Advertisement
imBEheAR

Untitled

Jul 7th, 2021
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1.  
  2. import org.bukkit.Material;
  3. import org.bukkit.entity.Player;
  4. import org.bukkit.inventory.ItemStack;
  5.  
  6. public class InventoryHelper {
  7.  
  8.  
  9.  
  10. public static int getItemAmount(final Player player, final ItemStack is) {
  11. int amount = 0;
  12. ItemStack[] contents;
  13. for (int length = (contents = player.getInventory().getContents()).length, i = 0; i < length; ++i) {
  14. final ItemStack itemStack = contents[i];
  15. if(is.hasItemMeta() && is.getItemMeta().hasDisplayName()) {
  16. if (itemStack != null && itemStack.getType() == is.getType() && itemStack.getDurability() == is.getDurability()
  17. && (itemStack.hasItemMeta() && itemStack.getItemMeta().hasDisplayName() && itemStack.getItemMeta().equals(is.getItemMeta()))) {
  18. amount += itemStack.getAmount();
  19. }
  20. }else{
  21. if (itemStack != null && itemStack.getType() == is.getType() && itemStack.getDurability() == is.getDurability()) {
  22. amount += itemStack.getAmount();
  23. }
  24. }
  25. }
  26. return amount;
  27. }
  28.  
  29. public static void removeItemAmount(Player player, Material material, byte data, int amount){
  30. removeItems(new ItemStack(material, 1, data),player,amount);
  31. }
  32.  
  33.  
  34. public static void removeItems(final ItemStack is, final Player p, int amount) {
  35. ItemStack[] contents;
  36. for (int length = (contents = p.getInventory().getContents()).length, i = 0; i < length; ++i) {
  37. final ItemStack itemStack = contents[i];
  38.  
  39. if(is.hasItemMeta() && is.getItemMeta().hasDisplayName()){
  40. if (amount > 0 && itemStack != null && itemStack.getType() == is.getType() && itemStack.getDurability() == is.getDurability()
  41. && (itemStack.hasItemMeta() && itemStack.getItemMeta().hasDisplayName() && itemStack.getItemMeta().equals(is.getItemMeta()))
  42. ) {
  43. if (itemStack.getAmount() > amount) {
  44. itemStack.setAmount(itemStack.getAmount() - amount);
  45. amount = 0;
  46. }
  47. else {
  48. amount -= itemStack.getAmount();
  49. p.getInventory().removeItem(itemStack);
  50. }
  51. }
  52. }else{
  53. if (amount > 0 && itemStack != null && itemStack.getType() == is.getType() && itemStack.getDurability() == is.getDurability()) {
  54. if (itemStack.getAmount() > amount) {
  55. itemStack.setAmount(itemStack.getAmount() - amount);
  56. amount = 0;
  57. }
  58. else {
  59. amount -= itemStack.getAmount();
  60. p.getInventory().removeItem(itemStack);
  61. }
  62. }
  63. }
  64. }
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement