-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeamManager.java
More file actions
177 lines (144 loc) · 5.95 KB
/
TeamManager.java
File metadata and controls
177 lines (144 loc) · 5.95 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package fr.dofsekai.sekonomy.managers;
import fr.dofsekai.sekonomy.Sekonomy;
import fr.dofsekai.sekonomy.models.Team;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class TeamManager {
private static TeamManager instance;
private final Sekonomy plugin;
private final File teamFile;
private final FileConfiguration teamConfig;
private final Map<String, Team> teams;
private TeamManager() {
this.plugin = Sekonomy.get();
teamFile = new File(plugin.getDataFolder(), "teams.yml");
teamConfig = YamlConfiguration.loadConfiguration(teamFile);
teams = new HashMap<>();
}
public static TeamManager get() {
if (instance == null) instance = new TeamManager();
return instance;
}
public void loadTeams() {
ConfigurationSection teamsSection;
try {
teamsSection = teamConfig.getConfigurationSection("teams");
if (teamsSection == null) {
plugin.getLogger().warning("No teams defined in teams.yml!");
return;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
for (String teamName: teamsSection.getKeys(false)) {
ConfigurationSection teamData = teamsSection.getConfigurationSection(teamName);
if (teamData == null) {
plugin.getLogger().warning("Team " + teamName + " not found in teams.yml!");
continue;
}
String leaderString = teamData.getString("leader");
if (leaderString == null) {
plugin.getLogger().warning("Team " + teamName + " leader not found in teams.yml!");
continue;
}
OfflinePlayer leader = Bukkit.getOfflinePlayer(leaderString);
List<String> members = teamData.getStringList("members");
if (members.isEmpty()) {
plugin.getLogger().warning("Team " + teamName + " members not found in teams.yml!");
continue;
}
Team team = new Team(teamName, leader);
members.forEach(member -> team.addMember(Bukkit.getOfflinePlayer(member)));
Location home = getHome(teamData.getConfigurationSection("home"));
if (home != null) team.setHome(home);
teams.put(teamName, team);
}
plugin.getLogger().info("Teams successfully loaded from teams.yml!");
}
private Location getHome(ConfigurationSection homeSection) {
if (homeSection == null || !homeSection.contains("x") || !homeSection.contains("world")) {
return null;
}
World world = Bukkit.getWorld(homeSection.getString("world"));
if (world == null) return null;
double x = homeSection.getDouble("x");
double y = homeSection.getDouble("y");
double z = homeSection.getDouble("z");
float pitch = (float) homeSection.getDouble("pitch");
float yaw = (float) homeSection.getDouble("yaw");
return new Location(world, x, y, z, yaw, pitch);
}
public void setHome(Location home, Team team) {
team.setHome(home);
if (home != null) {
teamConfig.set("teams." + team.getName() + ".home.world", home.getWorld().getName());
teamConfig.set("teams." + team.getName() + ".home.x", home.getX());
teamConfig.set("teams." + team.getName() + ".home.y", home.getY());
teamConfig.set("teams." + team.getName() + ".home.z", home.getZ());
teamConfig.set("teams." + team.getName() + ".home.pitch", home.getPitch());
teamConfig.set("teams." + team.getName() + ".home.yaw", home.getYaw());
}
save(team);
}
public void deleteHome(Team team) {
team.setHome(null);
teamConfig.set("teams." + team.getName() + ".home", null);
save(team);
}
public void save(Team team) {
try {
teams.put(team.getName(), team);
teamConfig.save(teamFile);
} catch (IOException e) {
plugin.getLogger().severe("Failed to save team " + team.getName() + "!");
}
}
public Team getTeam(OfflinePlayer offlinePlayer) {
return teams.values().stream()
.filter(team -> team.getMembers().contains(offlinePlayer))
.findAny()
.orElse(null);
}
public Team getTeam(String teamName) {
return teams.values().stream()
.filter(team -> team.getName().equalsIgnoreCase(teamName))
.findAny()
.orElse(null);
}
public double getTeamMoney(Team team) {
double totalMoney = 0;
for (OfflinePlayer offlinePlayer: team.getMembers()) {
String balance = PlaceholderAPI.setPlaceholders(offlinePlayer, "%xconomy_balance_value%");
totalMoney += Double.parseDouble(balance);
}
return totalMoney;
}
public List<Team> getTopTeams(int topN) {
return teams.values().stream()
.sorted(Comparator.comparingDouble(this::getTeamMoney).reversed())
.limit(topN)
.collect(Collectors.toList());
}
public boolean isSetHomeEnabled() {
return plugin.getConfig().getBoolean("commands.teams.sethome.enabled", true);
}
public boolean isDelHomeEnabled() {
return plugin.getConfig().getBoolean("commands.teams.delhome.enabled", true);
}
public boolean isHomeEnabled() {
return plugin.getConfig().getBoolean("commands.teams.home.enabled", true);
}
}