/
WorldGroup.java
247 lines (219 loc) · 7.8 KB
/
WorldGroup.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
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
package com.onarandombox.multiverseinventories;
import com.onarandombox.multiverseinventories.share.Sharable;
import com.onarandombox.multiverseinventories.share.Sharables;
import com.onarandombox.multiverseinventories.share.Shares;
import com.onarandombox.multiverseinventories.profile.container.ProfileContainer;
import org.bukkit.World;
import org.bukkit.event.EventPriority;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
public final class WorldGroup {
private final MultiverseInventories plugin;
private final String name;
private final HashSet<String> worlds = new HashSet<>();
private final Shares shares = Sharables.noneOf();
private String spawnWorld = null;
private EventPriority spawnPriority = EventPriority.NORMAL;
WorldGroup(final MultiverseInventories inventories, final String name) {
this.plugin = inventories;
this.name = name;
}
/**
* Get the name of this World Group.
*
* @return Name of this World Group.
*/
public String getName() {
return this.name;
}
/**
* Adds a world to this world group and updates it in the Config.
*
* @param worldName The name of the world to add.
*/
public void addWorld(String worldName) {
this.addWorld(worldName, true);
}
/**
* Adds a world to this world group and optionally updates it in the Config.
*
* @param worldName The name of the world to add.
* @param updateConfig True to update this group in the config.
*/
public void addWorld(String worldName, boolean updateConfig) {
this.getWorlds().add(worldName.toLowerCase());
if (updateConfig) {
plugin.getGroupManager().updateGroup(this);
}
}
/**
* Convenience method to add a {@link org.bukkit.World} to this World Group.
*
* @param world The world to add.
*/
public void addWorld(World world) {
this.addWorld(world.getName());
}
/**
* Convenience method to add multiple worlds to this World Group and updates it in the Config.
*
* @param worlds A collections of worlds to add.
*/
public void addWorlds(Collection<String> worlds) {
this.addWorlds(worlds, true);
}
/**
* Convenience method to add multiple worlds to this World Group.
*
* @param worlds A collections of worlds to add.
* @param updateConfig True to update this group in the config.
*/
public void addWorlds(Collection<String> worlds, boolean updateConfig) {
worlds.forEach(worldName -> this.addWorld(worldName, false));
if (updateConfig) {
this.plugin.getGroupManager().updateGroup(this);
}
}
/**
* Removes a world from this world group and updates the group in the Config.
*
* @param worldName The name of the world to remove.
*/
public void removeWorld(String worldName) {
this.removeWorld(worldName, true);
}
/**
* Removes a world from this world group and optionally updates it in the Config.
*
* @param worldName The name of the world to remove.
* @param updateConfig True to update this group in the config.
*/
public void removeWorld(String worldName, boolean updateConfig) {
this.getWorlds().remove(worldName.toLowerCase());
if (updateConfig) {
plugin.getGroupManager().updateGroup(this);
}
}
/**
* Convenience method to remove a {@link org.bukkit.World} from this World Group.
*
* @param world The world to remove.
*/
public void removeWorld(World world) {
this.removeWorld(world.getName());
}
/**
* Remove all the worlds in this World Group.
*/
public void removeAllWorlds() {
this.removeAllWorlds(true);
}
/**
* Remove all the worlds in this World Group.
*
* @param updateConfig True to update this group in the config.
*/
public void removeAllWorlds(boolean updateConfig) {
this.worlds.clear();
if (updateConfig) {
this.plugin.getGroupManager().updateGroup(this);
}
}
/**
* Retrieves all of the worlds in this World Group.
*
* @return The worlds of this World Group.
*/
public Set<String> getWorlds() {
return this.worlds;
}
/**
* Checks if this group is sharing sharable. This will check both shares and negative shares of the group.
* This is the preferred method for checking if a group shares something as shares may contain ALL shares while
* ones indicated in negative shares means those aren't actually shared.
*
* @param sharable Sharable to check if sharing.
* @return true is the sharable is shared for this group.
*/
public boolean isSharing(Sharable sharable) {
return getShares().isSharing(sharable);
}
/**
* Retrieves the shares for this World Group. Any changes to this group must be subsequently saved to the data
* source for the changes to be permanent.
*
* @return The shares for this World Group.
*/
public Shares getShares() {
return this.shares;
}
/**
* @param worldName Name of world to check for.
* @return True if specified world is part of this group.
*/
public boolean containsWorld(String worldName) {
return this.getWorlds().contains(worldName.toLowerCase());
}
/**
* @return The name of the world that will be used as the spawn for this group.
* Or null if no world was specified as the group spawn world.
*/
public String getSpawnWorld() {
return this.spawnWorld;
}
/**
* @param worldName The name of the world to set this groups spawn to.
*/
public void setSpawnWorld(String worldName) {
this.spawnWorld = worldName.toLowerCase();
}
/**
* @return The priority for the respawn event that this spawn will act on.
*/
public EventPriority getSpawnPriority() {
return this.spawnPriority;
}
/**
* @param priority The priority that will be used for respawning the player at
* this group's spawn location if there is one set.
*/
public void setSpawnPriority(EventPriority priority) {
this.spawnPriority = priority;
}
/**
* Is this a default group.
*
* @return true if this is the default group.
*/
public boolean isDefault() {
return AbstractWorldGroupManager.DEFAULT_GROUP_NAME.equals(getName());
}
/**
* Returns the profile container for this group.
*
* @return the profile container for this group.
*/
public ProfileContainer getGroupProfileContainer() {
return plugin.getGroupProfileContainerStore().getContainer(getName());
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(this.getName()).append(": {Worlds: [");
String[] worldsString = this.getWorlds().toArray(new String[this.getWorlds().size()]);
for (int i = 0; i < worldsString.length; i++) {
if (i != 0) {
builder.append(", ");
}
builder.append(worldsString[i]);
}
builder.append("], Shares: [").append(this.getShares().toString()).append("]");
if (this.getSpawnWorld() != null) {
builder.append(", Spawn World: ").append(this.getSpawnWorld());
builder.append(", Spawn Priority: ").append(this.getSpawnPriority().toString());
}
builder.append("}");
return builder.toString();
}
}