Skip to content

Commit

Permalink
Add a way to temporarily disable announcing your afk status to the wh…
Browse files Browse the repository at this point in the history
…ole server.

It's temporarily because it doesn't remember after restarting your server.
  • Loading branch information
andrew121410 committed Jul 11, 2024
1 parent 948ba4a commit d5267d8
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.andrew121410.mc.world16essentials.commands.*;
import com.andrew121410.mc.world16essentials.commands.afk.AfkCMD;
import com.andrew121410.mc.world16essentials.commands.afk.IgnoreAfkCMD;
import com.andrew121410.mc.world16essentials.commands.afk.IsAfkCMD;
import com.andrew121410.mc.world16essentials.commands.back.BackCMD;
import com.andrew121410.mc.world16essentials.commands.fly.FlyCMD;
Expand Down Expand Up @@ -195,6 +196,7 @@ private void registerCommands() {
new UUIDCMD(this);
new WorkBenchCMD(this);
new XyzdxdydzCMD(this);
new IgnoreAfkCMD(this);
}

private void registerListeners() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
return true;
}

String color = "&7";

if (args.length == 0) {
if (p.isOp()) color = "&4";
api.doAfk(p, color);
api.doAfk(p, null, true);
return true;
} else if (args.length == 1) {
if (!p.hasPermission("world16.afk.other")) {
Expand All @@ -46,8 +43,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
}
Player target = plugin.getServer().getPlayerExact(args[0]);
if (target != null && target.isOnline()) {
if (target.isOp()) color = "&4";
api.doAfk(target, color);
api.doAfk(target, null, true);
}
return true;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.andrew121410.mc.world16essentials.commands.afk;

import com.andrew121410.mc.world16essentials.World16Essentials;
import com.andrew121410.mc.world16essentials.objects.AfkObject;
import com.andrew121410.mc.world16essentials.utils.API;
import com.andrew121410.mc.world16utils.chat.Translate;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class IgnoreAfkCMD implements CommandExecutor {

private final World16Essentials plugin;
private final API api;

public IgnoreAfkCMD(World16Essentials getPlugin) {
this.plugin = getPlugin;
this.api = this.plugin.getApi();

this.plugin.getCommand("ignoreafk").setExecutor(this);
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!sender.hasPermission("world16.ignoreafk")) {
api.sendPermissionErrorMessage(sender);
return true;
}

if (args.length == 0) {
if (!(sender instanceof Player)) {
sender.sendMessage(Translate.miniMessage("<red>Consoles can do /ignoreafk <player>."));
return true;
}

handleIgnoreAfk(sender, null);
} else if (args.length == 1) { // /ignoreafk <player>
Player target = this.plugin.getServer().getPlayerExact(args[0]);
if (target == null) {
sender.sendMessage(Translate.miniMessage("<red>Player not found."));
return true;
}
handleIgnoreAfk(sender, target);
} else {
sender.sendMessage(Translate.miniMessage("<red>Usage: /ignoreafk <optional:player>"));
}
return true;
}

private void handleIgnoreAfk(CommandSender sender, Player target) {
AfkObject afkObject;
if (target == null) {
Player player = (Player) sender;
afkObject = this.plugin.getMemoryHolder().getAfkMap().get(player.getUniqueId());
} else {
afkObject = this.plugin.getMemoryHolder().getAfkMap().get(target.getUniqueId());
}

// Should never be null. But just in case.
if (afkObject == null) {
sender.sendMessage(Translate.miniMessage("<red>Something went wrong."));
return;
}

if (afkObject.isIgnore()) {
afkObject.setIgnore(false);

if (target != null) {
target.sendMessage(Translate.miniMessage("<green>Your AFK Status is no longer being ignored."));
sender.sendMessage(Translate.miniMessage("<green>" + target.getName() + " AFK Status is no longer being ignored."));
} else {
sender.sendMessage(Translate.miniMessage("<green>Your AFK Status is no longer being ignored."));
}
} else {
afkObject.setIgnore(true);

if (target != null) {
target.sendMessage(Translate.miniMessage("<red>Your AFK Status is now being ignored."));
sender.sendMessage(Translate.miniMessage("<red>" + target.getName() + " AFK Status is now being ignored."));
} else {
sender.sendMessage(Translate.miniMessage("<red>Your AFK Status is now being ignored."));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.andrew121410.mc.world16essentials.World16Essentials;
import com.andrew121410.mc.world16essentials.objects.AfkObject;
import com.andrew121410.mc.world16essentials.utils.API;
import com.andrew121410.mc.world16utils.chat.Translate;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

Expand Down Expand Up @@ -43,20 +44,19 @@ public void run() {
iterator.remove();
return;
}
String color = player.isOp() ? "&4" : "&7";

// Don't run if player is already AFK
if (afkObject.isAfk()) return;

// Checks if the player has not moved in 3 min if not afk them if so restart()
if (player.getLocation().equals(afkObject.getLocation()) && !api.didPlayerJustJoin(player)) {
api.doAfk(player, color);
api.doAfk(player, null, !afkObject.isIgnore());
} else afkObject.restart(player);
}
}
}.runTaskTimer(plugin, 1200L, 2400L);

// Checks if the player moves
// Checks if the player moves, this runs every 2 seconds
new BukkitRunnable() {
@Override
public void run() {
Expand All @@ -72,14 +72,17 @@ public void run() {
iterator.remove();
return;
}
String color = player.isOp() ? "&4" : "&7";

// If not afk don't run
if (!afkObject.isAfk()) return;

if (afkObject.isIgnore()) {
player.sendActionBar(Translate.miniMessage("<aqua>You are AFK, but it won't be announced."));
}

// Only if the player moves more than 3 blocks
if (player.getLocation().distanceSquared(afkObject.getLocation()) > 9) {
api.doAfk(player, color);
api.doAfk(player, null, !afkObject.isIgnore());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class AfkObject {
private Location location;
private boolean isAfk;

private boolean ignore;

public AfkObject(Player player) {
this.uuid = player.getUniqueId();
this.location = player.getLocation();
Expand Down Expand Up @@ -51,4 +53,12 @@ public void restart(Player player) {
this.location = player.getLocation();
this.isAfk = false;
}

public boolean isIgnore() {
return ignore;
}

public void setIgnore(boolean ignore) {
this.ignore = ignore;
}
}
19 changes: 16 additions & 3 deletions src/main/java/com/andrew121410/mc/world16essentials/utils/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,27 @@ public ConfigurationSection getPlayersYML(Player player) {
return configurationSection;
}

public void doAfk(Player player, String color) {
public boolean doAfk(Player player, String color, boolean announce) {
if (color == null) {
color = "<gray>";
if (player.isOp()) color = "<dark_red>";
}

AfkObject afkObject = this.afkMap.get(player.getUniqueId());
if (afkObject.isAfk()) {
this.plugin.getServer().broadcastMessage(Translate.color("&7*" + color + " " + player.getDisplayName() + "&r&7 is no longer AFK."));
if (announce) {
// this.plugin.getServer().broadcastMessage(Translate.color("&7*" + color + " " + player.getDisplayName() + "&r&7 is no longer AFK."));
this.plugin.getServer().broadcast(Translate.miniMessage("<gray>* " + color + player.getName() + " <reset><gray>is no longer AFK."));
}
afkObject.restart(player);
return false;
} else {
this.plugin.getServer().broadcastMessage(Translate.color("&7* " + color + player.getDisplayName() + "&r&7" + " is now AFK."));
if (announce) {
// this.plugin.getServer().broadcastMessage(Translate.color("&7* " + color + player.getDisplayName() + "&r&7" + " is now AFK."));
this.plugin.getServer().broadcast(Translate.miniMessage("<gray>* " + color + player.getName() + " <reset><gray>is now AFK."));
}
afkObject.setAfk(true, player.getLocation());
return true;
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ commands:

afk:
permission: world16.afk
ignoreafk:
permission: world16.ignoreafk
bed:
permission: world16.bed
broadcast:
Expand Down Expand Up @@ -256,6 +258,9 @@ permissions:
world16.afk:
description: Allows you to use afk cmd
default: op
world16.ignoreafk:
description: Allows you to use ignoreafk cmd
default: op
world16.bed:
description: Allows you to use bed cmd
default: op
Expand Down

0 comments on commit d5267d8

Please sign in to comment.