Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,22 @@ The Folder for Gameworlds and configs is `./MiniGameCore`. Every Game World shou

All available options:

| Field | Description | Required / Default |
|------------------------|-------------------------------------------------------------------|--------------------|
| `name` | Display name of the game, e.g. in the scoreboard or at `/mg host` | ✅ Yes |
| `maxPlayers` | Maximum number of players for this game instance | ✅ Yes |
| `teams` | Maximum number of teams (0 for no teams, 2-8 teams possible) | ❌ No (default: 0) |
| `spawnPoints` | Default spawn points for players without a team | ✅ Depends |
| `teamSpawnPoints` | Spawn points per team (e.g. `0: [...]`, `1: [...]`) | ✅ Depends |
| `inventory` | Starting items at game start (e.g. `["WOODEN_SHOVEL"]`) | ❌ No |
| `allowed_break_blocks` | Which blocks can be broken (e.g. `["SNOW_BLOCK"]`) | ❌ No |
| `respawnMode` | Control of respawn behavior: `"true"` or `"false"` | ❌ No (default: false) |
| `respawnDelay` | Seconds delay until respawn (if enabled) | ❌ No (default: 0) |
| `doDurability` | Control ItemDamage: `true` (vanilla) or `false` | ❌ No (default: true) |
| `allowPVP` | Allow PVP: `true` (vanilla) or `false` | ❌ No (default: true) |
| `blocked_damage_causes`| Stop these damage causes from happening | ❌ No |
| `timeLimit` | Stops a game after X seconds have passed | ❌ No (default: 600) |
| Field | Description | Required / Default |
|-------------------------|------------------------------------------------------------------|-----------------------|
| `name` | Display name of the game, e.g. in the scoreboard or at `/mg host` | ✅ Yes |
| `maxPlayers` | Maximum number of players for this game instance | ✅ Yes |
| `teams` | Maximum number of teams (0 for no teams, 2-8 teams possible) | ❌ No (default: 0) |
| `spawnPoints` | Default spawn points for players without a team | ✅ Depends |
| `teamSpawnPoints` | Spawn points per team (e.g. `0: [...]`, `1: [...]`) | ✅ Depends |
| `inventory` | Starting items at game start (e.g. `["WOODEN_SHOVEL"]`) | ❌ No |
| `allowed_break_blocks` | Which blocks can be broken (e.g. `["SNOW_BLOCK"]`) | ❌ No |
| `respawnMode` | Control of respawn behavior: `"true"` or `"false"` | ❌ No (default: false) |
| `respawnDelay` | Seconds delay until respawn (if enabled) | ❌ No (default: 0) |
| `doDurability` | Control ItemDamage: `true` (vanilla) or `false` | ❌ No (default: true) |
| `allowPVP` | Allow PVP: `true` (vanilla) or `false` | ❌ No (default: true) |
| `blocked_damage_causes` | Stop these damage causes from happening | ❌ No |
| `timeLimit` | Stops a game after X seconds have passed | ❌ No (default: 600) |
| `allowFriendlyFire` | Allow members of the same team to attack each other | ❌ No (default: false) |

