Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Commands: add new command /chat-settings and refactor /chat
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFaser committed Aug 26, 2023
1 parent 009a42f commit 25de44b
Show file tree
Hide file tree
Showing 30 changed files with 587 additions and 193 deletions.
7 changes: 6 additions & 1 deletion src/main/java/net/flectone/commands/CommandBall.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
|| fCommand.isHaveCD()
|| fCommand.isMuted()) return true;

if (fCommand.isDisabled()) {
fCommand.sendMeMessage("command.you-disabled");
return true;
}

List<String> answers = locale.getStringList("command.ball.format");

Random random = new Random();
Expand All @@ -34,7 +39,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
.replace("<player>", fCommand.getSenderName())
.replace("<answer>", answers.get(randomPer));

fCommand.sendGlobalMessage(formatString, ObjectUtil.toString(strings));
fCommand.sendGlobalMessage(formatString, ObjectUtil.toString(strings), null, true);

return true;
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/net/flectone/commands/CommandBroadcast.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import net.flectone.misc.commands.FCommand;
import net.flectone.misc.commands.FTabCompleter;
import net.flectone.utils.ObjectUtil;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import static net.flectone.managers.FileManager.locale;
Expand All @@ -29,7 +27,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
String formatString = locale.getString("command.broadcast.message")
.replace("<player>", fCommand.getSenderName());

fCommand.sendGlobalMessage(new HashSet<>(Bukkit.getOnlinePlayers()), formatString, ObjectUtil.toString(strings), null, false);
fCommand.sendGlobalMessage(formatString, ObjectUtil.toString(strings), null, false);

return true;
}
Expand Down
170 changes: 170 additions & 0 deletions src/main/java/net/flectone/commands/CommandChatSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package net.flectone.commands;

import net.flectone.Main;
import net.flectone.misc.commands.FCommand;
import net.flectone.misc.commands.FTabCompleter;
import net.flectone.misc.components.FComponent;
import net.flectone.misc.entity.FPlayer;
import net.flectone.utils.ObjectUtil;
import net.md_5.bungee.api.chat.ComponentBuilder;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static net.flectone.managers.FileManager.locale;
import static net.flectone.managers.FileManager.config;

public class CommandChatSettings implements FTabCompleter {

@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {

FCommand fCommand = new FCommand(commandSender, command.getName(), s, strings);

if (fCommand.isConsoleMessage()) return true;

FPlayer fPlayer = fCommand.getFPlayer();
assert fPlayer != null;

if (strings.length == 1) {

if (strings[0].equals("save")) {

Bukkit.getScheduler().runTaskAsynchronously(Main.getInstance(), () -> {
Main.getDatabase().updatePlayerInfo("chats", fPlayer.getChatInfo());
});

Bukkit.dispatchCommand(commandSender, "chat-settings");
return true;
}

if(!fPlayer.getChatInfo().getOptionsList().contains(strings[0])) {
fCommand.sendUsageMessage();
return true;
}

if (!optionEnable(strings[0])) {
fCommand.sendMeMessage("command.chat-settings.not-available");
return true;
}

fPlayer.getChatInfo().setOption(strings[0], !fPlayer.getChatInfo().getOption(strings[0]));
FComponent fComponent = new FComponent(locale.getFormatString("command.chat-settings.message-changed", commandSender));
fComponent.addRunCommand("/chat-settings save");
fPlayer.spigotMessage(fComponent.get());
return true;
}

String configString = locale.getStringList("command.chat-settings.message").stream()
.map(str -> ObjectUtil.formatString(str + "\n", commandSender)).
collect(Collectors.joining());

Set<String> optionsList = fPlayer.getChatInfo().getOptionsList().stream()
.map(string -> "<" + string + ">")
.collect(Collectors.toSet());

ArrayList<String> placeholders = new ArrayList<>(optionsList);

List<String> chatTypes = List.of("<local>", "<global>", "<onlylocal>", "<onlyglobal>");
placeholders.addAll(chatTypes);

String mainColor = "";
ComponentBuilder mainBuilder = new ComponentBuilder();

for(String mainPlaceholder : ObjectUtil.splitLine(configString, placeholders)) {

if (optionsList.contains(mainPlaceholder)) {

String name = mainPlaceholder.substring(1, mainPlaceholder.length() - 1);

String text = locale.getFormatString("command.chat-settings.format." + name, commandSender);
String hoverText = locale.getFormatString("command.chat-settings.hover-text", commandSender)
.replace("<component>", text);

String color = locale.getFormatString("command.chat-settings.color." + fPlayer.getChatInfo().getOption(name), commandSender);

FComponent fComponent = new FComponent(mainColor + color + text);

fComponent
.addHoverText(hoverText)
.addRunCommand("/chat-settings " + name);

mainBuilder.append(fComponent.get(), ComponentBuilder.FormatRetention.NONE);
} else if (chatTypes.contains(mainPlaceholder)) {

String name = mainPlaceholder.substring(1, mainPlaceholder.length() - 1);

String text = locale.getFormatString("command.chat-settings.format." + name, commandSender);
String hoverText = locale.getFormatString("command.chat-settings.hover-text", commandSender)
.replace("<component>", text);

String color = locale.getFormatString("command.chat-settings.color." + fPlayer.getChatInfo().getChatType().equals(name), commandSender);

String runCommandType = name.startsWith("only")
? "hide " + (name.substring(4).equals("local") ? "global" : "local")
: "switch " + name;

FComponent fComponent = new FComponent(mainColor + color + text);
fComponent
.addHoverText(hoverText)
.addRunCommand("/switch-chat " + runCommandType);

mainBuilder.append(fComponent.get(), ComponentBuilder.FormatRetention.NONE);

} else {
mainBuilder.append(new FComponent(mainColor + mainPlaceholder).get(), ComponentBuilder.FormatRetention.NONE);
}

mainColor = ChatColor.getLastColors(mainColor + mainBuilder.getCurrentComponent().toString());
}

fCommand.getFPlayer().spigotMessage(mainBuilder.create());

return true;
}

private boolean optionEnable(String option) {

if(!config.getString("command." + option + ".enable").isEmpty()) {
return config.getBoolean("command." + option + ".enable");
}

if(!config.getString(option + ".message.enable").isEmpty()) {
return config.getBoolean(option + ".message.enable");
}

if(!config.getString("player." + option + ".message.enable").isEmpty()) {
return config.getBoolean("player." + option + ".message.enable");
}

return true;

}


@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
wordsList.clear();

if (strings.length == 1) {
isStartsWith(strings[0], "save");
}

return wordsList;
}

