Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.broken.cheststorage.Crafting.FilterItems;
- import org.broken.cheststorage.data.ChestRegistry;
- import org.broken.cheststorage.data.LoadSettingsContainers;
- import org.broken.cheststorage.event.MoveItemsFromToContainerListner;
- import org.broken.cheststorage.inventoryholders.InventoryHolderDefultchest;
- import org.bukkit.Location;
- import org.bukkit.block.Hopper;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import java.math.BigInteger;
- import java.util.Map;
- import java.util.Objects;
- public class HopperMoveTask {
- public void moveItemTask(Location location, ChestRegistry registry) {
- //if (!PluginConfig.SHOULD_RUN_HOPPERS_UNLOADED_CHUNKS.get() && !Utils.isLocationChunkLoaded(location.getLocation()))
- // continue;
- Location below = location.clone().subtract(0, 1, 0);
- if (below.getBlock().getState() instanceof Hopper) {
- Hopper hopper = (Hopper) below.getBlock().getState();
- if (below.getBlock().isBlockIndirectlyPowered() || below.getBlock().isBlockPowered()) {
- //System.out.println("testings");
- return;
- }
- if (Objects.equals(registry.getTypeofContainer(location), ChestRegistry.TypeOfContainer.STORAGEUNIT)) {
- addItemsToHopper(registry, hopper.getLocation(), location, hopper.getInventory());
- } else {
- LoadSettingsContainers containers = LoadSettingsContainers.getInstance();
- String settingName = registry.getContainerSettingsName(location);
- int guiSize = containers.getContainerType(settingName).getGuiSize();
- InventoryHolderDefultchest holder = new InventoryHolderDefultchest(location, null, registry.getAmountOfPages(location), guiSize);
- Inventory inventory = holder.getEmptyPages(registry);
- if (move(hopper.getLocation(), inventory, hopper.getInventory(), location)) {
- //System.out.println(inventory + " " + location);
- //registry.updateDisplayItem();
- System.out.println("test");
- }
- }
- }
- //if (registry.getInventory().getViewers().size() > 0) registry.sort();
- }
- //}
- // }
- //}
- public static boolean move(Location targetLocation, Inventory source, Inventory target, Location location) {
- int hopperAmount = 1;
- return MoveItemsFromToContainerListner.hopperMoveItemFromChest(source, hopperAmount, target, location, targetLocation);
- }
- public ItemStack moveItemsFromStorageUnit(ChestRegistry registry, Location targetLocation, Location location) {
- ItemStack[] itemstacks = registry.getcontents(location);
- ItemStack RemoveItemsFromContainerBefore;
- ItemStack stack = null;
- if (itemstacks != null && itemstacks.length > 0)
- stack = itemstacks[0];
- FilterItems filter = new FilterItems();
- boolean customfilterContainer = true;
- if (targetLocation != null && registry.getTypeofContainer(targetLocation) != null)
- if (stack != null && registry.getTypeofContainer(targetLocation).equals(ChestRegistry.TypeOfContainer.LINKEDCONTAINER)) {
- customfilterContainer = filter.checkIfItemMatch(targetLocation, stack);
- }
- if (stack != null && filter.filerItems(stack, location) && customfilterContainer) {
- RemoveItemsFromContainerBefore = stack.clone();
- RemoveItemsFromContainerBefore.setAmount(Math.min(Integer.parseInt(registry.getAmontOfItems(location).toString()), 1));
- stack.setAmount(RemoveItemsFromContainerBefore.getAmount() - Integer.parseInt(registry.getAmontOfItems(location).toString()));
- //System.out.println("amnount of items" + registry.getAmontOfItems(location).toString());
- //System.out.println("stackamount " + stack.getAmount() + "removed items " + RemoveItemsFromContainerBefore);
- registry.setAmontOfItems(location, BigInteger.valueOf((Integer.parseInt(registry.getAmontOfItems(location).toString()) - RemoveItemsFromContainerBefore.getAmount())));
- return RemoveItemsFromContainerBefore;
- }
- return null;
- }
- public boolean addItemsToHopper(ChestRegistry registry, Location targetLocation, Location location, Inventory toInventory) {
- /*ItemStack itemStack = moveItemsFromStorageUnit(registry, targetLocation, location);
- if (itemStack != null) {
- HashMap<Integer, ItemStack> leftOvers = toInventory.addItem(itemStack);
- return leftOvers.isEmpty();
- }*/
- ItemStack[] itemstacks = registry.getcontents(location);
- Map<Integer, ItemStack> leftOvers;
- ItemStack itemToAdd = null;
- if (itemstacks != null && itemstacks.length > 0) {
- itemToAdd = itemstacks[0];
- }
- if (itemToAdd != null) {
- int items = 1;
- do {
- itemToAdd.setAmount(items);
- leftOvers = toInventory.addItem(itemToAdd);
- items++;
- } while (!leftOvers.isEmpty() || registry.getAmontOfItems(location).toString().equals("0"));
- BigInteger subtractedValues = registry.getAmontOfItems(location).subtract(BigInteger.valueOf(items));
- registry.setAmontOfItems(location, subtractedValues);
- /*int newAmount = registry.getAmontOfItems(location).min(BigInteger.valueOf(64)).intValue();
- itemToAdd.setAmount(newAmount);
- if (registry.getAmontOfItems(location).intValue() <= 64) {
- Map<Integer, ItemStack> leftOvers = toInventory.addItem(itemToAdd);
- if (!leftOvers.isEmpty()) {
- ItemStack leftOver = leftOvers.get(0);
- if (leftOver.getAmount() == newAmount) {
- return false;
- } else {
- newAmount -= leftOver.getAmount();
- }
- }
- BigInteger subtractedValues = registry.getAmontOfItems(location).subtract(BigInteger.valueOf(newAmount));
- registry.setAmontOfItems(location, subtractedValues);
- }*/
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment