1
0
mirror of https://github.com/nkomarn/harbor.git synced 2024-12-24 09:17:34 +01:00

🙌 Bossbar feature + Robust AFK system

This commit is contained in:
Mykyta 2020-02-21 21:00:28 -08:00
parent 0c616035c6
commit 233116061d
No known key found for this signature in database
GPG Key ID: C147E30C19EA3570
8 changed files with 125 additions and 25 deletions

View File

@ -4,6 +4,7 @@ import com.earth2me.essentials.Essentials;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import xyz.nkomarn.Harbor.command.HarborCommand; import xyz.nkomarn.Harbor.command.HarborCommand;
import xyz.nkomarn.Harbor.listener.AfkListener;
import xyz.nkomarn.Harbor.listener.BedListener; import xyz.nkomarn.Harbor.listener.BedListener;
import xyz.nkomarn.Harbor.listener.JoinListener; import xyz.nkomarn.Harbor.listener.JoinListener;
import xyz.nkomarn.Harbor.task.Checker; import xyz.nkomarn.Harbor.task.Checker;
@ -18,10 +19,8 @@ public class Harbor extends JavaPlugin {
public void onEnable() { public void onEnable() {
instance = this; instance = this;
saveDefaultConfig(); saveDefaultConfig();
if (Config.getBoolean("features.skip")) {
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this,
new Checker(), 0L, Config.getInteger("values.timer") * 20); new Checker(), 0L, Config.getInteger("values.timer") * 20);
}
getCommand("harbor").setExecutor(new HarborCommand()); getCommand("harbor").setExecutor(new HarborCommand());
getServer().getPluginManager().registerEvents(new JoinListener(), this); getServer().getPluginManager().registerEvents(new JoinListener(), this);
@ -32,5 +31,11 @@ public class Harbor extends JavaPlugin {
// Essentials hook // Essentials hook
essentials = (Essentials) getServer().getPluginManager().getPlugin("Essentials"); essentials = (Essentials) getServer().getPluginManager().getPlugin("Essentials");
// If Essentials isn't present, enable fallback AFK system
if (essentials == null) {
System.out.println("Registered Listener");
getServer().getPluginManager().registerEvents(new AfkListener(), this);
}
} }
} }

View File

@ -2,9 +2,13 @@ package xyz.nkomarn.Harbor.command;
import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -29,6 +33,11 @@ public class HarborCommand implements TabExecutor {
if (args.length < 1) { if (args.length < 1) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + "&7Harbor version " sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + "&7Harbor version "
+ Harbor.version + " by TechToolbox (@nkomarn).")); + Harbor.version + " by TechToolbox (@nkomarn)."));
BossBar bar = Bukkit.createBossBar("AAAAAAAAAAAA", BarColor.BLUE, BarStyle.SOLID);
bar.setProgress(1);
bar.addPlayer((Player) sender); // TODO remove
return true; return true;
} }

View File

@ -0,0 +1,32 @@
package xyz.nkomarn.Harbor.listener;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import xyz.nkomarn.Harbor.util.Afk;
public class AfkListener implements Listener {
@EventHandler
public void onChat(AsyncPlayerChatEvent event) {
Afk.updateActivity(event.getPlayer());
}
@EventHandler
public void onCommand(PlayerCommandPreprocessEvent event) {
Afk.updateActivity(event.getPlayer());
}
@EventHandler
public void onMove(PlayerMoveEvent event) {
Afk.updateActivity(event.getPlayer());
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
Afk.updateActivity((Player) event.getWhoClicked());
}
}

View File

