Skip to content

Commit

Permalink
Added database integration (#62)
Browse files Browse the repository at this point in the history
@Xernas78 J'ai désactivé l'inventaire de team parce que flemme de
trouver un moyen de l'stocker dans la db + mon code est vraiment bancal
+ il y a pas de cache x) Mais bon, ça fait l'taff
  • Loading branch information
Margouta authored Jul 5, 2024
2 parents b1f9c0d + c806ad2 commit a0fe2bf
Show file tree
Hide file tree
Showing 10 changed files with 357 additions and 64 deletions.
16 changes: 14 additions & 2 deletions src/main/java/fr/communaywen/core/AywenCraftPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
import fr.communaywen.core.listeners.ChatListener;
import fr.communaywen.core.listeners.SleepListener;
import fr.communaywen.core.teams.TeamManager;
import fr.communaywen.core.commands.ProutCommand;
import fr.communaywen.core.utils.DiscordWebhook;
import fr.communaywen.core.utils.MOTDChanger;
import fr.communaywen.core.commands.VersionCommand;
import fr.communaywen.core.utils.PermissionCategory;
import fr.communaywen.core.commands.RTPCommand;
import fr.communaywen.core.utils.database.DatabaseManager;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
Expand All @@ -17,6 +21,7 @@
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.sql.SQLException;

