Search in sources :

Example 1 with CrateLocation

use of com.badbones69.crazycrates.api.objects.CrateLocation in project Crazy-Crates by Crazy-Crew.

the class CrazyManager method loadCrates.

/**
 * Loads all the information the plugin needs to run.
 */
public void loadCrates() {
    giveNewPlayersKeys = false;
    crates.clear();
    brokecrates.clear();
    crateLocations.clear();
    crateSchematics.clear();
    nmsSupport = new NMS_Support();
    quadCrateTimer = Files.CONFIG.getFile().getInt("Settings.QuadCrate.Timer") * 20;
    giveVirtualKeysWhenInventoryFull = Files.CONFIG.getFile().getBoolean("Settings.Give-Virtual-Keys-When-Inventory-Full");
    if (Support.HOLOGRAPHIC_DISPLAYS.isPluginLoaded()) {
        hologramController = new HolographicSupport();
    } else if (Support.DECENT_HOLOGRAMS.isPluginLoaded()) {
        hologramController = new DecentHolograms();
    }
    // Removes all holograms so that they can be replaced.
    if (hologramController != null) {
        hologramController.removeAllHolograms();
    }
    if (fileManager.isLogging())
        getJavaPlugin().getLogger().info("Loading all crate information...");
    for (String crateName : fileManager.getAllCratesNames()) {
        // if(fileManager.isLogging()) plugin.getLogger().info(fileManager.getPrefix() + "Loading " + crateName + ".yml information....");
        try {
            FileConfiguration file = fileManager.getFile(crateName).getFile();
            CrateType crateType = CrateType.getFromName(file.getString("Crate.CrateType"));
            ArrayList<Prize> prizes = new ArrayList<>();
            String previewName = file.contains("Crate.Preview-Name") ? file.getString("Crate.Preview-Name") : file.getString("Crate.Name");
            ArrayList<Tier> tiers = new ArrayList<>();
            if (file.contains("Crate.Tiers") && file.getConfigurationSection("Crate.Tiers") != null) {
                for (String tier : file.getConfigurationSection("Crate.Tiers").getKeys(false)) {
                    String path = "Crate.Tiers." + tier;
                    tiers.add(new Tier(tier, file.getString(path + ".Name"), file.getString(path + ".Color"), file.getInt(path + ".Chance"), file.getInt(path + ".MaxRange")));
                }
            }
            if (crateType == CrateType.COSMIC && tiers.isEmpty()) {
                brokecrates.add(crateName);
                getJavaPlugin().getLogger().warning("No tiers were found for this cosmic crate " + crateName + ".yml file.");
                continue;
            }
            for (String prize : file.getConfigurationSection("Crate.Prizes").getKeys(false)) {
                Prize altPrize = null;
                String path = "Crate.Prizes." + prize;
                ArrayList<Tier> prizeTiers = new ArrayList<>();
                for (String tier : file.getStringList(path + ".Tiers")) {
                    for (Tier loadedTier : tiers) {
                        if (loadedTier.getName().equalsIgnoreCase(tier)) {
                            prizeTiers.add(loadedTier);
                        }
                    }
                }
                if (file.contains(path + ".Alternative-Prize")) {
                    if (file.getBoolean(path + ".Alternative-Prize.Toggle")) {
                        altPrize = new Prize("Alternative-Prize", file.getStringList(path + ".Alternative-Prize.Messages"), file.getStringList(path + ".Alternative-Prize.Commands"), // No editor items
                        null, getItems(file, prize + ".Alternative-Prize"));
                    }
                }
                ArrayList<ItemStack> editorItems = new ArrayList<>();
                if (file.contains(path + ".Editor-Items")) {
                    for (Object list : file.getList(path + ".Editor-Items")) {
                        editorItems.add((ItemStack) list);
                    }
                }
                prizes.add(new Prize(prize, getDisplayItem(file, prize), file.getStringList(path + ".Messages"), file.getStringList(path + ".Commands"), editorItems, getItems(file, prize), crateName, file.getInt(path + ".Chance", 100), file.getInt(path + ".MaxRange", 100), file.getBoolean(path + ".Firework"), file.getStringList(path + ".BlackListed-Permissions"), prizeTiers, altPrize));
            }
            int newPlayersKeys = file.getInt("Crate.StartingKeys");
            if (!giveNewPlayersKeys) {
                if (newPlayersKeys > 0) {
                    giveNewPlayersKeys = true;
                }
            }
            crates.add(new Crate(crateName, previewName, crateType, getKey(file), prizes, file, newPlayersKeys, tiers, new CrateHologram(file.getBoolean("Crate.Hologram.Toggle"), file.getDouble("Crate.Hologram.Height", 0.0), file.getStringList("Crate.Hologram.Message"))));
        } catch (Exception e) {
            brokecrates.add(crateName);
            getJavaPlugin().getLogger().warning("There was an error while loading the " + crateName + ".yml file.");
            e.printStackTrace();
        }
    }
    crates.add(new Crate("Menu", "Menu", CrateType.MENU, new ItemStack(Material.AIR), new ArrayList<>(), null, 0, null, null));
    if (fileManager.isLogging())
        getJavaPlugin().getLogger().info("All crate information has been loaded.");
    if (fileManager.isLogging())
        getJavaPlugin().getLogger().info("Loading all the physical crate locations.");
    FileConfiguration locations = Files.LOCATIONS.getFile();
    int loadedAmount = 0;
    int brokeAmount = 0;
    if (locations.getConfigurationSection("Locations") != null) {
        for (String locationName : locations.getConfigurationSection("Locations").getKeys(false)) {
            try {
                String worldName = locations.getString("Locations." + locationName + ".World");
                World world = getJavaPlugin().getServer().getWorld(worldName);
                int x = locations.getInt("Locations." + locationName + ".X");
                int y = locations.getInt("Locations." + locationName + ".Y");
                int z = locations.getInt("Locations." + locationName + ".Z");
                Location location = new Location(world, x, y, z);
                Crate crate = getCrateFromName(locations.getString("Locations." + locationName + ".Crate"));
                if (world != null && crate != null) {
                    crateLocations.add(new CrateLocation(locationName, crate, location));
                    if (hologramController != null) {
                        hologramController.createHologram(location.getBlock(), crate);
                    }
                    loadedAmount++;
                } else {
                    brokeLocations.add(new BrokeLocation(locationName, crate, x, y, z, worldName));
                    brokeAmount++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    // Checking if all physical locations loaded
    if (fileManager.isLogging()) {
        if (loadedAmount > 0 || brokeAmount > 0) {
            if (brokeAmount <= 0) {
                getJavaPlugin().getLogger().info("All physical crate locations have been loaded.");
            } else {
                getJavaPlugin().getLogger().info("Loaded " + loadedAmount + " physical crate locations.");
                getJavaPlugin().getLogger().info("Failed to load " + brokeAmount + " physical crate locations.");
            }
        }
    }
    // Loading schematic files
    if (fileManager.isLogging())
        getJavaPlugin().getLogger().info("Searching for schematics to load.");
    String[] schems = new File(getJavaPlugin().getDataFolder() + "/schematics/").list();
    for (String schematicName : schems) {
        if (schematicName.endsWith(".nbt")) {
            crateSchematics.add(new CrateSchematic(schematicName.replace(".nbt", ""), new File(getJavaPlugin().getDataFolder() + "/schematics/" + schematicName)));
            if (fileManager.isLogging())
                getJavaPlugin().getLogger().info(schematicName + " was successfully found and loaded.");
        }
    }
    if (fileManager.isLogging())
        getJavaPlugin().getLogger().info("All schematics were found and loaded.");
    cleanDataFile();
    Preview.loadButtons();
}
Also used : HolographicSupport(com.badbones69.crazycrates.support.holograms.HolographicSupport) BrokeLocation(com.badbones69.crazycrates.api.enums.BrokeLocation) DecentHolograms(com.badbones69.crazycrates.support.holograms.DecentHolograms) World(org.bukkit.World) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) NMS_Support(com.badbones69.crazycrates.nms.NMS_Support) CrateType(com.badbones69.crazycrates.api.enums.CrateType) ItemStack(org.bukkit.inventory.ItemStack) File(java.io.File) Location(org.bukkit.Location) BrokeLocation(com.badbones69.crazycrates.api.enums.BrokeLocation)

Example 2 with CrateLocation

use of com.badbones69.crazycrates.api.objects.CrateLocation 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)

Example 3 with CrateLocation

use of com.badbones69.crazycrates.api.objects.CrateLocation in project Crazy-Crates by Crazy-Crew.

the class BrokeLocationsControl method onWorldLoad.

@EventHandler
public void onWorldLoad(WorldLoadEvent e) {
    if (!cc.getBrokeCrateLocations().isEmpty()) {
        int fixedAmount = 0;
        List<BrokeLocation> fixedWorlds = new ArrayList<>();
        for (BrokeLocation brokeLocation : cc.getBrokeCrateLocations()) {
            Location location = brokeLocation.getLocation();
            if (location.getWorld() != null) {
                cc.getCrateLocations().add(new CrateLocation(brokeLocation.getLocationName(), brokeLocation.getCrate(), location));
                if (cc.getHologramController() != null) {
                    cc.getHologramController().createHologram(location.getBlock(), brokeLocation.getCrate());
                }
                fixedWorlds.add(brokeLocation);
                fixedAmount++;
            }
        }
        cc.getBrokeCrateLocations().removeAll(fixedWorlds);
        if (fileManager.isLogging()) {
            CrazyManager.getJavaPlugin().getLogger().warning("Fixed " + fixedAmount + " broken crate locations.");
            if (cc.getBrokeCrateLocations().isEmpty()) {
                CrazyManager.getJavaPlugin().getLogger().warning("All broken crate locations have been fixed.");
            }
        }
    }
}
Also used : BrokeLocation(com.badbones69.crazycrates.api.enums.BrokeLocation) CrateLocation(com.badbones69.crazycrates.api.objects.CrateLocation) ArrayList(java.util.ArrayList) Location(org.bukkit.Location) CrateLocation(com.badbones69.crazycrates.api.objects.CrateLocation) BrokeLocation(com.badbones69.crazycrates.api.enums.BrokeLocation) EventHandler(org.bukkit.event.EventHandler)

Aggregations

BrokeLocation (com.badbones69.crazycrates.api.enums.BrokeLocation)2 CrateLocation (com.badbones69.crazycrates.api.objects.CrateLocation)2 Location (org.bukkit.Location)2 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)2 EventHandler (org.bukkit.event.EventHandler)2 ItemStack (org.bukkit.inventory.ItemStack)2 CrateType (com.badbones69.crazycrates.api.enums.CrateType)1 KeyType (com.badbones69.crazycrates.api.enums.KeyType)1 PhysicalCrateKeyCheckEvent (com.badbones69.crazycrates.api.events.PhysicalCrateKeyCheckEvent)1 Crate (com.badbones69.crazycrates.api.objects.Crate)1 QuickCrate (com.badbones69.crazycrates.cratetypes.QuickCrate)1 NMS_Support (com.badbones69.crazycrates.nms.NMS_Support)1 DecentHolograms (com.badbones69.crazycrates.support.holograms.DecentHolograms)1 HolographicSupport (com.badbones69.crazycrates.support.holograms.HolographicSupport)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Sound (org.bukkit.Sound)1 World (org.bukkit.World)1 Block (org.bukkit.block.Block)1 Player (org.bukkit.entity.Player)1