Skip to content

Commit

Permalink
Fixed command running for player quit
Browse files Browse the repository at this point in the history
Added null checking to selfMessage
  • Loading branch information
Dart2112 committed Sep 24, 2024
1 parent 8cf9341 commit a0f5edd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/lapismc/afkplus/AFKPlus.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void onEnable() {
new Metrics(this, 424);
//Safely handle the stopping of AFKPlus in regard to player data
tasks.addShutdownTask(() -> {
players.values().forEach(AFKPlusPlayer::forceStopAFK);
players.values().forEach(player -> player.stopAFK(true));
players.clear();
});
tasks.addTask(Bukkit.getScheduler().runTaskTimerAsynchronously(this, getRepeatingTasks(), 20, 20));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/lapismc/afkplus/AFKPlusListeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void onPlayerJoin(PlayerJoinEvent e) {

@EventHandler
public void onPlayerQuit(PlayerQuitEvent e) {
plugin.getPlayer(e.getPlayer()).forceStopAFK();
plugin.getPlayer(e.getPlayer()).stopAFK(true);
}

@EventHandler
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/net/lapismc/afkplus/playerdata/AFKPlusPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,9 @@ public void stopAFK() {
* @param silent Skips broadcasting when true, used to cleanly exit afk when a player disconnects
*/
public void stopAFK(boolean silent) {
if (Bukkit.getPlayer(getUUID()) == null) {
//Player isn't online, stop here
//Don't run if the player isn't actually AFK since we are running commands and messages
if (!isAFK())
return;
}
//Get the command and broadcast message
String command = plugin.getConfig().getString("Commands.AFKStop");
String broadcastMessage = getMessage("Broadcast.Stop");
Expand Down Expand Up @@ -330,6 +329,7 @@ public void broadcastOthers(String msg) {
}
if (otherPlayers) {
for (Player p : Bukkit.getOnlinePlayers()) {
//Skip the player represented by this class, messages to them will be handled by selfMessage
if (p.getUniqueId().equals(getUUID()))
continue;
p.sendMessage(msg);
Expand All @@ -344,9 +344,13 @@ public void broadcastOthers(String msg) {
* @param msg The message to send
*/
public void selfMessage(String msg) {
//Get the config option for if players should be notified when they become AFK
boolean self = plugin.getConfig().getBoolean("Broadcast.Self");
if (!msg.isEmpty()) {
Bukkit.getPlayer(getUUID()).sendMessage(msg);
//Get the player so we can null check them, this ensures that they are online
Player p = Bukkit.getPlayer(uuid);
//Check that the message has content, the player should receive messages and that the player is online
if (!msg.isEmpty() && self && p != null) {
p.sendMessage(msg);
}
}

Expand Down

0 comments on commit a0f5edd

Please sign in to comment.