Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a warning system #7

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
17 changes: 15 additions & 2 deletions src/main/java/sky/afk/ActivityInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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){
Expand Down
83 changes: 61 additions & 22 deletions src/main/java/sky/afk/AfkPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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));
Expand All @@ -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)));
Expand All @@ -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();
}
}
});

Expand All @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont understand what you mean.
do u mean the Player player in Groups.player will always be not null?

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;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"author": "Skat",
"main": "sky.afk.AfkPlugin",
"description": "Auto kick inactive players.",
"version": 0.3
"version": 0.4
}