mirror of
https://github.com/nkomarn/harbor.git
synced 2024-11-16 07:05:22 +01:00
More AFK methods!
This commit is contained in:
parent
722d15134d
commit
519361da02
9
pom.xml
9
pom.xml
@ -19,6 +19,13 @@
|
||||
</dependency>
|
||||
|
||||
<!-- Spigot versions -->
|
||||
<dependency>
|
||||
<groupId>spigotmc.org</groupId>
|
||||
<artifactId>spigot-1.14.1</artifactId>
|
||||
<scope>system</scope>
|
||||
<systemPath>${spigot}/1.14.1.jar</systemPath>
|
||||
<version>1.14.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>spigotmc.org</groupId>
|
||||
<artifactId>spigot-1.14</artifactId>
|
||||
@ -100,7 +107,7 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<directory>/home/nkomarn/Documents/Spigot Servers/1.14/plugins/</directory>
|
||||
<directory>/home/nkomarn/Documents/Spigot Servers/1.14.1/plugins/</directory>
|
||||
<sourceDirectory>${basedir}/src/</sourceDirectory>
|
||||
<resources>
|
||||
<resource>
|
||||
|
15
src/main/java/mykyta/Harbor/Events/ChatEvent.java
Normal file
15
src/main/java/mykyta/Harbor/Events/ChatEvent.java
Normal file
@ -0,0 +1,15 @@
|
||||
package mykyta.Harbor.Events;
|
||||
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import mykyta.Harbor.Util;
|
||||
|
||||
public class ChatEvent implements Listener {
|
||||
Util util = new Util();
|
||||
|
||||
@EventHandler
|
||||
public void onChat(AsyncPlayerChatEvent event) {
|
||||
util.updateActivity(event.getPlayer());
|
||||
}
|
||||
}
|
15
src/main/java/mykyta/Harbor/Events/CommandEvent.java
Normal file
15
src/main/java/mykyta/Harbor/Events/CommandEvent.java
Normal file
@ -0,0 +1,15 @@
|
||||
package mykyta.Harbor.Events;
|
||||
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
import mykyta.Harbor.Util;
|
||||
|
||||
public class CommandEvent implements Listener {
|
||||
Util util = new Util();
|
||||
|
||||
@EventHandler
|
||||
public void onCommand(PlayerCommandPreprocessEvent event) {
|
||||
util.updateActivity(event.getPlayer());
|
||||
}
|
||||
}
|
@ -1,20 +1,24 @@
|
||||
package mykyta.Harbor.Events;
|
||||
|
||||
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.inventory.InventoryDragEvent;
|
||||
|
||||
import mykyta.Harbor.GUI.GUIType;
|
||||
import mykyta.Harbor.Util;
|
||||
import mykyta.Harbor.GUI.GUIHolder;
|
||||
|
||||
public class GUIEvent implements Listener {
|
||||
Util util = new Util();
|
||||
|
||||
@EventHandler
|
||||
public void onClick(InventoryClickEvent event) {
|
||||
if (event.getInventory().getHolder() != null && event.getInventory().getHolder() instanceof GUIHolder) {
|
||||
GUIType t = ((GUIHolder) event.getInventory().getHolder()).getType();
|
||||
if (t.equals(GUIType.SLEEPING)) event.setCancelled(true);
|
||||
}
|
||||
util.updateActivity((Player) event.getWhoClicked());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@ -23,5 +27,6 @@ public class GUIEvent implements Listener {
|
||||
GUIType t = ((GUIHolder) event.getInventory().getHolder()).getType();
|
||||
if (t.equals(GUIType.SLEEPING)) event.setCancelled(true);
|
||||
}
|
||||
util.updateActivity((Player) event.getWhoClicked());
|
||||
}
|
||||
}
|
@ -6,22 +6,12 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
|
||||
import mykyta.Harbor.Config;
|
||||
import mykyta.Harbor.Util;
|
||||
|
||||
public class Move implements Listener {
|
||||
@EventHandler
|
||||
public void onPlayerMovement(PlayerMoveEvent event) {
|
||||
Player p = event.getPlayer();
|
||||
Config config = new Config();
|
||||
|
||||
if (Util.afk.contains(p)) {
|
||||
Util.afk.remove(p);
|
||||
p.setPlayerListName(ChatColor.translateAlternateColorCodes('&', p.getName()));
|
||||
Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.unafk")
|
||||
.replace("[player]", p.getName())));
|
||||
}
|
||||
Util.activity.put(event.getPlayer(), System.currentTimeMillis());
|
||||
Util util = new Util();
|
||||
util.updateActivity(event.getPlayer());
|
||||
}
|
||||
}
|
@ -10,6 +10,8 @@ import mykyta.Harbor.Commands.Command;
|
||||
import mykyta.Harbor.Commands.Sleeping;
|
||||
import mykyta.Harbor.Events.BedEnter;
|
||||
import mykyta.Harbor.Events.BedLeave;
|
||||
import mykyta.Harbor.Events.ChatEvent;
|
||||
import mykyta.Harbor.Events.CommandEvent;
|
||||
import mykyta.Harbor.Events.GUIEvent;
|
||||
import mykyta.Harbor.Events.Move;
|
||||
import mykyta.Harbor.Events.PlayerJoin;
|
||||
@ -31,6 +33,8 @@ public class Harbor extends JavaPlugin {
|
||||
Bukkit.getPluginManager().registerEvents(new GUIEvent(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new PlayerJoin(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new PlayerLeave(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new CommandEvent(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new ChatEvent(), this);
|
||||
util.setupNMS();
|
||||
|
||||
Bukkit.getServer().getWorlds().forEach(w -> {
|
||||
|
@ -12,30 +12,33 @@ public class Task implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Bukkit.getServer().getWorlds().forEach(w -> {
|
||||
if (w.getTime() >= 12516 && w.getTime() <= 12537 && config.getBoolean("features.title")) w.getPlayers().forEach(p -> {
|
||||
util.sendTitle(p, config.getString("messages.title.evening.top"), config.getString("messages.title.evening.bottom"));
|
||||
});
|
||||
if (util.getSleeping(w) > 0 && util.getNeeded(w) == 0) {
|
||||
util.skip(w);
|
||||
}
|
||||
if (util.getSleeping(w) > 0 && util.getSleeping(w) < w.getPlayers().size()) {w.getPlayers().forEach(p -> {util.sendActionbar(p, config.getString("messages.actionbar.sleeping"), w);});}
|
||||
else if (util.getSleeping(w) == util.getNeeded(w)) {w.getPlayers().forEach(p -> {util.sendActionbar(p, config.getString("messages.actionbar.everyone"), w);});}
|
||||
|
||||
w.getPlayers().forEach(p -> {
|
||||
if (TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - Util.activity.get(p)) >= config.getInteger("values.timeout")) {
|
||||
if (Util.sleeping.get(w).contains(p)) {
|
||||
Util.activity.put(p, System.currentTimeMillis());
|
||||
return;
|
||||
try {
|
||||
Bukkit.getServer().getWorlds().forEach(w -> {
|
||||
if (w.getTime() >= 12516 && w.getTime() <= 12537 && config.getBoolean("features.title")) w.getPlayers().forEach(p -> {
|
||||
util.sendTitle(p, config.getString("messages.title.evening.top"), config.getString("messages.title.evening.bottom"));
|
||||
});
|
||||
if (util.getSleeping(w) > 0 && util.getNeeded(w) == 0) {
|
||||
util.skip(w);
|
||||
}
|
||||
if (util.getSleeping(w) > 0 && util.getSleeping(w) < w.getPlayers().size()) {w.getPlayers().forEach(p -> {util.sendActionbar(p, config.getString("messages.actionbar.sleeping"), w);});}
|
||||
else if (util.getSleeping(w) == util.getNeeded(w)) {w.getPlayers().forEach(p -> {util.sendActionbar(p, config.getString("messages.actionbar.everyone"), w);});}
|
||||
|
||||
w.getPlayers().forEach(p -> {
|
||||
if (TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - Util.activity.get(p)) >= config.getInteger("values.timeout")) {
|
||||
if (Util.sleeping.get(w).contains(p)) {
|
||||
Util.activity.put(p, System.currentTimeMillis());
|
||||
return;
|
||||
}
|
||||
if (!Util.afk.contains(p)) {
|
||||
Util.afk.add(p);
|
||||
p.setPlayerListName(ChatColor.translateAlternateColorCodes('&', config.getString("messages.miscellaneous.afkprefix") + p.getName()));
|
||||
Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.afk")
|
||||
.replace("[player]", p.getName())));
|
||||
}
|
||||
}
|
||||
if (!Util.afk.contains(p)) {
|
||||
Util.afk.add(p);
|
||||
p.setPlayerListName(ChatColor.translateAlternateColorCodes('&', config.getString("messages.miscellaneous.afkprefix") + p.getName()));
|
||||
Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.afk")
|
||||
.replace("[player]", p.getName())));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
catch (Exception e) {if (Util.debug) e.printStackTrace();}
|
||||
}
|
||||
}
|
@ -100,7 +100,8 @@ public class Util {
|
||||
*/
|
||||
public int getNeeded(World w) {
|
||||
//FIXME make sure to remove excluded players
|
||||
return Math.max(0, (int) Math.ceil(w.getPlayers().size() * (config.getDouble("values.percent") / 100) - this.getSleeping(w)));
|
||||
try {return Math.max(0, (int) Math.ceil(w.getPlayers().size() * (config.getDouble("values.percent") / 100) - this.getSleeping(w)));}
|
||||
catch (NullPointerException e) {return 0;}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -108,7 +109,8 @@ public class Util {
|
||||
* @param world World to check player count for
|
||||
*/
|
||||
public int getOnline(World w) {
|
||||
return Math.max(0, w.getPlayers().size() - getExcluded(w).size());
|
||||
try {return Math.max(0, w.getPlayers().size() - getExcluded(w).size());}
|
||||
catch (NullPointerException e) {return 0;}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -213,4 +215,17 @@ public class Util {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the current player activity (AFK system)
|
||||
* @param p Player to update activity for
|
||||
*/
|
||||
public void updateActivity(Player p) {
|
||||
if (afk.contains(p)) {
|
||||
afk.remove(p);
|
||||
p.setPlayerListName(ChatColor.translateAlternateColorCodes('&', p.getName()));
|
||||
Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.unafk").replace("[player]", p.getName())));
|
||||
}
|
||||
activity.put(p, System.currentTimeMillis());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user