Kill Processing

This commit is contained in:
Rsl1122 2017-08-22 12:06:02 +03:00
parent 96ff3b3fbf
commit 8002864c73
7 changed files with 80 additions and 101 deletions

View File

@ -12,7 +12,6 @@ import java.util.UUID;
public class KillData {
private final UUID victim;
private int victimUserID;
private final long date;
private final String weapon;
@ -20,14 +19,12 @@ public class KillData {
* Creates a KillData object with given parameters.
*
* @param victim UUID of the victim.
* @param victimID ID of the victim, get from the database.
* @param weapon Weapon used.
* @param date Epoch millisecond at which the kill occurred.
*/
public KillData(UUID victim, int victimID, String weapon, long date) {
public KillData(UUID victim, String weapon, long date) {
this.victim = victim;
this.weapon = weapon;
victimUserID = victimID;
this.date = date;
}
@ -58,18 +55,10 @@ public class KillData {
return weapon;
}
/**
* Get the UserID of the victim, found from the database.
*
* @return For example: 6
*/
public int getVictimUserID() {
return victimUserID;
}
@Override
public String toString() {
return "{victim:" + victim + "|victimUserID:" + victimUserID + "|date:" + date + "|weapon:" + weapon + '}';
return "{victim:" + victim + "|date:" + date + "|weapon:" + weapon + '}';
}
@Override
@ -97,8 +86,4 @@ public class KillData {
hash = 89 * hash + Objects.hashCode(this.weapon);
return hash;
}
public void setVictimUserID(int victimUserID) {
this.victimUserID = victimUserID;
}
}

View File

@ -4,6 +4,7 @@ import main.java.com.djrapitops.plan.data.Session;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
/**
@ -59,9 +60,12 @@ public class SessionCache {
* @param uuid UUID of the player.
* @return Session or null if not cached.
*/
@Deprecated
public Session getSession(UUID uuid) {
return activeSessions.get(uuid);
public Optional<Session> getSession(UUID uuid) {
Session session = activeSessions.get(uuid);
if (session != null) {
return Optional.of(session);
}
return Optional.empty();
}
/**

View File

@ -10,6 +10,7 @@ package main.java.com.djrapitops.plan.data.handling.info;
* @author Rsl1122
* @since 3.0.0
*/
@Deprecated
public enum InfoType {
/**

View File

@ -1,42 +0,0 @@
package main.java.com.djrapitops.plan.data.handling.info;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.data.handling.KillHandling;
import org.bukkit.entity.LivingEntity;
import java.util.UUID;
/**
* HandlingInfo Class for DeathEvent information when the dead entity is a
* player.
*
* @author Rsl1122
* @since 3.0.0
*/
public class KillInfo extends HandlingInfo {
private final LivingEntity dead;
private final String weaponName;
/**
* Constructor.
*
* @param uuid UUID of the killer.
* @param time Epoch ms the event occurred.
* @param dead Dead entity (Mob or Player)
* @param weaponName Weapon used.
*/
public KillInfo(UUID uuid, long time, LivingEntity dead, String weaponName) {
super(uuid, InfoType.KILL, time);
this.dead = dead;
this.weaponName = weaponName;
}
@Override
public void process(UserData uData) {
if (!uData.getUuid().equals(uuid)) {
return;
}
KillHandling.processKillInfo(uData, time, dead, weaponName);
}
}

View File

@ -0,0 +1,59 @@
package main.java.com.djrapitops.plan.data.handling.player;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.KillData;
import main.java.com.djrapitops.plan.data.Session;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import java.util.Optional;
import java.util.UUID;
/**
* Processor Class for KillEvent information when the killer is a
* player.
*
* @author Rsl1122
* @since 4.0.0
*/
public class KillProcessor extends PlayerProcessor {
private final LivingEntity dead;
private final String weaponName;
private final long time;
/**
* Constructor.
*
* @param uuid UUID of the killer.
* @param time Epoch ms the event occurred.
* @param dead Dead entity (Mob or Player)
* @param weaponName Weapon used.
*/
public KillProcessor(UUID uuid, long time, LivingEntity dead, String weaponName) {
super(uuid);
this.time = time;
this.dead = dead;
this.weaponName = weaponName;
}
@Override
public void process() {
UUID uuid = getUUID();
Plan plugin = Plan.getInstance();
Optional<Session> cachedSession = plugin.getDataCache().getSession(uuid);
if (!cachedSession.isPresent()) {
return;
}
Session session = cachedSession.get();
if (dead instanceof Player) {
Player deadPlayer = (Player) dead;
session.playerKilled(new KillData(deadPlayer.getUniqueId(), weaponName, time));
} else {
session.mobKilled();
}
}
}

View File

@ -2,8 +2,8 @@ package main.java.com.djrapitops.plan.data.listeners;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.handling.info.KillInfo;
import main.java.com.djrapitops.plan.data.handling.player.DeathProcessor;
import main.java.com.djrapitops.plan.data.handling.player.KillProcessor;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.bukkit.Material;
@ -100,8 +100,7 @@ public class PlanDeathEventListener implements Listener {
}
if (Verify.notNull(killerUUID, weapon)) {
plugin.addToProcessQueue(new KillInfo(killerUUID, time, dead, weapon));
plugin.addToProcessQueue(new KillProcessor(killerUUID, time, dead, weapon));
}
}

View File

@ -25,6 +25,8 @@ public class KillsTable extends Table {
private final String columnServerID; //TODO
private final String columnSessionID; //TODO
// TODO Automatic UserID Retrieval from UsersTable.
/**
* @param db
* @param usingMySQL
@ -97,9 +99,8 @@ public class KillsTable extends Table {
set = statement.executeQuery();
List<KillData> killData = new ArrayList<>();
while (set.next()) {
int victimID = set.getInt(columnVictimUserID);
UUID victimUUID = usersTable.getUserUUID(String.valueOf(victimID));
killData.add(new KillData(victimUUID, victimID, set.getString(columnWeapon), set.getLong(columnDate)));
UUID victimUUID = null; // TODO Victim UUID Retrieval
killData.add(new KillData(victimUUID, set.getString(columnWeapon), set.getLong(columnDate)));
}
return killData;
} finally {
@ -128,24 +129,12 @@ public class KillsTable extends Table {
+ columnDate
+ ") VALUES (?, ?, ?, ?)");
boolean commitRequired = false;
kills.stream().filter(Objects::nonNull).forEach(killData -> {
int victimUserID = killData.getVictimUserID();
if (victimUserID == -1) {
try {
int newVictimID = db.getUsersTable().getUserId(killData.getVictim());
killData.setVictimUserID(newVictimID);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
return;
}
}
});
for (KillData kill : kills) {
if (kill == null || kill.getVictimUserID() == -1) {
if (kill == null) {
continue;
}
statement.setInt(1, userId);
statement.setInt(2, kill.getVictimUserID());
statement.setInt(2, -1); // TODO Victim ID Retrieval
statement.setString(3, kill.getWeapon());
statement.setLong(4, kill.getDate());
statement.addBatch();
@ -183,12 +172,11 @@ public class KillsTable extends Table {
}
while (set.next()) {
int killerID = set.getInt(columnKillerUserID);
int victimID = set.getInt(columnVictimUserID);
if (!ids.contains(killerID)) {
continue;
}
UUID victimUUID = uuids.get(victimID);
kills.get(killerID).add(new KillData(victimUUID, victimID, set.getString(columnWeapon), set.getLong(columnDate)));
UUID victimUUID = null; // TODO Victim UUID Retrieval
kills.get(killerID).add(new KillData(victimUUID, set.getString(columnWeapon), set.getLong(columnDate)));
}
return kills;
} finally {
@ -230,14 +218,9 @@ public class KillsTable extends Table {
playerKills.removeAll(s);
}
findMissingIDs(playerKills);
for (KillData kill : playerKills) {
if (kill.getVictimUserID() == -1) {
continue;
}
statement.setInt(1, id);
statement.setInt(2, kill.getVictimUserID());
statement.setInt(2, -1); // TODO Victim ID Retrieval
statement.setString(3, kill.getWeapon());
statement.setLong(4, kill.getDate());
statement.addBatch();
@ -253,14 +236,4 @@ public class KillsTable extends Table {
Benchmark.stop("Database", "Save Kills multiple");
}
}
private void findMissingIDs(List<KillData> playerKills) throws SQLException {
for (KillData killData : playerKills) {
int victimUserID = killData.getVictimUserID();
if (victimUserID == -1) {
int newVictimID = db.getUsersTable().getUserId(killData.getVictim());
killData.setVictimUserID(newVictimID);
}
}
}
}