@ -15,12 +15,10 @@ public class BedListener implements Listener {
public void onBedEnter(final PlayerBedEnterEvent event) { public void onBedEnter(final PlayerBedEnterEvent event) {
if (event.getBedEnterResult() != PlayerBedEnterEvent.BedEnterResult.OK) return; if (event.getBedEnterResult() != PlayerBedEnterEvent.BedEnterResult.OK) return;
if (Checker.skippingWorlds.contains(event.getPlayer().getWorld())) return; if (Checker.skippingWorlds.contains(event.getPlayer().getWorld())) return;
Bukkit.getScheduler().runTaskLater(Harbor.instance, () -> { Bukkit.getScheduler().runTaskLater(Harbor.instance, () -> Messages.sendWorldChatMessage(event.getBed().getWorld(),
Messages.sendWorldChatMessage(event.getBed().getWorld(),
Config.getString("messages.chat.sleeping") Config.getString("messages.chat.sleeping")
.replace("[player]", event.getPlayer().getName()) .replace("[player]", event.getPlayer().getName())
.replace("[displayname]", event.getPlayer().getDisplayName())); .replace("[displayname]", event.getPlayer().getDisplayName())), 1);
}, 1);
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)

View File

@ -3,9 +3,13 @@ package xyz.nkomarn.Harbor.task;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import xyz.nkomarn.Harbor.Harbor; import xyz.nkomarn.Harbor.Harbor;
import xyz.nkomarn.Harbor.util.Afk;
import xyz.nkomarn.Harbor.util.Config; import xyz.nkomarn.Harbor.util.Config;
import xyz.nkomarn.Harbor.util.Messages; import xyz.nkomarn.Harbor.util.Messages;
@ -32,14 +36,17 @@ public class Checker implements Runnable {
// Send actionbar sleeping notification // Send actionbar sleeping notification
if (sleeping > 0 && needed > 0) { if (sleeping > 0 && needed > 0) {
world.getPlayers().forEach(player -> Messages.sendActionBarMessage(player, double percentage = Math.min(1, (double) sleeping / getSkipAmount(world));
Config.getString("messages.actionbar.sleeping"))); Messages.sendBossBarMessage(world, Config.getString("messages.bossbar.sleeping.message"),
BarColor.valueOf(Config.getString("messages.bossbar.sleeping.color")), percentage);
Messages.sendActionBarMessage(world, Config.getString("messages.actionbar.sleeping"));
} else if (needed == 0 && sleeping > 0) { } else if (needed == 0 && sleeping > 0) {
world.getPlayers().forEach(player -> Messages.sendActionBarMessage(player, Messages.sendBossBarMessage(world, Config.getString("messages.bossbar.everyone.message"),
Config.getString("messages.actionbar.everyone"))); BarColor.valueOf(Config.getString("messages.bossbar.everyone.color")), 1);
skippingWorlds.add(world); Messages.sendActionBarMessage(world, Config.getString("messages.actionbar.everyone"));
if (!Config.getBoolean("features.skip")) return; if (!Config.getBoolean("features.skip")) return;
skippingWorlds.add(world);
new AccelerateNightTask(world).runTaskTimer(Harbor.instance, 0L, 1); new AccelerateNightTask(world).runTaskTimer(Harbor.instance, 0L, 1);
Messages.sendRandomChatMessage(world, "messages.chat.accelerateNight"); Messages.sendRandomChatMessage(world, "messages.chat.accelerateNight");
} }
@ -81,10 +88,10 @@ public class Checker implements Runnable {
return world.getPlayers().stream().filter(Checker::isExcluded).collect(toList()); return world.getPlayers().stream().filter(Checker::isExcluded).collect(toList());
} }
private static boolean isExcluded(final Player p) { private static boolean isExcluded(final Player player) {
final boolean excludedByGameMode = Config.getBoolean("features.ignore") && p.getGameMode() != GameMode.SURVIVAL; final boolean excludedByGameMode = Config.getBoolean("features.ignore") && player.getGameMode() != GameMode.SURVIVAL;
final boolean excludedByPermission = Config.getBoolean("features.bypass") && p.hasPermission("harbor.bypass"); final boolean excludedByPermission = Config.getBoolean("features.bypass") && player.hasPermission("harbor.bypass");
final boolean excludedByAfk = Harbor.essentials != null && Harbor.essentials.getUser(p).isAfk(); // Essentials AFK detection final boolean excludedByAfk = Afk.isAfk(player);
return excludedByGameMode || excludedByPermission || excludedByAfk; return excludedByGameMode || excludedByPermission || excludedByAfk;
} }

View File

@ -0,0 +1,28 @@
package xyz.nkomarn.Harbor.util;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import xyz.nkomarn.Harbor.Harbor;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
public class Afk {
private static HashMap<Player, Long> activity = new HashMap<>();
public static boolean isAfk(Player player) {
if (!Config.getBoolean("features.afk")) return false;
if (Harbor.essentials != null) {
return Harbor.essentials.getUser(player).isAfk();
}
if (!activity.containsKey(player)) return false;
long minutes = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - activity.get(player));
return minutes >= Config.getInteger("values.timeout");
}
public static void updateActivity(Player player) {
activity.put(player, System.currentTimeMillis());
}
}

View File

@ -2,9 +2,14 @@ package xyz.nkomarn.Harbor.util;
import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import xyz.nkomarn.Harbor.Harbor;
import xyz.nkomarn.Harbor.task.Checker; import xyz.nkomarn.Harbor.task.Checker;
import java.util.List; import java.util.List;
@ -24,11 +29,18 @@ public class Messages {
prepareMessage(world, message)))); prepareMessage(world, message))));
} }
public static void sendActionBarMessage(final Player player, final String message) { public static void sendActionBarMessage(final World world, final String message) {
final World world = player.getWorld();
if (!Config.getBoolean("messages.actionbar.actionbar") || message.length() < 1) return; if (!Config.getBoolean("messages.actionbar.actionbar") || message.length() < 1) return;
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText( world.getPlayers().forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR,
prepareMessage(world, message))); TextComponent.fromLegacyText(prepareMessage(world, message))));
}
public static void sendBossBarMessage(final World world, final String message, final BarColor color, final double percentage) {
if (!Config.getBoolean("messages.bossbar.bossbar") || message.length() < 1) return;
BossBar bar = Bukkit.createBossBar(Messages.prepareMessage(world, message), color, BarStyle.SOLID);
bar.setProgress(percentage);
world.getPlayers().forEach(bar::addPlayer);
Bukkit.getScheduler().runTaskLater(Harbor.instance, bar::removeAll, Config.getInteger("values.timer") * 20);
} }
private static String prepareMessage(final World world, final String message) { private static String prepareMessage(final World world, final String message) {

View File

@ -11,7 +11,8 @@
values: values:
timer: 2 # How often (in seconds) to run the clock task (used to detect sleep, AFK players, time actionbar, etc.) timer: 2 # How often (in seconds) to run the clock task (used to detect sleep, AFK players, time actionbar, etc.)
percent: 50 # Percent of players that need to sleep to skip night (must be between 0 to 100) percent: 50 # Percent of players that need to sleep to skip night (must be between 0 to 100)
interval: 60 # Time skip interval that is added when the night get accelerated. interval: 60 # Time skip interval that is added when the night is accelerated.
timeout: 15 # Time (in minutes) until a player is considered AFK (for internal AFK detection system only- when Essentials isn't present)
features: features:
skip: true # Toggle night skipping feature. Configure amount of players needed to skip above (percent) skip: true # Toggle night skipping feature. Configure amount of players needed to skip above (percent)
@ -19,7 +20,7 @@ features:
phantoms: false # Reset the sleep statistic (practically disables phantom spawns - false = no phantoms) phantoms: false # Reset the sleep statistic (practically disables phantom spawns - false = no phantoms)
bypass: true # Toggle exclusion of operators/players with permission "harbor.bypass" from sleep count bypass: true # Toggle exclusion of operators/players with permission "harbor.bypass" from sleep count
ignore: true # Toggle exclusion of players in creative and spectator mode ignore: true # Toggle exclusion of players in creative and spectator mode
afk: true # Detect AFK players and remove them from the sleep count (Essentials API used for detection) afk: true # Detect AFK players and remove them from the sleep count (Essentials API used for detection when possible)
notifier: true # Displays a notification when a new update is released notifier: true # Displays a notification when a new update is released
messages: messages:
@ -38,6 +39,14 @@ messages:
actionbar: true # Enable/disable actionbar messages actionbar: true # Enable/disable actionbar messages
sleeping: "&e[sleeping] out of [needed] players are sleeping ([more] more needed to skip)." # Shown when some players are in bed sleeping: "&e[sleeping] out of [needed] players are sleeping ([more] more needed to skip)." # Shown when some players are in bed
everyone: "&eEveryone is sleeping. Sweet dreams!" # Shown when all players are in bed everyone: "&eEveryone is sleeping. Sweet dreams!" # Shown when all players are in bed
bossbar: # Use colors from https://hub.spigotmc.org/javadocs/spigot/org/bukkit/boss/BarColor.html
bossbar: true # Enable/disable bossbar messages
sleeping:
message: "&f&l[sleeping] out of [needed] are sleeping &7&l([more] more needed)" # Shown when some players are in bed
color: BLUE
everyone:
message: "&f&lEveryone is sleeping. Sweet dreams!" # Shown when all players are in bed
color: GREEN
miscellaneous: miscellaneous:
prefix: "&8&l(&6&lHarbor&8&l)&r " # Prefix for Harbor command/miscellaneous messages prefix: "&8&l(&6&lHarbor&8&l)&r " # Prefix for Harbor command/miscellaneous messages
permission: "&7Insufficient permissions." # Sent when player doesn't have permissions to run command permission: "&7Insufficient permissions." # Sent when player doesn't have permissions to run command