3838import in .twizmwaz .cardinal .module .ModuleEntry ;
3939import in .twizmwaz .cardinal .module .ModuleError ;
4040import in .twizmwaz .cardinal .module .region .Region ;
41- import in .twizmwaz .cardinal .module .region .RegionException ;
4241import in .twizmwaz .cardinal .module .region .RegionModule ;
42+ import in .twizmwaz .cardinal .module .region .type .modifications .PointProviderRegion ;
4343import in .twizmwaz .cardinal .module .team .Team ;
4444import in .twizmwaz .cardinal .module .team .TeamModule ;
4545import in .twizmwaz .cardinal .playercontainer .PlayingPlayerContainer ;
4646import in .twizmwaz .cardinal .util .ListUtil ;
4747import in .twizmwaz .cardinal .util .Numbers ;
4848import in .twizmwaz .cardinal .util .ParseUtil ;
49+ import in .twizmwaz .cardinal .util .Players ;
4950import lombok .NonNull ;
5051import org .bukkit .Bukkit ;
5152import org .bukkit .entity .Player ;
5455import org .bukkit .event .Listener ;
5556import org .bukkit .event .player .PlayerInitialSpawnEvent ;
5657import org .bukkit .event .player .PlayerJoinEvent ;
58+ import org .bukkit .util .Vector ;
5759import org .jdom2 .Document ;
5860import org .jdom2 .Element ;
5961import org .jdom2 .located .Located ;
6062
6163import java .util .List ;
6264import java .util .Map ;
65+ import java .util .stream .Collectors ;
6366
6467@ ModuleEntry (depends = {TeamModule .class /*, KitModule.class, FilterModule.class*/ , RegionModule .class })
6568public class SpawnModule extends AbstractModule implements Listener {
@@ -81,9 +84,11 @@ public boolean loadMatch(@NonNull Match match) {
8184 for (Element spawnsElement : document .getRootElement ().getChildren ("spawns" )) {
8285 for (Element spawnElement : spawnsElement .getChildren ("spawn" )) {
8386 Spawn spawn = parseSpawn (match , spawnElement , spawnsElement );
84- spawns .add (spawn );
85- if (spawn .isDefaultSpawn ()) {
86- defaultPresent = true ;
87+ if (spawn != null ) {
88+ spawns .add (spawn );
89+ if (spawn .isDefaultSpawn ()) {
90+ defaultPresent = true ;
91+ }
8792 }
8893 }
8994 }
@@ -129,42 +134,73 @@ private Spawn parseSpawn(Match match, Element... elements) {
129134 }
130135
131136 List <Region > regions = Lists .newArrayList ();
132- List <Element > working ;
133- if (elements [0 ].getChild ("regions" ) == null ) {
134- working = elements [0 ].getChildren ();
135- } else {
136- working = elements [0 ].getChild ("regions" ).getChildren ();
137+
138+ Region spawnRegion = getPointProvider (match , "region" , true , elements );
139+ if (spawnRegion != null ) {
140+ regions .add (spawnRegion );
137141 }
138- for (Element regionElement : working ) {
139- Located located = (Located ) regionElement ;
140- Region region ;
141- try {
142- region = Cardinal .getModule (RegionModule .class ).getRegion (match , regionElement );
143- } catch (RegionException e ) {
144- errors .add (new ModuleError (this , match .getMap (), new String []{
145- RegionModule .getRegionError (e , "region" , (defaultSpawn ? "default" : team .getName ()) + " spawn" ),
146- "Element at line " + located .getLine () + ", column " + located .getColumn ()
147- }, false ));
148- continue ;
142+ for (Element regionElement : elements [0 ].getChildren ("region" )) {
143+ Region region = getPointProvider (match , "id" , false , ParseUtil .addElement (regionElement , elements ));
144+ if (region != null ) {
145+ regions .add (region );
149146 }
150- if (region == null ) {
147+ }
148+ if (regions .size () == 0 ) {
149+ Located located = (Located ) elements [0 ];
150+ errors .add (new ModuleError (this , match .getMap (), new String []{
151+ "No regions specified for spawn at line " + located .getLine () + ", column " + located .getColumn (),
152+ }, false ));
153+ // Prevent errors later trying to get a spawn region from an empty list.
154+ return null ;
155+ }
156+ return new Spawn (defaultSpawn , team , safe , sequential , spread , exclusive , persistent , regions );
157+ }
158+
159+ private PointProviderRegion getPointProvider (Match match , String attr , boolean allowMissing , Element ... elements ) {
160+ Located located = (Located ) elements [0 ];
161+ String attribute = ParseUtil .getFirstAttribute (attr , elements );
162+ if (attribute == null ) {
163+ if (!allowMissing ) {
151164 errors .add (new ModuleError (this , match .getMap (), new String []{
152- "Invalid region specified for a spawn" ,
165+ "Missing \" " + attr + " \" attribute for spawn region " ,
153166 "Element at line " + located .getLine () + ", column " + located .getColumn ()
154167 }, false ));
155- continue ;
156168 }
157- if (!region .isRandomizable ()) {
169+ return null ;
170+ }
171+ Region region = Cardinal .getModule (RegionModule .class ).getRegionById (match , attribute );
172+ if (region == null ) {
173+ errors .add (new ModuleError (this , match .getMap (), new String []{
174+ "Invalid region specified for a spawn" ,
175+ "Element at line " + located .getLine () + ", column " + located .getColumn ()
176+ }, false ));
177+ return null ;
178+ }
179+ if (!region .isRandomizable ()) {
180+ errors .add (new ModuleError (this , match .getMap (), new String []{
181+ "Region specified for spawn spawn must be randomizable" ,
182+ "Element at line " + located .getLine () + ", column " + located .getColumn ()
183+ }, false ));
184+ return null ;
185+ }
186+
187+ String rawAngle = ParseUtil .getFirstAttribute ("angle" , elements );
188+ if (rawAngle != null ) {
189+ Vector angle = Numbers .getVector (rawAngle );
190+ if (angle == null ) {
158191 errors .add (new ModuleError (this , match .getMap (), new String []{
159- "Region specified for " + ( defaultSpawn ? "default" : team . getName ()) + " spawn must be randomizable " ,
192+ "Invalid angle attribute specified " ,
160193 "Element at line " + located .getLine () + ", column " + located .getColumn ()
161194 }, false ));
162- continue ;
195+ return null ;
163196 }
164- regions . add (region );
197+ return new PointProviderRegion (region , angle );
165198 }
166199
167- return new Spawn (defaultSpawn , team , safe , sequential , spread , exclusive , persistent , regions );
200+ float yaw = Numbers .parseFloat (ParseUtil .getFirstAttribute ("yaw" , elements ), 0 ),
201+ pitch = Numbers .parseFloat (ParseUtil .getFirstAttribute ("pitch" , elements ), 0 );
202+
203+ return new PointProviderRegion (region , yaw , pitch );
168204 }
169205
170206 /**
@@ -175,8 +211,7 @@ private Spawn parseSpawn(Match match, Element... elements) {
175211 @ EventHandler (priority = EventPriority .HIGH )
176212 public void onPlayerInitialSpawn (PlayerInitialSpawnEvent event ) {
177213 Match match = Cardinal .getMatch (event .getPlayer ());
178- List <Region > regions = getDefaultSpawn (match ).getRegions ();
179- event .setSpawnLocation (ListUtil .getRandom (regions ).getRandomPoint ().toLocation (match .getWorld ()));
214+ event .setSpawnLocation (getDefaultSpawn (match ).getSpawnPoint ());
180215 }
181216
182217 @ EventHandler
@@ -235,9 +270,9 @@ public void onMatchLoad(MatchLoadCompleteEvent event) {
235270 */
236271 @ EventHandler
237272 public void onCardinalRespawn (CardinalRespawnEvent event ) {
238- Player player = event .getPlayer ();
239- List < Region > regions = event .getSpawn ().getRegions ( );
240- player . teleport ( ListUtil . getRandom ( regions ). getRandomPoint ().toLocation ( player . getWorld () ));
273+ Players . reset ( event .getPlayer () );
274+ // event.getSpawn().getKit().apply(event.getPlayer(), false );
275+ event . getPlayer (). teleport ( event . getSpawn ().getSpawnPoint ( ));
241276 }
242277
243278 /**
@@ -257,18 +292,17 @@ public Spawn getDefaultSpawn(@NonNull Match match) {
257292 }
258293
259294 /**
260- * Gets a list of {@link Spawn} based on a team.
295+ * Gets a list of {@link Spawn} based on a team. If no spawn is found, spawns without a team will be returned.
261296 *
262297 * @param container The container for the spawns.
263298 * @return The list of spawns.
264299 */
265300 private List <Spawn > getSpawns (@ NonNull Match match , @ NonNull PlayingPlayerContainer container ) {
266- List <Spawn > results = Lists .newArrayList ();
267-
268- for (Spawn spawn : spawns .get (match )) {
269- if (container .equals (spawn .getTeam ())) {
270- results .add (spawn );
271- }
301+ List <Spawn > results =
302+ spawns .get (match ).stream ().filter (spawn -> container .equals (spawn .getTeam ())).collect (Collectors .toList ());
303+ if (results .size () == 0 ) {
304+ results = spawns .get (match ).stream ().filter (
305+ spawn -> !spawn .isDefaultSpawn () && spawn .getTeam () == null ).collect (Collectors .toList ());
272306 }
273307 return results ;
274308 }
0 commit comments