Bungee and refactoring

This commit is contained in:
Draycia committed 2020-06-24 01:24:40 -07:00
1 parent 707556d9f9
commit 5eefb5dda8
14 files changed
+997 -675

No files matched your search

@@ -0,0 +1,134 @@
package net.draycia.simplechat.managers;
import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.channels.impls.*;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import java.util.HashMap;
public class ChannelManager {
private SimpleChat simpleChat;
public ChannelManager(SimpleChat simpleChat) {
this.simpleChat = simpleChat;
boolean hasTownChat = false;
boolean hasNationChat = false;
boolean hasAllianceChat = false;
boolean hasPartyChat = false;
for (String key : simpleChat.getConfig().getConfigurationSection("channels").getKeys(false)) {
ChatChannel.Builder builder;
ConfigurationSection section = simpleChat.getConfig().getConfigurationSection("channels").getConfigurationSection(key);
if (section.contains("is-town-chat")) {
builder = TownChatChannel.townBuilder(key);
hasTownChat = true;
} else if (section.contains("is-nation-chat")) {
builder = NationChatChannel.nationBuilder(key);
hasNationChat = true;
} else if (section.contains("is-alliance-chat")) {
builder = AllianceChatChannel.allianceBuilder(key);
hasAllianceChat = true;
} else if (section.contains("is-party-chat")) {
builder = PartyChatChannel.partyBuilder(key);
hasPartyChat = true;
} else {
builder = SimpleChatChannel.builder(key);
}
if (section.contains("id")) {
builder.setId(section.getLong("id"));
}
if (section.contains("formats")) {
HashMap<String, String> formats = new HashMap<>();
ConfigurationSection formatSection = section.getConfigurationSection("formats");
for (String group : formatSection.getKeys(false)) {
formats.put(group, formatSection.getString(group));
}
builder.setFormats(formats);
}
if (section.contains("webhook")) {
builder.setWebhook(section.getString("webhook"));
}
if (section.contains("switch-message")) {
builder.setSwitchMessage(section.getString("switch-message"));
}
if (section.contains("distance")) {
builder.setDistance(section.getDouble("distance"));
}
if (section.contains("name")) {
builder.setName(section.getString("name"));
}
if (section.contains("color")) {
builder.setColor(section.getString("color"));
}
if (section.contains("ignorable")) {
builder.setIgnorable(section.getBoolean("ignorable"));
}
if (section.contains("default")) {
builder.setIsDefault(section.getBoolean("default"));
}
if (section.contains("toggle-on-message")) {
builder.setToggleOnMessage(section.getString("toggle-on-message"));
}
if (section.contains("toggle-off-message")) {
builder.setToggleOffMessage(section.getString("toggle-off-message"));
}
ChatChannel channel = builder.build(simpleChat);
if (channel.isTownChat() || channel.isNationChat() || channel.isAllianceChat()) {
if (!Bukkit.getPluginManager().isPluginEnabled("Towny")) {
simpleChat.getLogger().warning("Towny related channel with name [" + channel.getName() + "] found, but Towny isn't installed. Skipping channel.");
continue;
}
}
if (channel.isPartyChat()) {
if (!Bukkit.getPluginManager().isPluginEnabled("mcMMO")) {
simpleChat.getLogger().warning("mcMMO related channel with name [" + channel.getName() + "] found, but mcMMO isn't installed. Skipping channel.");
continue;
}
}
// TODO: register command for each channel
simpleChat.getChannels().add(channel);
}
if (!hasTownChat) {
simpleChat.getLogger().info("Towny installed but no Town channel is setup!");
}
if (!hasNationChat) {
simpleChat.getLogger().info("Towny installed but no Nation channel is setup!");
}
if (!hasAllianceChat) {
simpleChat.getLogger().info("Towny installed but no Alliance channel is setup!");
}
if (!hasPartyChat) {
simpleChat.getLogger().info("mcMMO installed but no Party channel is setup!");
}
}
}
@@ -0,0 +1,61 @@
package net.draycia.simplechat.managers;
import co.aikar.commands.BukkitCommandManager;
import co.aikar.commands.ConditionFailedException;
import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.commands.ChannelCommand;
import net.draycia.simplechat.commands.IgnoreCommand;
import net.draycia.simplechat.commands.ToggleCommand;
import java.util.ArrayList;
public class CommandManager {
private SimpleChat simpleChat;
public CommandManager(SimpleChat simpleChat) {
this.simpleChat = simpleChat;
BukkitCommandManager manager = new BukkitCommandManager(simpleChat);
manager.getCommandCompletions().registerCompletion("chatchannel", (context) -> {
ArrayList<String> completions = new ArrayList<>();
for (ChatChannel chatChannel : simpleChat.getChannels()) {
if (chatChannel.canPlayerUse(context.getPlayer())) {
completions.add(chatChannel.getName());
}
}
return completions;
});
manager.getCommandContexts().registerContext(ChatChannel.class, (context) -> {
String name = context.popFirstArg();
for (ChatChannel chatChannel : simpleChat.getChannels()) {
if (chatChannel.getName().equalsIgnoreCase(name)) {
return chatChannel;
}
}
return null;
});
manager.getCommandConditions().addCondition(ChatChannel.class,"canuse", (context, execution, value) -> {
if (!value.canPlayerUse(context.getIssuer().getPlayer())) {
throw new ConditionFailedException("You cannot use that channel!");
}
});
setupCommands(manager);
}
private void setupCommands(BukkitCommandManager manager) {
manager.registerCommand(new ToggleCommand(simpleChat));
manager.registerCommand(new ChannelCommand(simpleChat));
manager.registerCommand(new IgnoreCommand(simpleChat));
}
}
@@ -0,0 +1,26 @@
package net.draycia.simplechat.managers;
import net.draycia.simplechat.SimpleChat;
import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
public class DiscordManager {
private DiscordApi discordAPI = null;
public DiscordManager(SimpleChat simpleChat) {
if (simpleChat.getConfig().contains("bot-token") && !simpleChat.getConfig().getString("bot-token").isEmpty()) {
try {
discordAPI = new DiscordApiBuilder().setToken(simpleChat.getConfig().getString("bot-token")).login().join();
} catch (IllegalStateException exception) {
simpleChat.getLogger().warning("Unable to start bot. Reason: " + exception.getMessage());
simpleChat.getLogger().warning("If you're getting \"Websocket closed\", check that your bot token is valid!");
}
}
}
public DiscordApi getDiscordAPI() {
return discordAPI;
}
}
@@ -0,0 +1,71 @@
package net.draycia.simplechat.managers;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import java.util.UUID;
public class PluginMessageManager implements PluginMessageListener {
private SimpleChat simpleChat;
public PluginMessageManager(SimpleChat simpleChat) {
this.simpleChat = simpleChat;
simpleChat.getServer().getMessenger().registerIncomingPluginChannel(simpleChat, "BungeeCord", this);
simpleChat.getServer().getMessenger().registerOutgoingPluginChannel(simpleChat, "BungeeCord");
}
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (!channel.equals("BungeeCord")) {
return;
}
ByteArrayDataInput in = ByteStreams.newDataInput(message);
String messageSubChannel = in.readUTF();
if (!messageSubChannel.equalsIgnoreCase("simplechat-message")) {
return;
}
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;
}
OfflinePlayer messageAuthor = Bukkit.getOfflinePlayer(playerUUID);
String chatMessage = in.readUTF();
chatChannel.sendMessage(messageAuthor, chatMessage);
}
public void sendMessage(ChatChannel chatChannel, OfflinePlayer player, String message) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("simplechat-message");
out.writeUTF(chatChannel.getName());
out.writeUTF(player.getUniqueId().toString());
out.writeUTF(message);
}
}