Skip to content

Commit

Permalink
Commands for chat switching. Improved /clearchat command. Bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brikster committed Mar 30, 2020
1 parent d5e41b7 commit 03e6cbd
Show file tree
Hide file tree
Showing 22 changed files with 321 additions and 59 deletions.
2 changes: 1 addition & 1 deletion bungee/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>chatty</artifactId>
<groupId>ru.mrbrikster</groupId>
<version>2.17.1</version>
<version>2.18</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ru.mrbrikster</groupId>
<artifactId>chatty</artifactId>
<version>2.17.1</version>
<version>2.18</version>
<packaging>pom</packaging>

<modules>
Expand Down
2 changes: 1 addition & 1 deletion spigot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>chatty</artifactId>
<groupId>ru.mrbrikster</groupId>
<version>2.17.1</version>
<version>2.18</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
10 changes: 8 additions & 2 deletions spigot/src/main/java/ru/mrbrikster/chatty/Chatty.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public final class Chatty extends BukkitBasePlugin {
private Messages messages;
private Debugger debugger;
private Configuration configuration;
private ChatManager chatManager;

public static Chatty instance() {
return Chatty.instance;
Expand All @@ -51,17 +52,17 @@ public void onEnable() {
configuration = getConfiguration("config.yml");
}

ChatManager chatManager = new ChatManager(configuration);
ModerationManager moderationManager = new ModerationManager(this, configuration);
JsonStorage jsonStorage = new JsonStorage(configuration, this);
this.chatManager = new ChatManager(configuration, jsonStorage);
DependencyManager dependencyManager = new DependencyManager(configuration, jsonStorage, this);

this.messages = new Messages(this, configuration);
this.debugger = new Debugger(this, configuration);

configuration.onReload(config -> this.messages = new Messages(this, config));

this.commandManager = new CommandManager(configuration, dependencyManager, jsonStorage, moderationManager);
this.commandManager = new CommandManager(configuration, chatManager, dependencyManager, jsonStorage, moderationManager);
new NotificationManager(configuration);

EventPriority eventPriority;
Expand Down Expand Up @@ -146,6 +147,11 @@ public void onEnable() {
public void onDisable() {
this.getServer().getScheduler().cancelTasks(this);
this.commandManager.unregisterAll();
this.chatManager.getChats().forEach(chat -> {
if (chat.getBukkitCommand() != null) {
chat.getBukkitCommand().unregister(Chatty.instance());
}
});
}

}
14 changes: 13 additions & 1 deletion spigot/src/main/java/ru/mrbrikster/chatty/chat/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import ru.mrbrikster.baseplugin.commands.BukkitCommand;
import ru.mrbrikster.chatty.Chatty;
import ru.mrbrikster.chatty.reflection.Reflection;

Expand All @@ -26,8 +28,11 @@ public class Chat {
@Getter private final boolean permission;
@Getter private final long cooldown;
@Getter private final int money;
@Getter private final String command;

public Chat(String name, boolean enable, String format, int range, String symbol, boolean permission, long cooldown, int money) {
@Getter @Setter private BukkitCommand bukkitCommand;

public Chat(String name, boolean enable, String format, int range, String symbol, boolean permission, long cooldown, int money, String command) {
this.name = name;
this.enable = enable;
this.format = format;
Expand All @@ -36,6 +41,13 @@ public Chat(String name, boolean enable, String format, int range, String symbol
this.permission = permission;
this.cooldown = cooldown * 1000;
this.money = money;
this.command = command;
}

public boolean isAllowed(Player player) {
return !isPermission()
|| player.hasPermission(String.format("chatty.chat.%s", getName()))
|| player.hasPermission(String.format("chatty.chat.%s.write", getName()));
}

List<Player> getRecipients(Player player, JsonStorage jsonStorage) {
Expand Down
53 changes: 30 additions & 23 deletions spigot/src/main/java/ru/mrbrikster/chatty/chat/ChatListener.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.mrbrikster.chatty.chat;

import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -31,10 +32,7 @@
import ru.mrbrikster.chatty.reflection.Reflection;
import ru.mrbrikster.chatty.util.Pair;

import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -100,9 +98,10 @@ public void execute(Listener listener, Event event) {

private void onChat(AsyncPlayerChatEvent playerChatEvent) {
final Player player = playerChatEvent.getPlayer();
String message = playerChatEvent.getMessage();

Chat chat = getChat(player, message);
Pair<Chat, String> chatMessagePair = getChat(player, playerChatEvent.getMessage());
Chat chat = chatMessagePair.getKey();
String message = chatMessagePair.getValue();

if (chat == null) {
playerChatEvent.setCancelled(true);
Expand All @@ -112,10 +111,6 @@ private void onChat(AsyncPlayerChatEvent playerChatEvent) {

boolean json = configuration.getNode("json.enable").getAsBoolean(false);

if (!chat.getSymbol().isEmpty()) {
message = message.substring(chat.getSymbol().length());
}

message = stylish(player, message, chat.getName());

if (ChatColor.stripColor(message).isEmpty()) {
Expand Down Expand Up @@ -383,11 +378,11 @@ public void onJsonMessage(AsyncPlayerChatEvent playerChatEvent) {
List<Player> canSeeSwears = new ArrayList<>();
List<Player> cannotSeeSwears = new ArrayList<>();

Reflection.getOnlinePlayers().forEach(onlinePlayer -> {
if (onlinePlayer.hasPermission("chatty.swears.see")) {
canSeeSwears.add(onlinePlayer);
playerChatEvent.getRecipients().forEach(recipient -> {
if (recipient.hasPermission("chatty.swears.see")) {
canSeeSwears.add(recipient);
} else {
cannotSeeSwears.add(onlinePlayer);
cannotSeeSwears.add(recipient);
}
});

Expand All @@ -409,6 +404,7 @@ public void onJsonMessage(AsyncPlayerChatEvent playerChatEvent) {
formattedMessage.send(playerChatEvent.getRecipients());
}

playerChatEvent.setFormat(formattedMessage.toReadableText().replace("%", "%%"));
playerChatEvent.getRecipients().clear();
}

Expand Down Expand Up @@ -481,27 +477,38 @@ public void onPlayerDeath(PlayerDeathEvent playerDeathEvent) {
}
}

private Chat getChat(final Player player, final String message) {
private Pair<Chat, String> getChat(final Player player, String message) {
Chat currentChat = null;

for (Chat chat : this.chatManager.getChats()) {
if (!chat.isEnable()) {
continue;
Optional<JsonElement> optional = jsonStorage.getProperty(player, "chat");
if (optional.isPresent()) {
JsonElement jsonElement = optional.get();
if (jsonElement.isJsonPrimitive()) {
String chatName = jsonElement.getAsJsonPrimitive().getAsString();
Chat chat = chatManager.getChat(chatName);
if (chat != null) {
if (chat.isAllowed(player)) {
currentChat = chat;
}
}
}
}

if (!chat.isPermission()
|| player.hasPermission(String.format("chatty.chat.%s", chat.getName()))
|| player.hasPermission(String.format("chatty.chat.%s.write", chat.getName()))) {
for (Chat chat : this.chatManager.getChats()) {
if (chat.isAllowed(player)) {
if (chat.getSymbol().isEmpty()) {
currentChat = chat;
if (currentChat == null) {
currentChat = chat;
}
} else if (message.startsWith(chat.getSymbol())) {
currentChat = chat;
message = message.substring(chat.getSymbol().length());
break;
}
}
}

return currentChat;
return new Pair<>(currentChat, message);
}

private String stylish(Player player, String message, String chat) {
Expand Down
59 changes: 56 additions & 3 deletions spigot/src/main/java/ru/mrbrikster/chatty/chat/ChatManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package ru.mrbrikster.chatty.chat;

import com.google.gson.JsonPrimitive;
import lombok.Getter;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import ru.mrbrikster.baseplugin.commands.BukkitCommand;
import ru.mrbrikster.baseplugin.config.Configuration;
import ru.mrbrikster.chatty.Chatty;

Expand All @@ -20,16 +23,28 @@ public class ChatManager {
@Getter private final List<Chat> chats = new ArrayList<>();
@Getter private final Logger logger;
private final Configuration configuration;
private final JsonStorage jsonStorage;

public ChatManager(Configuration configuration) {
public ChatManager(Configuration configuration, JsonStorage jsonStorage) {
this.configuration = configuration;
this.jsonStorage = jsonStorage;
this.logger = new Logger();

init();

configuration.onReload(config -> reload());
}

public Chat getChat(String chatName) {
for (Chat chat : chats) {
if (chat.getName().equalsIgnoreCase(chatName)) {
return chat;
}
}

return null;
}

private void init() {
configuration.getNode("chats")
.getChildNodes().stream().map(chatNode ->
Expand All @@ -40,11 +55,49 @@ private void init() {
chatNode.getNode("symbol").getAsString(""),
chatNode.getNode("permission").getAsBoolean(true),
chatNode.getNode("cooldown").getAsLong(-1),
chatNode.getNode("money").getAsInt(0)))
.forEach(this.chats::add);
chatNode.getNode("money").getAsInt(0),
chatNode.getNode("command").getAsString(null)))
.forEach(chat -> {
if (chat.isEnable()) {
if (chat.getCommand() != null) {
chat.setBukkitCommand(new BukkitCommand(chat.getCommand()) {

@Override
public void handle(CommandSender sender, String label, String[] args) {
if (sender instanceof Player) {
if (!sender.hasPermission("chatty.command.chat")) {
sender.sendMessage(Chatty.instance().messages().get("no-permission"));
return;
}

if (chat.isAllowed((Player) sender)) {
jsonStorage.setProperty((Player) sender, "chat", new JsonPrimitive(chat.getName()));
sender.sendMessage(Chatty.instance().messages().get("chat-command.chat-switched").replace("{chat}", chat.getName()));
} else {
sender.sendMessage(Chatty.instance().messages().get("chat-command.no-chat-permission"));
}
} else {
sender.sendMessage(Chatty.instance().messages().get("only-for-players"));
}
}

});

chat.getBukkitCommand().register(Chatty.instance());
}

this.chats.add(chat);
}
});
}

private void reload() {
chats.forEach(chat -> {
if (chat.getBukkitCommand() != null) {
chat.getBukkitCommand().unregister(Chatty.instance());
}
});

chats.clear();
init();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ru.mrbrikster.chatty.commands;

import com.google.gson.JsonPrimitive;
import net.amoebaman.util.ArrayWrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import ru.mrbrikster.baseplugin.commands.BukkitCommand;
import ru.mrbrikster.baseplugin.config.Configuration;
import ru.mrbrikster.chatty.Chatty;
import ru.mrbrikster.chatty.chat.Chat;
import ru.mrbrikster.chatty.chat.ChatManager;
import ru.mrbrikster.chatty.chat.JsonStorage;

public class ChatCommand extends BukkitCommand {

private final ChatManager chatManager;
private final JsonStorage jsonStorage;

public ChatCommand(Configuration configuration, ChatManager chatManager, JsonStorage jsonStorage) {
super("chat", ArrayWrapper.toArray(configuration.getNode("miscellaneous.commands.chat.aliases").getAsStringList(), String.class));

this.chatManager = chatManager;
this.jsonStorage = jsonStorage;
}

@Override
public void handle(CommandSender sender, String label, String[] args) {
if (sender instanceof Player) {
if (!sender.hasPermission("chatty.command.chat")) {
sender.sendMessage(Chatty.instance().messages().get("no-permission"));
return;
}

if (args.length == 1) {
String chatName = args[0];
Chat chat = chatManager.getChat(chatName);

if (chat != null) {
if (!chat.isPermission()
|| sender.hasPermission(String.format("chatty.chat.%s", chat.getName()))
|| sender.hasPermission(String.format("chatty.chat.%s.write", chat.getName()))) {
jsonStorage.setProperty((Player) sender, "chat", new JsonPrimitive(chat.getName()));
sender.sendMessage(Chatty.instance().messages().get("chat-command.chat-switched").replace("{chat}", chat.getName()));
} else {
sender.sendMessage(Chatty.instance().messages().get("chat-command.no-chat-permission"));
}
} else {
sender.sendMessage(Chatty.instance().messages().get("chat-command.chat-not-found"));
}
} else {
sender.sendMessage(Chatty.instance().messages().get("chat-command.usage").replace("{label}", label));
}
} else {
sender.sendMessage(Chatty.instance().messages().get("only-for-players"));
}
}

}
Loading

0 comments on commit 03e6cbd

Please sign in to comment.