Skip to content

Commit

Permalink
Merge pull request #38 from efrei-craft/core-improvements
Browse files Browse the repository at this point in the history
Ajout de maps en dossier avec possibilité d'ajouter d'autres types de maps
  • Loading branch information
Niilyx authored Sep 5, 2023
2 parents beb0eca + 7b1ed64 commit 187af9a
Show file tree
Hide file tree
Showing 21 changed files with 256 additions and 104 deletions.
5 changes: 1 addition & 4 deletions core/src/main/java/fr/efreicraft/ludos/core/Core.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package fr.efreicraft.ludos.core;

import com.comphenix.protocol.ProtocolManager;
import fr.efreicraft.ACP.ACP;
import fr.efreicraft.ludos.core.commands.CommandManager;
import fr.efreicraft.ludos.core.games.GameServerDispatcher;
import fr.efreicraft.ludos.core.handlers.RedisHandler;
import fr.efreicraft.ludos.core.players.PlayerManager;
import fr.efreicraft.ludos.core.games.GameManager;
import fr.efreicraft.ludos.core.maps.MapManager;
import fr.efreicraft.ludos.core.players.PlayerManager;
import fr.efreicraft.ludos.core.teams.TeamManager;
import org.bukkit.Server;
import org.bukkit.plugin.java.JavaPlugin;
Expand Down
33 changes: 14 additions & 19 deletions core/src/main/java/fr/efreicraft/ludos/core/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,17 @@
import fr.efreicraft.ludos.core.games.GameManager;
import fr.efreicraft.ludos.core.players.LudosPlayer;
import fr.efreicraft.ludos.core.utils.ActionBarUtils;
import fr.efreicraft.ludos.core.utils.NBTUtils;
import net.kyori.adventure.text.Component;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.*;
import org.jetbrains.annotations.NotNull;
import org.spigotmc.event.player.PlayerSpawnLocationEvent;

import java.util.UUID;

