Search in sources :

Example 1 with MinigameTimer

use of au.com.mineauz.minigames.MinigameTimer in project Minigames by AddstarMC.

the class TreasureHuntMechanic method timerExpire.

@EventHandler
private void timerExpire(TimerExpireEvent event) {
    if (event.getMinigame().getType() != MinigameType.GLOBAL && !event.getMinigame().getMechanicName().equals(getMechanic()))
        return;
    Minigame mgm = event.getMinigame();
    TreasureHuntModule thm = TreasureHuntModule.getMinigameModule(mgm);
    if (thm.hasTreasureLocation()) {
        mgm.setMinigameTimer(new MinigameTimer(mgm, thm.getTreasureWaitTime()));
        Location old = thm.getTreasureLocation();
        removeTreasure(mgm);
        if (!thm.isTreasureFound()) {
            MinigameUtils.broadcast(MinigameUtils.formStr("minigame.treasurehunt.plyDespawn", mgm.getName(true)) + "\n" + ChatColor.GRAY + MinigameUtils.formStr("minigame.treasurehunt.plyDespawnCoords", old.getBlockX(), old.getBlockY(), old.getBlockZ()), mgm, "minigame.treasure.announce");
        }
        thm.setTreasureFound(false);
    } else {
        spawnTreasure(mgm);
    }
}
Also used : TreasureHuntModule(au.com.mineauz.minigames.minigame.modules.TreasureHuntModule) MinigameTimer(au.com.mineauz.minigames.MinigameTimer) Minigame(au.com.mineauz.minigames.minigame.Minigame) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 2 with MinigameTimer

use of au.com.mineauz.minigames.MinigameTimer in project Minigames by AddstarMC.

the class TreasureHuntMechanic method spawnTreasure.

public static void spawnTreasure(final Minigame mgm) {
    final TreasureHuntModule thm = TreasureHuntModule.getMinigameModule(mgm);
    if (thm.hasTreasureLocation())
        removeTreasure(mgm);
    if (!thm.getCurrentHints().isEmpty())
        thm.clearHints();
    thm.setTreasureFound(false);
    Location tcpos = mgm.getStartLocations().get(0).clone();
    final Location rpos = tcpos;
    double rx = 0;
    double ry = 0;
    double rz = 0;
    final int maxradius;
    if (thm.getMaxRadius() == 0) {
        maxradius = 1000;
    } else {
        maxradius = thm.getMaxRadius();
    }
    final int maxheight = thm.getMaxHeight();
    Random rand = new Random();
    int rrad = rand.nextInt(maxradius);
    double randCir = 2 * Math.PI * rand.nextInt(360) / 360;
    rx = tcpos.getX() - 0.5 + Math.round(rrad * Math.cos(randCir));
    rz = tcpos.getZ() - 0.5 + Math.round(rrad * Math.sin(randCir));
    ry = tcpos.getY() + rand.nextInt(maxheight);
    rpos.setX(rx);
    rpos.setY(ry);
    rpos.setZ(rz);
    //TODO: Improve so no invalid spawns (Not over void, Strict containment)
    if (rpos.getBlock().getType() == Material.AIR) {
        while (rpos.getBlock().getType() == Material.AIR && rpos.getY() > 1) {
            rpos.setY(rpos.getY() - 1);
        }
        rpos.setY(rpos.getY() + 1);
        Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

            @Override
            public void run() {
                rpos.getBlock().setType(Material.CHEST);
            }
        });
    } else {
        while (rpos.getBlock().getType() != Material.AIR && rpos.getY() < 255) {
            rpos.setY(rpos.getY() + 1);
        }
        Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

            @Override
            public void run() {
                rpos.getBlock().setType(Material.CHEST);
            }
        });
    }
    //Fill new chest
    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

        @Override
        public void run() {
            if (rpos.getBlock().getState() instanceof Chest) {
                final Chest chest = (Chest) rpos.getBlock().getState();
                // TODO: Treasure hunt needs own rewards specification
                RewardsModule rewards = RewardsModule.getModule(mgm);
                if (rewards.getScheme() instanceof StandardRewardScheme) {
                    if (!((StandardRewardScheme) rewards.getScheme()).getPrimaryReward().getRewards().isEmpty()) {
                        int numitems = (int) Math.round(Math.random() * (thm.getMaxTreasure() - thm.getMinTreasure())) + thm.getMinTreasure();
                        final ItemStack[] items = new ItemStack[27];
                        for (int i = 0; i < numitems; i++) {
                            RewardType rew = ((StandardRewardScheme) rewards.getScheme()).getPrimaryReward().getReward().get(0);
                            if (rew instanceof ItemReward) {
                                ItemReward irew = (ItemReward) rew;
                                items[i] = irew.getRewardItem();
                            }
                        }
                        Collections.shuffle(Arrays.asList(items));
                        chest.getInventory().setContents(items);
                    }
                }
            }
        }
    });
    thm.setTreasureLocation(rpos);
    plugin.getLogger().info(MinigameUtils.formStr("minigame.treasurehunt.consSpawn", mgm.getName(false), rpos.getBlockX() + ", " + rpos.getBlockY() + ", " + rpos.getBlockZ()));
    MinigameUtils.broadcast(MinigameUtils.formStr("minigame.treasurehunt.plySpawn", maxradius, thm.getLocation()), mgm, "minigame.treasure.announce");
    mgm.setMinigameTimer(new MinigameTimer(mgm, mgm.getTimer()));
}
Also used : Chest(org.bukkit.block.Chest) ItemReward(au.com.mineauz.minigames.minigame.reward.ItemReward) MinigameTimer(au.com.mineauz.minigames.MinigameTimer) RewardsModule(au.com.mineauz.minigames.minigame.reward.RewardsModule) TreasureHuntModule(au.com.mineauz.minigames.minigame.modules.TreasureHuntModule) Random(java.util.Random) RewardType(au.com.mineauz.minigames.minigame.reward.RewardType) StandardRewardScheme(au.com.mineauz.minigames.minigame.reward.scheme.StandardRewardScheme) Location(org.bukkit.Location)

Aggregations

MinigameTimer (au.com.mineauz.minigames.MinigameTimer)2 TreasureHuntModule (au.com.mineauz.minigames.minigame.modules.TreasureHuntModule)2 Location (org.bukkit.Location)2 Minigame (au.com.mineauz.minigames.minigame.Minigame)1 ItemReward (au.com.mineauz.minigames.minigame.reward.ItemReward)1 RewardType (au.com.mineauz.minigames.minigame.reward.RewardType)1 RewardsModule (au.com.mineauz.minigames.minigame.reward.RewardsModule)1 StandardRewardScheme (au.com.mineauz.minigames.minigame.reward.scheme.StandardRewardScheme)1 Random (java.util.Random)1 Chest (org.bukkit.block.Chest)1 EventHandler (org.bukkit.event.EventHandler)1