Search in sources :

Example 1 with CrateType

use of gg.projecteden.nexus.features.crates.models.CrateType in project Nexus by ProjectEdenGG.

the class Crates method onClickWithKey.

@EventHandler
public void onClickWithKey(PlayerInteractEvent event) {
    try {
        if (!Utils.ActionGroup.CLICK_BLOCK.applies(event))
            return;
        if (event.getClickedBlock() == null)
            return;
        CrateType locationType = CrateType.fromLocation(event.getClickedBlock().getLocation());
        if (locationType == null)
            return;
        event.setCancelled(true);
        Location location = LocationUtils.getCenteredLocation(event.getClickedBlock().getLocation());
        if (event.getHand() == null)
            return;
        if (!event.getHand().equals(EquipmentSlot.HAND))
            return;
        if (!enabled)
            throw new CrateOpeningException("Crates are temporarily disabled");
        if (RebootCommand.isQueued())
            throw new CrateOpeningException("Server reboot is queued, cannot open crates");
        CrateType keyType = CrateType.fromKey(event.getItem());
        if (locationType != keyType && locationType != CrateType.ALL)
            if (Crates.getLootByType(locationType).stream().filter(CrateLoot::isActive).toArray().length == 0)
                throw new CrateOpeningException("&3Coming soon...");
            else
                locationType.previewDrops(null).open(event.getPlayer());
        else if (keyType != null)
            try {
                if (event.getPlayer().isSneaking() && event.getItem().getAmount() > 1)
                    keyType.getCrateClass().openMultiple(location, event.getPlayer(), event.getItem().getAmount());
                else
                    keyType.getCrateClass().openCrate(location, event.getPlayer());
            } catch (CrateOpeningException ex) {
                if (ex.getMessage() != null)
                    PlayerUtils.send(event.getPlayer(), Crates.PREFIX + ex.getMessage());
                keyType.getCrateClass().reset();
            }
    } catch (NexusException ex) {
        PlayerUtils.send(event.getPlayer(), ex.withPrefix(Crates.PREFIX));
    }
}
Also used : CrateType(gg.projecteden.nexus.features.crates.models.CrateType) CrateLoot(gg.projecteden.nexus.features.crates.models.CrateLoot) CrateOpeningException(gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException) Location(org.bukkit.Location) NexusException(gg.projecteden.nexus.framework.exceptions.NexusException) EventHandler(org.bukkit.event.EventHandler)

Aggregations

CrateLoot (gg.projecteden.nexus.features.crates.models.CrateLoot)1 CrateType (gg.projecteden.nexus.features.crates.models.CrateType)1 NexusException (gg.projecteden.nexus.framework.exceptions.NexusException)1 CrateOpeningException (gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException)1 Location (org.bukkit.Location)1 EventHandler (org.bukkit.event.EventHandler)1