Advertisement
broken-arrow

Untitled

Sep 2nd, 2021
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.40 KB | None | 0 0
  1. import org.broken.cheststorage.ChestStorage;
  2. import org.broken.cheststorage.Crafting.FilterItems;
  3. import org.broken.cheststorage.cache.ContainerRegistry;
  4. import org.broken.cheststorage.containerholders.InventoryHolder;
  5. import org.broken.cheststorage.containerholders.InventoryHolderDefultchest;
  6. import org.broken.cheststorage.containerholders.InventoryHolderStorageUnit;
  7. import org.broken.cheststorage.util.MathCalations;
  8. import org.broken.cheststorage.util.YamlSettingsContainers;
  9. import org.bukkit.Location;
  10. import org.bukkit.Material;
  11. import org.bukkit.Particle;
  12. import org.bukkit.World;
  13. import org.bukkit.block.Barrel;
  14. import org.bukkit.block.Block;
  15. import org.bukkit.block.Chest;
  16. import org.bukkit.block.Hopper;
  17. import org.bukkit.entity.Entity;
  18. import org.bukkit.entity.Item;
  19. import org.bukkit.inventory.Inventory;
  20. import org.bukkit.inventory.ItemStack;
  21.  
  22. import java.util.*;
  23.  
  24. public class TeleportItems {
  25.  
  26.     private final ChestStorage plugin = ChestStorage.getInstance();
  27.     private final ContainerRegistry registry = ContainerRegistry.getInstance();
  28.  
  29.     private World world;
  30.     private Inventory hopperInventory;
  31.     private Inventory containerInventory;
  32.     int delayItemPicupTeleport = 0;
  33.     int secondstoPickcup = 0;
  34.     private boolean ifContainerFull;
  35.     private final Location location;
  36.  
  37.     public TeleportItems(int secondstoPickcup, Location location) {
  38.         this.location = location;
  39.         this.secondstoPickcup = secondstoPickcup;
  40.     }
  41.  
  42.     public void run() {
  43.         delayItemPicupTeleport++;
  44.  
  45.         if (delayItemPicupTeleport >= secondstoPickcup) {
  46.             System.out.println("tes " + delayItemPicupTeleport + "loc" + this.location);
  47.             linkedContainer();
  48.             delayItemPicupTeleport = 0;
  49.         }
  50.     }
  51.  
  52.     private void linkedContainer() {
  53.         Iterator<Entity> items = null;
  54.  
  55.         String nextUpdate = registry.getCurrentUpdate(location);
  56.         String sugtionRange = YamlSettingsContainers.getSuctionRange(registry.getContainerFileName(location), nextUpdate);
  57.  
  58.         String[] radieSplited;
  59.         int locX;
  60.         int locY;
  61.         int locZ;
  62.  
  63.         world = location.getChunk().getWorld();
  64.         Block placedItem = location.getBlock();
  65.  
  66.         int radiesLengt = (radieSplited = sugtionRange.split(",")).length;
  67.  
  68.         if (radiesLengt == 3) {
  69.             locX = Integer.parseInt(radieSplited[0]);
  70.             locY = Integer.parseInt(radieSplited[1]);
  71.             locZ = Integer.parseInt(radieSplited[2]);
  72.  
  73.             items = this.world.getNearbyEntities(location.clone().add(0.5, 0, 0.5), locX, locY, locZ).iterator();
  74.  
  75.         } else if (sugtionRange.equals("-1")) {
  76.             Location locationCloned = MathCalations.getCentrumBlock(location.clone(), -0.5, false);
  77.             items = this.world.getNearbyEntities(locationCloned, 8, location.getWorld().getMaxHeight(), 8).iterator();
  78.  
  79.         } else if (sugtionRange.equals("-2")) return;
  80.  
  81.  
  82.         if (placedItem.getState() instanceof Hopper) {
  83.             this.hopperInventory = ((Hopper) placedItem.getState()).getInventory();
  84.         } else if (placedItem.getState() instanceof Chest) {
  85.             this.hopperInventory = ((Chest) placedItem.getState()).getInventory();
  86.         } else if (placedItem.getState() instanceof Barrel)
  87.             this.hopperInventory = ((Barrel) placedItem.getState()).getInventory();
  88.  
  89.  
  90.         ItemStack[] hopperContents = this.hopperInventory.getContents();
  91.         teleportItemsToContainer(registry, location, hopperContents);
  92.         if (items != null)
  93.             findItemAndAddToContainer(items, location);
  94.  
  95.     }
  96.  
  97.     public boolean getIfContainerFull() {
  98.         return ifContainerFull;
  99.     }
  100.  
  101.     private void findItemAndAddToContainer(Iterator<Entity> items, Location location) {
  102.  
  103.         while (items.hasNext()) {
  104.             Entity itemstacks = items.next();
  105.             if (itemstacks.isValid() && itemstacks.getTicksLived() >= 20 && itemstacks instanceof Item) {
  106.                 ItemStack itemOnGrund = ((Item) itemstacks).getItemStack().clone();
  107.                 FilterItems itemsSort = new FilterItems();
  108.                 System.out.println("test");
  109.                 if (itemsSort.checkIfItemMatch(location, itemOnGrund)) {
  110.  
  111.                     if (hopperInventory.firstEmpty() == -1 && !hopperInventory.containsAtLeast(itemOnGrund, 1))
  112.                         break;
  113.                     itemstacks.getWorld().spawnParticle(Particle.FIREWORKS_SPARK, itemstacks.getLocation(), 2, 0.0, 0.1, 0.0, 0.2);
  114.  
  115.                     if (((Item) itemstacks).getItemStack().getAmount() > 64) {
  116.                         ((Item) itemstacks).getItemStack().setAmount(((Item) itemstacks).getItemStack().getAmount() - 64);
  117.                         itemOnGrund = itemOnGrund.clone();
  118.                         itemOnGrund.setAmount(64);
  119.                     } else {
  120.                         itemOnGrund.setAmount(((Item) itemstacks).getItemStack().getAmount());
  121.                         itemstacks.remove();
  122.                     }
  123.  
  124.                     HashMap<Integer, ItemStack> leftOvers = hopperInventory.addItem(new ItemStack(itemOnGrund));
  125.  
  126.                     if (!leftOvers.isEmpty())
  127.                         this.world.dropItemNaturally(location, itemOnGrund);
  128.                 }
  129.             }
  130.         }
  131.     }
  132.  
  133.     private void teleportItemsToContainer(ContainerRegistry registry, Location location, ItemStack[] hopperContents) {
  134.         Location containerLocations = inventoryFull(location, registry);
  135.  
  136.  
  137.         if (containerLocations != null) {
  138.             InventoryHolder customInventory = plugin.getClassinstans().createObjektInventoryHolder(containerLocations, null);
  139.             InventoryHolderDefultchest holderDefultchest = new InventoryHolderDefultchest(containerLocations, null);
  140.             InventoryHolderStorageUnit storageUnit = new InventoryHolderStorageUnit(containerLocations, null);
  141.             if (registry.getTypeofContainer(containerLocations) != null)
  142.                 switch (registry.getTypeofContainer(containerLocations)) {
  143.                     case PAGEDCONTAINER:
  144.                     case CRAFTINGCONTAINER:
  145.                     case SELLCHEST:
  146.                     case STORAGEUNIT: {
  147.                         Map<Integer, ItemStack> containerLeftOvers = customInventory.addItems(Arrays.stream(hopperContents).filter((contents) -> contents != null &&
  148.                                 contents.getType() != Material.AIR).toArray(ItemStack[]::new));
  149.                         hopperInventory.clear();
  150.                         if (!containerLeftOvers.isEmpty()) {
  151.                             ifContainerFull = true;
  152.                             Collection<ItemStack> collectionItemStack = containerLeftOvers.values();
  153.  
  154.                             hopperInventory.addItem(collectionItemStack.toArray(new ItemStack[0]));
  155.                         }
  156.                         break;
  157.                     }
  158.                 /*case LINKEDCONTAINER:
  159.                     break;*/
  160.                     default:
  161.                         /*if (containerInventory != null) {
  162.  
  163.                             HashMap<Integer, ItemStack> containerLeftOvers = containerInventory.addItem(
  164.                                     Arrays.stream(hopperContents).filter((contents) -> contents != null &&
  165.                                             contents.getType() != Material.AIR).toArray(ItemStack[]::new));
  166.  
  167.                             hopperInventory.clear();
  168.                             if (!containerLeftOvers.isEmpty()) {
  169.                                 ifContainerFull = true;
  170.                                 Collection<ItemStack> collectionItemStack = containerLeftOvers.values();
  171.  
  172.                                 hopperInventory.addItem(collectionItemStack.toArray(new ItemStack[0]));
  173.  
  174.                             }
  175.                         }*/
  176.                 }
  177.             else if (containerInventory != null) {
  178.  
  179.                 HashMap<Integer, ItemStack> containerLeftOvers = containerInventory.addItem(
  180.                         Arrays.stream(hopperContents).filter((contents) -> contents != null &&
  181.                                 contents.getType() != Material.AIR).toArray(ItemStack[]::new));
  182.  
  183.                 hopperInventory.clear();
  184.                 if (!containerLeftOvers.isEmpty()) {
  185.                     ifContainerFull = true;
  186.                     Collection<ItemStack> collectionItemStack = containerLeftOvers.values();
  187.  
  188.                     hopperInventory.addItem(collectionItemStack.toArray(new ItemStack[0]));
  189.                 }
  190.             }
  191.         }
  192.     }
  193.  
  194.     private Location inventoryFull(Location location, ContainerRegistry registry) {
  195.         List<Location> containerLocation = registry.getLinkContainerLinkedToLocation(location);
  196.         Location locations = null;
  197.         //System.out.println("if container full" + getIfContainerFull());
  198.         if (containerLocation != null && containerLocation.size() > 0) {
  199.             int random = (int) ((Math.random() * containerLocation.size()) + 0);
  200.             locations = containerLocation.get(random);
  201.         }
  202.         for (Location loc : containerLocation)
  203.  
  204.  
  205.             getLinkedInventory(locations, location);
  206.         return locations;
  207.     }
  208.  
  209.  
  210.     private void getLinkedInventory(Location listedLocations, Location containerLocation) {
  211.  
  212.         if (listedLocations != null) {
  213.             Block blocktype = listedLocations.getBlock();
  214.             //System.out.println("containerLocation " + blocktype.getState().getLocation());
  215.             if (blocktype.getState() instanceof Hopper) {
  216.                 containerInventory = (((Hopper) blocktype.getState()).getInventory());
  217.                 //System.out.println("test ");
  218.             } else {
  219.                 if (!(blocktype.getState() instanceof Chest)) {
  220.                     containerInventory = null;
  221.                     this.registry.removeLinkContainerLinkedToLocation(listedLocations, containerLocation);
  222.                     return;
  223.                 } else {
  224.                     containerInventory = (((Chest) blocktype.getState()).getInventory());
  225.                 }
  226.             }
  227.         }
  228.     }
  229.  
  230. }
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement