Skip to content

Commit

Permalink
Clean up PvPListener and fix some small issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhydium committed Nov 10, 2024
1 parent 85e5a2b commit 9b476fa
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 171 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/mythicamc/commands/SetSpawnCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.mythicamc.commands;

public class SetSpawnCommand {
}
4 changes: 4 additions & 0 deletions src/main/java/com/mythicamc/commands/SpawnCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.mythicamc.commands;

public class SpawnCommand {
}
34 changes: 34 additions & 0 deletions src/main/java/com/mythicamc/listeners/PlayerCommandListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mythicamc.listeners;

import com.mythicamc.managers.CombatManager;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;

import java.util.List;

public class PlayerCommandListener implements Listener {

private final CombatManager combatManager;

public PlayerCommandListener(CombatManager combatManager) {
this.combatManager = combatManager;
}

@EventHandler
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) {
Player p = e.getPlayer();
if (combatManager.isTagged(p)) {
// List of blocked commands
List<String> blockedCommands = List.of("/spawn");
String command = e.getMessage().split(" ")[0].toLowerCase();

if (blockedCommands.contains(command)) {
p.sendMessage(ChatColor.RED + "You cannot use that command while in combat!");
e.setCancelled(true);
}
}
}
}
13 changes: 2 additions & 11 deletions src/main/java/com/mythicamc/listeners/PlayerJoinListener.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.mythicamc.listeners;

import com.mythicamc.KitPvP;
import com.mythicamc.utils.KitSelectorGUI;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class PlayerJoinListener implements Listener {

Expand Down Expand Up @@ -39,13 +37,6 @@ public void onPlayerJoin(PlayerJoinEvent event) {
plugin.getScoreboardManager().updateScoreboard(player);

// Give player a chest item for kit selection
ItemStack kitSelector = new ItemStack(Material.CHEST);
ItemMeta meta = kitSelector.getItemMeta();
if (meta != null) {
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&6&lKit Selector"));
kitSelector.setItemMeta(meta);
}

player.getInventory().setItem(0, kitSelector);
KitSelectorGUI.givePlayerKitSelectorItem(player);
}
}
24 changes: 17 additions & 7 deletions src/main/java/com/mythicamc/listeners/PlayerQuitListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.mythicamc.KitPvP;
import com.mythicamc.managers.CombatManager;
import com.mythicamc.managers.PlayerStatsManager;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -21,22 +23,30 @@ public PlayerQuitListener(KitPvP plugin) {
}

