Search in sources :

Example 11 with TreasureHuntModule

use of au.com.mineauz.minigames.minigame.modules.TreasureHuntModule in project Minigames by AddstarMC.

the class TreasureHuntMechanic method stopMinigame.

@Override
public void stopMinigame(Minigame minigame, MinigamePlayer caller) {
    TreasureHuntModule thm = TreasureHuntModule.getMinigameModule(minigame);
    minigame.getMinigameTimer().stopTimer();
    minigame.setMinigameTimer(null);
    thm.clearHints();
    if (thm.hasTreasureLocation()) {
        removeTreasure(minigame);
        if (!thm.isTreasureFound()) {
            MinigameUtils.broadcast(MinigameUtils.formStr("minigame.treasurehunt.plyRemoved", minigame.getName(true)), minigame, "minigame.treasure.announce");
        }
    }
}
Also used : TreasureHuntModule(au.com.mineauz.minigames.minigame.modules.TreasureHuntModule)

Example 12 with TreasureHuntModule

use of au.com.mineauz.minigames.minigame.modules.TreasureHuntModule 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

TreasureHuntModule (au.com.mineauz.minigames.minigame.modules.TreasureHuntModule)12 Minigame (au.com.mineauz.minigames.minigame.Minigame)4 Location (org.bukkit.Location)4 Chest (org.bukkit.block.Chest)3 EventHandler (org.bukkit.event.EventHandler)3 MinigameTimer (au.com.mineauz.minigames.MinigameTimer)2 MinigamePlayer (au.com.mineauz.minigames.MinigamePlayer)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 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 Chunk (org.bukkit.Chunk)1 Block (org.bukkit.block.Block)1