mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Add channel registry and remove custom channels
This commit is contained in:
1 parent
bbeaa3bb85
commit
dceb47e160
19 files changed
+200
-565
No files matched your search
@@ -23,7 +23,6 @@ import java.util.regex.Pattern;
|
||||
|
||||
public final class SimpleChat extends JavaPlugin {
|
||||
|
||||
private ArrayList<ChatChannel> channels = new ArrayList<>();
|
||||
private ArrayList<Pattern> itemPatterns = new ArrayList<>();
|
||||
|
||||
private Permission permission;
|
||||
@@ -128,34 +127,6 @@ public final class SimpleChat extends JavaPlugin {
|
||||
return BungeeCordComponentSerializer.get().deserialize(MineDown.parse(input, placeholders));
|
||||
}
|
||||
|
||||
public ChatChannel getDefaultChannel() {
|
||||
for (ChatChannel channel : channels) {
|
||||
if (channel.isDefault()) {
|
||||
return channel;
|
||||
}
|
||||
}
|
||||
|
||||
return channels.get(0);
|
||||
}
|
||||
|
||||
public ChatChannel getChannel(String name) {
|
||||
if (name == null) {
|
||||
return getDefaultChannel();
|
||||
}
|
||||
|
||||
for (ChatChannel chatChannel : channels) {
|
||||
if (chatChannel.getName().equalsIgnoreCase(name)) {
|
||||
return chatChannel;
|
||||
}
|
||||
}
|
||||
|
||||
return getDefaultChannel();
|
||||
}
|
||||
|
||||
public List<ChatChannel> getChannels() {
|
||||
return channels;
|
||||
}
|
||||
|
||||
public Permission getPermission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package net.draycia.simplechat.channels;
|
||||
|
||||
import co.aikar.commands.CommandManager;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.commands.AliasedChannelCommand;
|
||||
import net.draycia.simplechat.util.Registry;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ChannelRegistry implements Registry<ChatChannel> {
|
||||
|
||||
private Map<String, ChatChannel> registry = new HashMap<>();
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public ChannelRegistry(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean register(@NotNull String key, @NotNull ChatChannel value) {
|
||||
boolean registerSuccessful = registry.putIfAbsent(key, value) == null;
|
||||
|
||||
if (registerSuccessful) {
|
||||
CommandManager commandManager = simpleChat.getCommandManager().getCommandManager();
|
||||
|
||||
commandManager.getCommandReplacements().addReplacement("channelName", value.getAliases());
|
||||
commandManager.registerCommand(new AliasedChannelCommand(simpleChat, value));
|
||||
|
||||
if (value instanceof Listener) {
|
||||
Bukkit.getPluginManager().registerEvents((Listener)value, simpleChat);
|
||||
}
|
||||
}
|
||||
|
||||
return registerSuccessful;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Collection<ChatChannel> values() {
|
||||
return registry.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public ChatChannel get(String key) {
|
||||
return registry.get(key);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -39,6 +39,8 @@ public abstract class ChatChannel {
|
||||
|
||||
public abstract String getKey();
|
||||
|
||||
public abstract String getAliases();
|
||||
|
||||
/**
|
||||
* @return The distance other players must be within to the sender to see messages in this channel.
|
||||
*/
|
||||
@@ -67,33 +69,7 @@ public abstract class ChatChannel {
|
||||
|
||||
public abstract String getToggleOtherOffMessage();
|
||||
|
||||
/**
|
||||
* @return If this channel is Towny's town chat.
|
||||
*/
|
||||
public boolean isTownChat() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return If this channel is Towny's nation chat.
|
||||
*/
|
||||
public boolean isNationChat() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return If this channel is a Towny alliance chat.
|
||||
*/
|
||||
public boolean isAllianceChat() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return If this channel is mcMMO's party chat.
|
||||
*/
|
||||
public boolean isPartyChat() {
|
||||
return false;
|
||||
}
|
||||
public abstract String getCannotUseMessage();
|
||||
|
||||
public abstract boolean firstMatchingGroup();
|
||||
|
||||
|
||||
-50
@@ -1,50 +0,0 @@
|
||||
package net.draycia.simplechat.channels.impls;
|
||||
|
||||
import com.palmergames.bukkit.towny.TownyAPI;
|
||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class AllianceChatChannel extends NationChatChannel implements Listener {
|
||||
|
||||
public AllianceChatChannel(String name, SimpleChat simpleChat) {
|
||||
super(name, simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
|
||||
if (checkSpying && target.asPlayer().hasPermission("simplechat.spy." + getName())) {
|
||||
if (target.getChannelSettings(this).isSpying()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (super.canPlayerSee(sender, target, false) && sender != null) {
|
||||
try {
|
||||
Resident resident = TownyAPI.getInstance().getDataSource().getResident(target.asPlayer().getName());
|
||||
Resident targetResident = TownyAPI.getInstance().getDataSource().getResident(sender.asPlayer().getName());
|
||||
|
||||
if (resident.isAlliedWith(targetResident)) {
|
||||
return true;
|
||||
}
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAllianceChat() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNationChat() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
-125
@@ -1,125 +0,0 @@
|
||||
package net.draycia.simplechat.channels.impls;
|
||||
|
||||
import com.palmergames.bukkit.towny.TownyAPI;
|
||||
import com.palmergames.bukkit.towny.event.NationRemoveTownEvent;
|
||||
import com.palmergames.bukkit.towny.event.TownRemoveResidentEvent;
|
||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||
import com.palmergames.bukkit.towny.object.Nation;
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
public class NationChatChannel extends SimpleChatChannel implements Listener {
|
||||
|
||||
public NationChatChannel(String name, SimpleChat simpleChat) {
|
||||
super(name, simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String processPlaceholders(ChatUser user, String input) {
|
||||
return input.replace("<nation>", getNation(user));
|
||||
}
|
||||
|
||||
private String getNation(ChatUser user) {
|
||||
try {
|
||||
Resident resident = TownyAPI.getInstance().getDataSource().getResident(user.asPlayer().getName());
|
||||
|
||||
if (resident.hasNation()) {
|
||||
return resident.getTown().getNation().getFormattedName();
|
||||
}
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
|
||||
if (checkSpying && target.asPlayer().hasPermission("simplechat.spy." + getName())) {
|
||||
if (target.getChannelSettings(this).isSpying()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (super.canPlayerSee(sender, target, false) && sender != null) {
|
||||
try {
|
||||
Resident resident = TownyAPI.getInstance().getDataSource().getResident(sender.asPlayer().getName());
|
||||
|
||||
if (resident.hasNation()) {
|
||||
Nation nation = resident.getTown().getNation();
|
||||
|
||||
if (nation.hasResident(target.asPlayer().getName())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerUse(ChatUser user) {
|
||||
return super.canPlayerUse(user) && isInNation(user);
|
||||
}
|
||||
|
||||
public boolean isInNation(ChatUser user) {
|
||||
try {
|
||||
return TownyAPI.getInstance().getDataSource().getResident(user.asPlayer().getName()).hasNation();
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onResidentRemove(TownRemoveResidentEvent event) {
|
||||
String name = event.getResident().getName();
|
||||
ChatUser user = getSimpleChat().getUserService().wrap(Bukkit.getOfflinePlayer(name));
|
||||
|
||||
if (user.getSelectedChannel().isAllianceChat()) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onNationRemoveTown(NationRemoveTownEvent event) {
|
||||
for (Resident resident : event.getTown().getResidents()) {
|
||||
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(resident.getName());
|
||||
|
||||
if (!offlinePlayer.isOnline()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ChatUser user = getSimpleChat().getUserService().wrap(offlinePlayer);
|
||||
|
||||
if (user.getSelectedChannel().isAllianceChat()) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
ChatUser user = getSimpleChat().getUserService().wrap(event.getPlayer());
|
||||
|
||||
if (user.getSelectedChannel().isAllianceChat() && !isInNation(user)) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNationChat() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
-82
@@ -1,82 +0,0 @@
|
||||
package net.draycia.simplechat.channels.impls;
|
||||
|
||||
import com.gmail.nossr50.api.PartyAPI;
|
||||
import com.gmail.nossr50.events.party.McMMOPartyChangeEvent;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
public class PartyChatChannel extends SimpleChatChannel implements Listener {
|
||||
|
||||
public PartyChatChannel(String name, SimpleChat simpleChat) {
|
||||
super(name, simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String processPlaceholders(ChatUser user, String input) {
|
||||
String partyName = PartyAPI.getPartyName(user.asPlayer());
|
||||
|
||||
if (partyName == null) {
|
||||
partyName = "NotLoaded";
|
||||
}
|
||||
|
||||
return input.replace("<party>", partyName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
|
||||
if (checkSpying && target.asPlayer().hasPermission("simplechat.spy." + getName())) {
|
||||
if (target.getChannelSettings(this).isSpying()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (super.canPlayerSee(sender, target, false) && sender != null) {
|
||||
return PartyAPI.inSameParty(sender.asPlayer(), target.asPlayer());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerUse(ChatUser user) {
|
||||
return super.canPlayerUse(user) && isInParty(user);
|
||||
}
|
||||
|
||||
private McMMOPartyChangeEvent.EventReason LEFT = McMMOPartyChangeEvent.EventReason.LEFT_PARTY;
|
||||
private McMMOPartyChangeEvent.EventReason KICKED = McMMOPartyChangeEvent.EventReason.KICKED_FROM_PARTY;
|
||||
|
||||
@EventHandler
|
||||
public void onPartyLeave(McMMOPartyChangeEvent event) {
|
||||
if (event.getReason() != LEFT && event.getReason() != KICKED) {
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser user = getSimpleChat().getUserService().wrap(event.getPlayer());
|
||||
|
||||
if (user.getSelectedChannel().isPartyChat()) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
ChatUser user = getSimpleChat().getUserService().wrap(event.getPlayer());
|
||||
|
||||
if (user.getSelectedChannel().isPartyChat() && !isInParty(user)) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isInParty(ChatUser user) {
|
||||
return PartyAPI.inParty(user.asPlayer());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPartyChat() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
+17
-13
@@ -89,19 +89,7 @@ public class SimpleChatChannel extends ChatChannel {
|
||||
|
||||
TextColor userColor = chatUser.getChannelSettings(this).getColor();
|
||||
|
||||
String prefix;
|
||||
|
||||
if (isNationChat()) {
|
||||
prefix = simpleChat.getConfig().getString("spy-prefix-nation");
|
||||
} else if (isTownChat()) {
|
||||
prefix = simpleChat.getConfig().getString("spy-prefix-town");
|
||||
} else if (isPartyChat()) {
|
||||
prefix = simpleChat.getConfig().getString("spy-prefix-mcmmo");
|
||||
} else {
|
||||
prefix = simpleChat.getConfig().getString("spy-prefix");
|
||||
}
|
||||
|
||||
prefix = processPlaceholders(user, prefix);
|
||||
String prefix = processPlaceholders(user, simpleChat.getConfig().getString("spy-prefix"));
|
||||
|
||||
if (userColor == null) {
|
||||
if (isUserSpying(user, chatUser)) {
|
||||
@@ -338,6 +326,11 @@ public class SimpleChatChannel extends ChatChannel {
|
||||
return (String) getSetting("toggle-other-off");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCannotUseMessage() {
|
||||
return (String) getSetting("cannot-use-channel");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldForwardFormatting() {
|
||||
return (Boolean) getSetting("forward-format");
|
||||
@@ -380,4 +373,15 @@ public class SimpleChatChannel extends ChatChannel {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAliases() {
|
||||
String aliases = (String)getSetting("aliases");
|
||||
|
||||
if (aliases == null) {
|
||||
return getKey();
|
||||
}
|
||||
|
||||
return aliases;
|
||||
}
|
||||
|
||||
}
|
||||
-109
@@ -1,109 +0,0 @@
|
||||
package net.draycia.simplechat.channels.impls;
|
||||
|
||||
import com.palmergames.bukkit.towny.TownyAPI;
|
||||
import com.palmergames.bukkit.towny.event.TownRemoveResidentEvent;
|
||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
public class TownChatChannel extends SimpleChatChannel implements Listener {
|
||||
|
||||
public TownChatChannel(String name, SimpleChat simpleChat) {
|
||||
super(name, simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String processPlaceholders(ChatUser user, String input) {
|
||||
return input.replace("<town>", getTown(user));
|
||||
}
|
||||
|
||||
private String getTown(ChatUser user) {
|
||||
try {
|
||||
Resident resident = TownyAPI.getInstance().getDataSource().getResident(user.asPlayer().getName());
|
||||
|
||||
if (resident.hasTown()) {
|
||||
return resident.getTown().getFormattedName();
|
||||
}
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
|
||||
if (checkSpying && target.asPlayer().hasPermission("simplechat.spy." + getName())) {
|
||||
if (target.getChannelSettings(this).isSpying()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (super.canPlayerSee(sender, target, false) && sender != null) {
|
||||
try {
|
||||
Resident resident = TownyAPI.getInstance().getDataSource().getResident(target.asPlayer().getName());
|
||||
|
||||
if (resident.hasTown()) {
|
||||
return resident.getTown().hasResident(sender.asPlayer().getName());
|
||||
}
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerUse(ChatUser user) {
|
||||
if (super.canPlayerUse(user)) {
|
||||
try {
|
||||
return TownyAPI.getInstance().getDataSource().getResident(user.asPlayer().getName()).hasTown();
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isInTown(ChatUser user) {
|
||||
try {
|
||||
return TownyAPI.getInstance().getDataSource().getResident(user.asPlayer().getName()).hasTown();
|
||||
} catch (NotRegisteredException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onResidentRemove(TownRemoveResidentEvent event) {
|
||||
String name = event.getResident().getName();
|
||||
ChatUser user = getSimpleChat().getUserService().wrap(Bukkit.getOfflinePlayer(name));
|
||||
|
||||
if (user.getSelectedChannel().isTownChat()) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
ChatUser user = getSimpleChat().getUserService().wrap(event.getPlayer());
|
||||
|
||||
if (user.getSelectedChannel().isTownChat() && !isInTown(user)) {
|
||||
user.clearSelectedChannel();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTownChat() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-15
@@ -5,10 +5,6 @@ import co.aikar.commands.ConditionFailedException;
|
||||
import co.aikar.commands.annotation.*;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.AllianceChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.NationChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.PartyChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.TownChatChannel;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -30,17 +26,7 @@ public class AliasedChannelCommand extends BaseCommand {
|
||||
ChatUser user = simpleChat.getUserService().wrap(player);
|
||||
|
||||
if (!chatChannel.canPlayerUse(user)) {
|
||||
if (chatChannel.isTownChat() && !((TownChatChannel)chatChannel).isInTown(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.town-cannot-use"));
|
||||
} else if (chatChannel.isNationChat() && !((NationChatChannel)chatChannel).isInNation(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.nation-cannot-use"));
|
||||
} else if (chatChannel.isAllianceChat() && !((AllianceChatChannel)chatChannel).isInNation(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("alliance-cannot-use"));
|
||||
} else if (chatChannel.isPartyChat() && !((PartyChatChannel)chatChannel).isInParty(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("party-cannot-use"));
|
||||
} else {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("cannot-use-channel"));
|
||||
}
|
||||
throw new ConditionFailedException(chatChannel.getCannotUseMessage());
|
||||
}
|
||||
|
||||
if (args == null || args.length == 0) {
|
||||
|
||||
@@ -21,7 +21,6 @@ public class ChatReloadCommand extends BaseCommand {
|
||||
@Default
|
||||
public void baseCommand(CommandSender sender) {
|
||||
simpleChat.reloadConfig();
|
||||
simpleChat.getChannelManager().reloadChannels();
|
||||
simpleChat.reloadPatterns();
|
||||
|
||||
Component message = simpleChat.processMessage(simpleChat.getConfig().getString("language.reloaded"),
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package net.draycia.simplechat.events;
|
||||
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.draycia.simplechat.util.Registry;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ChannelRegisterEvent extends Event {
|
||||
|
||||
/** Bukkit event stuff **/
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
/** Relevant stuff **/
|
||||
private List<ChatChannel> registeredChannels;
|
||||
private Registry<ChatChannel> registry;
|
||||
|
||||
public ChannelRegisterEvent(List<ChatChannel> registeredChannels, Registry<ChatChannel> registry) {
|
||||
this.registeredChannels = registeredChannels;
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
public void register(ChatChannel chatChannel) {
|
||||
registry.register(chatChannel.getKey(), chatChannel);
|
||||
}
|
||||
|
||||
}
|
||||
+39
-81
@@ -1,24 +1,28 @@
|
||||
package net.draycia.simplechat.managers;
|
||||
|
||||
import co.aikar.commands.CommandManager;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChannelRegistry;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.*;
|
||||
import net.draycia.simplechat.commands.AliasedChannelCommand;
|
||||
import net.draycia.simplechat.events.ChannelRegisterEvent;
|
||||
import net.draycia.simplechat.util.Registry;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ChannelManager {
|
||||
|
||||
private ChannelRegistry registry;
|
||||
private String defaultChannelKey = null;
|
||||
|
||||
public ChannelManager(SimpleChat simpleChat) {
|
||||
boolean hasTownChat = false;
|
||||
boolean hasNationChat = false;
|
||||
boolean hasAllianceChat = false;
|
||||
boolean hasPartyChat = false;
|
||||
registry = new ChannelRegistry(simpleChat);
|
||||
List<ChatChannel> channels = new ArrayList<>();
|
||||
|
||||
for (String key : simpleChat.getConfig().getConfigurationSection("channels").getKeys(false)) {
|
||||
ChatChannel channel;
|
||||
ChatChannel channel = new SimpleChatChannel(key, simpleChat);
|
||||
|
||||
ConfigurationSection section = simpleChat.getConfig().getConfigurationSection("channels").getConfigurationSection(key);
|
||||
|
||||
@@ -30,87 +34,41 @@ public class ChannelManager {
|
||||
continue;
|
||||
}
|
||||
|
||||
String type = section.getString("type");
|
||||
channels.add(channel);
|
||||
|
||||
if (type == null) {
|
||||
type = "normal";
|
||||
if (channel.isDefault()) {
|
||||
defaultChannelKey = channel.getKey();
|
||||
}
|
||||
|
||||
switch (type.toLowerCase()) {
|
||||
case "towny":
|
||||
channel = new TownChatChannel(key, simpleChat);
|
||||
hasTownChat = true;
|
||||
break;
|
||||
case "nation":
|
||||
channel = new NationChatChannel(key, simpleChat);
|
||||
hasNationChat = true;
|
||||
break;
|
||||
case "alliance":
|
||||
channel = new AllianceChatChannel(key, simpleChat);
|
||||
hasAllianceChat = true;
|
||||
break;
|
||||
case "party":
|
||||
channel = new PartyChatChannel(key, simpleChat);
|
||||
hasPartyChat = true;
|
||||
break;
|
||||
case "normal":
|
||||
channel = new SimpleChatChannel(key, simpleChat);
|
||||
break;
|
||||
default:
|
||||
simpleChat.getLogger().warning("Invalid channel type for channel [" + key + "]. Skipping channel.");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (channel.isTownChat() || channel.isNationChat() || channel.isAllianceChat()) {
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("Towny")) {
|
||||
simpleChat.getLogger().warning("Towny related channel with key [" + key + "] found, but Towny isn't installed. Skipping channel.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (channel.isPartyChat()) {
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("mcMMO")) {
|
||||
simpleChat.getLogger().warning("mcMMO related channel with key [" + key + "] found, but mcMMO isn't installed. Skipping channel.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
simpleChat.getChannels().add(channel);
|
||||
|
||||
CommandManager commandManager = simpleChat.getCommandManager().getCommandManager();
|
||||
|
||||
String commandName = section.contains("aliases") ? section.getString("aliases") : key;
|
||||
|
||||
commandManager.getCommandReplacements().addReplacement("channelName", commandName);
|
||||
commandManager.registerCommand(new AliasedChannelCommand(simpleChat, channel));
|
||||
|
||||
if (channel instanceof Listener) {
|
||||
Bukkit.getPluginManager().registerEvents((Listener)channel, simpleChat);
|
||||
}
|
||||
}
|
||||
|
||||
boolean townyInstalled = Bukkit.getPluginManager().isPluginEnabled("Towny");
|
||||
boolean mcmmoInstalled = Bukkit.getPluginManager().isPluginEnabled("mcMMO");
|
||||
|
||||
if (!hasTownChat && townyInstalled) {
|
||||
simpleChat.getLogger().info("Towny installed but no Town channel is setup!");
|
||||
}
|
||||
|
||||
if (!hasNationChat && townyInstalled) {
|
||||
simpleChat.getLogger().info("Towny installed but no Nation channel is setup!");
|
||||
}
|
||||
|
||||
if (!hasAllianceChat && townyInstalled) {
|
||||
simpleChat.getLogger().info("Towny installed but no Alliance channel is setup!");
|
||||
}
|
||||
|
||||
if (!hasPartyChat && mcmmoInstalled) {
|
||||
simpleChat.getLogger().info("mcMMO installed but no Party channel is setup!");
|
||||
}
|
||||
Bukkit.getPluginManager().callEvent(new ChannelRegisterEvent(channels, getRegistry()));
|
||||
}
|
||||
|
||||
public void reloadChannels() {
|
||||
public Registry<ChatChannel> getRegistry() {
|
||||
return registry;
|
||||
}
|
||||
|
||||
public ChatChannel getDefaultChannel() {
|
||||
if (defaultChannelKey != null) {
|
||||
return registry.get(defaultChannelKey);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public ChatChannel getChannelOrDefault(String key) {
|
||||
if (key == null) {
|
||||
return getDefaultChannel();
|
||||
}
|
||||
|
||||
ChatChannel channel = registry.get(key);
|
||||
|
||||
if (channel == null) {
|
||||
return getDefaultChannel();
|
||||
}
|
||||
|
||||
return channel;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,10 +3,6 @@ package net.draycia.simplechat.managers;
|
||||
import co.aikar.commands.*;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.AllianceChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.NationChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.PartyChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.TownChatChannel;
|
||||
import net.draycia.simplechat.commands.*;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -30,7 +26,7 @@ public class CommandManager {
|
||||
|
||||
ArrayList<String> completions = new ArrayList<>();
|
||||
|
||||
for (ChatChannel chatChannel : simpleChat.getChannels()) {
|
||||
for (ChatChannel chatChannel : simpleChat.getChannelManager().getRegistry().values()) {
|
||||
if (chatChannel.canPlayerUse(user)) {
|
||||
completions.add(chatChannel.getName());
|
||||
}
|
||||
@@ -42,7 +38,7 @@ public class CommandManager {
|
||||
commandManager.getCommandCompletions().registerCompletion("channel", (context) -> {
|
||||
ArrayList<String> completions = new ArrayList<>();
|
||||
|
||||
for (ChatChannel chatChannel : simpleChat.getChannels()) {
|
||||
for (ChatChannel chatChannel : simpleChat.getChannelManager().getRegistry().values()) {
|
||||
completions.add(chatChannel.getName());
|
||||
}
|
||||
|
||||
@@ -52,7 +48,7 @@ public class CommandManager {
|
||||
commandManager.getCommandContexts().registerContext(ChatChannel.class, (context) -> {
|
||||
String name = context.popFirstArg();
|
||||
|
||||
for (ChatChannel chatChannel : simpleChat.getChannels()) {
|
||||
for (ChatChannel chatChannel : simpleChat.getChannelManager().getRegistry().values()) {
|
||||
if (chatChannel.getName().equalsIgnoreCase(name)) {
|
||||
return chatChannel;
|
||||
}
|
||||
@@ -73,17 +69,7 @@ public class CommandManager {
|
||||
ChatUser user = simpleChat.getUserService().wrap(context.getIssuer().getPlayer());
|
||||
|
||||
if (!value.canPlayerUse(user)) {
|
||||
if (value.isTownChat() && !((TownChatChannel)value).isInTown(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.town-cannot-use"));
|
||||
} else if (value.isNationChat() && !((NationChatChannel)value).isInNation(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.nation-cannot-use"));
|
||||
} else if (value.isAllianceChat() && !((AllianceChatChannel)value).isInNation(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.alliance-cannot-use"));
|
||||
} else if (value.isPartyChat() && !((PartyChatChannel)value).isInParty(user)) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.party-cannot-use"));
|
||||
} else {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.cannot-use-channel"));
|
||||
}
|
||||
throw new ConditionFailedException(value.getCannotUseMessage());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+2
-2
@@ -57,7 +57,7 @@ public class PluginMessageManager implements PluginMessageListener {
|
||||
private void onMessageReceived(ByteArrayDataInput in) {
|
||||
String chatChannelName = in.readUTF();
|
||||
|
||||
ChatChannel chatChannel = simpleChat.getChannel(chatChannelName);
|
||||
ChatChannel chatChannel = simpleChat.getChannelManager().getRegistry().get(chatChannelName);
|
||||
|
||||
if (chatChannel == null) {
|
||||
return;
|
||||
@@ -84,7 +84,7 @@ public class PluginMessageManager implements PluginMessageListener {
|
||||
public void onComponentReceived(ByteArrayDataInput in) {
|
||||
String chatChannelName = in.readUTF();
|
||||
|
||||
ChatChannel chatChannel = simpleChat.getChannel(chatChannelName);
|
||||
ChatChannel chatChannel = simpleChat.getChannelManager().getRegistry().get(chatChannelName);
|
||||
|
||||
if (chatChannel == null) {
|
||||
return;
|
||||
|
||||
+2
-2
@@ -118,13 +118,13 @@ public class MySQLUserService extends UserService {
|
||||
List<DbRow> channelSettings = database.getResults("SELECT * from sc_channel_settings WHERE uuid = ?;", uuid.toString());
|
||||
List<DbRow> ignoredUsers = database.getResults("SELECT * from sc_ignored_users WHERE uuid = ?;", uuid.toString());
|
||||
|
||||
user.setSelectedChannel(simpleChat.getChannel(users.getString("channel")));
|
||||
user.setSelectedChannel(simpleChat.getChannelManager().getRegistry().get(users.getString("channel")));
|
||||
user.setMuted(users.<Boolean>get("muted"));
|
||||
user.setShadowMuted(users.<Boolean>get("shadowmuted"));
|
||||
user.setSpyingWhispers(users.<Boolean>get("spyingwhispers"));
|
||||
|
||||
for (DbRow channelSetting : channelSettings) {
|
||||
ChatChannel chatChannel = simpleChat.getChannel(channelSetting.getString("channel"));
|
||||
ChatChannel chatChannel = simpleChat.getChannelManager().getRegistry().get(channelSetting.getString("channel"));
|
||||
UserChannelSettings settings = user.getChannelSettings(chatChannel);
|
||||
|
||||
settings.setSpying(channelSetting.<Boolean>get("spying"));
|
||||
|
||||
+2
-2
@@ -70,7 +70,7 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
|
||||
|
||||
@Override
|
||||
public ChatChannel getSelectedChannel() {
|
||||
return selectedChannel == null ? simpleChat.getDefaultChannel() : simpleChat.getChannel(selectedChannel);
|
||||
return simpleChat.getChannelManager().getChannelOrDefault(selectedChannel);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -83,7 +83,7 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
|
||||
|
||||
@Override
|
||||
public void clearSelectedChannel() {
|
||||
setSelectedChannel(simpleChat.getDefaultChannel());
|
||||
setSelectedChannel(simpleChat.getChannelManager().getDefaultChannel());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
/*
|
||||
Simple channel based chat plugin for Spigot
|
||||
Copyright (C) 2020 Alexander Söderberg
|
||||
Copyright (C) 2020 Josua Parks (Draycia)
|
||||
|
||||
Modifications made to work with Kyori/Adventure
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package net.draycia.simplechat.util;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface Registry<T> {
|
||||
|
||||
/**
|
||||
* Adds the specified key and value to the registry
|
||||
* @param key The entry's key
|
||||
* @param value The value to insert
|
||||
* @return If successfully registered
|
||||
*/
|
||||
boolean register(@NotNull String key, @NotNull T value);
|
||||
|
||||
/**
|
||||
* Gets the list of registered values
|
||||
* @return The list of registered values
|
||||
*/
|
||||
@NotNull
|
||||
Collection<T> values();
|
||||
|
||||
/**
|
||||
* Obtains an entry associated with the specified key
|
||||
* @param key The key for the entry
|
||||
* @return The entry associated with the key
|
||||
*/
|
||||
@Nullable
|
||||
T get(String key);
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user