@NotNull
@Override
public String getCommandName() {
return "chat-settings";
}
}
2 changes: 1 addition & 1 deletion src/main/java/net/flectone/commands/CommandHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
String formatMessage = locale.getString("command.helper.global-message")
.replace("<player>", fCommand.getSenderName());

fCommand.sendGlobalMessage(playerSet, formatMessage, ObjectUtil.toString(strings, 0), null, true);
fCommand.sendFilterGlobalMessage(playerSet, formatMessage, ObjectUtil.toString(strings, 0), null, true);

fCommand.sendMeMessage("command.helper.local-message");

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/flectone/commands/CommandIgnore.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
if (fCommand.isConsoleMessage()
|| fCommand.isInsufficientArgs(1)) return true;

if (fCommand.isSelfCommand()) {
if (strings[0].equalsIgnoreCase(commandSender.getName())) {
fCommand.sendMeMessage("command.ignore.myself");
return true;
}
Expand Down
36 changes: 25 additions & 11 deletions src/main/java/net/flectone/commands/CommandMail.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,64 @@ public class CommandMail implements FTabCompleter {

@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
Bukkit.getScheduler().runTaskAsynchronously(Main.getInstance(), () ->
command(commandSender, command, s, strings));
return true;
}

private void command(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
FCommand fCommand = new FCommand(commandSender, command.getName(), s, strings);

if (fCommand.isConsoleMessage() || fCommand.isInsufficientArgs(2) || fCommand.getFPlayer() == null) return true;
if (fCommand.isConsoleMessage() || fCommand.isInsufficientArgs(2) || fCommand.getFPlayer() == null) return;

String playerName = strings[0];
FPlayer fPlayer = FPlayerManager.getPlayerFromName(playerName);

if (fPlayer == null) {
fCommand.sendMeMessage("command.null-player");
return true;
return;
}

if (fCommand.isDisabled()) {
fCommand.sendMeMessage("command.you-disabled");
return;
}

fPlayer.synchronizeDatabase();

if (!fPlayer.getChatInfo().getOption("mail")) {
fCommand.sendMeMessage("command.he-disabled");
return;
}

String message = ObjectUtil.toString(strings, 1);

if (fCommand.getFPlayer().isIgnored(fPlayer.getUUID())) {
fCommand.sendMeMessage("command.you_ignore");
return true;
return;
}

if (fPlayer.isIgnored(fCommand.getFPlayer().getUUID())) {
fCommand.sendMeMessage("command.he_ignore");
return true;
return;
}

if (fCommand.isHaveCD() || fCommand.isMuted()) return true;
if (fCommand.isHaveCD() || fCommand.isMuted()) return;

if (fPlayer.isOnline()) {
Bukkit.dispatchCommand(commandSender, "tell " + playerName + " " + message);
return true;
fCommand.dispatchCommand("tell " + playerName + " " + message);
return;
}

Mail mail = new Mail(fCommand.getFPlayer().getUUID(), fPlayer.getUUID(), message);
fPlayer.addMail(mail.getUUID(), mail);

Bukkit.getScheduler().runTaskAsynchronously(Main.getInstance(), () ->
Main.getDatabase().updateFPlayer(fPlayer, "mails"));
Main.getDatabase().updateFPlayer(fPlayer, "mails");

String[] replaceString = {"<player>", "<message>"};
String[] replaceTo = {fPlayer.getRealName(), message};

fCommand.sendMeMessage("command.mail.send", replaceString, replaceTo);

return true;
}

@Nullable
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/net/flectone/commands/CommandMe.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
|| fCommand.isHaveCD()
|| fCommand.isMuted()) return true;

if (fCommand.isDisabled()) {
fCommand.sendMeMessage("command.you-disabled");
return true;
}

String formatString = locale.getString("command.me.message")
.replace("<player>", fCommand.getSenderName());

fCommand.sendGlobalMessage(formatString, ObjectUtil.toString(strings));
fCommand.sendGlobalMessage(formatString, ObjectUtil.toString(strings), null, true);

return true;
}
Expand Down

0 comments on commit 25de44b

Please sign in to comment.