Skip to content

Commit f333e8c

Browse files
Pablete1234twizmwazin
authored andcommitted
Fix spawns, convert to proto 2.0.0 syntax
1 parent c037654 commit f333e8c

5 files changed

Lines changed: 136 additions & 52 deletions

File tree

src/main/java/in/twizmwaz/cardinal/module/region/RegionModule.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ public boolean loadMatch(@NonNull Match match) {
9999
continue;
100100
}
101101
try {
102-
String id = regionElement.getAttributeValue("id");
102+
Map.Entry<String, String> id =
103+
ParseUtil.getFirstNonNullAttributeValue(regionElement, Lists.newArrayList("id", "name"));
103104
if (id != null) {
104-
regions.put(id, getRegion(match, regionElement));
105+
regions.put(id.getValue(), getRegion(match, regionElement));
105106
}
106107
} catch (RegionException e) {
107108
errors.add(new ModuleError(this, match.getMap(), new String[]{getRegionError(e, "region", null)}, false));

src/main/java/in/twizmwaz/cardinal/module/region/type/modifications/PointProviderRegion.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,41 @@
3434
public class PointProviderRegion extends AbstractRegion {
3535

3636
private final Region region;
37-
private Vector direction;
37+
private Vector angle;
3838
private final float yaw;
3939
private final float pitch;
4040

4141
/**
42-
* @param region The region to get points from.
43-
* @param direction A vector to set direction.
44-
* @param yaw Yaw for direction.
45-
* @param pitch Pitch for direction.
42+
* @param region The region to get points from.
43+
* @param angle A vector to set direction.
44+
* @param yaw Yaw for direction.
45+
* @param pitch Pitch for direction.
4646
*/
47-
public PointProviderRegion(@NonNull Region region, Vector direction, float yaw, float pitch) {
47+
public PointProviderRegion(@NonNull Region region, Vector angle, float yaw, float pitch) {
4848
super(region.getBounds());
4949
this.region = region;
50+
this.angle = angle;
5051
this.yaw = yaw;
5152
this.pitch = pitch;
5253
}
5354

55+
/**
56+
* @param region The region to get points from.
57+
* @param angle A vector to set direction.
58+
*/
59+
public PointProviderRegion(@NonNull Region region, Vector angle) {
60+
this(region, angle, 0, 0);
61+
}
62+
63+
/**
64+
* @param region The region to get points from.
65+
* @param yaw Yaw for direction.
66+
* @param pitch Pitch for direction.
67+
*/
68+
public PointProviderRegion(@NonNull Region region, float yaw, float pitch) {
69+
this(region, null, yaw, pitch);
70+
}
71+
5472
@Override
5573
public boolean isRandomizable() {
5674
return true;
@@ -64,8 +82,8 @@ public boolean isBounded() {
6482
@Override
6583
public Vector getRandomPoint() {
6684
Location location = region.getRandomPoint().toLocation(getBounds().getMatch().getWorld(), yaw, pitch);
67-
if (direction != null) {
68-
location.setDirection(direction);
85+
if (angle != null) {
86+
location.setDirection(angle);
6987
}
7088
return location;
7189
}

src/main/java/in/twizmwaz/cardinal/module/spawn/Spawn.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@
2727

2828
import in.twizmwaz.cardinal.module.region.Region;
2929
import in.twizmwaz.cardinal.module.team.Team;
30-
import lombok.AllArgsConstructor;
30+
import in.twizmwaz.cardinal.util.ListUtil;
3131
import lombok.Getter;
32+
import lombok.RequiredArgsConstructor;
33+
import org.bukkit.Location;
34+
import org.bukkit.util.Vector;
3235

3336
import java.util.List;
3437

3538
@Getter
36-
@AllArgsConstructor
39+
@RequiredArgsConstructor
3740
public class Spawn {
3841

3942
private final boolean defaultSpawn;
@@ -48,4 +51,19 @@ public class Spawn {
4851
// private final Filter filter;
4952
private final List<Region> regions;
5053

54+
private int position = 0;
55+
56+
public Location getSpawnPoint() {
57+
Vector result;
58+
if (sequential) {
59+
result = regions.get(position).getRandomPoint();
60+
if (position++ == regions.size()) {
61+
position = 0;
62+
}
63+
} else {
64+
result = ListUtil.getRandom(regions).getRandomPoint();
65+
}
66+
return ((Location) result);
67+
}
68+
5169
}

src/main/java/in/twizmwaz/cardinal/module/spawn/SpawnModule.java

Lines changed: 74 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@
3838
import in.twizmwaz.cardinal.module.ModuleEntry;
3939
import in.twizmwaz.cardinal.module.ModuleError;
4040
import in.twizmwaz.cardinal.module.region.Region;
41-
import in.twizmwaz.cardinal.module.region.RegionException;
4241
import in.twizmwaz.cardinal.module.region.RegionModule;
42+
import in.twizmwaz.cardinal.module.region.type.modifications.PointProviderRegion;
4343
import in.twizmwaz.cardinal.module.team.Team;
4444
import in.twizmwaz.cardinal.module.team.TeamModule;
4545
import in.twizmwaz.cardinal.playercontainer.PlayingPlayerContainer;
4646
import in.twizmwaz.cardinal.util.ListUtil;
4747
import in.twizmwaz.cardinal.util.Numbers;
4848
import in.twizmwaz.cardinal.util.ParseUtil;
49+
import in.twizmwaz.cardinal.util.Players;
4950
import lombok.NonNull;
5051
import org.bukkit.Bukkit;
5152
import org.bukkit.entity.Player;
@@ -54,12 +55,14 @@
5455
import org.bukkit.event.Listener;
5556
import org.bukkit.event.player.PlayerInitialSpawnEvent;
5657
import org.bukkit.event.player.PlayerJoinEvent;
58+
import org.bukkit.util.Vector;
5759
import org.jdom2.Document;
5860
import org.jdom2.Element;
5961
import org.jdom2.located.Located;
6062

6163
import java.util.List;
6264
import java.util.Map;
65+
import java.util.stream.Collectors;
6366

6467
@ModuleEntry(depends = {TeamModule.class/*, KitModule.class, FilterModule.class*/, RegionModule.class})
6568
public 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
}

src/main/java/in/twizmwaz/cardinal/util/ParseUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,17 @@ public static Map.Entry<String, String> getFirstNonNullAttributeValue(Element el
6565
return null;
6666
}
6767

68+
/**
69+
* Adds an element on the front of an array of elements.
70+
* @param element The element to add.
71+
* @param elements The element array.
72+
* @return A new array with the element and the elements.
73+
*/
74+
public static Element[] addElement(Element element, Element... elements) {
75+
Element[] newElements = new Element[elements.length + 1];
76+
newElements[0] = element;
77+
System.arraycopy(elements, 0, newElements, 1, elements.length);
78+
return newElements;
79+
}
80+
6881
}

0 commit comments

Comments
 (0)