diff --git a/src/main/java/sky/afk/ActivityInfo.java b/src/main/java/sky/afk/ActivityInfo.java index d305fd5..2731998 100644 --- a/src/main/java/sky/afk/ActivityInfo.java +++ b/src/main/java/sky/afk/ActivityInfo.java @@ -19,11 +19,13 @@ public class ActivityInfo{ public float mouseY; public boolean afk; + public int warns; public long lastBuildActivityTime; public ActivityInfo(Player player){ update(player); + warns=0; } public void update(Player player){ @@ -34,14 +36,25 @@ public void update(Player player){ mouseX = player.mouseX(); mouseY = player.mouseY(); + } public void ifAfk(){ if(afk){ - Player player = Objects.requireNonNull(Groups.player.find(p -> Objects.equals(p.uuid(), uuid)), "User with uuid '" + uuid + "' not found"); - Call.sendMessage(Strings.format("[lightgray]Player @[lightgray] at now active!", NetClient.colorizeName(player.id(), player.name()))); afk = false; } + if (warns>0){ + if (warns>1) { + try { + Player player = Objects.requireNonNull(Groups.player.find(p -> Objects.equals(p.uuid(), uuid)), "User with uuid '" + uuid + "' not found"); + if (player != null) { + player.sendMessage(Strings.format("Player @[lightgray] is not afk anymore! Warns reset (0/" + (config.warnthreshold + 1) + ")", NetClient.colorizeName(player.id(), player.name()))); + } + } catch(Exception ignored){ + } + } + warns=0; + } } public boolean isOldMessage(Player player){ diff --git a/src/main/java/sky/afk/AfkPlugin.java b/src/main/java/sky/afk/AfkPlugin.java index a31b38b..64c1c21 100644 --- a/src/main/java/sky/afk/AfkPlugin.java +++ b/src/main/java/sky/afk/AfkPlugin.java @@ -5,6 +5,7 @@ import arc.struct.*; import arc.util.*; import com.google.gson.*; +import mindustry.Vars; import mindustry.core.*; import mindustry.game.EventType.*; import mindustry.gen.*; @@ -24,7 +25,7 @@ public class AfkPlugin extends Plugin{ protected static Config config; public AfkPlugin(){ - Fi cfg = dataDirectory.child("afk-plugin.json"); + Fi cfg = dataDirectory.child("mods/afk-plugin.json"); if(!cfg.exists()){ config = new Config(); cfg.writeString(gson.toJson(config)); @@ -37,14 +38,18 @@ public void init(){ Events.on(TapEvent.class, event -> { Player player = event.player; - ActivityInfo activity = activities.get(player.uuid(), () -> new ActivityInfo(player)); - activity.ifAfk(); + if(player != null) { + ActivityInfo activity = activities.get(player.uuid(), () -> new ActivityInfo(player)); + activity.ifAfk(); + } }); Events.on(PlayerChatEvent.class, event -> { Player player = event.player; - ActivityInfo activity = activities.get(player.uuid(), () -> new ActivityInfo(player)); - activity.ifAfk(); + if(player != null) { + ActivityInfo activity = activities.get(player.uuid(), () -> new ActivityInfo(player)); + activity.ifAfk(); + } }); Events.on(PlayerJoin.class, event -> activities.put(event.player.uuid(), new ActivityInfo(event.player))); @@ -54,9 +59,11 @@ public void init(){ Events.on(BlockBuildBeginEvent.class, event -> { if(event.unit.isPlayer()){ Player player = (Player)event.unit.controller(); - ActivityInfo activity = activities.get(player.uuid(), () -> new ActivityInfo(player)); - activity.lastBuildActivityTime = Time.millis(); - activity.ifAfk(); + if(player != null) { + ActivityInfo activity = activities.get(player.uuid(), () -> new ActivityInfo(player)); + activity.lastBuildActivityTime = Time.millis(); + activity.ifAfk(); + } } }); @@ -71,37 +78,69 @@ public void init(){ Timer.schedule(() -> { if(state.isPlaying()){ + //Call.sendMessage("running afk timer"); for(Player player : Groups.player){ + if (player==null) continue; ActivityInfo activity = activities.get(player.uuid(), () -> new ActivityInfo(player)); Log.debug("@ | @ | @ | @", !activity.afk, activity.isStand(player), activity.isOldMessage(player), Time.timeSinceMillis(activity.lastBuildActivityTime) < config.inactivityTime); if(!activity.afk && activity.isStand(player) && activity.isOldMessage(player) ^ Time.timeSinceMillis(activity.lastBuildActivityTime) < config.inactivityTime){ - Call.sendMessage(Strings.format("[lightgray]Player @[lightgray] at now inactive!", NetClient.colorizeName(player.id(), player.name()))); - activity.afk = true; - if(config.enableKick){ - if(!player.admin() || player.admin() && !config.ignoreAdmins){ - player.kick("You have been kicked for inactive from the server!", (int)config.kickDuration); + + if (activity.warns >(config.warnthreshold+1)) { + activity.afk = true; + if (config.enableKick) { + if (!player.admin() || player.admin && !config.ignoreAdmins) { + Call.sendMessage(Strings.format("[lightgray]Player @[lightgray] is kicked for afk!", NetClient.colorizeName(player.id(), player.name()))); + //player.kick("You have been kicked for being afk!", (int)config.kickDuration); + Vars.net.pingHost(config.kickurl, config.kickport, host -> { + Call.infoMessage(player.con, "You have been moved into hub for AFK ;-;"); + Call.connect(player.con, config.kickurl, config.kickport); + }, (e) -> { + }); + }else{ + player.sendMessage(Strings.format("[lightgray]Player @[lightgray] is afk!!", NetClient.colorizeName(player.id(), player.name()))); + } + }else{ + player.sendMessage(Strings.format("[lightgray]Player @[lightgray] is afk!", NetClient.colorizeName(player.id(), player.name()))); + } + }else{ + if (activity.warns != 0) { + player.sendMessage(Strings.format("[lightgray]Player @[lightgray] is afk! Warning (" + (activity.warns) + "/" + (config.warnthreshold+1) + ")", NetClient.colorizeName(player.id(), player.name()))); + //Call.infoMessage(player.con,"AFK Warning! ("+(activity.warns+1)+"/"+config.warnthreshold+")"); } + activity.warns++; + activity.update(player); } - }else{ + }else if (!activity.isStand(player)) { + activity.ifAfk(); activity.update(player); + }else{ + activity.warns=0; + activity.update(player); + } } } - } - }, 5, 15); + + }, 2, 60); } static class Config{ /** Enable auto kick */ - public boolean enableKick; + public boolean enableKick = true; /** Kick inactive admins */ public boolean ignoreAdmins = true; - /** Inactivity player time. In milliseconds. Default 30 minutes */ - public long inactivityTime = 1000 * 60 * 30; - - /** Kick duration. Default 1 minute */ - public long kickDuration = 1000 * 60; + /** Inactivity player time. In milliseconds. Default 1 minutes */ + public long inactivityTime = 1000 * 60 * 1; + + /** warning threshold, number of times for warnings before kick*/ + public int warnthreshold = 3;// first warn is "free" , so total warns is warnthreshold+1 + /** Kick duration. Default 1 second */ + public long kickDuration = 1000 * 1; + /** add kick url, kicks player into hub url */ + public String kickurl = "alexmindustryhub.ddns.net"; + /** add kick url, kicks player into hub port */ + public int kickport = 6568; } } diff --git a/src/main/resources/plugin.json b/src/main/resources/plugin.json index 27734f9..8f8b3eb 100644 --- a/src/main/resources/plugin.json +++ b/src/main/resources/plugin.json @@ -3,5 +3,5 @@ "author": "Skat", "main": "sky.afk.AfkPlugin", "description": "Auto kick inactive players.", - "version": 0.3 + "version": 0.4 }