From 11cbad3e775bea671bb37e4b3b1f59a8c96551e9 Mon Sep 17 00:00:00 2001
From: destro174 <40720638+destro174@users.noreply.github.com>
Date: Fri, 9 Jul 2021 09:20:46 +0200
Subject: [PATCH] Add Minimessage
---
pom.xml | 18 +-
.../com/alttd/afkdectector/AFKCheckTimer.java | 15 +-
.../com/alttd/afkdectector/AFKDetector.java | 168 +++---------------
.../java/com/alttd/afkdectector/Lang.java | 68 -------
.../com/alttd/afkdectector/MessageTimer.java | 19 +-
.../afkdectector/afkplayer/AFKPlayer.java | 8 +-
.../afkdectector/command/AFKCheckCommand.java | 15 +-
.../afkdectector/command/AFKListCommand.java | 24 ++-
.../afkdectector/config/AbstractConfig.java | 144 +++++++++++++++
.../com/alttd/afkdectector/config/Config.java | 70 ++++++++
.../alttd/afkdectector/config/Messages.java | 34 ++++
.../afkdectector/config/MessagesConfig.java | 31 ++++
.../com/alttd/afkdectector/util/Logger.java | 27 +++
13 files changed, 396 insertions(+), 245 deletions(-)
delete mode 100755 src/main/java/com/alttd/afkdectector/Lang.java
create mode 100644 src/main/java/com/alttd/afkdectector/config/AbstractConfig.java
create mode 100644 src/main/java/com/alttd/afkdectector/config/Config.java
create mode 100644 src/main/java/com/alttd/afkdectector/config/Messages.java
create mode 100644 src/main/java/com/alttd/afkdectector/config/MessagesConfig.java
create mode 100644 src/main/java/com/alttd/afkdectector/util/Logger.java
diff --git a/pom.xml b/pom.xml
index 18fd552..1ce3d1c 100755
--- a/pom.xml
+++ b/pom.xml
@@ -31,17 +31,21 @@
- papermc
- https://papermc.io/repo/repository/maven-public/
+ Alttd-Nexus
+ http://leo:8081/snapshots/
+
+
+ dv8tion
+ m2-dv8tion
+ https://m2.dv8tion.net/releases
-
- com.destroystokyo.paper
- paper-api
- 1.16.5-R0.1-SNAPSHOT
- provided
+
+ com.alttd
+ galaxy-api
+ 1.17-R0.1-SNAPSHOT
\ No newline at end of file
diff --git a/src/main/java/com/alttd/afkdectector/AFKCheckTimer.java b/src/main/java/com/alttd/afkdectector/AFKCheckTimer.java
index f84414b..778efdc 100755
--- a/src/main/java/com/alttd/afkdectector/AFKCheckTimer.java
+++ b/src/main/java/com/alttd/afkdectector/AFKCheckTimer.java
@@ -3,6 +3,10 @@ package com.alttd.afkdectector;
import java.util.UUID;
import com.alttd.afkdectector.afkplayer.AFKPlayer;
+import com.alttd.afkdectector.config.Config;
+import com.alttd.afkdectector.config.Messages;
+import net.kyori.adventure.text.minimessage.MiniMessage;
+import net.kyori.adventure.text.minimessage.Template;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
@@ -35,7 +39,7 @@ public class AFKCheckTimer extends BukkitRunnable{
pastLocation = player.getLocation();
afkplayer.setplayerToSphereCenter(pastLocation);
}
- if (player.getLocation().distanceSquared(pastLocation) > plugin.radius * plugin.radius) {
+ if (player.getLocation().distanceSquared(pastLocation) > Config.RADIUS * Config.RADIUS) {
if(player.getLocation().getYaw() != pastLocation.getYaw() && player.getLocation().getPitch() != pastLocation.getPitch()) {
afkplayer.setplayerToSphereCenter(pastLocation);
afkplayer.setstandingTime(System.currentTimeMillis());
@@ -51,20 +55,19 @@ public class AFKCheckTimer extends BukkitRunnable{
}
}
long standingTime = afkplayer.getstandingTime();
- if(System.currentTimeMillis() - standingTime > plugin.toggletime * 60 * 1000 && !afkplayer.isafk()) {
+ if(System.currentTimeMillis() - standingTime > Config.TOGGLETIME * 60 * 1000 && !afkplayer.isafk()) {
afkplayer.setafk(true);
player.setSleepingIgnored(true);
//player.setCanPickupItems(false);
plugin.AFKPlayers.addEntry(player.getName());
- Bukkit.broadcast(
- ChatColor.translateAlternateColorCodes('&', Lang.AFKTOGGLEON.toString().replace("%player%", player.getName())), "afkdetector.notify");
+ Bukkit.broadcast(MiniMessage.get().parse(Messages.AFKTOGGLEON.getMessage(), Template.of("player", player.getName())), "afkdetector.notify");
}
if(System.currentTimeMillis() - standingTime > afkplayer.getafkTime() * 60 * 1000) {
MessageTimer currentTimer = plugin.messageTimers.get(uuid);
if(currentTimer == null) {
- currentTimer = new MessageTimer(plugin, uuid, plugin.messageRepeats);
+ currentTimer = new MessageTimer(plugin, uuid, Config.MESSAGEREPEATS);
plugin.messageTimers.put(uuid, currentTimer);
- new MessageTimer(plugin, uuid, plugin.messageRepeats).init();
+ new MessageTimer(plugin, uuid, Config.MESSAGEREPEATS).init();
}
} else {
MessageTimer currentTimer = plugin.messageTimers.get(uuid);
diff --git a/src/main/java/com/alttd/afkdectector/AFKDetector.java b/src/main/java/com/alttd/afkdectector/AFKDetector.java
index 0bf3fbe..3cd6bdc 100755
--- a/src/main/java/com/alttd/afkdectector/AFKDetector.java
+++ b/src/main/java/com/alttd/afkdectector/AFKDetector.java
@@ -3,14 +3,9 @@ package com.alttd.afkdectector;
import com.alttd.afkdectector.afkplayer.AFKPlayer;
import com.alttd.afkdectector.command.AFKCheckCommand;
import com.alttd.afkdectector.command.AFKListCommand;
-import net.kyori.adventure.audience.Audience;
-import org.bukkit.plugin.java.JavaPlugin;
-import org.bukkit.scoreboard.Scoreboard;
-import org.bukkit.scoreboard.ScoreboardManager;
-import org.bukkit.scoreboard.Team;
-
+import com.alttd.afkdectector.config.Config;
+import com.alttd.afkdectector.config.MessagesConfig;
import org.bukkit.Bukkit;
-import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@@ -19,49 +14,34 @@ import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.permissions.PermissionAttachmentInfo;
+import org.bukkit.plugin.java.JavaPlugin;
+import org.bukkit.scoreboard.Scoreboard;
+import org.bukkit.scoreboard.ScoreboardManager;
+import org.bukkit.scoreboard.Team;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.util.*;
+import java.util.HashMap;
+import java.util.UUID;
public class AFKDetector extends JavaPlugin implements Listener{
+ public static AFKDetector instance;
+
public HashMap players = new HashMap<>();
public HashMap messageTimers = new HashMap<>();
public HashMap PlayerAfkTime = new HashMap<>();
- public boolean sleepignore, serverfull, countdownenabled, fulloverride;
- public int radius, flagTime, messageDelay, messageRepeats, defaultafkTime, maxafkTime, toggletime, commandcooldown, playerlimit;
- public int fadein, stay, fadeout = 0;
- public String title, subtitle, message, title1, title2 = "";
- public String kickCommand = "kick ${ChatColor.RED}You have been kicked for being AFK.";
- /**
- * Adventure
- */
- private Audience audience;
-
- /**
- * events that cancel
- */
- public boolean chatWillCancel, commandWillCancel;
-
+ public boolean fulloverride;
/**
* afkplayers need to be added to a team.
*/
Team AFKPlayers;
- /**
- * Messages.yml
- */
- public static YamlConfiguration LANG;
- public static File LANG_FILE;
-
-
@Override
public void onEnable() {
try {
- loadSettings();
+ instance = this;
+ Config.reload();
+ MessagesConfig.reload();
settupAfkState();
getServer().getPluginManager().registerEvents(this, this);
//getCommand("afk").setExecutor(new AFKCommand(this));
@@ -101,108 +81,13 @@ public class AFKDetector extends JavaPlugin implements Listener{
AFKPlayers.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER);
//AFKPlayers.setOption(Team.Option.NAME_TAG_VISIBILITY, Team.OptionStatus.NEVER);
}
-
- /**
- * Load the config.yml file.
- * @return The config.yml config.
- */
- private void loadSettings() {
- saveDefaultConfig();
-
- /**
- * Player section
- */
- sleepignore = getConfig().getBoolean("player.sleep" ,false);
- radius = getConfig().getInt("player.radius", 4);
- toggletime = getConfig().getInt("player.toggle-time", 5);
- defaultafkTime = getConfig().getInt("player.afk-time", 5);
- maxafkTime = getConfig().getInt("player.maxafk-time", 5);
- serverfull = getConfig().getBoolean("player.serverfull" ,false);
- playerlimit = Math.round(Bukkit.getMaxPlayers() * 90 / 100);
- commandcooldown = getConfig().getInt("player.commandcooldown");
- /**
- * Countdown section
- */
- countdownenabled = getConfig().getBoolean("countdown.enabled" ,false);
- message = getConfig().getString("countdown.message");
- title1 = getConfig().getString("countdown.title1");
- title2 = getConfig().getString("countdown.title2");
- fadein = getConfig().getInt("countdown.fadein", 10);
- stay = getConfig().getInt("countdown.stay", 50);
- fadeout = getConfig().getInt("countdown.fadeout", 10);
- messageDelay = getConfig().getInt("countdown.message-delay", 30);
- messageRepeats = getConfig().getInt("countdown.message-repeats", 4);
- kickCommand = getConfig().getString("countdown.kick-command");
- /**
- * events
- */
- chatWillCancel = getConfig().getBoolean("events.chat", true);
- commandWillCancel = getConfig().getBoolean("events.commands", true);
- loadLang();
- }
-
- /**
- * Load the lang.yml file.
- * @return
- * @return The lang.yml config.
- */
- public YamlConfiguration loadLang() {
- File lang = new File(getDataFolder(), "lang.yml");
- if (!lang.exists()) {
- try {
- getDataFolder().mkdir();
- lang.createNewFile();
- InputStreamReader defConfigStream = new InputStreamReader(this.getResource("lang.yml"));
- if (defConfigStream != null) {
- YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
- defConfig.save(lang);
- Lang.setFile(defConfig);
- return defConfig;
- }
- } catch(IOException e) {
- e.printStackTrace(); // So they notice
- this.setEnabled(false); // Without it loaded, we can't send them messages
- }
- }
- YamlConfiguration conf = YamlConfiguration.loadConfiguration(lang);
- for(Lang item:Lang.values()) {
- if (conf.getString(item.getPath()) == null) {
- conf.set(item.getPath(), item.getDefault());
- }
- }
- Lang.setFile(conf);
- LANG = conf;
- LANG_FILE = lang;
- try {
- conf.save(getLangFile());
- } catch(IOException e) {
- e.printStackTrace();
- }
- return conf;
- }
-
- /**
- * Gets the lang.yml config.
- * @return The lang.yml config.
- */
- public YamlConfiguration getLang() {
- return LANG;
- }
-
- /**
- * Get the lang.yml file.
- * @return The lang.yml file.
- */
- public File getLangFile() {
- return LANG_FILE;
- }
/**
* Get the afk time for a player
*/
public int getaAllowedAfktime(Player player) {
- if(fulloverride) {
- return defaultafkTime;
+ if(Config.SERVERFULL) {
+ return Config.DEFAULTAFKTIME;
}
return PlayerAfkTime.get(player.getUniqueId());
}
@@ -213,14 +98,14 @@ public class AFKDetector extends JavaPlugin implements Listener{
if (attachmentInfo.getPermission().startsWith(permissionPrefix)) {
String perm = attachmentInfo.getPermission();
int Time = Integer.parseInt(perm.substring(perm.lastIndexOf(".") + 1));
- if(Time >= maxafkTime) {
- return maxafkTime;
- } else if(Time < maxafkTime) {
+ if(Time >= Config.MAXAFKTIME) {
+ return Config.MAXAFKTIME;
+ } else {
return Time;
}
}
}
- return defaultafkTime;
+ return Config.DEFAULTAFKTIME;
}
@EventHandler
@@ -230,7 +115,7 @@ public class AFKDetector extends JavaPlugin implements Listener{
PlayerAfkTime.put(player.getUniqueId(), getPlayerAfktime(player));
players.put(player.getUniqueId(), new AFKPlayer(player, this));
}
- if(Bukkit.getOnlinePlayers().size() >= playerlimit && !fulloverride) {
+ if(Bukkit.getOnlinePlayers().size() >= Config.PLAYERLIMIT && !Config.SERVERFULL) {
fulloverride = true;
}
}
@@ -242,14 +127,14 @@ public class AFKDetector extends JavaPlugin implements Listener{
if (player != null) {
players.remove(player.getUniqueId());
}
- if(Bukkit.getOnlinePlayers().size() < playerlimit && fulloverride){
+ if(Bukkit.getOnlinePlayers().size() < Config.PLAYERLIMIT && fulloverride){
fulloverride = false;
}
}
@EventHandler
public void onTalkCancel(AsyncPlayerChatEvent event) {
- if (!chatWillCancel) {
+ if (!Config.CHATWILLCANCEL) {
return;
}
Player player = event.getPlayer();
@@ -260,7 +145,7 @@ public class AFKDetector extends JavaPlugin implements Listener{
@EventHandler
public void oncommandCancel(PlayerCommandPreprocessEvent event) {
- if (!commandWillCancel) {
+ if (!Config.COMMANDWILLCANCEL) {
return;
}
Player player = event.getPlayer();
@@ -284,5 +169,8 @@ public class AFKDetector extends JavaPlugin implements Listener{
}
}
}*/
-
+
+ public static AFKDetector getInstance() {
+ return instance;
+ }
}
diff --git a/src/main/java/com/alttd/afkdectector/Lang.java b/src/main/java/com/alttd/afkdectector/Lang.java
deleted file mode 100755
index bf29621..0000000
--- a/src/main/java/com/alttd/afkdectector/Lang.java
+++ /dev/null
@@ -1,68 +0,0 @@
-package com.alttd.afkdectector;
-
-import org.bukkit.ChatColor;
-import org.bukkit.configuration.file.YamlConfiguration;
-
-/**
-* An enum for requesting strings from the language file.
-*/
-public enum Lang {
- TITLE("title-name", "&4[&fAFKDetector&4]:"),
- COOLDOWN("cooldown-message", "You need to wait %timeleft% seconds before using this command."),
- INVALID_PLAYER("invalid-args", "&cInvalid args!usage: -p:playername"),
- INVALID_REASON("invalid-reason" , "&cInvalid args! usage: -r:reason"),
- NOT_ONLINE("player-notonline", "&cInvalid args! player not online"),
- PLAYER_ONLY("player-only", "Teri have you ever seen an afk console?"),
- NO_PERMS("no-permissions", "&cYou don''t have permission for that!"),
- AFK_LIST("afk-list", "There are %afkplayers% afk."),
- AFK_PREFIX("afk-prefix", "[AFK]"),
- AFKCHECKTITLE("afkcheck-title", "AFK CHECK"),
- AFKTOGGLEON("afk-toggle-on", "&b%player% is not afk."),
- AFKTOGGLEOFF("afk-toggle-off", "&b%player% is no longer afk."),
- AFKCHECKSUBTITLE("afkcheck-subtitle", "Please respond to the dm from staff!"),
- AFKCHECKCMESSAGE("afkcheck-message", "Hey, since you're near a farm and not moving. I'm making sure you aren't afk. Please respond to me if you're not AFK.");
-
-
- private String path;
- private String def;
- private static YamlConfiguration LANG;
-
- /**
- * Lang enum constructor.
- * @param path The string path.
- * @param start The default string.
- */
- Lang(String path, String start) {
- this.path = path;
- this.def = start;
- }
-
- /**
- * Set the {@code YamlConfiguration} to use.
- * @param config The config to set.
- */
- public static void setFile(YamlConfiguration config) {
- LANG = config;
- }
-
- @Override
- public String toString() {
- return ChatColor.translateAlternateColorCodes('&', LANG.getString(this.path, def));
- }
-
- /**
- * Get the default value of the path.
- * @return The default value of the path.
- */
- public String getDefault() {
- return this.def;
- }
-
- /**
- * Get the path to the string.
- * @return The path to the string.
- */
- public String getPath() {
- return this.path;
- }
-}
\ No newline at end of file
diff --git a/src/main/java/com/alttd/afkdectector/MessageTimer.java b/src/main/java/com/alttd/afkdectector/MessageTimer.java
index ddbc64b..f1acf6e 100755
--- a/src/main/java/com/alttd/afkdectector/MessageTimer.java
+++ b/src/main/java/com/alttd/afkdectector/MessageTimer.java
@@ -2,6 +2,10 @@ package com.alttd.afkdectector;
import java.util.UUID;
+import com.alttd.afkdectector.config.Config;
+import com.alttd.afkdectector.config.Messages;
+import net.kyori.adventure.text.minimessage.MiniMessage;
+import net.kyori.adventure.title.Title;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
@@ -20,7 +24,7 @@ public class MessageTimer extends BukkitRunnable {
}
public void init() {
- runTaskTimer(plugin, 0, plugin.messageDelay * 20);
+ runTaskTimer(plugin, 0, Config.MESSAGEDELAY * 20);
}
// TODO get a better string builder
@@ -40,10 +44,13 @@ public class MessageTimer extends BukkitRunnable {
cancel();
return;
}
- if(plugin.countdownenabled) {
- player.sendTitle(ChatColor.translateAlternateColorCodes('&', plugin.title1),
- ChatColor.translateAlternateColorCodes('&', plugin.title2), plugin.fadein, plugin.stay, plugin.fadeout);
- player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.message));
+ if(Config.COUNTDOWNENABLED) {
+ MiniMessage miniMessage = MiniMessage.get();
+ Title title = Title.title(miniMessage.parse(Messages.COUNTDOWNTITLE1.getMessage()),
+ miniMessage.parse(Messages.COUNTDOWNTITLE2.getMessage()));
+ //Title.Times.of(Config.FADEIN, Config.STAY, Config.STAY);
+ player.showTitle(title);
+ player.sendMessage(miniMessage.parse(Messages.COUNTDOWNMESSAGE.getMessage()));
}
repeats = repeats - 1;
@@ -53,7 +60,7 @@ public class MessageTimer extends BukkitRunnable {
}
//Bukkit.dispatchCommand(Bukkit.getConsoleSender(), plugin.kickCommand.replace("%player%", player.getName()));
plugin.messageTimers.remove(player.getUniqueId());
- Bukkit.dispatchCommand(Bukkit.getConsoleSender(), return_placeholders(plugin.kickCommand, player));
+ Bukkit.dispatchCommand(Bukkit.getConsoleSender(), return_placeholders(Config.KICKCOMMAND, player));
cancel();
}
} else {
diff --git a/src/main/java/com/alttd/afkdectector/afkplayer/AFKPlayer.java b/src/main/java/com/alttd/afkdectector/afkplayer/AFKPlayer.java
index eb26682..f873883 100755
--- a/src/main/java/com/alttd/afkdectector/afkplayer/AFKPlayer.java
+++ b/src/main/java/com/alttd/afkdectector/afkplayer/AFKPlayer.java
@@ -2,7 +2,9 @@ package com.alttd.afkdectector.afkplayer;
import java.util.UUID;
-import com.alttd.afkdectector.Lang;
+import com.alttd.afkdectector.config.Messages;
+import net.kyori.adventure.text.minimessage.MiniMessage;
+import net.kyori.adventure.text.minimessage.Template;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
@@ -27,8 +29,6 @@ public class AFKPlayer {
this.standingTime = System.currentTimeMillis();
this.afkTime = plugin.getaAllowedAfktime(player);
this.isafk = false;
- //plugin.getLogger().info("Loading " + playerName + ", Time " + standingTime + ", allowed afktime " + afkTime);
-
}
public String getPlayerName() {
@@ -69,7 +69,7 @@ public class AFKPlayer {
public void ResetAFK() {
if(isafk) {
- Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', Lang.AFKTOGGLEOFF.toString().replace("%player%", playerName)), "afkdetector.notify");
+ Bukkit.broadcast(MiniMessage.get().parse(Messages.AFKTOGGLEOFF.getMessage(), Template.of("player", playerName)), "afkdetector.notify");
}
standingTime = System.currentTimeMillis();
playerToSphereCenter = Bukkit.getPlayer(getPlayerUuid()).getLocation();
diff --git a/src/main/java/com/alttd/afkdectector/command/AFKCheckCommand.java b/src/main/java/com/alttd/afkdectector/command/AFKCheckCommand.java
index 7c10be4..57c0054 100755
--- a/src/main/java/com/alttd/afkdectector/command/AFKCheckCommand.java
+++ b/src/main/java/com/alttd/afkdectector/command/AFKCheckCommand.java
@@ -1,10 +1,11 @@
package com.alttd.afkdectector.command;
import com.alttd.afkdectector.AFKDetector;
-import com.alttd.afkdectector.Lang;
+import com.alttd.afkdectector.config.Messages;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
+import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.title.Title;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
@@ -39,15 +40,17 @@ public class AFKCheckCommand implements CommandExecutor, TabCompleter {
sender.sendMessage(Component.text(command.getUsage(), NamedTextColor.RED));
return true;
}
- target.showTitle(Title.title(Component.text(Lang.AFKCHECKTITLE.toString(), NamedTextColor.RED),
- Component.text(Lang.AFKCHECKSUBTITLE.toString(), NamedTextColor.RED)));
+ MiniMessage miniMessage = MiniMessage.get();
+ target.showTitle(Title.title(miniMessage.parse(Messages.AFKCHECKTITLE.getMessage()),
+ miniMessage.parse(Messages.AFKCHECKSUBTITLE.getMessage())));
if(sender instanceof Player) {
Player player = (Player) sender;
- PlayerCommandPreprocessEvent commandEvent = new PlayerCommandPreprocessEvent(player, "/" + "msg " + args[0] + " " + Lang.AFKCHECKCMESSAGE.toString());
+ String cmd = "msg " + args[0] + " " + Messages.AFKCHECKMESSAGE.getMessage();
+ PlayerCommandPreprocessEvent commandEvent = new PlayerCommandPreprocessEvent(player, "/" + cmd);
Bukkit.getPluginManager().callEvent(commandEvent);
- player.performCommand("msg " + args[0] + " " + Lang.AFKCHECKCMESSAGE.toString());
+ player.performCommand(cmd);
} else {
- Bukkit.dispatchCommand(sender, "msg " + args[0] + " " + Lang.AFKCHECKCMESSAGE.toString());
+ Bukkit.dispatchCommand(sender, "msg " + args[0] + " " + Messages.AFKCHECKMESSAGE.getMessage());
}
return true;
}
diff --git a/src/main/java/com/alttd/afkdectector/command/AFKListCommand.java b/src/main/java/com/alttd/afkdectector/command/AFKListCommand.java
index 48b18ad..5e510bb 100755
--- a/src/main/java/com/alttd/afkdectector/command/AFKListCommand.java
+++ b/src/main/java/com/alttd/afkdectector/command/AFKListCommand.java
@@ -1,16 +1,19 @@
package com.alttd.afkdectector.command;
import com.alttd.afkdectector.AFKDetector;
-import com.alttd.afkdectector.Lang;
import com.alttd.afkdectector.afkplayer.AFKPlayer;
+import com.alttd.afkdectector.config.Config;
+import com.alttd.afkdectector.config.Messages;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
-import net.kyori.adventure.text.format.TextDecoration;
+import net.kyori.adventure.text.minimessage.MiniMessage;
+import net.kyori.adventure.text.minimessage.Template;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
+import java.util.ArrayList;
import java.util.List;
public class AFKListCommand implements CommandExecutor, TabCompleter {
@@ -25,19 +28,24 @@ public class AFKListCommand implements CommandExecutor, TabCompleter {
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
int afkplayers = 0;
Component message = Component.empty();
+ MiniMessage miniMessage = MiniMessage.get();
for (AFKPlayer afkplayer : plugin.players.values()) {
long standingTime = afkplayer.getstandingTime();
- if(System.currentTimeMillis() - standingTime > plugin.toggletime * 60 * 1000) {
+ if(System.currentTimeMillis() - standingTime > Config.TOGGLETIME * 60 * 1000) {
afkplayers += 1;
message = message.append(Component.newline());
- Component userinfo = Component.text(afkplayer.getPlayerName(), NamedTextColor.GOLD)
- .append(Component.text(" has been afk for " + (System.currentTimeMillis() - standingTime) / 1000 + " seconds.", NamedTextColor.WHITE))
- .hoverEvent(Component.text("Click here to send an afkcheck to " + afkplayer.getPlayerName() + "!" , NamedTextColor.GRAY).asHoverEvent())
- .clickEvent(net.kyori.adventure.text.event.ClickEvent.runCommand("/afkcheck " + afkplayer.getPlayerName()));
+ List templates = new ArrayList<>(List.of(
+ Template.of("player", afkplayer.getPlayerName()),
+ Template.of("afktime", (System.currentTimeMillis() - standingTime) / 1000 + "")
+ ));
+ Component userinfo = miniMessage.parse(Messages.AFK_LIST_ENTRY.getMessage(), templates);
message = message.append(userinfo);
}
}
- Component component = Component.text(Lang.AFK_LIST.toString().replaceAll("%afkplayers%", Integer.toString(afkplayers)), NamedTextColor.YELLOW);
+ List templates = new ArrayList<>(List.of(
+ Template.of("afkplayers", Integer.toString(afkplayers))
+ ));
+ Component component = miniMessage.parse(Messages.AFK_LIST.getMessage(), templates);
sender.sendMessage(component.append(message));
return true;
}
diff --git a/src/main/java/com/alttd/afkdectector/config/AbstractConfig.java b/src/main/java/com/alttd/afkdectector/config/AbstractConfig.java
new file mode 100644
index 0000000..8acad15
--- /dev/null
+++ b/src/main/java/com/alttd/afkdectector/config/AbstractConfig.java
@@ -0,0 +1,144 @@
+package com.alttd.afkdectector.config;
+
+import com.alttd.afkdectector.AFKDetector;
+import com.alttd.afkdectector.util.Logger;
+import com.google.common.collect.ImmutableMap;
+import org.bukkit.Bukkit;
+import org.bukkit.Location;
+import org.bukkit.configuration.ConfigurationSection;
+import org.bukkit.configuration.InvalidConfigurationException;
+import org.bukkit.configuration.file.YamlConfiguration;
+import org.bukkit.inventory.ItemStack;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.*;
+
+@SuppressWarnings({"unused", "SameParameterValue"})
+abstract class AbstractConfig {
+ File file;
+ YamlConfiguration yaml;
+
+ AbstractConfig(String filename) {
+ this.file = new File(AFKDetector.getInstance().getDataFolder(), filename);
+ this.yaml = new YamlConfiguration();
+ try {
+ yaml.load(file);
+ } catch (IOException ignore) {
+ } catch (InvalidConfigurationException ex) {
+ Logger.severe(String.format("Could not load %s, please correct your syntax errors", filename));
+ throw new RuntimeException(ex);
+ }
+ yaml.options().copyDefaults(true);
+ }
+
+ void readConfig(Class> clazz, Object instance) {
+ for (Method method : clazz.getDeclaredMethods()) {
+ if (Modifier.isPrivate(method.getModifiers())) {
+ if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
+ try {
+ method.setAccessible(true);
+ method.invoke(instance);
+ } catch (InvocationTargetException ex) {
+ throw new RuntimeException(ex.getCause());
+ } catch (Exception ex) {
+ Logger.severe("Error invoking " + method);
+ ex.printStackTrace();
+ }
+ }
+ }
+ }
+
+ try {
+ yaml.save(file);
+ } catch (IOException ex) {
+ Logger.severe("Could not save " + file);
+ ex.printStackTrace();
+ }
+ }
+
+ void set(String path, Object val) {
+ yaml.addDefault(path, val);
+ yaml.set(path, val);
+ }
+
+ String getString(String path, String def) {
+ yaml.addDefault(path, def);
+ return yaml.getString(path, yaml.getString(path));
+ }
+
+ boolean getBoolean(String path, boolean def) {
+ yaml.addDefault(path, def);
+ return yaml.getBoolean(path, yaml.getBoolean(path));
+ }
+
+ int getInt(String path, int def) {
+ yaml.addDefault(path, def);
+ return yaml.getInt(path, yaml.getInt(path));
+ }
+
+ double getDouble(String path, double def) {
+ yaml.addDefault(path, def);
+ return yaml.getDouble(path, yaml.getDouble(path));
+ }
+
+ List> getList(String path, T def) {
+ yaml.addDefault(path, def);
+ return yaml.getList(path, yaml.getList(path));
+ }
+
+ @NonNull
+ Map getMap(final @NonNull String path, final @Nullable Map def) {
+ final ImmutableMap.Builder builder = ImmutableMap.builder();
+ if (def != null && yaml.getConfigurationSection(path) == null) {
+ yaml.addDefault(path, def.isEmpty() ? new HashMap<>() : def);
+ return def;
+ }
+ final ConfigurationSection section = yaml.getConfigurationSection(path);
+ if (section != null) {
+ for (String key : section.getKeys(false)) {
+ @SuppressWarnings("unchecked")
+ final T val = (T) section.get(key);
+ if (val != null) {
+ builder.put(key, val);
+ }
+ }
+ }
+ return builder.build();
+ }
+
+ ItemStack getItemStack(String path, ItemStack def) {
+ yaml.addDefault(path, def);
+ return yaml.getItemStack(path);
+ }
+
+ void setLocations(String path, ArrayList locs) {
+ ArrayList list = new ArrayList<>();
+ for(Location l : locs) {
+ String toSave = l.getWorld().getName() + ":" + l.getX() + ":" + l.getY() + ":" + l.getZ();
+ list.add(toSave);
+ }
+ yaml.set(path, list);
+ }
+
+ List getLocations(String path){
+ List attempts = yaml.getStringList(path);
+
+ if (attempts == null || attempts.isEmpty()) {
+ return Collections.emptyList();
+ }
+ List locations = new ArrayList<>();
+
+ for (String attempt : attempts) {
+ String[] parts = attempt.split(":");
+ locations.add(new Location(Bukkit.getWorld(parts[0]), Double.parseDouble(parts[1]), Double.parseDouble(parts[2]), Double.parseDouble(parts[3])));
+ }
+ return locations;
+ }
+
+}
diff --git a/src/main/java/com/alttd/afkdectector/config/Config.java b/src/main/java/com/alttd/afkdectector/config/Config.java
new file mode 100644
index 0000000..0602adb
--- /dev/null
+++ b/src/main/java/com/alttd/afkdectector/config/Config.java
@@ -0,0 +1,70 @@
+package com.alttd.afkdectector.config;
+
+import org.bukkit.Bukkit;
+
+@SuppressWarnings("unused")
+public class Config extends AbstractConfig {
+ private Config() {
+ super("config.yml");
+ }
+
+ static Config config;
+ static int version;
+
+ public static void reload() {
+ config = new Config();
+
+ version = config.getInt("config-version", 1);
+ config.set("config-version", 1);
+
+ config.readConfig(Config.class, null);
+ }
+
+ public static boolean DEBUG_MODE = false;
+ private static void settings() {
+ DEBUG_MODE = config.getBoolean("debug-mode", DEBUG_MODE);
+ }
+
+ public static boolean SLEEPIGNORE = false;
+ public static int RADIUS = 4;
+ public static int TOGGLETIME = 4;
+ public static int DEFAULTAFKTIME = 5;
+ public static int MAXAFKTIME = 5;
+ public static boolean SERVERFULL = false;
+ public static int PLAYERLIMIT = Math.round(Bukkit.getMaxPlayers() * 90 / 100);
+ public static int COMMANDCOOLDOWWN = 60;
+ private static void playerSettings() {
+ SLEEPIGNORE = config.getBoolean("player.sleep", SLEEPIGNORE);
+ RADIUS = config.getInt("player.radius", RADIUS);
+ TOGGLETIME = config.getInt("player.toggle-time", TOGGLETIME);
+ DEFAULTAFKTIME = config.getInt("player.afk-time", 5);
+ MAXAFKTIME = config.getInt("player.maxafk-time", 5);
+ SERVERFULL = config.getBoolean("player.serverfull" ,false);
+ COMMANDCOOLDOWWN = config.getInt("player.commandcooldown", COMMANDCOOLDOWWN);
+ }
+
+ public static boolean COUNTDOWNENABLED = false;
+ public static int FADEIN = 10;
+ public static int STAY = 50;
+ public static int FADEOUt = 10;
+ public static int MESSAGEDELAY = 15;
+ public static int MESSAGEREPEATS = 15;
+ public static String KICKCOMMAND = "kickfrombungee %player% &cYou have been afk for %afktime% minutes.";
+ private static void countdownSettigns() {
+ COUNTDOWNENABLED = config.getBoolean("countdown.enabled" ,COUNTDOWNENABLED);
+ FADEIN = config.getInt("countdown.fadein", FADEIN);
+ STAY = config.getInt("countdown.stay", STAY);
+ FADEOUt = config.getInt("countdown.fadeout", FADEOUt);
+ MESSAGEDELAY = config.getInt("countdown.message-delay", MESSAGEDELAY);
+ MESSAGEREPEATS = config.getInt("countdown.message-repeats", MESSAGEREPEATS);
+ KICKCOMMAND = config.getString("countdown.kick-command", KICKCOMMAND);
+ }
+
+ public static boolean CHATWILLCANCEL = true;
+ public static boolean COMMANDWILLCANCEL = true;
+ private static void eventSettigns() {
+ CHATWILLCANCEL = config.getBoolean("events.chat", CHATWILLCANCEL);
+ COMMANDWILLCANCEL = config.getBoolean("events.commands", COMMANDWILLCANCEL);
+ }
+
+}
diff --git a/src/main/java/com/alttd/afkdectector/config/Messages.java b/src/main/java/com/alttd/afkdectector/config/Messages.java
new file mode 100644
index 0000000..44e7371
--- /dev/null
+++ b/src/main/java/com/alttd/afkdectector/config/Messages.java
@@ -0,0 +1,34 @@
+package com.alttd.afkdectector.config;
+
+public enum Messages {
+ TITLE("title-name", "[AFKDetector] "),
+ COOLDOWN("cooldown-message", "You need to wait %timeleft% seconds before using this command."),
+ INVALID_PLAYER("invalid-args", "Invalid args!usage: -p:playername"),
+ INVALID_REASON("invalid-reason" , "Invalid args! usage: -r:reason"),
+ NOT_ONLINE("player-notonline", "Invalid args! player not online"),
+ PLAYER_ONLY("player-only", "Teri have you ever seen an afk console?"),
+ NO_PERMS("no-permissions", "You don''t have permission for that!"),
+ AFK_LIST("afk-list", "There are %afkplayers% afk."),
+ AFK_LIST_ENTRY("afk-list-entry", " has been afk for seconds. >[Teleport] >[Afkcheck]"),
+ AFK_PREFIX("afk-prefix", "[AFK]"),
+ COUNTDOWNMESSAGE("countdown-message", "You need to move around to avoid being kicked"),
+ COUNTDOWNTITLE1("countdown-title1", "You need to move around to avoid being kicked"),
+ COUNTDOWNTITLE2("countdown-title2", "You need to move around to avoid being kicked"),
+ AFKTOGGLEON("afk-toggle-on", " is now afk."),
+ AFKTOGGLEOFF("afk-toggle-off", " is no longer afk."),
+ AFKCHECKTITLE("afkcheck-title", "AFK CHECK"),
+ AFKCHECKSUBTITLE("afkcheck-subtitle", "Please respond to the dm from staff!"),
+ AFKCHECKMESSAGE("afkcheck-message", "Hey, since you're near a farm and not moving. I'm making sure you aren't afk. Please respond to me if you're not AFK.");
+
+ public final String key;
+ public String message;
+
+ Messages(String key, String... message) {
+ this.key = key;
+ this.message = String.join("\n", message);
+ }
+
+ public String getMessage() {
+ return message;
+ }
+}
diff --git a/src/main/java/com/alttd/afkdectector/config/MessagesConfig.java b/src/main/java/com/alttd/afkdectector/config/MessagesConfig.java
new file mode 100644
index 0000000..578b4b6
--- /dev/null
+++ b/src/main/java/com/alttd/afkdectector/config/MessagesConfig.java
@@ -0,0 +1,31 @@
+package com.alttd.afkdectector.config;
+
+@SuppressWarnings("unused")
+public class MessagesConfig extends AbstractConfig {
+ private MessagesConfig() {
+ super("messages.yml");
+ }
+
+ static MessagesConfig config;
+ static int version;
+
+ public static void reload() {
+ config = new MessagesConfig();
+
+ version = config.getInt("config-version", 1);
+ config.set("config-version", 1);
+
+ config.readConfig(MessagesConfig.class, null);
+ }
+
+ public static boolean DEBUG_MODE = false;
+ private static void settings() {
+ DEBUG_MODE = config.getBoolean("debug-mode", DEBUG_MODE);
+ }
+
+ private static void loadMessages() {
+ for (Messages message : Messages.values()) {
+ message.message = config.getString(message.key, message.message);
+ }
+ }
+}
diff --git a/src/main/java/com/alttd/afkdectector/util/Logger.java b/src/main/java/com/alttd/afkdectector/util/Logger.java
new file mode 100644
index 0000000..5288fe5
--- /dev/null
+++ b/src/main/java/com/alttd/afkdectector/util/Logger.java
@@ -0,0 +1,27 @@
+package com.alttd.afkdectector.util;
+
+import net.md_5.bungee.api.ChatColor;
+import org.bukkit.Bukkit;
+
+import java.util.logging.Level;
+public class Logger {
+
+ public static void info(String str) {
+ log(Level.INFO,"&e" + str);
+ }
+
+ public static void warn(String str) {
+ log(Level.SEVERE,"&6" + str);
+ }
+
+ public static void severe(String str) {
+ log(Level.SEVERE,"&c" + str);
+ }
+
+ public static void log(Level level, String str) {
+ Bukkit.getLogger().log(level,
+ ChatColor.translateAlternateColorCodes('&',
+ "&r " + str));
+ }
+
+}