2012-08-26 00:54:15 +02:00
|
|
|
package com.massivecraft.factions.integration.herochat;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
import net.milkbowl.vault.chat.Chat;
|
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
|
|
|
import com.dthielke.herochat.Channel;
|
|
|
|
import com.dthielke.herochat.ChannelChatEvent;
|
|
|
|
import com.dthielke.herochat.ChannelStorage;
|
2013-06-27 11:03:08 +02:00
|
|
|
import com.dthielke.herochat.ChatCompleteEvent;
|
2012-08-26 00:54:15 +02:00
|
|
|
import com.dthielke.herochat.Chatter;
|
|
|
|
import com.dthielke.herochat.Herochat;
|
|
|
|
import com.dthielke.herochat.MessageFormatSupplier;
|
|
|
|
import com.dthielke.herochat.MessageNotFoundException;
|
2013-04-09 13:24:55 +02:00
|
|
|
import com.massivecraft.factions.Rel;
|
2013-04-22 17:59:51 +02:00
|
|
|
import com.massivecraft.factions.entity.UPlayer;
|
2013-04-22 09:37:53 +02:00
|
|
|
import com.massivecraft.factions.entity.Faction;
|
2012-08-26 00:54:15 +02:00
|
|
|
|
2014-06-04 14:02:23 +02:00
|
|
|
public abstract class ChannelFactionsAbstract implements Channel
|
2012-08-26 00:54:15 +02:00
|
|
|
{
|
|
|
|
private static final Pattern msgPattern = Pattern.compile("(.*)<(.*)%1\\$s(.*)> %2\\$s");
|
|
|
|
private final ChannelStorage storage = Herochat.getChannelManager().getStorage();
|
|
|
|
private final MessageFormatSupplier formatSupplier = Herochat.getChannelManager();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean addMember(Chatter chatter, boolean announce, boolean flagUpdate)
|
|
|
|
{
|
|
|
|
if (chatter.hasChannel(this)) return false;
|
2013-06-27 11:03:08 +02:00
|
|
|
|
|
|
|
if ((announce) && (this.isVerbose()))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
this.announce(Herochat.getMessage("channel_join").replace("$1", chatter.getPlayer().getDisplayName()));
|
|
|
|
}
|
|
|
|
catch (MessageNotFoundException e)
|
|
|
|
{
|
|
|
|
Herochat.severe("Messages.properties is missing: channel_join");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-26 00:54:15 +02:00
|
|
|
chatter.addChannel(this, announce, flagUpdate);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean kickMember(Chatter chatter, boolean announce)
|
|
|
|
{
|
|
|
|
if (!chatter.hasChannel(this)) return false;
|
|
|
|
this.removeMember(chatter, false, true);
|
|
|
|
|
|
|
|
if (announce)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
announce(Herochat.getMessage("channel_kick").replace("$1", chatter.getPlayer().getDisplayName()));
|
|
|
|
}
|
|
|
|
catch (MessageNotFoundException e)
|
|
|
|
{
|
|
|
|
Herochat.severe("Messages.properties is missing: channel_kick");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean removeMember(Chatter chatter, boolean announce, boolean flagUpdate)
|
|
|
|
{
|
|
|
|
if (!chatter.hasChannel(this)) return false;
|
|
|
|
chatter.removeChannel(this, announce, flagUpdate);
|
2013-06-27 11:03:08 +02:00
|
|
|
|
|
|
|
if (announce && this.isVerbose())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
this.announce(Herochat.getMessage("channel_leave").replace("$1", chatter.getPlayer().getDisplayName()));
|
|
|
|
}
|
|
|
|
catch (MessageNotFoundException e)
|
|
|
|
{
|
|
|
|
Herochat.severe("Messages.properties is missing: channel_leave");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-26 00:54:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<Chatter> getMembers()
|
|
|
|
{
|
|
|
|
Set<Chatter> ret = new HashSet<Chatter>();
|
|
|
|
for (Chatter chatter : Herochat.getChatterManager().getChatters())
|
|
|
|
{
|
|
|
|
if(chatter.hasChannel(this)) ret.add(chatter);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void announce(String message)
|
|
|
|
{
|
2013-06-27 11:03:08 +02:00
|
|
|
String colorized = message.replaceAll("(?i)&([a-fklmno0-9])", "§$1");
|
|
|
|
message = applyFormat(this.getFormatSupplier().getAnnounceFormat(), "").replace("%2$s", colorized);
|
2012-08-26 00:54:15 +02:00
|
|
|
for (Chatter member : this.getMembers())
|
|
|
|
{
|
|
|
|
member.getPlayer().sendMessage(message);
|
|
|
|
}
|
|
|
|
Herochat.logChat(ChatColor.stripColor(message));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String applyFormat(String format, String originalFormat)
|
|
|
|
{
|
2013-06-27 11:03:08 +02:00
|
|
|
format = format.replace("{default}", this.getFormatSupplier().getStandardFormat());
|
2012-08-26 00:54:15 +02:00
|
|
|
format = format.replace("{name}", this.getName());
|
|
|
|
format = format.replace("{nick}", this.getNick());
|
|
|
|
format = format.replace("{color}", this.getColor().toString());
|
|
|
|
format = format.replace("{msg}", "%2$s");
|
|
|
|
|
|
|
|
Matcher matcher = msgPattern.matcher(originalFormat);
|
2013-06-27 11:03:08 +02:00
|
|
|
if (matcher.matches() && matcher.groupCount() == 3)
|
2012-08-26 00:54:15 +02:00
|
|
|
{
|
|
|
|
format = format.replace("{sender}", matcher.group(1) + matcher.group(2) + "%1$s" + matcher.group(3));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
format = format.replace("{sender}", "%1$s");
|
|
|
|
}
|
|
|
|
|
2013-06-27 11:03:08 +02:00
|
|
|
format = format.replaceAll("(?i)&([a-fklmnor0-9])", "§$1");
|
2012-08-26 00:54:15 +02:00
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String applyFormat(String format, String originalFormat, Player sender)
|
|
|
|
{
|
2013-06-27 11:03:08 +02:00
|
|
|
format = this.applyFormat(format, originalFormat);
|
2012-08-26 00:54:15 +02:00
|
|
|
format = format.replace("{plainsender}", sender.getName());
|
|
|
|
format = format.replace("{world}", sender.getWorld().getName());
|
|
|
|
Chat chat = Herochat.getChatService();
|
|
|
|
if (chat != null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
String prefix = chat.getPlayerPrefix(sender);
|
2013-06-27 11:03:08 +02:00
|
|
|
if (prefix == null || prefix == "")
|
|
|
|
{
|
|
|
|
prefix = chat.getPlayerPrefix((String)null, sender.getName());
|
|
|
|
}
|
2012-08-26 00:54:15 +02:00
|
|
|
String suffix = chat.getPlayerSuffix(sender);
|
2013-06-27 11:03:08 +02:00
|
|
|
if (suffix == null || suffix == "")
|
|
|
|
{
|
|
|
|
suffix = chat.getPlayerSuffix((String)null, sender.getName());
|
|
|
|
}
|
2012-08-26 00:54:15 +02:00
|
|
|
String group = chat.getPrimaryGroup(sender);
|
|
|
|
String groupPrefix = group == null ? "" : chat.getGroupPrefix(sender.getWorld(), group);
|
2013-06-27 11:03:08 +02:00
|
|
|
if (group != null && (groupPrefix == null || groupPrefix == ""))
|
|
|
|
{
|
|
|
|
groupPrefix = chat.getGroupPrefix((String)null, group);
|
|
|
|
}
|
2012-08-26 00:54:15 +02:00
|
|
|
String groupSuffix = group == null ? "" : chat.getGroupSuffix(sender.getWorld(), group);
|
2013-06-27 11:03:08 +02:00
|
|
|
if (group != null && (groupSuffix == null || groupSuffix == ""))
|
|
|
|
{
|
|
|
|
groupSuffix = chat.getGroupSuffix((String)null, group);
|
|
|
|
}
|
2012-08-26 00:54:15 +02:00
|
|
|
format = format.replace("{prefix}", prefix == null ? "" : prefix.replace("%", "%%"));
|
|
|
|
format = format.replace("{suffix}", suffix == null ? "" : suffix.replace("%", "%%"));
|
|
|
|
format = format.replace("{group}", group == null ? "" : group.replace("%", "%%"));
|
|
|
|
format = format.replace("{groupprefix}", groupPrefix == null ? "" : groupPrefix.replace("%", "%%"));
|
|
|
|
format = format.replace("{groupsuffix}", groupSuffix == null ? "" : groupSuffix.replace("%", "%%"));
|
2013-06-27 11:03:08 +02:00
|
|
|
}
|
|
|
|
catch (UnsupportedOperationException ignored)
|
|
|
|
{
|
|
|
|
|
2012-08-26 00:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
format = format.replace("{prefix}", "");
|
|
|
|
format = format.replace("{suffix}", "");
|
|
|
|
format = format.replace("{group}", "");
|
|
|
|
format = format.replace("{groupprefix}", "");
|
|
|
|
format = format.replace("{groupsuffix}", "");
|
|
|
|
}
|
|
|
|
format = format.replaceAll("(?i)&([a-fklmno0-9])", "§$1");
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void emote(Chatter sender, String message)
|
|
|
|
{
|
2013-06-27 11:03:08 +02:00
|
|
|
message = this.applyFormat(this.getFormatSupplier().getEmoteFormat(), "").replace("%2$s", message);
|
|
|
|
|
2012-08-26 00:54:15 +02:00
|
|
|
Set<Player> recipients = new HashSet<Player>();
|
|
|
|
for (Chatter member : this.getMembers())
|
|
|
|
{
|
|
|
|
recipients.add(member.getPlayer());
|
|
|
|
}
|
|
|
|
|
2013-06-27 11:03:08 +02:00
|
|
|
this.trimRecipients(recipients, sender);
|
2012-08-26 00:54:15 +02:00
|
|
|
|
2013-06-27 11:03:08 +02:00
|
|
|
for (Player recipient : recipients)
|
2012-08-26 00:54:15 +02:00
|
|
|
{
|
2013-06-27 11:03:08 +02:00
|
|
|
recipient.sendMessage(message);
|
2012-08-26 00:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isMuted(String name)
|
|
|
|
{
|
|
|
|
if (this.isMuted()) return true;
|
|
|
|
return this.getMutes().contains(name.toLowerCase());
|
|
|
|
}
|
|
|
|
|
2012-08-26 02:12:02 +02:00
|
|
|
public abstract Set<Rel> getTargetRelations();
|
|
|
|
|
|
|
|
public Set<Player> getRecipients(Player sender)
|
|
|
|
{
|
|
|
|
Set<Player> ret = new HashSet<Player>();
|
|
|
|
|
2013-04-22 17:59:51 +02:00
|
|
|
UPlayer fpsender = UPlayer.get(sender);
|
2013-04-22 12:26:13 +02:00
|
|
|
Faction faction = fpsender.getFaction();
|
|
|
|
String universe = fpsender.getUniverse();
|
2012-08-26 02:12:02 +02:00
|
|
|
|
2013-04-22 12:26:13 +02:00
|
|
|
for (Player player : Bukkit.getOnlinePlayers())
|
2012-08-26 02:12:02 +02:00
|
|
|
{
|
2013-04-22 17:59:51 +02:00
|
|
|
UPlayer frecipient = UPlayer.get(player);
|
2013-04-22 12:26:13 +02:00
|
|
|
if (!frecipient.getUniverse().equals(universe)) continue;
|
|
|
|
if (!this.getTargetRelations().contains(faction.getRelationTo(frecipient))) continue;
|
|
|
|
ret.add(player);
|
2012-08-26 02:12:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-26 00:54:15 +02:00
|
|
|
@Override
|
|
|
|
public void processChat(ChannelChatEvent event)
|
|
|
|
{
|
2013-06-27 11:03:08 +02:00
|
|
|
Player player = event.getSender().getPlayer();
|
|
|
|
|
|
|
|
String format = this.applyFormat(event.getFormat(), event.getBukkitFormat(), player);
|
|
|
|
|
2012-08-26 00:54:15 +02:00
|
|
|
Chatter sender = Herochat.getChatterManager().getChatter(player);
|
2013-06-28 07:44:00 +02:00
|
|
|
|
|
|
|
// NOTE: This line is not standard deobfuscation. It's altered to achieve the recipient limitations.
|
|
|
|
Set<Player> recipients = this.getRecipients(player);
|
2013-06-27 11:03:08 +02:00
|
|
|
|
|
|
|
this.trimRecipients(recipients, sender);
|
|
|
|
String msg = String.format(format, new Object[] { player.getDisplayName(), event.getMessage() });
|
2012-08-26 00:54:15 +02:00
|
|
|
for (Player recipient : recipients)
|
|
|
|
{
|
|
|
|
recipient.sendMessage(msg);
|
|
|
|
}
|
2013-06-27 11:03:08 +02:00
|
|
|
|
|
|
|
Bukkit.getPluginManager().callEvent(new ChatCompleteEvent(sender, this, msg));
|
2012-08-26 00:54:15 +02:00
|
|
|
Herochat.logChat(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isMessageHeard(Set<Player> recipients, Chatter sender)
|
|
|
|
{
|
|
|
|
if (!isLocal()) return true;
|
|
|
|
|
|
|
|
Player senderPlayer = sender.getPlayer();
|
|
|
|
for (Player recipient : recipients)
|
|
|
|
{
|
|
|
|
if (recipient.equals(senderPlayer)) continue;
|
|
|
|
if (recipient.hasPermission("herochat.admin.stealth")) continue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void trimRecipients(Set<Player> recipients, Chatter sender)
|
|
|
|
{
|
|
|
|
World world = sender.getPlayer().getWorld();
|
|
|
|
|
|
|
|
Set<Chatter> members = this.getMembers();
|
|
|
|
Iterator<Player> iterator = recipients.iterator();
|
|
|
|
while(iterator.hasNext())
|
|
|
|
{
|
|
|
|
Chatter recipient = Herochat.getChatterManager().getChatter(iterator.next());
|
|
|
|
if (recipient == null) continue;
|
|
|
|
World recipientWorld = recipient.getPlayer().getWorld();
|
|
|
|
|
|
|
|
if (!members.contains(recipient))
|
|
|
|
iterator.remove();
|
|
|
|
else if ((isLocal()) && (!sender.isInRange(recipient, this.getDistance())))
|
|
|
|
iterator.remove();
|
|
|
|
else if (!hasWorld(recipientWorld))
|
|
|
|
iterator.remove();
|
|
|
|
else if (recipient.isIgnoring(sender))
|
|
|
|
iterator.remove();
|
|
|
|
else if ((!this.isCrossWorld()) && (!world.equals(recipientWorld)))
|
|
|
|
iterator.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean equals(Object other)
|
|
|
|
{
|
|
|
|
if (other == this) return true;
|
|
|
|
if (other == null) return false;
|
|
|
|
if (!(other instanceof Channel)) return false;
|
|
|
|
Channel channel = (Channel)other;
|
|
|
|
return (this.getName().equalsIgnoreCase(channel.getName())) || (this.getName().equalsIgnoreCase(channel.getNick()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public int hashCode()
|
|
|
|
{
|
|
|
|
int prime = 31;
|
|
|
|
int result = 1;
|
|
|
|
result = prime * result + (this.getName() == null ? 0 : this.getName().toLowerCase().hashCode());
|
|
|
|
result = prime * result + (this.getNick() == null ? 0 : this.getNick().toLowerCase().hashCode());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override public boolean isTransient() { return false; }
|
|
|
|
@Override public String getPassword() { return ""; }
|
|
|
|
@Override public void setPassword(String password) {}
|
|
|
|
@Override public boolean isVerbose() { return false; }
|
|
|
|
@Override public void setVerbose(boolean verbose) {}
|
|
|
|
@Override public boolean isHidden() { return false; }
|
|
|
|
@Override public boolean isLocal() { return this.getDistance() != 0; }
|
|
|
|
@Override public void attachStorage(ChannelStorage storage) { }
|
|
|
|
@Override public boolean banMember(Chatter chatter, boolean announce) { return false; }
|
|
|
|
@Override public Set<String> getBans() { return Collections.emptySet(); }
|
|
|
|
@Override public Set<String> getModerators() { return Collections.emptySet(); }
|
|
|
|
@Override public Set<String> getMutes() { return Collections.emptySet(); }
|
|
|
|
@Override public ChannelStorage getStorage() { return this.storage; }
|
|
|
|
@Override public boolean hasWorld(String world) { return (this.getWorlds().isEmpty()) || (this.getWorlds().contains(world)); }
|
|
|
|
@Override public boolean hasWorld(World world) { return this.hasWorld(world.getName()); }
|
|
|
|
@Override public boolean isBanned(String name) { return this.getBans().contains(name.toLowerCase()); }
|
|
|
|
@Override public boolean isMember(Chatter chatter) { return this.getMembers().contains(chatter); }
|
|
|
|
@Override public boolean isModerator(String name) { return this.getModerators().contains(name.toLowerCase()); }
|
|
|
|
|
|
|
|
@Override public void onFocusGain(Chatter chatter) {}
|
|
|
|
@Override public void onFocusLoss(Chatter chatter) {}
|
|
|
|
|
|
|
|
@Override public void removeWorld(String world) { this.getWorlds().remove(world); }
|
|
|
|
@Override public void setBanned(String name, boolean banned) {}
|
|
|
|
@Override public void setBans(Set<String> bans) {}
|
|
|
|
@Override public void setModerator(String name, boolean moderator) {}
|
|
|
|
@Override public void setModerators(Set<String> moderators) { }
|
|
|
|
@Override public void setMuted(String name, boolean muted) {}
|
|
|
|
@Override public void setMutes(Set<String> mutes) {}
|
2013-06-27 11:03:08 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public MessageFormatSupplier getFormatSupplier()
|
|
|
|
{
|
|
|
|
return this.formatSupplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void sendRawMessage(String rawMessage)
|
|
|
|
{
|
|
|
|
for (Chatter member : this.getMembers())
|
|
|
|
{
|
|
|
|
member.getPlayer().sendMessage(rawMessage);
|
|
|
|
}
|
|
|
|
}
|
2012-08-26 00:54:15 +02:00
|
|
|
}
|