Skip to content
This repository has been archived by the owner on Apr 6, 2024. It is now read-only.

Commit

Permalink
Added more automatic removals from the AFK list
Browse files Browse the repository at this point in the history
Players are now being automatically removed from the afklist when they move or they write something in chat

Also fixed the additional message that a player is back when he only was removed from the AFKlist due to leaving the server (Before sending a message: checking if the player is still online)
  • Loading branch information
kiriDevs committed Dec 16, 2020
1 parent b012132 commit 88c18c6
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# kiriCore
`NOTE: The kiriCore is currently being rewritten. Please check back later for more information or a finished version!`

[Current version: 0.5.0]
[Current version: 0.6.0]

### Commands:
- /afk
Expand Down
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
main: de.kiridevs.kiricore.main.Main

name: kiriCore
version: 0.5.0
version: 0.6.0
api-version: 1.16
author: kiriDevs

Expand Down
40 changes: 40 additions & 0 deletions src/de/kiridevs/kiricore/listeners/LISTonAsyncPlayerChatEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.kiridevs.kiricore.listeners;

import de.kiridevs.kiricore.main.Main;
import de.kiridevs.kiricore.managers.AfkManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;

import java.util.concurrent.Callable;

public class LISTonAsyncPlayerChatEvent implements Listener {
@EventHandler
public void onAsyncPlayerChatEvent(AsyncPlayerChatEvent event) {
Player player = event.getPlayer();

if (!(AfkManager.isAfk(player))) { return; } // Skip all events where the player isn't AFK to enhance speed

event.setCancelled(true); // Cancel event so the "player is back" message is sent before the actual chat message is

if (event.isAsynchronous()) {
// Event was caused by an actual player -> async
// Running sync method AfkManager.markBack(player) from async context: (Needed as another event is fired)
Bukkit.getScheduler().callSyncMethod(Main.getPlugin(), new Callable<>() {
@Override
public Object call() {
AfkManager.markBack(player);
return null;
}
});
} else {
// Event was caused by another Plugin forcing a player to speak -> sync
AfkManager.markBack(player);
}

event.setCancelled(false); // Un-cancel event so the players message is sent

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public void onPlayerChangeAfkStatus(PlayerChangeAfkStatusEvent event) {
ArrayList<String> completionList = new ArrayList<>();
completionList.add(playerName);

if (!(event.getPlayer().isOnline())) { return; } // Don't allow messages to be sent when player is no longer online (changed AFK status by leaving)

if (isAfkNow) { messageService.broadcastMessage("nowafk", completionList); }
else { messageService.broadcastMessage("nolongerafk", completionList); }
}
Expand Down
15 changes: 15 additions & 0 deletions src/de/kiridevs/kiricore/listeners/LISTonPlayerMoveEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.kiridevs.kiricore.listeners;

import de.kiridevs.kiricore.managers.AfkManager;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;

public class LISTonPlayerMoveEvent implements Listener {
@EventHandler
public void onPlayerMoveEvent(PlayerMoveEvent event) {
Player player = event.getPlayer();
AfkManager.markBack(player);
}
}
10 changes: 10 additions & 0 deletions src/de/kiridevs/kiricore/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
import de.kiridevs.kiricore.commands.CMDafkList;
import de.kiridevs.kiricore.commands.CMDisAfk;
import de.kiridevs.kiricore.commands.CMDrename;
import de.kiridevs.kiricore.listeners.LISTonAsyncPlayerChatEvent;
import de.kiridevs.kiricore.listeners.LISTonPlayerChangeAfkStatus;
import de.kiridevs.kiricore.listeners.LISTonPlayerLeave;
import de.kiridevs.kiricore.listeners.LISTonPlayerMoveEvent;
import de.kiridevs.kiricore.managers.MessageService;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("unused") // Class is used by Spigot
public class Main extends JavaPlugin {
private static Main plugin;

Prefix successPrefix = new Prefix("kiriCore", "2", "a");
Prefix errorPrefix = new Prefix("kiriCore", "2", "c");
Prefix infoPrefix = new Prefix("kiriCore", "2", "b");
Expand All @@ -26,6 +31,8 @@ public class Main extends JavaPlugin {
@SuppressWarnings("ConstantConditions")
@Override
public void onEnable() {
plugin = this;

// MessageService creation
prefixMap.put("success", successPrefix);
prefixMap.put("error", errorPrefix);
Expand All @@ -48,7 +55,10 @@ public void onEnable() {

// Listener registration
Bukkit.getPluginManager().registerEvents(new LISTonPlayerLeave(), this);
Bukkit.getPluginManager().registerEvents(new LISTonPlayerMoveEvent(), this);
Bukkit.getPluginManager().registerEvents(new LISTonAsyncPlayerChatEvent(), this);
Bukkit.getPluginManager().registerEvents(new LISTonPlayerChangeAfkStatus(messageService), this);
}

public static Main getPlugin() { return plugin; }
}
1 change: 0 additions & 1 deletion src/de/kiridevs/kiricore/managers/AfkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public static boolean markBack(Player player) {
PlayerChangeAfkStatusEvent event = new PlayerChangeAfkStatusEvent(player, false);
Bukkit.getPluginManager().callEvent(event);


afkList.remove(player.getName()); // Removing Player from the AFKList
return true; // Action completed
} else {
Expand Down

0 comments on commit 88c18c6

Please sign in to comment.