Skip to content
This repository has been archived by the owner on Feb 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #57 from kylejuliandev/tws.management/add-requeste…
Browse files Browse the repository at this point in the history
…d-enhancements

Add requested enhancements for AFK and Whitelisting
  • Loading branch information
kylejuliandev authored Dec 27, 2021
2 parents e3aefc3 + 19b9e40 commit 9c3b709
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 37 deletions.
11 changes: 7 additions & 4 deletions src/main/java/dev/kylejulian/twsmanagement/afk/AfkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ public void run() {
private boolean canKickPlayer(int afkKickTime, int afkTime) {
// Player has to be inactive for both the AFK time and AFK Kick time
if (this.getPlayerAfkMinutes() >= (afkKickTime + afkTime)) {
int numberOfPlayersOnline = this.plugin.getServer().getOnlinePlayers().size();
int numberOfRequiredPlayersToKick = this.afkConfig.getPlayerCountNeededForKick();

return numberOfPlayersOnline >= numberOfRequiredPlayersToKick;
// Only kick if it is enabled in the configuration
if (this.afkConfig.getAfkKick()) {
int numberOfPlayersOnline = this.plugin.getServer().getOnlinePlayers().size();
int numberOfRequiredPlayersToKick = this.afkConfig.getPlayerCountNeededForKick();

return numberOfPlayersOnline >= numberOfRequiredPlayersToKick;
}
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ public class AfkConfigModel {
private String kickMessage;
private int playerCountNeededForKick;
private String[] events;
private boolean sendPlayerAfkMessage;
private boolean afkKick;

public int getTimeMinutes() {
return this.timeMinutes;
}

public int getKickTimeMinutes() {
return this.kickTimeMinutes;
}
Expand All @@ -27,6 +29,14 @@ public int getPlayerCountNeededForKick() {
public String[] getEvents() {
return this.events;
}

public boolean getSendPlayerAfkMessage() {
return this.sendPlayerAfkMessage;
}

public boolean getAfkKick() {
return this.afkKick;
}

public void setTimeMinutes(int timeMinutes) {
this.timeMinutes = timeMinutes;
Expand All @@ -47,4 +57,12 @@ public void setPlayerCountNeededForKick(int playerCountNeededForKick) {
public void setEvents(String[] events) {
this.events = events;
}

public void setSendPlayerAfkMessage(boolean sendPlayerAfkMessage) {
this.sendPlayerAfkMessage = sendPlayerAfkMessage;
}

public void setAfkKick(boolean afkKick) {
this.afkKick = afkKick;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ private boolean verifyConfigurationDefaults(@NotNull ConfigModel configModel) {
afkConfigModel.setKickTimeMinutes(5);
afkConfigModel.setPlayerCountNeededForKick(25);
afkConfigModel.setTimeMinutes(5);
afkConfigModel.setSendPlayerAfkMessage(true);
afkConfigModel.setAfkKick(true);

configModel.setAfkConfig(afkConfigModel);
saveRequired = true;
Expand Down Expand Up @@ -120,6 +122,7 @@ private boolean verifyConfigurationDefaults(@NotNull ConfigModel configModel) {
whitelistConfigModel.setEnabled(true);
whitelistConfigModel.setInactivity(Duration.ofDays(14));
whitelistConfigModel.setCheck(Duration.ofMinutes(30));
whitelistConfigModel.setWriteLogFile(true);

configModel.setWhitelistConfig(whitelistConfigModel);
saveRequired = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class WhitelistConfigModel {

private boolean enabled;
private boolean enabled, writeLogFile;
private Duration inactivity, check;

public void setEnabled(boolean enabled) { this.enabled = enabled; }
Expand All @@ -19,6 +19,8 @@ public class WhitelistConfigModel {
*/
public void setCheck(@NotNull Duration duration) { this.check = duration; }

public void setWriteLogFile(boolean writeLogFile) { this.writeLogFile = writeLogFile; }

public boolean getEnabled() { return this.enabled; }

public @NotNull Duration getInactivity() { return this.inactivity; }
Expand All @@ -28,4 +30,6 @@ public class WhitelistConfigModel {
* @return Duration between checks
*/
public @NotNull Duration getCheck() { return this.check; }

public boolean getWriteLogFile() { return this.writeLogFile; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,15 @@ public void onAfk(@NotNull AfkEvent e) {
if (!TabPluginHelper.hasTabSuffix(playerId)) {
Player player = this.plugin.getServer().getPlayer(playerId);
if (player != null) {
player.sendMessage(ChatColor.DARK_RED + "You are now AFK");
Runnable tabTask = () -> TabPluginHelper.setTabSuffix(playerId,
ChatColor.GRAY + "[" + ChatColor.RED + "AFK" + ChatColor.GRAY + "] " +
ChatColor.RESET);
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, tabTask);
AfkConfigModel afkConfig = configManager.getConfig().getAfkConfig();
if (afkConfig.getSendPlayerAfkMessage()) {
player.sendMessage(ChatColor.DARK_RED + "You are now AFK");
}

Runnable tabTask = () -> TabPluginHelper.setTabSuffix(playerId,
ChatColor.GRAY + "[" + ChatColor.RED + "AFK" + ChatColor.GRAY + "] " + ChatColor.RESET);

this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, tabTask);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PlayerWhitelisted {

public PlayerWhitelisted(@NotNull UUID playerId, Boolean whitelist) {
this.playerId = playerId;
this.whitelist = whitelist;
this.setWhitelist(whitelist);;
}

public UUID getPlayerId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -81,8 +83,7 @@ public void run() {
CompletableFuture.allOf(completableFutures);
}

private @Nullable
LuckPermsHelper getLuckPermsHelper() {
private @Nullable LuckPermsHelper getLuckPermsHelper() {
LuckPermsHelper luckPermsHelper = null;
try {
// Fully qualified names as to avoid issues creating the class when LP isn't deployed with this plugin
Expand All @@ -99,9 +100,7 @@ LuckPermsHelper getLuckPermsHelper() {
return luckPermsHelper;
}

private @NotNull
CompletableFuture<Void> getPlayerNeedsToUnwhitelistedFuture(
@NotNull CompletableFuture<PlayerWhitelisted> future) {
private @NotNull CompletableFuture<Void> getPlayerNeedsToUnwhitelistedFuture(@NotNull CompletableFuture<PlayerWhitelisted> future) {
return future.thenAcceptAsync(player -> {
if (!player.getWhitelist()) { // Player needs to be unwhitelisted
Runnable unwhitelistPlayerTask = () -> {
Expand All @@ -114,43 +113,59 @@ CompletableFuture<Void> getPlayerNeedsToUnwhitelistedFuture(
});
}

private @NotNull
CompletableFuture<PlayerWhitelisted>
getVerifyWhitelistedPlayerFuture(@NotNull LocalDateTime unwhitelistDate,
@NotNull OfflinePlayer whitelistedPlayer,
@NotNull CompletableFuture<Boolean> future) {
private @NotNull CompletableFuture<PlayerWhitelisted> getVerifyWhitelistedPlayerFuture(
@NotNull LocalDateTime unwhitelistDate,
@NotNull OfflinePlayer whitelistedPlayer,
@NotNull CompletableFuture<Boolean> future) {

return future.thenApplyAsync(result -> {
PlayerWhitelisted playerWhitelisted =
new PlayerWhitelisted(whitelistedPlayer.getUniqueId(), true);
PlayerWhitelisted playerWhitelisted = new PlayerWhitelisted(whitelistedPlayer.getUniqueId(), true);

if (result) { // Player has Permission
return playerWhitelisted;
} else {
long lastPlayTime = whitelistedPlayer.getLastLogin();

// If Player is exempt in database

if (lastPlayTime == 0L) { // Player has not joined
return playerWhitelisted;
}

LocalDateTime lastPlayDate = LocalDateTime.ofInstant(Instant.ofEpochMilli(lastPlayTime),
ZoneId.systemDefault());
long days = lastPlayDate.until(LocalDateTime.now(), ChronoUnit.DAYS);
LocalDateTime now = LocalDateTime.now();
LocalDateTime lastPlayDate = LocalDateTime.ofInstant(Instant.ofEpochMilli(lastPlayTime), ZoneId.systemDefault());
Duration inactivityPeriod = Duration.between(lastPlayDate, now);

if (lastPlayDate.isBefore(unwhitelistDate)) {
this.plugin.getLogger().log(Level.INFO,
"Player ({0}) has been unwhitelisted due to inactivity! " +
"Has been inactive for : {1} days!",
new Object[]{whitelistedPlayer.getName(), days});

if (whitelistConfigModel.getWriteLogFile()) {
File logFile = new File(this.plugin.getDataFolder(), "unwhitelists.log");
try {
logFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}

try {
FileWriter printWriter = new FileWriter(logFile, true);
// Time of Unwhitelist, Name, Days Inactive, Last Online Date
printWriter.append(now + " | " + whitelistedPlayer.getName() + " | inactive for: " +
inactivityPeriod.toDays() + " days | Last online at " + lastPlayDate);
printWriter.append(System.lineSeparator());
printWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}

this.plugin.getLogger().log(Level.INFO, "Player ({0}) has been unwhitelisted due to inactivity! Has been inactive for : {1} days!",
new Object[]{whitelistedPlayer.getName(), inactivityPeriod.toDays()});

playerWhitelisted.setWhitelist(false);
return playerWhitelisted;
}

this.plugin.getLogger().log(Level.FINE,
"Player ({0}) will not be unwhitelisted! Has been inactive for : {1} days!",
new Object[]{whitelistedPlayer.getName(), days});
new Object[]{whitelistedPlayer.getName(), inactivityPeriod.toDays()});
}

return playerWhitelisted;
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"kickMessage": "You have been kicked for being AFK for too long!",
"playerCountNeededForKick": 25,
"events": ["onPlayerChat", "onPlayerInteractEntity", "onPlayerInteract", "onPlayerBedEnter", "onPlayerChangedWorld", "onPlayerEditBook", "onPlayerDropItem", "onPlayerItemBreak", "onPlayerShearEntity", "onPlayerToggleFlight", "onPlayerToggleSprint", "onPlayerToggleSneak", "onPlayerUnleashEntity", "onPlayerBucketFill", "onPlayerBucketEmpty", "onPlayerMove", "onPlayerExpChange"
]
],
"sendPlayerAfkMessage": true,
"afkKick": true
},
"nightResetConfig": {
"enabled": true
Expand All @@ -17,7 +19,8 @@
"whitelistConfig": {
"enabled": true,
"inactivity": "P14DT0S",
"check": "PT30M"
"check": "PT30M",
"writeLogFile": true
},
"databaseConfig": {
"name": "tws-local.db",
Expand Down

0 comments on commit 9c3b709

Please sign in to comment.