feat: add AFK Player kill message

This commit is contained in:
Simon Giesel 2020-05-26 14:57:44 +02:00
parent 0dc4c3cb80
commit 950fc6ea01
7 changed files with 71 additions and 46 deletions

View file

@ -1,18 +1,21 @@
package de.cliffbreak.varo.events; package de.cliffbreak.varo.events;
import org.bukkit.entity.Player;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
public class PlayerInteractNPCEvent extends Event { public class PlayerInteractNPCEvent extends Event {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private final Type type; private final Type type;
private final Player damager;
private final int id; private final int id;
private final double damage; private final double damage;
private final boolean isCritical; private final boolean isCritical;
public PlayerInteractNPCEvent(Type type, int id, double damage, boolean isCritical) { public PlayerInteractNPCEvent(Type type, Player damager, int id, double damage, boolean isCritical) {
super(true); super(true);
this.type = type; this.type = type;
this.damager = damager;
this.id = id; this.id = id;
this.damage = damage; this.damage = damage;
this.isCritical = isCritical; this.isCritical = isCritical;
@ -25,6 +28,13 @@ public class PlayerInteractNPCEvent extends Event {
return type; return type;
} }
/**
* @return Player who did the Damage
*/
public Player getDamager() {
return damager;
}
/** /**
* @return EntityId of the Event * @return EntityId of the Event
*/ */

View file

@ -30,78 +30,86 @@ public class PlayerDeathListener implements Listener {
final EntityDamageEvent damageCause = e.getEntity().getLastDamageCause(); final EntityDamageEvent damageCause = e.getEntity().getLastDamageCause();
final Player killer = e.getEntity().getKiller(); final Player killer = e.getEntity().getKiller();
if (killer != null) { if (killer != null) {
System.out.println(killer.getName());
this.plugin.getDatabase().addKill(killer.getUniqueId().toString().replace("-", ""), this.plugin.getDatabase().addKill(killer.getUniqueId().toString().replace("-", ""),
e.getEntity().getUniqueId().toString().replace("-", "")); e.getEntity().getUniqueId().toString().replace("-", ""));
} else {
System.out.println("No killer");
} }
System.out.println(damageCause.getCause()); System.out.println(damageCause.getCause());
switch (damageCause.getCause()) { switch (damageCause.getCause()) {
case BLOCK_EXPLOSION: case BLOCK_EXPLOSION:
// TNT // TNT
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.BLOCK_EXPLOSION)); e.setDeathMessage(
this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.BLOCK_EXPLOSION));
break; break;
case CONTACT: case CONTACT:
// Cactus // Cactus
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.CONTACT)); e.setDeathMessage(
this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.CONTACT));
break; break;
case DROWNING: case DROWNING:
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.DROWNING)); e.setDeathMessage(
this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.DROWNING));
break; break;
case ENTITY_EXPLOSION: case ENTITY_EXPLOSION:
// Creeper // Creeper
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.ENTITY_EXPLOSION)); e.setDeathMessage(
this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.ENTITY_EXPLOSION));
break; break;
case ENTITY_ATTACK: case ENTITY_ATTACK:
case ENTITY_SWEEP_ATTACK: case ENTITY_SWEEP_ATTACK:
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.ENTITY_ATTACK)); e.setDeathMessage(
this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.ENTITY_ATTACK));
break; break;
case FALL: case FALL:
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.FALL)); e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.FALL));
break; break;
case FALLING_BLOCK: case FALLING_BLOCK:
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.FALLING_BLOCK)); e.setDeathMessage(
this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.FALLING_BLOCK));
break; break;
case FIRE: case FIRE:
case FIRE_TICK: case FIRE_TICK:
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.FIRE)); e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.FIRE));
break; break;
case HOT_FLOOR: case HOT_FLOOR:
// Magma Block // Magma Block
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.HOT_FLOOR)); e.setDeathMessage(
this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.HOT_FLOOR));
break; break;
case LAVA: case LAVA:
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.LAVA)); e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.LAVA));
break; break;
case LIGHTNING: case LIGHTNING:
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.LIGHTNING)); e.setDeathMessage(
this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.LIGHTNING));
break; break;
case MAGIC: case MAGIC:
// Potion (should be impossible) // Potion (should be impossible)
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.MAGIC)); e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.MAGIC));
break; break;
case POISON: case POISON:
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.POISON)); e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.POISON));
break; break;
case PROJECTILE: case PROJECTILE:
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.PROJECTILE)); e.setDeathMessage(
this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.PROJECTILE));
break; break;
case STARVATION: case STARVATION:
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.STARVATION)); e.setDeathMessage(
this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.STARVATION));
break; break;
case SUFFOCATION: case SUFFOCATION:
// Border damage or sand/gravel // Border damage or sand/gravel
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.SUFFOCATION)); e.setDeathMessage(
this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.SUFFOCATION));
break; break;
case THORNS: case THORNS:
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.THORNS)); e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.THORNS));
break; break;
case VOID: case VOID:
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.VOID)); e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.VOID));
break; break;
case WITHER: case WITHER:
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.WITHER)); e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.WITHER));
break; break;
case CRAMMING: // Very rare so handle as default death case CRAMMING: // Very rare so handle as default death
case CUSTOM: // No custom damage provider so impossible case CUSTOM: // No custom damage provider so impossible
@ -111,7 +119,7 @@ public class PlayerDeathListener implements Listener {
case MELTING: // No snowman so impossible case MELTING: // No snowman so impossible
case SUICIDE: // No access to /kill command case SUICIDE: // No access to /kill command
default: default:
e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.CUSTOM)); // CUSTOM == default e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.CUSTOM)); // CUSTOM == default
break; break;
} }
@ -121,17 +129,7 @@ public class PlayerDeathListener implements Listener {
this.plugin.getBorderManager().addPlayerDeath(); this.plugin.getBorderManager().addPlayerDeath();
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()
+ this.plugin.getTeamManager().getDiscordIdByPlayer(e.getEntity()) + "> ist gestorben."); .getDiscordIdByUuid(e.getEntity().getUniqueId().toString().replace("-", "")) + "> ist gestorben.");
} }
private String getDeathMessage(final Player victim, final Player killer, final DamageCause cause) {
if (killer == null) {
return this.plugin.getLangUtils().get("Death." + cause.toString()).replace("%player%", victim.getName());
} else {
return this.plugin.getLangUtils().get("Death." + cause.toString() + "killer")
.replace("%player%", victim.getName()).replace("%killer%", killer.getName());
}
}
} }

