Search in sources :

Example 1 with Tasks

use of gg.projecteden.nexus.utils.Tasks 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)

Aggregations

ParticleBuilder (com.destroystokyo.paper.ParticleBuilder)1 Hologram (com.gmail.filoghost.holographicdisplays.api.Hologram)1 HologramsAPI (com.gmail.filoghost.holographicdisplays.api.HologramsAPI)1 AtomicDouble (com.google.common.util.concurrent.AtomicDouble)1 Nexus (gg.projecteden.nexus.Nexus)1 Chat (gg.projecteden.nexus.features.chat.Chat)1 MuteMenuItem (gg.projecteden.nexus.features.commands.MuteMenuCommand.MuteMenuProvider.MuteMenuItem)1 Crates (gg.projecteden.nexus.features.crates.Crates)1 CrateSpawnItemEvent (gg.projecteden.nexus.features.crates.models.events.CrateSpawnItemEvent)1 MenuUtils (gg.projecteden.nexus.features.menus.MenuUtils)1 CrateOpeningException (gg.projecteden.nexus.framework.exceptions.postconfigured.CrateOpeningException)1 Nickname (gg.projecteden.nexus.models.nickname.Nickname)1 ItemUtils (gg.projecteden.nexus.utils.ItemUtils)1 Nullables.isNullOrAir (gg.projecteden.nexus.utils.Nullables.isNullOrAir)1 PlayerUtils (gg.projecteden.nexus.utils.PlayerUtils)1 RandomUtils (gg.projecteden.nexus.utils.RandomUtils)1 Jingle (gg.projecteden.nexus.utils.SoundUtils.Jingle)1 SoundUtils.getPitch (gg.projecteden.nexus.utils.SoundUtils.getPitch)1 StringUtils (gg.projecteden.nexus.utils.StringUtils)1 Tasks (gg.projecteden.nexus.utils.Tasks)1