use of com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer 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);
}
use of com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer in project buildinggame by stefvanschie.
the class Arena method setVotingPlot.
/**
* Sets the new voting plot, send messages and titles, teleports all players, gives them new vote items and changes
* their time and weather.
*
* @param votingPlot the new voting plot
* @see Plot
* @since 2.1.0
*/
public void setVotingPlot(Plot votingPlot) {
SettingsManager instance = SettingsManager.getInstance();
YamlConfiguration config = instance.getConfig();
YamlConfiguration messages = instance.getMessages();
this.votingPlot = votingPlot;
Region boundary = votingPlot.getBoundary();
Location safeLocation = boundary.getSafeLocation();
Location center = boundary.getCenter();
// https://bukkit.org/threads/lookat-and-move-functions.26768/
if (safeLocation != null) {
// Clone the loc to prevent applied changes to the input loc
safeLocation = safeLocation.clone();
// Values of change in distance (make it relative)
double dx = center.getX() - safeLocation.getX();
double dz = center.getZ() - safeLocation.getZ();
// Set yaw
if (dx != 0)
safeLocation.setYaw((dx < 0 ? ((float) (1.5 * Math.PI)) : ((float) (0.5 * Math.PI))) - (float) Math.atan(dz / dx));
else if (dz < 0)
safeLocation.setYaw((float) Math.PI);
// Set pitch
safeLocation.setPitch((float) -Math.atan((center.getY() - safeLocation.getY()) / (Math.sqrt(Math.pow(dx, 2) + Math.pow(dz, 2)))));
// Set values, convert to degrees (invert the yaw since Bukkit uses a different yaw dimension format)
safeLocation.setYaw(-safeLocation.getYaw() * 180f / (float) Math.PI);
safeLocation.setPitch(safeLocation.getPitch() * 180f / (float) Math.PI);
}
for (Plot plot : getUsedPlots()) {
for (GamePlayer gamePlayer : plot.getAllGamePlayers()) {
Player player = gamePlayer.getPlayer();
if (!config.getBoolean("names-after-voting")) {
messages.getStringList("voting.message").forEach(message -> MessageManager.getInstance().send(player, message.replace("%playerplot%", votingPlot.getPlayerFormat())));
gamePlayer.addTitleAndSubtitle(messages.getString("voting.title").replace("%playerplot%", votingPlot.getPlayerFormat()), messages.getString("voting.subtitle").replace("%playerplot%", votingPlot.getPlayerFormat()));
gamePlayer.sendActionbar(messages.getString("voting.actionbar").replace("%playerplot%", votingPlot.getPlayerFormat()));
}
int blockIndex = ThreadLocalRandom.current().nextInt(votingPlot.getBoundary().getAllBlocks().size());
player.teleport(Objects.requireNonNullElseGet(safeLocation, () -> boundary.getAllBlocks().get(blockIndex).getLocation()));
// give blocks
player.getInventory().clear();
if (gamePlayer.getGamePlayerType() == GamePlayerType.PLAYER) {
config.getConfigurationSection("voting.items").getKeys(false).forEach(identifier -> {
boolean save = false;
if (!messages.contains("voting.items." + identifier + ".name")) {
messages.set("voting.items." + identifier + ".name", "Type: Null");
save = true;
}
if (!messages.contains("voting.items." + identifier + ".lore")) {
messages.set("voting.items." + identifier + ".lore", new ArrayList<String>(0));
save = true;
}
if (save)
instance.save();
Material material = SettingsManager.getInstance().getMaterial("voting.items." + identifier + ".id", Material.BARRIER);
player.getInventory().setItem(config.getInt("voting.items." + identifier + ".slot") - 1, new ItemBuilder(player, material).setDisplayName(MessageManager.translate(messages.getString("voting.items." + identifier + ".name"))).setLore(MessageManager.translate(messages.getStringList("voting.items." + identifier + ".lore"))).movable(false).setClickEvent(event -> {
int points = config.getInt("voting.items." + identifier + ".points");
votingPlot.addVote(new Vote(points, player));
event.setCancelled(true);
}).build());
});
boolean worldEditEnabled = Bukkit.getPluginManager().isPluginEnabled("WorldEdit");
if (worldEditEnabled && config.getBoolean("reports.enable")) {
player.getInventory().setItem(8, new ItemBuilder(player, Material.BOOK).setDisplayName(ChatColor.RED + "Report build").movable(false).setClickEvent(event -> {
var dateTimeFormatter = DateTimeFormatter.ofPattern("yyy-MM-dd_HH-mm-ss");
String players = getVotingPlot().getGamePlayers().stream().map(gp -> '-' + gp.getPlayer().getName()).reduce("", String::concat);
var fileName = LocalDateTime.now().format(dateTimeFormatter) + players + ".schem";
var file = new File(instance.getReportsSchematicsFolder(), fileName);
getVotingPlot().getBoundary().saveSchematic(file, () -> MessageManager.getInstance().send(event.getPlayer(), ChatColor.GREEN + "Your report has been saved."));
getVotingPlot().getGamePlayers().forEach(gp -> Report.add(new Report(gp.getPlayer(), player, ZonedDateTime.now(), file)));
}).build());
}
}
// update scoreboard and update time and weather
if (config.getBoolean("scoreboards.vote.enable"))
getVoteScoreboard(plot).show(player);
player.setPlayerTime(plot.getTime(), false);
player.setPlayerWeather(plot.isRaining() ? WeatherType.DOWNFALL : WeatherType.CLEAR);
}
}
getVotedPlots().add(votingPlot);
}
use of com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer in project buildinggame by stefvanschie.
the class Arena method postStart.
/**
* Starts a new match. An ArenaStartEvent will be fired once this method is called.
*
* @since 2.1.0
*/
private void postStart() {
YamlConfiguration config = SettingsManager.getInstance().getConfig();
YamlConfiguration messages = SettingsManager.getInstance().getMessages();
// call event
ArenaStartEvent event = new ArenaStartEvent(this);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
return;
subject = getSubjectMenu().getHighestVote();
// update bossbar
getBossBar().setTitle(MessageManager.translate(messages.getString("global.bossbar-header").replace("%subject%", getSubject())));
getUsedPlots().forEach(plot -> plot.getGamePlayers().forEach(gamePlayer -> {
plot.getLocation().teleport(gamePlayer.getPlayer());
MessageManager.getInstance().send(gamePlayer.getPlayer(), messages.getStringList("gameStarts.message"));
messages.getStringList("gameStarts.subject").forEach(message -> MessageManager.getInstance().send(gamePlayer.getPlayer(), message.replace("%subject%", getSubject())));
gamePlayer.addTitleAndSubtitle(messages.getString("gameStarts.title").replace("%subject%", getSubject()), messages.getString("gameStarts.subtitle").replace("%subject%", getSubject()));
gamePlayer.sendActionbar(messages.getString("gameStarts.actionbar").replace("%subject%", subject));
final var player = gamePlayer.getPlayer();
player.getInventory().clear();
player.setGameMode(GameMode.CREATIVE);
player.setPlayerTime(plot.getTime(), false);
// hotbar
for (int i = 0; i < 9; i++) {
Material material = SettingsManager.getInstance().getMaterial("hotbar.default.slot-" + (i + 1), Material.AIR);
player.getInventory().setItem(i, new ItemStack(material));
}
// bossbar
getBossBar().setVisible(true);
tryGiveOptionsMenu(player);
}));
setState(GameState.BUILDING);
// save blocks
getPlots().forEach(Plot::save);
matches++;
SignManager.getInstance().updateJoinSigns(this);
buildTimer.runTaskTimer(Main.getInstance(), 20L, 20L);
}
Aggregations