Search in sources :

Example 1 with PlayerReceiveKeyEvent

use of com.badbones69.crazycrates.api.events.PlayerReceiveKeyEvent in project Crazy-Crates by Crazy-Crew.

the class Cosmic method onInvClick.

@EventHandler
public void onInvClick(InventoryClickEvent e) {
    final Inventory inv = e.getInventory();
    final Player player = (Player) e.getWhoClicked();
    if (inv != null) {
        final Crate crate = cc.getOpeningCrate(player);
        if (cc.isInOpeningList(player)) {
            if (!crate.getFile().getString("Crate.CrateType").equalsIgnoreCase("Cosmic"))
                return;
        } else {
            return;
        }
        final FileConfiguration file = crate.getFile();
        if (e.getView().getTitle().equals(Methods.sanitizeColor(file.getString("Crate.CrateName") + " - Shuffling"))) {
            e.setCancelled(true);
        }
        if (e.getView().getTitle().equals(Methods.sanitizeColor(file.getString("Crate.CrateName") + " - Prizes"))) {
            e.setCancelled(true);
            int slot = e.getRawSlot();
            if (inCosmic(slot)) {
                for (int i : picks.get(player)) {
                    if (slot == i) {
                        ItemStack item = e.getCurrentItem();
                        Tier tier = getTier(crate, item);
                        if (item != null && tier != null) {
                            Prize prize = crate.pickPrize(player, tier);
                            for (int stop = 0; prize == null && stop <= 2000; stop++) {
                                prize = crate.pickPrize(player, tier);
                            }
                            if (prize != null) {
                                cc.givePrize(player, prize);
                                CrazyManager.getJavaPlugin().getServer().getPluginManager().callEvent(new PlayerPrizeEvent(player, crate, cc.getOpeningCrate(player).getName(), prize));
                                e.setCurrentItem(prize.getDisplayItem());
                                player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 1);
                                if (prize.useFireworks()) {
                                    Methods.fireWork(player.getLocation().add(0, 1, 0));
                                }
                            }
                            return;
                        }
                    }
                }
            }
        }
        if (e.getView().getTitle().equals(Methods.sanitizeColor(file.getString("Crate.CrateName") + " - Choose"))) {
            e.setCancelled(true);
            int slot = e.getRawSlot();
            if (inCosmic(slot)) {
                ItemStack item = e.getCurrentItem();
                if (item != null) {
                    CosmicCrateManager manager = (CosmicCrateManager) crate.getManager();
                    int totalPrizes = manager.getTotalPrizes();
                    int pickedSlot = slot + 1;
                    NBTItem nbtItem = new NBTItem(item);
                    if (nbtItem.hasNBTData()) {
                        if (nbtItem.hasKey("Cosmic-Mystery-Crate")) {
                            if (!glass.containsKey(player))
                                glass.put(player, new ArrayList<>());
                            if (glass.get(player).size() < totalPrizes) {
                                e.setCurrentItem(manager.getPickedCrate().setAmount(pickedSlot).addNamePlaceholder("%Slot%", pickedSlot + "").addLorePlaceholder("%Slot%", pickedSlot + "").build());
                                glass.get(player).add(slot);
                            }
                            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1);
                        } else if (nbtItem.hasKey("Cosmic-Picked-Crate")) {
                            if (!glass.containsKey(player))
                                glass.put(player, new ArrayList<>());
                            e.setCurrentItem(manager.getMysteryCrate().setAmount(pickedSlot).addNamePlaceholder("%Slot%", pickedSlot + "").addLorePlaceholder("%Slot%", pickedSlot + "").build());
                            ArrayList<Integer> l = new ArrayList<>();
                            for (int i : glass.get(player)) if (i != slot)
                                l.add(i);
                            glass.put(player, l);
                            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1);
                        }
                    }
                    if (glass.get(player).size() >= totalPrizes) {
                        KeyType keyType = cc.getPlayerKeyType(player);
                        if (keyType == KeyType.PHYSICAL_KEY && !cc.hasPhysicalKey(player, crate, checkHands.get(player))) {
                            player.closeInventory();
                            player.sendMessage(Messages.NO_KEY.getMessage());
                            if (cc.isInOpeningList(player)) {
                                cc.removePlayerFromOpeningList(player);
                                cc.removePlayerKeyType(player);
                            }
                            checkHands.remove(player);
                            glass.remove(player);
                            return;
                        }
                        if (cc.hasPlayerKeyType(player) && !cc.takeKeys(1, player, crate, keyType, checkHands.get(player))) {
                            Methods.failedToTakeKey(player, crate);
                            cc.removePlayerFromOpeningList(player);
                            cc.removePlayerKeyType(player);
                            checkHands.remove(player);
                            glass.remove(player);
                            return;
                        }
                        cc.addCrateTask(player, new BukkitRunnable() {

                            int time = 0;

                            @Override
                            public void run() {
                                try {
                                    startRoll(player, crate);
                                } catch (Exception e) {
                                    PlayerReceiveKeyEvent event = new PlayerReceiveKeyEvent(player, crate, PlayerReceiveKeyEvent.KeyReceiveReason.REFUND, 1);
                                    CrazyManager.getJavaPlugin().getServer().getPluginManager().callEvent(event);
                                    if (!event.isCancelled()) {
                                        cc.addKeys(1, player, crate, keyType);
                                        cc.endCrate(player);
                                        cancel();
                                        player.sendMessage(Methods.getPrefix("&cAn issue has occurred and so a key refund was given."));
                                        CrazyManager.getJavaPlugin().getServer().getLogger().warning("An issue occurred when the user " + player.getName() + " was using the " + crate.getName() + " crate and so they were issued a key refund.");
                                        e.printStackTrace();
                                    }
                                    return;
                                }
                                time++;
                                if (time == 40) {
                                    cc.endCrate(player);
                                    showRewards(player, crate);
                                    player.playSound(player.getLocation(), Sound.BLOCK_ANVIL_PLACE, 1, 1);
                                    new BukkitRunnable() {

                                        @Override
                                        public void run() {
                                            if (player.getOpenInventory().getTopInventory().equals(inv)) {
                                                player.closeInventory();
                                            }
                                        }
                                    }.runTaskLater(CrazyManager.getJavaPlugin(), 40);
                                }
                            }
                        }.runTaskTimer(CrazyManager.getJavaPlugin(), 0, 2));
                    }
                }
            }
        }
    }
}
Also used : PlayerPrizeEvent(com.badbones69.crazycrates.api.events.PlayerPrizeEvent) Player(org.bukkit.entity.Player) KeyType(com.badbones69.crazycrates.api.enums.KeyType) Tier(com.badbones69.crazycrates.api.objects.Tier) CosmicCrateManager(com.badbones69.crazycrates.api.managers.CosmicCrateManager) Crate(com.badbones69.crazycrates.api.objects.Crate) ArrayList(java.util.ArrayList) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) Prize(com.badbones69.crazycrates.api.objects.Prize) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) PlayerReceiveKeyEvent(com.badbones69.crazycrates.api.events.PlayerReceiveKeyEvent) NBTItem(de.tr7zw.changeme.nbtapi.NBTItem) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) EventHandler(org.bukkit.event.EventHandler)