public final class AywenCraftPlugin extends JavaPlugin {

Expand All @@ -26,6 +31,8 @@ public final class AywenCraftPlugin extends JavaPlugin {
private static AywenCraftPlugin instance;
private EconomyManager economyManager;

private DatabaseManager databaseManager;

private void loadBookConfig() {
File bookFile = new File(getDataFolder(), "rules.yml");
if (!bookFile.exists()) {
Expand All @@ -37,8 +44,10 @@ private void loadBookConfig() {
@Override
public void onEnable() {
super.getLogger().info("Hello le monde, ici le plugin AywenCraft !");
saveDefaultConfig();

instance = this;
databaseManager = new DatabaseManager(this);

MenuLib.init(this);

Expand Down Expand Up @@ -71,7 +80,6 @@ public void onEnable() {
this.getCommand("rtp").setExecutor(new RTPCommand(this));
getServer().getPluginManager().registerEvents(new AntiTrampling(),this);
getServer().getPluginManager().registerEvents(new SleepListener(),this);
saveDefaultConfig();

// Initialiser EconomyManager et enregistrer la commande money
economyManager = new EconomyManager(getDataFolder());
Expand All @@ -80,13 +88,17 @@ public void onEnable() {

@Override
public void onDisable() {
// Logic to save data if necessary
this.databaseManager.close();
}

public TeamManager getTeamManager() {
return teamManager;
}

public DatabaseManager getDatabaseManager() {
return databaseManager;
}

public static AywenCraftPlugin getInstance() {
return instance;
}
Expand Down
121 changes: 100 additions & 21 deletions src/main/java/fr/communaywen/core/commands/TeamCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand All @@ -25,13 +27,18 @@
public class TeamCommand implements CommandExecutor, TabCompleter {

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
TeamManager teamManager = AywenCraftPlugin.getInstance().getTeamManager();
if (!(sender instanceof Player player)) {
return CommandUtils.sendMessage(sender, "Vous devez être un joueur pour exécuter cette commande !", true);
}
if (args.length == 0) {
Team team = teamManager.isInTeam(player.getUniqueId());
Team team = null;
try {
team = teamManager.isInTeam(player.getUniqueId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
if (team == null) {
return CommandUtils.sendMessage(sender, "Vous n'êtes pas dans une team !", true);
}
Expand All @@ -43,17 +50,31 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return CommandUtils.sendMessage(sender, ChatColor.WHITE + "Usage: /team create <nom de la team>", true);
}
if (args[0].equalsIgnoreCase("list")) {
Menu menu = new TeamListMenu(player, teamManager);
Menu menu = null;
try {
menu = new TeamListMenu(player, teamManager);
} catch (SQLException e) {
throw new RuntimeException(e);
}
menu.open();
}
if (args[0].equalsIgnoreCase("invite")) {
return CommandUtils.sendMessage(sender, ChatColor.WHITE + "Usage: /team invite <joueur>", true);
}
if (args[0].equalsIgnoreCase("accept")) {
if (teamManager.isInTeam(player.getUniqueId()) != null) {
return CommandUtils.sendMessage(sender, "Vous êtes déjà dans une team !", true);
try {
if (teamManager.isInTeam(player.getUniqueId()) != null) {
return CommandUtils.sendMessage(sender, "Vous êtes déjà dans une team !", true);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
Team team = null;
try {
team = teamManager.acceptInvite(player.getUniqueId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
Team team = teamManager.acceptInvite(player.getUniqueId());
if (team != null) {
CommandUtils.sendMessage(sender, ChatColor.GREEN + "Vous avez bien rejoint la team " + team.getName() + " !", false);
for (UUID teamPlayer : team.getPlayers()) {
Expand All @@ -70,14 +91,28 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return CommandUtils.sendMessage(sender, ChatColor.WHITE + "Usage: /team kick <joueur>", true);
}
if (args[0].equalsIgnoreCase("leave")) {
Team team = teamManager.isInTeam(player.getUniqueId());
Team team = null;
try {
team = teamManager.isInTeam(player.getUniqueId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
if (team == null) {
return CommandUtils.sendMessage(sender, "Vous n'êtes pas dans une team !", true);
}
return TeamUtils.quit(team, player);
try {
return TeamUtils.quit(team, player);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (args[0].equalsIgnoreCase("inventory")) {
Team team = teamManager.isInTeam(player.getUniqueId());
Team team = null;
try {
team = teamManager.isInTeam(player.getUniqueId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
if (team == null) {
return CommandUtils.sendMessage(sender, "Vous n'êtes pas dans une team !", true);
}
Expand All @@ -90,21 +125,37 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
for (int i = 1; i < args.length; i++) {
teamName.append(args[i]).append(" ");
}
if (teamManager.isInTeam(player.getUniqueId()) != null) {
return CommandUtils.sendMessage(sender, "Vous êtes déjà dans une team !", true);
try {
if (teamManager.isInTeam(player.getUniqueId()) != null) {
return CommandUtils.sendMessage(sender, "Vous êtes déjà dans une team !", true);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
if (teamName.length() > 16) {
return CommandUtils.sendMessage(sender, "Le nom de la team ne doit pas dépasser 16 caractères !", true);
}
Team createdTeam = teamManager.createTeam(player.getUniqueId(), teamName.toString().trim());
Team createdTeam = null;
try {
createdTeam = teamManager.createTeam(player.getUniqueId(), teamName.toString().trim());
} catch (SQLException e) {
e.printStackTrace();
CommandUtils.sendMessage(sender, "Erreur lors de la création de la team !", true);
}
assert createdTeam != null;
boolean couldAdd = createdTeam.addPlayer(player.getUniqueId());
if (!couldAdd) {
return CommandUtils.sendMessage(sender, "La team est déjà au complet !", true);
}
CommandUtils.sendMessage(sender, ChatColor.GREEN + "Vous avez créé la team " + createdTeam.getName() + " !", false);
}
if (args[0].equalsIgnoreCase("invite")) {
Team team = teamManager.isInTeam(player.getUniqueId());
Team team = null;
try {
team = teamManager.isInTeam(player.getUniqueId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
if (team == null) {
return CommandUtils.sendMessage(sender, "Vous n'êtes pas dans une team !", true);
}
Expand All @@ -115,8 +166,12 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if (target == null) {
return CommandUtils.sendMessage(sender, "Le joueur " + args[1] + " n'est pas en ligne !", true);
}
if (teamManager.isInTeam(target.getUniqueId()) != null) {
return CommandUtils.sendMessage(sender, "Le joueur " + target.getName() + " est déjà dans une team !", true);
try {
if (teamManager.isInTeam(target.getUniqueId()) != null) {
return CommandUtils.sendMessage(sender, "Le joueur " + target.getName() + " est déjà dans une team !", true);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
boolean couldInvite = teamManager.invite(target.getUniqueId(), team);
if (!couldInvite) {
Expand All @@ -127,7 +182,12 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
CommandUtils.sendMessage(target, "Pour accepter, faites " + ChatColor.GREEN + "/team accept", false);
}
if (args[0].equalsIgnoreCase("kick")) {
Team team = teamManager.isInTeam(player.getUniqueId());
Team team = null;
try {
team = teamManager.isInTeam(player.getUniqueId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
if (team == null) {
return CommandUtils.sendMessage(sender, "Vous n'êtes pas dans une team !", true);
}
Expand All @@ -136,7 +196,12 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
UUID target = team.getPlayerByUsername(args[1]);
if (target != null) {
MethodState state = team.removePlayer(target);
MethodState state = null;
try {
state = team.removePlayer(target);
} catch (SQLException e) {
throw new RuntimeException(e);
}
if (state == MethodState.VALID || state == MethodState.WARNING) CommandUtils.sendMessage(sender, "Le joueur " + args[1] + " a été kické de la team !", false);
if (state == MethodState.INVALID) return CommandUtils.sendMessage(sender, ChatColor.DARK_RED + "Impossible de kick, la team serait supprimée et il reste des items dans l'inventaire !", true);
Player targetPlayer = Bukkit.getPlayer(target);
Expand Down Expand Up @@ -166,20 +231,34 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
}
if (args[0].equalsIgnoreCase("invite")) {
TeamManager teamManager = AywenCraftPlugin.getInstance().getTeamManager();
Team team = teamManager.isInTeam(player.getUniqueId());
Team team = null;
try {
team = teamManager.isInTeam(player.getUniqueId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
if (team != null) {
List<String> players = new ArrayList<>();
for (Player onlinePlayer : player.getServer().getOnlinePlayers()) {
if (teamManager.isInTeam(onlinePlayer.getUniqueId()) == null) {
players.add(onlinePlayer.getName());
try {
if (teamManager.isInTeam(onlinePlayer.getUniqueId()) == null) {
players.add(onlinePlayer.getName());
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return players;
}

}
if (args[0].equalsIgnoreCase("kick")) {
Team team = AywenCraftPlugin.getInstance().getTeamManager().isInTeam(player.getUniqueId());
Team team = null;
try {
team = AywenCraftPlugin.getInstance().getTeamManager().isInTeam(player.getUniqueId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
if (team != null) {
List<String> players = new ArrayList<>();
for (UUID uuid : team.getPlayers()) {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/fr/communaywen/core/teams/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.sql.SQLException;
import java.util.*;

public class Team {
Expand Down Expand Up @@ -51,6 +52,10 @@ public boolean isIn(UUID player) {
}

public void openInventory(Player player) {
if(true) {
CommandUtils.sendMessage(player, "Fonctionnalité temporairement désativée :/", true);
return;
}
player.openInventory(inventory);
}

Expand Down Expand Up @@ -86,7 +91,7 @@ public boolean addPlayer(UUID player) {
return true;
}

public MethodState removePlayer(UUID player) {
public MethodState removePlayer(UUID player) throws SQLException {
if (players.size() - 1 == 0) {
players.remove(player);
if (!AywenCraftPlugin.getInstance().getTeamManager().deleteTeam(this)) {
Expand Down
Loading

0 comments on commit a0fe2bf

Please sign in to comment.