|
|
|
|
@ -2,6 +2,8 @@ package com.pobnellion.floorGame.game;
|
|
|
|
|
|
|
|
|
|
import com.pobnellion.floorGame.FloorGame;
|
|
|
|
|
import com.pobnellion.floorGame.util.Area;
|
|
|
|
|
import com.pobnellion.floorGame.util.Util;
|
|
|
|
|
import net.md_5.bungee.api.ChatColor;
|
|
|
|
|
import net.md_5.bungee.api.ChatMessageType;
|
|
|
|
|
import net.md_5.bungee.api.chat.ComponentBuilder;
|
|
|
|
|
import org.bukkit.*;
|
|
|
|
|
@ -15,6 +17,7 @@ import org.bukkit.inventory.EquipmentSlotGroup;
|
|
|
|
|
import org.bukkit.potion.PotionEffect;
|
|
|
|
|
import org.bukkit.potion.PotionEffectType;
|
|
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
|
|
|
import org.bukkit.scoreboard.*;
|
|
|
|
|
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
@ -35,8 +38,9 @@ public class GameInstance {
|
|
|
|
|
private LocalDateTime countdownStartTime;
|
|
|
|
|
private boolean stopped = false;
|
|
|
|
|
private final DecimalFormat timeFormat = new DecimalFormat("#.##");
|
|
|
|
|
private final Map<UUID, Integer> playerLives = new HashMap<>();
|
|
|
|
|
private final AttributeModifier setZeroModifier = new AttributeModifier(NamespacedKey.minecraft("set_zero"), -1, AttributeModifier.Operation.ADD_SCALAR, EquipmentSlotGroup.ANY);
|
|
|
|
|
private final Scoreboard livesScoreboard;
|
|
|
|
|
private final Objective livesScore;
|
|
|
|
|
|
|
|
|
|
public GameInstance(GameConfig config) {
|
|
|
|
|
floor = new Floor(config.getFloorCenter(), config.getTileSize(), config.getGridSize(), config.getAvailableColours());
|
|
|
|
|
@ -46,6 +50,10 @@ public class GameInstance {
|
|
|
|
|
|
|
|
|
|
if (world == null)
|
|
|
|
|
throw new NullPointerException("floorCenter world is not set");
|
|
|
|
|
|
|
|
|
|
livesScoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
|
|
|
|
|
livesScore = livesScoreboard.registerNewObjective("lives", Criteria.DUMMY, "lives", RenderType.INTEGER);
|
|
|
|
|
livesScore.setDisplaySlot(DisplaySlot.SIDEBAR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean IsPlayerInGame(Player player) {
|
|
|
|
|
@ -53,15 +61,22 @@ public class GameInstance {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void PlayerFell(Player player) {
|
|
|
|
|
playerLives.put(player.getUniqueId(), playerLives.get(player.getUniqueId()) - 1);
|
|
|
|
|
livesScore.getScore(player.getName()).setScore(livesScore.getScore(player.getName()).getScore() - 1);
|
|
|
|
|
player.teleport(config.getFloorCenter().clone().add(0, 10, 0));
|
|
|
|
|
|
|
|
|
|
if (playerLives.get(player.getUniqueId()) == 0) {
|
|
|
|
|
|
|
|
|
|
if (livesScore.getScore(player.getName()).getScore() == 0) {
|
|
|
|
|
players.remove(player);
|
|
|
|
|
spectators.add(player);
|
|
|
|
|
|
|
|
|
|
if (players.isEmpty()) {
|
|
|
|
|
spectators.forEach(spectator -> spectator.sendTitle("Game over!", player.getName() + " won", 10, 40, 10));
|
|
|
|
|
FloorGame.StopGame();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
player.setGameMode(GameMode.SPECTATOR);
|
|
|
|
|
player.getAttribute(Attribute.GENERIC_GRAVITY).removeModifier(setZeroModifier);
|
|
|
|
|
player.getAttribute(Attribute.GENERIC_FALL_DAMAGE_MULTIPLIER).removeModifier(setZeroModifier);
|
|
|
|
|
Util.resetAttributeModifiers(player);
|
|
|
|
|
player.sendTitle("", "You are now spectating", 0, 40, 20);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -82,6 +97,9 @@ public class GameInstance {
|
|
|
|
|
spectators.add(player);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (players.isEmpty())
|
|
|
|
|
FloorGame.StopGame();
|
|
|
|
|
|
|
|
|
|
countdownBossbar = Bukkit.createBossBar("", BarColor.WHITE, BarStyle.SOLID);
|
|
|
|
|
countdownBossbar.setVisible(false);
|
|
|
|
|
|
|
|
|
|
@ -89,14 +107,25 @@ public class GameInstance {
|
|
|
|
|
|
|
|
|
|
var playerTpLocation = config.getFloorCenter().clone().add(0, 2, 0);
|
|
|
|
|
var spectatorTpLocation = config.getFloorCenter().clone().add(0, 15, 0);
|
|
|
|
|
|
|
|
|
|
players.forEach(player -> {
|
|
|
|
|
player.setExp(0);
|
|
|
|
|
player.getInventory().clear();
|
|
|
|
|
player.teleport(playerTpLocation);
|
|
|
|
|
player.setGameMode(GameMode.ADVENTURE);
|
|
|
|
|
countdownBossbar.addPlayer(player);
|
|
|
|
|
playerLives.put(player.getUniqueId(), config.getFailLimit());
|
|
|
|
|
livesScore.getScore(player.getName()).setScore(config.getFailLimit());
|
|
|
|
|
Util.resetAttributeModifiers(player);
|
|
|
|
|
player.getAttribute(Attribute.GENERIC_FALL_DAMAGE_MULTIPLIER).addModifier(setZeroModifier);
|
|
|
|
|
player.getActivePotionEffects().forEach(effect -> player.removePotionEffect(effect.getType()));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
spectators.forEach(player -> {
|
|
|
|
|
Util.resetAttributeModifiers(player);
|
|
|
|
|
player.getActivePotionEffects().forEach(effect -> player.removePotionEffect(effect.getType()));
|
|
|
|
|
player.setGameMode(GameMode.SPECTATOR);
|
|
|
|
|
player.teleport(spectatorTpLocation);
|
|
|
|
|
});
|
|
|
|
|
spectators.forEach(player -> player.teleport(spectatorTpLocation));
|
|
|
|
|
|
|
|
|
|
roundInit().runTaskLater(FloorGame.GetInstance(), 50);
|
|
|
|
|
hudBar.runTaskTimer(FloorGame.GetInstance(), 0, 10);
|
|
|
|
|
@ -105,17 +134,35 @@ public class GameInstance {
|
|
|
|
|
private final BukkitRunnable hudBar = new BukkitRunnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
if (stopped)
|
|
|
|
|
if (stopped) {
|
|
|
|
|
cancel();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
players.forEach(player -> {
|
|
|
|
|
var actionBarComponent = new ComponentBuilder("Round " + round)
|
|
|
|
|
.append(" | ")
|
|
|
|
|
.append(playerLives.get(player.getUniqueId()) + " lives")
|
|
|
|
|
var actionBarComponent = new ComponentBuilder("Round " + round).color(ChatColor.WHITE)
|
|
|
|
|
.append(" | ").color(ChatColor.GRAY)
|
|
|
|
|
.append(livesScore.getScore(player.getName()).getScore() +
|
|
|
|
|
(livesScore.getScore(player.getName()).getScore() == 1 ? " life" : " lives"))
|
|
|
|
|
.color(LivesColour(player))
|
|
|
|
|
.append(" | ").color(ChatColor.GRAY)
|
|
|
|
|
.append("Time " + timeFormat.format(GetRoundTime()) + "s").color(ChatColor.WHITE)
|
|
|
|
|
.create();
|
|
|
|
|
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, actionBarComponent);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ChatColor LivesColour(Player player) {
|
|
|
|
|
var lives = livesScore.getScore(player.getName()).getScore();
|
|
|
|
|
|
|
|
|
|
if (lives == 1)
|
|
|
|
|
return ChatColor.RED;
|
|
|
|
|
|
|
|
|
|
if (lives == 2 || lives < config.getFailLimit() / 2)
|
|
|
|
|
return ChatColor.YELLOW;
|
|
|
|
|
|
|
|
|
|
return ChatColor.WHITE;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private BukkitRunnable roundInit() {
|
|
|
|
|
@ -147,7 +194,6 @@ public class GameInstance {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private BukkitRunnable roundWarn() {
|
|
|
|
|
return new BukkitRunnable() {
|
|
|
|
|
@Override
|
|
|
|
|
@ -206,18 +252,15 @@ public class GameInstance {
|
|
|
|
|
new BukkitRunnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
stopped = true;
|
|
|
|
|
floor.Clear();
|
|
|
|
|
Bukkit.getScheduler().cancelTasks(FloorGame.GetInstance());
|
|
|
|
|
countdownBossbar.removeAll();
|
|
|
|
|
countdownBossbar.setVisible(false);
|
|
|
|
|
players.forEach(player -> {
|
|
|
|
|
player.getActivePotionEffects().forEach(effect -> player.removePotionEffect(effect.getType()));
|
|
|
|
|
player.getAttribute(Attribute.GENERIC_GRAVITY).removeModifier(setZeroModifier);
|
|
|
|
|
player.getAttribute(Attribute.GENERIC_FALL_DAMAGE_MULTIPLIER).removeModifier(setZeroModifier);
|
|
|
|
|
player.teleport(config.getPostGameTpLocation());
|
|
|
|
|
player.setGameMode(GameMode.CREATIVE);
|
|
|
|
|
});
|
|
|
|
|
floor.Clear();
|
|
|
|
|
|
|
|
|
|
spectators.forEach(player -> {
|
|
|
|
|
players.addAll(spectators);
|
|
|
|
|
players.forEach(player -> {
|
|
|
|
|
Util.resetAttributeModifiers(player);
|
|
|
|
|
player.getActivePotionEffects().forEach(effect -> player.removePotionEffect(effect.getType()));
|
|
|
|
|
player.teleport(config.getPostGameTpLocation());
|
|
|
|
|
player.setGameMode(GameMode.CREATIVE);
|
|
|
|
|
});
|
|
|
|
|
@ -226,7 +269,7 @@ public class GameInstance {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double GetRoundTime() {
|
|
|
|
|
return 0.5 * floor.tileSize + Math.pow(0.5, Math.floor((double) round / timeDecreaseRounds));
|
|
|
|
|
return 0.5 * floor.tileSize - 0.03 * floor.tileSize * Math.floor((double) round / timeDecreaseRounds) + floor.tileSize * Math.pow(0.6, Math.floor((double) round / timeDecreaseRounds));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Collection<PotionEffect> GetRoundEffects() {
|
|
|
|
|
|