mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 14:38:48 +00:00
Bungee and refactoring
This commit is contained in:
1 parent
707556d9f9
commit
5eefb5dda8
14 files changed
+997
-675
No files matched your search
@@ -1,25 +1,29 @@
|
||||
package net.draycia.simplechat;
|
||||
|
||||
import co.aikar.commands.BukkitCommandManager;
|
||||
import co.aikar.commands.ConditionFailedException;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.channels.impls.*;
|
||||
import net.draycia.simplechat.commands.ChannelCommand;
|
||||
import net.draycia.simplechat.commands.IgnoreCommand;
|
||||
import net.draycia.simplechat.commands.ToggleCommand;
|
||||
import net.draycia.simplechat.listeners.PlayerChatListener;
|
||||
import net.kyori.text.format.TextColor;
|
||||
import net.draycia.simplechat.managers.ChannelManager;
|
||||
import net.draycia.simplechat.managers.CommandManager;
|
||||
import net.draycia.simplechat.managers.DiscordManager;
|
||||
import net.draycia.simplechat.managers.PluginMessageManager;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.javacord.api.DiscordApi;
|
||||
import org.javacord.api.DiscordApiBuilder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class SimpleChat extends JavaPlugin {
|
||||
@@ -32,189 +36,168 @@ public final class SimpleChat extends JavaPlugin {
|
||||
|
||||
private Permission permission;
|
||||
|
||||
private DiscordApi discordAPI = null;
|
||||
private PluginMessageManager pluginMessageManager;
|
||||
private DiscordManager discordManager;
|
||||
private CommandManager commandManager;
|
||||
private ChannelManager channelManager;
|
||||
|
||||
private Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Setup vault and permissions
|
||||
permission = getServer().getServicesManager().getRegistration(Permission.class).getProvider();
|
||||
|
||||
// Ensure config is present to be modified by the user
|
||||
saveDefaultConfig();
|
||||
|
||||
if (getConfig().contains("bot-token") && !getConfig().getString("bot-token").isEmpty()) {
|
||||
try {
|
||||
discordAPI = new DiscordApiBuilder().setToken(getConfig().getString("bot-token")).login().join();
|
||||
} catch (IllegalStateException exception) {
|
||||
getLogger().warning("Unable to start bot. Reason: " + exception.getMessage());
|
||||
getLogger().warning("If you're getting \"Websocket closed\", check that your bot token is valid!");
|
||||
}
|
||||
}
|
||||
|
||||
BukkitCommandManager manager = new BukkitCommandManager(this);
|
||||
|
||||
manager.getCommandCompletions().registerCompletion("chatchannel", (context) -> {
|
||||
ArrayList<String> completions = new ArrayList<>();
|
||||
|
||||
for (ChatChannel chatChannel : channels) {
|
||||
if (chatChannel.canPlayerUse(context.getPlayer())) {
|
||||
completions.add(chatChannel.getName());
|
||||
}
|
||||
}
|
||||
|
||||
return completions;
|
||||
});
|
||||
|
||||
manager.getCommandContexts().registerContext(ChatChannel.class, (context) -> {
|
||||
String name = context.popFirstArg();
|
||||
|
||||
for (ChatChannel chatChannel : channels) {
|
||||
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!");
|
||||
}
|
||||
});
|
||||
|
||||
boolean hasTownChat = false;
|
||||
boolean hasNationChat = false;
|
||||
boolean hasAllianceChat = false;
|
||||
boolean hasPartyChat = false;
|
||||
|
||||
for (String key : getConfig().getConfigurationSection("channels").getKeys(false)) {
|
||||
ChatChannel.Builder builder;
|
||||
|
||||
ConfigurationSection section = 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(TextColor.valueOf(section.getString("color").toUpperCase()));
|
||||
}
|
||||
|
||||
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(this);
|
||||
|
||||
if (channel.isTownChat() || channel.isNationChat() || channel.isAllianceChat()) {
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("Towny")) {
|
||||
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")) {
|
||||
getLogger().warning("mcMMO related channel with name [" + channel.getName() + "] found, but mcMMO isn't installed. Skipping channel.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: register command for each channel
|
||||
channels.add(channel);
|
||||
}
|
||||
|
||||
if (!hasTownChat) {
|
||||
getLogger().info("Towny installed but no Town channel is setup!");
|
||||
}
|
||||
|
||||
if (!hasNationChat) {
|
||||
getLogger().info("Towny installed but no Nation channel is setup!");
|
||||
}
|
||||
|
||||
if (!hasAllianceChat) {
|
||||
getLogger().info("Towny installed but no Alliance channel is setup!");
|
||||
}
|
||||
|
||||
if (!hasPartyChat) {
|
||||
getLogger().info("mcMMO installed but no Party channel is setup!");
|
||||
}
|
||||
// Initialize managers
|
||||
pluginMessageManager = new PluginMessageManager(this);
|
||||
discordManager = new DiscordManager(this);
|
||||
commandManager = new CommandManager(this);
|
||||
channelManager = new ChannelManager(this);
|
||||
|
||||
// Setup listeners
|
||||
setupListeners();
|
||||
setupCommands(manager);
|
||||
|
||||
// Load userChannels
|
||||
File userChannelsFile = new File(getDataFolder(), "userchannels.json");
|
||||
|
||||
try {
|
||||
if (!userChannelsFile.exists()) {
|
||||
userChannelsFile.getParentFile().mkdirs();
|
||||
userChannelsFile.createNewFile();
|
||||
} else {
|
||||
try (JsonReader reader = new JsonReader(new FileReader(userChannelsFile))) {
|
||||
Type type = new TypeToken<HashMap<UUID, String>>(){}.getType();
|
||||
HashMap<UUID, String> userChannelsBuffer = gson.fromJson(reader, type);
|
||||
|
||||
if (userChannelsBuffer != null) {
|
||||
for (Map.Entry<UUID, String> entry : userChannelsBuffer.entrySet()) {
|
||||
ChatChannel channel = getChannel(entry.getValue());
|
||||
|
||||
if (channel == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
userChannels.put(entry.getKey(), channel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Load userToggles
|
||||
File userTogglesFile = new File(getDataFolder(), "usertoggles.json");
|
||||
|
||||
try {
|
||||
if (!userTogglesFile.exists()) {
|
||||
userTogglesFile.getParentFile().mkdirs();
|
||||
userTogglesFile.createNewFile();
|
||||
} else {
|
||||
try (JsonReader reader = new JsonReader(new FileReader(userTogglesFile))) {
|
||||
Type type = new TypeToken<HashMap<UUID, ArrayList<String>>>(){}.getType();
|
||||
HashMap<UUID, ArrayList<String>> userTogglesBuffer = gson.fromJson(reader, type);
|
||||
|
||||
if (userTogglesBuffer != null) {
|
||||
userToggles = userTogglesBuffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Load userIgnores
|
||||
File userIgnoresFile = new File(getDataFolder(), "userignores.json");
|
||||
|
||||
try {
|
||||
if (!userIgnoresFile.exists()) {
|
||||
userIgnoresFile.getParentFile().mkdirs();
|
||||
userIgnoresFile.createNewFile();
|
||||
} else {
|
||||
try (JsonReader reader = new JsonReader(new FileReader(userIgnoresFile))) {
|
||||
Type type = new TypeToken<HashMap<UUID, ArrayList<UUID>>>(){}.getType();
|
||||
HashMap<UUID, ArrayList<UUID>> userIgnoresBuffer = gson.fromJson(reader, type);
|
||||
|
||||
if (userIgnoresBuffer != null) {
|
||||
userIgnores = userIgnoresBuffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// Plugin shutdown logic
|
||||
}
|
||||
// Save userChannels
|
||||
File userChannelsFile = new File(getDataFolder(), "userchannels.json");
|
||||
|
||||
private void setupCommands(BukkitCommandManager manager) {
|
||||
manager.registerCommand(new ToggleCommand(this));
|
||||
manager.registerCommand(new ChannelCommand(this));
|
||||
manager.registerCommand(new IgnoreCommand(this));
|
||||
try {
|
||||
if (!userChannelsFile.exists()) {
|
||||
userChannelsFile.getParentFile().mkdirs();
|
||||
userChannelsFile.createNewFile();
|
||||
}
|
||||
|
||||
try (JsonWriter writer = gson.newJsonWriter(new FileWriter(userChannelsFile))) {
|
||||
Type type = new TypeToken<HashMap<UUID, String>>(){}.getType();
|
||||
|
||||
HashMap<UUID, String> userChannelsBuffer = new HashMap<>();
|
||||
|
||||
for (Map.Entry<UUID, ChatChannel> entry : userChannels.entrySet()) {
|
||||
userChannelsBuffer.put(entry.getKey(), entry.getValue().getName());
|
||||
}
|
||||
|
||||
gson.toJson(userChannelsBuffer, type, writer);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Save userToggles
|
||||
File userTogglesFile = new File(getDataFolder(), "usertoggles.json");
|
||||
|
||||
try {
|
||||
if (!userTogglesFile.exists()) {
|
||||
userTogglesFile.getParentFile().mkdirs();
|
||||
userTogglesFile.createNewFile();
|
||||
}
|
||||
|
||||
try (JsonWriter writer = gson.newJsonWriter(new FileWriter(userTogglesFile))) {
|
||||
Type type = new TypeToken<HashMap<UUID, ArrayList<String>>>(){}.getType();
|
||||
gson.toJson(userToggles, type, writer);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Save userIgnores
|
||||
File userIgnoresFile = new File(getDataFolder(), "userignores.json");
|
||||
|
||||
try {
|
||||
if (!userIgnoresFile.exists()) {
|
||||
userIgnoresFile.getParentFile().mkdirs();
|
||||
userIgnoresFile.createNewFile();
|
||||
}
|
||||
|
||||
try (JsonWriter writer = gson.newJsonWriter(new FileWriter(userIgnoresFile))) {
|
||||
Type type = new TypeToken<HashMap<UUID, ArrayList<UUID>>>(){}.getType();
|
||||
gson.toJson(userIgnores, type, writer);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void setupListeners() {
|
||||
getServer().getPluginManager().registerEvents(new PlayerChatListener(this), this);
|
||||
}
|
||||
|
||||
public boolean playerHasPlayerIgnored(Player player, OfflinePlayer target) {
|
||||
public boolean playerHasPlayerIgnored(OfflinePlayer player, OfflinePlayer target) {
|
||||
ArrayList<UUID> ignores = userIgnores.get(player.getUniqueId());
|
||||
|
||||
if (ignores == null) {
|
||||
@@ -274,11 +257,37 @@ public final class SimpleChat extends JavaPlugin {
|
||||
return null;
|
||||
}
|
||||
|
||||
public ChatChannel getChannel(String name) {
|
||||
for (ChatChannel chatChannel : channels) {
|
||||
if (chatChannel.getName().equalsIgnoreCase(name)) {
|
||||
return chatChannel;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public ArrayList<ChatChannel> getChannels() {
|
||||
return channels;
|
||||
}
|
||||
|
||||
public Permission getPermission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
public DiscordApi getDiscordAPI() {
|
||||
return discordAPI;
|
||||
public DiscordManager getDiscordManager() {
|
||||
return discordManager;
|
||||
}
|
||||
|
||||
public CommandManager getCommandManager() {
|
||||
return commandManager;
|
||||
}
|
||||
|
||||
public ChannelManager getChannelManager() {
|
||||
return channelManager;
|
||||
}
|
||||
|
||||
public PluginMessageManager getPluginMessageManager() {
|
||||
return pluginMessageManager;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package net.draycia.simplechat.channels;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.impls.TownChatChannel;
|
||||
import net.kyori.text.format.TextColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.javacord.api.event.message.MessageCreateEvent;
|
||||
|
||||
@@ -22,9 +23,9 @@ public abstract class ChatChannel {
|
||||
public abstract long getChannelId();
|
||||
|
||||
/**
|
||||
* @return The MiniMessage styled format for the player in this channel.
|
||||
* @return The MiniMessage styled format for the group in this channel.
|
||||
*/
|
||||
public abstract String getFormat(Player player);
|
||||
public abstract String getFormat(String group);
|
||||
|
||||
/**
|
||||
* @return The url of the webhook used to send messages to discord for chat bridging.
|
||||
@@ -106,15 +107,14 @@ public abstract class ChatChannel {
|
||||
* @param player The player who is saying the message.
|
||||
* @param message The message to be sent.
|
||||
*/
|
||||
public abstract void sendMessage(Player player, String message);
|
||||
public abstract void sendMessage(OfflinePlayer player, String message);
|
||||
|
||||
public abstract void processDiscordMessage(MessageCreateEvent event);
|
||||
|
||||
//public abstract void sendMessageToMinecraft(String message);
|
||||
|
||||
public static abstract class Builder {
|
||||
public abstract ChatChannel build(SimpleChat simpleChat);
|
||||
public abstract ChatChannel.Builder setColor(TextColor color);
|
||||
public abstract ChatChannel.Builder setColor(String color);
|
||||
public abstract ChatChannel.Builder setId(long id);
|
||||
public abstract ChatChannel.Builder setFormats(Map<String, String> formats);
|
||||
public abstract ChatChannel.Builder setWebhook(String webhook);
|
||||
|
||||
@@ -6,16 +6,15 @@ import com.palmergames.bukkit.towny.object.Resident;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.minidigger.minimessage.text.MiniMessageParser;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.events.ChannelChatEvent;
|
||||
import net.kyori.text.Component;
|
||||
import net.kyori.text.adapter.bukkit.TextAdapter;
|
||||
import net.kyori.text.format.TextColor;
|
||||
import net.kyori.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class AllianceChatChannel extends SimpleChatChannel {
|
||||
@@ -25,10 +24,18 @@ public class AllianceChatChannel extends SimpleChatChannel {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(Player player, String message) {
|
||||
public void sendMessage(OfflinePlayer player, String message) {
|
||||
message = MiniMessageParser.escapeTokens(message);
|
||||
|
||||
String messageFormat = getFormat(player);
|
||||
String group;
|
||||
|
||||
if (player.isOnline()) {
|
||||
group = getSimpleChat().getPermission().getPrimaryGroup(player.getPlayer());
|
||||
} else {
|
||||
group = getSimpleChat().getPermission().getPrimaryGroup(null, player);
|
||||
}
|
||||
|
||||
String messageFormat = getFormat(group);
|
||||
|
||||
ChannelChatEvent event = new ChannelChatEvent(player, this, messageFormat, message);
|
||||
|
||||
@@ -68,6 +75,7 @@ public class AllianceChatChannel extends SimpleChatChannel {
|
||||
System.out.println(LegacyComponentSerializer.INSTANCE.serialize(formattedMessage));
|
||||
|
||||
sendMessageToDiscord(player, message);
|
||||
sendMessageToBungee(player, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -79,107 +87,9 @@ public class AllianceChatChannel extends SimpleChatChannel {
|
||||
return new AllianceChatChannel.Builder(name);
|
||||
}
|
||||
|
||||
public static class Builder extends ChatChannel.Builder {
|
||||
|
||||
private TextColor color = TextColor.WHITE;
|
||||
private long id = -1;
|
||||
private Map<String, String> formats = new HashMap<>();
|
||||
private String webhook = null;
|
||||
private boolean isDefault = false;
|
||||
private boolean ignorable = true;
|
||||
private String name;
|
||||
private double distance = -1;
|
||||
private String switchMessage = "<gray>You are now in <color><channel> <gray>chat!";
|
||||
private String toggleOffMessage = "<gray>You can no longer see <color><channel> <gray>chat!";
|
||||
private String toggleOnMessage = "<gray>You can now see <color><channel> <gray>chat!";
|
||||
|
||||
private Builder() { }
|
||||
|
||||
public static class Builder extends SimpleChatChannel.Builder {
|
||||
private Builder(String name) {
|
||||
this.name = name.toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AllianceChatChannel build(SimpleChat simpleChat) {
|
||||
return new AllianceChatChannel(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setColor(TextColor color) {
|
||||
this.color = color;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setId(long id) {
|
||||
this.id = id;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setFormats(Map<String, String> formats) {
|
||||
this.formats = formats;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setWebhook(String webhook) {
|
||||
this.webhook = webhook;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setIsDefault(boolean aDefault) {
|
||||
this.isDefault = aDefault;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setIgnorable(boolean ignorable) {
|
||||
this.ignorable = ignorable;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setName(String name) {
|
||||
this.name = name;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setDistance(double distance) {
|
||||
this.distance = distance;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setSwitchMessage(String switchMessage) {
|
||||
this.switchMessage = switchMessage;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setToggleOffMessage(String toggleOffMessage) {
|
||||
this.toggleOffMessage = toggleOffMessage;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setToggleOnMessage(String toggleOnMessage) {
|
||||
this.toggleOnMessage = toggleOnMessage;
|
||||
|
||||
return this;
|
||||
super(name);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,16 +7,15 @@ import com.palmergames.bukkit.towny.object.Resident;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.minidigger.minimessage.text.MiniMessageParser;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.events.ChannelChatEvent;
|
||||
import net.kyori.text.Component;
|
||||
import net.kyori.text.adapter.bukkit.TextAdapter;
|
||||
import net.kyori.text.format.TextColor;
|
||||
import net.kyori.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class NationChatChannel extends SimpleChatChannel {
|
||||
@@ -26,10 +25,18 @@ public class NationChatChannel extends SimpleChatChannel {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(Player player, String message) {
|
||||
public void sendMessage(OfflinePlayer player, String message) {
|
||||
message = MiniMessageParser.escapeTokens(message);
|
||||
|
||||
String messageFormat = getFormat(player);
|
||||
String group;
|
||||
|
||||
if (player.isOnline()) {
|
||||
group = getSimpleChat().getPermission().getPrimaryGroup(player.getPlayer());
|
||||
} else {
|
||||
group = getSimpleChat().getPermission().getPrimaryGroup(null, player);
|
||||
}
|
||||
|
||||
String messageFormat = getFormat(group);
|
||||
|
||||
ChannelChatEvent event = new ChannelChatEvent(player, this, messageFormat, message);
|
||||
|
||||
@@ -73,6 +80,7 @@ public class NationChatChannel extends SimpleChatChannel {
|
||||
|
||||
System.out.println(LegacyComponentSerializer.INSTANCE.serialize(formattedMessage));
|
||||
|
||||
sendMessageToBungee(player, message);
|
||||
sendMessageToDiscord(player, message);
|
||||
}
|
||||
|
||||
@@ -80,106 +88,9 @@ public class NationChatChannel extends SimpleChatChannel {
|
||||
return new NationChatChannel.Builder(name);
|
||||
}
|
||||
|
||||
public static class Builder extends ChatChannel.Builder {
|
||||
|
||||
private TextColor color = TextColor.WHITE;
|
||||
private long id = -1;
|
||||
private Map<String, String> formats = new HashMap<>();
|
||||
private String webhook = null;
|
||||
private boolean isDefault = false;
|
||||
private boolean ignorable = true;
|
||||
private String name;
|
||||
private double distance = -1;
|
||||
private String switchMessage = "<gray>You are now in <color><channel> <gray>chat!";
|
||||
private String toggleOffMessage = "<gray>You can no longer see <color><channel> <gray>chat!";
|
||||
private String toggleOnMessage = "<gray>You can now see <color><channel> <gray>chat!";
|
||||
|
||||
private Builder() { }
|
||||
|
||||
public static class Builder extends SimpleChatChannel.Builder {
|
||||
private Builder(String name) {
|
||||
this.name = name.toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NationChatChannel build(SimpleChat simpleChat) {
|
||||
return new NationChatChannel(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setColor(TextColor color) {
|
||||
this.color = color;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setId(long id) {
|
||||
this.id = id;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setFormats(Map<String, String> formats) {
|
||||
this.formats = formats;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setWebhook(String webhook) {
|
||||
this.webhook = webhook;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setIsDefault(boolean aDefault) {
|
||||
this.isDefault = aDefault;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setIgnorable(boolean ignorable) {
|
||||
this.ignorable = ignorable;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setName(String name) {
|
||||
this.name = name;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setDistance(double distance) {
|
||||
this.distance = distance;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setSwitchMessage(String switchMessage) {
|
||||
this.switchMessage = switchMessage;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setToggleOffMessage(String toggleOffMessage) {
|
||||
this.toggleOffMessage = toggleOffMessage;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setToggleOnMessage(String toggleOnMessage) {
|
||||
this.toggleOnMessage = toggleOnMessage;
|
||||
|
||||
return this;
|
||||
super(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,16 +4,15 @@ import com.gmail.nossr50.api.PartyAPI;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.minidigger.minimessage.text.MiniMessageParser;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.events.ChannelChatEvent;
|
||||
import net.kyori.text.Component;
|
||||
import net.kyori.text.adapter.bukkit.TextAdapter;
|
||||
import net.kyori.text.format.TextColor;
|
||||
import net.kyori.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PartyChatChannel extends SimpleChatChannel {
|
||||
@@ -23,10 +22,18 @@ public class PartyChatChannel extends SimpleChatChannel {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(Player player, String message) {
|
||||
public void sendMessage(OfflinePlayer player, String message) {
|
||||
message = MiniMessageParser.escapeTokens(message);
|
||||
|
||||
String messageFormat = getFormat(player);
|
||||
String group;
|
||||
|
||||
if (player.isOnline()) {
|
||||
group = getSimpleChat().getPermission().getPrimaryGroup(player.getPlayer());
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
String messageFormat = getFormat(group);
|
||||
|
||||
ChannelChatEvent event = new ChannelChatEvent(player, this, messageFormat, message);
|
||||
|
||||
@@ -42,9 +49,9 @@ public class PartyChatChannel extends SimpleChatChannel {
|
||||
|
||||
Component formattedMessage = /*TextUtilsKt.removeEscape(*/MiniMessageParser.parseFormat(messageFormat)/*, '\\')*/;
|
||||
|
||||
if (PartyAPI.inParty(player)) {
|
||||
if (PartyAPI.inParty((Player)player)) {
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
if (!PartyAPI.inSameParty(player, onlinePlayer)) {
|
||||
if (!PartyAPI.inSameParty((Player)player, onlinePlayer)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -68,106 +75,9 @@ public class PartyChatChannel extends SimpleChatChannel {
|
||||
return new PartyChatChannel.Builder(name);
|
||||
}
|
||||
|
||||
public static class Builder extends ChatChannel.Builder {
|
||||
|
||||
private TextColor color = TextColor.WHITE;
|
||||
private long id = -1;
|
||||
private Map<String, String> formats = new HashMap<>();
|
||||
private String webhook = null;
|
||||
private boolean isDefault = false;
|
||||
private boolean ignorable = true;
|
||||
private String name;
|
||||
private double distance = -1;
|
||||
private String switchMessage = "<gray>You are now in <color><channel> <gray>chat!";
|
||||
private String toggleOffMessage = "<gray>You can no longer see <color><channel> <gray>chat!";
|
||||
private String toggleOnMessage = "<gray>You can now see <color><channel> <gray>chat!";
|
||||
|
||||
private Builder() { }
|
||||
|
||||
public static class Builder extends SimpleChatChannel.Builder {
|
||||
private Builder(String name) {
|
||||
this.name = name.toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PartyChatChannel build(SimpleChat simpleChat) {
|
||||
return new PartyChatChannel(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setColor(TextColor color) {
|
||||
this.color = color;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setId(long id) {
|
||||
this.id = id;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setFormats(Map<String, String> formats) {
|
||||
this.formats = formats;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setWebhook(String webhook) {
|
||||
this.webhook = webhook;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setIsDefault(boolean aDefault) {
|
||||
this.isDefault = aDefault;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setIgnorable(boolean ignorable) {
|
||||
this.ignorable = ignorable;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setName(String name) {
|
||||
this.name = name;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setDistance(double distance) {
|
||||
this.distance = distance;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setSwitchMessage(String switchMessage) {
|
||||
this.switchMessage = switchMessage;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setToggleOffMessage(String toggleOffMessage) {
|
||||
this.toggleOffMessage = toggleOffMessage;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setToggleOnMessage(String toggleOnMessage) {
|
||||
this.toggleOnMessage = toggleOnMessage;
|
||||
|
||||
return this;
|
||||
super(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,28 +1,24 @@
|
||||
package net.draycia.simplechat.channels.impls;
|
||||
|
||||
import club.minnced.discord.webhook.WebhookClient;
|
||||
import club.minnced.discord.webhook.send.WebhookMessageBuilder;
|
||||
import com.gmail.nossr50.api.PartyAPI;
|
||||
import com.palmergames.bukkit.towny.TownyAPI;
|
||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||
import com.palmergames.bukkit.towny.object.Nation;
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.minidigger.minimessage.text.MiniMessageParser;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.events.ChannelChatEvent;
|
||||
import net.draycia.simplechat.util.DiscordWebhook;
|
||||
import net.kyori.text.Component;
|
||||
import net.kyori.text.adapter.bukkit.TextAdapter;
|
||||
import net.kyori.text.format.TextColor;
|
||||
import net.kyori.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.javacord.api.entity.channel.ServerTextChannel;
|
||||
import org.javacord.api.entity.message.MessageAuthor;
|
||||
import org.javacord.api.entity.permission.Role;
|
||||
import org.javacord.api.event.message.MessageCreateEvent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -41,7 +37,7 @@ public class SimpleChatChannel extends ChatChannel {
|
||||
private String toggleOffMessage;
|
||||
private String toggleOnMessage;
|
||||
|
||||
private WebhookClient webhookClient = null;
|
||||
private DiscordWebhook discordWebhook = null;
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
@@ -62,15 +58,19 @@ public class SimpleChatChannel extends ChatChannel {
|
||||
|
||||
this.simpleChat = simpleChat;
|
||||
|
||||
if (getChannelId() > 0 && simpleChat.getDiscordAPI() != null) {
|
||||
simpleChat.getDiscordAPI().addMessageCreateListener(this::processDiscordMessage);
|
||||
if (getChannelId() > 0 && simpleChat.getDiscordManager().getDiscordAPI() != null) {
|
||||
simpleChat.getDiscordManager().getDiscordAPI().addMessageCreateListener(this::processDiscordMessage);
|
||||
}
|
||||
|
||||
if (getWebhook() != null) {
|
||||
webhookClient = WebhookClient.withUrl(getWebhook());
|
||||
discordWebhook = new DiscordWebhook(getWebhook());
|
||||
}
|
||||
}
|
||||
|
||||
SimpleChat getSimpleChat() {
|
||||
return simpleChat;
|
||||
}
|
||||
|
||||
private String getDiscordFormatting() {
|
||||
return formats.getOrDefault("discord-to-mc", "<gray>[<blue>Discord<gray>] <username><white>: <message>");
|
||||
}
|
||||
@@ -83,7 +83,7 @@ public class SimpleChatChannel extends ChatChannel {
|
||||
|
||||
@Override
|
||||
public void processDiscordMessage(MessageCreateEvent event) {
|
||||
if (event.getChannel().getId() != getChannelId() || !event.getMessageAuthor().isRegularUser()) {
|
||||
if (event.getChannel().getId() != getChannelId() || event.getMessageAuthor().isRegularUser()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -114,12 +114,18 @@ public class SimpleChatChannel extends ChatChannel {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(Player player, String message) {
|
||||
public void sendMessage(OfflinePlayer player, String message) {
|
||||
message = MiniMessageParser.escapeTokens(message);
|
||||
|
||||
String messageFormat = getFormat(player);
|
||||
String group;
|
||||
|
||||
System.out.println(messageFormat);
|
||||
if (player.isOnline()) {
|
||||
group = simpleChat.getPermission().getPrimaryGroup(player.getPlayer());
|
||||
} else {
|
||||
group = simpleChat.getPermission().getPrimaryGroup(null, player);
|
||||
}
|
||||
|
||||
String messageFormat = getFormat(group);
|
||||
|
||||
ChannelChatEvent event = new ChannelChatEvent(player, this, messageFormat, message);
|
||||
|
||||
@@ -146,30 +152,39 @@ public class SimpleChatChannel extends ChatChannel {
|
||||
|
||||
System.out.println(LegacyComponentSerializer.INSTANCE.serialize(formattedMessage));
|
||||
|
||||
sendMessageToBungee(player, message);
|
||||
sendMessageToDiscord(player, message);
|
||||
}
|
||||
|
||||
public void sendMessageToDiscord(Player player, String message) {
|
||||
if (webhookClient == null) {
|
||||
public void sendMessageToDiscord(OfflinePlayer player, String message) {
|
||||
if (discordWebhook == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
WebhookMessageBuilder builder = new WebhookMessageBuilder();
|
||||
discordWebhook.setUsername(player.getName());
|
||||
discordWebhook.setContent(message.replace("@", "@\u200B"));
|
||||
discordWebhook.setAvatarUrl("https://minotar.net/helm/" + player.getUniqueId().toString() + "/100.png");
|
||||
|
||||
builder.setUsername(player.getName());
|
||||
builder.setContent(message.replace("@", "@\u200B"));
|
||||
builder.setAvatarUrl("https://minotar.net/helm/" + player.getUniqueId().toString() + "/100.png");
|
||||
|
||||
webhookClient.send(builder.build());
|
||||
try {
|
||||
discordWebhook.execute();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canUserSeeMessage(Player sender, Player target) {
|
||||
public void sendMessageToBungee(OfflinePlayer player, String message) {
|
||||
simpleChat.getPluginMessageManager().sendMessage(this, player, message);
|
||||
}
|
||||
|
||||
public boolean canUserSeeMessage(OfflinePlayer sender, Player target) {
|
||||
if (!target.hasPermission("simplechat.see." + getName().toLowerCase())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (getDistance() > 0 && target.getLocation().distance(sender.getLocation()) > getDistance()) {
|
||||
return false;
|
||||
if (sender instanceof Player) {
|
||||
if (getDistance() > 0 && target.getLocation().distance(((Player)sender).getLocation()) > getDistance()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (isIgnorable()) {
|
||||
@@ -200,9 +215,7 @@ public class SimpleChatChannel extends ChatChannel {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFormat(Player player) {
|
||||
String group = simpleChat.getPermission().getPrimaryGroup(player);
|
||||
|
||||
public String getFormat(String group) {
|
||||
return formats.getOrDefault(group, formats.getOrDefault("default", "<white><%player_displayname%<white>> <message>"));
|
||||
}
|
||||
|
||||
@@ -266,7 +279,7 @@ public class SimpleChatChannel extends ChatChannel {
|
||||
|
||||
private Builder() { }
|
||||
|
||||
private Builder(String name) {
|
||||
Builder(String name) {
|
||||
this.name = name.toLowerCase();
|
||||
}
|
||||
|
||||
@@ -282,6 +295,11 @@ public class SimpleChatChannel extends ChatChannel {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setColor(String color) {
|
||||
return setColor(TextColor.valueOf(color.toUpperCase()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setId(long id) {
|
||||
this.id = id;
|
||||
|
||||
@@ -7,16 +7,15 @@ import com.palmergames.bukkit.towny.object.Town;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.minidigger.minimessage.text.MiniMessageParser;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.events.ChannelChatEvent;
|
||||
import net.kyori.text.Component;
|
||||
import net.kyori.text.adapter.bukkit.TextAdapter;
|
||||
import net.kyori.text.format.TextColor;
|
||||
import net.kyori.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TownChatChannel extends SimpleChatChannel {
|
||||
@@ -26,10 +25,18 @@ public class TownChatChannel extends SimpleChatChannel {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(Player player, String message) {
|
||||
public void sendMessage(OfflinePlayer player, String message) {
|
||||
message = MiniMessageParser.escapeTokens(message);
|
||||
|
||||
String messageFormat = getFormat(player);
|
||||
String group;
|
||||
|
||||
if (player.isOnline()) {
|
||||
group = getSimpleChat().getPermission().getPrimaryGroup(player.getPlayer());
|
||||
} else {
|
||||
group = getSimpleChat().getPermission().getPrimaryGroup(null, player);
|
||||
}
|
||||
|
||||
String messageFormat = getFormat(group);
|
||||
|
||||
ChannelChatEvent event = new ChannelChatEvent(player, this, messageFormat, message);
|
||||
|
||||
@@ -73,6 +80,7 @@ public class TownChatChannel extends SimpleChatChannel {
|
||||
|
||||
System.out.println(LegacyComponentSerializer.INSTANCE.serialize(formattedMessage));
|
||||
|
||||
sendMessageToBungee(player, message);
|
||||
sendMessageToDiscord(player, message);
|
||||
}
|
||||
|
||||
@@ -80,106 +88,9 @@ public class TownChatChannel extends SimpleChatChannel {
|
||||
return new TownChatChannel.Builder(name);
|
||||
}
|
||||
|
||||
public static class Builder extends ChatChannel.Builder {
|
||||
|
||||
private TextColor color = TextColor.WHITE;
|
||||
private long id = -1;
|
||||
private Map<String, String> formats = new HashMap<>();
|
||||
private String webhook = null;
|
||||
private boolean isDefault = false;
|
||||
private boolean ignorable = true;
|
||||
private String name;
|
||||
private double distance = -1;
|
||||
private String switchMessage = "<gray>You are now in <color><channel> <gray>chat!";
|
||||
private String toggleOffMessage = "<gray>You can no longer see <color><channel> <gray>chat!";
|
||||
private String toggleOnMessage = "<gray>You can now see <color><channel> <gray>chat!";
|
||||
|
||||
private Builder() { }
|
||||
|
||||
public static class Builder extends SimpleChatChannel.Builder {
|
||||
private Builder(String name) {
|
||||
this.name = name.toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TownChatChannel build(SimpleChat simpleChat) {
|
||||
return new TownChatChannel(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setColor(TextColor color) {
|
||||
this.color = color;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setId(long id) {
|
||||
this.id = id;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setFormats(Map<String, String> formats) {
|
||||
this.formats = formats;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setWebhook(String webhook) {
|
||||
this.webhook = webhook;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setIsDefault(boolean aDefault) {
|
||||
this.isDefault = aDefault;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setIgnorable(boolean ignorable) {
|
||||
this.ignorable = ignorable;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setName(String name) {
|
||||
this.name = name;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setDistance(double distance) {
|
||||
this.distance = distance;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setSwitchMessage(String switchMessage) {
|
||||
this.switchMessage = switchMessage;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setToggleOffMessage(String toggleOffMessage) {
|
||||
this.toggleOffMessage = toggleOffMessage;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setToggleOnMessage(String toggleOnMessage) {
|
||||
this.toggleOnMessage = toggleOnMessage;
|
||||
|
||||
return this;
|
||||
super(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.draycia.simplechat.events;
|
||||
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
@@ -11,12 +12,12 @@ public class ChannelChatEvent extends Event implements Cancellable {
|
||||
private static final HandlerList HANDLERS_LIST = new HandlerList();
|
||||
private boolean isCancelled = false;
|
||||
|
||||
private Player player;
|
||||
private OfflinePlayer player;
|
||||
private ChatChannel chatChannel;
|
||||
private String format;
|
||||
private String message;
|
||||
|
||||
public ChannelChatEvent(Player player, ChatChannel chatChannel, String format, String message) {
|
||||
public ChannelChatEvent(OfflinePlayer player, ChatChannel chatChannel, String format, String message) {
|
||||
super(true);
|
||||
|
||||
this.player = player;
|
||||
@@ -44,7 +45,7 @@ public class ChannelChatEvent extends Event implements Cancellable {
|
||||
return HANDLERS_LIST;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
public OfflinePlayer getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,391 @@
|
||||
package net.draycia.simplechat.util;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.awt.Color;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Array;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Class used to execute Discord Webhooks with low effort
|
||||
*/
|
||||
public class DiscordWebhook {
|
||||
|
||||
private final String url;
|
||||
private String content;
|
||||
private String username;
|
||||
private String avatarUrl;
|
||||
private boolean tts;
|
||||
private List<EmbedObject> embeds = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Constructs a new DiscordWebhook instance
|
||||
*
|
||||
* @param url The webhook URL obtained in Discord
|
||||
*/
|
||||
public DiscordWebhook(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public void setAvatarUrl(String avatarUrl) {
|
||||
this.avatarUrl = avatarUrl;
|
||||
}
|
||||
|
||||
public void setTts(boolean tts) {
|
||||
this.tts = tts;
|
||||
}
|
||||
|
||||
public void addEmbed(EmbedObject embed) {
|
||||
this.embeds.add(embed);
|
||||
}
|
||||
|
||||
public void execute() throws IOException {
|
||||
if (this.content == null && this.embeds.isEmpty()) {
|
||||
throw new IllegalArgumentException("Set content or add at least one EmbedObject");
|
||||
}
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
|
||||
json.put("content", this.content);
|
||||
json.put("username", this.username);
|
||||
json.put("avatar_url", this.avatarUrl);
|
||||
json.put("tts", this.tts);
|
||||
|
||||
if (!this.embeds.isEmpty()) {
|
||||
List<JSONObject> embedObjects = new ArrayList<>();
|
||||
|
||||
for (EmbedObject embed : this.embeds) {
|
||||
JSONObject jsonEmbed = new JSONObject();
|
||||
|
||||
jsonEmbed.put("title", embed.getTitle());
|
||||
jsonEmbed.put("description", embed.getDescription());
|
||||
jsonEmbed.put("url", embed.getUrl());
|
||||
|
||||
if (embed.getColor() != null) {
|
||||
Color color = embed.getColor();
|
||||
int rgb = color.getRed();
|
||||
rgb = (rgb << 8) + color.getGreen();
|
||||
rgb = (rgb << 8) + color.getBlue();
|
||||
|
||||
jsonEmbed.put("color", rgb);
|
||||
}
|
||||
|
||||
EmbedObject.Footer footer = embed.getFooter();
|
||||
EmbedObject.Image image = embed.getImage();
|
||||
EmbedObject.Thumbnail thumbnail = embed.getThumbnail();
|
||||
EmbedObject.Author author = embed.getAuthor();
|
||||
List<EmbedObject.Field> fields = embed.getFields();
|
||||
|
||||
if (footer != null) {
|
||||
JSONObject jsonFooter = new JSONObject();
|
||||
|
||||
jsonFooter.put("text", footer.getText());
|
||||
jsonFooter.put("icon_url", footer.getIconUrl());
|
||||
jsonEmbed.put("footer", jsonFooter);
|
||||
}
|
||||
|
||||
if (image != null) {
|
||||
JSONObject jsonImage = new JSONObject();
|
||||
|
||||
jsonImage.put("url", image.getUrl());
|
||||
jsonEmbed.put("image", jsonImage);
|
||||
}
|
||||
|
||||
if (thumbnail != null) {
|
||||
JSONObject jsonThumbnail = new JSONObject();
|
||||
|
||||
jsonThumbnail.put("url", thumbnail.getUrl());
|
||||
jsonEmbed.put("thumbnail", jsonThumbnail);
|
||||
}
|
||||
|
||||
if (author != null) {
|
||||
JSONObject jsonAuthor = new JSONObject();
|
||||
|
||||
jsonAuthor.put("name", author.getName());
|
||||
jsonAuthor.put("url", author.getUrl());
|
||||
jsonAuthor.put("icon_url", author.getIconUrl());
|
||||
jsonEmbed.put("author", jsonAuthor);
|
||||
}
|
||||
|
||||
List<JSONObject> jsonFields = new ArrayList<>();
|
||||
for (EmbedObject.Field field : fields) {
|
||||
JSONObject jsonField = new JSONObject();
|
||||
|
||||
jsonField.put("name", field.getName());
|
||||
jsonField.put("value", field.getValue());
|
||||
jsonField.put("inline", field.isInline());
|
||||
|
||||
jsonFields.add(jsonField);
|
||||
}
|
||||
|
||||
jsonEmbed.put("fields", jsonFields.toArray());
|
||||
embedObjects.add(jsonEmbed);
|
||||
}
|
||||
|
||||
json.put("embeds", embedObjects.toArray());
|
||||
}
|
||||
|
||||
URL url = new URL(this.url);
|
||||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
||||
connection.addRequestProperty("Content-Type", "application/json");
|
||||
connection.addRequestProperty("User-Agent", "Java-DiscordWebhook-BY-Gelox_");
|
||||
connection.setDoOutput(true);
|
||||
connection.setRequestMethod("POST");
|
||||
|
||||
OutputStream stream = connection.getOutputStream();
|
||||
stream.write(json.toString().getBytes());
|
||||
stream.flush();
|
||||
stream.close();
|
||||
|
||||
connection.getInputStream().close(); //I'm not sure why but it doesn't work without getting the InputStream
|
||||
connection.disconnect();
|
||||
}
|
||||
|
||||
public static class EmbedObject {
|
||||
private String title;
|
||||
private String description;
|
||||
private String url;
|
||||
private Color color;
|
||||
|
||||
private Footer footer;
|
||||
private Thumbnail thumbnail;
|
||||
private Image image;
|
||||
private Author author;
|
||||
private List<Field> fields = new ArrayList<>();
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public Footer getFooter() {
|
||||
return footer;
|
||||
}
|
||||
|
||||
public Thumbnail getThumbnail() {
|
||||
return thumbnail;
|
||||
}
|
||||
|
||||
public Image getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public Author getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public List<Field> getFields() {
|
||||
return fields;
|
||||
}
|
||||
|
||||
public EmbedObject setTitle(String title) {
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EmbedObject setDescription(String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EmbedObject setUrl(String url) {
|
||||
this.url = url;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EmbedObject setColor(Color color) {
|
||||
this.color = color;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EmbedObject setFooter(String text, String icon) {
|
||||
this.footer = new Footer(text, icon);
|
||||
return this;
|
||||
}
|
||||
|
||||
public EmbedObject setThumbnail(String url) {
|
||||
this.thumbnail = new Thumbnail(url);
|
||||
return this;
|
||||
}
|
||||
|
||||
public EmbedObject setImage(String url) {
|
||||
this.image = new Image(url);
|
||||
return this;
|
||||
}
|
||||
|
||||
public EmbedObject setAuthor(String name, String url, String icon) {
|
||||
this.author = new Author(name, url, icon);
|
||||
return this;
|
||||
}
|
||||
|
||||
public EmbedObject addField(String name, String value, boolean inline) {
|
||||
this.fields.add(new Field(name, value, inline));
|
||||
return this;
|
||||
}
|
||||
|
||||
private class Footer {
|
||||
private String text;
|
||||
private String iconUrl;
|
||||
|
||||
private Footer(String text, String iconUrl) {
|
||||
this.text = text;
|
||||
this.iconUrl = iconUrl;
|
||||
}
|
||||
|
||||
private String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
private String getIconUrl() {
|
||||
return iconUrl;
|
||||
}
|
||||
}
|
||||
|
||||
private class Thumbnail {
|
||||
private String url;
|
||||
|
||||
private Thumbnail(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
private String getUrl() {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
private class Image {
|
||||
private String url;
|
||||
|
||||
private Image(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
private String getUrl() {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
private class Author {
|
||||
private String name;
|
||||
private String url;
|
||||
private String iconUrl;
|
||||
|
||||
private Author(String name, String url, String iconUrl) {
|
||||
this.name = name;
|
||||
this.url = url;
|
||||
this.iconUrl = iconUrl;
|
||||
}
|
||||
|
||||
private String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
private String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
private String getIconUrl() {
|
||||
return iconUrl;
|
||||
}
|
||||
}
|
||||
|
||||
private class Field {
|
||||
private String name;
|
||||
private String value;
|
||||
private boolean inline;
|
||||
|
||||
private Field(String name, String value, boolean inline) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
this.inline = inline;
|
||||
}
|
||||
|
||||
private String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
private String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
private boolean isInline() {
|
||||
return inline;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class JSONObject {
|
||||
|
||||
private final HashMap<String, Object> map = new HashMap<>();
|
||||
|
||||
void put(String key, Object value) {
|
||||
if (value != null) {
|
||||
map.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
|
||||
builder.append("{");
|
||||
|
||||
int i = 0;
|
||||
for (Map.Entry<String, Object> entry : entrySet) {
|
||||
Object val = entry.getValue();
|
||||
builder.append(quote(entry.getKey())).append(":");
|
||||
|
||||
if (val instanceof String) {
|
||||
builder.append(quote(String.valueOf(val)));
|
||||
} else if (val instanceof Integer) {
|
||||
builder.append(Integer.valueOf(String.valueOf(val)));
|
||||
} else if (val instanceof Boolean) {
|
||||
builder.append(val);
|
||||
} else if (val instanceof JSONObject) {
|
||||
builder.append(val.toString());
|
||||
} else if (val.getClass().isArray()) {
|
||||
builder.append("[");
|
||||
int len = Array.getLength(val);
|
||||
for (int j = 0; j < len; j++) {
|
||||
builder.append(Array.get(val, j).toString()).append(j != len - 1 ? "," : "");
|
||||
}
|
||||
builder.append("]");
|
||||
}
|
||||
|
||||
builder.append(++i == entrySet.size() ? "}" : ",");
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private String quote(String string) {
|
||||
return "\"" + string + "\"";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user