use of com.gmail.stefvanschiedev.buildinggame.Main in project buildinggame by stefvanschie.
the class Arena method join.
/**
* Joins the given player this arena, with all messages being send and all items given to the player. It'll teleport
* the player to the {@link #lobby} and will join the first open plot. An ArenaJoinEvent may fire once this method
* is called.
*
* @param player the player to join this arena
* @since 2.1.0
*/
public void join(final Player player) {
final YamlConfiguration config = SettingsManager.getInstance().getConfig();
final YamlConfiguration messages = SettingsManager.getInstance().getMessages();
// check if everything is set up
if (!isSetup()) {
MessageManager.getInstance().send(player, ChatColor.RED + "Your arena isn't setup right, you still need to do this:");
if (lobby == null)
MessageManager.getInstance().send(player, ChatColor.RED + " - The lobby of arena " + getName() + "(/bg setlobby " + getName() + ')');
if (MainSpawnManager.getInstance().getMainSpawn() == null)
MessageManager.getInstance().send(player, ChatColor.RED + " - The main spawn (/bg setmainspawn)");
getPlots().stream().filter(plot -> plot.getBoundary() == null).forEach(plot -> MessageManager.getInstance().send(player, ChatColor.RED + " - The boundary of plot " + plot.getId() + " (/bg setbounds " + getName() + ' ' + plot.getId() + ')'));
getPlots().stream().filter(plot -> plot.getFloor() == null).forEach(plot -> MessageManager.getInstance().send(player, ChatColor.RED + " - The floor of plot " + plot.getId() + " (/bg setfloor " + getName() + ' ' + plot.getId() + ')'));
return;
}
if (ArenaManager.getInstance().getArena(player) != null) {
MessageManager.getInstance().send(player, ChatColor.RED + "You're already in a game");
return;
}
if (!canJoin()) {
MessageManager.getInstance().send(player, messages.getStringList("join.in-game"));
return;
}
if (isFull()) {
MessageManager.getInstance().send(player, ChatColor.RED + "This arena is full");
return;
}
// call event
ArenaJoinEvent event = new ArenaJoinEvent(this, player);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
return;
GamePlayer p = new GamePlayer(player, GamePlayerType.PLAYER);
Plot plot = null;
for (Plot pl : getPlots()) {
if (!pl.isFull()) {
plot = pl;
pl.join(p);
break;
}
}
if (config.getBoolean("scoreboards.main.enable"))
MainScoreboardManager.getInstance().remove(player);
boolean enableLobbyScoreboard = config.getBoolean("scoreboards.lobby.enable");
if (enableLobbyScoreboard && (getState() == GameState.WAITING || getState() == GameState.STARTING)) {
getLobbyScoreboard(plot).show(player);
} else {
getBuildScoreboard(plot).show(player);
}
String name = player.getName();
// add player to red scoreboard of others
getPlots().stream().filter(pl -> !pl.getGamePlayers().contains(p)).forEach(pl -> {
getLobbyScoreboard(pl).getRedTeam().addEntry(name);
getBuildScoreboard(pl).getRedTeam().addEntry(name);
getVoteScoreboard(pl).getRedTeam().addEntry(name);
getWinScoreboard(pl).getRedTeam().addEntry(name);
});
messages.getStringList("join.message").forEach(message -> MessageManager.getInstance().send(player, message.replace("%players%", getPlayers() + "").replace("%max_players%", getMaxPlayers() + "")));
getUsedPlots().stream().flatMap(pl -> pl.getGamePlayers().stream()).forEach(gamePlayer -> {
Player pla = gamePlayer.getPlayer();
messages.getStringList("join.otherPlayers").forEach(message -> MessageManager.getInstance().send(pla, message.replace("%player%", name).replace("%players%", getPlayers() + "").replace("%max_players%", getMaxPlayers() + "")));
});
if (lobby != null && (getState() == GameState.WAITING || getState() == GameState.STARTING)) {
lobby.teleport(player);
} else {
plot.getLocation().teleport(player);
}
if (enableLobbyScoreboard && (getState() == GameState.WAITING || getState() == GameState.STARTING)) {
getUsedPlots().forEach(pl -> pl.getGamePlayers().forEach(gamePlayer -> lobbyScoreboards.get(pl).show(gamePlayer.getPlayer())));
} else {
getUsedPlots().forEach(pl -> pl.getGamePlayers().forEach(gamePlayer -> buildScoreboards.get(pl).show(gamePlayer.getPlayer())));
}
player.getInventory().clear();
player.getInventory().setArmorContents(null);
// fill lives and hunger
player.setHealth(player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
player.setFoodLevel(20);
if (getState() == GameState.WAITING || getState() == GameState.STARTING) {
player.setGameMode(GameMode.ADVENTURE);
} else {
player.setGameMode(GameMode.CREATIVE);
}
// time
if (config.getBoolean("join.time-change.change"))
player.setPlayerTime(config.getInt("join.time-change.time"), false);
// bossbar
getBossBar().addPlayer(player);
// hide players from tab list
if (config.getBoolean("tab-list.adjust"))
Bukkit.getOnlinePlayers().stream().filter(pl -> !contains(pl)).forEach(pl -> player.hidePlayer(Main.getInstance(), pl));
// show current joined player to others
getUsedPlots().stream().flatMap(pl -> pl.getGamePlayers().stream()).forEach(gamePlayer -> gamePlayer.getPlayer().showPlayer(Main.getInstance(), player));
if (getPlayers() >= minPlayers && !lobbyTimer.isActive() && (getState() == GameState.WAITING || getState() == GameState.STARTING)) {
lobbyTimer.runTaskTimer(Main.getInstance(), 0L, 20L);
}
if (getPlayers() >= getMaxPlayers())
lobbyTimer.setSeconds(0);
var arena = this;
// bukkit runnable because of instant leaving and instant subject opening
var runnable = new BukkitRunnable() {
@Override
public void run() {
// give team selection
if (getState() == GameState.WAITING || getState() == GameState.STARTING) {
if (getMode() == ArenaMode.TEAM) {
Material material = SettingsManager.getInstance().getMaterial("team-selection.item.id", Material.BARRIER);
player.getInventory().setItem(0, new ItemBuilder(player, material).setDisplayName(MessageManager.translate(messages.getString("team-gui.item.name"), player)).setLore(MessageManager.translate(messages.getStringList("team-gui.item.lores"), player)).setClickEvent(event -> {
getTeamSelection().show(player);
event.setCancelled(true);
}).build());
}
// give paper for subject
if (player.hasPermission("bg.subjectmenu") && config.getBoolean("enable-subject-voting") && getSubjectMenu().getWhen() == SubjectMenu.When.LOBBY) {
giveSubjectMenuItem(player);
if (getSubjectMenu().opensInstantly()) {
getSubjectMenu().show(player);
}
}
Material material = SettingsManager.getInstance().getMaterial("leave-item.id", Material.BARRIER);
player.getInventory().setItem(config.getInt("leave-item.slot"), new ItemBuilder(player, material).setDisplayName(MessageManager.translate(messages.getString("leave-item.name"), player)).setClickEvent(event -> {
leave(player);
event.setCancelled(true);
}).build());
player.updateInventory();
} else if (getState() == GameState.BUILDING) {
tryGiveOptionsMenu(player);
}
}
/**
* Returns the team selection and creates one if it doesn't exist yet
*
* @return the team selection
* @see TeamSelection
* @since 2.1.0
*/
@NotNull
private TeamSelection getTeamSelection() {
if (teamSelection == null)
teamSelection = new TeamSelection(arena);
return teamSelection;
}
};
runnable.runTaskLater(Main.getInstance(), 1L);
SignManager.getInstance().updateJoinSigns(this);
}
Aggregations