Example config for an 8 player Spleef-Game:
```
Expand Down
59 changes: 34 additions & 25 deletions src/main/java/wueffi/MiniGameCore/managers/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import wueffi.MiniGameCore.MiniGameCore;
import wueffi.MiniGameCore.api.GameOverEvent;
import wueffi.MiniGameCore.api.GameStartEvent;
Expand Down Expand Up @@ -58,6 +59,8 @@ public static void startGame(Lobby lobby) {
}

public static void endGame(Lobby lobby, Winner winner) {
lobby.endTimer();

if (winner instanceof Winner.TieWinner(List<Player> playerList)) {
for (Player player : lobby.getPlayers()) {
if (playerList.contains(player)) Stats.tie(lobby.getGameName(), player);
Expand Down Expand Up @@ -132,7 +135,7 @@ private static void startCountdown(Lobby lobby) {

for (int teamIndex = 0; teamIndex < teamCount; teamIndex++) {
Set<Player> teamPlayers = lobby.getTeam(teamIndex).getPlayers();
List<GameConfig.TeamSpawnPoint> teamSpawns = new ArrayList<>(gameConfig.getTeamSpawnPoints().get(teamIndex).getSpawnPoints());
List<GameConfig.TeamSpawnPoint> teamSpawns = new ArrayList<>(gameConfig.getTeamSpawnPoints().get(teamIndex).spawnPoints());
Collections.shuffle(teamSpawns);

for (Player teamPlayer : teamPlayers) {
Expand Down Expand Up @@ -186,7 +189,9 @@ public void run() {
player.getInventory().addItem(new ItemStack(material));
}
frozenPlayers.remove(player);
runDelayed(() -> timeLimitGame(lobby), timeLimit);
if (timeLimit > 0) {
lobby.startTimer(timeLimit);
}
}
cancel();
}
Expand All @@ -205,8 +210,8 @@ public static GameConfig loadGameConfigFromWorld(File worldFolder) {
}
}

public static void runDelayed(Runnable task, int seconds) {
Bukkit.getScheduler().runTaskLater(Objects.requireNonNull(Bukkit.getPluginManager().getPlugin("MiniGameCore")), task, seconds * 20L);
public static BukkitTask runDelayed(Runnable task, int seconds) {
return Bukkit.getScheduler().runTaskLater(Objects.requireNonNull(Bukkit.getPluginManager().getPlugin("MiniGameCore")), task, seconds * 20L);
}

public void hostGame(String gameName, CommandSender sender) {
Expand Down Expand Up @@ -278,6 +283,7 @@ public void hostGame(String gameName, CommandSender sender) {

public static void timeLimitGame(Lobby lobby) {
List<Player> alive = alivePlayers.get(lobby);
lobby.endTimer();
GameManager.endGame(lobby, new Winner.TieWinner(alive));
}

Expand Down Expand Up @@ -504,31 +510,34 @@ public void onToolDamage(PlayerItemDamageEvent event) {

@EventHandler
public void onDamage(EntityDamageByEntityEvent event) {
final Entity damager = event.getDamager();
final Entity damaged = event.getEntity();
if (!(event.getDamager() instanceof Player damager && event.getEntity() instanceof Player damaged)) return;

if (damager instanceof Player && damaged instanceof Player) {
Lobby lobby = LobbyManager.getLobbyByPlayer((Player) damager);
if (lobby == null) {
event.setCancelled(false);
return;
}
if (Objects.equals(lobby.getLobbyState(), "WAITING")) {
damager.sendMessage("§8[§6MiniGameCore§8]§c You are not allowed to PVP (yet)");
event.setCancelled(true);
return;
}
Lobby lobby = LobbyManager.getLobbyByPlayer(damager);
if (lobby == null) return;

GameConfig config = loadGameConfigFromWorld(lobby.getWorldFolder());
if (!config.getPVPMode() && Objects.equals(lobby.getLobbyState(), "GAME")) {
event.setCancelled(true);
damager.sendMessage("§8[§6MiniGameCore§8]§c You are not allowed to PVP");
return;
}
if (Objects.equals(lobby.getLobbyState(), "WAITING")) {
damager.sendMessage("§8[§6MiniGameCore§8]§c You are not allowed to PVP (yet)");
event.setCancelled(true);
return;
}

GameConfig config = loadGameConfigFromWorld(lobby.getWorldFolder());
if (!config.getPVPMode() && Objects.equals(lobby.getLobbyState(), "GAME")) {
event.setCancelled(true);
damager.sendMessage("§8[§6MiniGameCore§8]§c You are not allowed to PVP");
return;
}

lastHit.remove((Player) damaged);
lastHit.put((Player) damaged, (Player) damager);
if (!config.getAllowFriendlyFire()
&& config.getTeams() > 0
&& lobby.getTeamByPlayer(damager).equals(lobby.getTeamByPlayer(damaged))) {
event.setCancelled(true);
damager.sendMessage("§8[§6MiniGameCore§8]§c Friendly fire is not enabled for this minigame");
return;
}

lastHit.remove(damaged);
lastHit.put(damaged, damager);
}

@EventHandler
Expand Down
23 changes: 7 additions & 16 deletions src/main/java/wueffi/MiniGameCore/utils/GameConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class GameConfig {
private final boolean doDurability;
private final boolean allowPVP;
private final boolean respawnByAPI;
private final boolean allowFriendlyFire;
private final int timeLimit;

public GameConfig(File configFile) {
Expand All @@ -40,6 +41,7 @@ public GameConfig(File configFile) {
this.allowPVP = config.getBoolean("game.allowPVP", true);
this.respawnByAPI = config.getBoolean("game.respawnByAPI", false);
this.timeLimit = config.getInt("game.timeLimit", 600); //10 Minutes
this.allowFriendlyFire = config.getBoolean("game.allowFriendlyFire", false);

if (config.contains("game.spawnPoints")) {
for (String key : config.getConfigurationSection("game.spawnPoints").getKeys(false)) {
Expand Down Expand Up @@ -156,27 +158,16 @@ public Set<DamageCause> getBlockedDamageCauses() {
return blockedDamageCauses;
}

public boolean getAllowFriendlyFire() {
return allowFriendlyFire;
}

public record SpawnPoint(int x, int y, int z) {
}

public record TeamSpawnPoint(int x, int y, int z) {
}

public static class TeamSpawnPoints {
private final String teamName;
private final List<TeamSpawnPoint> spawnPoints;

public TeamSpawnPoints(String teamName, List<TeamSpawnPoint> spawnPoints) {
this.teamName = teamName;
this.spawnPoints = spawnPoints;
}

public String getTeamName() {
return teamName;
}

public List<TeamSpawnPoint> getSpawnPoints() {
return spawnPoints;
}
public record TeamSpawnPoints(String teamName, List<TeamSpawnPoint> spawnPoints) {
}
}
16 changes: 16 additions & 0 deletions src/main/java/wueffi/MiniGameCore/utils/Lobby.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import wueffi.MiniGameCore.managers.LobbyManager;
import wueffi.MiniGameCore.managers.ScoreBoardManager;

import java.io.File;
import java.util.*;

import static wueffi.MiniGameCore.managers.GameManager.runDelayed;
import static wueffi.MiniGameCore.managers.GameManager.timeLimitGame;

public class Lobby {
private final String lobbyId;
private final String gameName;
Expand All @@ -19,6 +23,7 @@ public class Lobby {
private String lobbyState;
private final List<Team> teamList = new ArrayList<>();
private int teamCounter = 0;
private BukkitTask timeLimiter;

public Lobby(String lobbyId, String gameName, int maxPlayers, Player owner, File worldFolder, String LobbyState) {
this.lobbyId = lobbyId;
Expand All @@ -30,6 +35,17 @@ public Lobby(String lobbyId, String gameName, int maxPlayers, Player owner, File
this.lobbyState = LobbyState;
}

public void startTimer(int timer) {
if (timeLimiter != null) return;
timeLimiter = runDelayed(() -> timeLimitGame(this), timer);
}

public void endTimer() {
if (timeLimiter == null) return;
timeLimiter.cancel();
timeLimiter = null;
}

public boolean addPlayer(Player player) {
if (players.contains(player.getUniqueId())) return false;
if (players.size() >= maxPlayers) return false;
Expand Down