View file

@ -18,7 +18,7 @@ public class PlayerInteractNPCListener implements Listener {
@EventHandler @EventHandler
public void onEntityDamageByEntity(final PlayerInteractNPCEvent e) { public void onEntityDamageByEntity(final PlayerInteractNPCEvent e) {
if (e.getType().equals(Type.ATTACK)) { if (e.getType().equals(Type.ATTACK)) {
this.plugin.getNPCManager().addDamage(e.getId(), e.getDamage(), e.getIsCritical()); this.plugin.getNPCManager().addDamage(e.getDamager(), e.getId(), e.getDamage(), e.getIsCritical());
} }
} }

View file

@ -17,6 +17,7 @@ import org.bukkit.craftbukkit.v1_15_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.PlayerInventory;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
@ -138,7 +139,7 @@ public class NPCManager {
return false; return false;
} }
public void addDamage(final int id, final double damage, final boolean isCritical) { public void addDamage(Player damager, final int id, final double damage, final boolean isCritical) {
for (final EntityPlayer npc : this.npcs) { for (final EntityPlayer npc : this.npcs) {
if (npc.getId() == id) { if (npc.getId() == id) {
npc.setHealth((float) (npc.getHealth() - damage)); npc.setHealth((float) (npc.getHealth() - damage));
@ -182,8 +183,13 @@ public class NPCManager {
}.runTask(this.plugin); // Run Task in sync to be thread safe }.runTask(this.plugin); // Run Task in sync to be thread safe
this.plugin.getBanUtils().addBan(npc.getUniqueID()); this.plugin.getBanUtils().addBan(npc.getUniqueID());
// TODO:
Bukkit.broadcastMessage("TODO: Player(" + "??" + ") killed AFK Player (" + npc.getName() + ")"); Bukkit.broadcastMessage(this.plugin.getMessageUtils()
.getDeathMessage(npc.getBukkitEntity().getPlayer(), damager, DamageCause.CUSTOM));
this.plugin.getDatabase().addKill(damager.getUniqueId().toString().replace("-", ""),
npc.getUniqueID().toString().replace("-", ""));
this.plugin.getDiscordBot().broadcastMessage("Der Spieler <@!" + this.plugin.getTeamManager()
.getDiscordIdByUuid(npc.getUniqueID().toString().replace("-", "")) + "> ist gestorben.");
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override

View file

@ -109,13 +109,13 @@ public class TeamManager {
return null; return null;
} }
public String getDiscordIdByPlayer(final Player player) { public String getDiscordIdByUuid(final String uuid) {
final JSONArray jsonTeams = config.getArray("teams"); final JSONArray jsonTeams = config.getArray("teams");
for (final Object jsonRawTeam : jsonTeams) { for (final Object jsonRawTeam : jsonTeams) {
final JSONObject jsonTeam = (JSONObject) jsonRawTeam; final JSONObject jsonTeam = (JSONObject) jsonRawTeam;
for (final Object rawPlayer : ((JSONArray) jsonTeam.get("players"))) { for (final Object rawPlayer : ((JSONArray) jsonTeam.get("players"))) {
final JSONObject jsonPlayer = (JSONObject) rawPlayer; final JSONObject jsonPlayer = (JSONObject) rawPlayer;
if (jsonPlayer.get("uuid").equals(player.getUniqueId().toString().replace("-", ""))) { if (jsonPlayer.get("uuid").equals(uuid)) {
return jsonPlayer.get("discord").toString(); return jsonPlayer.get("discord").toString();
} }
} }

View file

@ -2,6 +2,8 @@ package de.cliffbreak.varo.utils;
import java.util.Calendar; import java.util.Calendar;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
import de.cliffbreak.varo.Varo; import de.cliffbreak.varo.Varo;
@ -58,4 +60,13 @@ public class MessageUtils {
+ String.format("%02d", calendar.get(Calendar.SECOND))).create())); + String.format("%02d", calendar.get(Calendar.SECOND))).create()));
return component; return component;
} }
public String getDeathMessage(final Player victim, final Player killer, final DamageCause cause) {
if (killer == null) {
return this.plugin.getLangUtils().get("Death." + cause.toString()).replace("%player%", victim.getName());
} else {
return this.plugin.getLangUtils().get("Death." + cause.toString() + "killer")
.replace("%player%", victim.getName()).replace("%killer%", killer.getName());
}
}
} }

