use of com.bgsoftware.wildchests.utils.LinkedChestInteractEvent in project WildChests by BG-Software-LLC.
the class CommandLink method perform.
@Override
public void perform(WildChestsPlugin plugin, CommandSender sender, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Only players can use this command.");
return;
}
Player player = (Player) sender;
Block targetBlock = player.getTargetBlock((Set<Material>) null, 5);
if (targetBlock == null || targetBlock.getType() != Material.CHEST) {
Locale.INVALID_BLOCK_CHEST.send(player);
return;
}
LinkedChestInteractEvent linkedChestInteractEvent = new LinkedChestInteractEvent(player, targetBlock);
Bukkit.getPluginManager().callEvent(linkedChestInteractEvent);
if (linkedChestInteractEvent.isCancelled()) {
Locale.NOT_LINKED_CHEST.send(player);
return;
}
Chest chest = plugin.getChestsManager().getChest(targetBlock.getLocation());
if (!(chest instanceof LinkedChest)) {
Locale.NOT_LINKED_CHEST.send(player);
return;
}
LinkedChest linkedChest = (LinkedChest) chest;
if (players.containsKey(player.getUniqueId())) {
LinkedChest originalChest = plugin.getChestsManager().getLinkedChest(players.get(player.getUniqueId()));
players.remove(player.getUniqueId());
if (originalChest == null || originalChest.getLocation().equals(linkedChest.getLocation()) || originalChest.equals(linkedChest.getLinkedChest())) {
Locale.NOT_LINKED_CHEST.send(player);
return;
}
List<ItemStack> toMove = new ArrayList<>();
for (int page = 0; page < originalChest.getPagesAmount(); page++) {
Inventory inventory = originalChest.getPage(page);
for (ItemStack itemStack : inventory.getContents()) {
if (itemStack != null && itemStack.getType() != Material.AIR) {
toMove.add(itemStack);
}
}
inventory.clear();
}
originalChest.linkIntoChest(linkedChest);
Locale.LINKED_SUCCEED.send(player, LocationUtils.toString(originalChest.getLocation()));
if (!toMove.isEmpty()) {
linkedChest.addItems(toMove.toArray(new ItemStack[] {}));
Locale.LEFTOVERS_ITEMS_WARNING.send(player);
}
return;
}
players.put(player.getUniqueId(), linkedChest.getLocation());
Executor.async(() -> players.remove(player.getUniqueId()), 6000L);
Locale.SELECT_ANOTHER_CHEST.send(player);
}
use of com.bgsoftware.wildchests.utils.LinkedChestInteractEvent in project WildChests by BG-Software-LLC.
the class InventoryListener method onChestOpen.
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onChestOpen(PlayerInteractEvent e) {
if (e instanceof LinkedChestInteractEvent || e.getAction() != Action.RIGHT_CLICK_BLOCK || e.getClickedBlock().getType() != Material.CHEST)
return;
if (buyNewPage.containsKey(e.getPlayer().getUniqueId())) {
e.setCancelled(true);
return;
}
Chest chest = plugin.getChestsManager().getChest(e.getClickedBlock().getLocation());
if (chest == null)
return;
plugin.getNMSInventory().updateTileEntity(chest);
chest.onOpen(e);
}
Aggregations