mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
Begin (broken) modularization
This commit is contained in:
1 parent
b3a0d71ba8
commit
ad8089855c
56 files changed
+553
-369
No files matched your search
@@ -0,0 +1,116 @@
|
||||
package net.draycia.simplechat.managers;
|
||||
|
||||
import co.aikar.commands.CommandManager;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.*;
|
||||
import net.draycia.simplechat.commands.AliasedChannelCommand;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class ChannelManager {
|
||||
|
||||
public ChannelManager(SimpleChat simpleChat) {
|
||||
boolean hasTownChat = false;
|
||||
boolean hasNationChat = false;
|
||||
boolean hasAllianceChat = false;
|
||||
boolean hasPartyChat = false;
|
||||
|
||||
for (String key : simpleChat.getConfig().getConfigurationSection("channels").getKeys(false)) {
|
||||
ChatChannel channel;
|
||||
|
||||
ConfigurationSection section = simpleChat.getConfig().getConfigurationSection("channels").getConfigurationSection(key);
|
||||
|
||||
String name = section.getString("name");
|
||||
|
||||
if (name != null && name.length() > 16) {
|
||||
simpleChat.getLogger().warning("Channel name [" + name + "] too long! Max length: 16.");
|
||||
simpleChat.getLogger().warning("Skipping channel, please check your settings!");
|
||||
continue;
|
||||
}
|
||||
|
||||
String type = section.getString("type");
|
||||
|
||||
if (type == null) {
|
||||
type = "normal";
|
||||
}
|
||||
|
||||
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!");
|
||||
}
|
||||
}
|
||||
|
||||
public void reloadChannels() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CommandManager {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
private BukkitCommandManager commandManager;
|
||||
|
||||
public CommandManager(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
|
||||
commandManager = new BukkitCommandManager(simpleChat);
|
||||
|
||||
commandManager.enableUnstableAPI("help");
|
||||
|
||||
commandManager.getCommandCompletions().registerCompletion("chatchannel", (context) -> {
|
||||
ChatUser user = simpleChat.getUserService().wrap(context.getPlayer());
|
||||
|
||||
ArrayList<String> completions = new ArrayList<>();
|
||||
|
||||
for (ChatChannel chatChannel : simpleChat.getChannels()) {
|
||||
if (chatChannel.canPlayerUse(user)) {
|
||||
completions.add(chatChannel.getName());
|
||||
}
|
||||
}
|
||||
|
||||
return completions;
|
||||
});
|
||||
|
||||
commandManager.getCommandCompletions().registerCompletion("channel", (context) -> {
|
||||
ArrayList<String> completions = new ArrayList<>();
|
||||
|
||||
for (ChatChannel chatChannel : simpleChat.getChannels()) {
|
||||
completions.add(chatChannel.getName());
|
||||
}
|
||||
|
||||
return completions;
|
||||
});
|
||||
|
||||
commandManager.getCommandContexts().registerContext(ChatChannel.class, (context) -> {
|
||||
String name = context.popFirstArg();
|
||||
|
||||
for (ChatChannel chatChannel : simpleChat.getChannels()) {
|
||||
if (chatChannel.getName().equalsIgnoreCase(name)) {
|
||||
return chatChannel;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
commandManager.getCommandContexts().registerContext(ChatUser.class, (context) -> {
|
||||
return simpleChat.getUserService().wrap(Bukkit.getOfflinePlayer(context.popFirstArg()));
|
||||
});
|
||||
|
||||
commandManager.getCommandConditions().addCondition(ChatChannel.class,"canuse", (context, execution, value) -> {
|
||||
if (value == null) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.cannot-use-channel"));
|
||||
}
|
||||
|
||||
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"));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
commandManager.getCommandConditions().addCondition(ChatChannel.class,"exists", (context, execution, value) -> {
|
||||
if (value == null) {
|
||||
throw new ConditionFailedException(simpleChat.getConfig().getString("language.cannot-use-channel"));
|
||||
}
|
||||
});
|
||||
|
||||
setupCommands(commandManager);
|
||||
}
|
||||
|
||||
private void setupCommands(BukkitCommandManager manager) {
|
||||
manager.registerCommand(new ToggleCommand(simpleChat));
|
||||
manager.registerCommand(new ChannelCommand(simpleChat));
|
||||
manager.registerCommand(new IgnoreCommand(simpleChat));
|
||||
manager.registerCommand(new ChatReloadCommand(simpleChat));
|
||||
manager.registerCommand(new MeCommand(simpleChat));
|
||||
manager.registerCommand(new MessageCommand(simpleChat));
|
||||
manager.registerCommand(new ReplyCommand(simpleChat));
|
||||
manager.registerCommand(new SetColorCommand(simpleChat));
|
||||
manager.registerCommand(new SpyChannelCommand(simpleChat));
|
||||
}
|
||||
|
||||
public co.aikar.commands.CommandManager getCommandManager() {
|
||||
return commandManager;
|
||||
}
|
||||
|
||||
}
|
||||
+167
@@ -0,0 +1,167 @@
|
||||
package net.draycia.simplechat.managers;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import io.github.leonardosnt.bungeechannelapi.BungeeChannelApi;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class PluginMessageManager implements PluginMessageListener {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
private BungeeChannelApi api;
|
||||
|
||||
public PluginMessageManager(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
|
||||
simpleChat.getServer().getMessenger().registerOutgoingPluginChannel(simpleChat, "BungeeCord");
|
||||
simpleChat.getServer().getMessenger().registerIncomingPluginChannel(simpleChat, "BungeeCord", this);
|
||||
|
||||
api = BungeeChannelApi.of(simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!channel.equals("BungeeCord")) {
|
||||
return;
|
||||
}
|
||||
|
||||
ByteArrayDataInput input = ByteStreams.newDataInput(message);
|
||||
|
||||
String subchannel = input.readUTF();
|
||||
short length = input.readShort();
|
||||
byte[] msgbytes = new byte[length];
|
||||
input.readFully(msgbytes);
|
||||
|
||||
ByteArrayDataInput msg = ByteStreams.newDataInput(msgbytes);
|
||||
|
||||
if (subchannel.equals("simplechat:message")) {
|
||||
onMessageReceived(msg);
|
||||
} else if (subchannel.equals("simplechat:component")) {
|
||||
onComponentReceived(msg);
|
||||
} else if (subchannel.equals("simplechat:whisper")) {
|
||||
onWhisperReceived(msg);
|
||||
}
|
||||
}
|
||||
|
||||
private void onMessageReceived(ByteArrayDataInput in) {
|
||||
String chatChannelName = in.readUTF();
|
||||
|
||||
ChatChannel chatChannel = simpleChat.getChannel(chatChannelName);
|
||||
|
||||
if (chatChannel == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
UUID playerUUID;
|
||||
|
||||
try {
|
||||
playerUUID = UUID.fromString(in.readUTF());
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser user = simpleChat.getUserService().wrap(playerUUID);
|
||||
|
||||
String chatMessage = in.readUTF();
|
||||
|
||||
Bukkit.getScheduler().scheduleAsyncDelayedTask(simpleChat, () -> {
|
||||
chatChannel.sendMessage(user, chatMessage, true);
|
||||
});
|
||||
}
|
||||
|
||||
public void onComponentReceived(ByteArrayDataInput in) {
|
||||
String chatChannelName = in.readUTF();
|
||||
|
||||
ChatChannel chatChannel = simpleChat.getChannel(chatChannelName);
|
||||
|
||||
if (chatChannel == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
UUID playerUUID;
|
||||
|
||||
try {
|
||||
playerUUID = UUID.fromString(in.readUTF());
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser user = simpleChat.getUserService().wrap(playerUUID);
|
||||
|
||||
String chatMessage = in.readUTF();
|
||||
|
||||
Bukkit.getScheduler().scheduleAsyncDelayedTask(simpleChat, () -> {
|
||||
chatChannel.sendComponent(user, MiniMessage.get().parse(chatMessage, "br", "\n"));
|
||||
});
|
||||
}
|
||||
|
||||
private void onWhisperReceived(ByteArrayDataInput in) {
|
||||
UUID targetUUID;
|
||||
|
||||
try {
|
||||
targetUUID = UUID.fromString(in.readUTF());
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
String chatMessage = in.readUTF();
|
||||
|
||||
ChatUser target = simpleChat.getUserService().wrap(targetUUID);
|
||||
target.sendMessage(MiniMessage.get().parse(chatMessage, "br", "\n"));
|
||||
}
|
||||
|
||||
public void sendMessage(ChatChannel chatChannel, Player player, String message) {
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(chatChannel.getName());
|
||||
msg.writeUTF(player.getUniqueId().toString());
|
||||
msg.writeUTF(message);
|
||||
|
||||
api.forward("ALL", "simplechat:message", msg.toByteArray());
|
||||
}
|
||||
|
||||
public void sendComponent(ChatChannel chatChannel, Player player, Component component) {
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(chatChannel.getName());
|
||||
msg.writeUTF(player.getUniqueId().toString());
|
||||
msg.writeUTF(MiniMessage.get().serialize(component));
|
||||
|
||||
api.forward("ALL", "simplechat:component", msg.toByteArray());
|
||||
}
|
||||
|
||||
public void sendComponentToPlayer(ChatUser sender, ChatUser target, Component toComponent, Component fromComponent) {
|
||||
api.getPlayerList("ALL").whenComplete((result, error) -> {
|
||||
if (!result.contains(target.asOfflinePlayer().getName())) {
|
||||
return;
|
||||
}
|
||||
|
||||
sender.sendMessage(toComponent);
|
||||
|
||||
if (sender.isShadowMuted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(target.getUUID().toString());
|
||||
msg.writeUTF(MiniMessage.get().serialize(fromComponent));
|
||||
|
||||
api.forward("ALL", "simplechat:whisper", msg.toByteArray());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package net.draycia.simplechat.managers;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import io.lettuce.core.RedisClient;
|
||||
import io.lettuce.core.RedisURI;
|
||||
import io.lettuce.core.pubsub.RedisPubSubListener;
|
||||
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
|
||||
import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.storage.impl.SimpleChatUser;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.UUID;
|
||||
|
||||
public class RedisManager {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
private RedisClient client;
|
||||
private StatefulRedisPubSubConnection<String, String> connection;
|
||||
private RedisPubSubCommands<String, String> sync;
|
||||
|
||||
private Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
private Type userType = new TypeToken<SimpleChatUser>() {}.getType();
|
||||
|
||||
public RedisManager(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
|
||||
String host = simpleChat.getConfig().getString("redis.host");
|
||||
int port = simpleChat.getConfig().getInt("redis.port");
|
||||
int database = simpleChat.getConfig().getInt("redis.database");
|
||||
|
||||
RedisURI uri = RedisURI.Builder.redis(host, port)
|
||||
.withDatabase(database)
|
||||
.build();
|
||||
|
||||
this.client = RedisClient.create(uri);
|
||||
this.connection = client.connectPubSub();
|
||||
this.sync = connection.sync();
|
||||
|
||||
connection.addListener(new RedisPubSubListener<String, String>() {
|
||||
@Override
|
||||
public void message(String channel, String message) {
|
||||
if (channel.equalsIgnoreCase("refreshuser")) {
|
||||
simpleChat.getUserService().refreshUser(UUID.fromString(message));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void message(String pattern, String channel, String message) { }
|
||||
|
||||
@Override
|
||||
public void subscribed(String channel, long count) { }
|
||||
|
||||
@Override
|
||||
public void psubscribed(String pattern, long count) { }
|
||||
|
||||
@Override
|
||||
public void unsubscribed(String channel, long count) { }
|
||||
|
||||
@Override
|
||||
public void punsubscribed(String pattern, long count) { }
|
||||
});
|
||||
|
||||
sync.subscribe("userdata");
|
||||
}
|
||||
|
||||
public void publishUser(SimpleChatUser user) {
|
||||
String data = gson.toJson(user, userType);
|
||||
|
||||
sync.set(user.getUUID().toString(), data);
|
||||
sync.publish("refreshuser", user.getUUID().toString());
|
||||
}
|
||||
|
||||
public SimpleChatUser getUser(UUID uuid) {
|
||||
String value = sync.get(uuid.toString());
|
||||
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return gson.fromJson(value, userType);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user