Sketch of connection refusing system
This commit is contained in:
parent
49e5051aaf
commit
053bc0a83b
12 changed files with 663 additions and 93 deletions
93
README.md
93
README.md
|
|
@ -1,93 +0,0 @@
|
|||
# BanHammer
|
||||
|
||||
|
||||
|
||||
## Getting started
|
||||
|
||||
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
|
||||
|
||||
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
|
||||
|
||||
## Add your files
|
||||
|
||||
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
|
||||
- [ ] [Add files using the command line](https://docs.gitlab.com/topics/git/add_files/#add-files-to-a-git-repository) or push an existing Git repository with the following command:
|
||||
|
||||
```
|
||||
cd existing_repo
|
||||
git remote add origin https://git.kuraczyk.net/kotopolismc/banhammer.git
|
||||
git branch -M main
|
||||
git push -uf origin main
|
||||
```
|
||||
|
||||
## Integrate with your tools
|
||||
|
||||
- [ ] [Set up project integrations](https://git.kuraczyk.net/kotopolismc/banhammer/-/settings/integrations)
|
||||
|
||||
## Collaborate with your team
|
||||
|
||||
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
|
||||
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
|
||||
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
|
||||
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
|
||||
- [ ] [Set auto-merge](https://docs.gitlab.com/user/project/merge_requests/auto_merge/)
|
||||
|
||||
## Test and Deploy
|
||||
|
||||
Use the built-in continuous integration in GitLab.
|
||||
|
||||
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/)
|
||||
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
|
||||
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
|
||||
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
|
||||
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
|
||||
|
||||
***
|
||||
|
||||
# Editing this README
|
||||
|
||||
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
|
||||
|
||||
## Suggestions for a good README
|
||||
|
||||
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
|
||||
|
||||
## Name
|
||||
Choose a self-explaining name for your project.
|
||||
|
||||
## Description
|
||||
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
|
||||
|
||||
## Badges
|
||||
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
|
||||
|
||||
## Visuals
|
||||
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
|
||||
|
||||
## Installation
|
||||
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
|
||||
|
||||
## Usage
|
||||
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
|
||||
|
||||
## Support
|
||||
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
|
||||
|
||||
## Roadmap
|
||||
If you have ideas for releases in the future, it is a good idea to list them in the README.
|
||||
|
||||
## Contributing
|
||||
State if you are open to contributions and what your requirements are for accepting them.
|
||||
|
||||
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
|
||||
|
||||
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
|
||||
|
||||
## Authors and acknowledgment
|
||||
Show your appreciation to those who have contributed to the project.
|
||||
|
||||
## License
|
||||
For open source projects, say how it is licensed.
|
||||
|
||||
## Project status
|
||||
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
|
||||
68
build.gradle
Normal file
68
build.gradle
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
plugins {
|
||||
id 'java'
|
||||
id 'com.gradleup.shadow' version '8.3.5'
|
||||
id 'io.papermc.paperweight.userdev' version '1.7.7'
|
||||
id 'xyz.jpenilla.run-paper' version '2.3.1'
|
||||
}
|
||||
|
||||
group = 'net.kuraczyk'
|
||||
version = '1.0-SNAPSHOT'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven {
|
||||
name = 'papermc'
|
||||
url = 'https://repo.papermc.io/repository/maven-public/'
|
||||
}
|
||||
maven {
|
||||
name = 'purpur'
|
||||
url = 'https://repo.purpurmc.org/snapshots'
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
paperweight.paperDevBundle('1.21-R0.1-SNAPSHOT')
|
||||
implementation 'org.postgresql:postgresql:42.7.7'
|
||||
implementation 'org.flywaydb:flyway-core:10.7.2'
|
||||
implementation 'org.flywaydb:flyway-database-postgresql:10.7.2'
|
||||
}
|
||||
|
||||
shadowJar {
|
||||
relocate 'org.postgresql', 'net.kuraczyk.libs.postgresql'
|
||||
relocate 'org.flywaydb', 'net.kuraczyk.lib.flyway'
|
||||
mergeServiceFiles()
|
||||
archiveFileName.set("BanHammer-${project.version}.jar")
|
||||
}
|
||||
|
||||
tasks.build.dependsOn(shadowJar)
|
||||
|
||||
def targetJavaVersion = 21
|
||||
java {
|
||||
def javaVersion = JavaVersion.toVersion(targetJavaVersion)
|
||||
sourceCompatibility = javaVersion
|
||||
targetCompatibility = javaVersion
|
||||
if (JavaVersion.current() < javaVersion) {
|
||||
toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
|
||||
}
|
||||
}
|
||||
|
||||
tasks.withType(JavaCompile).configureEach {
|
||||
options.encoding = 'UTF-8'
|
||||
|
||||
if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) {
|
||||
options.release.set(targetJavaVersion)
|
||||
}
|
||||
}
|
||||
|
||||
processResources {
|
||||
def props = [version: version]
|
||||
inputs.properties props
|
||||
filteringCharset 'UTF-8'
|
||||
filesMatching('plugin.yml') {
|
||||
expand props
|
||||
}
|
||||
}
|
||||
|
||||
tasks.named('runServer') {
|
||||
minecraftVersion('1.21')
|
||||
}
|
||||
0
gradle.properties
Normal file
0
gradle.properties
Normal file
1
settings.gradle
Normal file
1
settings.gradle
Normal file
|
|
@ -0,0 +1 @@
|
|||
rootProject.name = 'BanHammer'
|
||||
61
src/main/java/net/kuraczyk/banhammer/BanHammer.java
Normal file
61
src/main/java/net/kuraczyk/banhammer/BanHammer.java
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package net.kuraczyk.banhammer;
|
||||
|
||||
import net.kuraczyk.banhammer.events.PlayerPreLogin;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import net.kuraczyk.banhammer.utils.*;
|
||||
|
||||
public final class BanHammer extends JavaPlugin {
|
||||
|
||||
public FileConfiguration config;
|
||||
public DBManager db;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
saveDefaultConfig();
|
||||
config = getConfig();
|
||||
|
||||
try {
|
||||
Class.forName("net.kuraczyk.libs.postgresql.Driver");
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
db = new DBManager(
|
||||
config.getString("database.host"),
|
||||
config.getInt("database.port"),
|
||||
config.getString("database.user"),
|
||||
config.getString("database.pass"),
|
||||
config.getString("database.database"),
|
||||
this.getClassLoader()
|
||||
);
|
||||
String error = db.connect();
|
||||
if(error != null){
|
||||
getLogger().severe(
|
||||
"Failed to connect to DB!!!!\n"
|
||||
+error
|
||||
);
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
}
|
||||
|
||||
getLogger().info(ChatColor.translateAlternateColorCodes('&', "&a&b+&r&8 | &aBan&7Hammer"));
|
||||
|
||||
getServer().getPluginManager().registerEvents(new PlayerPreLogin(this), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
if(db != null){
|
||||
String error = db.disconnect();
|
||||
if(error != null) {
|
||||
getLogger().severe(
|
||||
"Error occured while disconnecting from from DB!!!!\n"
|
||||
+error
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
getLogger().info(ChatColor.translateAlternateColorCodes('&', "&c&b-&r&8 | &aBan&7Hammer"));
|
||||
}
|
||||
}
|
||||
126
src/main/java/net/kuraczyk/banhammer/events/PlayerPreLogin.java
Normal file
126
src/main/java/net/kuraczyk/banhammer/events/PlayerPreLogin.java
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
package net.kuraczyk.banhammer.events;
|
||||
|
||||
import net.kuraczyk.banhammer.BanHammer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.sql.*;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import net.kuraczyk.banhammer.utils.TextUtils;
|
||||
|
||||
public class PlayerPreLogin implements Listener {
|
||||
|
||||
BanHammer plugin;
|
||||
|
||||
Component kickMessage = Component.text("If this message is displayed something went terribly wrong...\nModule: BanHammer");
|
||||
|
||||
public PlayerPreLogin(BanHammer m) {
|
||||
plugin = m;
|
||||
m.getClass();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerPreLogin(AsyncPlayerPreLoginEvent e) {
|
||||
if(checkPlayerBanStatus(e.getPlayerProfile().getName(), e.getUniqueId(), e.getAddress()))
|
||||
e.disallow(AsyncPlayerPreLoginEvent.Result.KICK_BANNED, kickMessage);
|
||||
}
|
||||
|
||||
private boolean checkPlayerBanStatus(String nickname, UUID uuid, InetAddress ip) {
|
||||
try {
|
||||
Connection conn = plugin.db.getConnection();
|
||||
if(conn!=null){
|
||||
PreparedStatement ps = conn.prepareStatement("SELECT * from player_bans WHERE (player_name = ? OR player_uuid = ?::uuid) AND (scope = ? OR scope = '*')");
|
||||
ps.setString(1, nickname);
|
||||
ps.setObject(2, uuid);
|
||||
ps.setString(3, plugin.getConfig().getString("serverID"));
|
||||
ResultSet rs = ps.executeQuery();
|
||||
while(rs.next()){
|
||||
|
||||
String reason = rs.getString("reason");
|
||||
String parsedDate;
|
||||
|
||||
if(rs.getObject("end_time")!=null){
|
||||
long timeLeft = ((Timestamp)rs.getObject("end_time")).getTime() - new Timestamp(System.currentTimeMillis()).getTime();
|
||||
|
||||
if(timeLeft <= 0) {
|
||||
PreparedStatement archiveBan = conn.prepareStatement("DELETE FROM player_bans WHERE id = ?::uuid");
|
||||
archiveBan.setString(1, rs.getObject("id").toString());
|
||||
archiveBan.executeUpdate();
|
||||
continue;
|
||||
}
|
||||
|
||||
long timeLeftDays = TimeUnit.MILLISECONDS.toDays(timeLeft);
|
||||
timeLeft-=TimeUnit.DAYS.toMillis(timeLeftDays);
|
||||
long timeLeftHours = TimeUnit.MILLISECONDS.toHours(timeLeft);
|
||||
timeLeft-=TimeUnit.HOURS.toMillis(timeLeftHours);
|
||||
long timeLeftMinutes = TimeUnit.MILLISECONDS.toMinutes(timeLeft);
|
||||
timeLeft-=TimeUnit.MINUTES.toMillis(timeLeftMinutes);
|
||||
long timeLeftSeconds = TimeUnit.MILLISECONDS.toSeconds(timeLeft);
|
||||
timeLeft-=TimeUnit.SECONDS.toMillis(timeLeftSeconds);
|
||||
|
||||
parsedDate = TextUtils.parsePolishDate(timeLeftDays, timeLeftHours, timeLeftMinutes, timeLeftSeconds);
|
||||
kickMessage = TextUtils.colorString(TextUtils.resolvePlaceholders(String.format("Zostałeś zbanowany na %s.\nPowód: %s", parsedDate, reason)));
|
||||
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
kickMessage = TextUtils.colorString(TextUtils.resolvePlaceholders(String.format("Zostałeś permanentnie zbanowany.\nPowód: %s", reason)));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
PreparedStatement ps2 = conn.prepareStatement("SELECT * from ip_bans WHERE (ip_address = ?::inet) AND (scope = ? OR scope = '*')");
|
||||
ps2.setObject(1, ip.getHostAddress());
|
||||
ps2.setString(2, plugin.getConfig().getString("serverID"));
|
||||
ResultSet rs2 = ps2.executeQuery();
|
||||
while(rs2.next()) {
|
||||
String reason = rs2.getString("reason");
|
||||
String parsedDate;
|
||||
|
||||
if(rs2.getObject("end_time")!=null){
|
||||
long timeLeft = ((Timestamp)rs2.getObject("end_time")).getTime() - new Timestamp(System.currentTimeMillis()).getTime();
|
||||
|
||||
if(timeLeft <= 0) {
|
||||
PreparedStatement archiveBan = conn.prepareStatement("DELETE FROM ip_bans WHERE id = ?::uuid");
|
||||
archiveBan.setString(1, rs2.getObject("id").toString());
|
||||
archiveBan.executeUpdate();
|
||||
continue;
|
||||
}
|
||||
|
||||
long timeLeftDays = TimeUnit.MILLISECONDS.toDays(timeLeft);
|
||||
timeLeft-=TimeUnit.DAYS.toMillis(timeLeftDays);
|
||||
long timeLeftHours = TimeUnit.MILLISECONDS.toHours(timeLeft);
|
||||
timeLeft-=TimeUnit.HOURS.toMillis(timeLeftHours);
|
||||
long timeLeftMinutes = TimeUnit.MILLISECONDS.toMinutes(timeLeft);
|
||||
timeLeft-=TimeUnit.MINUTES.toMillis(timeLeftMinutes);
|
||||
long timeLeftSeconds = TimeUnit.MILLISECONDS.toSeconds(timeLeft);
|
||||
timeLeft-=TimeUnit.SECONDS.toMillis(timeLeftSeconds);
|
||||
|
||||
parsedDate = TextUtils.parsePolishDate(timeLeftDays, timeLeftHours, timeLeftMinutes, timeLeftSeconds);
|
||||
kickMessage = TextUtils.colorString(TextUtils.resolvePlaceholders(String.format("Zostałeś IPzbanowany na %s.\nPowód: %s", parsedDate, reason)));
|
||||
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
kickMessage = TextUtils.colorString(TextUtils.resolvePlaceholders(String.format("Zostałeś permanentnie IPzbanowany.\nPowód: %s", reason)));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
plugin.getLogger().severe(e.toString());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
92
src/main/java/net/kuraczyk/banhammer/utils/DBManager.java
Normal file
92
src/main/java/net/kuraczyk/banhammer/utils/DBManager.java
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
package net.kuraczyk.banhammer.utils;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.flywaydb.core.api.output.MigrateResult;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Struct;
|
||||
|
||||
public class DBManager {
|
||||
|
||||
private String host, user, pass, database;
|
||||
private int port;
|
||||
private Connection connection;
|
||||
private ClassLoader classLoader;
|
||||
|
||||
|
||||
public DBManager(String host, int port, String user, String pass, String database, ClassLoader classLoader) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.user = user;
|
||||
this.pass = pass;
|
||||
this.database = database;
|
||||
this.classLoader = classLoader;
|
||||
}
|
||||
|
||||
public String connect() {
|
||||
try {
|
||||
if(connection != null && !connection.isClosed()){
|
||||
return null;
|
||||
}
|
||||
|
||||
connection = DriverManager.getConnection(String.format("jdbc:postgresql://%s:%d/%s", host, port, database), user, pass);
|
||||
|
||||
String migrationSuccess = performMigration();
|
||||
if(migrationSuccess != null){
|
||||
return migrationSuccess;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
} catch (SQLException e) {
|
||||
return e.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public String disconnect() {
|
||||
try {
|
||||
if(connection != null && !connection.isClosed()){
|
||||
connection.close();
|
||||
return null;
|
||||
}
|
||||
return "Error";
|
||||
} catch (SQLException e) {
|
||||
return e.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
if(connection == null || connection.isClosed()){
|
||||
return null;
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
private String performMigration() {
|
||||
try {
|
||||
Flyway flyway = Flyway.configure(classLoader)
|
||||
.driver("net.kuraczyk.libs.postgresql.Driver")
|
||||
.dataSource(String.format("jdbc:postgresql://%s:%d/%s", host, port, database), user, pass)
|
||||
.locations("classpath:db/migration")
|
||||
.baselineOnMigrate(true)
|
||||
.validateOnMigrate(true)
|
||||
.cleanDisabled(true)
|
||||
.table("flyway_schema_history")
|
||||
.load();
|
||||
|
||||
MigrateResult result = flyway.migrate();
|
||||
if (result.success) {
|
||||
return null;
|
||||
} else {
|
||||
return result.warnings.toString();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
return e.toString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
121
src/main/java/net/kuraczyk/banhammer/utils/TextUtils.java
Normal file
121
src/main/java/net/kuraczyk/banhammer/utils/TextUtils.java
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
package net.kuraczyk.banhammer.utils;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TextUtils {
|
||||
|
||||
public static Component colorString(String s){
|
||||
return colorMarkupFormattedString(
|
||||
colorLegacyFormattedString(s)
|
||||
);
|
||||
}
|
||||
|
||||
private static Component colorMarkupFormattedString(String s){
|
||||
return MiniMessage.miniMessage().deserialize(s);
|
||||
}
|
||||
|
||||
private static String colorLegacyFormattedString(String s){
|
||||
return ChatColor.translateAlternateColorCodes('&', s);
|
||||
}
|
||||
|
||||
public static String resolvePlaceholders(String s){
|
||||
return s.replace("{onlinePlayerCount}", String.valueOf(
|
||||
Bukkit.getOnlinePlayers().size()+Arrays.stream(Bukkit.getOfflinePlayers()).count()
|
||||
));
|
||||
}
|
||||
public static String resolvePlaceholders(String s, Player p){
|
||||
return resolvePlaceholders(s)
|
||||
.replace("{playerName}", p.getName())
|
||||
.replace("{ping}", String.valueOf(p.getPing()));
|
||||
}
|
||||
|
||||
public static String parsePolishDate(long days, long hours, long minutes, long seconds){
|
||||
String parsedDays, parsedHours, parsedMinutes, parsedSeconds;
|
||||
|
||||
// Parse days string block
|
||||
if(days >= 2){
|
||||
parsedDays="dni";
|
||||
}
|
||||
else if (days==1){
|
||||
parsedDays="dzień";
|
||||
}
|
||||
else{
|
||||
parsedDays="";
|
||||
}
|
||||
if(parsedDays!="") parsedDays=Long.toString(days)+' '+parsedDays+", ";
|
||||
|
||||
// Parse hours string block
|
||||
if(hours >= 20 || (hours < 10 && hours > 1)){
|
||||
if(hours%10 >=2 && hours%10 <=4){
|
||||
parsedHours="godziny";
|
||||
}
|
||||
else{
|
||||
parsedHours="godzin";
|
||||
}
|
||||
}
|
||||
else if(hours < 20 && hours >= 10){
|
||||
parsedHours="godzin";
|
||||
}
|
||||
else if (hours==1){
|
||||
parsedHours="godzina";
|
||||
}
|
||||
else{
|
||||
parsedHours="";
|
||||
}
|
||||
if(parsedHours!="") parsedHours=Long.toString(hours)+' '+parsedHours+", ";
|
||||
|
||||
// Parse minutes string block
|
||||
if(minutes >= 20 || (minutes < 10 && minutes > 1)){
|
||||
if(minutes%10 >=2 && minutes%10 <=4){
|
||||
parsedMinutes="minuty";
|
||||
}
|
||||
else{
|
||||
parsedMinutes="minut";
|
||||
}
|
||||
}
|
||||
else if(minutes < 20 && minutes >= 10){
|
||||
parsedMinutes="minut";
|
||||
}
|
||||
else if (minutes==1){
|
||||
parsedMinutes="minuta";
|
||||
}
|
||||
else{
|
||||
parsedMinutes="";
|
||||
}
|
||||
if(parsedMinutes!="") parsedMinutes=Long.toString(minutes)+' '+parsedMinutes+", ";
|
||||
|
||||
// Parse seconds string block
|
||||
if(seconds >= 20 || (seconds < 10 && seconds > 1)){
|
||||
if(seconds%10 >=2 && seconds%10 <=4){
|
||||
parsedSeconds="sekundy";
|
||||
}
|
||||
else{
|
||||
parsedSeconds="sekund";
|
||||
}
|
||||
}
|
||||
else if(seconds < 20 && seconds >= 10){
|
||||
parsedSeconds="sekund";
|
||||
}
|
||||
else if (seconds==1){
|
||||
parsedSeconds="sekunda";
|
||||
}
|
||||
else{
|
||||
parsedSeconds="";
|
||||
}
|
||||
if(parsedSeconds!="") parsedSeconds=Long.toString(seconds)+' '+parsedSeconds+", ";
|
||||
|
||||
// Create final gramatically correct string
|
||||
String result = String.format("%s%s%s%s", parsedDays, parsedHours, parsedMinutes, parsedSeconds);
|
||||
if(result.length()>=2) result = result.substring(0, result.length() - 2);
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
8
src/main/resources/config.yml
Normal file
8
src/main/resources/config.yml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
database:
|
||||
host: "localhost"
|
||||
port: 5432
|
||||
user: "postgres"
|
||||
pass: "postgres"
|
||||
database: "postgres"
|
||||
|
||||
serverID: "mcserver"
|
||||
129
src/main/resources/db/migration/V1__Initial_schema.sql
Normal file
129
src/main/resources/db/migration/V1__Initial_schema.sql
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
CREATE TABLE player_bans (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
player_name VARCHAR(16) NOT NULL,
|
||||
player_uuid UUID NOT NULL,
|
||||
reason TEXT,
|
||||
scope VARCHAR(20) NOT NULL DEFAULT '*',
|
||||
operator_name VARCHAR(16) NOT NULL,
|
||||
operator_uuid UUID,
|
||||
start_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
end_time TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE ip_bans (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
ip_address INET NOT NULL,
|
||||
reason TEXT,
|
||||
scope VARCHAR(20) NOT NULL DEFAULT '*',
|
||||
operator_name VARCHAR(16) NOT NULL,
|
||||
operator_uuid UUID,
|
||||
start_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
end_time TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE ban_history (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
target JSONB NOT NULL,
|
||||
reason TEXT,
|
||||
scope VARCHAR(20) NOT NULL DEFAULT '*',
|
||||
operator_name VARCHAR(16) NOT NULL,
|
||||
operator_uuid UUID,
|
||||
start_time TIMESTAMP NOT NULL,
|
||||
end_time TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE appeals (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
ban_id UUID NOT NULL,
|
||||
target JSONB NOT NULL, --so appealing player could use email, account, discord
|
||||
sustained BOOLEAN, -- null is pending
|
||||
response TEXT,
|
||||
operator_name VARCHAR(16),
|
||||
operator_uuid UUID,
|
||||
appeal_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
resolve_date TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX idx_player_bans_uuid ON player_bans(player_uuid);
|
||||
CREATE INDEX idx_player_bans_name ON player_bans(player_name);
|
||||
CREATE INDEX idx_player_bans_scope ON player_bans(scope);
|
||||
CREATE INDEX idx_player_bans_end_time ON player_bans(end_time) WHERE end_time IS NOT NULL;
|
||||
|
||||
CREATE INDEX idx_player_bans_active_lookup
|
||||
ON player_bans(player_uuid, scope);
|
||||
|
||||
CREATE INDEX idx_ip_bans_ip ON ip_bans(ip_address);
|
||||
CREATE INDEX idx_ip_bans_scope ON ip_bans(scope);
|
||||
CREATE INDEX idx_ip_bans_end_time ON ip_bans(end_time) WHERE end_time IS NOT NULL;
|
||||
|
||||
CREATE INDEX idx_ip_bans_active_lookup
|
||||
ON ip_bans(ip_address, scope);
|
||||
|
||||
CREATE INDEX idx_ban_history_target ON ban_history USING GIN (target);
|
||||
CREATE INDEX idx_ban_history_scope ON ban_history(scope);
|
||||
CREATE INDEX idx_ban_history_start_time ON ban_history(start_time DESC);
|
||||
|
||||
CREATE INDEX idx_appeals_ban_id ON appeals(ban_id);
|
||||
CREATE INDEX idx_appeals_sustained ON appeals(sustained);
|
||||
CREATE INDEX idx_appeals_appeal_date ON appeals(appeal_date DESC);
|
||||
CREATE INDEX idx_appeals_target ON appeals USING GIN (target);
|
||||
|
||||
CREATE UNIQUE INDEX idx_unique_active_player_ban
|
||||
ON player_bans(player_uuid, scope);
|
||||
|
||||
CREATE UNIQUE INDEX idx_unique_active_ip_ban
|
||||
ON ip_bans(ip_address, scope);
|
||||
|
||||
CREATE OR REPLACE FUNCTION archive_ban_on_delete()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
INSERT INTO ban_history (target, reason, scope, operator_name, operator_uuid, start_time, end_time)
|
||||
VALUES (
|
||||
CASE TG_TABLE_NAME
|
||||
WHEN 'player_bans' THEN jsonb_build_object(
|
||||
'player_uuid', OLD.player_uuid,
|
||||
'player_name', OLD.player_name
|
||||
)
|
||||
WHEN 'ip_bans' THEN jsonb_build_object(
|
||||
'ip_address', host(OLD.ip_address)
|
||||
)
|
||||
END,
|
||||
OLD.reason,
|
||||
OLD.scope,
|
||||
OLD.operator_name,
|
||||
OLD.operator_uuid,
|
||||
OLD.start_time,
|
||||
OLD.end_time
|
||||
);
|
||||
RETURN OLD;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE TRIGGER archive_player_ban_on_delete
|
||||
BEFORE DELETE ON player_bans
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION archive_ban_on_delete();
|
||||
|
||||
CREATE TRIGGER archive_ip_ban_on_delete
|
||||
BEFORE DELETE ON ip_bans
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION archive_ban_on_delete();
|
||||
|
||||
-- Comments for documentation (written by sonnet 4.5)
|
||||
COMMENT ON TABLE player_bans IS 'Active player bans - deleted bans are moved to ban_history';
|
||||
COMMENT ON TABLE ip_bans IS 'Active IP bans - deleted bans are moved to ban_history';
|
||||
COMMENT ON TABLE ban_history IS 'Historical record of expired or revoked bans';
|
||||
COMMENT ON TABLE appeals IS 'Ban appeal requests and their outcomes';
|
||||
|
||||
COMMENT ON COLUMN player_bans.end_time IS 'NULL indicates permanent ban';
|
||||
COMMENT ON COLUMN player_bans.scope IS 'Ban scope: * for global, server name for specific server';
|
||||
|
||||
COMMENT ON COLUMN ip_bans.end_time IS 'NULL indicates permanent ban';
|
||||
COMMENT ON COLUMN ip_bans.scope IS 'Ban scope: * for global, server name for specific server';
|
||||
|
||||
COMMENT ON COLUMN ban_history.target IS 'JSONB containing player info (player_uuid, player_name) or IP info (ip_address)';
|
||||
COMMENT ON COLUMN ban_history.end_time IS 'NULL if ban was permanent and manually revoked';
|
||||
|
||||
COMMENT ON COLUMN appeals.sustained IS 'NULL = pending review, TRUE = ban kept (appeal rejected), FALSE = ban removed (appeal approved)';
|
||||
COMMENT ON COLUMN appeals.response IS 'Operator response to the appeal';
|
||||
COMMENT ON COLUMN appeals.target IS 'JSONB containing appellant contact info (player_uuid, player_name, email, discord, etc.)';
|
||||
53
src/main/resources/db/migration/V2__Fix_archive_triggers.sql
Normal file
53
src/main/resources/db/migration/V2__Fix_archive_triggers.sql
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
DROP TRIGGER IF EXISTS archive_player_ban_on_delete ON player_bans;
|
||||
DROP TRIGGER IF EXISTS archive_ip_ban_on_delete ON ip_bans;
|
||||
|
||||
DROP FUNCTION IF EXISTS archive_ban_on_delete();
|
||||
|
||||
CREATE OR REPLACE FUNCTION archive_player_ban_on_delete()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
INSERT INTO ban_history (target, reason, scope, operator_name, operator_uuid, start_time, end_time)
|
||||
VALUES (
|
||||
jsonb_build_object(
|
||||
'player_uuid', OLD.player_uuid,
|
||||
'player_name', OLD.player_name
|
||||
),
|
||||
OLD.reason,
|
||||
OLD.scope,
|
||||
OLD.operator_name,
|
||||
OLD.operator_uuid,
|
||||
OLD.start_time,
|
||||
OLD.end_time
|
||||
);
|
||||
RETURN OLD;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE OR REPLACE FUNCTION archive_ip_ban_on_delete()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
INSERT INTO ban_history (target, reason, scope, operator_name, operator_uuid, start_time, end_time)
|
||||
VALUES (
|
||||
jsonb_build_object(
|
||||
'ip_address', host(OLD.ip_address)
|
||||
),
|
||||
OLD.reason,
|
||||
OLD.scope,
|
||||
OLD.operator_name,
|
||||
OLD.operator_uuid,
|
||||
OLD.start_time,
|
||||
OLD.end_time
|
||||
);
|
||||
RETURN OLD;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE TRIGGER archive_player_ban_on_delete
|
||||
BEFORE DELETE ON player_bans
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION archive_player_ban_on_delete();
|
||||
|
||||
CREATE TRIGGER archive_ip_ban_on_delete
|
||||
BEFORE DELETE ON ip_bans
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION archive_ip_ban_on_delete();
|
||||
4
src/main/resources/plugin.yml
Normal file
4
src/main/resources/plugin.yml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
name: BanHammer
|
||||
version: '0.1.0-SNAPSHOT'
|
||||
main: net.kuraczyk.banhammer.BanHammer
|
||||
api-version: '1.21'
|
||||
Loading…
Add table
Add a link
Reference in a new issue