Search in sources :

Example 6 with KeyType

use of com.badbones69.crazycrates.api.enums.KeyType in project Crazy-Crates by Crazy-Crew.

the class CrateControl method onCrateOpen.

// This must run as highest, so it doesn't cause other plugins to check
// the items that were added to the players inventory and replaced the item in the player's hand.
// This is only an issue with QuickCrate
@EventHandler(priority = EventPriority.HIGHEST)
public void onCrateOpen(PlayerInteractEvent e) {
    Player player = e.getPlayer();
    FileConfiguration config = Files.CONFIG.getFile();
    if (e.getHand() == EquipmentSlot.OFF_HAND) {
        if (cc.isKey(player.getInventory().getItemInOffHand())) {
            e.setCancelled(true);
            player.updateInventory();
        }
        return;
    }
    Block clickedBlock = e.getClickedBlock();
    if (e.getAction() == Action.LEFT_CLICK_BLOCK) {
        // Loops through all loaded physical locations.
        for (CrateLocation loc : cc.getCrateLocations()) {
            // Checks to see if the clicked block is the same as a physical crate.
            if (loc.getLocation().equals(clickedBlock.getLocation())) {
                // Checks to see if the player is removing a crate location.
                if (player.getGameMode() == GameMode.CREATIVE && player.isSneaking() && player.hasPermission("crazycrates.admin")) {
                    e.setCancelled(true);
                    cc.removeCrateLocation(loc.getID());
                    player.sendMessage(Messages.REMOVED_PHYSICAL_CRATE.getMessage("%ID%", loc.getID()));
                    return;
                }
                e.setCancelled(true);
                if (loc.getCrateType() != CrateType.MENU) {
                    if (loc.getCrate().isPreviewEnabled()) {
                        Preview.setPlayerInMenu(player, false);
                        Preview.openNewPreview(player, loc.getCrate());
                    } else {
                        player.sendMessage(Messages.PREVIEW_DISABLED.getMessage());
                    }
                }
            }
        }
    } else if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
        // Checks if the item in their hand is a key and if so it stops them from right-clicking with it.
        ItemStack key = cc.getNMSSupport().getItemInMainHand(player);
        boolean keyInHand = cc.isKey(key);
        if (!keyInHand) {
            keyInHand = cc.isKey(player.getEquipment().getItemInOffHand());
        }
        if (keyInHand) {
            e.setCancelled(true);
            player.updateInventory();
        }
        // Checks to see if the clicked block is a physical crate.
        CrateLocation crateLocation = cc.getCrateLocation(clickedBlock.getLocation());
        if (crateLocation != null && crateLocation.getCrate() != null) {
            Crate crate = crateLocation.getCrate();
            e.setCancelled(true);
            if (crate.getCrateType() == CrateType.MENU) {
                // This is to stop players in QuadCrate to not be able to try and open a crate set to menu.
                if (!cc.isInOpeningList(player)) {
                    GUIMenu.openGUI(player);
                }
                return;
            }
            PhysicalCrateKeyCheckEvent event = new PhysicalCrateKeyCheckEvent(player, crateLocation);
            CrazyManager.getJavaPlugin().getServer().getPluginManager().callEvent(event);
            if (!event.isCancelled()) {
                boolean hasKey = false;
                boolean isPhysical = false;
                boolean useQuickCrateAgain = false;
                String keyName = crate.getKey().getItemMeta().getDisplayName();
                keyName = keyName != null ? keyName : crate.getKey().getType().toString();
                if (crate.getCrateType() != CrateType.CRATE_ON_THE_GO && keyInHand && cc.isKeyFromCrate(key, crate)) {
                    hasKey = true;
                    isPhysical = true;
                }
                if (config.getBoolean("Settings.Physical-Accepts-Virtual-Keys") && cc.getVirtualKeys(player, crate) >= 1) {
                    hasKey = true;
                }
                if (hasKey) {
                    // Checks if the player uses the quick crate again.
                    if (cc.isInOpeningList(player) && cc.getOpeningCrate(player).getCrateType() == CrateType.QUICK_CRATE && inUse.containsKey(player) && inUse.get(player).equals(crateLocation.getLocation())) {
                        useQuickCrateAgain = true;
                    }
                    if (!useQuickCrateAgain) {
                        if (cc.isInOpeningList(player)) {
                            player.sendMessage(Messages.ALREADY_OPENING_CRATE.getMessage("%Key%", keyName));
                            return;
                        }
                        if (inUse.containsValue(crateLocation.getLocation())) {
                            player.sendMessage(Messages.QUICK_CRATE_IN_USE.getMessage());
                            return;
                        }
                    }
                    if (Methods.isInventoryFull(player)) {
                        player.sendMessage(Messages.INVENTORY_FULL.getMessage());
                        return;
                    }
                    if (useQuickCrateAgain) {
                        QuickCrate.endQuickCrate(player, crateLocation.getLocation());
                    }
                    KeyType keyType = isPhysical ? KeyType.PHYSICAL_KEY : KeyType.VIRTUAL_KEY;
                    if (crate.getCrateType() == CrateType.COSMIC) {
                        // Only cosmic crate type uses this method.
                        cc.addPlayerKeyType(player, keyType);
                    }
                    cc.addPlayerToOpeningList(player, crate);
                    cc.openCrate(player, crate, keyType, crateLocation.getLocation(), false, true);
                } else {
                    if (crate.getCrateType() != CrateType.CRATE_ON_THE_GO) {
                        if (config.getBoolean("Settings.KnockBack")) {
                            knockBack(player, clickedBlock.getLocation());
                        }
                        if (config.contains("Settings.Need-Key-Sound")) {
                            Sound sound = Sound.valueOf(config.getString("Settings.Need-Key-Sound"));
                            if (sound != null) {
                                player.playSound(player.getLocation(), sound, 1f, 1f);
                            }
                        }
                        player.sendMessage(Messages.NO_KEY.getMessage("%Key%", keyName));
                    }
                }
            }
        }
    }
}
Also used : FileConfiguration(org.bukkit.configuration.file.FileConfiguration) Player(org.bukkit.entity.Player) PhysicalCrateKeyCheckEvent(com.badbones69.crazycrates.api.events.PhysicalCrateKeyCheckEvent) KeyType(com.badbones69.crazycrates.api.enums.KeyType) QuickCrate(com.badbones69.crazycrates.cratetypes.QuickCrate) Crate(com.badbones69.crazycrates.api.objects.Crate) CrateLocation(com.badbones69.crazycrates.api.objects.CrateLocation) Block(org.bukkit.block.Block) Sound(org.bukkit.Sound) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Aggregations

