From d5267d806b03b3bdcc693031b99291dafe5eccd2 Mon Sep 17 00:00:00 2001 From: Andrew121410 Date: Thu, 11 Jul 2024 14:06:45 -0400 Subject: [PATCH] Add a way to temporarily disable announcing your afk status to the whole server. It's temporarily because it doesn't remember after restarting your server. --- .../world16essentials/World16Essentials.java | 2 + .../commands/afk/AfkCMD.java | 8 +- .../commands/afk/IgnoreAfkCMD.java | 86 +++++++++++++++++++ .../managers/AfkManager.java | 13 +-- .../world16essentials/objects/AfkObject.java | 10 +++ .../mc/world16essentials/utils/API.java | 19 +++- src/main/resources/plugin.yml | 5 ++ 7 files changed, 129 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/andrew121410/mc/world16essentials/commands/afk/IgnoreAfkCMD.java diff --git a/src/main/java/com/andrew121410/mc/world16essentials/World16Essentials.java b/src/main/java/com/andrew121410/mc/world16essentials/World16Essentials.java index 51d71773..b9ef0023 100644 --- a/src/main/java/com/andrew121410/mc/world16essentials/World16Essentials.java +++ b/src/main/java/com/andrew121410/mc/world16essentials/World16Essentials.java @@ -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; @@ -195,6 +196,7 @@ private void registerCommands() { new UUIDCMD(this); new WorkBenchCMD(this); new XyzdxdydzCMD(this); + new IgnoreAfkCMD(this); } private void registerListeners() { diff --git a/src/main/java/com/andrew121410/mc/world16essentials/commands/afk/AfkCMD.java b/src/main/java/com/andrew121410/mc/world16essentials/commands/afk/AfkCMD.java index 58c0b5be..2678f0a6 100644 --- a/src/main/java/com/andrew121410/mc/world16essentials/commands/afk/AfkCMD.java +++ b/src/main/java/com/andrew121410/mc/world16essentials/commands/afk/AfkCMD.java @@ -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")) { @@ -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 { diff --git a/src/main/java/com/andrew121410/mc/world16essentials/commands/afk/IgnoreAfkCMD.java b/src/main/java/com/andrew121410/mc/world16essentials/commands/afk/IgnoreAfkCMD.java new file mode 100644 index 00000000..204a27f6 --- /dev/null +++ b/src/main/java/com/andrew121410/mc/world16essentials/commands/afk/IgnoreAfkCMD.java @@ -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("Consoles can do /ignoreafk .")); + return true; + } + + handleIgnoreAfk(sender, null); + } else if (args.length == 1) { // /ignoreafk + Player target = this.plugin.getServer().getPlayerExact(args[0]); + if (target == null) { + sender.sendMessage(Translate.miniMessage("Player not found.")); + return true; + } + handleIgnoreAfk(sender, target); + } else { + sender.sendMessage(Translate.miniMessage("Usage: /ignoreafk ")); + } + 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("Something went wrong.")); + return; + } + + if (afkObject.isIgnore()) { + afkObject.setIgnore(false); + + if (target != null) { + target.sendMessage(Translate.miniMessage("Your AFK Status is no longer being ignored.")); + sender.sendMessage(Translate.miniMessage("" + target.getName() + " AFK Status is no longer being ignored.")); + } else { + sender.sendMessage(Translate.miniMessage("Your AFK Status is no longer being ignored.")); + } + } else { + afkObject.setIgnore(true); + + if (target != null) { + target.sendMessage(Translate.miniMessage("Your AFK Status is now being ignored.")); + sender.sendMessage(Translate.miniMessage("" + target.getName() + " AFK Status is now being ignored.")); + } else { + sender.sendMessage(Translate.miniMessage("Your AFK Status is now being ignored.")); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/andrew121410/mc/world16essentials/managers/AfkManager.java b/src/main/java/com/andrew121410/mc/world16essentials/managers/AfkManager.java index 2b0fecde..d50a395b 100644 --- a/src/main/java/com/andrew121410/mc/world16essentials/managers/AfkManager.java +++ b/src/main/java/com/andrew121410/mc/world16essentials/managers/AfkManager.java @@ -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; @@ -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() { @@ -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("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()); } } } diff --git a/src/main/java/com/andrew121410/mc/world16essentials/objects/AfkObject.java b/src/main/java/com/andrew121410/mc/world16essentials/objects/AfkObject.java index 15490ace..1d246219 100644 --- a/src/main/java/com/andrew121410/mc/world16essentials/objects/AfkObject.java +++ b/src/main/java/com/andrew121410/mc/world16essentials/objects/AfkObject.java @@ -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(); @@ -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; + } } diff --git a/src/main/java/com/andrew121410/mc/world16essentials/utils/API.java b/src/main/java/com/andrew121410/mc/world16essentials/utils/API.java index ce06adce..fa5d8373 100644 --- a/src/main/java/com/andrew121410/mc/world16essentials/utils/API.java +++ b/src/main/java/com/andrew121410/mc/world16essentials/utils/API.java @@ -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 = ""; + if (player.isOp()) color = ""; + } + 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("* " + color + player.getName() + " 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("* " + color + player.getName() + " is now AFK.")); + } afkObject.setAfk(true, player.getLocation()); + return true; } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 935c6f52..9edcb4c8 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -92,6 +92,8 @@ commands: afk: permission: world16.afk + ignoreafk: + permission: world16.ignoreafk bed: permission: world16.bed broadcast: @@ -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