-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoinTeamCommand.java
97 lines (85 loc) · 4.37 KB
/
JoinTeamCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package me.bretmounet.scavengerhunt;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
public class JoinTeamCommand implements CommandExecutor {
public static String teamName = null;
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Connection connection = Scavengerhunt.connection;
if (sender instanceof Player) {
List<String> playersTeam = new ArrayList<>();
List<Boolean> onTeam = new ArrayList<>();
List<String> fullTeams = new ArrayList<>();
List<String> teamsNotFull = new ArrayList<>();
Player player = (Player) sender;
UUID id = player.getUniqueId();
String playerName = id.toString();
teamName = args[0];
int maxTeamSize = Scavengerhunt.maxteamsize; // Variable used to set max team size
try {
TeamManager.getTeamNames();
TeamManager.getPlayersTeam(playerName, playersTeam, onTeam);
TeamManager.getFullTeams(teamsNotFull,maxTeamSize,fullTeams);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
if (!playersTeam.isEmpty()){
if (TeamManager.teamNames.contains(teamName) && !playersTeam.get(0).equals(teamName)) {
try {
Statement statement = connection.createStatement();
PreparedStatement pstmt = null;
String SQL = "DELETE FROM " + playersTeam.get(0) + " WHERE PlayerNames ='" + playerName + "'";
pstmt = connection.prepareStatement(SQL);
if(onTeam.get(0).equals(true) && !fullTeams.contains(teamName)){
TeamManager.removePlayerFromTeam(teamName,player);
pstmt.executeUpdate();
}
if (!fullTeams.contains(teamName)){
statement.executeUpdate("INSERT INTO " + teamName + " (PlayerNames, Points) VALUES ('" + playerName + "', 0);");
TeamManager.assignTeam(teamName,player);
player.setScoreboard(TeamManager.board);
player.sendMessage(ChatColor.GREEN + "You have Succesfully Joined a Team.");
}else{
player.sendMessage(ChatColor.RED + "Error: Team is full.");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}else if (playersTeam.get(0).equals(teamName)){
player.sendMessage(ChatColor.RED + "You are already on that team.");
}else {
player.sendMessage(ChatColor.RED + "Error: Unknown Team Name");
}
}else if (playersTeam.isEmpty() && TeamManager.teamNames.contains(teamName)){
try {
Statement statement = connection.createStatement();
statement.executeUpdate("INSERT INTO " + teamName + " (PlayerNames, Points) VALUES ('" + playerName + "', 0);");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
TeamManager.assignTeam(teamName,player);
player.setScoreboard(TeamManager.board);
player.sendMessage(ChatColor.GREEN + "You have Succesfully Joined a Team.");
}else if (!TeamManager.teamNames.contains(teamName)){
player.sendMessage(ChatColor.RED + "Error: Unknown Team Name");
}
}
return true;
}
public int getRandomElement(List<Integer> list)
{
Random rand = new Random(); // initializes rand
return list.get(rand.nextInt(list.size())); // randomly returns a value that is stored in the list.
}
}