Initial commit

This commit is contained in:
Draycia committed 2020-06-13 00:29:39 -07:00
commit 3b11513586
11 files changed
+1203

No files matched your search

@@ -0,0 +1,189 @@
package net.draycia.simplechat;
import co.aikar.commands.BukkitCommandManager;
import co.aikar.commands.ConditionFailedException;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.channels.SimpleChatChannel;
import net.draycia.simplechat.commands.ChannelCommand;
import net.draycia.simplechat.listeners.PlayerChatListener;
import net.kyori.text.format.TextColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
public final class SimpleChat extends JavaPlugin {
private ArrayList<ChatChannel> channels = new ArrayList<>();
private HashMap<UUID, ChatChannel> userChannels = new HashMap<>();
private HashMap<UUID, ArrayList<String>> userToggles = new HashMap<>();
@Override
public void onEnable() {
saveDefaultConfig();
BukkitCommandManager manager = new BukkitCommandManager(this);
manager.getCommandCompletions().registerCompletion("chatchannel", (context) -> {
ArrayList<String> completions = new ArrayList<>();
for (ChatChannel chatChannel : channels) {
if (chatChannel.canPlayerUse(context.getPlayer())) {
completions.add(chatChannel.getName());
}
}
return completions;
});
manager.getCommandContexts().registerContext(ChatChannel.class, (context) -> {
String name = context.popFirstArg();
for (ChatChannel chatChannel : channels) {
if (chatChannel.getName().equalsIgnoreCase(name)) {
return chatChannel;
}
}
return null;
});
manager.getCommandConditions().addCondition(ChatChannel.class,"canuse", (context, execution, value) -> {
if (!value.canPlayerUse(context.getIssuer().getPlayer())) {
throw new ConditionFailedException("You cannot use that channel!");
}
});
for (String key : getConfig().getConfigurationSection("channels").getKeys(false)) {
SimpleChatChannel.Builder builder = SimpleChatChannel.builder(key);
ConfigurationSection section = getConfig().getConfigurationSection("channels").getConfigurationSection(key);
if (section.contains("id")) {
builder.setId(section.getLong("id"));
}
if (section.contains("format")) {
builder.setFormat(section.getString("format"));
}
if (section.contains("format-staff")) {
builder.setStaffFormat(section.getString("format-staff"));
}
if (section.contains("webhook")) {
builder.setWebhook(section.getString("webhook"));
}
if (section.contains("color")) {
builder.setColor(TextColor.valueOf(section.getString("color").toUpperCase()));
}
if (section.contains("ignorable")) {
builder.setIgnorable(section.getBoolean("ignorable"));
}
if (section.contains("is-town-chat")) {
builder.setIsTownChat(section.getBoolean("is-town-chat"));
}
if (section.contains("is-nation-chat")) {
builder.setIsNationChat(section.getBoolean("is-nation-chat"));
}
if (section.contains("is-alliance-chat")) {
builder.setIsAllianceChat(section.getBoolean("is-alliance-chat"));
}
if (section.contains("is-party-chat")) {
builder.setIsAllianceChat(section.getBoolean("is-party-chat"));
}
if (section.contains("default")) {
builder.setIsDefault(section.getBoolean("default"));
}
ChatChannel channel = builder.build(this);
// TODO: register command for each channel
channels.add(channel);
}
boolean hasTownChat = channels.stream().anyMatch(ChatChannel::isTownChat);
boolean hasNationChat = channels.stream().anyMatch(ChatChannel::isNationChat);
boolean hasAllianceChat = channels.stream().anyMatch(ChatChannel::isAllianceChat);
boolean hasPartyChat = channels.stream().anyMatch(ChatChannel::isPartyChat);
if (!hasTownChat) {
getLogger().info("Towny installed but no Town channel is setup!");
}
if (!hasNationChat) {
getLogger().info("Towny installed but no Nation channel is setup!");
}
if (!hasAllianceChat) {
getLogger().info("Towny installed but no Alliance channel is setup!");
}
if (!hasPartyChat) {
getLogger().info("mcMMO installed but no Party channel is setup!");
}
manager.registerCommand(new ChannelCommand(this));
setupListeners();
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
private void setupListeners() {
getServer().getPluginManager().registerEvents(new PlayerChatListener(this), this);
}
public boolean playerHasChannelMuted(Player player, ChatChannel chatChannel) {
return getUserChannelMutes(player).contains(chatChannel.getName());
}
public boolean togglePlayerChannelMute(Player player, ChatChannel chatChannel) {
ArrayList<String> toggles = getUserChannelMutes(player);
if (toggles.contains(chatChannel.getName())) {
toggles.remove(chatChannel.getName());
return false;
} else {
toggles.add(chatChannel.getName());
return true;
}
}
public ArrayList<String> getUserChannelMutes(Player player) {
return userToggles.computeIfAbsent(player.getUniqueId(), (uuid) -> new ArrayList<>());
}
public ChatChannel getPlayerChannel(Player player) {
return userChannels.computeIfAbsent(player.getUniqueId(), (uuid) -> getDefaultChannel());
}
public void setPlayerChannel(Player player, ChatChannel chatChannel) {
userChannels.put(player.getUniqueId(), chatChannel);
}
public ChatChannel getDefaultChannel() {
for (ChatChannel channel : channels) {
if (channel.isDefault()) {
return channel;
}
}
return null;
}
}
@@ -0,0 +1,26 @@
package net.draycia.simplechat
import net.kyori.text.Component
import net.kyori.text.TextComponent
import net.kyori.text.event.ClickEvent
import net.kyori.text.event.HoverEvent
import net.kyori.text.event.HoverEvent.Action.SHOW_TEXT
fun Component.removeEscape(escape: Char = '\\'): Component {
if (this is TextComponent) {
return content(content().filterNot { it == escape })
.children(children().map { it.removeEscape(escape) })
.clickEvent(clickEvent()?.run {
ClickEvent.of(action(), value().filterNot { it == escape })
})
.hoverEvent(hoverEvent()?.run {
if (action() == SHOW_TEXT) {
HoverEvent.of(action(), value().removeEscape(escape))
} else {
this
}
})
}
return this
}
@@ -0,0 +1,90 @@
package net.draycia.simplechat.channels;
import net.kyori.text.format.TextColor;
import org.bukkit.entity.Player;
public abstract class ChatChannel {
/**
* @return The color that represents this channel. Optionally used in formatting.
*/
public abstract TextColor getColor();
/**
* @return The ID of the discord channel to read messages from for chat bridging.
*/
public abstract long getChannelId();
/**
* @return The MiniMessage styled format used by non staff in this channel.
*/
public abstract String getFormat();
/**
* @return The MiniMessage styled format used by staff in this channel.
*/
public abstract String getStaffFormat();
/**
* @return The url of the webhook used to send messages to discord for chat bridging.
*/
public abstract String getWebhook();
/**
* @return If this is the default (typically Global) channel players use when they're in no other channel.
*/
public abstract boolean isDefault();
/**
* @return If this channel can be toggled off and if players can ignore player messages in this channel.
*/
public abstract boolean isIgnorable();
/**
* @return The name of this channel.
*/
public abstract String getName();
/**
* @return The distance other players must be within to the sender to see messages in this channel.
*/
public abstract double getDistance();
/**
* @return The message to be sent to the player when switching to this channel.
*/
public abstract String getSwitchMessage();
/**
* @return If this channel is Towny's town chat.
*/
public abstract boolean isTownChat();
/**
* @return If this channel is Towny's nation chat.
*/
public abstract boolean isNationChat();
/**
* @return If this channel is a Towny alliance chat.
*/
public abstract boolean isAllianceChat();
/**
* @return If this channel is mcMMO's party chat.
*/
public abstract boolean isPartyChat();
/**
* @return If the player can use this channel.
*/
public abstract boolean canPlayerUse(Player player);
/**
* Parses the specified message, calls a {@link net.draycia.simplechat.events.ChannelChatEvent}, and sends the message to everyone who can view this channel.
* @param player The player who is saying the message.
* @param message The message to be sent.
*/
public abstract void sendMessage(Player player, String message);
}
@@ -0,0 +1,430 @@
package net.draycia.simplechat.channels;
import com.earth2me.essentials.Essentials;
import com.gmail.nossr50.api.PartyAPI;
import com.gmail.nossr50.mcMMO;
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import me.clip.placeholderapi.PlaceholderAPI;
import me.minidigger.minimessage.text.MiniMessageParser;
import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.TextUtilsKt;
import net.draycia.simplechat.events.ChannelChatEvent;
import net.kyori.text.Component;
import net.kyori.text.TextComponent;
import net.kyori.text.adapter.bukkit.TextAdapter;
import net.kyori.text.format.TextColor;
import net.kyori.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public class SimpleChatChannel extends ChatChannel {
private TextColor color;
private long id;
private String format;
private String staffFormat;
private String webhook;
private boolean isDefault;
private boolean ignorable;
private String name;
private double distance;
private String switchMessage;
// Plugin hooks
private boolean isTownChat;
private boolean isNationChat;
private boolean isAllianceChat;
private boolean isPartyChat;
private SimpleChat simpleChat;
private SimpleChatChannel() { }
private SimpleChatChannel(TextColor color, long id, String format, String staffFormat, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, boolean isTownChat, boolean isNationChat, boolean isAllianceChat, boolean isPartyChat, SimpleChat simpleChat) {
this.color = color;
this.id = id;
this.format = format;
this.staffFormat = staffFormat;
this.webhook = webhook;
this.isDefault = isDefault;
this.ignorable = ignorable;
this.name = name;
this.distance = distance;
this.switchMessage = switchMessage;
this.isTownChat = isTownChat;
this.isNationChat = isNationChat;
this.isAllianceChat = isAllianceChat;
this.isPartyChat = isPartyChat;
this.simpleChat = simpleChat;
}
@Override
public boolean canPlayerUse(Player player) {
if (player.hasPermission("simplechat.channels." + getName())) {
return true;
}
// TODO: other use conditions
return false;
}
@Override
public void sendMessage(Player player, String message) {
message = MiniMessageParser.escapeTokens(message);
String messageFormat;
if (player.hasPermission("simplechat.staff")) {
messageFormat = getStaffFormat();
} else {
messageFormat = getFormat();
}
ChannelChatEvent event = new ChannelChatEvent(player, this, messageFormat, message);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}
messageFormat = PlaceholderAPI.setPlaceholders(player, event.getFormat());
messageFormat = MiniMessageParser.handlePlaceholders(messageFormat, "color", "<" + color.toString() + ">");
messageFormat = MiniMessageParser.handlePlaceholders(messageFormat, "message", event.getMessage());
Component formattedMessage = TextUtilsKt.removeEscape(MiniMessageParser.parseFormat(messageFormat), '\\');
if (isTownChat()) {
try {
Resident resident = TownyAPI.getInstance().getDataSource().getResident(player.getName());
if (resident.hasTown()) {
Town town = resident.getTown();
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
if (!town.hasResident(onlinePlayer.getName())) {
return;
}
if (!canUserSeeMessage(player, onlinePlayer)) {
return;
}
TextAdapter.sendMessage(onlinePlayer, formattedMessage);
}
} else {
// TODO: send "you don't belong to a town" message
return;
}
} catch (NotRegisteredException e) {
e.printStackTrace();
return;
}
} else if (isNationChat()) {
try {
Resident resident = TownyAPI.getInstance().getDataSource().getResident(player.getName());
if (resident.hasNation()) {
Nation nation = resident.getTown().getNation();
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
if (!nation.hasResident(onlinePlayer.getName())) {
return;
}
if (!canUserSeeMessage(player, onlinePlayer)) {
return;
}
TextAdapter.sendMessage(onlinePlayer, formattedMessage);
}
} else {
// TODO: send "you don't belong to a nation" message
return;
}
} catch (NotRegisteredException e) {
e.printStackTrace();
return;
}
} else if (isAllianceChat()) {
try {
Resident resident = TownyAPI.getInstance().getDataSource().getResident(player.getName());
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
Resident target = TownyAPI.getInstance().getDataSource().getResident(onlinePlayer.getName());
if (!resident.isAlliedWith(target)) {
return;
}
if (!canUserSeeMessage(player, onlinePlayer)) {
return;
}
TextAdapter.sendMessage(onlinePlayer, formattedMessage);
}
} catch (NotRegisteredException e) {
e.printStackTrace();
return;
}
} else if (isPartyChat()) {
if (PartyAPI.inParty(player)) {
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
if (!PartyAPI.inSameParty(player, onlinePlayer)) {
return;
}
if (!canUserSeeMessage(player, onlinePlayer)) {
return;
}
TextAdapter.sendMessage(onlinePlayer, formattedMessage);
}
} else {
// TODO: send "you don't belong to a party" message
return;
}
} else {
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
if (onlinePlayer.hasPermission("simplechat.see." + getName().toLowerCase())) {
if (!canUserSeeMessage(player, onlinePlayer)) {
return;
}
TextAdapter.sendMessage(onlinePlayer, formattedMessage);
}
}
}
System.out.println(LegacyComponentSerializer.INSTANCE.serialize(formattedMessage));
}
private boolean isUserIgnoringUser(Player user, Player target) {
Plugin essentials = Bukkit.getServer().getPluginManager().getPlugin("Essentials");
if (essentials != null) {
Essentials essx = (Essentials)essentials;
return essx.getUser(user).isIgnoredPlayer(essx.getUser(target));
}
return false;
}
public boolean canUserSeeMessage(Player sender, Player target) {
if (getDistance() > 0 && target.getLocation().distance(sender.getLocation()) > getDistance()) {
return false;
}
if (isIgnorable()) {
if (isUserIgnoringUser(sender, target)) {
return false;
}
// if (!simpleChat.hasToggledOn(player, this)) {
//
// }
}
return true;
}
@Override
public TextColor getColor() {
return color;
}
@Override
public long getChannelId() {
return id;
}
@Override
public String getFormat() {
return format;
}
@Override
public String getStaffFormat() {
if (staffFormat == null) {
return format;
}
return staffFormat;
}
@Override
public String getWebhook() {
return webhook;
}
@Override
public boolean isDefault() {
return isDefault;
}
@Override
public boolean isIgnorable() {
return ignorable;
}
@Override
public String getName() {
return name;
}
@Override
public double getDistance() {
return distance;
}
@Override
public String getSwitchMessage() {
return switchMessage;
}
@Override
public boolean isTownChat() {
return isTownChat;
}
@Override
public boolean isNationChat() {
return isNationChat;
}
@Override
public boolean isAllianceChat() {
return isAllianceChat;
}
@Override
public boolean isPartyChat() {
return isPartyChat;
}
public static SimpleChatChannel.Builder builder(String name) {
return new SimpleChatChannel.Builder(name);
}
public static class Builder {
private TextColor color = TextColor.WHITE;
private long id = -1;
private String format = "<<white><%player_displayname%<white>>";
private String staffFormat = null;
private String webhook = null;
private boolean isDefault = false;
private boolean ignorable = true;
private String name;
private double distance = 0;
private String switchMessage = "<gray>You are now in <color><channel> <gray>chat!";
private boolean isTownChat = false;
private boolean isNationChat = false;
private boolean isAllianceChat = false;
private boolean isPartyChat = false;
private Builder() { }
private Builder(String name) {
this.name = name.toLowerCase();
}
public SimpleChatChannel build(SimpleChat simpleChat) {
return new SimpleChatChannel(color, id, format, staffFormat, webhook, isDefault, ignorable, name, distance, switchMessage, isTownChat, isNationChat, isAllianceChat, isPartyChat, simpleChat);
}
public Builder setColor(TextColor color) {
this.color = color;
return this;
}
public Builder setId(long id) {
this.id = id;
return this;
}
public Builder setFormat(String format) {
this.format = format;
return this;
}
public Builder setStaffFormat(String staffFormat) {
this.staffFormat = staffFormat;
return this;
}
public Builder setWebhook(String webhook) {
this.webhook = webhook;
return this;
}
public Builder setIsDefault(boolean aDefault) {
isDefault = aDefault;
return this;
}
public Builder setIgnorable(boolean ignorable) {
this.ignorable = ignorable;
return this;
}
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder setDistance(double distance) {
this.distance = distance;
return this;
}
public Builder setSwitchMessage(String switchMessage) {
this.switchMessage = switchMessage;
return this;
}
public Builder setIsTownChat(boolean isTownChat) {
this.isTownChat = isTownChat;
return this;
}
public Builder setIsNationChat(boolean isNationChat) {
this.isNationChat = isNationChat;
return this;
}
public Builder setIsAllianceChat(boolean isAllianceChat) {
this.isAllianceChat = isAllianceChat;
return this;
}
public Builder setIsPartyChat(boolean isPartyChat) {
this.isPartyChat = isPartyChat;
return this;
}
}
}
@@ -0,0 +1,38 @@
package net.draycia.simplechat.commands;
import co.aikar.commands.BaseCommand;
import co.aikar.commands.annotation.*;
import me.minidigger.minimessage.text.MiniMessageParser;
import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel;
import net.kyori.text.adapter.bukkit.TextAdapter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@CommandAlias("ch|channel|switch")
public class ChannelCommand extends BaseCommand {
private SimpleChat simpleChat;
public ChannelCommand(SimpleChat simpleChat) {
this.simpleChat = simpleChat;
}
@Default
@CommandCompletion("@chatchannel")
public void baseCommand(Player player, @Conditions("canuse:true") ChatChannel channel, @Optional String[] args) {
if (args == null || args.length == 0) {
simpleChat.setPlayerChannel(player, channel);
String switchMessage = MiniMessageParser.handlePlaceholders(channel.getSwitchMessage(),
"color", "<" + channel.getColor().toString() + ">", "channel", channel.getName());
TextAdapter.sendMessage(player, MiniMessageParser.parseFormat(switchMessage));
} else {
Bukkit.getScheduler().scheduleAsyncDelayedTask(simpleChat, () -> {
channel.sendMessage(player, String.join(" ", args));
});
}
}
}
@@ -0,0 +1,74 @@
package net.draycia.simplechat.events;
import net.draycia.simplechat.channels.ChatChannel;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class ChannelChatEvent extends Event implements Cancellable {
private static final HandlerList HANDLERS_LIST = new HandlerList();
private boolean isCancelled = false;
private Player player;
private ChatChannel chatChannel;
private String format;
private String message;
public ChannelChatEvent(Player player, ChatChannel chatChannel, String format, String message) {
super(true);
this.player = player;
this.chatChannel = chatChannel;
this.format = format;
this.message = message;
}
@Override
public boolean isCancelled() {
return isCancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.isCancelled = cancelled;
}
@Override
public HandlerList getHandlers() {
return HANDLERS_LIST;
}
public static HandlerList getHandlerList() {
return HANDLERS_LIST;
}
public Player getPlayer() {
return player;
}
public ChatChannel getChatChannel() {
return chatChannel;
}
public void setChatChannel(ChatChannel chatChannel) {
this.chatChannel = chatChannel;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
@@ -0,0 +1,31 @@
package net.draycia.simplechat.listeners;
import net.draycia.simplechat.SimpleChat;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
public class PlayerChatListener implements Listener {
private SimpleChat simpleChat;
public PlayerChatListener(SimpleChat simpleChat) {
this.simpleChat = simpleChat;
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerchat(AsyncPlayerChatEvent event) {
event.getRecipients().clear();
if (event.isAsynchronous()) {
simpleChat.getPlayerChannel(event.getPlayer()).sendMessage(event.getPlayer(), event.getMessage());
} else {
Bukkit.getScheduler().scheduleAsyncDelayedTask(simpleChat, () -> {
simpleChat.getPlayerChannel(event.getPlayer()).sendMessage(event.getPlayer(), event.getMessage());
});
}
}
}