/**
* Evenements de Core.
*
Expand All @@ -31,7 +26,7 @@ public class EventListener implements Listener {
/**
* Evenement de login d'un joueur.
*/
@EventHandler
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerLogin(PlayerLoginEvent event) {
if(Core.get().getGameManager().getStatus() == GameManager.GameStatus.WAITING
&& Core.get().getGameManager().getCurrentGame() != null
Expand All @@ -47,7 +42,7 @@ public void onPlayerLogin(PlayerLoginEvent event) {
* Evenement de connexion d'un joueur.
* @param event Evenement Bukkit
*/
@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerSpawn(PlayerSpawnLocationEvent event) {
if(Core.get().getGameManager().getStatus() == GameManager.GameStatus.WAITING) {
event.setSpawnLocation(Core.get().getMapManager().getLobbyWorld().getSpawnLocation().add(-0.5, 0, -0.5));
Expand All @@ -60,7 +55,7 @@ public void onPlayerSpawn(PlayerSpawnLocationEvent event) {
* Evenement de spawn d'un joueur sur le serveur.
* @param event Evenement Bukkit
*/
@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerJoin(PlayerJoinEvent event) {
event.joinMessage(null);
}
Expand All @@ -69,7 +64,7 @@ public void onPlayerJoin(PlayerJoinEvent event) {
* Evenement de spawn d'un joueur initialisé par ECATUP.
* @param event Evenement ECATUP
*/
@EventHandler
@EventHandler(priority = EventPriority.LOWEST)
public void onECPlayerJoin(ECPlayerJoined event) {
Core.get().getPlayerManager().addPlayer(new LudosPlayer(event.getPlayer()));
}
Expand All @@ -78,7 +73,7 @@ public void onECPlayerJoin(ECPlayerJoined event) {
* Evenement de deconnexion d'un joueur du serveur.
* @param event Evenement Bukkit
*/
@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerQuit(PlayerQuitEvent event) {
event.quitMessage(null);
Core.get().getPlayerManager().removePlayer(event.getPlayer());
Expand All @@ -97,7 +92,7 @@ private boolean hasMovedInBlockXAndBlockZ(@NotNull PlayerMoveEvent event) {
* Evenement de deplacement d'un joueur.
* @param event Evenement Bukkit
*/
@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerMove(PlayerMoveEvent event) {
if(
hasMovedInBlockXAndBlockZ(event)
Expand All @@ -115,7 +110,7 @@ public void onPlayerMove(PlayerMoveEvent event) {
}
}

@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.LOWEST)
public void onEntityDamage(EntityDamageEvent event) {
if(event.getEntity() instanceof org.bukkit.entity.Player
&& Core.get().getGameManager().getStatus() != GameManager.GameStatus.INGAME) {
Expand All @@ -126,50 +121,50 @@ public void onEntityDamage(EntityDamageEvent event) {
}
}

@EventHandler(priority = EventPriority.NORMAL)
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerDeath(PlayerDeathEvent event) {
LudosPlayer player = Core.get().getPlayerManager().getPlayer(event.getPlayer());
if(player != null) {
player.deathEvent(event);
}
}

@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerRespawn(PlayerRespawnEvent event) {
LudosPlayer player = Core.get().getPlayerManager().getPlayer(event.getPlayer());
if(player != null) {
player.respawnEvent(event);
}
}

@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerPostRespawn(PlayerPostRespawnEvent event) {
LudosPlayer player = Core.get().getPlayerManager().getPlayer(event.getPlayer());
if(player != null) {
player.postRespawnEvent();
}
}

@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerInteractEvent(PlayerInteractEvent event) {
if(Core.get().getGameManager().getStatus() != GameManager.GameStatus.INGAME) {
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerPortalEvent(PlayerPortalEvent event) {
event.setCancelled(true);
}

@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.LOWEST)
public void onFoodLevelChange(FoodLevelChangeEvent event) {
if(Core.get().getGameManager().getStatus() == GameManager.GameStatus.WAITING){
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerDropItemEvent(PlayerDropItemEvent event) {
if(Core.get().getGameManager().getStatus() != GameManager.GameStatus.INGAME){
event.setCancelled(true);
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/fr/efreicraft/ludos/core/LudosCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.comphenix.protocol.ProtocolLibrary;
import fr.efreicraft.ACP.ACP;
import fr.efreicraft.ludos.core.games.GameServerDispatcher;
import fr.efreicraft.ludos.core.handlers.RedisHandler;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import fr.efreicraft.ludos.core.Core;
import fr.efreicraft.ludos.core.games.exceptions.GameRegisteringException;
import fr.efreicraft.ludos.core.games.exceptions.GameStatusException;
import fr.efreicraft.ludos.core.players.LudosPlayer;
import fr.efreicraft.ludos.core.utils.MessageUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down
40 changes: 19 additions & 21 deletions core/src/main/java/fr/efreicraft/ludos/core/games/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import fr.efreicraft.ludos.core.IManager;
import fr.efreicraft.ludos.core.games.exceptions.GameRegisteringException;
import fr.efreicraft.ludos.core.games.exceptions.GameStatusException;
import fr.efreicraft.ludos.core.games.interfaces.Game;
import fr.efreicraft.ludos.core.games.interfaces.GamePlugin;
import fr.efreicraft.ludos.core.games.runnables.LobbyCountdown;
import fr.efreicraft.ludos.core.players.LudosPlayer;
import fr.efreicraft.ludos.core.games.interfaces.Game;
import org.bukkit.Bukkit;
import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPluginLoader;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -200,9 +199,7 @@ public void resetServer() {
* @return Liste des jeux disponibles
*/
public List<String> getAvailableGames() {
ArrayList<String> gamesAvailable = new ArrayList<>();
gamesAvailable.addAll(gamePlugins.keySet());
return gamesAvailable;
return new ArrayList<>(gamePlugins.keySet());
}

/**
Expand Down Expand Up @@ -313,23 +310,24 @@ public void setStatus(GameStatus status) {
player.setupScoreboard();
}

if (status == GameStatus.STARTING) {
currentGame.startGame();
} else if (status == GameStatus.INGAME) {
if (!currentGame.checkIfGameHasToBeEnded()) {
currentGame.beginGame();
switch (status) {
case STARTING -> currentGame.startGame();
case INGAME -> {
if (!currentGame.checkIfGameHasToBeEnded()) {
currentGame.beginGame();
}
}
} else if (status == GameStatus.ENDING) {
currentGame.endGame();
} else if (status == GameStatus.WAITING) {
this.unregisterCurrentGame();
if (defaultGamePluginName != null && autoGameStart) {
try {
this.loadGame(defaultGamePluginName);
} catch (GameStatusException e) {
e.printStackTrace();
} catch (GameRegisteringException e) {
throw new RuntimeException(e);
case ENDING -> currentGame.endGame();
case WAITING -> {
this.unregisterCurrentGame();
if (defaultGamePluginName != null && autoGameStart) {
try {
this.loadGame(defaultGamePluginName);
} catch (GameStatusException e) {
e.printStackTrace();
} catch (GameRegisteringException e) {
throw new RuntimeException(e);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public static void updateStatus() {
Core.get().getGameManager().getDefaultGamePluginName(),
Core.get().getGameManager().getStatus().name()
);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
} catch (ApiException e) {
} catch (UnknownHostException | ApiException e) {
throw new RuntimeException(e);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
import org.bukkit.Location;
import org.bukkit.event.Listener;

import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.*;
import java.util.logging.Level;

/**
Expand Down Expand Up @@ -52,6 +49,8 @@ public void prepareServer() {
GameMetadata metadata = getMetadata();
MessageUtils.broadcastMessage(MessageUtils.ChatPrefix.GAME, "&7Le prochain jeu sera " + metadata.color() + metadata.name() + "&7!");

Core.get().getMapManager().setupCurrentGameMaps(this);

List<String> maps = getMaps();
if (maps.isEmpty()) {
Core.get().getLogger().warning("No maps available for " + this.getClass().getName() + "!");
Expand Down Expand Up @@ -193,7 +192,7 @@ public void endGame() {
* @return Liste des cartes disponibles en String
*/
public List<String> getMaps() {
return Core.get().getMapManager().getMapsForGame(this);
return new ArrayList<>(Core.get().getMapManager().getMapsForGame().keySet());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fr.efreicraft.ludos.core.games.interfaces;

import fr.efreicraft.ludos.core.Core;
import fr.efreicraft.ludos.core.games.exceptions.GameRegisteringException;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public GameTimer(GameTimerAction action, int time) {
@Override
public void run() {
if(Core.get().getGameManager().getStatus() != GameManager.GameStatus.INGAME) {
this.cancel();
this.cancel(); // TODO : peut-être digne d'une exception ça.
return;
}
action.run(Math.abs(time));
Expand Down
Loading

0 comments on commit 187af9a

Please sign in to comment.