@EventHandler
public void onPlayerQuit(PlayerQuitEvent e) {
Player p = e.getPlayer();
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();

// Set custom quit message
String quitText = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.leave-message", "&8[&4-&8] &7%player_name% &7has left the server."));
quitText = PlaceholderAPI.setPlaceholders(player, quitText);
event.setQuitMessage(quitText);

// Check if player is tagged, if so punish them.
if (combatManager.isTagged(p)) {
punishPlayerForCombatLogging(p);
if (combatManager.isTagged(player)) {
punishPlayerForCombatLogging(player);
}

// Save PlayerStats to database
playerStatsManager.updateStatsInDatabase(p);
playerStatsManager.updateStatsInDatabase(player);

// Remove scoreboard
plugin.getScoreboardManager().removeScoreboard(p);
plugin.getScoreboardManager().removeScoreboard(player);

// Clear inventory
player.getInventory().clear();

// Clean up any combat tags
combatManager.removeTag(p);
combatManager.removeTag(player);
}

private void punishPlayerForCombatLogging(Player p) {
Expand Down
188 changes: 46 additions & 142 deletions src/main/java/com/mythicamc/listeners/PvPListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

import com.mythicamc.KitPvP;
import com.mythicamc.managers.CombatManager;
import com.mythicamc.utils.KitSelectorGUI;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.*;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

Expand All @@ -25,174 +25,79 @@ public class PvPListener implements Listener {
private final KitPvP plugin;
private final CombatManager combatManager;

private final Map<UUID, BukkitTask> combatTagTimers = new HashMap<>();
private final Set<UUID> respawningPlayers = new HashSet<>();

public PvPListener(KitPvP plugin) {
this.plugin = plugin;
this.combatManager = plugin.getCombatManager();
}

@EventHandler
public void onEntityDamageByEntity(EntityDamageByEntityEvent e) {
// Check if both entities are players
if (e.getEntity() instanceof Player damaged && e.getDamager() instanceof Player damager) {
// Tag both players
boolean damagedWasTagged = combatManager.tagPlayer(damaged);
boolean damagerWasTagged = combatManager.tagPlayer(damager);

// Notify players only if they were not already tagged
String tagMessage = plugin.getConfig().getString("messages.combat-tagged", "&cYou are now in combat!");
if (!damagerWasTagged) {
damager.sendMessage(ChatColor.translateAlternateColorCodes('&', tagMessage));
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
if (event.getEntity() instanceof Player damaged) {
// Determine the source of the damage
Entity damagerEntity = event.getDamager();
Player damager = null;

// If the damager is a player, assign directly
if (damagerEntity instanceof Player) {
damager = (Player) damagerEntity;
}
if (!damagedWasTagged) {
damaged.sendMessage(ChatColor.translateAlternateColorCodes('&', tagMessage));
// If the damager is a projectile get its shooter
else if (damagerEntity instanceof Projectile projectile) {
if (projectile.getShooter() instanceof Player) {
damager = (Player) projectile.getShooter();
}
}

// Start combat tag timer in action bar
startCombatTagTimer(damaged);
startCombatTagTimer(damager);
// Tag both players only if the damager is a player
if (damager != null) {
boolean damagedWasTagged = combatManager.tagPlayer(damaged);
boolean damagerWasTagged = combatManager.tagPlayer(damager);

// Fake death behaviour
double finalDamage = e.getFinalDamage();
// Notify players only if they were not already tagged
String tagMessage = plugin.getConfig().getString("messages.combat-tagged", "&cYou are now in combat!");
if (!damagerWasTagged) {
damager.sendMessage(ChatColor.translateAlternateColorCodes('&', tagMessage));
}
if (!damagedWasTagged) {
damaged.sendMessage(ChatColor.translateAlternateColorCodes('&', tagMessage));
}
}

// Fake death behavior
double finalDamage = event.getFinalDamage();
double health = damaged.getHealth();

// Check if the player would die from this damage
if (health - finalDamage <= 0) {
e.setCancelled(true); // Prevent death

// Determine the damager if killed by bow, by default we already have the damager
if (damager instanceof Projectile projectile) {
if (projectile.getShooter() instanceof Player) {
damager = (Player) projectile.getShooter();
}
}

// Simulate death
simulateDeath(damaged, damager);
event.setCancelled(true); // Prevent actual death
simulateDeath(damaged, damager); // Simulate death, passing damager (null if not player)
}
}
}

@EventHandler
public void onEntityDamage(EntityDamageEvent e) {
// Skip handling if this is an EntityDamageByEntityEvent to avoid duplication
if (e instanceof EntityDamageByEntityEvent) {
return;
}

// Check if the damaged entity is a player
if (e.getEntity() instanceof Player victim) {
double finalDamage = e.getFinalDamage();
double health = victim.getHealth();

// Check if the player would die from this damage
if (health - finalDamage <= 0) {
e.setCancelled(true); // Prevent death

// Simulate death with no killer
simulateDeath(victim, (Player) null);
}
}
}

@EventHandler
public void onPlayerQuit(PlayerQuitEvent e) {
Player p = e.getPlayer();
cancelCombatTagTimer(p);
}

@EventHandler
public void onPlayerTeleport(PlayerTeleportEvent e) {
Player player = e.getPlayer();
if (respawningPlayers.contains(player.getUniqueId())) {
e.setCancelled(true);
}
}

@EventHandler
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) {
Player p = e.getPlayer();
if (combatManager.isTagged(p)) {
// List of blocked commands
List<String> blockedCommands = List.of("/spawn");
String command = e.getMessage().split(" ")[0].toLowerCase();

if (blockedCommands.contains(command)) {
p.sendMessage(ChatColor.RED + "You cannot use that command while in combat!");
e.setCancelled(true);
e.setCancelled(true); // Prevent actual death
simulateDeath(victim, null); // Simulate death with no specific killer
}
}
}

private void cancelCombatTagTimer(Player p) {
UUID playerId = p.getUniqueId();
if (combatTagTimers.containsKey(playerId)) {
combatTagTimers.get(playerId).cancel();
combatTagTimers.remove(playerId);
}
}

private void startCombatTagTimer(Player p) {
UUID playerId = p.getUniqueId();

// Cancel any existing timer for the player
if (combatTagTimers.containsKey(playerId)) {
combatTagTimers.get(playerId).cancel();
}

BukkitTask task = new BukkitRunnable() {
@Override
public void run() {
if (!p.isOnline()) {
cancel();
combatTagTimers.remove(playerId);
return;
}

long remainingTime = combatManager.getRemainingTagTime(p);
int secondsLeft = (int) Math.ceil(remainingTime / 1000.0);

if (remainingTime <= 0) {
// Play sound
p.playSound(p.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1.0f, 1.0f);

// Send action bar message
String actionBarMessage = ChatColor.translateAlternateColorCodes('&', "&aYou are no longer in combat!");
p.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(actionBarMessage));

// Send chat message
String untagMessage = plugin.getConfig().getString("messages.combat-untagged", "&aYou are no longer in combat!");
p.sendMessage(ChatColor.translateAlternateColorCodes('&', untagMessage));

// Remove combat tag
combatManager.removeTag(p);

// Update scoreboard
plugin.getScoreboardManager().updateScoreboard(p);

// Cancel the timer
cancel();
combatTagTimers.remove(playerId);
return;
}

if (secondsLeft <= 3) {
p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1.0f, 1.0f);
}

String actionBarMessage = ChatColor.translateAlternateColorCodes('&', "&cIn combat: " + secondsLeft + " seconds!");
p.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(actionBarMessage));
}
}.runTaskTimer(plugin, 0L, 20L); // Update every second

// Store the task so we can cancel it later if needed
combatTagTimers.put(playerId, task);
}

private void startRespawnCountdown(Player player) {
int countdownTime = plugin.getConfig().getInt("respawn.countdown", 3);

// Add player to respawningPlayers set
respawningPlayers.add(player.getUniqueId());

// Set player to Spectator mode
player.setGameMode(GameMode.SPECTATOR);

Expand All @@ -211,13 +116,13 @@ public void run() {
// Set player back to Survival mode
player.setGameMode(GameMode.SURVIVAL);

// Remove player from respawningPlayers set
respawningPlayers.remove(player.getUniqueId());

// Teleport player to spawn location
Location respawnLocation = player.getWorld().getSpawnLocation(); // Has to be replaced with config value
player.teleport(respawnLocation);

// Give player the kit selector
KitSelectorGUI.givePlayerKitSelectorItem(player);

// Send a title upon respawn
String respawnTitle = ChatColor.translateAlternateColorCodes('&', "&aReady to fight.");
player.sendTitle(respawnTitle, "", 10, 70, 20);
Expand All @@ -231,7 +136,6 @@ public void run() {
private void simulateDeath(Player victim, Player killer) {
// Remove combat tag and cancel timers
combatManager.removeTag(victim);
cancelCombatTagTimer(victim);

// Send action bar and chat message
victim.sendMessage(ChatColor.translateAlternateColorCodes('&', "&aYou are no longer in combat!"));
Expand All @@ -249,12 +153,12 @@ private void simulateDeath(Player victim, Player killer) {
victim.setExp(0);
victim.setLevel(0);

// Send messages to victim and killer
sendDeathMessages(victim, killer);

// Update stats
plugin.getStatsManager().addDeath(victim);
if (killer != null) {
// Send messages to victim and killer
sendDeathMessages(victim, killer);

plugin.getStatsManager().addKill(killer);

// Reward killer with coins
Expand Down
Loading

0 comments on commit 9b476fa

Please sign in to comment.