broken-arrow

Untitled

May 7th, 2021
724
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.broken.cheststorage.ChestStorage;
  2. import org.broken.cheststorage.data.ChestRegistry;
  3. import org.broken.cheststorage.data.ChestRegistry.ChestData;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.Location;
  6. import org.bukkit.Material;
  7. import org.bukkit.block.Block;
  8. import org.bukkit.block.Hopper;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.event.EventHandler;
  11. import org.bukkit.event.EventPriority;
  12. import org.bukkit.event.Listener;
  13. import org.bukkit.event.block.Action;
  14. import org.bukkit.event.inventory.*;
  15. import org.bukkit.event.player.PlayerInteractEvent;
  16. import org.bukkit.inventory.Inventory;
  17. import org.bukkit.inventory.InventoryHolder;
  18. import org.bukkit.inventory.ItemStack;
  19. import org.bukkit.metadata.FixedMetadataValue;
  20. import org.bukkit.scheduler.BukkitRunnable;
  21. import org.mineacademy.fo.plugin.SimplePlugin;
  22.  
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27.  
  28. public final class ChestInventoryListener extends ChestGuiInventory implements Listener {
  29.  
  30.     public static final Map<Player, Object> viewers = new HashMap<>();
  31.     private final static String CHEST_METADATA = "SpecialChestLocation";
  32.  
  33.     @EventHandler(priority = EventPriority.LOWEST)
  34.     public void onInventoryPlayerUpdate(InventoryClickEvent event) {
  35.         inventoryUpdates(event);
  36.     }
  37.  
  38.     @EventHandler(priority = EventPriority.LOWEST)
  39.     public void onInventoryPlayerUpdate(InventoryDragEvent event) {
  40.         inventoryUpdates(event);
  41.         //craftingUpdate(event);
  42.     }
  43.  
  44.     @EventHandler(priority = EventPriority.LOW)
  45.     public void onBlockClick(PlayerInteractEvent event) {
  46.  
  47.         Player player = event.getPlayer();
  48.         Block clickedBlock = event.getClickedBlock();
  49.  
  50.         if (clickedBlock != null && clickedBlock.getType() == Material.CHEST
  51.                 && event.getAction() == Action.RIGHT_CLICK_BLOCK && !player.isSneaking()) {
  52.  
  53.             Location blockLocation = event.getClickedBlock().getLocation();
  54.             ChestData chestData = ChestRegistry.getInstance().findChest(blockLocation);
  55.  
  56.             //System.out.println("Attempted to load a chest inventory at " + Common.shortLocation(blockLocation) + " = " + chestData);
  57.  
  58.             if (chestData != null) {
  59.                 player.setMetadata(CHEST_METADATA, new FixedMetadataValue(SimplePlugin.getInstance(), blockLocation));
  60.                 Inventory inventory = createMenu();
  61.  
  62.                 onOpen(event);
  63.                 player.openInventory(getInventory());
  64.  
  65.                 System.out.println(ChestGuiInventory.viewers.get(player.getUniqueId()));
  66.                 //showMenus(player, chestData);
  67.                 event.setCancelled(true);
  68.                 getInventory().getViewers().remove(event.getPlayer());
  69.                 //ItemStack[] itemStacks = chestData.getChestContents();
  70.                 //getInventory().setContents(itemStacks);
  71.                 System.out.println("Inventory  " + getInventory());
  72.                 //System.out.println("createMenu" + createMenu());
  73.             }
  74.         }
  75.     }
  76.  
  77.  
  78.     private void showMenu(Player player, ChestData chestData) {
  79.         int size = 27;
  80.         Inventory gui = Bukkit.createInventory(null, size, "Gui Chest");
  81.         ItemStack[] items = chestData.getChestContents();
  82.  
  83.         if (items == null)
  84.             items = new ItemStack[size];
  85.  
  86.         gui.setContents(items);
  87.         player.openInventory(gui);
  88.     }
  89.  
  90.     @EventHandler
  91.     public void inventoryUpdates(InventoryInteractEvent event) {
  92.         InventoryHolder holder = event.getInventory().getHolder();
  93.         Location locations = event.getInventory().getLocation();
  94.         System.out.println("if InventoryInteractEvent ");
  95.         Player player = (Player) event.getWhoClicked();
  96.  
  97.         if (!player.hasMetadata(CHEST_METADATA)) return;
  98.  
  99.         Location location = (Location) player.getMetadata("SpecialChestLocation").get(0).value();
  100.         ChestRegistry registry = ChestRegistry.getInstance();
  101.         ChestData chestData = registry.findChest(location);
  102.  
  103.         System.out.println("chest loc" + location);
  104.         if (chestData != null) {
  105.  
  106.             System.out.println("if Inventory ");
  107.             Bukkit.getScheduler().scheduleSyncDelayedTask(ChestStorage.getInstance(), () -> {
  108.                 ItemStack[] items = chestData.getChestContents();
  109.                 if (getInventory().getHolder() != null)
  110.                     getInventory().setContents(items);
  111.                 ItemStack[] item = getInventory().getContents();
  112.                 registry.saveChestItemsFromHopper(location, item);
  113.             }, 1);
  114.         }
  115.     }
  116.  
  117.     @EventHandler
  118.     public void onInventoryclick(InventoryClickEvent event) {
  119.  
  120.     /*  ChestStorage.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(ChestStorage.getInstance(), () -> {
  121.             List<HumanEntity> plyers = event.getInventory().getViewers();
  122.  
  123.             for (Map.Entry<Player, Object> entry : viewers.entrySet()) {
  124.                 Player p = entry.getKey();
  125.                 //Player p = (Player) he;
  126.                 p.updateInventory();
  127.                 System.out.println("player fun " + p);
  128.             }
  129.         }, 1L);*/
  130.     }
  131.  
  132.     @EventHandler(priority = EventPriority.LOW)
  133.     public void onInventoryClose(InventoryCloseEvent event) {
  134.         Inventory inventory = event.getInventory();
  135.         Player player = (Player) event.getPlayer();
  136.  
  137.         if (!player.hasMetadata(CHEST_METADATA)) {
  138.             System.out.println("@RETURN DUE TO NO METADATA");
  139.  
  140.             return;
  141.         }
  142.  
  143.         Location location = (Location) player.getMetadata("SpecialChestLocation").get(0).value();
  144.         ChestRegistry registry = ChestRegistry.getInstance();
  145.         ChestData chestData = registry.findChest(location);
  146.  
  147.         //System.out.println("Attempted to save chest data at " + Common.shortLocation(location) + " and found: " + chestData);
  148.         player.removeMetadata(CHEST_METADATA, ChestStorage.getInstance());
  149.  
  150.         if (chestData != null)
  151.             //registry.saveChestItems(location, player.getUniqueId(), inventory.getContents());
  152.  
  153.             //viewers.remove(event.getPlayer());
  154.             onClose(event);
  155.         getInventory().getViewers().remove(event.getPlayer());
  156.     }
  157.  
  158.  
  159.     public void onInventoryMoveIn(InventoryMoveItemEvent event) {
  160.  
  161.  
  162.         ArrayList<ItemStack> items = new ArrayList<>();
  163.         Inventory destination = event.getDestination();
  164.         Location destinationloc = event.getDestination().getLocation();
  165.         Inventory source = event.getSource();
  166.         Location sourceloc = event.getSource().getLocation();
  167.         Inventory initiator = event.getInitiator();
  168.         Location initiatorloc = event.getInitiator().getLocation();
  169.         ItemStack item = event.getItem();
  170.         items.add(item);
  171.  
  172.         ItemStack[] test = items.toArray(new ItemStack[0]);
  173.  
  174.         ChestRegistry registry = ChestRegistry.getInstance();
  175.         ChestData chestData = registry.findChest(destinationloc);
  176.         System.out.println("Destination " + test);
  177.         if (chestData != null) {
  178.             System.out.println("Destination " + destination + " " + destinationloc);
  179.             event.setCancelled(true);
  180.             //event.setItem();
  181.             registry.saveChestItemsFromHopper(destinationloc, test);
  182.  
  183.         }
  184.         /*System.out.println("source " + source + " " + sourceloc);
  185.         System.out.println("Initiator " + initiator + " " + initiatorloc);
  186.         System.out.println("item " + item);*/
  187.     }
  188.  
  189.     /**
  190.      * Detect if items are moved from hopper to chest or chest to hopper.
  191.      *
  192.      * @param event
  193.      */
  194.  
  195.     @EventHandler(priority = EventPriority.NORMAL)
  196.     public void fromHopperToContainer(InventoryMoveItemEvent event) {
  197.         if (event.getInitiator().getHolder() instanceof Hopper) {
  198.             ChestRegistry registry = ChestRegistry.getInstance();
  199.             Location location = event.getDestination().getLocation();
  200.  
  201.             ChestData chestData = registry.findChest(location);
  202.             //ItemStack[] itemStacks = chestData.getChestContents();
  203.             //chestData = registry.findChest(location);
  204.  
  205.             ArrayList<ItemStack> items = new ArrayList<>();
  206.             ItemStack item = event.getItem();
  207.             items.add(item);
  208.             ItemStack[] test = items.toArray(new ItemStack[0]);
  209.  
  210.             //System.out.println("Items " + Arrays.toString(test));
  211.  
  212.             if (chestData != null) {
  213.                 if (!event.isCancelled()) {
  214.                     event.setCancelled(true);
  215.                     new BukkitRunnable() {
  216.                         @Override
  217.                         public void run() {
  218.                             if (location != null) {
  219.                                 int hopperAmount = 1;
  220.                                 if (hopperMoveItemFromToChest(event.getSource(), hopperAmount, getInventory())) {
  221.                                     registry.saveChestItemsFromHopper(location, test);
  222.                                 }
  223.                                 /*if (event.getDestination().getHolder() != null)
  224.                                     event.getDestination().getHolder().getInventory().clear();*/
  225.                                 //if (getInventory().getViewers().size() > 0) getInventory().setContents(itemStacks);
  226.                             }
  227.                         }
  228.                     }.runTaskLater(ChestStorage.getInstance(), 1);
  229.                 }
  230.             }
  231.         }
  232.     }
  233.  
  234.     public static ItemStack removeStackFromInventory(Inventory inventory, int amount, List filters) {
  235.         ItemStack RemoveItemsFromContainerBefore;
  236.         for (int i = 0; i < inventory.getContents().length; i++) {
  237.             ItemStack stack = inventory.getItem(i);
  238.             if ((stack != null)) {
  239.                 RemoveItemsFromContainerBefore = stack.clone();
  240.                 RemoveItemsFromContainerBefore.setAmount(Math.min(stack.getAmount(), amount));
  241.                 stack.setAmount(stack.getAmount() - RemoveItemsFromContainerBefore.getAmount());
  242.  
  243.                 System.out.println("stack.getAmount " + stack.getAmount());
  244.                 System.out.println("Items before " + RemoveItemsFromContainerBefore);
  245.  
  246.                 return RemoveItemsFromContainerBefore;
  247.             }
  248.         }
  249.         return null;
  250.     }
  251.  
  252.     public static boolean hopperMoveItemFromToChest(Inventory from, int amount, Inventory to, List filters) {
  253.         ItemStack removed = removeStackFromInventory(from, amount, null);
  254.         if (removed != null) {
  255.             HashMap<Integer, ItemStack> leftOvers = to.addItem(removed);
  256.             for (ItemStack leftOver : leftOvers.values()) {
  257.                 from.addItem(leftOver);
  258.  
  259.                 System.out.println("Items leftOver " + leftOver);
  260.                 if (removed.equals(leftOver)) return false;
  261.             }
  262.             return true;
  263.         }
  264.         return false;
  265.     }
  266.  
  267.     public static boolean hopperMoveItemFromToChest(Inventory from, int amount, Inventory to) {
  268.         return hopperMoveItemFromToChest(from, amount, to, null);
  269.     }
  270.  
  271.     public static boolean hopperMoveItemFromToChest(Inventory from, ItemStack stack, int amount, Inventory to) {
  272.         if (stack != null) {
  273.             ItemStack toRemoveFromHopper = stack.clone();
  274.             toRemoveFromHopper.setAmount(Math.min(stack.getAmount(), amount));
  275.             stack.setAmount(stack.getAmount() - toRemoveFromHopper.getAmount());
  276.  
  277.             HashMap<Integer, ItemStack> leftOvers = to.addItem(toRemoveFromHopper);
  278.             for (ItemStack leftOver : leftOvers.values()) {
  279.                 from.addItem(leftOver);
  280.                 if (toRemoveFromHopper.equals(leftOver)) return false;
  281.             }
  282.             return true;
  283.         }
  284.         return false;
  285.     }
  286.  
  287. }
RAW Paste Data