use of au.com.mineauz.minigames.events.JoinMinigameEvent in project Minigames by AddstarMC.
the class PlayerData method joinMinigame.
public void joinMinigame(MinigamePlayer player, Minigame minigame, boolean isBetting, Double betAmount) {
MinigameType type = minigame.getType();
JoinMinigameEvent event = new JoinMinigameEvent(player, minigame);
Bukkit.getServer().getPluginManager().callEvent(event);
boolean canStart = minigame.getMechanic().checkCanStart(minigame, player);
if (!event.isCancelled()) {
if ((minigame.isEnabled() || player.getPlayer().hasPermission("minigame.join.disabled")) && (minigame.getState() == MinigameState.IDLE || minigame.getState() == MinigameState.OCCUPIED || minigame.getState() == MinigameState.WAITING || (minigame.getState() == MinigameState.STARTED && minigame.canLateJoin())) && /*!minigame.isRegenerating() &&
(!minigame.isNotWaitingForPlayers() || (minigame.canLateJoin() && minigame.getMpTimer().getPlayerWaitTimeLeft() == 0)) &&*/
(minigame.getStartLocations().size() > 0 || (minigame.isTeamGame() && TeamsModule.getMinigameModule(minigame).hasTeamStartLocations())) && minigame.getEndPosition() != null && minigame.getQuitPosition() != null && (minigame.getType() == MinigameType.SINGLEPLAYER || minigame.getLobbyPosition() != null) && ((type == MinigameType.SINGLEPLAYER && !minigame.isSpMaxPlayers()) || minigame.getPlayers().size() < minigame.getMaxPlayers()) && minigame.getMechanic().validTypes().contains(minigame.getType()) && canStart) {
//Do betting stuff
if (isBetting) {
if (minigame.getMpBets() == null && (player.getPlayer().getInventory().getItemInMainHand().getType() != Material.AIR || betAmount != 0)) {
minigame.setMpBets(new MultiplayerBets());
}
MultiplayerBets pbet = minigame.getMpBets();
ItemStack item = player.getPlayer().getInventory().getItemInMainHand().clone();
if (pbet != null && ((betAmount != 0 && pbet.canBet(player, betAmount) && plugin.getEconomy().getBalance(player.getPlayer().getPlayer()) >= betAmount) || (pbet.canBet(player, item) && item.getType() != Material.AIR && pbet.betValue(item.getType()) > 0))) {
player.sendMessage(MinigameUtils.getLang("player.bet.plyMsg"), null);
if (betAmount == 0) {
pbet.addBet(player, item);
} else {
pbet.addBet(player, betAmount);
plugin.getEconomy().withdrawPlayer(player.getPlayer().getPlayer(), betAmount);
}
player.getPlayer().getInventory().removeItem(new ItemStack(item.getType(), 1));
} else if (item.getType() == Material.AIR && betAmount == 0) {
player.sendMessage(MinigameUtils.getLang("player.bet.plyNoBet"), "error");
return;
} else if (betAmount != 0 && !pbet.canBet(player, betAmount)) {
player.sendMessage(MinigameUtils.getLang("player.bet.incorrectAmount"), "error");
player.sendMessage(MinigameUtils.formStr("player.bet.incorrectAmountInfo", Minigames.plugin.getEconomy().format(minigame.getMpBets().getHighestMoneyBet())), "error");
return;
} else if (betAmount != 0 && plugin.getEconomy().getBalance(player.getPlayer().getPlayer()) < betAmount) {
player.sendMessage(MinigameUtils.getLang("player.bet.notEnoughMoney"), "error");
player.sendMessage(MinigameUtils.formStr("player.bet.notEnoughMoneyInfo", Minigames.plugin.getEconomy().format(minigame.getMpBets().getHighestMoneyBet())), "error");
return;
} else {
player.sendMessage(MinigameUtils.getLang("player.bet.incorrectItem"), "error");
player.sendMessage(MinigameUtils.formStr("player.bet.incorrectItemInfo", 1, minigame.getMpBets().highestBetName()), "error");
return;
}
}
//Try teleport the player to their designated area.
boolean tpd = false;
if (type == MinigameType.SINGLEPLAYER) {
List<Location> locs = new ArrayList<Location>(minigame.getStartLocations());
Collections.shuffle(locs);
tpd = player.teleport(locs.get(0));
if (plugin.getConfig().getBoolean("warnings") && player.getPlayer().getWorld() != locs.get(0).getWorld() && player.getPlayer().hasPermission("minigame.set.start")) {
player.sendMessage(ChatColor.RED + "WARNING: " + ChatColor.WHITE + "Join location is across worlds! This may cause some server performance issues!", "error");
}
} else {
tpd = player.teleport(minigame.getLobbyPosition());
if (plugin.getConfig().getBoolean("warnings") && player.getPlayer().getWorld() != minigame.getLobbyPosition().getWorld() && player.getPlayer().hasPermission("minigame.set.lobby")) {
player.sendMessage(ChatColor.RED + "WARNING: " + ChatColor.WHITE + "Lobby location is across worlds! This may cause some server performance issues!", "error");
}
}
if (!tpd) {
player.sendMessage(MinigameUtils.getLang("minigame.error.noTeleport"), "error");
return;
}
//Give them the game type name
if (minigame.getGametypeName() == null)
player.sendMessage(MinigameUtils.formStr("player.join.plyInfo", minigame.getType().getName()), "win");
else
player.sendMessage(MinigameUtils.formStr("player.join.plyInfo", minigame.getGametypeName()), "win");
//Give them the objective
if (minigame.getObjective() != null) {
player.sendMessage(ChatColor.GREEN + "----------------------------------------------------");
player.sendMessage(ChatColor.AQUA.toString() + ChatColor.BOLD + MinigameUtils.formStr("player.join.objective", ChatColor.RESET.toString() + ChatColor.WHITE + minigame.getObjective()));
player.sendMessage(ChatColor.GREEN + "----------------------------------------------------");
}
//Prepare regeneration region for rollback.
if (minigame.getBlockRecorder().hasRegenArea() && !minigame.getBlockRecorder().hasCreatedRegenBlocks()) {
RecorderData d = minigame.getBlockRecorder();
d.setCreatedRegenBlocks(true);
Location cur = new Location(minigame.getRegenArea1().getWorld(), 0, 0, 0);
for (double y = d.getRegenMinY(); y <= d.getRegenMaxY(); y++) {
cur.setY(y);
for (double x = d.getRegenMinX(); x <= d.getRegenMaxX(); x++) {
cur.setX(x);
for (double z = d.getRegenMinZ(); z <= d.getRegenMaxZ(); z++) {
cur.setZ(z);
d.addBlock(cur.getBlock(), null);
}
}
}
}
//Standardize player
player.storePlayerData();
player.setMinigame(minigame);
minigame.addPlayer(player);
WeatherTimeModule.getMinigameModule(minigame).applyCustomTime(player);
WeatherTimeModule.getMinigameModule(minigame).applyCustomWeather(player);
player.setCheckpoint(player.getPlayer().getLocation());
player.getPlayer().setFallDistance(0);
player.getPlayer().setWalkSpeed(0.2f);
player.setStartTime(Calendar.getInstance().getTimeInMillis());
player.setGamemode(minigame.getDefaultGamemode());
if (minigame.getType() == MinigameType.SINGLEPLAYER) {
if (!minigame.isAllowedFlight()) {
player.setCanFly(false);
} else {
player.setCanFly(true);
if (minigame.isFlightEnabled())
player.getPlayer().setFlying(true);
}
} else {
player.getPlayer().setAllowFlight(false);
}
for (PotionEffect potion : player.getPlayer().getActivePotionEffects()) {
player.getPlayer().removePotionEffect(potion.getType());
}
//Hide Spectators
for (MinigamePlayer pl : minigame.getSpectators()) {
player.getPlayer().hidePlayer(pl.getPlayer());
}
if (minigame.getPlayers().size() == 1) {
//Register regen recorder events
if (minigame.getBlockRecorder().hasRegenArea())
Bukkit.getServer().getPluginManager().registerEvents(minigame.getBlockRecorder(), plugin);
WeatherTimeModule.getMinigameModule(minigame).startTimeLoop();
}
//Call Type specific join
mdata.minigameType(type).joinMinigame(player, minigame);
//Call Mechanic specific join
minigame.getMechanic().joinMinigame(minigame, player);
//Send other players the join message.
mdata.sendMinigameMessage(minigame, MinigameUtils.formStr("player.join.plyMsg", player.getName(), minigame.getName(true)), null, player);
player.updateInventory();
if (minigame.canDisplayScoreboard()) {
player.getPlayer().setScoreboard(minigame.getScoreboardManager());
minigame.setScore(player, 1);
minigame.setScore(player, 0);
}
} else if (!minigame.isEnabled()) {
player.sendMessage(MinigameUtils.getLang("minigame.error.notEnabled"), "error");
} else if (minigame.getState() == MinigameState.REGENERATING) {
player.sendMessage(MinigameUtils.getLang("minigame.error.regenerating"), "error");
} else if (minigame.getState() == MinigameState.STARTED && !minigame.canLateJoin()) {
player.sendMessage(MinigameUtils.getLang("minigame.started"), "error");
} else if (minigame.getState() == MinigameState.STARTING && minigame.canLateJoin() && minigame.getPlayers().size() != minigame.getMaxPlayers()) {
player.sendMessage(MinigameUtils.formStr("minigame.lateJoinWait", minigame.getMpTimer().getStartWaitTimeLeft()), null);
} else if (minigame.getStartLocations().size() == 0 || (minigame.isTeamGame() && !TeamsModule.getMinigameModule(minigame).hasTeamStartLocations())) {
player.sendMessage(MinigameUtils.getLang("minigame.error.noStart"), "error");
} else if (minigame.getEndPosition() == null) {
player.sendMessage(MinigameUtils.getLang("minigame.error.noEnd"), "error");
} else if (minigame.getQuitPosition() == null) {
player.sendMessage(MinigameUtils.getLang("minigame.error.noQuit"), "error");
} else if (minigame.getLobbyPosition() == null) {
player.sendMessage(MinigameUtils.getLang("minigame.error.noLobby"), "error");
} else if (minigame.getPlayers().size() >= minigame.getMaxPlayers()) {
player.sendMessage(MinigameUtils.getLang("minigame.full"), "error");
} else if (!minigame.getMechanic().validTypes().contains(minigame.getType())) {
player.sendMessage(MinigameUtils.getLang("minigame.error.invalidMechanic"), "error");
} else if (!canStart) {
player.sendMessage(MinigameUtils.getLang("minigame.error.mechanicStartFail"), "error");
}
}
}
Aggregations