Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.broken.cheststorage.ChestStorage;
- import org.broken.cheststorage.Crafting.FilterItems;
- import org.broken.cheststorage.cache.ContainerRegistry;
- import org.broken.cheststorage.containerholders.InventoryHolder;
- import org.broken.cheststorage.containerholders.InventoryHolderDefultchest;
- import org.broken.cheststorage.containerholders.InventoryHolderStorageUnit;
- import org.broken.cheststorage.util.MathCalations;
- import org.broken.cheststorage.util.YamlSettingsContainers;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.Particle;
- import org.bukkit.World;
- import org.bukkit.block.Barrel;
- import org.bukkit.block.Block;
- import org.bukkit.block.Chest;
- import org.bukkit.block.Hopper;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.Item;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import java.util.*;
- public class TeleportItems {
- private final ChestStorage plugin = ChestStorage.getInstance();
- private final ContainerRegistry registry = ContainerRegistry.getInstance();
- private World world;
- private Inventory hopperInventory;
- private Inventory containerInventory;
- int delayItemPicupTeleport = 0;
- int secondstoPickcup = 0;
- private boolean ifContainerFull;
- private final Location location;
- public TeleportItems(int secondstoPickcup, Location location) {
- this.location = location;
- this.secondstoPickcup = secondstoPickcup;
- }
- public void run() {
- delayItemPicupTeleport++;
- if (delayItemPicupTeleport >= secondstoPickcup) {
- System.out.println("tes " + delayItemPicupTeleport + "loc" + this.location);
- linkedContainer();
- delayItemPicupTeleport = 0;
- }
- }
- private void linkedContainer() {
- Iterator<Entity> items = null;
- String nextUpdate = registry.getCurrentUpdate(location);
- String sugtionRange = YamlSettingsContainers.getSuctionRange(registry.getContainerFileName(location), nextUpdate);
- String[] radieSplited;
- int locX;
- int locY;
- int locZ;
- world = location.getChunk().getWorld();
- Block placedItem = location.getBlock();
- int radiesLengt = (radieSplited = sugtionRange.split(",")).length;
- if (radiesLengt == 3) {
- locX = Integer.parseInt(radieSplited[0]);
- locY = Integer.parseInt(radieSplited[1]);
- locZ = Integer.parseInt(radieSplited[2]);
- items = this.world.getNearbyEntities(location.clone().add(0.5, 0, 0.5), locX, locY, locZ).iterator();
- } else if (sugtionRange.equals("-1")) {
- Location locationCloned = MathCalations.getCentrumBlock(location.clone(), -0.5, false);
- items = this.world.getNearbyEntities(locationCloned, 8, location.getWorld().getMaxHeight(), 8).iterator();
- } else if (sugtionRange.equals("-2")) return;
- if (placedItem.getState() instanceof Hopper) {
- this.hopperInventory = ((Hopper) placedItem.getState()).getInventory();
- } else if (placedItem.getState() instanceof Chest) {
- this.hopperInventory = ((Chest) placedItem.getState()).getInventory();
- } else if (placedItem.getState() instanceof Barrel)
- this.hopperInventory = ((Barrel) placedItem.getState()).getInventory();
- ItemStack[] hopperContents = this.hopperInventory.getContents();
- teleportItemsToContainer(registry, location, hopperContents);
- if (items != null)
- findItemAndAddToContainer(items, location);
- }
- public boolean getIfContainerFull() {
- return ifContainerFull;
- }
- private void findItemAndAddToContainer(Iterator<Entity> items, Location location) {
- while (items.hasNext()) {
- Entity itemstacks = items.next();
- if (itemstacks.isValid() && itemstacks.getTicksLived() >= 20 && itemstacks instanceof Item) {
- ItemStack itemOnGrund = ((Item) itemstacks).getItemStack().clone();
- FilterItems itemsSort = new FilterItems();
- System.out.println("test");
- if (itemsSort.checkIfItemMatch(location, itemOnGrund)) {
- if (hopperInventory.firstEmpty() == -1 && !hopperInventory.containsAtLeast(itemOnGrund, 1))
- break;
- itemstacks.getWorld().spawnParticle(Particle.FIREWORKS_SPARK, itemstacks.getLocation(), 2, 0.0, 0.1, 0.0, 0.2);
- if (((Item) itemstacks).getItemStack().getAmount() > 64) {
- ((Item) itemstacks).getItemStack().setAmount(((Item) itemstacks).getItemStack().getAmount() - 64);
- itemOnGrund = itemOnGrund.clone();
- itemOnGrund.setAmount(64);
- } else {
- itemOnGrund.setAmount(((Item) itemstacks).getItemStack().getAmount());
- itemstacks.remove();
- }
- HashMap<Integer, ItemStack> leftOvers = hopperInventory.addItem(new ItemStack(itemOnGrund));
- if (!leftOvers.isEmpty())
- this.world.dropItemNaturally(location, itemOnGrund);
- }
- }
- }
- }
- private void teleportItemsToContainer(ContainerRegistry registry, Location location, ItemStack[] hopperContents) {
- Location containerLocations = inventoryFull(location, registry);
- if (containerLocations != null) {
- InventoryHolder customInventory = plugin.getClassinstans().createObjektInventoryHolder(containerLocations, null);
- InventoryHolderDefultchest holderDefultchest = new InventoryHolderDefultchest(containerLocations, null);
- InventoryHolderStorageUnit storageUnit = new InventoryHolderStorageUnit(containerLocations, null);
- if (registry.getTypeofContainer(containerLocations) != null)
- switch (registry.getTypeofContainer(containerLocations)) {
- case PAGEDCONTAINER:
- case CRAFTINGCONTAINER:
- case SELLCHEST:
- case STORAGEUNIT: {
- Map<Integer, ItemStack> containerLeftOvers = customInventory.addItems(Arrays.stream(hopperContents).filter((contents) -> contents != null &&
- contents.getType() != Material.AIR).toArray(ItemStack[]::new));
- hopperInventory.clear();
- if (!containerLeftOvers.isEmpty()) {
- ifContainerFull = true;
- Collection<ItemStack> collectionItemStack = containerLeftOvers.values();
- hopperInventory.addItem(collectionItemStack.toArray(new ItemStack[0]));
- }
- break;
- }
- /*case LINKEDCONTAINER:
- break;*/
- default:
- /*if (containerInventory != null) {
- HashMap<Integer, ItemStack> containerLeftOvers = containerInventory.addItem(
- Arrays.stream(hopperContents).filter((contents) -> contents != null &&
- contents.getType() != Material.AIR).toArray(ItemStack[]::new));
- hopperInventory.clear();
- if (!containerLeftOvers.isEmpty()) {
- ifContainerFull = true;
- Collection<ItemStack> collectionItemStack = containerLeftOvers.values();
- hopperInventory.addItem(collectionItemStack.toArray(new ItemStack[0]));
- }
- }*/
- }
- else if (containerInventory != null) {
- HashMap<Integer, ItemStack> containerLeftOvers = containerInventory.addItem(
- Arrays.stream(hopperContents).filter((contents) -> contents != null &&
- contents.getType() != Material.AIR).toArray(ItemStack[]::new));
- hopperInventory.clear();
- if (!containerLeftOvers.isEmpty()) {
- ifContainerFull = true;
- Collection<ItemStack> collectionItemStack = containerLeftOvers.values();
- hopperInventory.addItem(collectionItemStack.toArray(new ItemStack[0]));
- }
- }
- }
- }
- private Location inventoryFull(Location location, ContainerRegistry registry) {
- List<Location> containerLocation = registry.getLinkContainerLinkedToLocation(location);
- Location locations = null;
- //System.out.println("if container full" + getIfContainerFull());
- if (containerLocation != null && containerLocation.size() > 0) {
- int random = (int) ((Math.random() * containerLocation.size()) + 0);
- locations = containerLocation.get(random);
- }
- for (Location loc : containerLocation)
- getLinkedInventory(locations, location);
- return locations;
- }
- private void getLinkedInventory(Location listedLocations, Location containerLocation) {
- if (listedLocations != null) {
- Block blocktype = listedLocations.getBlock();
- //System.out.println("containerLocation " + blocktype.getState().getLocation());
- if (blocktype.getState() instanceof Hopper) {
- containerInventory = (((Hopper) blocktype.getState()).getInventory());
- //System.out.println("test ");
- } else {
- if (!(blocktype.getState() instanceof Chest)) {
- containerInventory = null;
- this.registry.removeLinkContainerLinkedToLocation(listedLocations, containerLocation);
- return;
- } else {
- containerInventory = (((Chest) blocktype.getState()).getInventory());
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement