Skip to content

Commit

Permalink
2.0.1 - Added back chat commands-formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanguygab committed Jun 9, 2023
1 parent 7e07dd1 commit 5a1f20e
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'io.github.tanguygab'
version = '2.0.0'
version = '2.0.1'

compileJava.options.encoding = 'UTF-8'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class TranslationFile extends YamlConfigurationFile {
public final String chatOff = getString("chat.off", "&aYou won't receive any new chat message!");
private final String chatCooldown = getString("chat.cooldown", "&cYou have to wait %seconds% more seconds!");
private final String chatCleared = getString("chat.cleared", "&aChat cleared by %name%!");
private final String CHAT_CMD_JOIN = getString("chat.commands-formats.join", "&7You joined %name%!");
private final String CHAT_CMD_LEAVE = getString("chat.commands-formats.leave", "&7You left %name%!");

private final String ignoreOn = getString("ignore.on", "&cYou won't receive any new message from %name%!");
private final String ignoreOff = getString("ignore.off", "&aYou will now receive new messages from %name%!");
Expand Down Expand Up @@ -95,4 +97,11 @@ public String getEmojiHeader(int owned, int max) {
.replace("%max%",max+"");
}

public String getChatCmdJoin(String name) {
return CHAT_CMD_JOIN.replace("%name%",name);
}
public String getChatCmdLeave(String name) {
return CHAT_CMD_LEAVE.replace("%name%",name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import io.github.tanguygab.tabadditions.shared.TABAdditions;
import io.github.tanguygab.tabadditions.shared.TranslationFile;
import io.github.tanguygab.tabadditions.shared.features.advancedconditions.AdvancedConditions;
import io.github.tanguygab.tabadditions.shared.features.chat.commands.CommandManager;
import io.github.tanguygab.tabadditions.shared.features.chat.commands.FormatCommand;
import io.github.tanguygab.tabadditions.shared.features.chat.emojis.EmojiManager;
import io.github.tanguygab.tabadditions.shared.features.chat.mentions.MentionManager;
import lombok.Getter;
Expand Down Expand Up @@ -53,6 +55,7 @@ public class Chat extends TabFeature implements UnLoadable, JoinListener, Comman
private final MentionManager mentionManager;
private final MsgManager msgManager;
protected final SocialSpyManager socialSpyManager;
private final CommandManager commandsManager;

public Double cooldownTime;
public Map<UUID, LocalDateTime> cooldown = new HashMap<>();
Expand Down Expand Up @@ -84,14 +87,15 @@ public Chat(ConfigurationFile config) {
String channel = (String) format.get("channel");
@SuppressWarnings("unchecked")
Map<String,Map<String,Object>> display = (Map<String, Map<String, Object>>) format.get("display");
this.formats.put(name,new ChatFormat(displayName,
this.formats.put(name,new ChatFormat(name,
displayName,
AdvancedConditions.getCondition(condition),
AdvancedConditions.getCondition(viewCondition),
channel == null ? "" : channel,
ChatUtils.componentsToMM(display)));
});
PlaceholderManager pm = tab.getPlaceholderManager();
pm.registerPlayerPlaceholder("%chat-format%",1000,p->getFormat((TabPlayer) p).getName());
pm.registerPlayerPlaceholder("%chat-format%",1000,p->getFormat((TabPlayer) p).getDisplayName());

emojiManager = config.getBoolean("emojis.enabled",false)
? new EmojiManager(this,
Expand Down Expand Up @@ -130,6 +134,10 @@ public Chat(ConfigurationFile config) {
config.getBoolean("socialspy.view-conditions.spy",true),
ChatUtils.componentToMM(config.getConfigurationSection("socialspy.view-conditions.output")))
: null;
commandsManager = config.hasConfigOption("commands-formats")
&& !config.getConfigurationSection("commands-formats").isEmpty()
? new CommandManager(this,config.getConfigurationSection("commands-formats"))
: null;

cooldownTime = config.getDouble("cooldown",0);

Expand Down Expand Up @@ -166,6 +174,7 @@ public void unload() {
if (mentionManager != null) mentionManager.unload();
if (msgManager != null) msgManager.unload();
if (socialSpyManager != null) socialSpyManager.unload();
if (commandsManager != null) commandsManager.unload();
}

public ChatFormat getFormat(TabPlayer player) {
Expand Down Expand Up @@ -228,7 +237,7 @@ public boolean onCommand(@NotNull TabPlayer p, String cmd) {
return true;
}

return false;
return commandsManager != null && commandsManager.onCommand(p,cmd);
}

public void onChat(TabPlayer sender, String message) {
Expand All @@ -244,7 +253,16 @@ public void onChat(TabPlayer sender, String message) {
if (cooldownTime != 0 && !sender.hasPermission("tabadditions.chat.bypass.cooldown"))
cooldown.put(sender.getUniqueId(),LocalDateTime.now());

ChatFormat format = getFormat(sender);
ChatFormat format = null;
if (commandsManager != null) {
format = commandsManager.getFromPrefix(sender,message);
if (format != null)
message = message.substring(((FormatCommand) format).getPrefix().length());

else if (commandsManager.contains(sender))
format = commandsManager.getFormat(sender);
}
if (format == null) format = getFormat(sender);
if (format == null) return;
String text = format.getText();
kyori.console().sendMessage(createMessage(sender,null,message,text));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class ChatFormat {

@Getter private final String name;
@Getter private final String displayName;
private final AdvancedConditions condition;
@Getter private final AdvancedConditions viewCondition;
@Getter private final String channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public abstract class ChatManager {
private final String data;
protected final boolean toggleCmd;
protected final List<UUID> toggled;

public ChatManager(Chat chat) {
this(chat,false,null,null,null);
}

public ChatManager(Chat chat, boolean toggleCmd, String data, String cmd, String placeholder) {
this.chat = chat;
this.toggleCmd = toggleCmd;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package io.github.tanguygab.tabadditions.shared.features.chat.commands;

import io.github.tanguygab.tabadditions.shared.features.advancedconditions.AdvancedConditions;
import io.github.tanguygab.tabadditions.shared.features.chat.Chat;
import io.github.tanguygab.tabadditions.shared.features.chat.ChatManager;
import io.github.tanguygab.tabadditions.shared.features.chat.ChatUtils;
import me.neznamy.tab.shared.platform.TabPlayer;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class CommandManager extends ChatManager {

private final Map<String,FormatCommand> commands = new HashMap<>();
private final Map<UUID,FormatCommand> players = new HashMap<>();

public CommandManager(Chat chat, Map<String,Map<String,Object>> commands) {
super(chat);

commands.forEach((name,cmd)-> {
String displayName = (String) cmd.getOrDefault("name",name);
String condition = (String) cmd.get("condition");
String viewCondition = (String) cmd.get("view-condition");
String channel = (String) cmd.get("channel");
boolean save = (boolean) cmd.getOrDefault("keep-on-reload",false);
String prefix = (String) cmd.get("prefix");
@SuppressWarnings("unchecked")
Map<String,Map<String,Object>> display = (Map<String, Map<String, Object>>) cmd.get("display");
this.commands.put(name,new FormatCommand(name,
displayName,
AdvancedConditions.getCondition(condition),
AdvancedConditions.getCondition(viewCondition),
channel == null ? "" : channel,
ChatUtils.componentsToMM(display),
save,
prefix));
});

Map<String,String> data = plugin.getPlayerData().getConfigurationSection("chat-commands-formats");
data.forEach((uuid,cmd)->{
if (this.commands.containsKey(cmd))
players.put(UUID.fromString(uuid),this.commands.get(cmd));
});
}

@Override
public void unload() {
Map<String,String> data = new HashMap<>();
players.forEach((uuid,cmd)->{
if (cmd.saveOnReload())
data.put(uuid.toString(),cmd.getName());
});
plugin.getPlayerData().set("chat-commands-formats",data.isEmpty() ? null : data);
}

public boolean contains(TabPlayer player) {
return players.containsKey(player.getUniqueId());
}

public FormatCommand getFormat(TabPlayer player) {
return players.get(player.getUniqueId());
}

public FormatCommand getFromPrefix(TabPlayer sender, String message) {
for (FormatCommand cmd : commands.values())
if (message.startsWith(cmd.getPrefix()) && cmd.isConditionMet(sender))
return cmd;
return null;
}

@Override
public boolean onCommand(TabPlayer sender, String command) {
FormatCommand cmd = commands.get(command.substring(1));
if (cmd == null) return false;

if (!cmd.isConditionMet(sender)) {
sender.sendMessage(tab.getConfiguration().getMessages().getNoPermission(), true);
return true;
}
String name = cmd.getDisplayName();
if (players.containsKey(sender.getUniqueId())) {
players.remove(sender.getUniqueId());
sender.sendMessage(translation.getChatCmdLeave(name), true);
return true;
}
players.put(sender.getUniqueId(), cmd);
sender.sendMessage(translation.getChatCmdJoin(name), true);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.tanguygab.tabadditions.shared.features.chat.commands;

import io.github.tanguygab.tabadditions.shared.features.advancedconditions.AdvancedConditions;
import io.github.tanguygab.tabadditions.shared.features.chat.ChatFormat;
import lombok.Getter;
import lombok.experimental.Accessors;

public class FormatCommand extends ChatFormat {

@Getter @Accessors(fluent = true)
private final boolean saveOnReload;
@Getter private final String prefix;

public FormatCommand(String name, String displayName, AdvancedConditions condition, AdvancedConditions viewCondition, String channel, String text, boolean saveOnReload, String prefix) {
super(name, displayName, condition, viewCondition, channel, text.replace("%chat-format%",displayName));
this.saveOnReload = saveOnReload;
this.prefix = prefix;
}

}
3 changes: 3 additions & 0 deletions src/main/resources/chat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ commands-formats:
name: "&2LocalChat"
view-condition: "inRange" # view-conditions use advanced-conditions which support relational placeholders (check TAB-Additions' config.yml)
channel: "local"
prefix: "!"
display:
prefix:
text: "%chat-format% &a%player% &8» "
Expand All @@ -46,6 +47,8 @@ commands-formats:
name: "&4StaffChat"
condition: "permission:tab.staff"
view-condition: "permission:tab.staff"
keep-on-reload: true
prefix: "s!"
display:
prefix:
text: "%chat-format% &c%player% &8» "
Expand Down

0 comments on commit 5a1f20e

Please sign in to comment.