Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Commit

Permalink
added player base item into file
Browse files Browse the repository at this point in the history
redoing playerjoin event
  • Loading branch information
Synchroneyes committed Mar 25, 2020
1 parent ed2c502 commit f2c947e
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/main/java/fr/mineral/Core/Arena/Coffre.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.mineral.Core.Arena;

import fr.mineral.Utils.Player.PlayerUtils;
import fr.mineral.Utils.Range;
import fr.mineral.mineralcontest;
import org.bukkit.Location;
Expand Down Expand Up @@ -28,6 +29,8 @@ public Location getPosition() throws Exception {
public void clear() {
Block b = this.position.getBlock();
((Chest)b.getState()).getInventory().clear();
if(!b.getType().equals(Material.AIR)) b.breakNaturally();

}

public void spawn() {
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/fr/mineral/Core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public class Game implements Listener {
public int killCounter = 0;
private AutomaticDoors portes;
private LinkedList<CouplePlayerTeam> disconnectedPlayers;
// <username, allowed to login>
private HashMap<String, Boolean> PlayerThatTriedToLogIn;

public Votemap votemap;

// Save the blocks
Expand All @@ -83,6 +86,34 @@ public Game() {
this.affectedBlocks = new LinkedList<>();
this.referees = new LinkedList<>();
this.playersReady = new LinkedList<>();
this.PlayerThatTriedToLogIn = new HashMap<>();
}

public boolean isThereAnAdminLoggedIn() {
for(Player player : mineralcontest.plugin.pluginWorld.getPlayers())
if(player.isOp()) return true;
return false;
}

public void addPlayerTriedToLogin(String playerDisplayName) {
if( ! havePlayerTriedToLogin(playerDisplayName)) this.PlayerThatTriedToLogIn.put(playerDisplayName, false);
}

public boolean havePlayerTriedToLogin(String playerDisplayName) {
return this.PlayerThatTriedToLogIn.containsKey(playerDisplayName);
}

public void allowPlayerLogin(String playerDisplayName) {
if(havePlayerTriedToLogin(playerDisplayName)) this.PlayerThatTriedToLogIn.replace(playerDisplayName, true);
}

public void removePlayerLoginAttempt(String playerDisplayName) {
if(havePlayerTriedToLogin(playerDisplayName)) this.PlayerThatTriedToLogIn.remove(playerDisplayName);
}

public boolean isPlayerAllowedToLogIn(String playerName) {
if(havePlayerTriedToLogin(playerName)) return this.PlayerThatTriedToLogIn.get(playerName);
return false;
}

public boolean areAllPlayersReady() {
Expand Down
185 changes: 177 additions & 8 deletions src/main/java/fr/mineral/Events/PlayerJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,137 @@ public class PlayerJoin implements Listener {


@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) throws Exception {
public void onPlayerJoin(PlayerJoinEvent event) {
World worldEvent = event.getPlayer().getWorld();
if (worldEvent.equals(mineralcontest.plugin.pluginWorld)) {

// Init variables
mineralcontest plugin = mineralcontest.plugin;
Game game = plugin.getGame();
Player player = event.getPlayer();
String playerName = player.getDisplayName();
// if havePlayerTriedToJoin = true, then it's not player's first attempt
boolean isPlayerFirstJoinAttempt = !game.havePlayerTriedToLogin(playerName);
boolean isPlayerAllowedToJoin = game.isPlayerAllowedToLogIn(playerName);
boolean havePlayerDisconnectedEarlier = game.havePlayerDisconnected(playerName);
boolean thereIsAnAdminLoggedIn = game.isThereAnAdminLoggedIn();
// If true, we allow every admin login
boolean allow_player_to_join = false;

CouplePlayerTeam playerInfos = (havePlayerDisconnectedEarlier) ? game.getDisconnectedPlayerInfo(playerName) : null;
Equipe oldPlayerTeam = (playerInfos != null) ? playerInfos.getTeam() : null;

// SETTINGS
boolean enable_admin_login = true;


// First, we check if the map is correct
mineralcontest.checkIfMapIsCorrect();

// If the game is started
if(game.isGameStarted()) {
// We need to check if the player have disconnected earlier
if(havePlayerDisconnectedEarlier) {
// Player was connected earlier

// if oldPLayerTeam is null, then he wasn't in a team
// If he is OP, then he should be referee
// Else, admin need to decide what to do
if(oldPlayerTeam == null) {

if(player.isOp()) {
// If player is op and oldteam is null, then he is a referee
game.addReferee(player);
mineralcontest.log.info("Player " + playerName + " was OP, had no team but was registered inside the disconnected players list, so we added him to the referees");
return;
} else {
// Player is not OP
if(thereIsAnAdminLoggedIn) {
// Admin needs to decide what to do, should'nt happen
mineralcontest.broadcastMessageToAdmins(playerName + " joined the game, he was connected earlier but it seems like we dont know his team. You can switch him into a team by using /switch " + playerName + " <team>");
mineralcontest.log.info("Player " + playerName + " joined the game, he was NOT OP, he had no team but was registered inside the disconnected players, admin needs to decide what to do.");
if(!game.isGamePaused()) game.pauseGame();
} else {
player.kickPlayer("PlayerJoin-L-74 - There is no admin logged in and the system doesn't know what to do. Please contact the plugin author");
mineralcontest.log.severe("PlayerJoin-L-74 - " + playerName + " tried to join but There is no admin logged in and the system doesn't know what to do. Please contact the plugin author");
}
return;
}

} else {
// His old team isnt null, we add him back
oldPlayerTeam.addPlayerToTeam(player);
game.resumeGame();
return;
}

} else {
// The player was not connected earlier
// This is a "random" player that tries to join
// If the player is OP and we allow admin to join, then he should be added as a referee
if(player.isOp() && enable_admin_login) { game.addReferee(player); return;}
// The player is now (op and enable_admin_login = false) or this is his first attempt
// If this is his first attemps, that mean he shouldnt be allowed to join, except if he is an admin
if(isPlayerFirstJoinAttempt) {
// We register him into the player attempts list
game.addPlayerTriedToLogin(playerName);
// We inform the admins
informAdminsThatAPlayerTriedToJoin(playerName);
return;
} else {
// This is not player first attempt
if(! isPlayerAllowedToJoin) {
// Player is NOT allowed to join, an admin needs to do /allow <playername>
player.kickPlayer("You are not allowed to join the game");
// We inform the admins
informAdminsThatAPlayerTriedToJoin(playerName);
} else {
//Player is allowed to join!
informAdminThatAPlayerNeedASwitch(playerName);
game.pauseGame();
return;
}
}
}
}

if(game.isPreGame()) {
// If the player have disconnected during preGame
if(havePlayerDisconnectedEarlier) {

}

if(isPlayerFirstJoinAttempt) {
if(thereIsAnAdminLoggedIn) {
game.addPlayerTriedToLogin(playerName);
informAdminsThatAPlayerTriedToJoin(playerName);
}
game.addPlayerTriedToLogin(playerName);
player.kickPlayer("You are not allowed to join the game");
} else {
if(isPlayerAllowedToJoin) {

}
}

}

// todo: game is pregame



}
}

private void informAdminsThatAPlayerTriedToJoin(String playerName) {
mineralcontest.broadcastMessageToAdmins("Player " + playerName + " tried to join, you can allow him to join by typing /allow " + playerName);
}

private void informAdminThatAPlayerNeedASwitch(String playerName) {
mineralcontest.broadcastMessageToAdmins("Player " + playerName + " have joined the game; switch him into a team with /switch " + playerName + " <team>");
}

/*public void onPlayerJoin(PlayerJoinEvent event) throws Exception {
World worldEvent = event.getPlayer().getWorld();
if(worldEvent.equals(mineralcontest.plugin.pluginWorld)) {
Expand All @@ -35,6 +165,8 @@ public void onPlayerJoin(PlayerJoinEvent event) throws Exception {
PlayerUtils.setMaxHealth(p);
// SI la game n'a pas démarré et que tout le monde est connecté
Game game = mineralcontest.plugin.getGame();
Expand All @@ -43,11 +175,11 @@ public void onPlayerJoin(PlayerJoinEvent event) throws Exception {
mineralcontest.checkIfMapIsCorrect();
}
/*
Si la partie n'a pas encore démarré, et que le joueur se connecte au serveur, alors
on le téléporte sur la plateforme
ET on le remet à zero
*/
if (!game.isGameStarted() &&
!game.isPreGame() &&
Expand All @@ -67,18 +199,18 @@ public void onPlayerJoin(PlayerJoinEvent event) throws Exception {
// Si le joueur se connecte (et qu'il n'est pas admin) et que le vote est terminé, c'est trop tard
// Si il s'était déconnecté, on le laisse revenir
/*if(mineralcontest.plugin.getGame().votemap.isVoteEnded()) {
if(mineralcontest.plugin.getGame().votemap.isVoteEnded()) {
if(!game.havePlayerDisconnected(p.getDisplayName()) && !p.isOp()) {
p.kickPlayer(Lang.kick_game_already_in_progress.toString());
} else if(p.isOp()) {
// Sinon, on le met arbitre
mineralcontest.plugin.getGame().addReferee(p);
}
}*/
}
if (!game.isGameStarted())
mineralcontest.broadcastMessage(mineralcontest.prefixGlobal + Lang.hud_awaiting_players.toString());
//if (!game.isGameStarted())
//mineralcontest.broadcastMessage(mineralcontest.prefixGlobal + Lang.hud_awaiting_players.toString());
if (game.isPreGame() && p.isOp()) {
Expand Down Expand Up @@ -131,5 +263,42 @@ public void onPlayerJoin(PlayerJoinEvent event) throws Exception {
}
}
}
}
}*/

/*
// If the game is already started
if(game.isGameStarted()) {
// If player was disconnected
if(game.havePlayerDisconnected(player.getDisplayName())) {
CouplePlayerTeam playerInfos = game.getDisconnectedPlayerInfo(player.getDisplayName());
Equipe playerOldTeam = playerInfos.getTeam();
// IF he wasn't in a team, he must be a referee
if(playerOldTeam == null) {
if(player.isOp()) {
game.addReferee(player);
} else {
// He isnt op ... But has disconnected. So, an admin should switch him
mineralcontest.broadcastMessage("A player that was disconnted have joined the game, but he doesn't have a team.");
}
} else {
// We put him back in his team
playerOldTeam.addPlayerToTeam(player);
}
// We remove the user from disconnected players
game.removePlayerFromDisconnected(player);
return;
}
} else {
// Game is not started
if(game.isPreGame()) {
// IF it's the pregame, he should get kicked
game.addPlayerTriedToLogin(player.getDisplayName());
mineralcontest.broadcastMessageToAdmins("A player tried to join the game, his username is:" + playerName);
mineralcontest.broadcastMessageToAdmins("To allow him to join, enter the following command /allow " + playerName);
player.kickPlayer(Lang.kick_game_already_in_progress.toString());
}
}
*/
}
2 changes: 1 addition & 1 deletion src/main/java/fr/mineral/Utils/Player/PlayerBaseItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class PlayerBaseItem {
private static HashMap<ItemStack, String> itemsToGive;


private static void copyDefaultFileToPluginDataFolder() {
public static void copyDefaultFileToPluginDataFolder() {
String file_name = "player_base_items.yml";
baseItemFile = new File(plugin.getDataFolder(), file_name);
if(!baseItemFile.exists())
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/fr/mineral/mineralcontest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import fr.mineral.Translation.Language;
import fr.mineral.Utils.Metric.SendInformation;
import fr.mineral.Utils.Player.PlayerBaseItem;
import fr.mineral.Utils.Player.PlayerUtils;
import fr.mineral.Utils.Save.MapFileHandler;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -71,6 +72,7 @@ public void onEnable() {
registerCommands();
registerEvents();
MapFileHandler.copyMapFileToPluginRessourceFolder();
PlayerBaseItem.copyDefaultFileToPluginDataFolder();

pluginWorld = Bukkit.getWorld((String) GameSettingsCvar.getValueFromCVARName("world_name"));

Expand Down Expand Up @@ -163,4 +165,10 @@ public static void broadcastMessage(String message) {
Bukkit.getLogger().info(message);
}

public static void broadcastMessageToAdmins(String message) {
for(Player player : mineralcontest.plugin.pluginWorld.getPlayers())
if(player.isOp()) player.sendMessage(message);
Bukkit.getLogger().info(message);
}

}

0 comments on commit f2c947e

Please sign in to comment.