use of au.com.mineauz.minigames.minigame.reward.RewardsModule 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;
double ry;
double rz;
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)
switch(rpos.getBlock().getType()) {
case AIR:
case CAVE_AIR:
case VOID_AIR:
while (rpos.getBlock().getType() == Material.AIR && rpos.getY() > 1) {
rpos.setY(rpos.getY() - 1);
}
rpos.setY(rpos.getY() + 1);
Bukkit.getScheduler().runTaskLater(plugin, () -> rpos.getBlock().setType(Material.CHEST), 1L);
break;
default:
while (rpos.getBlock().getType() != Material.AIR && rpos.getY() < 255) {
rpos.setY(rpos.getY() + 1);
}
Bukkit.getScheduler().runTaskLater(plugin, () -> rpos.getBlock().setType(Material.CHEST), 1L);
break;
}
// Fill new chest
Bukkit.getScheduler().runTaskLater(plugin, () -> {
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);
}
}
}
}, 0L);
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()));
}
Aggregations