View file

@ -43,7 +43,7 @@ public class PacketReader {
protected void decode(final ChannelHandlerContext arg0, final Packet<?> packet, final List<Object> arg2) protected void decode(final ChannelHandlerContext arg0, final Packet<?> packet, final List<Object> arg2)
throws Exception { throws Exception {
arg2.add(packet); arg2.add(packet);
readPacket(packet); readPacket(player, packet);
} }
}); });
} }
@ -55,7 +55,7 @@ public class PacketReader {
} }
// https://wiki.vg/Protocol#Interact_Entity // https://wiki.vg/Protocol#Interact_Entity
public void readPacket(final Packet<?> packet) { public void readPacket(final Player player, final Packet<?> packet) {
if (packet.getClass().getSimpleName().equalsIgnoreCase("PacketPlayInUseEntity")) { if (packet.getClass().getSimpleName().equalsIgnoreCase("PacketPlayInUseEntity")) {
final int id = (int) getValue(packet, "a"); final int id = (int) getValue(packet, "a");
@ -65,8 +65,8 @@ public class PacketReader {
try { try {
final Type type = Type.valueOf(getValue(packet, "action").toString()); final Type type = Type.valueOf(getValue(packet, "action").toString());
final PlayerInteractNPCEvent event = new PlayerInteractNPCEvent(type, id, calculateDamage(this.player), final PlayerInteractNPCEvent event = new PlayerInteractNPCEvent(type, player, id,
isCriticalHit(this.player)); calculateDamage(this.player), isCriticalHit(this.player));
Bukkit.getServer().getPluginManager().callEvent(event); Bukkit.getServer().getPluginManager().callEvent(event);
} catch (final Exception e) { } catch (final Exception e) {
// noop() // noop()