Skip to content

Commit

Permalink
Optimization: updated the code to use the isTeamRegistered methods. (#58
Browse files Browse the repository at this point in the history
)

Fix: fixed team-teleportation not working with some players in the offline (NPE).
Fix: fixed team-teleportation without the UHTeam.teleportTo method.
  • Loading branch information
AmauryCarrade committed Nov 27, 2014
1 parent bea5da9 commit 4e5208d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/me/azenet/UHPlugin/UHGameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public void start(CommandSender sender, Boolean slow) throws IllegalStateExcepti

String teamName = player.getName();

if(tm.getTeam(teamName) != null) { // Team registered
if(tm.isTeamRegistered(teamName)) {
// The probability of a conflict here is so small...
// I will not take this possibility into account.
teamName = player.getName() + this.random.nextInt(1000000);
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/me/azenet/UHPlugin/UHPluginCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -1684,9 +1684,7 @@ else if(sender instanceof BlockCommandSender) {
double y = Integer.parseInt(args[3]) + 0.5;
double z = Integer.parseInt(args[4]) + 0.5;

for(Player player : team.getOnlinePlayers()) {
player.teleport(new Location(targetWorld, x, y, z), TeleportCause.PLUGIN);
}
team.teleportTo(new Location(targetWorld, x, y, z));

return;
} catch(NumberFormatException e) {
Expand Down Expand Up @@ -1716,9 +1714,7 @@ else if(sender instanceof BlockCommandSender) {
sender.sendMessage(i.t("tp.targetOffline", args[2]));
}
else {
for(Player player : team.getOnlinePlayers()) {
player.teleport(target.getLocation(), TeleportCause.PLUGIN);
}
team.teleportTo(target.getLocation());
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/me/azenet/UHPlugin/UHTeam.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,10 @@ public void teleportTo(Location lo) {
Validate.notNull(lo, "The location cannot be null.");

for (UUID id : players) {
plugin.getServer().getPlayer(id).teleport(lo, TeleportCause.PLUGIN);
Player player = plugin.getServer().getPlayer(id);
if(player != null && player.isOnline()) {
player.teleport(lo, TeleportCause.PLUGIN);
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/azenet/UHPlugin/UHTeamManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public boolean isTeamRegistered(String name) {
* @throws IllegalArgumentException if a team with the same name already exists.
*/
public UHTeam addTeam(TeamColor color, String name) {
if(this.getTeam(name) != null) {
if(isTeamRegistered(name)) {
throw new IllegalArgumentException("There is already a team named " + name + " registered!");
}

Expand All @@ -103,11 +103,11 @@ public UHTeam addTeam(TeamColor color) {
color = generateColor(color);
String teamName = color.toString().toLowerCase();

if(getTeam(teamName) != null) { // Taken!
if(isTeamRegistered(teamName)) { // Taken!
Random rand = new Random();
do {
teamName = color.toString().toLowerCase() + rand.nextInt(1000);
} while(getTeam(teamName) != null);
} while(isTeamRegistered(teamName));
}

UHTeam team = new UHTeam(teamName, color, p);
Expand Down

0 comments on commit 4e5208d

Please sign in to comment.