fixes n things

This commit is contained in:
ruby
2024-08-17 23:20:37 +12:00
parent f08d2662d9
commit e05870dccb
4 changed files with 84 additions and 37 deletions

View File

@ -57,10 +57,6 @@ public final class FloorGame extends JavaPlugin {
return false; return false;
} }
public static boolean IsGameRunning() {
return gameInstance != null;
}
public static boolean IsPlayerInGame(Player player) { public static boolean IsPlayerInGame(Player player) {
return gameInstance != null && gameInstance.IsPlayerInGame(player); return gameInstance != null && gameInstance.IsPlayerInGame(player);
} }

View File

@ -126,9 +126,9 @@ public class Floor {
xzCorner.getBlockX() -1, xzCorner.getBlockX() -1,
yMin, yMin,
xzCorner.getBlockZ() + floorSize, xzCorner.getBlockZ() + floorSize,
xzCorner.getBlockX() + floorSize, xzCorner.getBlockX() + floorSize + 1,
yMax, yMax,
xzCorner.getBlockZ() + floorSize + 1, xzCorner.getBlockZ() + floorSize,
material); material);
} }

View File

@ -2,6 +2,8 @@ package com.pobnellion.floorGame.game;
import com.pobnellion.floorGame.FloorGame; import com.pobnellion.floorGame.FloorGame;
import com.pobnellion.floorGame.util.Area; 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.ChatMessageType;
import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.ComponentBuilder;
import org.bukkit.*; import org.bukkit.*;
@ -15,6 +17,7 @@ import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.*;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.time.Duration; import java.time.Duration;
@ -35,8 +38,9 @@ public class GameInstance {
private LocalDateTime countdownStartTime; private LocalDateTime countdownStartTime;
private boolean stopped = false; private boolean stopped = false;
private final DecimalFormat timeFormat = new DecimalFormat("#.##"); 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 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) { public GameInstance(GameConfig config) {
floor = new Floor(config.getFloorCenter(), config.getTileSize(), config.getGridSize(), config.getAvailableColours()); floor = new Floor(config.getFloorCenter(), config.getTileSize(), config.getGridSize(), config.getAvailableColours());
@ -46,6 +50,10 @@ public class GameInstance {
if (world == null) if (world == null)
throw new NullPointerException("floorCenter world is not set"); 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) { public boolean IsPlayerInGame(Player player) {
@ -53,15 +61,22 @@ public class GameInstance {
} }
public void PlayerFell(Player player) { 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)); 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); players.remove(player);
spectators.add(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.setGameMode(GameMode.SPECTATOR);
player.getAttribute(Attribute.GENERIC_GRAVITY).removeModifier(setZeroModifier); Util.resetAttributeModifiers(player);
player.getAttribute(Attribute.GENERIC_FALL_DAMAGE_MULTIPLIER).removeModifier(setZeroModifier);
player.sendTitle("", "You are now spectating", 0, 40, 20); player.sendTitle("", "You are now spectating", 0, 40, 20);
} }
@ -82,6 +97,9 @@ public class GameInstance {
spectators.add(player); spectators.add(player);
}); });
if (players.isEmpty())
FloorGame.StopGame();
countdownBossbar = Bukkit.createBossBar("", BarColor.WHITE, BarStyle.SOLID); countdownBossbar = Bukkit.createBossBar("", BarColor.WHITE, BarStyle.SOLID);
countdownBossbar.setVisible(false); countdownBossbar.setVisible(false);
@ -89,14 +107,25 @@ public class GameInstance {
var playerTpLocation = config.getFloorCenter().clone().add(0, 2, 0); var playerTpLocation = config.getFloorCenter().clone().add(0, 2, 0);
var spectatorTpLocation = config.getFloorCenter().clone().add(0, 15, 0); var spectatorTpLocation = config.getFloorCenter().clone().add(0, 15, 0);
players.forEach(player -> { players.forEach(player -> {
player.setExp(0);
player.getInventory().clear();
player.teleport(playerTpLocation); player.teleport(playerTpLocation);
player.setGameMode(GameMode.ADVENTURE); player.setGameMode(GameMode.ADVENTURE);
countdownBossbar.addPlayer(player); 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.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); roundInit().runTaskLater(FloorGame.GetInstance(), 50);
hudBar.runTaskTimer(FloorGame.GetInstance(), 0, 10); hudBar.runTaskTimer(FloorGame.GetInstance(), 0, 10);
@ -105,17 +134,35 @@ public class GameInstance {
private final BukkitRunnable hudBar = new BukkitRunnable() { private final BukkitRunnable hudBar = new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
if (stopped) if (stopped) {
cancel(); cancel();
return;
}
players.forEach(player -> { players.forEach(player -> {
var actionBarComponent = new ComponentBuilder("Round " + round) var actionBarComponent = new ComponentBuilder("Round " + round).color(ChatColor.WHITE)
.append(" | ") .append(" | ").color(ChatColor.GRAY)
.append(playerLives.get(player.getUniqueId()) + " lives") .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(); .create();
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, actionBarComponent); 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() { private BukkitRunnable roundInit() {
@ -147,7 +194,6 @@ public class GameInstance {
}; };
} }
private BukkitRunnable roundWarn() { private BukkitRunnable roundWarn() {
return new BukkitRunnable() { return new BukkitRunnable() {
@Override @Override
@ -206,18 +252,15 @@ public class GameInstance {
new BukkitRunnable() { new BukkitRunnable() {
public void run() { public void run() {
stopped = true; stopped = true;
floor.Clear(); Bukkit.getScheduler().cancelTasks(FloorGame.GetInstance());
countdownBossbar.removeAll(); countdownBossbar.removeAll();
countdownBossbar.setVisible(false); countdownBossbar.setVisible(false);
players.forEach(player -> { floor.Clear();
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);
});
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.teleport(config.getPostGameTpLocation());
player.setGameMode(GameMode.CREATIVE); player.setGameMode(GameMode.CREATIVE);
}); });
@ -226,7 +269,7 @@ public class GameInstance {
} }
private double GetRoundTime() { 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() { private Collection<PotionEffect> GetRoundEffects() {

View File

@ -1,17 +1,11 @@
package com.pobnellion.floorGame.util; package com.pobnellion.floorGame.util;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.entity.Player;
public class Util { 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) { public static String PrintBlockLocation(Location location) {
return location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ(); return location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ();
} }
@ -19,5 +13,19 @@ public class Util {
public static String PrintArea(Area area) { public static String PrintArea(Area area) {
return "[" + PrintBlockLocation(area.min) + "] - [" + PrintBlockLocation(area.max) + "]"; 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);
}
}
}
} }