SkyVox

InventoryItemManipulation

Mar 23rd, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.15 KB | None | 0 0
  1. package me.skyvox.api;
  2.  
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.Location;
  8. import org.bukkit.Material;
  9. import org.bukkit.block.Block;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.inventory.ItemStack;
  12.  
  13. /**
  14.  *
  15.  * @author SkyVox
  16.  *
  17.  */
  18.  
  19. public class InventoryItemManipulation {
  20.     private boolean full;
  21.     private int rest;
  22.    
  23.     // CONSTRUCTOR
  24.     public InventoryItemManipulation(boolean full, int rest) {
  25.         this.full = full;
  26.         this.rest = rest;
  27.     }
  28.    
  29.     public boolean isFull() {
  30.         return full;
  31.     }
  32.    
  33.     public boolean hasRest() {
  34.         return rest > 0;
  35.     }
  36.    
  37.     public int getRest() {
  38.         return rest;
  39.     }
  40.    
  41.     public void send(Player player, ItemStack item, Location location) {
  42.         ItemStack clone = item.clone();
  43.        
  44.         if (!full) {
  45.             clone.setAmount(clone.getAmount() - getRest());
  46.             player.getInventory().addItem(clone);
  47.         }
  48.        
  49.         if (hasRest()) {
  50.             clone.setAmount(getRest());
  51.             send(clone, location);
  52.         }
  53.     }
  54.    
  55.     private void send(ItemStack item, Location location) {
  56.         location.getWorld().dropItem(location, item);
  57.     }
  58.    
  59.     /*
  60.      * Static Methods.
  61.      */
  62.    
  63.     /**
  64.      * Check if ItemStacks are similar.
  65.      *
  66.      * @param paramOne - Item to compare
  67.      * @param paramTwo - Item to compare
  68.      * @return if @paramOne is similar than @paramTwo
  69.      */
  70.     private static boolean isSimilar(ItemStack paramOne, ItemStack paramTwo) {
  71.         return isSimilar(paramOne, paramTwo, true);
  72.     }
  73.    
  74.     public static boolean isSimilar(ItemStack paramOne, ItemStack paramTwo, boolean value) {
  75.         if ((paramOne == null) || (paramTwo == null)) return false;
  76.        
  77.         boolean meta = (paramOne.hasItemMeta() == paramTwo.hasItemMeta());
  78.        
  79.         if (meta && value) {
  80.             meta = Bukkit.getItemFactory().equals(paramOne.getItemMeta(), paramTwo.getItemMeta());
  81.         }
  82.        
  83.         if ((paramOne.getDurability() == paramTwo.getDurability()) && (paramOne.getType().toString().toUpperCase().equals(paramTwo.getType().toString().toUpperCase())) && (paramOne.getEnchantments().equals(paramTwo.getEnchantments())) && ((value && meta) || (!value))) return true;
  84.        
  85.         // These items are not similar.
  86.         return false;
  87.     }
  88.    
  89.     /**
  90.      * This method will simple check
  91.      * if player's inventory is full.
  92.      *
  93.      * @param player
  94.      * @return if player inventory is full
  95.      */
  96.     public static boolean isFull(Player player) {
  97.         ItemStack item = null;
  98.        
  99.         // Loop all inventory items, This will start on the hotbar slot.
  100.         for (int i = player.getInventory().getSize(); i > 0; i--) {
  101.             item = player.getInventory().getItem(i);
  102.            
  103.             // If the item is equals null or the Material type is AIR
  104.             // This mean that have an empty slot.
  105.             if ((item == null) || (item.getType() == Material.AIR)) {
  106.                 return false;
  107.             }
  108.         }
  109.        
  110.         // Player's inventory is full.
  111.         return true;
  112.     }
  113.    
  114.     /**
  115.      * Check if player's inventory is full.
  116.      * This will compare two items and its amount.
  117.      *
  118.      * @param player
  119.      * @param drop - Item to compare
  120.      * @return new Object of InventoryAPI
  121.      */
  122.     public static InventoryItemManipulation isFull(Player player, ItemStack drop) {
  123.         ItemStack item = null;
  124.         int item_amount = 0;
  125.         int drop_amount = drop.getAmount();
  126.        
  127.         // Loop all inventory items, This will start on the hotbar slot.
  128.         for (int i = player.getInventory().getSize(); i > 0; i--) {
  129.             item = player.getInventory().getItem(i);
  130.            
  131.             if ((item == null) || (item.getType() == Material.AIR)) {
  132.                 if (drop_amount > 64) {
  133.                     drop_amount-=64;
  134.                     continue;
  135.                 }
  136.                
  137.                 return new InventoryItemManipulation(false, 0);
  138.             } else if (isSimilar(item, drop)) {
  139.                 item_amount = item.getAmount();
  140.                
  141.                 if (item_amount >= item.getMaxStackSize()) {
  142.                     continue;
  143.                 } else {
  144.                     while (item_amount < item.getMaxStackSize()) {
  145.                         // Check if drop amount is less or equals than zero.
  146.                         if (drop_amount <= 0) return new InventoryItemManipulation(false, 0);
  147.                        
  148.                         drop_amount-=1;
  149.                         item_amount+=1;
  150.                     }
  151.                 }
  152.                
  153.                 // Again check if drop amount is less or equals than zero.
  154.                 if (drop_amount <= 0) return new InventoryItemManipulation(false, 0);
  155.             }
  156.         }
  157.        
  158.         // Player's inventory is full.
  159.         return new InventoryItemManipulation(drop_amount == drop.getAmount(), drop_amount);
  160.     }
  161.    
  162.     /**
  163.      * Check if player's inventory is full.
  164.      * This will compare two items and its amount.
  165.      *
  166.      * @param player
  167.      * @param drop - Item to compare instanced a Block
  168.      * @return if player's inventory is full
  169.      */
  170.     public static InventoryItemManipulation isFull(Player player, Block drop) {
  171.         Collection<ItemStack> items = drop.getDrops(player.getItemInHand());
  172.        
  173.         if ((items == null) || (items.isEmpty())) {
  174.             return new InventoryItemManipulation(false, 0);
  175.         }
  176.        
  177.         Iterator<ItemStack> item = items.iterator();
  178.        
  179.         if (item.hasNext()) {
  180.             return isFull(player, item.next());
  181.         }
  182.        
  183.         return new InventoryItemManipulation(false, 0);
  184.     }
  185.    
  186.     /**
  187.      * Check if a player has certain amount of
  188.      * a item on his inventory.
  189.      *
  190.      * @param player
  191.      * @param item - item to check
  192.      * @param amount - amount to check
  193.      * @return if player has certain amount of a item.
  194.      */
  195.     public static boolean has(Player player, ItemStack item, int amount) {
  196.         return has(player, item, amount, true);
  197.     }
  198.    
  199.     /**
  200.      * Check if a player has certain amount of
  201.      * a item on his inventory.
  202.      *
  203.      * @param player
  204.      * @param item - item to check
  205.      * @param amount - amount to check
  206.      * @param value - compare display-name
  207.      * @return if player has certain amount of a item.
  208.      */
  209.     public static boolean has(Player player, ItemStack item, int amount, boolean value) {
  210.         if (item == null) return false;
  211.         int player_item_amount = 0;
  212.        
  213.         for (ItemStack items : player.getInventory().all(item.getType()).values()) {
  214.             if (player_item_amount >= amount) break;
  215.            
  216.             if (isSimilar(item, items, value)) {
  217.                 player_item_amount+=items.getAmount();
  218.             }
  219.         }
  220.        
  221.         return player_item_amount >= amount;
  222.     }
  223.    
  224.     /**
  225.      * Remove an specific item from player's inventory.
  226.      *
  227.      * @param player
  228.      * @param item - item to compare
  229.      * @param bypass - remove if item has different name?
  230.      */
  231.     public static void take(Player player, ItemStack item, boolean bypass) {
  232.         // Call the take method.
  233.         take(player, item, 1, bypass);
  234.     }
  235.    
  236.     /**
  237.      * Remove an specific item from player's inventory.
  238.      *
  239.      * If bypass==false, We'll check the name as well.
  240.      * If bypass==true, Item name doesn't matter in this case.
  241.      *
  242.      * @param player
  243.      * @param item - item to compare
  244.      * @param amount - amount of item to take
  245.      * @param bypass - remove if item has different name?
  246.      */
  247.     public static void take(Player player, ItemStack item, int amount, boolean bypass) {
  248.         if (item == null) return;
  249.        
  250.         boolean display = false;
  251.        
  252.         if (!bypass) {
  253.             if ((!(item.hasItemMeta())) || (!(item.getItemMeta().hasDisplayName()))) return;
  254.            
  255.             display = true;
  256.         }
  257.        
  258.         if (!has(player, item, amount, display)) return;
  259.        
  260.         // Take this item.
  261.         takeFromInventory(player, item, amount, display);
  262.     }
  263.    
  264.     /**
  265.      * Take a certain amount of item from
  266.      * player's inventory.
  267.      *
  268.      * @param player
  269.      * @param item - item to take
  270.      * @param amount - amount of item to take
  271.      * @param value - compare display-name
  272.      * @throws NullPointerException
  273.      */
  274.     private static void takeFromInventory(Player player, ItemStack item, int amount, boolean value) throws NullPointerException {
  275.         ItemStack items = null;
  276.         int remove = 0;
  277.        
  278.         for (int i = player.getInventory().getSize(); i > -1; i--) {
  279.             items = player.getInventory().getItem(i);
  280.            
  281.             if ((items == null) || (items.getType() == Material.AIR)) continue;
  282.             if (!isSimilar(item, items, value)) continue;
  283.            
  284.             while ((items.getAmount() - remove) > 0) {
  285.                 if (amount <= 0) break;
  286.                
  287.                 remove+=1;
  288.                 amount-=1;
  289.             }
  290.            
  291.             // Set item amount.
  292.             items.setAmount(items.getAmount() - remove);
  293.            
  294.             // Take this item.
  295.             player.getInventory().clear(i);
  296.            
  297.             if (items.getAmount() > 0) {
  298.                 // Add the item again if item amount is more than one.
  299.                 player.getInventory().addItem(items);
  300.             }
  301.             remove = 0;
  302.            
  303.             if (amount <= 0) break;
  304.         }
  305.        
  306.         // Update player inventory.
  307.         player.updateInventory();
  308.     }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment