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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
package gregtech.common.misc.spaceprojects;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import net.minecraft.server.MinecraftServer;
import org.apache.commons.lang3.tuple.Pair;
import gregtech.api.util.GT_Recipe;
import gregtech.common.misc.spaceprojects.interfaces.ISpaceBody;
import gregtech.common.misc.spaceprojects.interfaces.ISpaceProject;
/**
* @author BlueWeabo
*/
public class SpaceProjectManager {
/**
* Do not use! Only meant to be used in SpaceProjectWorldSavedData.java
*/
public static Map<UUID, Map<Pair<ISpaceBody, String>, ISpaceProject>> spaceTeamProjects = new HashMap<>();
/**
* Do not use! Only meant to be used in SpaceProjectWorldSavedData.java Stores a Players UUID to the Leader UUID,
* players in lone groups give back their own UUID.
*/
public static Map<UUID, UUID> spaceTeams = new HashMap<>();
/**
* Stores all the locations to a hash map to be accessed easier instead of through an enum
*/
private static final HashMap<String, ISpaceBody> spaceLocations = new HashMap<>();
/**
* Stores all projects that have been made. Only adds them to this map if {@link #addProject(ISpaceProject)} has
* been used
*/
private static final Map<String, ISpaceProject> spaceProjects = new HashMap<>();
// #region Space Project Team Helper methods
/**
* Used to get a specific project of the team dependent on the location and the project's name
*/
public static ISpaceProject getTeamProject(UUID member, ISpaceBody location, String projectName) {
Map<Pair<ISpaceBody, String>, ISpaceProject> map = spaceTeamProjects.get(getLeader(member));
if (map == null) {
return null;
}
return map.get(Pair.of(location, projectName));
}
/**
* Makes a new Map for the teams if they don't have one. Adds a project to the team's project map.
*
* @param member Member of the team.
* @param location The location of where the project will belong to.
* @param projectName The name of the project being added.
* @param project Project which will be added to the team.
* @return Returns true when a project was added to the map of the player. Returns false otherwise.
*/
public static boolean addTeamProject(UUID member, ISpaceBody location, String projectName, ISpaceProject project) {
if (!spaceTeamProjects.containsKey(getLeader(member)) || spaceTeamProjects.get(getLeader(member)) == null) {
spaceTeamProjects.put(getLeader(member), new HashMap<>());
}
Map<Pair<ISpaceBody, String>, ISpaceProject> map = spaceTeamProjects.get(getLeader(member));
if (map.containsKey(Pair.of(location, projectName))) {
return false;
}
project.setProjectLocation(location);
map.put(Pair.of(location, projectName), project);
SpaceProjectWorldSavedData.INSTANCE.markDirty();
return true;
}
/**
* Check whether a team has a project or not
*
* @param member Member of the team
* @param project The project, which you are checking for. This only compares the internal names of the project.
* @return True if the team has said project, false otherwise
*/
public static boolean teamHasProject(UUID member, ISpaceProject project) {
Map<Pair<ISpaceBody, String>, ISpaceProject> map = spaceTeamProjects.get(getLeader(member));
if (map == null) {
return false;
}
return map.containsValue(project);
}
/**
* Used to handle when 2 players want to join together in a team. Player A can join player B's team. If player C
* gets an invite from A, C will join player B's team.
*
* @param teamMember Member which is joining the teamLeader
* @param teamLeader Leader of the party
*/
public static void putInTeam(UUID teamMember, UUID teamLeader) {
if (teamMember.equals(teamLeader)) {
spaceTeams.put(teamMember, teamLeader);
} else if (!spaceTeams.get(teamLeader)
.equals(teamLeader)) {
putInTeam(teamMember, spaceTeams.get(teamLeader));
} else {
spaceTeams.put(teamMember, teamLeader);
}
SpaceProjectWorldSavedData.INSTANCE.markDirty();
}
/**
* Used to give back the UUID of the team leader.
*
* @return The UUID of the team leader.
*/
public static UUID getLeader(UUID teamMember) {
checkOrCreateTeam(teamMember);
return spaceTeams.get(teamMember);
}
/**
* Used the multiblocks to check whether a given player has a team or not. If they don't have a team create one
* where they are their own leader.
*
* @param teamMember Member to check for.
*/
public static void checkOrCreateTeam(UUID teamMember) {
if (spaceTeams.containsKey(teamMember)) {
return;
}
spaceTeams.put(teamMember, teamMember);
SpaceProjectWorldSavedData.INSTANCE.markDirty();
}
/**
* Will give back all the projects a team has made or is making.
*
* @param member UUID of the team member, used to find the leader of the team.
* @return All the projects a team has.
*/
public static Collection<ISpaceProject> getTeamSpaceProjects(UUID member) {
Map<Pair<ISpaceBody, String>, ISpaceProject> map = spaceTeamProjects.get(getLeader(member));
if (map == null) {
return null;
}
return map.values();
}
/**
* Getting the project of a Team or a new copy.
*
* @param member UUID of the team member, which is used to find the team leader.
* @param projectName The name of the project, which needs to be found.
* @param location The location at which the project is found at.
* @return the project that the team has or a copy of a project.
*/
public static ISpaceProject getTeamProjectOrCopy(UUID member, String projectName, ISpaceBody location) {
Map<Pair<ISpaceBody, String>, ISpaceProject> map = spaceTeamProjects.get(getLeader(member));
if (map == null) {
return getProject(projectName);
}
return map.getOrDefault(Pair.of(location, projectName), getProject(projectName));
}
// #endregion
// #region Project Helper methods
/**
* Used to add projects to the internal map.
*
* @param project Newly created project.
*/
public static void addProject(ISpaceProject project) {
spaceProjects.put(project.getProjectName(), project);
GT_Recipe.GT_Recipe_Map.sFakeSpaceProjectRecipes.add(
new GT_Recipe.GT_Recipe_Map.GT_FakeSpaceProjectRecipe(
false,
project.getTotalItemsCost(),
project.getTotalFluidsCost(),
project.getProjectBuildTime(),
(int) project.getProjectVoltage(),
project.getTotalStages(),
project.getProjectName()));
}
/**
* @param projectName Internal name of the project.
* @return a copy of the stored project.
*/
public static ISpaceProject getProject(String projectName) {
ISpaceProject tProject = spaceProjects.get(projectName);
return tProject != null ? tProject.copy() : null;
}
/**
* Should only be used for GUIs!
*
* @return The Map that the projects are stored at.
*/
public static Map<String, ISpaceProject> getProjectsMap() {
return spaceProjects;
}
/**
* Should only be used for GUIs!
*
* @return A Collection of all the projects contained in the map.
*/
public static Collection<ISpaceProject> getAllProjects() {
return spaceProjects.values();
}
// #endregion
// #region Location Helper methods
/**
* Adds a location to the internal map. For it to be used later
*
* @param location to add to the internal map
*/
public static void addLocation(ISpaceBody location) {
spaceLocations.put(location.getName(), location);
}
/**
*
* @return a Collection of all locations, which have been registered.
*/
public static Collection<ISpaceBody> getLocations() {
return spaceLocations.values();
}
/**
*
* @return a Collection fo all location names, which have been registered
*/
public static Collection<String> getLocationNames() {
return spaceLocations.keySet();
}
/**
*
* @param locationName Name used to search for the location
* @return The location, which has been registered with said name
*/
public static ISpaceBody getLocation(String locationName) {
return spaceLocations.get(locationName);
}
// #endregion
// #region General Helper methods
/**
* Gets the UUID using the player's username
*/
public static UUID getPlayerUUIDFromName(String playerName) {
return MinecraftServer.getServer()
.func_152358_ax()
.func_152655_a(playerName)
.getId();
}
/**
* Gets the player's name using their UUID
*/
public static String getPlayerNameFromUUID(UUID playerUUID) {
return MinecraftServer.getServer()
.func_152358_ax()
.func_152652_a(playerUUID)
.getName();
}
// #endregion
}
|