From f91c36b9c70ccdec9985fac6d59486049d03504a Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Fri, 25 Jan 2019 15:52:52 +0100 Subject: [PATCH] Add simple VentureChat integration This integration can filter the recipients of a chat message --- pom.xml | 5 ++ src/com/massivecraft/factions/Factions.java | 2 + .../factions/engine/EngineChat.java | 37 ++++++++++ .../massivecraft/factions/entity/MConf.java | 10 ++- .../venturechat/EngineVentureChat.java | 74 +++++++++++++++++++ .../venturechat/IntegrationVentureChat.java | 29 ++++++++ 6 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 src/com/massivecraft/factions/integration/venturechat/EngineVentureChat.java create mode 100644 src/com/massivecraft/factions/integration/venturechat/IntegrationVentureChat.java diff --git a/pom.xml b/pom.xml index 7d0246fa..54063f6f 100644 --- a/pom.xml +++ b/pom.xml @@ -64,6 +64,11 @@ me.clip placeholderapi + + + mineverse.Aust1n46.chat + MineverseChat + diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index e1826d3e..764c9ea1 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -63,6 +63,7 @@ import com.massivecraft.factions.integration.V18.IntegrationV18; import com.massivecraft.factions.integration.V19.IntegrationV19; import com.massivecraft.factions.integration.lwc.IntegrationLwc; import com.massivecraft.factions.integration.placeholderapi.IntegrationPlaceholderAPI; +import com.massivecraft.factions.integration.venturechat.IntegrationVentureChat; import com.massivecraft.factions.integration.worldguard.IntegrationWorldGuard; import com.massivecraft.factions.mixin.PowerMixin; import com.massivecraft.factions.task.TaskEconLandReward; @@ -180,6 +181,7 @@ public class Factions extends MassivePlugin { return MUtil.list( IntegrationPlaceholderAPI.class, + IntegrationVentureChat.class, IntegrationV18.class, IntegrationV19.class, IntegrationLwc.class, diff --git a/src/com/massivecraft/factions/engine/EngineChat.java b/src/com/massivecraft/factions/engine/EngineChat.java index dcb3ebf7..59d3baf4 100644 --- a/src/com/massivecraft/factions/engine/EngineChat.java +++ b/src/com/massivecraft/factions/engine/EngineChat.java @@ -1,8 +1,12 @@ package com.massivecraft.factions.engine; import com.massivecraft.factions.Factions; +import com.massivecraft.factions.Rel; +import com.massivecraft.factions.RelationParticipator; import com.massivecraft.factions.chat.ChatFormatter; +import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.MConf; +import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.massivecore.Engine; import com.massivecraft.massivecore.event.EventMassiveCorePlayerToRecipientChat; import com.massivecraft.massivecore.util.MUtil; @@ -15,6 +19,9 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.plugin.EventExecutor; +import java.util.Iterator; +import java.util.function.Predicate; + public class EngineChat extends Engine { // -------------------------------------------- // @@ -138,5 +145,35 @@ public class EngineChat extends Engine format = ChatFormatter.format(format, event.getSender(), event.getRecipient()); event.setFormat(format); } + + // -------------------------------------------- // + // FILTER CHAT CHANNEL + // -------------------------------------------- // + + public static Predicate getPredicateIsInFaction(RelationParticipator participator) + { + return player -> MPlayer.get(player).getRelationTo(participator).isAtLeast(Rel.FACTION); + } + + public static Predicate getPredicateIsAlly(RelationParticipator participator) + { + return player -> MPlayer.get(player).getFaction().getRelationTo(participator).isAtLeast(Rel.ALLY); + } + + public static void filterToPredicate(AsyncPlayerChatEvent event, Predicate predicate) + { + Player player = event.getPlayer(); + MPlayer mplayer = MPlayer.get(player); + Faction faction = mplayer.getFaction(); + + // Get and filter recipients + for (Iterator it = event.getRecipients().iterator(); it.hasNext();) + { + Player recipient = it.next(); + if (predicate.test(recipient)) continue; + + it.remove(); + } + } } diff --git a/src/com/massivecraft/factions/entity/MConf.java b/src/com/massivecraft/factions/entity/MConf.java index 1ec0c2c1..0f6a2909 100644 --- a/src/com/massivecraft/factions/entity/MConf.java +++ b/src/com/massivecraft/factions/entity/MConf.java @@ -553,7 +553,15 @@ public class MConf extends Entity // Enable the WorldGuard check per-world // Specify which worlds the WorldGuard Check can be used in public WorldExceptionSet worldguardCheckWorldsEnabled = new WorldExceptionSet(); - + + // -------------------------------------------- // + // INTEGRATION: VentureChat + // -------------------------------------------- // + + public String ventureChatFactionChannelName = "faction"; + public String ventureChatAllyChannelName = "ally"; + public boolean ventureChatAllowFactionchatBetweenFactionless = false; + // -------------------------------------------- // // INTEGRATION: ECONOMY // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/integration/venturechat/EngineVentureChat.java b/src/com/massivecraft/factions/integration/venturechat/EngineVentureChat.java new file mode 100644 index 00000000..1d3f139c --- /dev/null +++ b/src/com/massivecraft/factions/integration/venturechat/EngineVentureChat.java @@ -0,0 +1,74 @@ +package com.massivecraft.factions.integration.venturechat; + +import com.massivecraft.factions.engine.EngineChat; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.MConf; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.massivecore.Engine; +import kangarko.chatcontrol.ChatControl; +import mineverse.Aust1n46.chat.api.MineverseChatAPI; +import mineverse.Aust1n46.chat.api.MineverseChatPlayer; +import mineverse.Aust1n46.chat.channel.ChatChannel; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.player.AsyncPlayerChatEvent; + +import java.util.function.Predicate; + +public class EngineVentureChat extends Engine +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static EngineVentureChat i = new EngineVentureChat(); + public static EngineVentureChat get() { return i; } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void setActiveInner(boolean active) + { + + } + + // -------------------------------------------- // + // LISTENER + // -------------------------------------------- // + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void filterFaction(AsyncPlayerChatEvent event) + { + // Get player + Player player = event.getPlayer(); + MineverseChatPlayer chatPlayer = MineverseChatAPI.getMineverseChatPlayer(player); + ChatChannel channel = chatPlayer.getCurrentChannel(); + String channelName = channel.getName(); + + // If the channel is the Factions channel + boolean factionChat = channelName.equalsIgnoreCase(MConf.get().ventureChatFactionChannelName); + boolean allyChat = channelName.equalsIgnoreCase(MConf.get().ventureChatAllyChannelName); + if (!(factionChat || allyChat)) return; + + MPlayer mplayer = MPlayer.get(player); + Faction faction = mplayer.getFaction(); + + // Wilderness check + if ( ! MConf.get().ventureChatAllowFactionchatBetweenFactionless && faction.isNone()) + { + mplayer.msg("Factionless can't use faction chat."); + event.setCancelled(true); + } + + Predicate predicateChannel = factionChat ? EngineChat.getPredicateIsInFaction(faction) : EngineChat.getPredicateIsAlly(faction); + Predicate isSpy = recipient -> MineverseChatAPI.getMineverseChatPlayer(recipient).isSpy(); + Predicate predicate = isSpy.or(predicateChannel); + + EngineChat.filterToPredicate(event, predicate); + ChatControl + } + +} diff --git a/src/com/massivecraft/factions/integration/venturechat/IntegrationVentureChat.java b/src/com/massivecraft/factions/integration/venturechat/IntegrationVentureChat.java new file mode 100644 index 00000000..b3da2c2f --- /dev/null +++ b/src/com/massivecraft/factions/integration/venturechat/IntegrationVentureChat.java @@ -0,0 +1,29 @@ +package com.massivecraft.factions.integration.venturechat; + +import com.massivecraft.massivecore.Engine; +import com.massivecraft.massivecore.Integration; + +public class IntegrationVentureChat extends Integration +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static IntegrationVentureChat i = new IntegrationVentureChat(); + public static IntegrationVentureChat get() { return i; } + private IntegrationVentureChat() + { + this.setPluginName("VentureChat"); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public Engine getEngine() + { + return EngineVentureChat.get(); + } + +}