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;
/**
* Instantiates a new boundary bypass listener
*
* @param hungerArena <p>A reference to the hungerArena plugin</p>
*/
public BoundaryBypassListener(HungerArena hungerArena) {
this.plugin = hungerArena;
}

View File

@ -1,5 +1,6 @@
package net.knarcraft.hungerarena.Listeners;
import net.knarcraft.hungerarena.Arena;
import net.knarcraft.hungerarena.HungerArena;
import org.bukkit.ChatColor;
import org.bukkit.entity.Entity;
@ -10,38 +11,69 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
import java.util.List;
/**
* A listener for any in-arena chat messages
*/
public class ChatListener implements Listener {
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
public void TributeChat(AsyncPlayerChatEvent event) {
Player p = event.getPlayer();
String pname = p.getName();
if (plugin.getArena(p) != null) {
String msg = "<" + ChatColor.RED + "[Tribute] " + ChatColor.WHITE + pname + ">" + " " + event.getMessage();
if (plugin.config.getBoolean("ChatClose")) {
double radius = plugin.config.getDouble("ChatClose_Radius");
List<Entity> near = p.getNearbyEntities(radius, radius, radius);
event.setCancelled(true);
if (!(near.size() == 0)) {
p.sendMessage(msg);
for (Entity e : near) {
if (e instanceof Player) {
e.sendMessage(msg);
}
}
} else {
p.sendMessage(msg);
p.sendMessage(ChatColor.YELLOW + "No one near!");
}
} else {
event.setCancelled(true);
plugin.getServer().broadcastMessage(msg);
}
public void tributeChatListener(AsyncPlayerChatEvent event) {
Player player = event.getPlayer();
String playerName = player.getName();
Arena playerArena = plugin.getArena(player);
//If not in an arena, don't change the message
if (playerArena == null) {
return;
}
String chatMessage = "<" + ChatColor.RED + "[Tribute] " + ChatColor.WHITE + playerName + ">" + " " +
event.getMessage();
//Cancel the normal chat event
event.setCancelled(true);
if (plugin.config.getBoolean("ChatClose")) {
displayProximityChat(player, chatMessage);
} else {
//Broadcast the message to add the tribute prefix
plugin.getServer().broadcastMessage(chatMessage);
}
}
/**
* 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!");
}
}
}