ItemStack (org.bukkit.inventory.ItemStack)5 Inventory (org.bukkit.inventory.Inventory)4 KeyType (com.badbones69.crazycrates.api.enums.KeyType)3 PlayerPrizeEvent (com.badbones69.crazycrates.api.events.PlayerPrizeEvent)3 Crate (com.badbones69.crazycrates.api.objects.Crate)3 Prize (com.badbones69.crazycrates.api.objects.Prize)3 ArrayList (java.util.ArrayList)3 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)3 Player (org.bukkit.entity.Player)3 EventHandler (org.bukkit.event.EventHandler)3 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)3 ItemBuilder (com.badbones69.crazycrates.api.objects.ItemBuilder)2 NBTItem (de.tr7zw.changeme.nbtapi.NBTItem)2 Sound (org.bukkit.Sound)2 BrokeLocation (com.badbones69.crazycrates.api.enums.BrokeLocation)1 PhysicalCrateKeyCheckEvent (com.badbones69.crazycrates.api.events.PhysicalCrateKeyCheckEvent)1 PlayerReceiveKeyEvent (com.badbones69.crazycrates.api.events.PlayerReceiveKeyEvent)1 CosmicCrateManager (com.badbones69.crazycrates.api.managers.CosmicCrateManager)1 CrateLocation (com.badbones69.crazycrates.api.objects.CrateLocation)1 Tier (com.badbones69.crazycrates.api.objects.Tier)1