Finished party command, and sub commands
This commit is contained in:
@@ -8,7 +8,8 @@ import com.alttd.chat.handler.ChatHandler;
|
||||
import com.alttd.chat.listeners.ChatListener;
|
||||
import com.alttd.chat.listeners.PlayerListener;
|
||||
import com.alttd.chat.listeners.PluginMessage;
|
||||
import com.alttd.chat.objects.Channel;
|
||||
import com.alttd.chat.objects.channels.Channel;
|
||||
import com.alttd.chat.objects.channels.CustomChannel;
|
||||
import com.alttd.chat.util.ALogger;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
@@ -47,8 +48,11 @@ public class ChatPlugin extends JavaPlugin {
|
||||
registerCommand("unignore", new Unignore());
|
||||
registerCommand("muteserver", new MuteServer());
|
||||
registerCommand("spy", new Spy());
|
||||
registerCommand("party", new PartyCommand());
|
||||
registerCommand("pc", new PartyChatCommand());
|
||||
for (Channel channel : Channel.getChannels()) {
|
||||
this.getServer().getCommandMap().register(channel.getChannelName().toLowerCase(), new ChatChannel(channel));
|
||||
if (!(channel instanceof CustomChannel customChannel)) continue;
|
||||
this.getServer().getCommandMap().register(channel.getChannelName().toLowerCase(), new ChatChannel(customChannel));
|
||||
}
|
||||
|
||||
messageChannel = Config.MESSAGECHANNEL;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.alttd.chat.commands;
|
||||
|
||||
import com.alttd.chat.ChatPlugin;
|
||||
import com.alttd.chat.objects.Channel;
|
||||
import com.alttd.chat.objects.channels.CustomChannel;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.defaults.BukkitCommand;
|
||||
@@ -13,10 +13,10 @@ import java.util.Collections;
|
||||
|
||||
public class ChatChannel extends BukkitCommand {
|
||||
|
||||
Channel channel;
|
||||
CustomChannel channel;
|
||||
String command;
|
||||
|
||||
public ChatChannel(Channel channel) {
|
||||
public ChatChannel(CustomChannel channel) {
|
||||
super(channel.getChannelName().toLowerCase());
|
||||
this.channel = channel;
|
||||
this.command = channel.getChannelName().toLowerCase();
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.alttd.chat.commands;
|
||||
|
||||
import com.alttd.chat.ChatPlugin;
|
||||
import com.alttd.chat.managers.PartyManager;
|
||||
import com.alttd.chat.objects.Party;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PartyChatCommand implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if(!(sender instanceof Player player)) { // must be a player
|
||||
return true;
|
||||
}
|
||||
Party party = PartyManager.getParty(player.getUniqueId());
|
||||
if (party == null) {
|
||||
sender.sendMessage(MiniMessage.get().parse("<red>You are not in a party. For more info do <gold>/party</gold>.</red>"));
|
||||
return true;
|
||||
}
|
||||
|
||||
if(args.length == 0) {
|
||||
// TODO: 08/08/2021 lock into party chat
|
||||
return true;
|
||||
}
|
||||
|
||||
String message = StringUtils.join(args, " ", 0, args.length);
|
||||
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ChatPlugin.getInstance().getChatHandler().partyMessage(party, player, message);
|
||||
}
|
||||
}.runTaskAsynchronously(ChatPlugin.getInstance());
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,23 @@ package com.alttd.chat.commands;
|
||||
|
||||
import com.alttd.chat.ChatPlugin;
|
||||
import com.alttd.chat.database.Queries;
|
||||
import com.alttd.chat.managers.ChatUserManager;
|
||||
import com.alttd.chat.managers.PartyManager;
|
||||
import com.alttd.chat.objects.ChatUser;
|
||||
import com.alttd.chat.objects.Party;
|
||||
import com.alttd.chat.util.Utility;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class PartyCommand implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
@@ -17,34 +26,145 @@ public class PartyCommand implements CommandExecutor {
|
||||
if(!(sender instanceof Player player)) { // must be a player
|
||||
return true;
|
||||
}
|
||||
if(args.length == 0) return false;
|
||||
|
||||
if (args.length == 0) {
|
||||
helpMessage(sender);
|
||||
return true;
|
||||
}
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
switch (args[0].toLowerCase()) {
|
||||
case "create" -> {
|
||||
// TODO: 06/08/2021 verify args 1, 2 and check args length (3-16 char limit?)
|
||||
if (args.length < 3 || !args[1].matches("[\\w]{3,16}") || !args[2].matches("[\\w]{3,16}")) {
|
||||
invalidMessage(sender, CommandUsage.CREATE);
|
||||
break;
|
||||
}
|
||||
if (PartyManager.getParty(args[1]) != null) {
|
||||
sender.sendMessage(MiniMessage.get().parse("<red>A party with this name already exists.</red>"));
|
||||
break;
|
||||
}
|
||||
Party party = Queries.addParty(player.getUniqueId(), args[1], args[2]);
|
||||
party.addUser(ChatUserManager.getChatUser(player.getUniqueId()));
|
||||
PartyManager.addParty(party);
|
||||
sender.sendMessage(MiniMessage.get().parse("<green>You created a party called: '<gold>" +
|
||||
party.getPartyName() + "</gold>' with the password: '<gold>" +
|
||||
party.getPartyPassword() + "</gold>'</green>"));
|
||||
}
|
||||
case "invite" -> {
|
||||
// TODO: 07/08/2021 send invite to user, when they click execute </party join partyname password>
|
||||
if (args.length < 2) {
|
||||
invalidMessage(sender, CommandUsage.INVITE);
|
||||
break;
|
||||
}
|
||||
Party party = PartyManager.getParty(player.getUniqueId());
|
||||
if (party == null) {
|
||||
sender.sendMessage(MiniMessage.get().parse("<red>You're not in a party.</red>"));
|
||||
break;
|
||||
}
|
||||
if (!party.getOwnerUuid().equals(player.getUniqueId())) {
|
||||
sender.sendMessage("<red>You don't own this party.</red>");
|
||||
break;
|
||||
}
|
||||
Player target = Bukkit.getPlayer(args[1]);
|
||||
if (target == null || !target.isOnline()) {
|
||||
sender.sendMessage(MiniMessage.get().parse("<red>The player must be on the same server to receive an invite.</red>"));
|
||||
break;
|
||||
}
|
||||
|
||||
target.sendMessage(MiniMessage.get().parse("<click:run_command:'/party join " + party.getPartyName() + " " + party.getPartyPassword() +
|
||||
"'><dark_aqua>You received an invite to join " + party.getPartyName() + " click this message to accept.</dark_aqua></click>"));
|
||||
sender.sendMessage(MiniMessage.get().parse("<green>You send a party invite to " + target.getName() + "!</green>"));
|
||||
}
|
||||
case "join" -> {
|
||||
// TODO: 07/08/2021 verify password and join party
|
||||
if (args.length < 3 || !args[1].matches("[\\w]{3,16}") || !args[2].matches("[\\w]{3,16}")) {
|
||||
invalidMessage(sender, CommandUsage.JOIN);
|
||||
break;
|
||||
}
|
||||
|
||||
Party party = PartyManager.getParty(args[1]);
|
||||
if (party == null) {
|
||||
sender.sendMessage(MiniMessage.get().parse("<red>This party does not exist.</red>"));
|
||||
break;
|
||||
}
|
||||
if (!party.getPartyPassword().equals(args[2])) {
|
||||
sender.sendMessage(MiniMessage.get().parse("<red>Invalid password.</red>"));
|
||||
break;
|
||||
}
|
||||
|
||||
party.addUser(ChatUserManager.getChatUser(player.getUniqueId()));
|
||||
sender.sendMessage(MiniMessage.get().parse("<green>You joined " + party.getPartyName() + "!</green>"));
|
||||
}
|
||||
case "leave" -> {
|
||||
Party party = PartyManager.getParty(player.getUniqueId());
|
||||
if (party == null) {
|
||||
sender.sendMessage(MiniMessage.get().parse("<red>You're not in a party.</red>"));
|
||||
break;
|
||||
}
|
||||
|
||||
party.removeUser(player.getUniqueId());
|
||||
if (party.getOwnerUuid().equals(player.getUniqueId())) {
|
||||
if (party.getPartyUsers().size() > 0) {
|
||||
ChatUser chatUser = ChatUserManager.getChatUser(party.newOwner());
|
||||
sender.sendMessage(MiniMessage.get().parse("<dark_aqua>Since you own this party a new party owner will be chosen.<dark_aqua>"));
|
||||
ChatPlugin.getInstance().getChatHandler().partyMessage(party, player, "<dark_aqua>" +
|
||||
ChatUserManager.getChatUser(player.getUniqueId()).getDisplayName() +
|
||||
" left the party, the new party owner is " + chatUser.getDisplayName());
|
||||
} else {
|
||||
party.delete();
|
||||
}
|
||||
}
|
||||
// TODO: 07/08/2021 leave the party
|
||||
}
|
||||
case "remove" -> {
|
||||
// TODO: 07/08/2021 remove specified user
|
||||
if (args.length < 2) {
|
||||
invalidMessage(sender, CommandUsage.REMOVE);
|
||||
break;
|
||||
}
|
||||
Party party = PartyManager.getParty(player.getUniqueId());
|
||||
if (party == null) {
|
||||
sender.sendMessage(MiniMessage.get().parse("<red>You're not in a party.</red>"));
|
||||
break;
|
||||
}
|
||||
if (!party.getOwnerUuid().equals(player.getUniqueId())) {
|
||||
sender.sendMessage("<red>You don't own this party.</red>");
|
||||
break;
|
||||
}
|
||||
OfflinePlayer offlinePlayerIfCached = Bukkit.getOfflinePlayerIfCached((args[1]));
|
||||
if (offlinePlayerIfCached == null) {
|
||||
sender.sendMessage(MiniMessage.get().parse("<red>Unable to find this player.</red>"));
|
||||
return;
|
||||
}
|
||||
party.removeUser(ChatUserManager.getChatUser(offlinePlayerIfCached.getUniqueId()));
|
||||
|
||||
if (offlinePlayerIfCached.isOnline()) {
|
||||
Objects.requireNonNull(offlinePlayerIfCached.getPlayer())
|
||||
.sendMessage(MiniMessage.get().parse("<red>You were removed from the '" + party.getPartyName() + "' party."));
|
||||
}
|
||||
|
||||
sender.sendMessage(MiniMessage.get().parse("<green>You removed " + offlinePlayerIfCached.getName() + " from the party!</green>"));
|
||||
}
|
||||
case "delete" -> {
|
||||
// TODO: 07/08/2021 ask for confirmation, when they click repeat the command but with --confirm (so if it has that at the end obv delete the party)
|
||||
case "info" -> {
|
||||
Party party = PartyManager.getParty(player.getUniqueId());
|
||||
if (party == null) {
|
||||
sender.sendMessage(MiniMessage.get().parse("<red>You're not in a party.</red>"));
|
||||
break;
|
||||
}
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.append("<gold><bold>Party info</bold>:</gold>")
|
||||
.append("<green>Party name: <dark_aqua>").append(party.getPartyName()).append("</dark_aqua>\n")
|
||||
.append(party.getOwnerUuid().equals(player.getUniqueId()) ? "Party password: <dark_aqua>" + party.getPartyPassword() + "</dark_aqua>\n" : "")
|
||||
.append("Party owner: ").append(party.getUserDisplayName(party.getOwnerUuid())).append("\n")
|
||||
.append("Party members: ");
|
||||
for (String displayName : party.getPartyUsers().values()) {
|
||||
stringBuilder.append(displayName).append(", ");
|
||||
}
|
||||
|
||||
stringBuilder.delete(stringBuilder.length() - 2, stringBuilder.length());
|
||||
|
||||
sender.sendMessage(Utility.applyColor(stringBuilder.toString()));
|
||||
}
|
||||
default -> {
|
||||
// TODO: 07/08/2021 send help message
|
||||
helpMessage(sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,4 +172,34 @@ public class PartyCommand implements CommandExecutor {
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void invalidMessage(CommandSender sender, CommandUsage commandUsage) {
|
||||
sender.sendMessage(MiniMessage.get().parse("<red>Invalid command, proper usage: %command%.</red>".replaceAll("%command%", commandUsage.message)));
|
||||
}
|
||||
|
||||
private void helpMessage(CommandSender sender) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.append("<dark_aqua>Party commands:</dark_aqua><green>");
|
||||
for (CommandUsage commandUsage : CommandUsage.values()) {
|
||||
stringBuilder.append("\n- ").append(commandUsage.message);
|
||||
}
|
||||
stringBuilder.append("</green>");
|
||||
sender.sendMessage(MiniMessage.get().parse(stringBuilder.toString()));
|
||||
}
|
||||
|
||||
private enum CommandUsage {
|
||||
CREATE("<gold>/party create <#FFE800><hover:show_text:'<gold>A party name must be 3-16 characters</gold>'><name></hover> " +
|
||||
"<hover:show_text:'<gold>A party password must be 3-16 characters\n</gold>" +
|
||||
"<red>When choosing a password keep in mind staff can see it and you might need to share it with other players!</red>'><password></#FFE800></hover></gold>"),
|
||||
INVITE("<gold>/party invite <username></gold>"),
|
||||
JOIN("<gold>/party join <party name> <password></gold>"),
|
||||
LEAVE("<gold><hover:show_text:'<red>If the party owner leaves the server will choose a new party owner</red>'>/party leave</hover></gold>"),
|
||||
REMOVE("<gold>/party remove <username></gold>");
|
||||
|
||||
private final String message;
|
||||
|
||||
CommandUsage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.alttd.chat.handler;
|
||||
|
||||
import com.alttd.chat.ChatPlugin;
|
||||
import com.alttd.chat.commands.ChatChannel;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.managers.ChatUserManager;
|
||||
import com.alttd.chat.managers.RegexManager;
|
||||
import com.alttd.chat.objects.Channel;
|
||||
import com.alttd.chat.objects.channels.CustomChannel;
|
||||
import com.alttd.chat.objects.ChatUser;
|
||||
import com.alttd.chat.objects.Party;
|
||||
import com.alttd.chat.util.GalaxyUtility;
|
||||
import com.alttd.chat.util.Utility;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
@@ -17,12 +17,10 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.StringUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -124,7 +122,7 @@ public class ChatHandler {
|
||||
sendPluginMessage(player, "globalchat", component);
|
||||
}
|
||||
|
||||
public void chatChannel(Player player, Channel channel, String message) {
|
||||
public void chatChannel(Player player, CustomChannel channel, String message) {
|
||||
if (!player.hasPermission(channel.getPermission())) {
|
||||
player.sendMessage(MiniMessage.get().parse("<red>You don't have permission to use this channel.</red>"));
|
||||
return;
|
||||
@@ -137,7 +135,7 @@ public class ChatHandler {
|
||||
|
||||
String updatedMessage = RegexManager.replaceText(player.getName(), player.getUniqueId(), message);
|
||||
if(updatedMessage == null) {
|
||||
GalaxyUtility.sendBlockedNotification("GC Language", player, message, "");
|
||||
GalaxyUtility.sendBlockedNotification(channel.getChannelName() + " Language", player, message, "");
|
||||
return; // the message was blocked
|
||||
}
|
||||
|
||||
@@ -163,7 +161,35 @@ public class ChatHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private void sendChatChannelMessage(Channel chatChannel, UUID uuid, Component component) {
|
||||
public void partyMessage(Party party, Player player, String message) {
|
||||
if (isMuted(player, message, "[" + party.getPartyName() + " Muted] ")) return;
|
||||
|
||||
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
|
||||
Component senderName = user.getDisplayName();
|
||||
|
||||
String updatedMessage = RegexManager.replaceText(player.getName(), player.getUniqueId(), message);
|
||||
if(updatedMessage == null) {
|
||||
GalaxyUtility.sendBlockedNotification("Party Language", player, message, "");
|
||||
return; // the message was blocked
|
||||
}
|
||||
|
||||
if(!player.hasPermission("chat.format")) {
|
||||
updatedMessage = miniMessage.stripTokens(updatedMessage);
|
||||
}
|
||||
|
||||
if(updatedMessage.contains("[i]")) updatedMessage = updatedMessage.replace("[i]", "<[i]>");
|
||||
|
||||
List<Template> templates = new ArrayList<>(List.of(
|
||||
Template.of("sender", senderName),
|
||||
Template.of("message", updatedMessage),
|
||||
Template.of("server", Bukkit.getServerName()),
|
||||
Template.of("[i]", itemComponent(player.getInventory().getItemInMainHand()))));
|
||||
|
||||
Component component = miniMessage.parse(Config.PARTY_FORMAT, templates);
|
||||
sendPartyMessage(player, party.getPartyId(), component);
|
||||
}
|
||||
|
||||
private void sendChatChannelMessage(CustomChannel chatChannel, UUID uuid, Component component) {
|
||||
if (!chatChannel.getServers().contains(Bukkit.getServerName())) return;
|
||||
|
||||
Bukkit.getServer().getOnlinePlayers().stream()
|
||||
@@ -197,6 +223,16 @@ public class ChatHandler {
|
||||
out.writeUTF(GsonComponentSerializer.gson().serialize(component));
|
||||
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
|
||||
}
|
||||
|
||||
private void sendPartyMessage(Player player, int partyId, Component component) {
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
out.writeUTF("party");
|
||||
out.writeUTF(String.valueOf(partyId));
|
||||
out.writeUTF(player.getUniqueId().toString());
|
||||
out.writeUTF(GsonComponentSerializer.gson().serialize(component));
|
||||
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
|
||||
}
|
||||
|
||||
// Start - move these to util
|
||||
|
||||
private boolean isMuted(Player player, String message, String prefix) {
|
||||
|
||||
@@ -2,12 +2,11 @@ package com.alttd.chat.listeners;
|
||||
|
||||
import com.alttd.chat.ChatPlugin;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.database.Queries;
|
||||
import com.alttd.chat.managers.ChatUserManager;
|
||||
import com.alttd.chat.objects.Channel;
|
||||
import com.alttd.chat.objects.channels.Channel;
|
||||
import com.alttd.chat.objects.channels.CustomChannel;
|
||||
import com.alttd.chat.objects.ChatUser;
|
||||
import com.alttd.chat.util.ALogger;
|
||||
import com.alttd.chat.util.Utility;
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@@ -79,11 +78,11 @@ public class PluginMessage implements PluginMessageListener {
|
||||
}
|
||||
|
||||
private void chatChannel(ByteArrayDataInput in) {
|
||||
Channel chatChannel = null;
|
||||
CustomChannel chatChannel = null;
|
||||
UUID uuid = null;
|
||||
Component component = null;
|
||||
try {
|
||||
chatChannel = Channel.getChatChannel(in.readUTF());
|
||||
chatChannel = (CustomChannel) Channel.getChatChannel(in.readUTF());
|
||||
uuid = UUID.fromString(in.readUTF());
|
||||
component = GsonComponentSerializer.gson().deserialize(in.readUTF());
|
||||
} catch (Exception e) { //Idk the exception for reading too far into in.readUTF()
|
||||
@@ -102,7 +101,7 @@ public class PluginMessage implements PluginMessageListener {
|
||||
ALogger.warn("Didn't receive a valid message for ChatChannel " + chatChannel.getChannelName() + ".");
|
||||
}
|
||||
|
||||
final Channel finalChatChannel = chatChannel;
|
||||
final CustomChannel finalChatChannel = chatChannel;
|
||||
final Component finalComponent = component;
|
||||
final UUID finalUuid = uuid;
|
||||
|
||||
|
||||
@@ -26,4 +26,8 @@ commands:
|
||||
muteserver:
|
||||
permission: command.mute-server
|
||||
spy:
|
||||
permission: command.togglespy
|
||||
permission: command.togglespy
|
||||
party:
|
||||
permission: command.party
|
||||
pc:
|
||||
permission: command.pc
|
||||
Reference in New Issue
Block a user