TF-Marriage/src/main/java/com/lenis0012/bukkit/marriage2/listeners/ChatListener.java

84 lines
3 KiB
Java
Raw Normal View History

2015-07-10 15:02:50 +00:00
package com.lenis0012.bukkit.marriage2.listeners;
import com.lenis0012.bukkit.marriage2.MPlayer;
2016-02-23 22:02:24 +00:00
import com.lenis0012.bukkit.marriage2.config.Settings;
2015-07-10 15:02:50 +00:00
import com.lenis0012.bukkit.marriage2.internal.MarriageCore;
2016-02-23 22:02:24 +00:00
import org.bukkit.Bukkit;
2015-07-10 15:02:50 +00:00
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
2016-02-23 22:02:24 +00:00
import org.bukkit.event.EventPriority;
2015-07-10 15:02:50 +00:00
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
public class ChatListener implements Listener {
private final MarriageCore core;
public ChatListener(MarriageCore core) {
this.core = core;
}
2016-02-23 22:02:24 +00:00
@EventHandler(priority = EventPriority.LOWEST)
2015-07-10 15:02:50 +00:00
public void onPlayerChat(AsyncPlayerChatEvent event) {
final Player player = event.getPlayer();
MPlayer mp = core.getMPlayer(player.getUniqueId());
2016-02-23 22:02:24 +00:00
if(mp.isInChat()) {
if(!isOnline(mp.getPartner())) {
mp.setInChat(false);
return;
}
event.setCancelled(true);
String message = Settings.PM_FORMAT.value()
.replace("{name}", player.getDisplayName())
.replace("{message}", event.getMessage())
.replace("{heart}", "\u2764");
message = ChatColor.translateAlternateColorCodes('&', message);
Player partner = Bukkit.getPlayer(mp.getPartner().getUniqueId());
player.sendMessage(message);
partner.sendMessage(message);
return;
}
2016-02-25 09:44:54 +00:00
if(!Settings.FORCE_FORMAT.value()) return;
event.setFormat("{marriage_status}" + event.getFormat()); // Enforce marriage format
2016-02-23 22:02:24 +00:00
}
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerChatLate(AsyncPlayerChatEvent event) {
2016-02-25 09:44:54 +00:00
final String format = event.getFormat();
2016-02-27 23:57:54 +00:00
final Player player = event.getPlayer();
final MPlayer mplayer = core.getMPlayer(player.getUniqueId());
2016-02-28 14:27:03 +00:00
if(format.contains("{marriage_status}")) {
String status = "";
if(mplayer.isMarried()) {
status = Settings.CHAT_FORMAT.value().replace("{heart}", "\u2764");
status = ChatColor.translateAlternateColorCodes('&', status);
}
2016-02-25 09:44:54 +00:00
event.setFormat(format.replace("{marriage_status}", status));
}
2016-02-23 22:02:24 +00:00
}
private void handleChat(AsyncPlayerChatEvent event, MPlayer mp) {
if(!mp.isMarried()) {
return;
2015-07-10 15:02:50 +00:00
}
2016-02-23 22:02:24 +00:00
// Set format
String format = Settings.CHAT_FORMAT.value()
.replace("{name}", "%1$s")
.replace("{message}", "%2$s")
.replace("{original_format}", event.getFormat())
.replace("{heart}", "\u2764");
format = ChatColor.translateAlternateColorCodes('&', format);
event.setFormat(format);
}
private boolean isOnline(MPlayer mp) {
if(mp == null) return false;
Player player = Bukkit.getPlayer(mp.getUniqueId());
return player != null && player.isOnline();
2015-07-10 15:02:50 +00:00
}
}