Search in sources :

Example 1 with CrateOpeningException

use of gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException in project Nexus by ProjectEdenGG.

the class Crate method pickCrateLoot.

public void pickCrateLoot() {
    Map<CrateLoot, Double> original = new HashMap<>();
    Crates.getLootByType(getCrateType()).stream().filter(CrateLoot::isActive).forEach(crateLoot -> original.put(crateLoot, crateLoot.getWeight()));
    if (original.size() == 0)
        throw new CrateOpeningException("&3Coming soon...");
    loot = RandomUtils.getWeightedRandom(original);
}
Also used : HashMap(java.util.HashMap) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) CrateOpeningException(gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException)

Example 2 with CrateOpeningException

use of gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException in project Nexus by ProjectEdenGG.

the class Crate method spawnItem.

public Item spawnItem(Location location, ItemStack itemStack) {
    try {
        Item item = location.getWorld().dropItem(location, itemStack);
        item.setVelocity(new Vector(0, 0, 0));
        item.setCanPlayerPickup(false);
        item.setCustomNameVisible(true);
        item.setCustomName(StringUtils.colorize(loot.getTitle()));
        spawnedItem = item;
        new CrateSpawnItemEvent(player, loot, getCrateType()).callEvent();
        return item;
    } catch (Exception ex) {
        player.getInventory().addItem(getCrateType().getKey());
        throw new CrateOpeningException("There was an error while trying to play the crate animation");
    }
}
Also used : Item(org.bukkit.entity.Item) MuteMenuItem(gg.projecteden.nexus.features.commands.MuteMenuCommand.MuteMenuProvider.MuteMenuItem) Vector(org.bukkit.util.Vector) CrateSpawnItemEvent(gg.projecteden.nexus.features.crates.models.events.CrateSpawnItemEvent) CrateOpeningException(gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException) CrateOpeningException(gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException)

Example 3 with CrateOpeningException

use of gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException in project Nexus by ProjectEdenGG.

the class Crate method openMultiple.

public void openMultiple(Location location, Player player, int amount) {
    if (inUse)
        return;
    this.player = player;
    MenuUtils.ConfirmationMenu.builder().title("Open " + amount + " Crates?").onConfirm(e -> {
        player.closeInventory();
        try {
            if (inUse)
                return;
            inUse = true;
            pickCrateLoot();
            if (!canHoldItems(player))
                return;
            takeKey();
            hideHologram();
            playAnimationSound(location);
            playAnimation(location).thenAccept(finalLocation -> {
                try {
                    AtomicInteger wait = new AtomicInteger(0);
                    Tasks.wait(TickTime.SECOND.x(wait.getAndAdd(1)), () -> {
                        try {
                            playFinalSound(location);
                            playFinalParticle(finalLocation);
                            spawnItem(finalLocation, loot.getDisplayItem());
                        } catch (CrateOpeningException ex) {
                            if (ex.getMessage() != null)
                                PlayerUtils.send(player, Crates.PREFIX + ex.getMessage());
                            reset();
                        }
                    });
                    List<Integer> tasks = new ArrayList<>();
                    for (int i = 0; i < amount - 1; i++) {
                        int j = i;
                        tasks.add(Tasks.wait(TickTime.SECOND.x(wait.getAndAdd(1)), () -> {
                            try {
                                giveItems();
                                removeItem();
                                pickCrateLoot();
                                if (!canHoldItems(player)) {
                                    tasks.forEach(Tasks::cancel);
                                    return;
                                }
                                takeKey();
                                playFinalSound(location);
                                playFinalParticle(finalLocation);
                                spawnItem(finalLocation, loot.getDisplayItem());
                                if (j == amount - 2)
                                    Tasks.wait(TickTime.SECOND.x(3), () -> {
                                        giveItems();
                                        reset();
                                    });
                            } catch (CrateOpeningException ex) {
                                if (ex.getMessage() != null)
                                    PlayerUtils.send(player, Crates.PREFIX + ex.getMessage());
                                tasks.forEach(Tasks::cancel);
                                reset();
                            }
                        }));
                    }
                } catch (CrateOpeningException ex) {
                    if (ex.getMessage() != null)
                        PlayerUtils.send(player, Crates.PREFIX + ex.getMessage());
                    reset();
                }
            });
        } catch (CrateOpeningException ex) {
            if (ex.getMessage() != null)
                PlayerUtils.send(player, Crates.PREFIX + ex.getMessage());
            reset();
        }
    }).open(player);
}
Also used : Hologram(com.gmail.filoghost.holographicdisplays.api.Hologram) Arrays(java.util.Arrays) StringUtils(gg.projecteden.nexus.utils.StringUtils) Item(org.bukkit.entity.Item) ParticleBuilder(com.destroystokyo.paper.ParticleBuilder) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Player(org.bukkit.entity.Player) CrateOpeningException(gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) PlayerUtils(gg.projecteden.nexus.utils.PlayerUtils) Location(org.bukkit.Location) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MuteMenuItem(gg.projecteden.nexus.features.commands.MuteMenuCommand.MuteMenuProvider.MuteMenuItem) RandomUtils(gg.projecteden.nexus.utils.RandomUtils) World(org.bukkit.World) Map(java.util.Map) Crates(gg.projecteden.nexus.features.crates.Crates) Nickname(gg.projecteden.nexus.models.nickname.Nickname) Color(org.bukkit.Color) Material(org.bukkit.Material) Listener(org.bukkit.event.Listener) Nexus(gg.projecteden.nexus.Nexus) HologramsAPI(com.gmail.filoghost.holographicdisplays.api.HologramsAPI) Tasks(gg.projecteden.nexus.utils.Tasks) Sound(org.bukkit.Sound) ItemUtils(gg.projecteden.nexus.utils.ItemUtils) Collectors(java.util.stream.Collectors) CrateSpawnItemEvent(gg.projecteden.nexus.features.crates.models.events.CrateSpawnItemEvent) ItemStack(org.bukkit.inventory.ItemStack) SoundUtils.getPitch(gg.projecteden.nexus.utils.SoundUtils.getPitch) Particle(org.bukkit.Particle) Vector(org.bukkit.util.Vector) TickTime(gg.projecteden.utils.TimeUtils.TickTime) List(java.util.List) Jingle(gg.projecteden.nexus.utils.SoundUtils.Jingle) Chat(gg.projecteden.nexus.features.chat.Chat) MenuUtils(gg.projecteden.nexus.features.menus.MenuUtils) Data(lombok.Data) Nullables.isNullOrAir(gg.projecteden.nexus.utils.Nullables.isNullOrAir) Tasks(gg.projecteden.nexus.utils.Tasks) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) CrateOpeningException(gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException)

