Cleans the chat listener

This commit is contained in:
Kristian Knarvik 2022-11-14 17:47:03 +01:00
parent ce63d39339
commit c060d55550
2 changed files with 63 additions and 26 deletions

View File

@ -19,6 +19,11 @@ public class BoundaryBypassListener implements Listener {
public final HungerArena plugin; public final HungerArena plugin;
/**
* Instantiates a new boundary bypass listener
*
* @param hungerArena <p>A reference to the hungerArena plugin</p>
*/
public BoundaryBypassListener(HungerArena hungerArena) { public BoundaryBypassListener(HungerArena hungerArena) {
this.plugin = hungerArena; this.plugin = hungerArena;
} }

View File

@ -1,5 +1,6 @@
package net.knarcraft.hungerarena.Listeners; package net.knarcraft.hungerarena.Listeners;
import net.knarcraft.hungerarena.Arena;
import net.knarcraft.hungerarena.HungerArena; import net.knarcraft.hungerarena.HungerArena;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -10,38 +11,69 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
import java.util.List; import java.util.List;
/**
* A listener for any in-arena chat messages
*/
public class ChatListener implements Listener { public class ChatListener implements Listener {
public final HungerArena plugin; public final HungerArena plugin;
public ChatListener(HungerArena m) { /**
this.plugin = m; * Instantiates a new chat listener
*
* @param hungerArena <p>A reference to the hungerArena plugin</p>
*/
public ChatListener(HungerArena hungerArena) {
this.plugin = hungerArena;
} }
@EventHandler @EventHandler
public void TributeChat(AsyncPlayerChatEvent event) { public void tributeChatListener(AsyncPlayerChatEvent event) {
Player p = event.getPlayer(); Player player = event.getPlayer();
String pname = p.getName(); String playerName = player.getName();
if (plugin.getArena(p) != null) { Arena playerArena = plugin.getArena(player);
String msg = "<" + ChatColor.RED + "[Tribute] " + ChatColor.WHITE + pname + ">" + " " + event.getMessage();
if (plugin.config.getBoolean("ChatClose")) { //If not in an arena, don't change the message
double radius = plugin.config.getDouble("ChatClose_Radius"); if (playerArena == null) {
List<Entity> near = p.getNearbyEntities(radius, radius, radius); return;
event.setCancelled(true); }
if (!(near.size() == 0)) {
p.sendMessage(msg); String chatMessage = "<" + ChatColor.RED + "[Tribute] " + ChatColor.WHITE + playerName + ">" + " " +
for (Entity e : near) { event.getMessage();
if (e instanceof Player) { //Cancel the normal chat event
e.sendMessage(msg); event.setCancelled(true);
}
} if (plugin.config.getBoolean("ChatClose")) {
} else { displayProximityChat(player, chatMessage);
p.sendMessage(msg); } else {
p.sendMessage(ChatColor.YELLOW + "No one near!"); //Broadcast the message to add the tribute prefix
} plugin.getServer().broadcastMessage(chatMessage);
} else {
event.setCancelled(true);
plugin.getServer().broadcastMessage(msg);
}
} }
} }
/**
* Displays the given chat message to anyone in the given player's proximity
*
* @param player <p>The player that sent the chat message</p>
* @param chatMessage <p>The chat message to be sent</p>
*/
private void displayProximityChat(Player player, String chatMessage) {
double radius = plugin.config.getDouble("ChatClose_Radius");
List<Entity> nearbyEntities = player.getNearbyEntities(radius, radius, radius);
//Display the message in the player's own chat
player.sendMessage(chatMessage);
if (nearbyEntities.size() != 0) {
//Display the message to all players in the set proximity
for (Entity nearbyEntity : nearbyEntities) {
if (nearbyEntity instanceof Player) {
nearbyEntity.sendMessage(chatMessage);
}
}
} else {
//Notify the player that no player is near enough to hear the message
player.sendMessage(ChatColor.YELLOW + "No one near!");
}
}
} }