broken-arrow

Untitled

Jul 1st, 2021 (edited)
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.46 KB | None | 0 0
  1.  
  2. import org.broken.cheststorage.Crafting.FilterItems;
  3. import org.broken.cheststorage.data.ChestRegistry;
  4. import org.broken.cheststorage.data.LoadSettingsContainers;
  5. import org.broken.cheststorage.event.MoveItemsFromToContainerListner;
  6. import org.broken.cheststorage.inventoryholders.InventoryHolderDefultchest;
  7. import org.bukkit.Location;
  8. import org.bukkit.block.Hopper;
  9. import org.bukkit.inventory.Inventory;
  10. import org.bukkit.inventory.ItemStack;
  11.  
  12. import java.math.BigInteger;
  13. import java.util.Map;
  14. import java.util.Objects;
  15.  
  16. public class HopperMoveTask {
  17.  
  18.     public void moveItemTask(Location location, ChestRegistry registry) {
  19.         //if (!PluginConfig.SHOULD_RUN_HOPPERS_UNLOADED_CHUNKS.get() && !Utils.isLocationChunkLoaded(location.getLocation()))
  20.         //  continue;
  21.         Location below = location.clone().subtract(0, 1, 0);
  22.         if (below.getBlock().getState() instanceof Hopper) {
  23.             Hopper hopper = (Hopper) below.getBlock().getState();
  24.             if (below.getBlock().isBlockIndirectlyPowered() || below.getBlock().isBlockPowered()) {
  25.                 //System.out.println("testings");
  26.                 return;
  27.             }
  28.             if (Objects.equals(registry.getTypeofContainer(location), ChestRegistry.TypeOfContainer.STORAGEUNIT)) {
  29.                 addItemsToHopper(registry, hopper.getLocation(), location, hopper.getInventory());
  30.  
  31.             } else {
  32.  
  33.                 LoadSettingsContainers containers = LoadSettingsContainers.getInstance();
  34.                 String settingName = registry.getContainerSettingsName(location);
  35.                 int guiSize = containers.getContainerType(settingName).getGuiSize();
  36.  
  37.                 InventoryHolderDefultchest holder = new InventoryHolderDefultchest(location, null, registry.getAmountOfPages(location), guiSize);
  38.  
  39.                 Inventory inventory = holder.getEmptyPages(registry);
  40.  
  41.  
  42.                 if (move(hopper.getLocation(), inventory, hopper.getInventory(), location)) {
  43.                     //System.out.println(inventory + "  " + location);
  44.                     //registry.updateDisplayItem();
  45.                     System.out.println("test");
  46.                 }
  47.             }
  48.         }
  49.         //if (registry.getInventory().getViewers().size() > 0) registry.sort();
  50.  
  51.     }
  52.     //}
  53.     //  }
  54.     //}
  55.  
  56.  
  57.     public static boolean move(Location targetLocation, Inventory source, Inventory target, Location location) {
  58.         int hopperAmount = 1;
  59.         return MoveItemsFromToContainerListner.hopperMoveItemFromChest(source, hopperAmount, target, location, targetLocation);
  60.  
  61.     }
  62.  
  63.     public ItemStack moveItemsFromStorageUnit(ChestRegistry registry, Location targetLocation, Location location) {
  64.         ItemStack[] itemstacks = registry.getcontents(location);
  65.         ItemStack RemoveItemsFromContainerBefore;
  66.         ItemStack stack = null;
  67.         if (itemstacks != null && itemstacks.length > 0)
  68.             stack = itemstacks[0];
  69.  
  70.         FilterItems filter = new FilterItems();
  71.         boolean customfilterContainer = true;
  72.         if (targetLocation != null && registry.getTypeofContainer(targetLocation) != null)
  73.             if (stack != null && registry.getTypeofContainer(targetLocation).equals(ChestRegistry.TypeOfContainer.LINKEDCONTAINER)) {
  74.                 customfilterContainer = filter.checkIfItemMatch(targetLocation, stack);
  75.             }
  76.  
  77.         if (stack != null && filter.filerItems(stack, location) && customfilterContainer) {
  78.             RemoveItemsFromContainerBefore = stack.clone();
  79.             RemoveItemsFromContainerBefore.setAmount(Math.min(Integer.parseInt(registry.getAmontOfItems(location).toString()), 1));
  80.             stack.setAmount(RemoveItemsFromContainerBefore.getAmount() - Integer.parseInt(registry.getAmontOfItems(location).toString()));
  81.             //System.out.println("amnount of items" + registry.getAmontOfItems(location).toString());
  82.             //System.out.println("stackamount " + stack.getAmount() + "removed items " + RemoveItemsFromContainerBefore);
  83.             registry.setAmontOfItems(location, BigInteger.valueOf((Integer.parseInt(registry.getAmontOfItems(location).toString()) - RemoveItemsFromContainerBefore.getAmount())));
  84.             return RemoveItemsFromContainerBefore;
  85.         }
  86.         return null;
  87.     }
  88.  
  89.  
  90.     public boolean addItemsToHopper(ChestRegistry registry, Location targetLocation, Location location, Inventory toInventory) {
  91.         /*ItemStack itemStack = moveItemsFromStorageUnit(registry, targetLocation, location);
  92.         if (itemStack != null) {
  93.             HashMap<Integer, ItemStack> leftOvers = toInventory.addItem(itemStack);
  94.  
  95.             return leftOvers.isEmpty();
  96.         }*/
  97.  
  98.         ItemStack[] itemstacks = registry.getcontents(location);
  99.         Map<Integer, ItemStack> leftOvers;
  100.         ItemStack itemToAdd = null;
  101.         if (itemstacks != null && itemstacks.length > 0) {
  102.             itemToAdd = itemstacks[0];
  103.         }
  104.         if (itemToAdd != null) {
  105.             int items = 1;
  106.  
  107.             do {
  108.                 itemToAdd.setAmount(items);
  109.                 leftOvers = toInventory.addItem(itemToAdd);
  110.                 items++;
  111.             } while (!leftOvers.isEmpty() || registry.getAmontOfItems(location).toString().equals("0"));
  112.  
  113.             BigInteger subtractedValues = registry.getAmontOfItems(location).subtract(BigInteger.valueOf(items));
  114.             registry.setAmontOfItems(location, subtractedValues);
  115.             /*int newAmount = registry.getAmontOfItems(location).min(BigInteger.valueOf(64)).intValue();
  116.             itemToAdd.setAmount(newAmount);
  117.             if (registry.getAmontOfItems(location).intValue() <= 64) {
  118.                 Map<Integer, ItemStack> leftOvers = toInventory.addItem(itemToAdd);
  119.                 if (!leftOvers.isEmpty()) {
  120.                     ItemStack leftOver = leftOvers.get(0);
  121.                     if (leftOver.getAmount() == newAmount) {
  122.                         return false;
  123.                     } else {
  124.                         newAmount -= leftOver.getAmount();
  125.  
  126.                     }
  127.                 }
  128.                 BigInteger subtractedValues = registry.getAmontOfItems(location).subtract(BigInteger.valueOf(newAmount));
  129.                 registry.setAmontOfItems(location, subtractedValues);
  130.             }*/
  131.         }
  132.         return false;
  133.     }
  134. }
Add Comment
Please, Sign In to add comment