From fba69de6885cc868e3bcd7b33f4947d4341db894 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 10 Mar 2019 20:45:59 +0100 Subject: [PATCH] Adding BanSystem --- src/de/warking/bungeecore/BungeeCore.java | 2 + .../bungeecore/PlayerJoinListener.java | 24 ++++++ src/de/warking/bungeecore/WarkingUser.java | 77 +++++++++++++++++++ src/de/warking/bungeecore/sql.java | 5 +- 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/de/warking/bungeecore/PlayerJoinListener.java create mode 100644 src/de/warking/bungeecore/WarkingUser.java diff --git a/src/de/warking/bungeecore/BungeeCore.java b/src/de/warking/bungeecore/BungeeCore.java index 13854b5..3cc5f21 100644 --- a/src/de/warking/bungeecore/BungeeCore.java +++ b/src/de/warking/bungeecore/BungeeCore.java @@ -45,6 +45,8 @@ public class BungeeCore extends Plugin { config.getString("db.password") ); + getProxy().getPluginManager().registerListener(this, new PlayerJoinListener()); + final Configuration servers = config.getSection("servers"); for(final String cmd : servers.getKeys()){ final Configuration server = servers.getSection(cmd); diff --git a/src/de/warking/bungeecore/PlayerJoinListener.java b/src/de/warking/bungeecore/PlayerJoinListener.java new file mode 100644 index 0000000..1cab613 --- /dev/null +++ b/src/de/warking/bungeecore/PlayerJoinListener.java @@ -0,0 +1,24 @@ +package de.warking.bungeecore; + +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.event.PostLoginEvent; +import net.md_5.bungee.api.plugin.Listener; +import net.md_5.bungee.event.EventHandler; + +public class PlayerJoinListener implements Listener { + + @EventHandler + public void onPostLogin(PostLoginEvent event) { + ProxiedPlayer player = event.getPlayer(); + WarkingUser user = new WarkingUser(player); + if(user.isBanned()){ + if(user.BanTime.equals(WarkingUser.PermaBan)){ + player.disconnect(BungeeCore.ChatPrefix + "§cDu bist permanent gebannt. §r§lGrund§r: §c" + user.BanReason); + }else{ + player.disconnect(BungeeCore.ChatPrefix + " Du bist auf bis zum " + + user.BanTime.getDay() + "." + user.BanTime.getMonth() + "." + user.BanTime.getYear() + " " + + user.BanTime.getHours() + ":" + user.BanTime.getMinutes() + " gebannt. §r§lGrund§r: §c" + user.BanReason); + } + } + } +} diff --git a/src/de/warking/bungeecore/WarkingUser.java b/src/de/warking/bungeecore/WarkingUser.java new file mode 100644 index 0000000..6c0af02 --- /dev/null +++ b/src/de/warking/bungeecore/WarkingUser.java @@ -0,0 +1,77 @@ +package de.warking.bungeecore; + +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.Date; +import java.util.UUID; + +public class WarkingUser { + public int id; + public UUID UUID; + public String UserName; + public String UserGroup; + public Timestamp BanTime; + public String BanReason; + + public static Timestamp PermaBan = new Timestamp(2000,1,1,0,0,0,0); + + public WarkingUser(UUID UUID){ + init(sql.select("SELECT * FROM UserData WHERE UUID = '" + UUID.toString() + "'")); + } + + public WarkingUser(String UserName){ + init(sql.select("SELECT * FROM UserData WHERE UserName = '" + UserName + "'")); + } + + public WarkingUser(ProxiedPlayer player){ + ResultSet dbplayer = sql.select("SELECT * FROM UserData WHERE UUID = '" + player.getUniqueId().toString() + "'"); + try { + if(dbplayer.next()){ + dbplayer.beforeFirst(); + init(dbplayer); + }else{ + UUID = player.getUniqueId(); + UserName = player.getName(); + UserGroup = "Member"; + sql.update("INSERT INTO UserData (UUID, UserName, UserGroup) VALUES ('" + UUID.toString() + "', '" + UserName + "', '" + UserGroup + "')"); + + dbplayer = sql.select("SELECT * FROM UserData WHERE UUID = '" + UUID.toString() + "'"); + init(dbplayer); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private void init(ResultSet dbplayer){ + try { + if(!dbplayer.next()){ + return; + } + id = dbplayer.getInt("id"); + this.UUID = java.util.UUID.fromString(dbplayer.getString("UUID")); + UserName = dbplayer.getString("UserName"); + UserGroup = dbplayer.getString("UserGroup"); + BanTime = dbplayer.getTimestamp("BanTime"); + BanReason = dbplayer.getString("BanReason"); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public boolean isBanned(){ + if(BanTime == null){ + return false; + }else if(BanTime.after(new Date()) || BanTime.equals(PermaBan)){ + return true; + }else{ + sql.update("UPDATE UserData SET BanTime = NULL, BanReason = '' WHERE UUID = '" + UUID.toString() + "'"); + BanTime = null; + BanReason = ""; + return false; + } + } +} diff --git a/src/de/warking/bungeecore/sql.java b/src/de/warking/bungeecore/sql.java index 6d48879..5ede6b4 100644 --- a/src/de/warking/bungeecore/sql.java +++ b/src/de/warking/bungeecore/sql.java @@ -37,7 +37,7 @@ public class sql { } } - public static ResultSet select(String qry) { + public static ResultSet select(String qry){ try { PreparedStatement st = con.prepareStatement(qry); @@ -48,4 +48,7 @@ public class sql { return null; } + public static Connection getCon(){ + return con; + } }