Example 4 with CrateOpeningException

use of gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException in project Nexus by ProjectEdenGG.

the class FebVoteRewardCrate method spawnItem.

@Override
public Item spawnItem(Location location, ItemStack itemStack) {
    try {
        Item item = location.getWorld().spawn(location, Item.class);
        item.setVelocity(new Vector(0, 0, 0));
        item.setItemStack(itemStack);
        item.setCanPlayerPickup(false);
        item.setCustomName(itemStack.getItemMeta().getDisplayName());
        item.setCustomNameVisible(true);
        spawnedItem = item;
        new CrateSpawnItemEvent(player, loot, getCrateType()).callEvent();
        return item;
    } catch (Exception ex) {
        player.getInventory().addItem(getCrateType().getKey());
        throw new CrateOpeningException("There was an error while trying to play the crate animation");
    }
}
Also used : Item(org.bukkit.entity.Item) Vector(org.bukkit.util.Vector) CrateSpawnItemEvent(gg.projecteden.nexus.features.crates.models.events.CrateSpawnItemEvent) CrateOpeningException(gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException) CrateOpeningException(gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException)

Example 5 with CrateOpeningException

use of gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException 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

CrateOpeningException (gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException)6 CrateSpawnItemEvent (gg.projecteden.nexus.features.crates.models.events.CrateSpawnItemEvent)3 Item (org.bukkit.entity.Item)3 Vector (org.bukkit.util.Vector)3 AtomicDouble (com.google.common.util.concurrent.AtomicDouble)2 MuteMenuItem (gg.projecteden.nexus.features.commands.MuteMenuCommand.MuteMenuProvider.MuteMenuItem)2 HashMap (java.util.HashMap)2 Location (org.bukkit.Location)2 ParticleBuilder (com.destroystokyo.paper.ParticleBuilder)1 Hologram (com.gmail.filoghost.holographicdisplays.api.Hologram)1 HologramsAPI (com.gmail.filoghost.holographicdisplays.api.HologramsAPI)1 Nexus (gg.projecteden.nexus.Nexus)1 Chat (gg.projecteden.nexus.features.chat.Chat)1 Crates (gg.projecteden.nexus.features.crates.Crates)1 CrateLoot (gg.projecteden.nexus.features.crates.models.CrateLoot)1 CrateType (gg.projecteden.nexus.features.crates.models.CrateType)1 MenuUtils (gg.projecteden.nexus.features.menus.MenuUtils)1 NexusException (gg.projecteden.nexus.framework.exceptions.NexusException)1 Nickname (gg.projecteden.nexus.models.nickname.Nickname)1 ItemUtils (gg.projecteden.nexus.utils.ItemUtils)1