Skip to content

Commit

Permalink
Merge pull request #16 from NightMirror21/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
NightMirror21 authored Aug 15, 2023
2 parents 5c330ca + 765ad92 commit 3fb5ee8
Show file tree
Hide file tree
Showing 30 changed files with 1,199 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sourceCompatibility = javaVersion;
targetCompatibility = javaVersion;

group = 'ru.nightmirror'
version = '1.0'
version = '1.0.0'
description = 'WhitelistByTime'

repositories {
Expand Down
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sourceCompatibility = javaVersion;
targetCompatibility = javaVersion;

group 'ru.nightmirror'
version '1.0'
version '1.0.0'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,31 @@ public void start() {

@Override
public void run() {
playerAccessor.getPlayers().thenAccept((players) -> {
playerAccessor.getPlayers().thenCompose(players -> {

long currentMilliseconds = System.currentTimeMillis();
List<WLPlayer> toRemove = players.stream().filter(player -> player.getUntil() != -1L && player.getUntil() <= currentMilliseconds).toList();
playerAccessor.delete(toRemove).join();
}).thenRun(() -> {
playersOnSeverAccessor.getPlayersOnServer().forEach(nickname -> {
playerAccessor.getPlayer(nickname).thenAccept(playerOptional -> {
if (playerOptional.isEmpty()) playersOnSeverAccessor.kickPlayer(nickname);
});
});
List<String> onServer = playersOnSeverAccessor.getPlayersOnServer();

boolean caseSensitive = playersOnSeverAccessor.isCaseSensitiveEnabled();

if (caseSensitive) {
for (String nickname : onServer) {
boolean toKick = players.stream().noneMatch(player -> player.getNickname().equals(nickname));
if (toKick) {
playersOnSeverAccessor.kickPlayer(nickname);
}
}
}

for (WLPlayer player : toRemove) {
boolean toKick = onServer.stream().anyMatch(nickname -> (caseSensitive && player.getNickname().equals(nickname) || (!caseSensitive && player.getNickname().equalsIgnoreCase(nickname))));
if (toKick) {
playersOnSeverAccessor.kickPlayer(player.getNickname());
}
}

return playerAccessor.delete(toRemove);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
public interface PlayersOnSeverAccessor {
List<String> getPlayersOnServer();
void kickPlayer(@NotNull String nickname);
boolean isCaseSensitiveEnabled();
}
59 changes: 59 additions & 0 deletions folia/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
plugins {
id 'java'
id 'com.github.johnrengelman.shadow' version '7.1.2'
}

def javaVersion = JavaVersion.VERSION_17;
sourceCompatibility = javaVersion;
targetCompatibility = javaVersion;

group = 'ru.nightmirror'
version = '1.0.0'
description = 'WhitelistByTime-Folia'

compileJava.options.encoding = "UTF-8"
compileTestJava.options.encoding = "UTF-8"

repositories {
mavenCentral()

maven {
name 'dmulloy2-repo'
url 'https://repo.dmulloy2.net/repository/public/'
}

maven {
name 'papermc-repo'
url 'https://repo.papermc.io/repository/maven-public/'
}

maven {
url = uri('https://repo.extendedclip.com/content/repositories/placeholderapi/')
}

maven {
url = uri('https://repo.maven.apache.org/maven2/')
}
}

dependencies {
shadow project(path: ':core', configuration: 'default')
compileOnly 'dev.folia:folia-api:1.19.4-R0.1-SNAPSHOT'
compileOnly 'net.kyori:adventure-text-minimessage:4.14.0'
implementation 'org.bstats:bstats-bukkit:3.0.1'
compileOnly 'me.clip:placeholderapi:2.11.3'
compileOnly 'org.jetbrains:annotations:24.0.1'
compileOnly 'org.projectlombok:lombok:1.18.26'
annotationProcessor 'org.projectlombok:lombok:1.18.26'
}

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
}

shadowJar {
relocate 'org.bstats', 'ru.nightmirror.wlbytime'
archiveClassifier.set(null)
configurations = [project.configurations.runtimeClasspath, project.configurations.shadow]
archiveFileName.set(project.description + '-' + project.version + '.jar')
}
181 changes: 181 additions & 0 deletions folia/src/main/java/ru/nightmirror/wlbytime/WhitelistByTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package ru.nightmirror.wlbytime;

import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
import org.bstats.bukkit.Metrics;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.java.JavaPlugin;
import ru.nightmirror.wlbytime.common.checker.PlayersChecker;
import ru.nightmirror.wlbytime.common.command.CommandsExecutor;
import ru.nightmirror.wlbytime.common.command.WhitelistCommandExecutor;
import ru.nightmirror.wlbytime.common.command.WhitelistTabCompleter;
import ru.nightmirror.wlbytime.common.covertors.time.TimeConvertor;
import ru.nightmirror.wlbytime.common.covertors.time.TimeUnitsConvertorSettings;
import ru.nightmirror.wlbytime.common.database.WLDatabase;
import ru.nightmirror.wlbytime.common.database.misc.DatabaseSettings;
import ru.nightmirror.wlbytime.common.listeners.PlayerKicker;
import ru.nightmirror.wlbytime.common.listeners.PlayerLoginListener;
import ru.nightmirror.wlbytime.common.listeners.WhitelistCmdListener;
import ru.nightmirror.wlbytime.common.placeholder.PlaceholderHook;
import ru.nightmirror.wlbytime.common.utils.ConfigUtils;
import ru.nightmirror.wlbytime.interfaces.IWhitelist;
import ru.nightmirror.wlbytime.interfaces.checker.Checker;

import java.sql.SQLException;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Objects;
import java.util.logging.Logger;

@FieldDefaults(level = AccessLevel.PRIVATE)
public class WhitelistByTime extends JavaPlugin implements IWhitelist {

static Logger log;

boolean whitelistEnabled = true;

TimeConvertor timeConvertor;

WLDatabase database;
Checker checker;
PlaceholderHook placeholderHook;
Metrics metrics;

public static void info(String message) {
if (log != null) log.info(message);
}

public static void warn(String message) {
if (log != null) log.warning(message);
}

public static void error(String message) {
if (log != null) log.severe(message);
}

@Override
public void onEnable() {
log = getLogger();

ConfigUtils.checkConfig(this);
whitelistEnabled = getConfig().getBoolean("enabled", true);

initTimeConvertor();

try {
initDatabase();
} catch (SQLException exception) {
exception.printStackTrace();
getServer().getPluginManager().disablePlugin(this);
}

initChecker();
initCommandsAndListeners();
initMetrics();
hookPlaceholder();

info("Enabled");
}

@Override
public void onDisable() {
HandlerList.unregisterAll(this);

if (placeholderHook != null) placeholderHook.unregister();
if (metrics != null) metrics.shutdown();
if (checker != null) checker.stop();
if (database != null) database.close();

info("Disabled");
}

@Override
public void reload() {
onDisable();
onEnable();
}

private void initTimeConvertor() {
TimeUnitsConvertorSettings settings = TimeUnitsConvertorSettings.builder()
.year(getConfig().getStringList("time-units.year"))
.month(getConfig().getStringList("time-units.month"))
.week(getConfig().getStringList("time-units.week"))
.day(getConfig().getStringList("time-units.day"))
.hour(getConfig().getStringList("time-units.hour"))
.minute(getConfig().getStringList("time-units.minute"))
.second(getConfig().getStringList("time-units.second"))
.forever(getConfig().getString("minecraft-commands.forever", "forever"))
.build();

timeConvertor = new TimeConvertor(settings);
}

private void initDatabase() throws SQLException {
DatabaseSettings settings = DatabaseSettings.builder()
.localStorageDir(getDataFolder())
.type(getConfig().getString("type", "sqlite"))
.address(getConfig().getString("address", "localhost"))
.databaseName(getConfig().getString("name", "whitelist"))
.userUserAndPassword(getConfig().getBoolean("userUserAndPassword", false))
.user(getConfig().getString("user", "user"))
.password(getConfig().getString("password", "password"))
.params(getConfig().getStringList("params"))
.build();

database = new WLDatabase(settings);
database.loadPlayersToCache(Arrays.stream(getServer().getOfflinePlayers())
.map(OfflinePlayer::getName)
.filter(Objects::nonNull)
.toList());
}

private void initCommandsAndListeners() {
getServer().getPluginManager().registerEvents(new WhitelistCmdListener(new CommandsExecutor(database, this, timeConvertor)), this);
getServer().getPluginManager().registerEvents(new PlayerLoginListener(database, getConfig().getBoolean("case-sensitive", false), this), this);

getCommand("whitelist").setExecutor(new WhitelistCommandExecutor(new CommandsExecutor(database, this, timeConvertor)));
getCommand("whitelist").setTabCompleter(new WhitelistTabCompleter(database, this));
}

private void initChecker() {
PlayerKicker playerKicker = new PlayerKicker(this, getConfig().getBoolean("case-sensitive", false), getConfig().getStringList("minecraft-commands.you-not-in-whitelist-kick"));
database.addListener(playerKicker);

checker = new PlayersChecker(database, playerKicker, Duration.of(getConfig().getInt("checker-delay", 1000), ChronoUnit.MILLIS));
checker.start();
}

private void hookPlaceholder() {
if (getConfig().getBoolean("placeholders-enabled", false)) {
try {
placeholderHook = new PlaceholderHook(database, timeConvertor, getPluginConfig());
placeholderHook.register();
log.info("Hooked with PlaceholderAPI");
} catch (Exception exception) {
log.warning("Can't hook with PlaceholderAPI. " + exception.getMessage());
}
}
}

private void initMetrics() {
new Metrics(this, 13834);
}

@Override
public boolean isWhitelistEnabled() {
return whitelistEnabled;
}

@Override
public void setWhitelistEnabled(boolean mode) {
whitelistEnabled = mode;
}

@Override
public FileConfiguration getPluginConfig() {
return getConfig();
}
}
Loading

0 comments on commit 3fb5ee8

Please sign in to comment.