From 994fccf8456cdf199627bc471aee37f96e640e7d Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 19 May 2020 14:28:01 +0200 Subject: [PATCH] feat: add basic player stats --- .vscode/extensions.json | 4 + .vscode/settings.json | 3 + .../main/java/de/cliffbreak/varo/Varo.java | 8 ++ .../de/cliffbreak/varo/database/Database.java | 59 ++++++++++++++ .../discord/listeners/MessageListener.java | 9 +++ .../varo/listeners/PlayerDeathListener.java | 79 ++++++++++++++++++- .../cliffbreak/varo/managers/TeamManager.java | 14 ++++ .../de/cliffbreak/varo/uitls/VaroUtils.java | 2 +- 8 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 varo/src/main/java/de/cliffbreak/varo/database/Database.java diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 48181e9..cb0432f 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,5 +1,9 @@ { "recommendations": [ "vscjava.vscode-java-pack", + "streetsidesoftware.code-spell-checker", + "streetsidesoftware.code-spell-checker-german", + "petekinnecom.terminal-command-keys", + "alexcvzz.vscode-sqlite", ] } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index d1e7de1..2d99b8d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,8 @@ "java.configuration.updateBuildConfiguration": "automatic", "cSpell.words": [ "Aragur", + "DRYOUT", + "Elytra", "Gamemode", "Gamerules", "Gson", @@ -14,6 +16,7 @@ "koords", "mkdir", "npcs", + "sqlite", "teamcolors", "testremove", "unban", diff --git a/varo/src/main/java/de/cliffbreak/varo/Varo.java b/varo/src/main/java/de/cliffbreak/varo/Varo.java index 2a912ad..7e6f740 100644 --- a/varo/src/main/java/de/cliffbreak/varo/Varo.java +++ b/varo/src/main/java/de/cliffbreak/varo/Varo.java @@ -11,6 +11,7 @@ import org.bukkit.plugin.java.JavaPlugin; import de.cliffbreak.varo.commands.CoordsCommand; import de.cliffbreak.varo.commands.VaroCommand; import de.cliffbreak.varo.commands.VaroTabCompleter; +import de.cliffbreak.varo.database.Database; import de.cliffbreak.varo.discord.DiscordBot; import de.cliffbreak.varo.listeners.BannedActionListener; import de.cliffbreak.varo.listeners.BannedItemListener; @@ -40,6 +41,7 @@ public class Varo extends JavaPlugin { private TeamManager teamManager; private MessageUtils messageUtils; private DiscordBot discordBot; + private Database database; private boolean canCreatePortal = false; @@ -57,6 +59,7 @@ public class Varo extends JavaPlugin { this.varoUtils = new VaroUtils(this); this.teamManager = new TeamManager(); this.messageUtils = new MessageUtils(this); + this.database = new Database(); this.config.addDefault("Varo.Start", 1590427800000f); this.config.addDefault("Varo.Debug", false); @@ -91,6 +94,7 @@ public class Varo extends JavaPlugin { public void onDisable() { this.getLogger().info("Stopping CliffbreakVaro!"); this.playerCache.shutdown(); + this.database.shutdown(); try { this.discordBot.shutdown(); } catch (final NoClassDefFoundError err) { @@ -144,4 +148,8 @@ public class Varo extends JavaPlugin { public DiscordBot getDiscordBot() { return this.discordBot; } + + public Database getDatabase() { + return this.database; + } } diff --git a/varo/src/main/java/de/cliffbreak/varo/database/Database.java b/varo/src/main/java/de/cliffbreak/varo/database/Database.java new file mode 100644 index 0000000..37f75c9 --- /dev/null +++ b/varo/src/main/java/de/cliffbreak/varo/database/Database.java @@ -0,0 +1,59 @@ +package de.cliffbreak.varo.database; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class Database { + + private Connection conn; + + public Database() { + try { + this.conn = DriverManager.getConnection("jdbc:sqlite:plugins/CliffbreakVaro/players.db"); + this.init(); + } catch (final SQLException e) { + e.printStackTrace(); + } + } + + private void init() throws SQLException { + final Statement s = conn.createStatement(); + s.executeUpdate( + "CREATE TABLE IF NOT EXISTS kills (`player` VARCHAR(32) NOT NULL, `victim` VARCHAR(32) NOT NULL);"); + s.close(); + } + + public void shutdown() { + try { + this.conn.close(); + } catch (final SQLException e) { + e.printStackTrace(); + } + } + + public void addKill(final String uuid, final String victimUuid) { + try { + final Statement s = conn.createStatement(); + s.executeUpdate("INSERT INTO kills (`player`, `victim`) VALUES ('" + uuid + "', '" + victimUuid + "');"); + s.close(); + } catch (final SQLException e) { + e.printStackTrace(); + } + } + + public int getKills(final String uuid) { + try { + final Statement s = conn.createStatement(); + final ResultSet rs = s.executeQuery("SELECT COUNT(`victim`) FROM kills WHERE `player` ='" + uuid + "';"); + final int kills = rs.getInt("COUNT(`victim`)"); + s.close(); + return kills; + } catch (final SQLException e) { + e.printStackTrace(); + } + return 0; + } +} \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/discord/listeners/MessageListener.java b/varo/src/main/java/de/cliffbreak/varo/discord/listeners/MessageListener.java index 6743965..9cf8e0e 100644 --- a/varo/src/main/java/de/cliffbreak/varo/discord/listeners/MessageListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/discord/listeners/MessageListener.java @@ -41,5 +41,14 @@ public class MessageListener extends ListenerAdapter { null, null, null, null, new ArrayList(Arrays.asList()))).queue(); break; } + if (message.getContentRaw().startsWith("!stats")) { + if (message.getMentionedMembers().size() != 1) + return; + + final String id = message.getMentionedMembers().get(0).getId(); + final String uuid = this.plugin.getTeamManager().getPlayerByDiscordId(id); + final int kills = this.plugin.getDatabase().getKills(uuid); + channel.sendMessage("Der Spieler <@!" + id + "> hat momentan " + kills + " Kills.").queue(); + } } } \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java index c731792..e130141 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java @@ -1,8 +1,10 @@ package de.cliffbreak.varo.listeners; import org.bukkit.GameMode; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.scheduler.BukkitRunnable; @@ -24,8 +26,83 @@ public class PlayerDeathListener implements Listener { e.getEntity().kickPlayer("§4Du bist gestorben.\n \n§cDamit bist du aus §eVaro§c ausgeschieden."); } }.runTaskLater(this.plugin, 20); - e.getEntity().setHealth(20d); // Reset health to prevent death dialog after rejoin (if unbanned) + final EntityDamageEvent damageCause = e.getEntity().getLastDamageCause(); + final Player killer = e.getEntity().getKiller(); + if (killer != null) { + System.out.println(killer.getName()); + this.plugin.getDatabase().addKill(killer.getUniqueId().toString().replace("-", ""), + e.getEntity().getUniqueId().toString().replace("-", "")); + } else { + System.out.println("No killer"); + } + System.out.println(damageCause.getCause()); + switch (damageCause.getCause()) { + case BLOCK_EXPLOSION: + // TNT + if (killer == null) { + e.setDeathMessage("Hier die Todesnachricht einfügen.."); + } else { + e.setDeathMessage("Hier die Todesnachricht einfügen.. mit " + killer + "."); + } + break; + case CONTACT: + // Cactus + break; + case DROWNING: + break; + case ENTITY_EXPLOSION: + // Creeper + break; + case ENTITY_ATTACK: + case ENTITY_SWEEP_ATTACK: + break; + case FALL: + break; + case FALLING_BLOCK: + break; + case FIRE: + case FIRE_TICK: + break; + case HOT_FLOOR: + // Magma Block + break; + case LAVA: + break; + case LIGHTNING: + break; + case MAGIC: + // Potion (should be impossible) + break; + case POISON: + break; + case PROJECTILE: + break; + case STARVATION: + break; + case SUFFOCATION: + // Border damage or sand/gravel + break; + case THORNS: + break; + case VOID: + break; + case WITHER: + break; + case CRAMMING: // Very rare so handle as default death + case CUSTOM: // No custom damage provider so impossible + case DRAGON_BREATH: // End is disabled so impossible + case DRYOUT: // No fish so impossible + case FLY_INTO_WALL: // No Elytra so impossible + case MELTING: // No snowman so impossible + case SUICIDE: // No access to /kill command + default: + break; + } + + // Reset health to prevent death dialog after rejoin (if unbanned) + e.getEntity().setHealth(20d); e.getEntity().setGameMode(GameMode.SPECTATOR); + this.plugin.getBanUtils().addBan(e.getEntity().getUniqueId()); this.plugin.getDiscordBot().broadcastMessage("Der Spieler <@!" + this.plugin.getTeamManager().getDiscordIdByPlayer(e.getEntity()) + "> ist gestorben."); diff --git a/varo/src/main/java/de/cliffbreak/varo/managers/TeamManager.java b/varo/src/main/java/de/cliffbreak/varo/managers/TeamManager.java index de71e45..7222742 100644 --- a/varo/src/main/java/de/cliffbreak/varo/managers/TeamManager.java +++ b/varo/src/main/java/de/cliffbreak/varo/managers/TeamManager.java @@ -117,6 +117,20 @@ public class TeamManager { return ""; } + public String getPlayerByDiscordId(final String id) { + final JSONArray jsonTeams = config.getArray("teams"); + for (final Object jsonRawTeam : jsonTeams) { + final JSONObject jsonTeam = (JSONObject) jsonRawTeam; + for (final Object rawPlayer : ((JSONArray) jsonTeam.get("players"))) { + final JSONObject jsonPlayer = (JSONObject) rawPlayer; + if (jsonPlayer.get("discord").equals(id)) { + return jsonPlayer.get("uuid").toString(); + } + } + } + return ""; + } + public void setAFK(final String player) { final Team oldTeam = getTeamByPlayer(player); for (final Team team : this.teams) { diff --git a/varo/src/main/java/de/cliffbreak/varo/uitls/VaroUtils.java b/varo/src/main/java/de/cliffbreak/varo/uitls/VaroUtils.java index 872b9ef..61486ca 100644 --- a/varo/src/main/java/de/cliffbreak/varo/uitls/VaroUtils.java +++ b/varo/src/main/java/de/cliffbreak/varo/uitls/VaroUtils.java @@ -53,7 +53,7 @@ public class VaroUtils { // DEBUG specific Settings if (this.plugin.getConfig().getBoolean("Varo.Debug")) { - world.setDifficulty(Difficulty.PEACEFUL); + // world.setDifficulty(Difficulty.PEACEFUL); world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false); world.setGameRule(GameRule.DO_WEATHER_CYCLE, false); }