From 72be3b4c54144dfb93ff0a57e6bcec186b68940e Mon Sep 17 00:00:00 2001 From: "alex.mindustry" Date: Thu, 31 Dec 2020 17:17:40 +0800 Subject: [PATCH 1/8] lower kick duration --- src/main/java/sky/afk/AfkPlugin.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/sky/afk/AfkPlugin.java b/src/main/java/sky/afk/AfkPlugin.java index bf03152..81a8909 100644 --- a/src/main/java/sky/afk/AfkPlugin.java +++ b/src/main/java/sky/afk/AfkPlugin.java @@ -95,10 +95,10 @@ static class Config{ /** Kick inactive admins */ public boolean ignoreAdmins = true; - /** Inactivity player time. In milliseconds. Default 30 minutes */ - public long inactivityTime = 1000 * 60 * 30; + /** Inactivity player time. In milliseconds. Default 5 minutes */ + public long inactivityTime = 1000 * 60 * 5; - /** Kick duration. Default 1 minute */ - public long kickDuration = 1000 * 60; + /** Kick duration. Default 1 second */ + public long kickDuration = 1000 * 1; } } From fba40b7375c884ae191e8add8b6efbf28f818396 Mon Sep 17 00:00:00 2001 From: "alex.mindustry" Date: Thu, 31 Dec 2020 17:18:45 +0800 Subject: [PATCH 2/8] add kick url --- src/main/java/sky/afk/AfkPlugin.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/sky/afk/AfkPlugin.java b/src/main/java/sky/afk/AfkPlugin.java index 81a8909..ec042e3 100644 --- a/src/main/java/sky/afk/AfkPlugin.java +++ b/src/main/java/sky/afk/AfkPlugin.java @@ -100,5 +100,7 @@ static class Config{ /** Kick duration. Default 1 second */ public long kickDuration = 1000 * 1; + /** add kick url, kicks player into hub url */ + public String kickurl = ""; } } From d71f0e402f33c52598a2c9931acd79801d7591b6 Mon Sep 17 00:00:00 2001 From: "alex.mindustry" Date: Fri, 1 Jan 2021 09:56:35 +0800 Subject: [PATCH 3/8] checked out patch 2 --- src/main/java/sky/afk/ActivityInfo.java | 1 + src/main/java/sky/afk/AfkPlugin.java | 59 ++++++++++++++++++------- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/main/java/sky/afk/ActivityInfo.java b/src/main/java/sky/afk/ActivityInfo.java index 5e15e57..7c4091f 100644 --- a/src/main/java/sky/afk/ActivityInfo.java +++ b/src/main/java/sky/afk/ActivityInfo.java @@ -19,6 +19,7 @@ public class ActivityInfo{ public float mouseY; public boolean afk; + public int warns; public long lastBuildActivityTime; diff --git a/src/main/java/sky/afk/AfkPlugin.java b/src/main/java/sky/afk/AfkPlugin.java index 9902fc7..83e808c 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)); @@ -62,43 +63,69 @@ public void init(){ Events.on(ConfigEvent.class, event -> { Player player = event.player; - 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(); + } }); +// Events.on(ConfigEvent.class, event -> { +// Player player = event.player; +// ActivityInfo activity = activities.get(player.uuid(), () -> new ActivityInfo(player)); +// activity.lastBuildActivityTime = Time.millis(); +// activity.ifAfk(); +// }); + Timer.schedule(() -> { if(state.isPlaying()){ for(Player player : Groups.player){ ActivityInfo activity = activities.get(player.uuid(), () -> new ActivityInfo(player)); - if(!activity.afk && activity.isStand(player) || activity.isOldMessage(player)){ - 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); + 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){ + if (activity.warns >1) { + 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); + Vars.net.pingHost(config.kickurl, config.kickport, host -> { + Call.infoMessage(player.con, "You have been kicked for AFK ;-;"); + Call.connect(player.con, config.kickurl, config.kickport); + }, (e) -> { + }); + } } + }else{ + Call.sendMessage(Strings.format("warn number"+activity.warns+"[lightgray]Player @[lightgray] at now inactive!", NetClient.colorizeName(player.id(), player.name()))); + activity.warns ++; + activity.update(player); } }else{ activity.update(player); + activity.warns=0; } } } - }, 5, 15); + }, 2, 60); } static class Config{ /** Enable auto kick */ - public boolean enableKick; + public boolean enableKick = false; /** Kick inactive admins */ public boolean ignoreAdmins = true; - /** Inactivity player time. In milliseconds. Default 30 minutes */ - public long inactivityTime = 1000 * 60 * 30; + /** Inactivity player time. In milliseconds. Default 5 minutes */ + public long inactivityTime = 1000*20;//1000 * 60 * 5; - /** Kick duration. Default 3 minutes */ - public long kickDuration = 1000 * 60; + /** 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; } } From 7f0aca522044cc10ebfa07fa1458e189cc7def03 Mon Sep 17 00:00:00 2001 From: "alex.mindustry" Date: Fri, 1 Jan 2021 10:19:49 +0800 Subject: [PATCH 4/8] added not null for block build begin event --- src/main/java/sky/afk/AfkPlugin.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/sky/afk/AfkPlugin.java b/src/main/java/sky/afk/AfkPlugin.java index 5ba7009..f573659 100644 --- a/src/main/java/sky/afk/AfkPlugin.java +++ b/src/main/java/sky/afk/AfkPlugin.java @@ -55,9 +55,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(); + } } }); @@ -76,7 +78,7 @@ public void init(){ 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){ - if (activity.warns >1) { + if (activity.warns >config.warnthreshold) { Call.sendMessage(Strings.format("[lightgray]Player @[lightgray] at now inactive!", NetClient.colorizeName(player.id(), player.name()))); activity.afk = true; if (config.enableKick) { @@ -114,6 +116,8 @@ static class Config{ /** Inactivity player time. In milliseconds. Default 5 minutes */ public long inactivityTime = 1000*20;//1000 * 60 * 5; + /** warning threshold, number of times for warnings before kick*/ + public int warnthreshold = 2; /** Kick duration. Default 1 second */ public long kickDuration = 1000 * 1; /** add kick url, kicks player into hub url */ From e6abbe9d4e8942243be18aaa1e17aaecf43df6d3 Mon Sep 17 00:00:00 2001 From: "alex.mindustry" Date: Fri, 1 Jan 2021 18:38:44 +0800 Subject: [PATCH 5/8] fixed bugs --- src/main/java/sky/afk/ActivityInfo.java | 17 +++++++- src/main/java/sky/afk/AfkPlugin.java | 54 ++++++++++++++++--------- src/main/resources/plugin.json | 2 +- 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/main/java/sky/afk/ActivityInfo.java b/src/main/java/sky/afk/ActivityInfo.java index a91310c..1e7612f 100644 --- a/src/main/java/sky/afk/ActivityInfo.java +++ b/src/main/java/sky/afk/ActivityInfo.java @@ -25,6 +25,7 @@ public class ActivityInfo{ public ActivityInfo(Player player){ update(player); + warns=0; } public void update(Player player){ @@ -35,14 +36,26 @@ 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()))); +// Player player = Objects.requireNonNull(Groups.player.find(p -> Objects.equals(p.uuid(), uuid)), "User with uuid '" + uuid + "' not found"); +// if (player!=null) { +// Call.sendMessage(Strings.format("@[lightgray] at now active! Warns reseted (0/" + config.warnthreshold + ")", NetClient.colorizeName(player.id(), player.name()))); +// } afk = false; } + if (warns>0){ + if (warns>1) { + Player player = Objects.requireNonNull(Groups.player.find(p -> Objects.equals(p.uuid(), uuid)), "User with uuid '" + uuid + "' not found"); + if (player != null) { + Call.sendMessage(Strings.format("Player @[lightgray] is not afk anymore! Warns reseted (0/" + (config.warnthreshold + 1) + ")", NetClient.colorizeName(player.id(), player.name()))); + } + } + 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 f573659..f949d11 100644 --- a/src/main/java/sky/afk/AfkPlugin.java +++ b/src/main/java/sky/afk/AfkPlugin.java @@ -38,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))); @@ -74,50 +78,64 @@ 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){ - if (activity.warns >config.warnthreshold) { - Call.sendMessage(Strings.format("[lightgray]Player @[lightgray] at now inactive!", NetClient.colorizeName(player.id(), player.name()))); + + if (activity.warns >(config.warnthreshold+1)) { 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 (!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 kicked for AFK ;-;"); + Call.infoMessage(player.con, "You have been moved into hub for AFK ;-;"); Call.connect(player.con, config.kickurl, config.kickport); }, (e) -> { }); + }else{ + Call.sendMessage(Strings.format("[lightgray]Player @[lightgray] is afk!!", NetClient.colorizeName(player.id(), player.name()))); } + }else{ + Call.sendMessage(Strings.format("[lightgray]Player @[lightgray] is afk!", NetClient.colorizeName(player.id(), player.name()))); } }else{ - Call.sendMessage(Strings.format("warn number"+activity.warns+"[lightgray]Player @[lightgray] at now inactive!", NetClient.colorizeName(player.id(), player.name()))); - activity.warns ++; + if (activity.warns != 0) { + Call.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); - activity.warns=0; + }else{ + activity.warns=0; + activity.update(player); + } } } - } + }, 2, 60); } static class Config{ /** Enable auto kick */ - public boolean enableKick = false; + public boolean enableKick = true; /** Kick inactive admins */ public boolean ignoreAdmins = true; - /** Inactivity player time. In milliseconds. Default 5 minutes */ - public long inactivityTime = 1000*20;//1000 * 60 * 5; + /** 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 = 2; + 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 */ 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 } From ebc841c79d3f3348b933970241d40e1a0243840b Mon Sep 17 00:00:00 2001 From: "alex.mindustry" Date: Sat, 2 Jan 2021 16:30:42 +0800 Subject: [PATCH 6/8] show afk notifications only to player, not everyone --- src/main/java/sky/afk/ActivityInfo.java | 6 +----- src/main/java/sky/afk/AfkPlugin.java | 6 +++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/sky/afk/ActivityInfo.java b/src/main/java/sky/afk/ActivityInfo.java index 1e7612f..97d9e2d 100644 --- a/src/main/java/sky/afk/ActivityInfo.java +++ b/src/main/java/sky/afk/ActivityInfo.java @@ -41,17 +41,13 @@ public void update(Player player){ public void ifAfk(){ if(afk){ -// Player player = Objects.requireNonNull(Groups.player.find(p -> Objects.equals(p.uuid(), uuid)), "User with uuid '" + uuid + "' not found"); -// if (player!=null) { -// Call.sendMessage(Strings.format("@[lightgray] at now active! Warns reseted (0/" + config.warnthreshold + ")", NetClient.colorizeName(player.id(), player.name()))); -// } afk = false; } if (warns>0){ if (warns>1) { Player player = Objects.requireNonNull(Groups.player.find(p -> Objects.equals(p.uuid(), uuid)), "User with uuid '" + uuid + "' not found"); if (player != null) { - Call.sendMessage(Strings.format("Player @[lightgray] is not afk anymore! Warns reseted (0/" + (config.warnthreshold + 1) + ")", NetClient.colorizeName(player.id(), player.name()))); + player.sendMessage(Strings.format("Player @[lightgray] is not afk anymore! Warns reseted (0/" + (config.warnthreshold + 1) + ")", NetClient.colorizeName(player.id(), player.name()))); } } warns=0; diff --git a/src/main/java/sky/afk/AfkPlugin.java b/src/main/java/sky/afk/AfkPlugin.java index f949d11..64c1c21 100644 --- a/src/main/java/sky/afk/AfkPlugin.java +++ b/src/main/java/sky/afk/AfkPlugin.java @@ -97,14 +97,14 @@ public void init(){ }, (e) -> { }); }else{ - Call.sendMessage(Strings.format("[lightgray]Player @[lightgray] is afk!!", NetClient.colorizeName(player.id(), player.name()))); + player.sendMessage(Strings.format("[lightgray]Player @[lightgray] is afk!!", NetClient.colorizeName(player.id(), player.name()))); } }else{ - Call.sendMessage(Strings.format("[lightgray]Player @[lightgray] is afk!", NetClient.colorizeName(player.id(), player.name()))); + player.sendMessage(Strings.format("[lightgray]Player @[lightgray] is afk!", NetClient.colorizeName(player.id(), player.name()))); } }else{ if (activity.warns != 0) { - Call.sendMessage(Strings.format("[lightgray]Player @[lightgray] is afk! Warning (" + (activity.warns) + "/" + (config.warnthreshold+1) + ")", NetClient.colorizeName(player.id(), player.name()))); + 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++; From 29216ca530208fdc107da8ea097e1f2e5fe16af6 Mon Sep 17 00:00:00 2001 From: "alex.mindustry" Date: Sun, 3 Jan 2021 13:19:38 +0800 Subject: [PATCH 7/8] grammarrrrrrrr --- src/main/java/sky/afk/ActivityInfo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/sky/afk/ActivityInfo.java b/src/main/java/sky/afk/ActivityInfo.java index 97d9e2d..b42af9c 100644 --- a/src/main/java/sky/afk/ActivityInfo.java +++ b/src/main/java/sky/afk/ActivityInfo.java @@ -47,7 +47,7 @@ public void ifAfk(){ if (warns>1) { 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 reseted (0/" + (config.warnthreshold + 1) + ")", NetClient.colorizeName(player.id(), player.name()))); + player.sendMessage(Strings.format("Player @[lightgray] is not afk anymore! Warns reset (0/" + (config.warnthreshold + 1) + ")", NetClient.colorizeName(player.id(), player.name()))); } } warns=0; From 67bd3047f13b1010e1b2ff8f9cc22e3603fc6664 Mon Sep 17 00:00:00 2001 From: "alex.mindustry" Date: Thu, 28 Jan 2021 17:30:35 +0800 Subject: [PATCH 8/8] fixed unhandled exception --- src/main/java/sky/afk/ActivityInfo.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/sky/afk/ActivityInfo.java b/src/main/java/sky/afk/ActivityInfo.java index b42af9c..2731998 100644 --- a/src/main/java/sky/afk/ActivityInfo.java +++ b/src/main/java/sky/afk/ActivityInfo.java @@ -45,9 +45,12 @@ public void ifAfk(){ } if (warns>0){ if (warns>1) { - 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()))); + 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;