From 950fc6ea01e177409c1eeb1de93a06d138320195 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 26 May 2020 14:57:44 +0200 Subject: [PATCH] feat: add AFK Player kill message --- .../varo/events/PlayerInteractNPCEvent.java | 12 +++- .../varo/listeners/PlayerDeathListener.java | 68 +++++++++---------- .../listeners/PlayerInteractNPCListener.java | 2 +- .../cliffbreak/varo/managers/NPCManager.java | 12 +++- .../cliffbreak/varo/managers/TeamManager.java | 4 +- .../cliffbreak/varo/utils/MessageUtils.java | 11 +++ .../cliffbreak/varo/utils/PacketReader.java | 8 +-- 7 files changed, 71 insertions(+), 46 deletions(-) diff --git a/varo/src/main/java/de/cliffbreak/varo/events/PlayerInteractNPCEvent.java b/varo/src/main/java/de/cliffbreak/varo/events/PlayerInteractNPCEvent.java index dfb2ff4..6f888ff 100644 --- a/varo/src/main/java/de/cliffbreak/varo/events/PlayerInteractNPCEvent.java +++ b/varo/src/main/java/de/cliffbreak/varo/events/PlayerInteractNPCEvent.java @@ -1,18 +1,21 @@ package de.cliffbreak.varo.events; +import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; public class PlayerInteractNPCEvent extends Event { private static final HandlerList handlers = new HandlerList(); private final Type type; + private final Player damager; private final int id; private final double damage; 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); this.type = type; + this.damager = damager; this.id = id; this.damage = damage; this.isCritical = isCritical; @@ -25,6 +28,13 @@ public class PlayerInteractNPCEvent extends Event { return type; } + /** + * @return Player who did the Damage + */ + public Player getDamager() { + return damager; + } + /** * @return EntityId of the Event */ 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 307712e..ed47211 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java @@ -30,78 +30,86 @@ public class PlayerDeathListener implements Listener { 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 - e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.BLOCK_EXPLOSION)); + e.setDeathMessage( + this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.BLOCK_EXPLOSION)); break; case CONTACT: // Cactus - e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.CONTACT)); + e.setDeathMessage( + this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.CONTACT)); break; case DROWNING: - e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.DROWNING)); + e.setDeathMessage( + this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.DROWNING)); break; case ENTITY_EXPLOSION: // Creeper - e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.ENTITY_EXPLOSION)); + e.setDeathMessage( + this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.ENTITY_EXPLOSION)); break; case ENTITY_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; case FALL: - e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.FALL)); + e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.FALL)); break; 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; case FIRE: case FIRE_TICK: - e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.FIRE)); + e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.FIRE)); break; case HOT_FLOOR: // 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; case LAVA: - e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.LAVA)); + e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.LAVA)); break; case LIGHTNING: - e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.LIGHTNING)); + e.setDeathMessage( + this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.LIGHTNING)); break; case MAGIC: // 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; case POISON: - e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.POISON)); + e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.POISON)); break; case PROJECTILE: - e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.PROJECTILE)); + e.setDeathMessage( + this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.PROJECTILE)); break; case STARVATION: - e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.STARVATION)); + e.setDeathMessage( + this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.STARVATION)); break; case SUFFOCATION: // 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; case THORNS: - e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.THORNS)); + e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.THORNS)); break; case VOID: - e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.VOID)); + e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.VOID)); break; case WITHER: - e.setDeathMessage(this.getDeathMessage(e.getEntity(), killer, DamageCause.WITHER)); + e.setDeathMessage(this.plugin.getMessageUtils().getDeathMessage(e.getEntity(), killer, DamageCause.WITHER)); break; case CRAMMING: // Very rare so handle as default death case CUSTOM: // No custom damage provider so impossible @@ -111,7 +119,7 @@ public class PlayerDeathListener implements Listener { case MELTING: // No snowman so impossible case SUICIDE: // No access to /kill command 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; } @@ -121,17 +129,7 @@ public class PlayerDeathListener implements Listener { this.plugin.getBorderManager().addPlayerDeath(); this.plugin.getBanUtils().addBan(e.getEntity().getUniqueId()); - this.plugin.getDiscordBot().broadcastMessage("Der Spieler <@!" - + this.plugin.getTeamManager().getDiscordIdByPlayer(e.getEntity()) + "> ist gestorben."); + this.plugin.getDiscordBot().broadcastMessage("Der Spieler <@!" + this.plugin.getTeamManager() + .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()); - } - } - } \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerInteractNPCListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerInteractNPCListener.java index e4d0431..4cc0e59 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerInteractNPCListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerInteractNPCListener.java @@ -18,7 +18,7 @@ public class PlayerInteractNPCListener implements Listener { @EventHandler public void onEntityDamageByEntity(final PlayerInteractNPCEvent e) { 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()); } } diff --git a/varo/src/main/java/de/cliffbreak/varo/managers/NPCManager.java b/varo/src/main/java/de/cliffbreak/varo/managers/NPCManager.java index 55c2215..34833a6 100644 --- a/varo/src/main/java/de/cliffbreak/varo/managers/NPCManager.java +++ b/varo/src/main/java/de/cliffbreak/varo/managers/NPCManager.java @@ -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.inventory.CraftItemStack; import org.bukkit.entity.Player; +import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.scheduler.BukkitRunnable; @@ -138,7 +139,7 @@ public class NPCManager { 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) { if (npc.getId() == id) { npc.setHealth((float) (npc.getHealth() - damage)); @@ -182,8 +183,13 @@ public class NPCManager { }.runTask(this.plugin); // Run Task in sync to be thread safe 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() { @Override 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 461bcd4..7206cb9 100644 --- a/varo/src/main/java/de/cliffbreak/varo/managers/TeamManager.java +++ b/varo/src/main/java/de/cliffbreak/varo/managers/TeamManager.java @@ -109,13 +109,13 @@ public class TeamManager { return null; } - public String getDiscordIdByPlayer(final Player player) { + public String getDiscordIdByUuid(final String uuid) { 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("uuid").equals(player.getUniqueId().toString().replace("-", ""))) { + if (jsonPlayer.get("uuid").equals(uuid)) { return jsonPlayer.get("discord").toString(); } } diff --git a/varo/src/main/java/de/cliffbreak/varo/utils/MessageUtils.java b/varo/src/main/java/de/cliffbreak/varo/utils/MessageUtils.java index 10b4027..9a3599d 100644 --- a/varo/src/main/java/de/cliffbreak/varo/utils/MessageUtils.java +++ b/varo/src/main/java/de/cliffbreak/varo/utils/MessageUtils.java @@ -2,6 +2,8 @@ package de.cliffbreak.varo.utils; import java.util.Calendar; +import org.bukkit.entity.Player; +import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.scoreboard.Team; import de.cliffbreak.varo.Varo; @@ -58,4 +60,13 @@ public class MessageUtils { + String.format("%02d", calendar.get(Calendar.SECOND))).create())); 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()); + } + } } \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/utils/PacketReader.java b/varo/src/main/java/de/cliffbreak/varo/utils/PacketReader.java index 0730e44..910a91e 100644 --- a/varo/src/main/java/de/cliffbreak/varo/utils/PacketReader.java +++ b/varo/src/main/java/de/cliffbreak/varo/utils/PacketReader.java @@ -43,7 +43,7 @@ public class PacketReader { protected void decode(final ChannelHandlerContext arg0, final Packet packet, final List arg2) throws Exception { arg2.add(packet); - readPacket(packet); + readPacket(player, packet); } }); } @@ -55,7 +55,7 @@ public class PacketReader { } // 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")) { final int id = (int) getValue(packet, "a"); @@ -65,8 +65,8 @@ public class PacketReader { try { final Type type = Type.valueOf(getValue(packet, "action").toString()); - final PlayerInteractNPCEvent event = new PlayerInteractNPCEvent(type, id, calculateDamage(this.player), - isCriticalHit(this.player)); + final PlayerInteractNPCEvent event = new PlayerInteractNPCEvent(type, player, id, + calculateDamage(this.player), isCriticalHit(this.player)); Bukkit.getServer().getPluginManager().callEvent(event); } catch (final Exception e) { // noop()