Add Minimessage

This commit is contained in:
destro174 2021-07-09 09:20:46 +02:00
parent 2d2681b9bc
commit 11cbad3e77
13 changed files with 396 additions and 245 deletions

18
pom.xml
View File

@ -31,17 +31,21 @@
<repositories>
<repository>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
<id>Alttd-Nexus</id>
<url>http://leo:8081/snapshots/</url>
</repository>
<repository> <!-- can be removed when galaxy api is fixed, I fucked up when shading this in -->
<id>dv8tion</id>
<name>m2-dv8tion</name>
<url>https://m2.dv8tion.net/releases</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.16.5-R0.1-SNAPSHOT</version>
<scope>provided</scope>
<dependency><!-- Galaxy -->
<groupId>com.alttd</groupId>
<artifactId>galaxy-api</artifactId>
<version>1.17-R0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -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);

View File

@ -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<UUID, AFKPlayer> players = new HashMap<>();
public HashMap<UUID, MessageTimer> messageTimers = new HashMap<>();
public HashMap<UUID, Integer> 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;
}
}

View File

@ -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;
}
}

View File

@ -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 {

View File

@ -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();

View File

@ -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;
}

View File

@ -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<Template> 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<Template> 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;
}

View File

@ -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));
}
<T> List<?> getList(String path, T def) {
yaml.addDefault(path, def);
return yaml.getList(path, yaml.getList(path));
}
@NonNull
<T> Map<String, T> getMap(final @NonNull String path, final @Nullable Map<String, T> def) {
final ImmutableMap.Builder<String, T> 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<Location> locs) {
ArrayList<String> 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<Location> getLocations(String path){
List<String> attempts = yaml.getStringList(path);
if (attempts == null || attempts.isEmpty()) {
return Collections.emptyList();
}
List<Location> 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;
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,34 @@
package com.alttd.afkdectector.config;
public enum Messages {
TITLE("title-name", "<red>[<white>AFKDetector</white>]</red> "),
COOLDOWN("cooldown-message", "You need to wait %timeleft% seconds before using this command."),
INVALID_PLAYER("invalid-args", "<red>Invalid args!usage: -p:playername"),
INVALID_REASON("invalid-reason" , "<red>Invalid args! usage: -r:reason"),
NOT_ONLINE("player-notonline", "<red>Invalid args! player not online"),
PLAYER_ONLY("player-only", "Teri have you ever seen an afk console?"),
NO_PERMS("no-permissions", "<red>You don''t have permission for that!"),
AFK_LIST("afk-list", "<gold>There are %afkplayers% afk."),
AFK_LIST_ENTRY("afk-list-entry", "<player> has been afk for <afktime> seconds. <hover:show_text:Click to teleport><click:suggest_command:/tp <player>>[Teleport]</click></hover> <hover:show_text:Click to send an afkcheck.><click:suggest_command:/afkcheck <player>>[Afkcheck]</click></hover>"),
AFK_PREFIX("afk-prefix", "[AFK]"),
COUNTDOWNMESSAGE("countdown-message", "<gray>You need to move around to avoid being kicked"),
COUNTDOWNTITLE1("countdown-title1", "<gray>You need to move around to avoid being kicked"),
COUNTDOWNTITLE2("countdown-title2", "<gray>You need to move around to avoid being kicked"),
AFKTOGGLEON("afk-toggle-on", "<teal><player> is now afk."),
AFKTOGGLEOFF("afk-toggle-off", "<teal><player> 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;
}
}

View File

@ -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);
}
}
}

View File

@ -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));
}
}