diff --git a/lib/Herochat.jar b/lib/Herochat.jar index c1f57739..b4827064 100644 Binary files a/lib/Herochat.jar and b/lib/Herochat.jar differ diff --git a/src/com/massivecraft/factions/Conf.java b/src/com/massivecraft/factions/Conf.java index 261d2a3d..93a3b50b 100644 --- a/src/com/massivecraft/factions/Conf.java +++ b/src/com/massivecraft/factions/Conf.java @@ -68,14 +68,33 @@ public class Conf public static boolean chatSetFormat = false; public static String chatSetFormatTo = "<{faction_relcolor}§l{faction_roleprefix}§r{faction_relcolor}{faction_tag_pr}"+ChatColor.WHITE.toString()+"%s> %s"; public static boolean chatParseTags = true; - public static boolean chatParseTagsColored = true; + public static boolean chatParseTagsColored = false; public static Map chatSingleFormats = new HashMap(); public static transient boolean chatTagHandledByAnotherPlugin = false; // Why do we need this? (Olof asks) public static String chatTagFormat = "%s"+ChatColor.WHITE; // This one is almost deprecated now right? or is it? // Herochat - public static String herochatFactionChannelName = "Faction"; - public static String herochatAllyChannelName = "Allies"; + public static String herochatFactionName = "Faction"; + public static String herochatFactionNick = "F"; + public static String herochatFactionFormat = "{color}[&l{nick}&r{color} &l{faction_roleprefix}&r{color}{faction_title_pr}{sender}{color}] &f{msg}"; + public static ChatColor herochatFactionColor = ChatColor.GREEN; + public static int herochatFactionDistance = 0; + public static boolean herochatFactionIsShortcutAllowed = false; + public static boolean herochatFactionCrossWorld = true; + public static boolean herochatFactionMuted = false; + public static Set herochatFactionWorlds = new HashSet(); + + public static String herochatAlliesName = "Allies"; + public static String herochatAlliesNick = "A"; + public static String herochatAlliesFormat = "{color}[&l{nick}&r&f {faction_relcolor}&l{faction_roleprefix}&r{faction_relcolor}{faction_tag_pr}{sender}{color}] &f{msg}"; + public static ChatColor herochatAlliesColor = ChatColor.DARK_PURPLE; + public static int herochatAlliesDistance = 0; + public static boolean herochatAlliesIsShortcutAllowed = false; + public static boolean herochatAlliesCrossWorld = true; + public static boolean herochatAlliesMuted = false; + public static Set herochatAlliesWorlds = new HashSet(); + + public static String herochatAllyName = "Allies"; public static double autoLeaveAfterDaysOfInactivity = 10.0; public static double autoLeaveRoutineRunsEveryXMinutes = 5.0; diff --git a/src/com/massivecraft/factions/P.java b/src/com/massivecraft/factions/P.java index 68e3b900..ae54a1b9 100644 --- a/src/com/massivecraft/factions/P.java +++ b/src/com/massivecraft/factions/P.java @@ -19,9 +19,9 @@ import com.massivecraft.factions.adapters.LocationTypeAdapter; import com.massivecraft.factions.adapters.RelTypeAdapter; import com.massivecraft.factions.cmd.*; import com.massivecraft.factions.integration.capi.CapiFeatures; +import com.massivecraft.factions.integration.herochat.HerochatFeatures; import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.integration.EssentialsFeatures; -import com.massivecraft.factions.integration.HerochatFeatures; import com.massivecraft.factions.integration.LWCFeatures; import com.massivecraft.factions.integration.SpoutFeatures; import com.massivecraft.factions.integration.Worldguard; diff --git a/src/com/massivecraft/factions/integration/HerochatFeatures.java b/src/com/massivecraft/factions/integration/HerochatFeatures.java deleted file mode 100644 index 6ed01a5a..00000000 --- a/src/com/massivecraft/factions/integration/HerochatFeatures.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.massivecraft.factions.integration; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.plugin.Plugin; - -import com.dthielke.herochat.Channel; -import com.dthielke.herochat.ChannelChatEvent; -import com.massivecraft.factions.Conf; -import com.massivecraft.factions.FPlayer; -import com.massivecraft.factions.FPlayers; -import com.massivecraft.factions.Faction; -import com.massivecraft.factions.P; -import com.massivecraft.factions.struct.Rel; - -public class HerochatFeatures implements Listener -{ - P p; - public HerochatFeatures(P p) - { - this.p = p; - } - - public static void setup() - { - Plugin plug = Bukkit.getServer().getPluginManager().getPlugin("Herochat"); - if (plug != null && plug.getClass().getName().equals("com.dthielke.herochat.Herochat")) - { - P.p.log("Integration with Herochat successful"); - Bukkit.getPluginManager().registerEvents(new HerochatFeatures(P.p), P.p); - } - } - - @EventHandler(priority = EventPriority.LOW) - public void onChannelChatEvent(ChannelChatEvent event) - { - Channel ch = event.getChannel(); - boolean isFactionChat = ch.getName().equals(Conf.herochatFactionChannelName); - boolean isAllyChat = ch.getName().equals(Conf.herochatAllyChannelName); - if ( ! isFactionChat && ! isAllyChat) return; - - // Do common setup - Player sender = event.getSender().getPlayer(); - FPlayer fpsender = FPlayers.i.get(sender); - event.getBukkitEvent().getRecipients().clear(); - if ( ! fpsender.hasFaction()) - { - sender.sendMessage(ChatColor.YELLOW.toString()+"You must join a faction to use the "+ch.getColor().toString()+ch.getName()+ChatColor.YELLOW.toString()+"-channel."); - event.getBukkitEvent().setCancelled(true); - return; - } - - Faction faction = fpsender.getFaction(); - event.getBukkitEvent().getRecipients().addAll(faction.getOnlinePlayers()); - - if (isAllyChat) - { - for (FPlayer fplayer : FPlayers.i.getOnline()) - { - if(faction.getRelationTo(fplayer) == Rel.ALLY) - { - event.getBukkitEvent().getRecipients().add(fplayer.getPlayer()); - } - } - } - } -} diff --git a/src/com/massivecraft/factions/integration/herochat/AlliesChannel.java b/src/com/massivecraft/factions/integration/herochat/AlliesChannel.java new file mode 100644 index 00000000..2e615d27 --- /dev/null +++ b/src/com/massivecraft/factions/integration/herochat/AlliesChannel.java @@ -0,0 +1,42 @@ +package com.massivecraft.factions.integration.herochat; + +import java.util.EnumSet; +import java.util.Set; + +import org.bukkit.ChatColor; + +import com.massivecraft.factions.Conf; +import com.massivecraft.factions.struct.Rel; + +public class AlliesChannel extends FactionsChannelAbstract +{ + public static final Set targetRelations = EnumSet.of(Rel.MEMBER, Rel.ALLY); + @Override public Set getTargetRelations() { return targetRelations; } + + @Override public String getName() { return Conf.herochatAlliesName; } + + @Override public String getNick() { return Conf.herochatAlliesNick; } + @Override public void setNick(String nick) { Conf.herochatAlliesNick = nick; } + + @Override public String getFormat() { return Conf.herochatAlliesFormat; } + @Override public void setFormat(String format) { Conf.herochatAlliesFormat = format; } + + @Override public ChatColor getColor() { return Conf.herochatAlliesColor; } + @Override public void setColor(ChatColor color) { Conf.herochatAlliesColor = color; } + + @Override public int getDistance() { return Conf.herochatAlliesDistance; } + @Override public void setDistance(int distance) { Conf.herochatAlliesDistance = distance; } + + @Override public void addWorld(String world) { Conf.herochatAlliesWorlds.add(world); } + @Override public Set getWorlds() { return Conf.herochatAlliesWorlds; } + @Override public void setWorlds(Set worlds) { Conf.herochatAlliesWorlds = worlds; } + + @Override public boolean isShortcutAllowed() { return Conf.herochatAlliesIsShortcutAllowed; } + @Override public void setShortcutAllowed(boolean shortcutAllowed) { Conf.herochatAlliesIsShortcutAllowed = shortcutAllowed; } + + @Override public boolean isCrossWorld() { return Conf.herochatAlliesCrossWorld; } + @Override public void setCrossWorld(boolean crossWorld) { Conf.herochatAlliesCrossWorld = crossWorld; } + + @Override public boolean isMuted() { return Conf.herochatAlliesMuted; } + @Override public void setMuted(boolean value) { Conf.herochatAlliesMuted = value; } +} diff --git a/src/com/massivecraft/factions/integration/herochat/FactionChannel.java b/src/com/massivecraft/factions/integration/herochat/FactionChannel.java new file mode 100644 index 00000000..62a2d653 --- /dev/null +++ b/src/com/massivecraft/factions/integration/herochat/FactionChannel.java @@ -0,0 +1,43 @@ +package com.massivecraft.factions.integration.herochat; + +import java.util.EnumSet; +import java.util.HashSet; +import java.util.Set; + +import org.bukkit.ChatColor; + +import com.massivecraft.factions.Conf; +import com.massivecraft.factions.struct.Rel; + +public class FactionChannel extends FactionsChannelAbstract +{ + public static final Set targetRelations = EnumSet.of(Rel.MEMBER); + @Override public Set getTargetRelations() { return targetRelations; } + + @Override public String getName() { return Conf.herochatFactionName; } + + @Override public String getNick() { return Conf.herochatFactionNick; } + @Override public void setNick(String nick) { Conf.herochatFactionNick = nick; } + + @Override public String getFormat() { return Conf.herochatFactionFormat; } + @Override public void setFormat(String format) { Conf.herochatFactionFormat = format; } + + @Override public ChatColor getColor() { return Conf.herochatFactionColor; } + @Override public void setColor(ChatColor color) { Conf.herochatFactionColor = color; } + + @Override public int getDistance() { return Conf.herochatFactionDistance; } + @Override public void setDistance(int distance) { Conf.herochatFactionDistance = distance; } + + @Override public void addWorld(String world) { Conf.herochatFactionWorlds.add(world); } + @Override public Set getWorlds() { return new HashSet(Conf.herochatFactionWorlds); } + @Override public void setWorlds(Set worlds) { Conf.herochatFactionWorlds = worlds; } + + @Override public boolean isShortcutAllowed() { return Conf.herochatFactionIsShortcutAllowed; } + @Override public void setShortcutAllowed(boolean shortcutAllowed) { Conf.herochatFactionIsShortcutAllowed = shortcutAllowed; } + + @Override public boolean isCrossWorld() { return Conf.herochatFactionCrossWorld; } + @Override public void setCrossWorld(boolean crossWorld) { Conf.herochatFactionCrossWorld = crossWorld; } + + @Override public boolean isMuted() { return Conf.herochatFactionMuted; } + @Override public void setMuted(boolean value) { Conf.herochatFactionMuted = value; } +} diff --git a/src/com/massivecraft/factions/integration/herochat/FactionsChannelAbstract.java b/src/com/massivecraft/factions/integration/herochat/FactionsChannelAbstract.java new file mode 100644 index 00000000..0730dd5e --- /dev/null +++ b/src/com/massivecraft/factions/integration/herochat/FactionsChannelAbstract.java @@ -0,0 +1,418 @@ +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; +import com.dthielke.herochat.Chatter; +import com.dthielke.herochat.Herochat; +import com.dthielke.herochat.MessageFormatSupplier; +import com.dthielke.herochat.MessageNotFoundException; +import com.dthielke.herochat.util.Messaging; +import com.massivecraft.factions.FPlayer; +import com.massivecraft.factions.FPlayers; +import com.massivecraft.factions.Faction; +import com.massivecraft.factions.struct.Rel; + +public abstract class FactionsChannelAbstract implements Channel +{ + private static final Pattern msgPattern = Pattern.compile("(.*)<(.*)%1\\$s(.*)> %2\\$s"); + private final ChannelStorage storage = Herochat.getChannelManager().getStorage(); + private final MessageFormatSupplier formatSupplier = Herochat.getChannelManager(); + + public FactionsChannelAbstract() + { + + } + + + + @Override + public boolean addMember(Chatter chatter, boolean announce, boolean flagUpdate) + { + if (chatter.hasChannel(this)) return false; + 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); + return true; + } + + + @Override + public Set getMembers() + { + Set ret = new HashSet(); + for (Chatter chatter : Herochat.getChatterManager().getChatters()) + { + if(chatter.hasChannel(this)) ret.add(chatter); + } + return ret; + } + + @Override + public void announce(String message) + { + message = applyFormat(this.formatSupplier.getAnnounceFormat(), "").replace("%2$s", message); + for (Chatter member : this.getMembers()) + { + member.getPlayer().sendMessage(message); + } + Herochat.logChat(ChatColor.stripColor(message)); + } + + @Override + public String applyFormat(String format, String originalFormat) + { + format = format.replace("{default}", this.formatSupplier.getStandardFormat()); + 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); + if ((matcher.matches()) && (matcher.groupCount() == 3)) + { + format = format.replace("{sender}", matcher.group(1) + matcher.group(2) + "%1$s" + matcher.group(3)); + } + else + { + format = format.replace("{sender}", "%1$s"); + } + + format = format.replaceAll("(?i)&([a-fklmno0-9])", "§$1"); + return format; + } + + @Override + public String applyFormat(String format, String originalFormat, Player sender) + { + format = applyFormat(format, originalFormat); + 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); + String suffix = chat.getPlayerSuffix(sender); + String group = chat.getPrimaryGroup(sender); + String groupPrefix = group == null ? "" : chat.getGroupPrefix(sender.getWorld(), group); + String groupSuffix = group == null ? "" : chat.getGroupSuffix(sender.getWorld(), group); + 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("%", "%%")); + } + catch (UnsupportedOperationException ignored) {} + } + 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) + { + message = applyFormat(this.formatSupplier.getEmoteFormat(), "").replace("%2$s", message); + Set recipients = new HashSet(); + for (Chatter member : this.getMembers()) + { + recipients.add(member.getPlayer()); + } + + trimRecipients(recipients, sender); + + final Player player = sender.getPlayer(); + + if (!isMessageHeard(recipients, sender)) + { + Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Herochat.getPlugin(), new Runnable() + { + public void run() + { + try + { + Messaging.send(player, Herochat.getMessage("channel_alone")); + } + catch (MessageNotFoundException e) + { + Herochat.severe("Messages.properties is missing: channel_alone"); + } + } + }, 1L); + } + else + { + for (Player p : recipients) + { + p.sendMessage(message); + } + } + } + + @Override + public boolean isMuted(String name) + { + if (this.isMuted()) return true; + return this.getMutes().contains(name.toLowerCase()); + } + + + public abstract Set getTargetRelations(); + + public Set getRecipients(Player sender) + { + Set ret = new HashSet(); + + FPlayer fpsender = FPlayers.i.get(sender); + Faction faction = fpsender.getFaction(); + ret.addAll(faction.getOnlinePlayers()); + + for (FPlayer fplayer : FPlayers.i.getOnline()) + { + if(this.getTargetRelations().contains(faction.getRelationTo(fplayer))) + { + ret.add(fplayer.getPlayer()); + } + } + + return ret; + } + + + @Override + public void processChat(ChannelChatEvent event) + { + final Player player = event.getSender().getPlayer(); + + String format = applyFormat(event.getFormat(), event.getBukkitFormat(), player); + + Chatter sender = Herochat.getChatterManager().getChatter(player); + Set recipients = this.getRecipients(player); + + trimRecipients(recipients, sender); + String msg = String.format(format, player.getDisplayName(), event.getMessage()); + if (!isMessageHeard(recipients, sender)) + { + Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Herochat.getPlugin(), new Runnable() + { + public void run() + { + try + { + Messaging.send(player, Herochat.getMessage("channel_alone")); + } + catch (MessageNotFoundException e) + { + Herochat.severe("Messages.properties is missing: channel_alone"); + } + } + }, 1L); + } + + for (Player recipient : recipients) + { + recipient.sendMessage(msg); + } + + Herochat.logChat(msg); + } + + /*@Override + public void processChat(ChannelChatEvent event) + { + final Player player = event.getSender().getPlayer(); + + String format = applyFormat(event.getFormat(), event.getBukkitFormat(), player); + + Chatter sender = Herochat.getChatterManager().getChatter(player); + Set recipients = new HashSet(Arrays.asList(Bukkit.getOnlinePlayers())); + + trimRecipients(recipients, sender); + if (!isMessageHeard(recipients, sender)) + { + Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Herochat.getPlugin(), new Runnable() + { + public void run() + { + try + { + Messaging.send(player, Herochat.getMessage("channel_alone")); + } + catch (MessageNotFoundException e) + { + Herochat.severe("Messages.properties is missing: channel_alone"); + } + } + }, 1L); + } + + FPlayer fplayer = FPlayers.i.get(player); + + String formatWithoutColor = FactionsChatListener.parseTags(format, player, fplayer); + + //String msg = String.format(format, player.getDisplayName(), event.getMessage()); + + + for (Player recipient : recipients) + { + String finalFormat; + if ( ! Conf.chatParseTags || Conf.chatTagHandledByAnotherPlugin) + { + finalFormat = format; + } + else if (! Conf.chatParseTagsColored) + { + finalFormat = formatWithoutColor; + } + else + { + FPlayer frecipient = FPlayers.i.get(recipient); + finalFormat = FactionsChatListener.parseTags(format, player, fplayer, recipient, frecipient); + } + String msg = String.format(finalFormat, player.getDisplayName(), event.getMessage()); + recipient.sendMessage(msg); + } + + Herochat.logChat(String.format(formatWithoutColor, player.getDisplayName(), event.getMessage())); + }*/ + + public boolean isMessageHeard(Set 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 recipients, Chatter sender) + { + World world = sender.getPlayer().getWorld(); + + Set members = this.getMembers(); + Iterator 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 getBans() { return Collections.emptySet(); } + @Override public Set getModerators() { return Collections.emptySet(); } + @Override public Set 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 bans) {} + @Override public void setModerator(String name, boolean moderator) {} + @Override public void setModerators(Set moderators) { } + @Override public void setMuted(String name, boolean muted) {} + @Override public void setMutes(Set mutes) {} +} diff --git a/src/com/massivecraft/factions/integration/herochat/HerochatFeatures.java b/src/com/massivecraft/factions/integration/herochat/HerochatFeatures.java new file mode 100644 index 00000000..f9b3212d --- /dev/null +++ b/src/com/massivecraft/factions/integration/herochat/HerochatFeatures.java @@ -0,0 +1,110 @@ +package com.massivecraft.factions.integration.herochat; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.plugin.Plugin; + +import com.dthielke.herochat.ChannelChatEvent; +import com.dthielke.herochat.Herochat; +import com.massivecraft.factions.Conf; +import com.massivecraft.factions.FPlayer; +import com.massivecraft.factions.FPlayers; +import com.massivecraft.factions.P; +import com.massivecraft.factions.listeners.FactionsChatListener; + +public class HerochatFeatures implements Listener +{ + P p; + public HerochatFeatures(P p) + { + this.p = p; + } + + public static void setup() + { + Plugin plug = Bukkit.getServer().getPluginManager().getPlugin("Herochat"); + if (plug == null) return; + if (!plug.getClass().getName().equals("com.dthielke.herochat.Herochat")) return; + Bukkit.getPluginManager().registerEvents(new HerochatFeatures(P.p), P.p); + Herochat.getChannelManager().addChannel(new FactionChannel()); + Herochat.getChannelManager().addChannel(new AlliesChannel()); + P.p.log("Integration with Herochat successful"); + } + + /** + * Due to limitations in the new version of Herochat we can not offer relation colored tags. + */ + @EventHandler(priority = EventPriority.NORMAL) + public void onChannelChatEvent(ChannelChatEvent event) + { + // Should we even parse? + if ( ! Conf.chatParseTags) return; + if (Conf.chatTagHandledByAnotherPlugin) return; + + Player from = event.getSender().getPlayer(); + FPlayer fpfrom = FPlayers.i.get(from); + String format = event.getFormat(); + + format = format.replaceAll("&r", "§r"); + + String formatWithoutColor = FactionsChatListener.parseTags(format, from, fpfrom); + + event.setFormat(formatWithoutColor); + } + + /* + + public static Chatter getChatter(Player player) + { + ChatterManager chatterManager = Herochat.getChatterManager(); + if (!chatterManager.hasChatter(player)) chatterManager.addChatter(player); + Chatter ret = chatterManager.getChatter(player); + if (ret == null) throw new RuntimeException("Chatter (" + player.getName() + ") not found."); + return ret; + } + + @EventHandler(priority = EventPriority.LOW) + public void onAsyncPlayerChatEvent(AsyncPlayerChatEvent event) + { + Player player = event.getPlayer(); + Chatter sender = getChatter(player); + Channel ch = sender.getActiveChannel(); + if (ch == null) return; + + boolean isFactionChat = ch.getName().equals(Conf.herochatFactionChannelName); + boolean isAllyChat = ch.getName().equals(Conf.herochatAllyChannelName); + if ( ! isFactionChat && ! isAllyChat) return; + + // Do common setup + Player sender = event.getSender().getPlayer(); + FPlayer fpsender = FPlayers.i.get(sender); + + event.getRecipients().clear(); + + if ( ! fpsender.hasFaction()) + { + sender.sendMessage(ChatColor.YELLOW.toString()+"You must join a faction to use the "+ch.getColor().toString()+ch.getName()+ChatColor.YELLOW.toString()+"-channel."); + event.getBukkitEvent().setCancelled(true); + return; + } + + Faction faction = fpsender.getFaction(); + event.getBukkitEvent().getRecipients().addAll(faction.getOnlinePlayers()); + + if (isAllyChat) + { + for (FPlayer fplayer : FPlayers.i.getOnline()) + { + if(faction.getRelationTo(fplayer) == Rel.ALLY) + { + event.getBukkitEvent().getRecipients().add(fplayer.getPlayer()); + } + } + } + } + + */ +} diff --git a/src/com/massivecraft/factions/listeners/FactionsChatListener.java b/src/com/massivecraft/factions/listeners/FactionsChatListener.java index aca5fc3c..dd8da2a4 100644 --- a/src/com/massivecraft/factions/listeners/FactionsChatListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsChatListener.java @@ -80,7 +80,7 @@ public class FactionsChatListener implements Listener * The side effect is that other plugins at EventPriority.HIGHEST may experience the event as cancelled. */ @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled=true) - public synchronized void onPlayerChat(AsyncPlayerChatEvent event) + public void onPlayerChat(AsyncPlayerChatEvent event) { // Should we even parse? if ( ! Conf.chatParseTags) return; @@ -126,7 +126,7 @@ public class FactionsChatListener implements Listener * This is some nasty woodo - I know :/ * I should make a pull request to Bukkit and CraftBukkit to support this feature natively */ - public static synchronized void callEventAtMonitorOnly(Event event) + public static void callEventAtMonitorOnly(Event event) { synchronized(Bukkit.getPluginManager()) { diff --git a/src/com/massivecraft/factions/zcore/util/DiscUtil.java b/src/com/massivecraft/factions/zcore/util/DiscUtil.java index 43e4c244..024e9864 100644 --- a/src/com/massivecraft/factions/zcore/util/DiscUtil.java +++ b/src/com/massivecraft/factions/zcore/util/DiscUtil.java @@ -4,31 +4,48 @@ import java.io.*; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; +import java.nio.file.Files; public class DiscUtil { + // -------------------------------------------- // + // CONSTANTS + // -------------------------------------------- // + + private final static String UTF8 = "UTF-8"; + + // -------------------------------------------- // + // BYTE + // -------------------------------------------- // + + public static byte[] readBytes(File file) throws IOException + { + return Files.readAllBytes(file.toPath()); + } + + public static void writeBytes(File file, byte[] bytes) throws IOException + { + Files.write(file.toPath(), bytes); + } + + // -------------------------------------------- // + // STRING + // -------------------------------------------- // + public static void write(File file, String content) throws IOException { - BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF8")); - out.write(content); - out.close(); + writeBytes(file, utf8(content)); } public static String read(File file) throws IOException { - BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); - String ret = new String(new byte[0], "UTF-8"); - - String line; - while ((line = in.readLine()) != null) - { - ret += line; - } - - in.close(); - return ret; + return utf8(readBytes(file)); } + // -------------------------------------------- // + // CATCH + // -------------------------------------------- // + public static boolean writeCatch(File file, String content) { try @@ -54,6 +71,10 @@ public class DiscUtil } } + // -------------------------------------------- // + // DOWNLOAD + // -------------------------------------------- // + public static boolean downloadUrl(String urlstring, File file) { try @@ -61,8 +82,8 @@ public class DiscUtil URL url = new URL(urlstring); ReadableByteChannel rbc = Channels.newChannel(url.openStream()); FileOutputStream fos = new FileOutputStream(file); - fos.getChannel().transferFrom(rbc, 0, 1 << 24); - return true; + fos.getChannel().transferFrom(rbc, 0, 1 << 24); + return true; } catch (Exception e) { @@ -75,4 +96,53 @@ public class DiscUtil { return downloadUrl(urlstring, new File(filename)); } -} + + // -------------------------------------------- // + // FILE DELETION + // -------------------------------------------- // + + public static boolean deleteRecursive(File path) throws FileNotFoundException + { + if ( ! path.exists()) throw new FileNotFoundException(path.getAbsolutePath()); + boolean ret = true; + if (path.isDirectory()) + { + for (File f : path.listFiles()) + { + ret = ret && deleteRecursive(f); + } + } + return ret && path.delete(); + } + + // -------------------------------------------- // + // UTF8 ENCODE AND DECODE + // -------------------------------------------- // + + public static byte[] utf8(String string) + { + try + { + return string.getBytes(UTF8); + } + catch (UnsupportedEncodingException e) + { + e.printStackTrace(); + return null; + } + } + + public static String utf8(byte[] bytes) + { + try + { + return new String(bytes, UTF8); + } + catch (UnsupportedEncodingException e) + { + e.printStackTrace(); + return null; + } + } + +} \ No newline at end of file