use of com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer in project buildinggame by stefvanschie.
the class PlaceIgnoreSpectators method onPlayerInteract.
/**
* Handles players trying to place a block where a spectator is in the way
*
* @param e the event used for this algorithm
*/
@EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
var player = e.getPlayer();
var arena = ArenaManager.getInstance().getArena(player);
Block clickedBlock = e.getClickedBlock();
// check if the player is actually playing and the clicked entity is a player
if (arena == null || clickedBlock == null)
return;
boolean nearbySpectator = false;
var relativeBlock = clickedBlock.getRelative(e.getBlockFace());
var blockLocation = relativeBlock.getLocation();
// check if there's a spectator nearby
for (GamePlayer spectator : arena.getPlot(player).getSpectators()) {
var location = spectator.getPlayer().getLocation();
// check if the locations are in the same world before measuring the distance
if (!location.getWorld().equals(blockLocation.getWorld()))
continue;
// this case, as the result will be the same (the player won't place the block, but we'll do it for them)
if (location.distanceSquared(blockLocation) <= 1.8)
nearbySpectator = true;
}
if (!nearbySpectator)
return;
PlayerInventory inventory = player.getInventory();
// set block
relativeBlock.setType((e.getHand() == EquipmentSlot.HAND ? inventory.getItemInMainHand() : inventory.getItemInOffHand()).getType());
}
use of com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer in project buildinggame by stefvanschie.
the class Plot method addVote.
/**
* Adds a vote to this plot
*
* @param vote the vote to be added
* @since 2.1.0
*/
@Contract("null -> fail")
public void addVote(Vote vote) {
YamlConfiguration config = SettingsManager.getInstance().getConfig();
YamlConfiguration messages = SettingsManager.getInstance().getMessages();
if (arena.getState() != GameState.VOTING)
return;
Player sender = vote.getSender();
// check if player votes on his/her own plot
for (GamePlayer gamePlayer : getGamePlayers()) {
if (gamePlayer.getPlayer().equals(sender)) {
MessageManager.getInstance().send(sender, messages.getStringList("vote.own-plot"));
return;
}
}
// check how many times voted
if (getTimesVoted(sender) == config.getInt("max-vote-change")) {
messages.getStringList("vote.maximum-votes").forEach(message -> MessageManager.getInstance().send(sender, message.replace("%max_votes%", config.getInt("max-votes-change") + "")));
return;
}
getTimesVoted().put(sender, getTimesVoted(sender) + 1);
messages.getStringList("vote.message").forEach(message -> MessageManager.getInstance().send(sender, message.replace("%playerplot%", arena.getVotingPlot().getPlayerFormat()).replace("%points%", vote.getPoints() + "")));
messages.getStringList("vote.receiver").forEach(message -> arena.getVotingPlot().getGamePlayers().forEach(player -> MessageManager.getInstance().send(player.getPlayer(), message.replace("%points%", vote.getPoints() + "").replace("%sender%", sender.getName()))));
var senderArena = ArenaManager.getInstance().getArena(sender);
if (senderArena != null)
senderArena.getPlot(sender).getGamePlayers().forEach(player -> {
player.addTitleAndSubtitle(messages.getString("vote.title").replace("%points%", vote.getPoints() + ""), messages.getString("vote.subtitle").replace("%points%", vote.getPoints() + ""));
player.sendActionbar(messages.getString("vote.actionbar").replace("%points%", String.valueOf(vote.getPoints())));
});
int previousPoints = getPoints();
if (hasVoted(sender))
getVotes().remove(getVote(sender));
votes.add(vote);
if (!config.getBoolean("scoreboards.vote.text"))
arena.getVoteScoreboard(this).setScore(getPlayerFormat(), getPoints());
if (!config.getBoolean("names-after-voting") && config.getBoolean("scoreboards.vote.enable"))
arena.getPlots().stream().filter(p -> !p.getGamePlayers().isEmpty()).flatMap(p -> getGamePlayers().stream()).forEach(player -> arena.getVoteScoreboard(this).show(player.getPlayer()));
// point actions
var configurationSection = config.getConfigurationSection("voting.point-actions");
configurationSection.getKeys(false).forEach(key -> {
int points;
try {
points = Integer.parseInt(key);
} catch (NumberFormatException e) {
if (config.getBoolean("debug"))
Main.getInstance().getLogger().warning("Unsupported value found in config.yml in voting > point-actions > " + key);
return;
}
// of points made the amount go over the minimum, it shouldn't already have been higher than the minimum)
if (getPoints() < points || previousPoints >= points)
return;
configurationSection.getStringList(key).forEach(command -> {
if (!command.isEmpty() && command.charAt(0) == '@') {
String targetText = command.split(" ")[0];
Target.parse(targetText).execute(command.substring(targetText.length() + 1));
} else
getGamePlayers().forEach(gamePlayer -> Bukkit.dispatchCommand(gamePlayer.getPlayer(), command));
});
});
// track stats
var statManager = StatManager.getInstance();
getGamePlayers().forEach(gamePlayer -> {
var player = gamePlayer.getPlayer();
var stat = statManager.getStat(player, StatType.POINTS_RECEIVED);
statManager.registerStat(player, StatType.POINTS_RECEIVED, (stat == null ? 0 : stat.getValue()) + vote.getPoints());
});
var stat = statManager.getStat(sender, StatType.POINTS_GIVEN);
statManager.registerStat(sender, StatType.POINTS_GIVEN, (stat == null ? 0 : stat.getValue()) + vote.getPoints());
}
use of com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer in project buildinggame by stefvanschie.
the class Plot method addSpectator.
/**
* Adds a spectator to the plot
*
* @param spectator the player that wants to spectate
* @param spectates the player the spectator wants to spectate
* @since 2.1.0
*/
@Contract("null, _ -> fail; _, null -> fail")
public void addSpectator(final Player spectator, GamePlayer spectates) {
YamlConfiguration config = SettingsManager.getInstance().getConfig();
final var gamePlayer = new GamePlayer(spectator, GamePlayerType.SPECTATOR);
gamePlayer.setSpectates(spectates);
getAllGamePlayers().add(gamePlayer);
getAllGamePlayers().forEach(player -> player.getPlayer().hidePlayer(Main.getInstance(), spectator));
Material material = SettingsManager.getInstance().getMaterial("leave-item.id", Material.BARRIER);
spectator.getInventory().setItem(config.getInt("leave-item.slot"), new ItemBuilder(spectator, material).setDisplayName(MessageManager.translate(SettingsManager.getInstance().getMessages().getString("leave-item.name"), spectator)).setClickEvent(event -> {
gamePlayer.connect(MainSpawnManager.getInstance().getServer(), MainSpawnManager.getInstance().getMainSpawn());
removeSpectator(gamePlayer);
MessageManager.getInstance().send(spectator, ChatColor.GREEN + "Stopped spectating");
event.setCancelled(true);
}).build());
spectator.getInventory().setItem(8, new ItemBuilder(spectator, Material.EMERALD).setDisplayName(ChatColor.GREEN + "Spectator menu").setClickEvent(event -> {
new SpectatorMenu().show(spectator);
event.setCancelled(true);
}).build());
spectator.teleport(spectates.getPlayer().getLocation());
spectator.setGameMode(GameMode.CREATIVE);
spectator.setCanPickupItems(false);
}
use of com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer in project buildinggame by stefvanschie.
the class Plot method removeSpectator.
/**
* Removes the specified spectator from this plot and thus makes him stop spectating
*
* @param spectator the spectator to remove
* @since 2.1.0
*/
@Contract("null -> fail")
public void removeSpectator(GamePlayer spectator) {
getAllGamePlayers().remove(spectator);
getAllGamePlayers().forEach(player -> player.getPlayer().showPlayer(Main.getInstance(), spectator.getPlayer()));
Player spPlayer = spectator.getPlayer();
spectator.restore();
spPlayer.setCanPickupItems(true);
}
use of com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer in project buildinggame by stefvanschie.
the class Arena method preStart.
/**
* This is called before starting the game as to allow the players to vote on a subject if the subject choosing
* should be done at {@link When#BEFORE_BUILD}. This will automatically progress the game when needed. When starting
* the game this should be called rather than {@link #postStart()}.
*
* @since 6.4.0
*/
public void preStart() {
if (getSubjectMenu().getWhen() == SubjectMenu.When.BEFORE_BUILD) {
new BukkitRunnable() {
@Override
public void run() {
postStart();
}
}.runTaskLater(Main.getInstance(), 200L);
getUsedPlots().forEach(plot -> plot.getGamePlayers().forEach(gamePlayer -> {
var player = gamePlayer.getPlayer();
plot.getLocation().teleport(player);
giveSubjectMenuItem(player);
if (getSubjectMenu().opensInstantly()) {
getSubjectMenu().show(player);
}
}));
return;
}
postStart();
}
Aggregations