diff --git a/pom.xml b/pom.xml index 3bb2f44..ffd7e27 100755 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ groupId AFKDetector - 2.1.3 + 2.1.4 AFKDetector ${project.artifactId} diff --git a/src/main/java/com/alttd/afkdectector/AFKCheckTimer.java b/src/main/java/com/alttd/afkdectector/AFKCheckTimer.java index 1e835ce..8e751e6 100755 --- a/src/main/java/com/alttd/afkdectector/AFKCheckTimer.java +++ b/src/main/java/com/alttd/afkdectector/AFKCheckTimer.java @@ -5,7 +5,7 @@ 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.Component; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.Bukkit; @@ -13,7 +13,6 @@ import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; - public class AFKCheckTimer extends BukkitRunnable{ private AFKDetector plugin; @@ -60,8 +59,13 @@ public class AFKCheckTimer extends BukkitRunnable{ player.setSleepingIgnored(true); //player.setCanPickupItems(false); plugin.AFKPlayers.addEntry(player.getName()); - if (Config.AFKTOGGLEMESSAGES) - Bukkit.broadcast(MiniMessage.miniMessage().deserialize(Messages.AFKTOGGLEON.getMessage(), TagResolver.resolver(Placeholder.unparsed("player", player.getName()))), "afkdetector.notify"); + if (Config.AFKTOGGLEMESSAGES) { + TagResolver placeholders = TagResolver.resolver( + Placeholder.parsed("player", player.getName()) + ); + Component component = AFKDetector.miniMessage.deserialize(Messages.AFKTOGGLEON.getMessage(), placeholders); + Bukkit.broadcast(component, "afkdetector.notify"); + } } if(System.currentTimeMillis() - standingTime > afkplayer.getafkTime() * 60 * 1000) { 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 3a539eb..6ede7bf 100755 --- a/src/main/java/com/alttd/afkdectector/AFKDetector.java +++ b/src/main/java/com/alttd/afkdectector/AFKDetector.java @@ -6,6 +6,7 @@ import com.alttd.afkdectector.command.AFKListCommand; import com.alttd.afkdectector.command.ReloadCommand; import com.alttd.afkdectector.config.Config; import com.alttd.afkdectector.config.MessagesConfig; +import net.kyori.adventure.text.minimessage.MiniMessage; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -33,6 +34,8 @@ public class AFKDetector extends JavaPlugin implements Listener{ public HashMap PlayerAfkTime = new HashMap<>(); public boolean fulloverride; + + public static MiniMessage miniMessage; /** * afkplayers need to be added to a team. */ @@ -42,6 +45,7 @@ public class AFKDetector extends JavaPlugin implements Listener{ public void onEnable() { try { instance = this; + miniMessage = MiniMessage.miniMessage(); loadConfig(null); settupAfkState(); getServer().getPluginManager().registerEvents(this, this); @@ -113,10 +117,8 @@ public class AFKDetector extends JavaPlugin implements Listener{ @EventHandler public void onJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); - if (player != null) { - PlayerAfkTime.put(player.getUniqueId(), getPlayerAfktime(player)); - players.put(player.getUniqueId(), new AFKPlayer(player, this)); - } + PlayerAfkTime.put(player.getUniqueId(), getPlayerAfktime(player)); + players.put(player.getUniqueId(), new AFKPlayer(player, this)); if(Bukkit.getOnlinePlayers().size() >= Config.PLAYERLIMIT && !Config.SERVERFULL) { fulloverride = true; } @@ -126,9 +128,7 @@ public class AFKDetector extends JavaPlugin implements Listener{ public void onQuit(PlayerQuitEvent event) { Player player = event.getPlayer(); // Remove the players from the timer if they logout - if (player != null) { - players.remove(player.getUniqueId()); - } + players.remove(player.getUniqueId()); if(Bukkit.getOnlinePlayers().size() < Config.PLAYERLIMIT && fulloverride){ fulloverride = false; } @@ -140,9 +140,7 @@ public class AFKDetector extends JavaPlugin implements Listener{ return; } Player player = event.getPlayer(); - if (player != null) { - getPlayer(player).ResetAFK(); - } + getPlayer(player).ResetAFK(); } @EventHandler @@ -151,10 +149,8 @@ public class AFKDetector extends JavaPlugin implements Listener{ return; } Player player = event.getPlayer(); - if (player != null) { - getPlayer(player).ResetAFK(); - } - } + getPlayer(player).ResetAFK(); + } /* @EventHandler public void onPlayerMove(PlayerMoveEvent event) { diff --git a/src/main/java/com/alttd/afkdectector/MessageTimer.java b/src/main/java/com/alttd/afkdectector/MessageTimer.java index 366c280..4377bf2 100755 --- a/src/main/java/com/alttd/afkdectector/MessageTimer.java +++ b/src/main/java/com/alttd/afkdectector/MessageTimer.java @@ -50,7 +50,7 @@ public class MessageTimer extends BukkitRunnable { return; } if(Config.COUNTDOWNENABLED) { - MiniMessage miniMessage = MiniMessage.miniMessage(); + MiniMessage miniMessage = AFKDetector.miniMessage; Title title = Title.title(miniMessage.deserialize(Messages.COUNTDOWNTITLE1.getMessage()), miniMessage.deserialize(Messages.COUNTDOWNTITLE2.getMessage())); //Title.Times.of(Config.FADEIN, Config.STAY, Config.STAY); diff --git a/src/main/java/com/alttd/afkdectector/afkplayer/AFKPlayer.java b/src/main/java/com/alttd/afkdectector/afkplayer/AFKPlayer.java index d00e7a1..4868ed1 100755 --- a/src/main/java/com/alttd/afkdectector/afkplayer/AFKPlayer.java +++ b/src/main/java/com/alttd/afkdectector/afkplayer/AFKPlayer.java @@ -4,7 +4,7 @@ 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.text.Component; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.Bukkit; @@ -17,7 +17,6 @@ public class AFKPlayer { private final String playerName; private final UUID uuid; - private Location playerToSphereCenter; private long standingTime; private final int afkTime; @@ -70,7 +69,11 @@ public class AFKPlayer { public void ResetAFK() { if(isafk && Config.AFKTOGGLEMESSAGES) { - Bukkit.broadcast(MiniMessage.miniMessage().deserialize(Messages.AFKTOGGLEOFF.getMessage(), TagResolver.resolver(Placeholder.unparsed("player", playerName))), "afkdetector.notify"); + TagResolver placeholders = TagResolver.resolver( + Placeholder.parsed("player", playerName) + ); + Component component = AFKDetector.miniMessage.deserialize(Messages.AFKTOGGLEOFF.getMessage(), placeholders); + Bukkit.broadcast(component, "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 e7d1399..f857b82 100755 --- a/src/main/java/com/alttd/afkdectector/command/AFKCheckCommand.java +++ b/src/main/java/com/alttd/afkdectector/command/AFKCheckCommand.java @@ -1,8 +1,8 @@ package com.alttd.afkdectector.command; import com.alttd.afkdectector.AFKDetector; +import com.alttd.afkdectector.afkplayer.AFKPlayer; 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; @@ -14,8 +14,11 @@ import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.util.StringUtil; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class AFKCheckCommand implements CommandExecutor, TabCompleter { @@ -36,7 +39,7 @@ public class AFKCheckCommand implements CommandExecutor, TabCompleter { sender.sendMessage(Component.text(command.getUsage(), NamedTextColor.RED)); return true; } - MiniMessage miniMessage = MiniMessage.miniMessage(); + MiniMessage miniMessage = AFKDetector.miniMessage; target.showTitle(Title.title(miniMessage.deserialize(Messages.AFKCHECKTITLE.getMessage()), miniMessage.deserialize(Messages.AFKCHECKSUBTITLE.getMessage()))); if(sender instanceof Player) { @@ -53,6 +56,10 @@ public class AFKCheckCommand implements CommandExecutor, TabCompleter { @Override public List onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) { + List completions = new ArrayList<>(); + if (strings.length == 1) { + StringUtil.copyPartialMatches(strings[0], plugin.players.values().stream().filter(AFKPlayer::isafk).map(AFKPlayer::getPlayerName).collect(Collectors.toList()), completions); + } return null; } } \ No newline at end of file diff --git a/src/main/java/com/alttd/afkdectector/command/AFKListCommand.java b/src/main/java/com/alttd/afkdectector/command/AFKListCommand.java index a47e172..f9f1042 100755 --- a/src/main/java/com/alttd/afkdectector/command/AFKListCommand.java +++ b/src/main/java/com/alttd/afkdectector/command/AFKListCommand.java @@ -28,23 +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.miniMessage(); + MiniMessage miniMessage = AFKDetector.miniMessage; for (AFKPlayer afkplayer : plugin.players.values()) { long standingTime = afkplayer.getstandingTime(); if(System.currentTimeMillis() - standingTime > Config.TOGGLETIME * 60 * 1000) { afkplayers += 1; message = message.append(Component.newline()); TagResolver templates = TagResolver.resolver( - Placeholder.unparsed("player", afkplayer.getPlayerName()), - Placeholder.unparsed("afktime", (System.currentTimeMillis() - standingTime) / 1000 + "")); + Placeholder.parsed("player", afkplayer.getPlayerName()), + Placeholder.parsed("afktime", (System.currentTimeMillis() - standingTime) / 1000 + "") + ); Component userinfo = miniMessage.deserialize(Messages.AFK_LIST_ENTRY.getMessage(), templates); message = message.append(userinfo); } } TagResolver templates = TagResolver.resolver( - Placeholder.unparsed("afkplayers", Integer.toString(afkplayers)) + Placeholder.parsed("afkplayers", Integer.toString(afkplayers)) ); - Component component = miniMessage.deserialize(Messages.AFK_LIST.getMessage(), templates); + Component component = miniMessage.deserialize(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 deleted file mode 100644 index fde4450..0000000 --- a/src/main/java/com/alttd/afkdectector/config/AbstractConfig.java +++ /dev/null @@ -1,151 +0,0 @@ -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; - File configPath; - - AbstractConfig(String filename) { - this.configPath = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + AFKDetector.getInstance().getName()); - this.file = new File(configPath, filename); - this.yaml = new YamlConfiguration(); - if (!this.file.getParentFile().exists()) { - if(!this.file.getParentFile().mkdirs()) { - return; - } - } - 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 index 3943ca9..93d7495 100644 --- a/src/main/java/com/alttd/afkdectector/config/Config.java +++ b/src/main/java/com/alttd/afkdectector/config/Config.java @@ -1,11 +1,15 @@ package com.alttd.afkdectector.config; +import com.alttd.afkdectector.AFKDetector; +import com.alttd.galaxy.configuration.AbstractConfiguration; import org.bukkit.Bukkit; +import java.io.File; + @SuppressWarnings("unused") -public class Config extends AbstractConfig { +public class Config extends AbstractConfiguration { private Config() { - super("config.yml"); + super(new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + AFKDetector.getInstance().getName()), "config"); } static Config config; diff --git a/src/main/java/com/alttd/afkdectector/config/MessagesConfig.java b/src/main/java/com/alttd/afkdectector/config/MessagesConfig.java index 578b4b6..264b828 100644 --- a/src/main/java/com/alttd/afkdectector/config/MessagesConfig.java +++ b/src/main/java/com/alttd/afkdectector/config/MessagesConfig.java @@ -1,9 +1,14 @@ package com.alttd.afkdectector.config; +import com.alttd.afkdectector.AFKDetector; +import com.alttd.galaxy.configuration.AbstractConfiguration; + +import java.io.File; + @SuppressWarnings("unused") -public class MessagesConfig extends AbstractConfig { +public class MessagesConfig extends AbstractConfiguration { private MessagesConfig() { - super("messages.yml"); + super(new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + AFKDetector.getInstance().getName()), "messages"); } static MessagesConfig config;