-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from NightMirror21/develop
Develop
- Loading branch information
Showing
30 changed files
with
1,199 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
181
folia/src/main/java/ru/nightmirror/wlbytime/WhitelistByTime.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.