feat: add basic player stats

This commit is contained in:
Simon Giesel 2020-05-19 14:28:01 +02:00
parent 72c6fa8d9c
commit 994fccf845
8 changed files with 176 additions and 2 deletions

View file

@ -1,5 +1,9 @@
{ {
"recommendations": [ "recommendations": [
"vscjava.vscode-java-pack", "vscjava.vscode-java-pack",
"streetsidesoftware.code-spell-checker",
"streetsidesoftware.code-spell-checker-german",
"petekinnecom.terminal-command-keys",
"alexcvzz.vscode-sqlite",
] ]
} }

View file

@ -5,6 +5,8 @@
"java.configuration.updateBuildConfiguration": "automatic", "java.configuration.updateBuildConfiguration": "automatic",
"cSpell.words": [ "cSpell.words": [
"Aragur", "Aragur",
"DRYOUT",
"Elytra",
"Gamemode", "Gamemode",
"Gamerules", "Gamerules",
"Gson", "Gson",
@ -14,6 +16,7 @@
"koords", "koords",
"mkdir", "mkdir",
"npcs", "npcs",
"sqlite",
"teamcolors", "teamcolors",
"testremove", "testremove",
"unban", "unban",

View file

@ -11,6 +11,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import de.cliffbreak.varo.commands.CoordsCommand; import de.cliffbreak.varo.commands.CoordsCommand;
import de.cliffbreak.varo.commands.VaroCommand; import de.cliffbreak.varo.commands.VaroCommand;
import de.cliffbreak.varo.commands.VaroTabCompleter; import de.cliffbreak.varo.commands.VaroTabCompleter;
import de.cliffbreak.varo.database.Database;
import de.cliffbreak.varo.discord.DiscordBot; import de.cliffbreak.varo.discord.DiscordBot;
import de.cliffbreak.varo.listeners.BannedActionListener; import de.cliffbreak.varo.listeners.BannedActionListener;
import de.cliffbreak.varo.listeners.BannedItemListener; import de.cliffbreak.varo.listeners.BannedItemListener;
@ -40,6 +41,7 @@ public class Varo extends JavaPlugin {
private TeamManager teamManager; private TeamManager teamManager;
private MessageUtils messageUtils; private MessageUtils messageUtils;
private DiscordBot discordBot; private DiscordBot discordBot;
private Database database;
private boolean canCreatePortal = false; private boolean canCreatePortal = false;
@ -57,6 +59,7 @@ public class Varo extends JavaPlugin {
this.varoUtils = new VaroUtils(this); this.varoUtils = new VaroUtils(this);
this.teamManager = new TeamManager(); this.teamManager = new TeamManager();
this.messageUtils = new MessageUtils(this); this.messageUtils = new MessageUtils(this);
this.database = new Database();
this.config.addDefault("Varo.Start", 1590427800000f); this.config.addDefault("Varo.Start", 1590427800000f);
this.config.addDefault("Varo.Debug", false); this.config.addDefault("Varo.Debug", false);
@ -91,6 +94,7 @@ public class Varo extends JavaPlugin {
public void onDisable() { public void onDisable() {
this.getLogger().info("Stopping CliffbreakVaro!"); this.getLogger().info("Stopping CliffbreakVaro!");
this.playerCache.shutdown(); this.playerCache.shutdown();
this.database.shutdown();
try { try {
this.discordBot.shutdown(); this.discordBot.shutdown();
} catch (final NoClassDefFoundError err) { } catch (final NoClassDefFoundError err) {
@ -144,4 +148,8 @@ public class Varo extends JavaPlugin {
public DiscordBot getDiscordBot() { public DiscordBot getDiscordBot() {
return this.discordBot; return this.discordBot;
} }
public Database getDatabase() {
return this.database;
}
} }

View file

@ -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;
}
}

View file

@ -41,5 +41,14 @@ public class MessageListener extends ListenerAdapter {
null, null, null, null, new ArrayList<Field>(Arrays.asList()))).queue(); null, null, null, null, new ArrayList<Field>(Arrays.asList()))).queue();
break; 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();
}
} }
} }

View file

@ -1,8 +1,10 @@
package de.cliffbreak.varo.listeners; package de.cliffbreak.varo.listeners;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.scheduler.BukkitRunnable; 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."); e.getEntity().kickPlayer("§4Du bist gestorben.\n \n§cDamit bist du aus §eVaro§c ausgeschieden.");
} }
}.runTaskLater(this.plugin, 20); }.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); e.getEntity().setGameMode(GameMode.SPECTATOR);
this.plugin.getBanUtils().addBan(e.getEntity().getUniqueId()); this.plugin.getBanUtils().addBan(e.getEntity().getUniqueId());
this.plugin.getDiscordBot().broadcastMessage("Der Spieler <@!" this.plugin.getDiscordBot().broadcastMessage("Der Spieler <@!"
+ this.plugin.getTeamManager().getDiscordIdByPlayer(e.getEntity()) + "> ist gestorben."); + this.plugin.getTeamManager().getDiscordIdByPlayer(e.getEntity()) + "> ist gestorben.");

View file

@ -117,6 +117,20 @@ public class TeamManager {
return ""; 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) { public void setAFK(final String player) {
final Team oldTeam = getTeamByPlayer(player); final Team oldTeam = getTeamByPlayer(player);
for (final Team team : this.teams) { for (final Team team : this.teams) {

View file

@ -53,7 +53,7 @@ public class VaroUtils {
// DEBUG specific Settings // DEBUG specific Settings
if (this.plugin.getConfig().getBoolean("Varo.Debug")) { 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_DAYLIGHT_CYCLE, false);
world.setGameRule(GameRule.DO_WEATHER_CYCLE, false); world.setGameRule(GameRule.DO_WEATHER_CYCLE, false);
} }