Example 2 with PlayerReceiveKeyEvent

use of com.badbones69.crazycrates.api.events.PlayerReceiveKeyEvent in project Crazy-Crates by Crazy-Crew.

the class CrazyManager method loadOfflinePlayersKeys.

/**
 * Load the offline keys of a player who has come online.
 * @param player The player which you would like to load the offline keys for.
 */
public void loadOfflinePlayersKeys(Player player) {
    FileConfiguration data = Files.DATA.getFile();
    String name = player.getName().toLowerCase();
    if (data.contains("Offline-Players." + name)) {
        for (Crate crate : getCrates()) {
            if (data.contains("Offline-Players." + name + "." + crate.getName())) {
                PlayerReceiveKeyEvent event = new PlayerReceiveKeyEvent(player, crate, KeyReceiveReason.OFFLINE_PLAYER, 1);
                getJavaPlugin().getServer().getPluginManager().callEvent(event);
                if (!event.isCancelled()) {
                    addKeys(data.getInt("Offline-Players." + name + "." + crate.getName()), player, crate, KeyType.VIRTUAL_KEY);
                }
            }
        }
        data.set("Offline-Players." + name, null);
        Files.DATA.saveFile();
    }
}
Also used : FileConfiguration(org.bukkit.configuration.file.FileConfiguration) PlayerReceiveKeyEvent(com.badbones69.crazycrates.api.events.PlayerReceiveKeyEvent)

Aggregations

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