Advertisement
broken-arrow

Untitled

Nov 12th, 2021
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 27.61 KB | None | 0 0
  1. package org.brokenarrow.storage.hopperteleportitems;
  2.  
  3. import lombok.Getter;
  4. import org.brokenarrow.storage.Crafting.FilterItems;
  5. import org.brokenarrow.storage.CustomContainersMainClass;
  6. import org.brokenarrow.storage.Settings.ChatMessages;
  7. import org.brokenarrow.storage.Settings.LogTimingsReport;
  8. import org.brokenarrow.storage.Settings.MainSettings;
  9. import org.brokenarrow.storage.api.ContainerRegistryAPI;
  10. import org.brokenarrow.storage.api.PreferenceSettingsRegisteryApi;
  11. import org.brokenarrow.storage.api.TeleportAndPickupItemsApi;
  12. import org.brokenarrow.storage.api.containerholders.InventoryHolders;
  13. import org.brokenarrow.storage.cache.ContainerRegistry;
  14. import org.brokenarrow.storage.util.ItemUtily;
  15. import org.brokenarrow.storage.util.MathCalations;
  16. import org.brokenarrow.storage.util.YamlSettingsContainers;
  17. import org.bukkit.*;
  18. import org.bukkit.block.*;
  19. import org.bukkit.entity.Entity;
  20. import org.bukkit.entity.Item;
  21. import org.bukkit.inventory.Inventory;
  22. import org.bukkit.inventory.ItemStack;
  23.  
  24. import java.util.*;
  25. import java.util.stream.Collectors;
  26.  
  27. public class TeleportAndPickupItems implements TeleportAndPickupItemsApi {
  28.  
  29.     private final CustomContainersMainClass plugin = CustomContainersMainClass.getInstance();
  30.     private final ContainerRegistryAPI registry = ContainerRegistryAPI.getInstance();
  31.     @Getter
  32.     private final static TeleportAndPickupItems instance = new TeleportAndPickupItems();
  33.     private final PreferenceSettingsRegisteryApi preferenceRegistery = PreferenceSettingsRegisteryApi.getInstance();
  34.  
  35.     private final Map<Location, CacheHelpClass> cachedLinkSuctionInventorys = new HashMap<>();
  36.     private Inventory containerInventory;
  37.  
  38.     public TeleportAndPickupItems() {
  39.  
  40.     }
  41.  
  42.     public void runTeleportSuctionTask(List<Location> locationsList) {
  43.         long timess = System.nanoTime();
  44.         locationsList = locationsList.stream().filter((location) -> ContainerRegistry.TypeOfContainer.LINKEDCONTAINER == registry.getTypeofContainer(location)).collect(Collectors.toList());
  45.  
  46.         for (ListIterator<Location> locations = locationsList.listIterator(); locations.hasNext(); ) {
  47.             Location location = locations.next();
  48.  
  49.             if (!(ContainerRegistry.TypeOfContainer.LINKEDCONTAINER == registry.getTypeofContainer(location))) continue;
  50.             if (!location.getWorld().isChunkLoaded(location.getBlockX() >> 4, location.getBlockZ() >> 4)) continue;
  51.             if (!(System.currentTimeMillis() >= getDelayItemPicupTeleport(location))) continue;
  52.  
  53.             if (getDelayItemPicupTeleport(location) == 0) {
  54.                 int secondsForPickupTeleport = YamlSettingsContainers.getSecondsForPickupTeleport(registry.getContainerFileName(location), registry.getCurrentUpdate(location));
  55.                 setDelayItemPicupTeleportInMilli(location,
  56.                         (System.currentTimeMillis() + (secondsForPickupTeleport < 0 ? 1000 * 2 : secondsForPickupTeleport * 1000L)));
  57.                 //delayItemPicupTeleport.put(location, (System.currentTimeMillis() + (secondsForPickupTeleport < 0 ? 1000 * 2 : secondsForPickupTeleport * 1000L)));
  58.  
  59.             } else if (System.currentTimeMillis() >= getDelayItemPicupTeleport(location)) {
  60.                 linkedContainer(location);
  61.                 int secondsForPickupTeleport = YamlSettingsContainers.getSecondsForPickupTeleport(registry.getContainerFileName(location), registry.getCurrentUpdate(location));
  62.                 setDelayItemPicupTeleportInMilli(location,
  63.                         (System.currentTimeMillis() + (secondsForPickupTeleport < 0 ? 1000 * 2 : secondsForPickupTeleport * 1000L)));
  64.                 //delayItemPicupTeleport.put(location, (System.currentTimeMillis() + (secondsForPickupTeleport <= 0 ? 1000 * 2 : secondsForPickupTeleport * 1000L)));
  65.             }
  66.         }
  67.         if (MainSettings.getInstance().getSettings().isDebugTeleportPickupItems()) {
  68.             LogTimingsReport.getInstance().setTimeListQueue("runTeleportSuctionTask", Math.round(((double) (System.nanoTime() - timess) / 1000000) * 1000.0) / 1000.0);
  69.         }
  70.     }
  71.  
  72.     private void linkedContainer(Location location) {
  73.         Iterator<Entity> items = null;
  74.         Inventory hopperInventory = null;
  75.  
  76.         String nextUpdate = registry.getCurrentUpdate(location);
  77.         String sugtionRange = YamlSettingsContainers.getSuctionRange(registry.getContainerFileName(location), nextUpdate);
  78.  
  79.         String[] radieSplited;
  80.         int locX;
  81.         int locY;
  82.         int locZ;
  83.  
  84.         World world = location.getChunk().getWorld();
  85.         Block placedItem = location.getBlock();
  86.         int radiesLengt = (radieSplited = sugtionRange.split(",")).length;
  87.  
  88.         if (radiesLengt == 3) {
  89.             locX = Integer.parseInt(radieSplited[0]);
  90.             locY = Integer.parseInt(radieSplited[1]);
  91.             locZ = Integer.parseInt(radieSplited[2]);
  92.  
  93.             items = world.getNearbyEntities(location.clone().add(0.5, 0, 0.5), locX, locY, locZ).iterator();
  94.  
  95.         } else if (sugtionRange.startsWith("-1")) {
  96.             Location locationCloned = MathCalations.getCentrumBlock(location.clone(), -0.5, false);
  97.             int chunkSize = 0;
  98.             if (sugtionRange.contains(",")) {
  99.                 String[] radiesplited = sugtionRange.split(",");
  100.                 if (radiesplited.length == 2) {
  101.                     String calculateChunkSize = radiesplited[1];
  102.                     chunkSize = Integer.parseInt(calculateChunkSize);
  103.                 }
  104.             }
  105.             items = world.getNearbyEntities(locationCloned, chunkSize > 0 ? 8 + (8 * chunkSize) : 8, location.getWorld().getMaxHeight(), chunkSize > 0 ? 8 + (8 * chunkSize) : 8).iterator();
  106.         }
  107.  
  108.         if (getCachedContainerInventory(location) == null) {
  109.             if (placedItem.getState() instanceof Hopper) {
  110.                 setCachedContainerInventory(location, ((Hopper) placedItem.getState()).getInventory());
  111.             } else if (placedItem.getState() instanceof Chest) {
  112.                 setCachedContainerInventory(location, ((Chest) placedItem.getState()).getInventory());
  113.             } else if (placedItem.getState() instanceof Barrel) {
  114.                 setCachedContainerInventory(location, ((Barrel) placedItem.getState()).getInventory());
  115.             }
  116.         }
  117.         hopperInventory = getCachedContainerInventory(location);
  118.         if (hopperInventory != null && !(YamlSettingsContainers.getLinkRange(registry.getContainerFileName(location), registry.getCurrentUpdate(location)) == -1)) {
  119.             ItemStack[] hopperContents = hopperInventory.getContents();
  120.             teleportItemsToContainer(location, hopperContents, hopperInventory);
  121.         }
  122.         if (items != null && !(sugtionRange.equals("-2") || sugtionRange.equals("")))
  123.             findItemAndAddToContainer(items, location, world, hopperInventory);
  124.  
  125.  
  126.     }
  127.  
  128.     private void findItemAndAddToContainer(Iterator<Entity> items, Location location, World world, Inventory hopperInventory) {
  129.  
  130.         while (items.hasNext()) {
  131.             Entity itemstacks = items.next();
  132.             if (itemstacks.isValid() && itemstacks.getTicksLived() >= 20 && itemstacks instanceof Item) {
  133.                 ItemStack itemOnGrund = ((Item) itemstacks).getItemStack().clone();
  134.                 FilterItems itemsSort = new FilterItems();
  135.                 if (itemsSort.checkIfItemMatch(location, itemOnGrund)) {
  136.  
  137.                     if (hopperInventory.firstEmpty() == -1 && !hopperInventory.containsAtLeast(itemOnGrund, 1))
  138.                         break;
  139.                     List<String> effectWhenContainerRunTask = YamlSettingsContainers.getEffectWhenContainerRunTask(registry.getContainerFileName(location), registry.getCurrentUpdate(location));
  140.                     String soundWhenContainerRunTask = YamlSettingsContainers.getSoundWhenContainerRunTask(registry.getContainerFileName(location), registry.getCurrentUpdate(location));
  141.                     if (preferenceRegistery.isSoundEffectsWhenContainerPickUpItems(registry.getPlayerUUID(location)))
  142.  
  143.                         if (soundWhenContainerRunTask != null && !soundWhenContainerRunTask.equals(""))
  144.                             itemstacks.getLocation().getWorld().playSound(itemstacks.getLocation(), Sound.valueOf(soundWhenContainerRunTask), 1.0F, 1.0F);
  145.                         else
  146.                             itemstacks.getLocation().getWorld().playSound(itemstacks.getLocation(), Sound.BLOCK_NOTE_BLOCK_BIT, 1.0F, 1.0F);
  147.  
  148.                     itemstacks.getWorld().spawnParticle(effectWhenContainerRunTask != null && !effectWhenContainerRunTask.isEmpty() ? Particle.valueOf(effectWhenContainerRunTask.get(0)) : Particle.FIREWORKS_SPARK, itemstacks.getLocation(), 2, 0.0, 0.1, 0.0, 0.2);
  149.  
  150.                     int itemAmount = plugin.getStackerProvider().getProvider() != null ? plugin.getStackerProvider().getProvider().getItemAmount((Item) itemstacks) : ((Item) itemstacks).getItemStack().getAmount();
  151.                     if (itemAmount > 64) {
  152.                         if (plugin.getStackerProvider().getProvider() != null)
  153.                             plugin.getStackerProvider().getProvider().setItemAmount((Item) itemstacks, itemAmount - 64);
  154.                         itemOnGrund = itemOnGrund.clone();
  155.                         itemOnGrund.setAmount(64);
  156.                     } else {
  157.                         itemOnGrund.setAmount(itemAmount);
  158.                         itemstacks.remove();
  159.                     }
  160.  
  161.                     HashMap<Integer, ItemStack> leftOvers = hopperInventory.addItem(new ItemStack(itemOnGrund));
  162.  
  163.                     if (!leftOvers.isEmpty())
  164.                         world.dropItemNaturally(location, itemOnGrund);
  165.                     else
  166.                         registry.setTotalAmountPickedUp(location, itemOnGrund.getAmount());
  167.                 }
  168.             }
  169.         }
  170.     }
  171.  
  172.     private void teleportItemsToContainer(Location location, ItemStack[] hopperContents, Inventory hopperInventory) {
  173.         Location linkedContainerLocation = getLinkLocation(location, registry);
  174.  
  175.         if (linkedContainerLocation != null) {
  176.  
  177.             if (!linkedContainerLocation.getWorld().isChunkLoaded(linkedContainerLocation.getBlockX() >> 4, linkedContainerLocation.getBlockZ() >> 4))
  178.                 return;
  179.  
  180.             InventoryHolders customInventory = plugin.getClassinstans().createObjektInventoryHolder(linkedContainerLocation, null);
  181.             if (registry.getTypeofContainer(linkedContainerLocation) != null)
  182.                 switch (registry.getTypeofContainer(linkedContainerLocation)) {
  183.                     case PAGEDCONTAINER:
  184.                     case CRAFTINGCONTAINER:
  185.                     case SELLCHEST:
  186.                     case STORAGEUNIT: {
  187.  
  188.                         ItemStack[] itemStacks = Arrays.stream(hopperContents).filter((contents) -> contents != null &&
  189.                                 contents.getType() != Material.AIR).toArray(ItemStack[]::new);
  190.  
  191.                         Map<Integer, ItemStack> containerLeftOvers = customInventory.addItems(itemStacks);
  192.                         hopperInventory.clear();
  193.                         if (!containerLeftOvers.isEmpty()) {
  194.                             setTeleportedItems(location, false);
  195.                             Collection<ItemStack> collectionItemStack = containerLeftOvers.values();
  196.  
  197.                             hopperInventory.addItem(collectionItemStack.toArray(new ItemStack[0]));
  198.                         } else {
  199.                             registry.setTotalAmountTeleportedItems(location, ItemUtily.countItemStacks(hopperContents, customInventory.getContents(linkedContainerLocation)));
  200.                             setTeleportedItems(location, true);
  201.                         }
  202.  
  203.                         break;
  204.                     }
  205.                     default:
  206.                         ItemStack[] itemStacks = Arrays.stream(hopperContents).filter((contents) -> contents != null &&
  207.                                 contents.getType() != Material.AIR).toArray(ItemStack[]::new);
  208.                         System.out.println("itemstacks " + isTeleportedItems(location));
  209.  
  210.                         HopperTeleportListener event = new HopperTeleportListener(linkedContainerLocation, isTeleportedItems(location), itemStacks);
  211.                         Bukkit.getPluginManager().callEvent(event);
  212.  
  213.                         if (event.isCancelled()) {
  214.                             if (event.isShallRemoveFromHopper())
  215.                                 hopperInventory.clear();
  216.                             break;
  217.                         } else {
  218.                             HashMap<Integer, ItemStack> containerLeftOvers = containerInventory.addItem(itemStacks);
  219.                             hopperInventory.clear();
  220.  
  221.                             if (!containerLeftOvers.isEmpty()) {
  222.                                 setTeleportedItems(location, false);
  223.                                 Collection<ItemStack> collectionItemStack = containerLeftOvers.values();
  224.  
  225.                                 hopperInventory.addItem(collectionItemStack.toArray(new ItemStack[0]));
  226.                             } else {
  227.                                 registry.setTotalAmountTeleportedItems(location, ItemUtily.countItemStacks(hopperContents, containerInventory));
  228.                                 setTeleportedItems(location, itemStacks.length >= 1);
  229.                             }
  230.  
  231.                         }
  232.                         break;
  233.                 }
  234.  
  235.         }
  236.     }
  237.  
  238.     //todo check and fix so it change location and are not same.
  239.     private Location getLinkLocation(Location containerLocation, ContainerRegistryAPI registry) {
  240.         List<Location> linkedContainerLocation = registry.getLinkContainerLinkedToLocation(containerLocation);
  241.         Location locations = null;
  242.         boolean ifLinkedContainerIsAir = false;
  243.  
  244.         Set<Location> noVaildLocation = new HashSet<>();
  245.         int locationNumberInList = 0;
  246.  
  247.  
  248.         if (getCachedContainerInventory(containerLocation) != null) {
  249.             locationNumberInList = getNumberInList(containerLocation);
  250.         }
  251.         if (linkedContainerLocation != null && linkedContainerLocation.size() > 0) {
  252.  
  253.             if (locationNumberInList >= linkedContainerLocation.size()) {
  254.                 locationNumberInList = 0;
  255.             }
  256.             Location loc = linkedContainerLocation.get(locationNumberInList);
  257.             if (loc.getWorld().isChunkLoaded(loc.getBlockX() >> 4, loc.getBlockZ() >> 4)) {
  258.                 if (!getLinkedInventory(loc, containerLocation)) {
  259.                     switch (loc.getBlock().getType()) {
  260.                         case HOPPER:
  261.                         case DISPENSER:
  262.                         case DROPPER:
  263.                         case BARREL:
  264.                         case CHEST:
  265.                             locationNumberInList++;
  266.                             if (locationNumberInList >= linkedContainerLocation.size()) {
  267.                                 locationNumberInList = 0;
  268.                             }
  269.                             loc = linkedContainerLocation.get(locationNumberInList);
  270.                             break;
  271.                         default:
  272.                             noVaildLocation.add(loc);
  273.                             ifLinkedContainerIsAir = true;
  274.                             break;
  275.                     }
  276.                 }
  277.                 locations = loc;
  278.  
  279.             }
  280.  
  281.             locationNumberInList++;
  282.             setNumberInList(containerLocation, locationNumberInList);
  283.         }
  284.  
  285.         if (ifLinkedContainerIsAir) {
  286.             noVaildLocation.stream().sorted().forEach((location) -> {
  287.                 ChatMessages.THIS_BLOCK_IS_NOT_VAILD.sendMessage(registry.getPlayer(containerLocation), location.getWorld().getName(), location.getBlockX(), location.getBlockY(), location.getBlockZ(), location.getBlock().getType().toString().toLowerCase());
  288.                 this.registry.removeLinkContainerLinkedToThisContainer(location, containerLocation);
  289.             });
  290.             noVaildLocation.clear();
  291.         }
  292.         return locations;
  293.     }
  294.  
  295.  
  296.     private boolean getLinkedInventory(Location linkedContainerLocation, Location containerLocation) {
  297.  
  298.         if (containerLocation != null && linkedContainerLocation != null) {
  299.             if (getCachedLinkedInventory(containerLocation) != null && getCachedLinkedInventory(containerLocation).containsKey(linkedContainerLocation)) {
  300.                 Inventory inventory = getCachedLinkedInventory(containerLocation).get(linkedContainerLocation);
  301.                 for (ItemStack items : inventory.getContents())
  302.                     if (inventory.firstEmpty() == -1 && !inventory.containsAtLeast(items, 1)) {
  303.                         removeCachedLinkedContainerInventory(containerLocation, linkedContainerLocation);
  304.                         return false;
  305.                     } else if (linkedContainerLocation.getBlock().getType() == Material.HOPPER || linkedContainerLocation.getBlock().getType() == Material.CHEST ||
  306.                             linkedContainerLocation.getBlock().getType() == Material.BARREL || linkedContainerLocation.getBlock().getType() == Material.DROPPER ||
  307.                             linkedContainerLocation.getBlock().getType() == Material.DISPENSER) {
  308.                         this.containerInventory = inventory;
  309.                         return true;
  310.                     } else {
  311.                         removeCachedLinkedContainerInventory(containerLocation, linkedContainerLocation);
  312.                     }
  313.  
  314.             } else {
  315.                 Block blocktype = linkedContainerLocation.getBlock();
  316.                 if (blocktype.getState() instanceof Hopper) {
  317.  
  318.                     setCachedLinkedContainer(containerLocation, (((Hopper) blocktype.getState()).getInventory()), linkedContainerLocation);
  319.                     this.containerInventory = (((Hopper) blocktype.getState()).getInventory());
  320.                     return true;
  321.                 } else if ((blocktype.getState() instanceof Chest)) {
  322.  
  323.                     setCachedLinkedContainer(containerLocation, (((Chest) blocktype.getState()).getInventory()), linkedContainerLocation);
  324.                     this.containerInventory = (((Chest) blocktype.getState()).getInventory());
  325.                     return true;
  326.                 } else if ((blocktype.getState() instanceof Barrel)) {
  327.  
  328.                     setCachedLinkedContainer(containerLocation, (((Barrel) blocktype.getState()).getInventory()), linkedContainerLocation);
  329.                     this.containerInventory = (((Barrel) blocktype.getState()).getInventory());
  330.                     return true;
  331.                 } else if ((blocktype.getState() instanceof Dropper)) {
  332.  
  333.                     setCachedLinkedContainer(containerLocation, (((Dropper) blocktype.getState()).getInventory()), linkedContainerLocation);
  334.                     this.containerInventory = (((Dropper) blocktype.getState()).getInventory());
  335.                     return true;
  336.                 } else if ((blocktype.getState() instanceof Dispenser)) {
  337.  
  338.                     setCachedLinkedContainer(containerLocation, (((Dispenser) blocktype.getState()).getInventory()), linkedContainerLocation);
  339.                     this.containerInventory = (((Dispenser) blocktype.getState()).getInventory());
  340.                     return true;
  341.                 } else {
  342.                     this.containerInventory = null;
  343.                     removeCachedLinkedContainerInventory(containerLocation, linkedContainerLocation);
  344.                     return false;
  345.                 }
  346.             }
  347.         }
  348.         return false;
  349.     }
  350.  
  351.     /**
  352.      * Remove all linked containers inventorys and locations from cache.
  353.      *
  354.      * @param containerLocation location of suction/link container you have link from.
  355.      */
  356.     @Override
  357.     public void removeCachedLinkedContainerInventory(Location containerLocation) {
  358.         if (getCachedLinkedInventory(containerLocation) != null) {
  359.             for (ListIterator<Location> locationListIterator = new ArrayList<>(getCachedLinkedInventory(containerLocation).keySet()).listIterator(); locationListIterator.hasNext(); )
  360.  
  361.                 getCachedLinkedInventory(containerLocation).remove(locationListIterator.next());
  362.         }
  363.         setInCache(containerLocation);
  364.     }
  365.  
  366.     /**
  367.      * Remove one linked container inventory and location from cache.
  368.      *
  369.      * @param containerLocation location of suction/link container you have link from.
  370.      * @param linkedTo          location of container you link to.
  371.      */
  372.     @Override
  373.     public void removeCachedLinkedContainerInventory(Location containerLocation, Location linkedTo) {
  374.         if (this.cachedLinkSuctionInventorys.containsKey(containerLocation)) {
  375.             this.cachedLinkSuctionInventorys.get(containerLocation).getCachedLinkedInventory().remove(linkedTo);
  376.         }
  377.         setInCache(containerLocation);
  378.     }
  379.  
  380.     /**
  381.      * Put linked containers inventorys and locations to cache.
  382.      *
  383.      * @param containerLocation location of suction/link container you have link from.
  384.      * @param toInventory       inventory you want to cache.
  385.      * @param toLocation        location of the inventory you link to.
  386.      */
  387.  
  388.     @Override
  389.     public void setCachedLinkedContainer(Location containerLocation, Inventory toInventory, Location toLocation) {
  390.         setInCache(containerLocation, SetTypeOfdataToCache.CACHED_LINKED_CONTAINER, toInventory, toLocation);
  391.     }
  392.  
  393.     /**
  394.      * Put Suction/link container´s inventory to cache.
  395.      *
  396.      * @param containerLocation  location of suction/link container.
  397.      * @param containerInventory inventory you want to cache.
  398.      */
  399.     @Override
  400.     public void setCachedContainerInventory(Location containerLocation, Inventory containerInventory) {
  401.         setInCache(containerLocation, SetTypeOfdataToCache.CACHED_CONTAINER_INVENTORY, containerInventory);
  402.     }
  403.  
  404.     /**
  405.      * remove Suction/link container´s inventory from cache.
  406.      *
  407.      * @param containerLocation location of suction/link container.
  408.      */
  409.     @Override
  410.     public void removeCachedContainerInventory(Location containerLocation) {
  411.         setInCache(containerLocation, SetTypeOfdataToCache.CACHED_CONTAINER_INVENTORY, null);
  412.     }
  413.  
  414.     /**
  415.      * Remove Suction/link container´s from cache. This entity will be removed
  416.      * from cache.
  417.      *
  418.      * @param containerLocation location of suction/link container.
  419.      */
  420.     @Override
  421.     public void clearCachedContainer(Location containerLocation) {
  422.         this.cachedLinkSuctionInventorys.remove(containerLocation);
  423.     }
  424.  
  425.     /**
  426.      * Put Suction/link container´s next run time for this task.
  427.      *
  428.      * @param containerLocation location of suction/link container.
  429.      * @param delayTime         set time in milliseconds.
  430.      */
  431.     @Override
  432.     public void setDelayItemPicupTeleportInMilli(Location containerLocation, long delayTime) {
  433.         setInCache(containerLocation, SetTypeOfdataToCache.DELAY_ITEM_PICKUP_TELEPORT, delayTime);
  434.     }
  435.  
  436.     /**
  437.      * Put Suction/link container´s next run time for this task.
  438.      *
  439.      * @param containerLocation location of suction/link container.
  440.      * @param delayTime         set time in seconds.
  441.      */
  442.  
  443.     @Override
  444.     public void setDelayItemPicupTeleportInSeconds(Location containerLocation, long delayTime) {
  445.         setInCache(containerLocation, SetTypeOfdataToCache.DELAY_ITEM_PICKUP_TELEPORT, delayTime * 1000);
  446.     }
  447.  
  448.     /**
  449.      * Set the number of location it will process next.
  450.      *
  451.      * @param containerLocation location of suction/link container.
  452.      * @param numberInList      set the number it shall process next.
  453.      */
  454.  
  455.     @Override
  456.     public void setNumberInList(Location containerLocation, int numberInList) {
  457.         setInCache(containerLocation, SetTypeOfdataToCache.LOCATIONNUMBERINLIST, numberInList);
  458.     }
  459.  
  460.     /**
  461.      * Set it to true if has sucessfull teleported the item to container.
  462.      *
  463.      * @param containerLocation location of suction/link container.
  464.      */
  465.     @Override
  466.     public void setTeleportedItems(Location containerLocation, boolean teleportItems) {
  467.         setInCache(containerLocation, SetTypeOfdataToCache.TELEPORT_ITEMS, teleportItems);
  468.     }
  469.  
  470.     /**
  471.      * Get the map of stored inventorys on location. Is a snapshot of inventory/inventory`s
  472.      * on the containers you has link from your hopper/chest or barrel. Is use less resources
  473.      * when you cache it insted of call getState() all the time.
  474.      * <p>
  475.      * Keep in main this is only temporary snapshot, if chunk this container are located to get unloaded
  476.      * you has to replace with new snapshot of the inventory.
  477.      *
  478.      * @param containerLocation location of suction/link container you have link from.
  479.      * @return map with all cached data of current stored location with snapshot of inventory.
  480.      */
  481.     @Override
  482.     public Map<Location, Inventory> getCachedLinkedInventory(Location containerLocation) {
  483.         if (this.cachedLinkSuctionInventorys.containsKey(containerLocation))
  484.             return this.cachedLinkSuctionInventorys.get(containerLocation).getCachedLinkedInventory();
  485.         return null;
  486.     }
  487.  
  488.     /**
  489.      * Get your Suction/link container´s inventory on location. Is use less resources
  490.      * when you cache it insted of call getState() all the time.
  491.      * <p>
  492.      * Keep in main this is only temporary snapshot, if chunk this container are located to get unloaded
  493.      * you has to replace with new snapshot of the inventory.
  494.      *
  495.      * @param containerLocation location of suction/link container.
  496.      * @return a cached snapshot of container inventory.
  497.      */
  498.  
  499.     @Override
  500.     public Inventory getCachedContainerInventory(Location containerLocation) {
  501.         if (this.cachedLinkSuctionInventorys.containsKey(containerLocation))
  502.             return this.cachedLinkSuctionInventorys.get(containerLocation).getCachedContainerInventory();
  503.         return null;
  504.     }
  505.  
  506.     /**
  507.      * Get the number of location it will process next, in the array for the containers some are linked
  508.      * to Suction/link container.
  509.      *
  510.      * @param containerLocation location of suction/link container.
  511.      * @return the number it will process next.
  512.      */
  513.  
  514.     @Override
  515.     public int getNumberInList(Location containerLocation) {
  516.         if (this.cachedLinkSuctionInventorys.containsKey(containerLocation))
  517.             return this.cachedLinkSuctionInventorys.get(containerLocation).getLocationNumberInList();
  518.         return 0;
  519.     }
  520.  
  521.     /**
  522.      * Get time for next time it will run the task (pickup items and/or teleport item).
  523.      *
  524.      * @param containerLocation location of suction/link container.
  525.      * @return next time it will run in milliseconds.
  526.      */
  527.     @Override
  528.     public long getDelayItemPicupTeleport(Location containerLocation) {
  529.         if (this.cachedLinkSuctionInventorys.get(containerLocation) != null)
  530.             return this.cachedLinkSuctionInventorys.get(containerLocation).getDelayItemPicupTeleport();
  531.         return 0;
  532.     }
  533.  
  534.     /**
  535.      * Get if items has successfully teleported items to container.
  536.      *
  537.      * @param containerLocation location of suction/link container.
  538.      * @return true if the item has bee moved.
  539.      */
  540.     @Override
  541.     public boolean isTeleportedItems(Location containerLocation) {
  542.         if (this.cachedLinkSuctionInventorys.containsKey(containerLocation))
  543.             return this.cachedLinkSuctionInventorys.get(containerLocation).isTeleportedItems();
  544.         return false;
  545.     }
  546.  
  547.     /**
  548.      * Do not use this methods. Is only help, to set data in cache.
  549.      *
  550.      * @param containerlocation location of suction/link container.
  551.      */
  552.  
  553.     private void setInCache(Location containerlocation) {
  554.         setInCache(containerlocation, null, null, null);
  555.     }
  556.  
  557.     /**
  558.      * Do not use this methods. Is only help, to set data in cache.
  559.      *
  560.      * @param containerlocation location of suction/link container.
  561.      */
  562.  
  563.     private void setInCache(Location containerlocation, SetTypeOfdataToCache dataSomeShallCache, Object object) {
  564.         setInCache(containerlocation, dataSomeShallCache, null, object);
  565.     }
  566.  
  567.     /**
  568.      * Do not use this methods. Is only help, to set data in cache.
  569.      *
  570.      * @param containerlocation location of suction/link container.
  571.      */
  572.  
  573.     private void setInCache(Location containerlocation, SetTypeOfdataToCache dataSomeShallCache, Inventory toinventory, Object object) {
  574.         Map<Location, Inventory> cachedLinkedInventory = new HashMap<>();
  575.  
  576.  
  577.         if (dataSomeShallCache == SetTypeOfdataToCache.CACHED_LINKED_CONTAINER) {
  578.             Map<Location, Inventory> olddata = null;
  579.             if (getCachedLinkedInventory(containerlocation) != null)
  580.                 olddata = getCachedLinkedInventory(containerlocation);
  581.             if (olddata != null)
  582.                 cachedLinkedInventory.putAll(olddata);
  583.             cachedLinkedInventory.put((Location) object, toinventory);
  584.  
  585.         }
  586.         //cachedLinkedInventory.put(obj);
  587.  
  588.         this.cachedLinkSuctionInventorys.put(containerlocation, CacheHelpClass.of(!cachedLinkedInventory.isEmpty() || dataSomeShallCache == SetTypeOfdataToCache.CACHED_LINKED_CONTAINER ? cachedLinkedInventory : getCachedLinkedInventory(containerlocation),
  589.                 dataSomeShallCache == SetTypeOfdataToCache.CACHED_CONTAINER_INVENTORY ? (Inventory) object : getCachedContainerInventory(containerlocation),
  590.                 dataSomeShallCache == SetTypeOfdataToCache.TELEPORT_ITEMS ? (boolean) object : isTeleportedItems(containerlocation),
  591.                 dataSomeShallCache == SetTypeOfdataToCache.LOCATIONNUMBERINLIST ? (int) object : getNumberInList(containerlocation),
  592.                 dataSomeShallCache == SetTypeOfdataToCache.DELAY_ITEM_PICKUP_TELEPORT ? (Long) object : getDelayItemPicupTeleport(containerlocation)));
  593.  
  594.     }
  595.  
  596.     public static class CacheHelpClass {
  597.  
  598.         private Map<Location, Inventory> cachedLinkedInventory;
  599.         private Inventory cachedContainerInventory;
  600.         private int locationNumberInList;
  601.         private long delayItemPicupTeleport;
  602.         private boolean teleportedItems;
  603.  
  604.         public CacheHelpClass(Map<Location, Inventory> cachedLinkedInventory, Inventory cachedContainerInventory, int locationNumberInList, long delayItemPicupTeleport, boolean teleportedItems) {
  605.             this.cachedLinkedInventory = cachedLinkedInventory;
  606.             this.cachedContainerInventory = cachedContainerInventory;
  607.             this.locationNumberInList = locationNumberInList;
  608.             this.delayItemPicupTeleport = delayItemPicupTeleport;
  609.             this.teleportedItems = teleportedItems;
  610.         }
  611.  
  612.         private CacheHelpClass() {
  613.         }
  614.  
  615.         public static CacheHelpClass of(Map<Location, Inventory> cachedLinkedInventory, Inventory cachedContainerInventory, boolean teleportedItems
  616.                 , int locationNumberInList, long delayItemPicupTeleport) {
  617.             CacheHelpClass data = new CacheHelpClass();
  618.  
  619.             data.cachedLinkedInventory = cachedLinkedInventory;
  620.             data.cachedContainerInventory = cachedContainerInventory;
  621.             data.locationNumberInList = locationNumberInList;
  622.             data.delayItemPicupTeleport = delayItemPicupTeleport;
  623.             data.teleportedItems = teleportedItems;
  624.  
  625.             return data;
  626.         }
  627.  
  628.         public Inventory getCachedContainerInventory() {
  629.             return cachedContainerInventory;
  630.         }
  631.  
  632.         public int getLocationNumberInList() {
  633.             return locationNumberInList;
  634.         }
  635.  
  636.         public long getDelayItemPicupTeleport() {
  637.             return delayItemPicupTeleport;
  638.         }
  639.  
  640.         public boolean isTeleportedItems() {
  641.             return teleportedItems;
  642.         }
  643.  
  644.         public Map<Location, Inventory> getCachedLinkedInventory() {
  645.             return cachedLinkedInventory;
  646.         }
  647.     }
  648.  
  649.     private enum SetTypeOfdataToCache {
  650.         CACHED_CONTAINER_INVENTORY,
  651.         CACHED_LINKED_CONTAINER,
  652.         LOCATIONNUMBERINLIST,
  653.         DELAY_ITEM_PICKUP_TELEPORT,
  654.         TELEPORT_ITEMS
  655.     }
  656. }
  657.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement