The beginnings of the staffchat

This commit is contained in:
Brianna O'Keefe 2019-03-08 11:24:47 -05:00
parent f7c3511da4
commit 07abbaf42b
7 changed files with 181 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import com.songoda.ultimatemoderation.punish.player.PlayerPunishData;
import com.songoda.ultimatemoderation.punish.player.PunishmentManager;
import com.songoda.ultimatemoderation.punish.template.Template;
import com.songoda.ultimatemoderation.punish.template.TemplateManager;
import com.songoda.ultimatemoderation.staffchat.StaffChatManager;
import com.songoda.ultimatemoderation.storage.Storage;
import com.songoda.ultimatemoderation.storage.StorageRow;
import com.songoda.ultimatemoderation.storage.types.StorageMysql;
@ -37,6 +38,7 @@ public class UltimateModeration extends JavaPlugin {
private SettingsManager settingsManager;
private CommandManager commandManager;
private PunishmentManager punishmentManager;
private StaffChatManager staffChatManager;
private Locale locale;
private Storage storage;
@ -89,6 +91,7 @@ public class UltimateModeration extends JavaPlugin {
this.templateManager = new TemplateManager();
this.commandManager = new CommandManager(this);
this.punishmentManager = new PunishmentManager();
this.staffChatManager = new StaffChatManager();
// Load data
this.checkStorage();
@ -236,4 +239,8 @@ public class UltimateModeration extends JavaPlugin {
public TicketManager getTicketManager() {
return ticketManager;
}
public StaffChatManager getStaffChatManager() {
return staffChatManager;
}
}

View File

@ -42,6 +42,7 @@ public class CommandManager implements CommandExecutor {
instance.getCommand("Warn").setExecutor(this);
instance.getCommand("RunTemplate").setExecutor(this);
instance.getCommand("Ticket").setExecutor(this);
instance.getCommand("StaffChat").setExecutor(this);
AbstractCommand commandUltimateModeration = addCommand(new CommandUltimateModeration());
addCommand(new CommandClearChat());
@ -62,6 +63,7 @@ public class CommandManager implements CommandExecutor {
addCommand(new CommandWarn());
addCommand(new CommandRunTemplate());
addCommand(new CommandTicket());
addCommand(new CommandStaffChat());
addCommand(new CommandSettings(commandUltimateModeration));
addCommand(new CommandHelp(commandUltimateModeration));

View File

@ -0,0 +1,61 @@
package com.songoda.ultimatemoderation.command.commands;
import com.songoda.ultimatemoderation.UltimateModeration;
import com.songoda.ultimatemoderation.command.AbstractCommand;
import com.songoda.ultimatemoderation.staffchat.StaffChannel;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
public class CommandStaffChat extends AbstractCommand {
public CommandStaffChat() {
super(true, true, "StaffChat");
}
@Override
protected ReturnType runCommand(UltimateModeration instance, CommandSender sender, String... args) {
if (args.length != 1)
return ReturnType.SYNTAX_ERROR;
String channelName = args[0];
Player player = (Player)sender;
if (channelName.trim().equalsIgnoreCase("leave")) {
for (StaffChannel channel : instance.getStaffChatManager().getChats().values()) {
if (!channel.listMembers().contains(player.getUniqueId())) continue;
channel.removeMember(player);
player.sendMessage("You left " + channel.getChannelName() + " successfully.");
return ReturnType.SUCCESS;
}
player.sendMessage("You are not in any channels.");
return ReturnType.FAILURE;
}
instance.getStaffChatManager().getChat(channelName).addMember(player);
return ReturnType.SUCCESS;
}
@Override
protected List<String> onTab(UltimateModeration instance, CommandSender sender, String... args) {
return new ArrayList<>(instance.getStaffChatManager().getChats().keySet());
}
@Override
public String getPermissionNode() {
return "um.staffchat";
}
@Override
public String getSyntax() {
return "/sc <channel/leave>";
}
@Override
public String getDescription() {
return "Opens a staff chat channel.";
}
}

View File

@ -3,13 +3,17 @@ package com.songoda.ultimatemoderation.listeners;
import com.songoda.ultimatemoderation.UltimateModeration;
import com.songoda.ultimatemoderation.punish.AppliedPunishment;
import com.songoda.ultimatemoderation.punish.PunishmentType;
import com.songoda.ultimatemoderation.staffchat.StaffChannel;
import com.songoda.ultimatemoderation.utils.Methods;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class ChatListener implements Listener {
@ -27,6 +31,20 @@ public class ChatListener implements Listener {
@EventHandler
public void onChat(AsyncPlayerChatEvent event) {
Player player = event.getPlayer();
for (Map.Entry<String, StaffChannel> entry : instance.getStaffChatManager().getChats().entrySet()) {
String channel = entry.getKey();
StaffChannel members = entry.getValue();
if (!members.listMembers().contains(player.getUniqueId())) continue;
event.setCancelled(true);
for (UUID uuid : members.listMembers()) {
Player p = Bukkit.getPlayer(uuid);
if (p == null) continue;
p.sendMessage(Methods.formatText(channel + " " + player.getDisplayName() + "&" + members.getChatChar() + ": " + event.getMessage()));
}
}
if (!isChatToggled && !player.hasPermission("um.togglechat.bypass")) {
event.setCancelled(true);
player.sendMessage(instance.getReferences().getPrefix() + Methods.formatText(instance.getLocale().getMessage("command.togglechat.muted")));

View File

@ -0,0 +1,60 @@
package com.songoda.ultimatemoderation.staffchat;
import com.songoda.ultimatemoderation.UltimateModeration;
import com.songoda.ultimatemoderation.utils.Methods;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class StaffChannel {
private final String channelName;
private char chatChar = 'b';
private final List<UUID> members = new ArrayList<>();
public StaffChannel(String channelName) {
this.channelName = channelName;
}
public List<UUID> listMembers() {
return new ArrayList<>(members);
}
public void addMember(Player player) {
UltimateModeration.getInstance().getStaffChatManager().getChats().values().stream().forEach(members1 -> {
if (members1.listMembers().contains(player.getUniqueId())) {
members1.removeMember(player);
}
});
members.add(player.getUniqueId());
for (UUID uuid : members) {
Player p = Bukkit.getPlayer(uuid);
if (p == null) continue;
p.sendMessage(Methods.formatText(channelName + " " + player.getDisplayName() + "&" + chatChar + " has just entered the channel."));
}
}
public void removeMember(Player player) {
members.remove(player.getUniqueId());
for (UUID uuid : members) {
Player p = Bukkit.getPlayer(uuid);
if (p == null) continue;
p.sendMessage(Methods.formatText(channelName + " " + player.getDisplayName() + "&" + chatChar + " has just left the channel."));
}
}
public String getChannelName() {
return channelName;
}
public char getChatChar() {
return chatChar;
}
public void setChatChar(char chatChar) {
this.chatChar = chatChar;
}
}

View File

@ -0,0 +1,27 @@
package com.songoda.ultimatemoderation.staffchat;
import java.util.*;
public class StaffChatManager {
private final Map<String, StaffChannel> chats = new HashMap<>();
public Map<String, StaffChannel> getChats() {
return Collections.unmodifiableMap(chats);
}
public StaffChannel getChat(String channel) {
return chats.computeIfAbsent(formatName(channel), k -> new StaffChannel(formatName(channel)));
}
public void removeChat(String channel) {
chats.remove(formatName(channel));
}
private String formatName(String name) {
if (name == null) return null;
name = name.toUpperCase().trim();
name = name.replace(" ", "_");
return name;
}
}

View File

@ -87,4 +87,9 @@ commands:
Ticket:
description: Ticket
defaullt: false
usage: /ticket
usage: /ticket
StaffChat:
description: StaffChat
defaullt: false
aliases: [sc]
usage: /staffchat