From e05870dccba35873a81381ec79afee3b7aed0f87 Mon Sep 17 00:00:00 2001 From: ruby Date: Sat, 17 Aug 2024 23:20:37 +1200 Subject: [PATCH] fixes n things --- .../com/pobnellion/floorGame/FloorGame.java | 4 - .../com/pobnellion/floorGame/game/Floor.java | 4 +- .../floorGame/game/GameInstance.java | 87 ++++++++++++++----- .../com/pobnellion/floorGame/util/Util.java | 26 ++++-- 4 files changed, 84 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/pobnellion/floorGame/FloorGame.java b/src/main/java/com/pobnellion/floorGame/FloorGame.java index 416091b..fb247ad 100644 --- a/src/main/java/com/pobnellion/floorGame/FloorGame.java +++ b/src/main/java/com/pobnellion/floorGame/FloorGame.java @@ -57,10 +57,6 @@ public final class FloorGame extends JavaPlugin { return false; } - public static boolean IsGameRunning() { - return gameInstance != null; - } - public static boolean IsPlayerInGame(Player player) { return gameInstance != null && gameInstance.IsPlayerInGame(player); } diff --git a/src/main/java/com/pobnellion/floorGame/game/Floor.java b/src/main/java/com/pobnellion/floorGame/game/Floor.java index 29a257d..9825d9b 100644 --- a/src/main/java/com/pobnellion/floorGame/game/Floor.java +++ b/src/main/java/com/pobnellion/floorGame/game/Floor.java @@ -126,9 +126,9 @@ public class Floor { xzCorner.getBlockX() -1, yMin, xzCorner.getBlockZ() + floorSize, - xzCorner.getBlockX() + floorSize, + xzCorner.getBlockX() + floorSize + 1, yMax, - xzCorner.getBlockZ() + floorSize + 1, + xzCorner.getBlockZ() + floorSize, material); } diff --git a/src/main/java/com/pobnellion/floorGame/game/GameInstance.java b/src/main/java/com/pobnellion/floorGame/game/GameInstance.java index 187d83e..e6d1014 100644 --- a/src/main/java/com/pobnellion/floorGame/game/GameInstance.java +++ b/src/main/java/com/pobnellion/floorGame/game/GameInstance.java @@ -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 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 GetRoundEffects() { diff --git a/src/main/java/com/pobnellion/floorGame/util/Util.java b/src/main/java/com/pobnellion/floorGame/util/Util.java index ce7cd1c..3945d58 100644 --- a/src/main/java/com/pobnellion/floorGame/util/Util.java +++ b/src/main/java/com/pobnellion/floorGame/util/Util.java @@ -1,17 +1,11 @@ package com.pobnellion.floorGame.util; import org.bukkit.Location; +import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeModifier; +import org.bukkit.entity.Player; public class Util { - public static boolean IsInBounds(Location location, Location bounds1, Location bounds2) { - return location.getX() > Math.min(bounds1.getX(), bounds2.getX()) - && location.getX() < Math.max(bounds1.getX(), bounds2.getX()) - && location.getY() > Math.min(bounds1.getY(), bounds2.getY()) - && location.getY() < Math.max(bounds1.getY(), bounds2.getY()) - && location.getZ() > Math.min(bounds1.getZ(), bounds2.getZ()) - && location.getZ() < Math.max(bounds1.getZ(), bounds2.getZ()); - } - public static String PrintBlockLocation(Location location) { return location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ(); } @@ -19,5 +13,19 @@ public class Util { public static String PrintArea(Area area) { return "[" + PrintBlockLocation(area.min) + "] - [" + PrintBlockLocation(area.max) + "]"; } + + public static void resetAttributeModifiers(Player player) { + for (Attribute attribute : Attribute.values()) { + var playerAttribute = player.getAttribute(attribute); + + if (playerAttribute == null) + continue; + + var modifiers = playerAttribute.getModifiers(); + for (AttributeModifier modifier : modifiers) { + playerAttribute.removeModifier(modifier); + } + } + } }