From 3a96c853d2ccaa3fbd93cdc33f03c24ae3847d9c Mon Sep 17 00:00:00 2001 From: Paul Reilly Date: Fri, 10 Mar 2023 19:21:03 -0600 Subject: [PATCH] Cyclic Dependency Patch - Fixed cyclic dependencies - Added a TFShoppe class for effective plugin initialization - Registered discord commands, did not before - Added SLF4J loggers for module logging. --- commons/pom.xml | 16 - .../totalfreedommod/ChatManager.java | 2 +- .../totalfreedommod/TotalFreedomMod.java | 34 +- .../totalfreedommod/api/Aggregator.java | 27 ++ .../totalfreedommod/api/Context.java | 16 + .../totalfreedommod/api}/ShopItem.java | 2 +- .../totalfreedommod/api/ShoppeCommons.java | 55 +++ .../totalfreedommod/api/TFD4JCommons.java | 33 ++ .../command/Command_cleardiscordqueue.java | 2 +- .../command/Command_clownfish.java | 2 +- .../command/Command_consolesay.java | 2 +- .../totalfreedommod/command/Command_doom.java | 2 +- .../command/Command_fireball.java | 2 +- .../command/Command_grapplinghook.java | 2 +- .../command/Command_lightningrod.java | 2 +- .../command/Command_linkdiscord.java | 10 +- .../command/Command_loginmessage.java | 2 +- .../command/Command_manageshop.java | 2 +- .../command/Command_myadmin.java | 2 +- .../command/Command_report.java | 2 +- .../command/Command_rideablepearl.java | 2 +- .../command/Command_saconfig.java | 6 +- .../totalfreedommod/command/Command_say.java | 2 +- .../command/Command_stackingpotato.java | 2 +- .../command/Command_trail.java | 2 +- .../command/Command_vanish.java | 4 +- .../totalfreedommod/fun/ItemFun.java | 2 +- .../totalfreedommod/fun/Trailer.java | 2 +- .../totalfreedommod/player/PlayerData.java | 2 +- .../java/me/totalfreedom/discord/Bot.java | 104 ----- .../java/me/totalfreedom/discord/TFD4J.java | 58 ++- .../discord/TFD4JCommonsImpl.java | 372 ++++++++++++++++++ .../me/totalfreedom/discord/TFM_Accessor.java | 18 - .../discord/listener/BukkitNative.java | 17 +- .../totalfreedom/discord/util/Utilities.java | 199 +--------- .../main/java/me/totalfreedom/shop/Shop.java | 75 ++-- .../java/me/totalfreedom/shop/TFShoppe.java | 53 +++ .../java/me/totalfreedom/shop/Votifier.java | 2 + 38 files changed, 721 insertions(+), 418 deletions(-) create mode 100644 commons/src/main/java/me/totalfreedom/totalfreedommod/api/Aggregator.java create mode 100644 commons/src/main/java/me/totalfreedom/totalfreedommod/api/Context.java rename {shop/src/main/java/me/totalfreedom/shop => commons/src/main/java/me/totalfreedom/totalfreedommod/api}/ShopItem.java (98%) create mode 100644 commons/src/main/java/me/totalfreedom/totalfreedommod/api/ShoppeCommons.java create mode 100644 commons/src/main/java/me/totalfreedom/totalfreedommod/api/TFD4JCommons.java create mode 100644 discord/src/main/java/me/totalfreedom/discord/TFD4JCommonsImpl.java delete mode 100644 discord/src/main/java/me/totalfreedom/discord/TFM_Accessor.java create mode 100644 shop/src/main/java/me/totalfreedom/shop/TFShoppe.java diff --git a/commons/pom.xml b/commons/pom.xml index 59102a81..50efb39d 100644 --- a/commons/pom.xml +++ b/commons/pom.xml @@ -46,22 +46,6 @@ compile - - me.totalfreedom - discord - 2023.02 - provided - true - - - - me.totalfreedom - shop - 2023.02 - provided - true - - org.reflections reflections diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java index 68550f01..cfdeb187 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java @@ -151,7 +151,7 @@ public class ChatManager extends FreedomService FLog.info("[ADMIN] " + sender.getName() + " " + display.getTag() + ": " + message, true); if (plugin.dc != null) { - plugin.dc.getUtils().messageAdminChatChannel(sender.getName() + " \u00BB " + message); + plugin.dc.messageAdminChatChannel(sender.getName() + " \u00BB " + message); } server.getOnlinePlayers().stream().filter(player -> plugin.al.isAdmin(player)).forEach(player -> diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java index c248d588..af95cd1d 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java @@ -4,9 +4,12 @@ import java.io.File; import java.io.InputStream; import java.util.Properties; -import me.totalfreedom.discord.TFM_Accessor; import me.totalfreedom.totalfreedommod.admin.ActivityLog; import me.totalfreedom.totalfreedommod.admin.AdminList; +import me.totalfreedom.totalfreedommod.api.Aggregator; +import me.totalfreedom.totalfreedommod.api.ShoppeCommons; +import me.totalfreedom.totalfreedommod.api.TFD4JCommons; +import me.totalfreedom.totalfreedommod.api.VotifierCommons; import me.totalfreedom.totalfreedommod.banning.BanManager; import me.totalfreedom.totalfreedommod.banning.IndefiniteBanList; import me.totalfreedom.totalfreedommod.blocking.BlockBlocker; @@ -74,8 +77,8 @@ public class TotalFreedomMod extends JavaPlugin public CommandLoader cl; // Services public WorldManager wm; - public AdminList al; public ActivityLog acl; + public AdminList al; public RankManager rm; public CommandBlocker cb; public EventBlocker eb; @@ -87,12 +90,12 @@ public class TotalFreedomMod extends JavaPlugin public AntiNuke nu; public AntiSpam as; public PlayerList pl; - public Shop sh; - public Votifier vo; + public ShoppeCommons sh; public SQLite sql; public Announcer an; public ChatManager cm; - public TFD4J dc; + public TFD4JCommons dc; + public Aggregator ag; public PunishmentList pul; public BanManager bm; public IndefiniteBanList im; @@ -266,6 +269,18 @@ public class TotalFreedomMod extends JavaPlugin } } + public void registerDiscord() { + if (Bukkit.getPluginManager().isPluginEnabled("TFD4J")) { + dc = ag.getDiscordContext().getValue(); + } + } + + public void registerShoppe() { + if (Bukkit.getPluginManager().isPluginEnabled("TF-Shoppe")) { + sh = ag.getShoppeContext().getValue(); + } + } + /** * This class is provided to please Codacy. */ @@ -299,18 +314,11 @@ public class TotalFreedomMod extends JavaPlugin as = new AntiSpam(); wr = new WorldRestrictions(); pl = new PlayerList(); - - if (Bukkit.getPluginManager().isPluginEnabled("TF-Shoppe")) { - sh = new Shop(); - vo = new Votifier(); - } + ag = new Aggregator(); an = new Announcer(); cm = new ChatManager(); - if (Bukkit.getPluginManager().isPluginEnabled("TFD4J")) { - dc = new TFM_Accessor().botAccessor(); - } pul = new PunishmentList(); bm = new BanManager(); diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/api/Aggregator.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/Aggregator.java new file mode 100644 index 00000000..7bfbb778 --- /dev/null +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/Aggregator.java @@ -0,0 +1,27 @@ +package me.totalfreedom.totalfreedommod.api; + +public class Aggregator +{ + private Context discord; + private Context shoppe; + + public Context getDiscordContext() + { + return discord; + } + + public void setDiscordContext(Context discord) + { + this.discord = discord; + } + + public Context getShoppeContext() + { + return shoppe; + } + + public void setShoppeContext(Context shoppe) + { + this.shoppe = shoppe; + } +} diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/api/Context.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/Context.java new file mode 100644 index 00000000..1c1e7c4e --- /dev/null +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/Context.java @@ -0,0 +1,16 @@ +package me.totalfreedom.totalfreedommod.api; + +public class Context +{ + private final T value; + + public Context(T value) + { + this.value = value; + } + + public T getValue() + { + return value; + } +} diff --git a/shop/src/main/java/me/totalfreedom/shop/ShopItem.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/ShopItem.java similarity index 98% rename from shop/src/main/java/me/totalfreedom/shop/ShopItem.java rename to commons/src/main/java/me/totalfreedom/totalfreedommod/api/ShopItem.java index 1231289d..f3865ce2 100644 --- a/shop/src/main/java/me/totalfreedom/shop/ShopItem.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/ShopItem.java @@ -1,4 +1,4 @@ -package me.totalfreedom.shop; +package me.totalfreedom.totalfreedommod.api; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import org.bukkit.ChatColor; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/api/ShoppeCommons.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/ShoppeCommons.java new file mode 100644 index 00000000..f1a87f2d --- /dev/null +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/ShoppeCommons.java @@ -0,0 +1,55 @@ +package me.totalfreedom.totalfreedommod.api; + +import me.totalfreedom.totalfreedommod.player.PlayerData; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; + +public interface ShoppeCommons +{ + + int getCoinsPerReactionWin(); + + void startReactionTimer(); + + void forceStartReaction(); + + void startReaction(); + + void endReaction(String winner); + + String getShopPrefix(); + + String getShopTitle(); + + Inventory generateShopGUI(PlayerData playerData); + + Inventory generateLoginMessageGUI(Player player); + + boolean isRealItem(PlayerData data, ShopItem shopItem, PlayerInventory inventory, ItemStack realItem); + + boolean isRealItem(PlayerData data, ShopItem shopItem, ItemStack givenItem, ItemStack realItem); + + ItemStack getLightningRod(); + + ItemStack getGrapplingHook(); + + ItemStack getFireBall(); + + ItemStack getRideablePearl(); + + ItemStack getStackingPotato(); + + ItemStack getClownFish(); + + boolean canAfford(int price, int coins); + + int amountNeeded(int price, int coins); + + ItemStack shopGUIItem(ShopItem item, PlayerData data); + + ShopItem getShopItem(int slot); + + String getReactionString(); +} diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/api/TFD4JCommons.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/TFD4JCommons.java new file mode 100644 index 00000000..b91c765c --- /dev/null +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/TFD4JCommons.java @@ -0,0 +1,33 @@ +package me.totalfreedom.totalfreedommod.api; + +import me.totalfreedom.totalfreedommod.admin.Admin; +import me.totalfreedom.totalfreedommod.player.PlayerData; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; + +import java.util.Map; + +public interface TFD4JCommons +{ + void messageAdminChatChannel(String message); + + void clearQueue(); + + void messageChatChannel(String message, boolean system); + + boolean syncRoles(Admin admin, String id); + + String getCode(PlayerData playerData); + + String generateCode(int size); + + Map getLinkCodes(); + + String formatBotTag(); + + boolean sendReportOffline(Player reporter, OfflinePlayer reported, String reason); + + boolean sendReport(Player reporter, Player reported, String reason); + + boolean isEnabled(); +} diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_cleardiscordqueue.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_cleardiscordqueue.java index b6d94fa9..113d3c39 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_cleardiscordqueue.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_cleardiscordqueue.java @@ -18,7 +18,7 @@ public class Command_cleardiscordqueue extends FreedomCommand return true; } - plugin.dc.getUtils().clearQueue(); + plugin.dc.clearQueue(); msg("Cleared the discord message queue."); return true; } diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_clownfish.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_clownfish.java index 9eb0e1c3..95d9c572 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_clownfish.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_clownfish.java @@ -1,7 +1,7 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.rank.Rank; -import me.totalfreedom.shop.ShopItem; +import me.totalfreedom.totalfreedommod.api.ShopItem; import net.kyori.adventure.text.Component; import org.bukkit.ChatColor; import org.bukkit.command.Command; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_consolesay.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_consolesay.java index d948575a..986acdfb 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_consolesay.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_consolesay.java @@ -25,7 +25,7 @@ public class Command_consolesay extends FreedomCommand FUtil.bcastMsg(String.format("§7[CONSOLE] §c%s §8» §f%s", sender.getName(), StringUtils.join(args, " "))); if (plugin.dc != null) { - plugin.dc.getUtils().messageChatChannel("[CONSOLE] " + sender.getName() + " \u00BB " + ChatColor.stripColor(message), true); + plugin.dc.messageChatChannel("[CONSOLE] " + sender.getName() + " \u00BB " + ChatColor.stripColor(message), true); } diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_doom.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_doom.java index b97b3f0d..f1bcafc0 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_doom.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_doom.java @@ -53,7 +53,7 @@ public class Command_doom extends FreedomCommand plugin.al.updateTables(); if (plugin.dc != null && plugin.dc.isEnabled() && ConfigEntry.DISCORD_ROLE_SYNC.getBoolean()) { - plugin.dc.getBot().syncRoles(admin, plugin.pl.getData(admin.getName()).getDiscordID()); + plugin.dc.syncRoles(admin, plugin.pl.getData(admin.getName()).getDiscordID()); } } diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_fireball.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_fireball.java index 28aebecd..6fef8ee5 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_fireball.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_fireball.java @@ -1,7 +1,7 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.rank.Rank; -import me.totalfreedom.shop.ShopItem; +import me.totalfreedom.totalfreedommod.api.ShopItem; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_grapplinghook.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_grapplinghook.java index a13aef3e..8579d717 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_grapplinghook.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_grapplinghook.java @@ -1,6 +1,6 @@ package me.totalfreedom.totalfreedommod.command; -import me.totalfreedom.shop.ShopItem; +import me.totalfreedom.totalfreedommod.api.ShopItem; import me.totalfreedom.totalfreedommod.rank.Rank; import org.bukkit.ChatColor; import org.bukkit.command.Command; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_lightningrod.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_lightningrod.java index de109494..d9ddd12d 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_lightningrod.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_lightningrod.java @@ -1,7 +1,7 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.rank.Rank; -import me.totalfreedom.shop.ShopItem; +import me.totalfreedom.totalfreedommod.api.ShopItem; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_linkdiscord.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_linkdiscord.java index bf626335..240fef65 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_linkdiscord.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_linkdiscord.java @@ -44,16 +44,16 @@ public class Command_linkdiscord extends FreedomCommand return true; } - if (plugin.dc.getBot().getLinkCodes().containsValue(data)) + if (plugin.dc.getLinkCodes().containsValue(data)) { - code = plugin.dc.getBot().getCode(data); + code = plugin.dc.getCode(data); } else { - code = plugin.dc.getBot().generateCode(5); - plugin.dc.getBot().getLinkCodes().put(code, data); + code = plugin.dc.generateCode(5); + plugin.dc.getLinkCodes().put(code, data); } msg("Your linking code is " + ChatColor.AQUA + code, ChatColor.GREEN); - msg("Take this code and DM the server bot (" + plugin.dc.getBot().formatBotTag() + ") the code (do not put anything else in the message, only the code)"); + msg("Take this code and DM the server bot (" + plugin.dc.formatBotTag() + ") the code (do not put anything else in the message, only the code)"); return true; } } \ No newline at end of file diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_loginmessage.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_loginmessage.java index 504977a2..eebf9e23 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_loginmessage.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_loginmessage.java @@ -2,7 +2,7 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.player.PlayerData; import me.totalfreedom.totalfreedommod.rank.Rank; -import me.totalfreedom.shop.ShopItem; +import me.totalfreedom.totalfreedommod.api.ShopItem; import org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; import org.bukkit.command.Command; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_manageshop.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_manageshop.java index 9922728a..73352840 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_manageshop.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_manageshop.java @@ -2,7 +2,7 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.player.PlayerData; import me.totalfreedom.totalfreedommod.rank.Rank; -import me.totalfreedom.shop.ShopItem; +import me.totalfreedom.totalfreedommod.api.ShopItem; import me.totalfreedom.totalfreedommod.util.FUtil; import org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_myadmin.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_myadmin.java index ae5a9416..72f67c27 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_myadmin.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_myadmin.java @@ -173,7 +173,7 @@ public class Command_myadmin extends FreedomCommand msg("Please run /linkdiscord first!", ChatColor.RED); return true; } - boolean synced = plugin.dc.getBot().syncRoles(target, playerData.getDiscordID()); + boolean synced = plugin.dc.syncRoles(target, playerData.getDiscordID()); if (synced) { msg("Successfully synced your roles.", ChatColor.GREEN); diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_report.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_report.java index 83182885..975fc60b 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_report.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_report.java @@ -56,7 +56,7 @@ public class Command_report extends FreedomCommand if (plugin.dc != null && plugin.dc.isEnabled()) { - logged = (player == null) ? plugin.dc.getUtils().sendReportOffline(playerSender, offlinePlayer, report) : plugin.dc.getUtils().sendReport(playerSender, player, report); + logged = (player == null) ? plugin.dc.sendReportOffline(playerSender, offlinePlayer, report) : plugin.dc.sendReport(playerSender, player, report); } msg(ChatColor.GREEN + "Thank you, your report has been successfully logged." diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rideablepearl.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rideablepearl.java index 15777970..ed4b078c 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rideablepearl.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rideablepearl.java @@ -1,7 +1,7 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.rank.Rank; -import me.totalfreedom.shop.ShopItem; +import me.totalfreedom.totalfreedommod.api.ShopItem; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_saconfig.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_saconfig.java index 1ba5a0f5..393863e1 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_saconfig.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_saconfig.java @@ -108,7 +108,7 @@ public class Command_saconfig extends FreedomCommand if (plugin.dc != null && plugin.dc.isEnabled() && ConfigEntry.DISCORD_ROLE_SYNC.getBoolean()) { - plugin.dc.getBot().syncRoles(admin, plugin.pl.getData(admin.getName()).getDiscordID()); + plugin.dc.syncRoles(admin, plugin.pl.getData(admin.getName()).getDiscordID()); } msg("Set " + admin.getName() + "'s rank to " + rank.getName()); @@ -204,7 +204,7 @@ public class Command_saconfig extends FreedomCommand if (plugin.dc != null && plugin.dc.isEnabled() && ConfigEntry.DISCORD_ROLE_SYNC.getBoolean()) { - plugin.dc.getBot().syncRoles(admin, plugin.pl.getData(player).getDiscordID()); + plugin.dc.syncRoles(admin, plugin.pl.getData(player).getDiscordID()); } } @@ -258,7 +258,7 @@ public class Command_saconfig extends FreedomCommand if (plugin.dc != null && plugin.dc.isEnabled() && ConfigEntry.DISCORD_ROLE_SYNC.getBoolean()) { - plugin.dc.getBot().syncRoles(admin, plugin.pl.getData(adminName).getDiscordID()); + plugin.dc.syncRoles(admin, plugin.pl.getData(adminName).getDiscordID()); } return true; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_say.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_say.java index 0bef2569..968677b4 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_say.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_say.java @@ -43,7 +43,7 @@ public class Command_say extends FreedomCommand FUtil.bcastMsg(String.format("[Server:%s] %s", sender.getName(), message), ChatColor.LIGHT_PURPLE); if (plugin.dc != null) { - plugin.dc.getUtils().messageChatChannel(String.format("[Server:%s] \u00BB %s", sender.getName(), message), true); + plugin.dc.messageChatChannel(String.format("[Server:%s] \u00BB %s", sender.getName(), message), true); } return true; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_stackingpotato.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_stackingpotato.java index e4a13ef6..fcd14097 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_stackingpotato.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_stackingpotato.java @@ -1,7 +1,7 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.rank.Rank; -import me.totalfreedom.shop.ShopItem; +import me.totalfreedom.totalfreedommod.api.ShopItem; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_trail.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_trail.java index 3b243065..b749a1a3 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_trail.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_trail.java @@ -1,7 +1,7 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.rank.Rank; -import me.totalfreedom.shop.ShopItem; +import me.totalfreedom.totalfreedommod.api.ShopItem; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_vanish.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_vanish.java index 532a872d..79c9f1d9 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_vanish.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_vanish.java @@ -48,7 +48,7 @@ public class Command_vanish extends FreedomCommand .color(NamedTextColor.YELLOW)); if (plugin.dc != null) { - plugin.dc.getUtils().messageChatChannel("**" + playerSender.getName() + " joined the server" + "**", true); + plugin.dc.messageChatChannel("**" + playerSender.getName() + " joined the server" + "**", true); } } @@ -101,7 +101,7 @@ public class Command_vanish extends FreedomCommand server.broadcast(Component.translatable("multiplayer.player.left", Component.text(playerSender.getName())) .color(NamedTextColor.YELLOW)); if (plugin.dc != null) { - plugin.dc.getUtils().messageChatChannel("**" + playerSender.getName() + " left the server" + "**", true); + plugin.dc.messageChatChannel("**" + playerSender.getName() + " left the server" + "**", true); } } diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/fun/ItemFun.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/fun/ItemFun.java index 41b6290c..fafef5e8 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/fun/ItemFun.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/fun/ItemFun.java @@ -1,6 +1,6 @@ package me.totalfreedom.totalfreedommod.fun; -import me.totalfreedom.shop.ShopItem; +import me.totalfreedom.totalfreedommod.api.ShopItem; import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.player.FPlayer; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java index 7ccf8692..fbe39168 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java @@ -7,7 +7,7 @@ import java.util.SplittableRandom; import java.util.UUID; import me.totalfreedom.totalfreedommod.FreedomService; -import me.totalfreedom.shop.ShopItem; +import me.totalfreedom.totalfreedommod.api.ShopItem; import me.totalfreedom.totalfreedommod.util.Groups; import org.bukkit.Location; import org.bukkit.Material; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerData.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerData.java index 4640d32c..59b2a610 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerData.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerData.java @@ -5,7 +5,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; -import me.totalfreedom.shop.ShopItem; +import me.totalfreedom.totalfreedommod.api.ShopItem; import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FUtil; import org.apache.commons.lang.StringUtils; diff --git a/discord/src/main/java/me/totalfreedom/discord/Bot.java b/discord/src/main/java/me/totalfreedom/discord/Bot.java index d56a92ac..d4c8bc6c 100644 --- a/discord/src/main/java/me/totalfreedom/discord/Bot.java +++ b/discord/src/main/java/me/totalfreedom/discord/Bot.java @@ -80,22 +80,6 @@ public class Bot RATELIMIT_EXECUTOR.setRemoveOnCancelPolicy(true); } - public String getCode(PlayerData playerData) { - for (String code : LINK_CODES.keySet()) - { - if (LINK_CODES.get(code).equals(playerData)) - { - return code; - } - } - return ""; - } - - public String generateCode(int size) - { - return RandomStringUtils.randomNumeric(size); - } - public String formatBotTag() { return client.getSelf() .blockOptional() @@ -106,94 +90,6 @@ public class Bot .getDiscriminator(); } - public boolean syncRoles(Admin admin, String discordID) - { - if (discordID == null) - { - return false; - } - - Guild server = client.getGuildById(SnowflakeEntry.serverID) - .blockOptional() - .orElseThrow(); - - Member member = server.getMemberById(Snowflake.of(discordID)) - .blockOptional() - .orElseThrow(); - - Role adminRole = server.getRoleById(SnowflakeEntry.adminRoleID) - .blockOptional() - .orElseThrow(); - - Role senioradminRole = server.getRoleById(SnowflakeEntry.seniorRoleID) - .blockOptional() - .orElseThrow(); - - if (!admin.isActive()) - { - syncRolesActivityCheck(member, adminRole, senioradminRole); - return true; - } - - if (admin.getRank().equals(Rank.ADMIN)) - { - syncRolesAdminAssignment(member, adminRole, senioradminRole); - return true; - } - else if (admin.getRank().equals(Rank.SENIOR_ADMIN)) - { - syncRolesSeniorAssignment(member, adminRole, senioradminRole); - return true; - } - return false; - } - - private void syncRolesAdminAssignment(Member member, Role adminRole, Role senioradminRole) { - member.getRoles().doFirst(() -> { - if (!member.getRoles().collectList().blockOptional().orElseThrow().contains(adminRole)) - { - member.addRole(adminRole.getId()).block(); - } - }).doOnEach(r -> { - Role role = r.get(); - if (role == null) return; - - if (role.equals(senioradminRole)) - { - member.removeRole(role.getId()).block(); - } - }).subscribe(); - } - - private void syncRolesActivityCheck(Member member, Role adminRole, Role senioradminRole) { - member.getRoles().doOnEach(r -> { - Role role = r.get(); - if (role == null) return; - - if (role.equals(adminRole) || role.equals(senioradminRole)) - { - member.removeRole(role.getId()).block(); - } - }).subscribe(); - } - - private void syncRolesSeniorAssignment(Member member, Role adminRole, Role senioradminRole) { - member.getRoles().doFirst(() -> { - if (!member.getRoles().collectList().blockOptional().orElseThrow().contains(senioradminRole)) - { - member.addRole(senioradminRole.getId()).block(); - } - }).doOnEach(r -> { - Role role = r.get(); - if (role == null) return; - - if (role.equals(adminRole)) - { - member.removeRole(role.getId()).block(); - } - }).subscribe(); - } - private String poolIdentifier() { diff --git a/discord/src/main/java/me/totalfreedom/discord/TFD4J.java b/discord/src/main/java/me/totalfreedom/discord/TFD4J.java index 27005a1f..2738ee87 100644 --- a/discord/src/main/java/me/totalfreedom/discord/TFD4J.java +++ b/discord/src/main/java/me/totalfreedom/discord/TFD4J.java @@ -1,35 +1,77 @@ package me.totalfreedom.discord; +import me.totalfreedom.discord.command.HelpCommand; +import me.totalfreedom.discord.command.ListCommand; +import me.totalfreedom.discord.command.TPSCommand; +import me.totalfreedom.discord.handling.CommandHandler; import me.totalfreedom.discord.listener.AdminChatListener; import me.totalfreedom.discord.listener.BukkitNative; import me.totalfreedom.discord.listener.MinecraftListener; -import me.totalfreedom.discord.util.Utilities; +import me.totalfreedom.totalfreedommod.api.Context; +import me.totalfreedom.totalfreedommod.api.TFD4JCommons; import org.bukkit.plugin.java.JavaPlugin; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class TFD4J extends JavaPlugin { + private final Logger slf4j = LoggerFactory.getLogger("TFD4J"); private Bot bot; - private Utilities utils; private MinecraftListener mc; private AdminChatListener ac; + private TFD4JCommons tfd4jcommons; + private BukkitNative bn; + private CommandHandler ch; @Override public void onEnable() { + slf4j().info("Hello from TFD4J! Initializing our API implementation..."); + this.tfd4jcommons = new TFD4JCommonsImpl(this); + + slf4j().info("API successfully initialized! Initializing our bot..."); this.bot = new Bot(); - this.utils = new Utilities(this); - new BukkitNative(this); + + slf4j().info("Bot successfully initialized! Registering the Bukkit Native listener..."); + this.bn = new BukkitNative(this); + + slf4j().info("Bukkit Native listener successfully registered! Registering the Discord4J Listeners..."); this.mc = new MinecraftListener(this); this.ac = new AdminChatListener(this); - mc.minecraftChatBound(); ac.adminChatBound(); + + slf4j().info("Discord4J listeners successfully registered! Registering the Command Handler..."); + this.ch = new CommandHandler(bot.getClient().getRestClient()); + + slf4j().info("Command Handler successfully registered! Registering commands..."); + this.ch.registerCommand(new HelpCommand()); + this.ch.registerCommand(new ListCommand()); + this.ch.registerCommand(new TPSCommand()); + + slf4j().info("Commands successfully registered! Providing context to TFM..."); + Context context = new Context<>(tfd4jcommons); + bot.getTFM().getCommons().ag.setDiscordContext(context); + bot.getTFM().getCommons().registerDiscord(); + + slf4j().info("Context provided! TFD4J is now ready to go!"); } @Override public void onDisable() { - bot.getClient().onDisconnect().subscribe(); + slf4j().info("Disconnecting the Discord bot..."); + bot.getClient() + .onDisconnect() + .doOnError(th -> slf4j().error("Error disconnecting the bot!", th)) + .doOnSuccess(v -> slf4j().info("Bot disconnected!")) + .subscribe(); + slf4j().info("Goodbye from TFD4J!"); + } + + public Logger slf4j() + { + return slf4j; } public Bot getBot() @@ -37,9 +79,9 @@ public class TFD4J extends JavaPlugin return bot; } - public Utilities getUtils() + public TFD4JCommons getImpl() { - return utils; + return tfd4jcommons; } public MinecraftListener getMinecraftListener() diff --git a/discord/src/main/java/me/totalfreedom/discord/TFD4JCommonsImpl.java b/discord/src/main/java/me/totalfreedom/discord/TFD4JCommonsImpl.java new file mode 100644 index 00000000..94ae9150 --- /dev/null +++ b/discord/src/main/java/me/totalfreedom/discord/TFD4JCommonsImpl.java @@ -0,0 +1,372 @@ +package me.totalfreedom.discord; + +import com.google.common.collect.ImmutableList; +import discord4j.common.util.Snowflake; +import discord4j.core.object.entity.Guild; +import discord4j.core.object.entity.Member; +import discord4j.core.object.entity.Message; +import discord4j.core.object.entity.Role; +import discord4j.core.object.entity.channel.TextChannel; +import discord4j.core.object.reaction.ReactionEmoji; +import discord4j.core.spec.EmbedCreateSpec; +import discord4j.core.spec.MessageCreateSpec; +import me.totalfreedom.discord.util.SnowflakeEntry; +import me.totalfreedom.discord.util.Utilities; +import me.totalfreedom.totalfreedommod.admin.Admin; +import me.totalfreedom.totalfreedommod.api.TFD4JCommons; +import me.totalfreedom.totalfreedommod.config.ConfigEntry; +import me.totalfreedom.totalfreedommod.player.PlayerData; +import me.totalfreedom.totalfreedommod.rank.Rank; +import me.totalfreedom.totalfreedommod.util.FLog; +import org.apache.commons.lang.RandomStringUtils; +import org.apache.commons.lang.WordUtils; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.time.Instant; +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +public class TFD4JCommonsImpl implements TFD4JCommons +{ + private final TFD4J tfd4J; + private final ImmutableList DISCORD_SUBDOMAINS; + private Flux sentMessages; + + public TFD4JCommonsImpl(TFD4J tfd4J) + { + this.tfd4J = tfd4J; + this.sentMessages = Flux.fromIterable(new ArrayList<>()); + this.DISCORD_SUBDOMAINS = (ImmutableList) + List.of("discordapp.com", + "discord.com", + "discord.gg"); + } + + @Override + public void messageAdminChatChannel(String message) + { + String chat_channel_id = ConfigEntry.DISCORD_ADMINCHAT_CHANNEL_ID.getString(); + + String sanitizedMessage = sanitizeChatMessage(message); + + if (sanitizedMessage.isBlank()) return; + + if (!chat_channel_id.isEmpty()) + { + MessageCreateSpec spec = MessageCreateSpec.builder() + .content(sanitizedMessage) + .build(); + + Mono sentMessage = tfd4J + .getBot() + .getClient() + .getChannelById(SnowflakeEntry.adminChatChannelID) + .ofType(TextChannel.class) + .flatMap(c -> c.createMessage(spec)); + + insert(sentMessage); + } + } + + public Flux getMessagesSent() + { + return sentMessages; + } + + private void insert(Mono messageMono) + { + sentMessages.concatWith(messageMono); + } + + @Override + public void clearQueue() + { + sentMessages = Flux.fromIterable(new ArrayList<>()); + } + + @Override + public void messageChatChannel(String message, boolean system) + { + String chat_channel_id = ConfigEntry.DISCORD_CHAT_CHANNEL_ID.getString(); + + String sanitizedMessage = (system) ? message : sanitizeChatMessage(message); + + if (sanitizedMessage.isBlank()) return; + + if (!chat_channel_id.isEmpty()) + { + MessageCreateSpec spec = MessageCreateSpec.builder() + .content(sanitizedMessage) + .build(); + + Mono sentMessage = tfd4J + .getBot() + .getClient() + .getChannelById(SnowflakeEntry.chatChannelID) + .ofType(TextChannel.class) + .flatMap(c -> c.createMessage(spec)); + + insert(sentMessage); + } + } + + private String sanitizeChatMessage(String message) + { + String newMessage = message; + + if (message.contains("@")) + { + // \u200B is Zero Width Space, invisible on Discord + newMessage = message.replace("@", "@\u200B"); + } + + if (message.toLowerCase().contains("discord.gg")) // discord.gg/invite works as an invite + { + return ""; + } + + for (String subdomain : DISCORD_SUBDOMAINS) + { + if (message.toLowerCase().contains(subdomain + "/invite")) + { + return ""; + } + } + + if (message.contains("§")) + { + newMessage = message.replace("§", ""); + } + + return Utilities.deformat(newMessage); + } + + @Override + public boolean syncRoles(Admin admin, String discordID) + { + if (discordID == null) + { + return false; + } + + Guild server = tfd4J.getBot().getGuildById() + .blockOptional() + .orElseThrow(); + + Member member = server.getMemberById(Snowflake.of(discordID)) + .blockOptional() + .orElseThrow(); + + Role adminRole = server.getRoleById(SnowflakeEntry.adminRoleID) + .blockOptional() + .orElseThrow(); + + Role senioradminRole = server.getRoleById(SnowflakeEntry.seniorRoleID) + .blockOptional() + .orElseThrow(); + + if (!admin.isActive()) + { + syncRolesActivityCheck(member, adminRole, senioradminRole); + return true; + } + + if (admin.getRank().equals(Rank.ADMIN)) + { + syncRolesAdminAssignment(member, adminRole, senioradminRole); + return true; + } else if (admin.getRank().equals(Rank.SENIOR_ADMIN)) + { + syncRolesSeniorAssignment(member, adminRole, senioradminRole); + return true; + } + return false; + } + + private void syncRolesAdminAssignment(Member member, Role adminRole, Role senioradminRole) + { + member.getRoles().doFirst(() -> + { + if (!member.getRoles().collectList().blockOptional().orElseThrow().contains(adminRole)) + { + member.addRole(adminRole.getId()).block(); + } + }).doOnEach(r -> + { + Role role = r.get(); + if (role == null) return; + + if (role.equals(senioradminRole)) + { + member.removeRole(role.getId()).block(); + } + }).subscribe(); + } + + private void syncRolesActivityCheck(Member member, Role adminRole, Role senioradminRole) + { + member.getRoles().doOnEach(r -> + { + Role role = r.get(); + if (role == null) return; + + if (role.equals(adminRole) || role.equals(senioradminRole)) + { + member.removeRole(role.getId()).block(); + } + }).subscribe(); + } + + private void syncRolesSeniorAssignment(Member member, Role adminRole, Role senioradminRole) + { + member.getRoles().doFirst(() -> + { + if (!member.getRoles().collectList().blockOptional().orElseThrow().contains(senioradminRole)) + { + member.addRole(senioradminRole.getId()).block(); + } + }).doOnEach(r -> + { + Role role = r.get(); + if (role == null) return; + + if (role.equals(adminRole)) + { + member.removeRole(role.getId()).block(); + } + }).subscribe(); + } + + @Override + public String getCode(PlayerData playerData) + { + for (String code : this.getLinkCodes().keySet()) + { + if (this.getLinkCodes().get(code).equals(playerData)) + { + return code; + } + } + return ""; + } + + @Override + public String generateCode(int size) + { + return RandomStringUtils.randomNumeric(size); + } + + @Override + public Map getLinkCodes() + { + return tfd4J.getBot().getLinkCodes(); + } + + @Override + public String formatBotTag() + { + return tfd4J.getBot().formatBotTag(); + } + + @Override + public boolean sendReportOffline(Player reporter, OfflinePlayer reported, String reason) + { + if (!tfd4J.getBot().shouldISendReport()) + { + return false; + } + + final Guild server = tfd4J.getBot().getGuildById().block(); + + if (server == null) return false; + + final TextChannel channel = server.getChannelById(SnowflakeEntry.reportChannelID) + .ofType(TextChannel.class) + .blockOptional() + .orElseThrow(); + + final EmbedCreateSpec.Builder builder = EmbedCreateSpec.builder() + .title("Report for " + reported.getName() + " (offline)") + .description(reason) + .footer("Reported by " + reporter.getName(), "https://minotar.net/helm/" + reporter.getName() + ".png") + .timestamp(Instant.from(ZonedDateTime.now())); + if (tfd4J.getBot().getTFM().getCommons().esb.isEnabled()) + { + com.earth2me.essentials.User user = tfd4J.getBot().getTFM().getCommons().esb.getEssentialsUser(reported.getName()); + String location = "World: " + Objects.requireNonNull(user.getLastLocation().getWorld()).getName() + ", X: " + user.getLastLocation().getBlockX() + ", Y: " + user.getLastLocation().getBlockY() + ", Z: " + user.getLastLocation().getBlockZ(); + builder.addField("Location", location, true); + builder.addField("God Mode", WordUtils.capitalizeFully(String.valueOf(user.isGodModeEnabled())), true); + if (user.getNickname() != null) + { + builder.addField("Nickname", user.getNickname(), true); + } + } + EmbedCreateSpec embed = builder.build(); + Message message = channel.createMessage(embed).block(); + + if (message != null && !ConfigEntry.DISCORD_REPORT_ARCHIVE_CHANNEL_ID.getString().isEmpty()) + { + message.addReaction(ReactionEmoji.unicode("\uD83D\uDCCB")).subscribe(); + } + + return true; + } + + @Override + public boolean sendReport(Player reporter, Player reported, String reason) + { + if (!tfd4J.getBot().shouldISendReport()) + { + return false; + } + + final Guild server = tfd4J.getBot() + .getClient() + .getGuildById(SnowflakeEntry.serverID) + .block(); + + if (server == null) + { + FLog.severe("The guild ID specified in the config is invalid."); + return false; + } + + final TextChannel channel = server.getChannelById(SnowflakeEntry.reportChannelID) + .ofType(TextChannel.class) + .blockOptional() + .orElseThrow(); + + String location = "World: " + Objects.requireNonNull(reported.getLocation().getWorld()).getName() + ", X: " + reported.getLocation().getBlockX() + ", Y: " + reported.getLocation().getBlockY() + ", Z: " + reported.getLocation().getBlockZ(); + + final EmbedCreateSpec spec = EmbedCreateSpec.builder() + .title("Report for " + reported.getName()) + .description(reason) + .footer("Reported by " + reporter.getName(), "https://minotar.net/helm/" + reporter.getName() + ".png") + .timestamp(Instant.from(ZonedDateTime.now())) + .addField("Location", location, true) + .addField("Game Mode", WordUtils.capitalizeFully(reported.getGameMode().name()), true) + .build(); + + Message message = channel.createMessage(spec).block(); + + if (!ConfigEntry.DISCORD_REPORT_ARCHIVE_CHANNEL_ID.getString().isEmpty() && message != null) + { + ReactionEmoji emoji = ReactionEmoji.unicode("\uD83D\uDCCB"); + message.addReaction(emoji); + } + + return true; + } + + @Override + public boolean isEnabled() + { + return tfd4J.isEnabled(); + } +} diff --git a/discord/src/main/java/me/totalfreedom/discord/TFM_Accessor.java b/discord/src/main/java/me/totalfreedom/discord/TFM_Accessor.java deleted file mode 100644 index 705d1641..00000000 --- a/discord/src/main/java/me/totalfreedom/discord/TFM_Accessor.java +++ /dev/null @@ -1,18 +0,0 @@ -package me.totalfreedom.discord; - -import org.bukkit.Bukkit; - -public class TFM_Accessor -{ - private final TFD4J accessor; - - public TFM_Accessor() - { - this.accessor = (TFD4J) Bukkit.getPluginManager().getPlugin("TFD4J"); - } - - public TFD4J botAccessor() - { - return accessor; - } -} diff --git a/discord/src/main/java/me/totalfreedom/discord/listener/BukkitNative.java b/discord/src/main/java/me/totalfreedom/discord/listener/BukkitNative.java index 84dcfc46..ea4ec64f 100644 --- a/discord/src/main/java/me/totalfreedom/discord/listener/BukkitNative.java +++ b/discord/src/main/java/me/totalfreedom/discord/listener/BukkitNative.java @@ -2,6 +2,7 @@ package me.totalfreedom.discord.listener; import me.totalfreedom.discord.Bot; import me.totalfreedom.discord.TFD4J; +import me.totalfreedom.discord.util.Utilities; import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import net.kyori.adventure.text.Component; @@ -28,8 +29,6 @@ public class BukkitNative implements Listener this.tfd4j = tfd4j; this.bot = tfd4j.getBot(); this.commons = bot.getTFM().getCommons(); - - tfd4j.getServer().getPluginManager().registerEvents(this, tfd4j); } @EventHandler(priority = EventPriority.MONITOR) @@ -37,8 +36,8 @@ public class BukkitNative implements Listener { if (!commons.al.isVanished(event.getPlayer().getUniqueId())) { - tfd4j.getUtils().messageChatChannel("**" - + tfd4j.getUtils().deformat(event.getPlayer().getName()) + tfd4j.getImpl().messageChatChannel("**" + + Utilities.deformat(event.getPlayer().getName()) + " joined the server" + "**", true); } } @@ -48,8 +47,8 @@ public class BukkitNative implements Listener { if (!commons.al.isVanished(event.getPlayer().getUniqueId())) { - tfd4j.getUtils().messageChatChannel("**" - + tfd4j.getUtils().deformat(event.getPlayer().getName()) + tfd4j.getImpl().messageChatChannel("**" + + Utilities.deformat(event.getPlayer().getName()) + " left the server" + "**", true); } } @@ -68,8 +67,8 @@ public class BukkitNative implements Listener if (deathMessage != null) { - tfd4j.getUtils().messageChatChannel("**" - + tfd4j.getUtils().deformat(PlainTextComponentSerializer.plainText().serialize(deathMessage)) + tfd4j.getImpl().messageChatChannel("**" + + Utilities.deformat(PlainTextComponentSerializer.plainText().serialize(deathMessage)) + "**", true); } } @@ -83,7 +82,7 @@ public class BukkitNative implements Listener if (!ConfigEntry.ADMIN_ONLY_MODE.getBoolean() && !tfd4j.getServer().hasWhitelist() && !commons.pl.getPlayer(player).isMuted() && bot != null) { - tfd4j.getUtils().messageChatChannel(player.getName() + tfd4j.getImpl().messageChatChannel(player.getName() + " \u00BB " + ChatColor.stripColor(message), true); } diff --git a/discord/src/main/java/me/totalfreedom/discord/util/Utilities.java b/discord/src/main/java/me/totalfreedom/discord/util/Utilities.java index 79c0e62e..47aad9bc 100644 --- a/discord/src/main/java/me/totalfreedom/discord/util/Utilities.java +++ b/discord/src/main/java/me/totalfreedom/discord/util/Utilities.java @@ -28,204 +28,13 @@ import java.util.Objects; public class Utilities { - private Flux sentMessages = Flux.fromIterable(new ArrayList<>()); - private final TFD4J tfd4J; - private final ImmutableList DISCORD_SUBDOMAINS = (ImmutableList) - List.of("discordapp.com", - "discord.com", - "discord.gg"); - - public Utilities(TFD4J tfd4J) - { - this.tfd4J = tfd4J; + private Utilities() { + throw new AssertionError(); } - public void clearQueue() - { - sentMessages = Flux.fromIterable(new ArrayList<>()); - } - - public String sanitizeChatMessage(String message) - { - String newMessage = message; - - if (message.contains("@")) - { - // \u200B is Zero Width Space, invisible on Discord - newMessage = message.replace("@", "@\u200B"); - } - - if (message.toLowerCase().contains("discord.gg")) // discord.gg/invite works as an invite - { - return ""; - } - - for (String subdomain : DISCORD_SUBDOMAINS) - { - if (message.toLowerCase().contains(subdomain + "/invite")) - { - return ""; - } - } - - if (message.contains("§")) - { - newMessage = message.replace("§", ""); - } - - return deformat(newMessage); - } - - public void messageChatChannel(String message, boolean system) - { - String chat_channel_id = ConfigEntry.DISCORD_CHAT_CHANNEL_ID.getString(); - - String sanitizedMessage = (system) ? message : sanitizeChatMessage(message); - - if (sanitizedMessage.isBlank()) return; - - if (!chat_channel_id.isEmpty()) - { - MessageCreateSpec spec = MessageCreateSpec.builder() - .content(sanitizedMessage) - .build(); - - Mono sentMessage = tfd4J - .getBot() - .getClient() - .getChannelById(SnowflakeEntry.chatChannelID) - .ofType(TextChannel.class) - .flatMap(c -> c.createMessage(spec)); - - insert(sentMessage); - } - } - - public void messageAdminChatChannel(String message) - { - String chat_channel_id = ConfigEntry.DISCORD_ADMINCHAT_CHANNEL_ID.getString(); - - String sanitizedMessage = sanitizeChatMessage(message); - - if (sanitizedMessage.isBlank()) return; - - if (!chat_channel_id.isEmpty()) - { - MessageCreateSpec spec = MessageCreateSpec.builder() - .content(sanitizedMessage) - .build(); - - Mono sentMessage = tfd4J - .getBot() - .getClient() - .getChannelById(SnowflakeEntry.adminChatChannelID) - .ofType(TextChannel.class) - .flatMap(c -> c.createMessage(spec)); - - insert(sentMessage); - } - } - - public boolean sendReportOffline(Player reporter, OfflinePlayer reported, String reason) - { - if (!tfd4J.getBot().shouldISendReport()) - { - return false; - } - - final Guild server = tfd4J.getBot().getGuildById().block(); - - if (server == null) return false; - - final TextChannel channel = server.getChannelById(SnowflakeEntry.reportChannelID) - .ofType(TextChannel.class) - .blockOptional() - .orElseThrow(); - - final EmbedCreateSpec.Builder builder = EmbedCreateSpec.builder() - .title("Report for " + reported.getName() + " (offline)") - .description(reason) - .footer("Reported by " + reporter.getName(), "https://minotar.net/helm/" + reporter.getName() + ".png") - .timestamp(Instant.from(ZonedDateTime.now())); - if (tfd4J.getBot().getTFM().getCommons().esb.isEnabled()) - { - com.earth2me.essentials.User user = tfd4J.getBot().getTFM().getCommons().esb.getEssentialsUser(reported.getName()); - String location = "World: " + Objects.requireNonNull(user.getLastLocation().getWorld()).getName() + ", X: " + user.getLastLocation().getBlockX() + ", Y: " + user.getLastLocation().getBlockY() + ", Z: " + user.getLastLocation().getBlockZ(); - builder.addField("Location", location, true); - builder.addField("God Mode", WordUtils.capitalizeFully(String.valueOf(user.isGodModeEnabled())), true); - if (user.getNickname() != null) - { - builder.addField("Nickname", user.getNickname(), true); - } - } - EmbedCreateSpec embed = builder.build(); - Message message = channel.createMessage(embed).block(); - - if (message != null && !ConfigEntry.DISCORD_REPORT_ARCHIVE_CHANNEL_ID.getString().isEmpty()) - { - message.addReaction(ReactionEmoji.unicode("\uD83D\uDCCB")).subscribe(); - } - - return true; - } - - public boolean sendReport(Player reporter, Player reported, String reason) - { - if (!tfd4J.getBot().shouldISendReport()) - { - return false; - } - - final Guild server = tfd4J.getBot() - .getClient() - .getGuildById(SnowflakeEntry.serverID) - .block(); - - if (server == null) - { - FLog.severe("The guild ID specified in the config is invalid."); - return false; - } - - final TextChannel channel = server.getChannelById(SnowflakeEntry.reportChannelID) - .ofType(TextChannel.class) - .blockOptional() - .orElseThrow(); - - String location = "World: " + Objects.requireNonNull(reported.getLocation().getWorld()).getName() + ", X: " + reported.getLocation().getBlockX() + ", Y: " + reported.getLocation().getBlockY() + ", Z: " + reported.getLocation().getBlockZ(); - - final EmbedCreateSpec spec = EmbedCreateSpec.builder() - .title("Report for " + reported.getName()) - .description(reason) - .footer("Reported by " + reporter.getName(), "https://minotar.net/helm/" + reporter.getName() + ".png") - .timestamp(Instant.from(ZonedDateTime.now())) - .addField("Location", location, true) - .addField("Game Mode", WordUtils.capitalizeFully(reported.getGameMode().name()), true) - .build(); - - Message message = channel.createMessage(spec).block(); - - if (!ConfigEntry.DISCORD_REPORT_ARCHIVE_CHANNEL_ID.getString().isEmpty() && message != null) - { - ReactionEmoji emoji = ReactionEmoji.unicode("\uD83D\uDCCB"); - message.addReaction(emoji); - } - - return true; - } - - public String deformat(String input) + // Leaving this here so I don't need to do more work than necessary. + public static String deformat(String input) { return input.replaceAll("([_\\\\`*>|])", "\\\\$1"); } - - public Flux getMessagesSent() - { - return sentMessages; - } - - public void insert(Mono messageMono) - { - sentMessages.concatWith(messageMono); - } } diff --git a/shop/src/main/java/me/totalfreedom/shop/Shop.java b/shop/src/main/java/me/totalfreedom/shop/Shop.java index c9774fbf..210cfa03 100644 --- a/shop/src/main/java/me/totalfreedom/shop/Shop.java +++ b/shop/src/main/java/me/totalfreedom/shop/Shop.java @@ -1,12 +1,9 @@ package me.totalfreedom.shop; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; -import java.util.List; -import java.util.Objects; +import io.papermc.paper.event.player.AsyncChatEvent; import me.totalfreedom.totalfreedommod.FreedomService; +import me.totalfreedom.totalfreedommod.api.ShopItem; +import me.totalfreedom.totalfreedommod.api.ShoppeCommons; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.player.PlayerData; import me.totalfreedom.totalfreedommod.util.FLog; @@ -21,7 +18,6 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; @@ -29,10 +25,12 @@ import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; -public class Shop extends FreedomService +import java.util.*; + +public class Shop extends FreedomService implements ShoppeCommons { - public final int coinsPerReactionWin = ConfigEntry.SHOP_REACTIONS_COINS_PER_WIN.getInteger(); - public final String prefix = ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Reaction" + ChatColor.DARK_GRAY + "] "; + private final int coinsPerReactionWin = ConfigEntry.SHOP_REACTIONS_COINS_PER_WIN.getInteger(); + private final String prefix = ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Reaction" + ChatColor.DARK_GRAY + "] "; private final String LOGIN_MESSAGE_GUI_TITLE = ChatColor.DARK_GREEN + ChatColor.BOLD.toString() + "Login Messages"; public String reactionString = ""; public Date reactionStartTime; @@ -49,6 +47,13 @@ public class Shop extends FreedomService } } + @Override + public int getCoinsPerReactionWin() + { + return coinsPerReactionWin; + } + + @Override public void startReactionTimer() { long interval = ConfigEntry.SHOP_REACTIONS_INTERVAL.getInteger() * 20L; @@ -64,12 +69,14 @@ public class Shop extends FreedomService }.runTaskLater(plugin, interval); } + @Override public void forceStartReaction() { reactions.cancel(); startReaction(); } + @Override public void startReaction() { if (!ConfigEntry.SHOP_ENABLED.getBoolean()) @@ -101,8 +108,7 @@ public class Shop extends FreedomService if ((seconds -= 1) == 0) { endReaction(null); - } - else + } else { countdownBar.setProgress(seconds / max); if (!countdownBar.getColor().equals(BarColor.YELLOW) && seconds / max <= 0.25) @@ -114,6 +120,7 @@ public class Shop extends FreedomService }.runTaskTimer(plugin, 0, 20); } + @Override public void endReaction(String winner) { countdownTask.cancel(); @@ -143,16 +150,19 @@ public class Shop extends FreedomService } } + @Override public String getShopPrefix() { return FUtil.colorize(ConfigEntry.SHOP_PREFIX.getString()); } + @Override public String getShopTitle() { return FUtil.colorize(ConfigEntry.SHOP_TITLE.getString()); } + @Override public Inventory generateShopGUI(PlayerData playerData) { Inventory gui = server.createInventory(null, 36, getShopTitle()); @@ -180,6 +190,7 @@ public class Shop extends FreedomService return gui; } + @Override public Inventory generateLoginMessageGUI(Player player) { Inventory gui = server.createInventory(null, 36, LOGIN_MESSAGE_GUI_TITLE); @@ -203,11 +214,13 @@ public class Shop extends FreedomService return gui; } + @Override public boolean isRealItem(PlayerData data, ShopItem shopItem, PlayerInventory inventory, ItemStack realItem) { return isRealItem(data, shopItem, inventory.getItemInMainHand(), realItem) || isRealItem(data, shopItem, inventory.getItemInOffHand(), realItem); } + @Override public boolean isRealItem(PlayerData data, ShopItem shopItem, ItemStack givenItem, ItemStack realItem) { if (!data.hasItem(shopItem) || !givenItem.getType().equals(realItem.getType())) @@ -223,6 +236,7 @@ public class Shop extends FreedomService return givenMeta.getDisplayName().equals(realMeta.getDisplayName()) && Objects.equals(givenMeta.getLore(), realMeta.getLore()); } + @Override public ItemStack getLightningRod() { ItemStack itemStack = new ItemStack(Material.BLAZE_ROD); @@ -235,6 +249,7 @@ public class Shop extends FreedomService return itemStack; } + @Override public ItemStack getGrapplingHook() { ItemStack itemStack = new ItemStack(Material.FISHING_ROD); @@ -246,6 +261,7 @@ public class Shop extends FreedomService return itemStack; } + @Override public ItemStack getFireBall() { ItemStack itemStack = new ItemStack(Material.FIRE_CHARGE); @@ -257,6 +273,7 @@ public class Shop extends FreedomService return itemStack; } + @Override public ItemStack getRideablePearl() { ItemStack itemStack = new ItemStack(Material.ENDER_PEARL); @@ -269,6 +286,7 @@ public class Shop extends FreedomService return itemStack; } + @Override public ItemStack getStackingPotato() { ItemStack itemStack = new ItemStack(Material.POTATO); @@ -280,6 +298,7 @@ public class Shop extends FreedomService return itemStack; } + @Override public ItemStack getClownFish() { ItemStack itemStack = new ItemStack(Material.TROPICAL_FISH); @@ -291,16 +310,19 @@ public class Shop extends FreedomService return itemStack; } + @Override public boolean canAfford(int price, int coins) { return coins >= price; } + @Override public int amountNeeded(int price, int coins) { return price - coins; } + @Override public ItemStack shopGUIItem(ShopItem item, PlayerData data) { ItemStack itemStack = new ItemStack(item.getIcon()); @@ -319,8 +341,7 @@ public class Shop extends FreedomService lore.add(ChatColor.RED + "You can not afford this item!"); lore.add(ChatColor.RED + "You need " + amountNeeded(price, coins) + " more coins to buy this item."); } - } - else + } else { lore.add(ChatColor.RED + "You already purchased this item."); } @@ -332,7 +353,7 @@ public class Shop extends FreedomService @EventHandler(priority = EventPriority.HIGH) public void onShopGUIClick(InventoryClickEvent event) { - if (!(event.getWhoClicked() instanceof Player)) + if (!(event.getWhoClicked() instanceof Player player)) { return; } @@ -350,7 +371,6 @@ public class Shop extends FreedomService return; } - Player player = (Player)event.getWhoClicked(); PlayerData playerData = plugin.pl.getData(player); int price = shopItem.getCost(); int coins = playerData.getCoins(); @@ -377,7 +397,7 @@ public class Shop extends FreedomService @EventHandler(priority = EventPriority.HIGH) public void onLoginMessageGUIClick(InventoryClickEvent event) { - if (!(event.getWhoClicked() instanceof Player)) + if (!(event.getWhoClicked() instanceof Player player)) { return; } @@ -391,7 +411,6 @@ public class Shop extends FreedomService int slot = event.getSlot(); - Player player = (Player)event.getWhoClicked(); PlayerData data = plugin.pl.getData(player); if (slot == 35) @@ -399,8 +418,7 @@ public class Shop extends FreedomService data.setLoginMessage(null); plugin.pl.save(data); player.sendMessage(ChatColor.GREEN + "Removed your login message"); - } - else + } else { String message = ConfigEntry.SHOP_LOGIN_MESSAGES.getStringList().get(slot); data.setLoginMessage(message); @@ -413,24 +431,25 @@ public class Shop extends FreedomService } @EventHandler - public void onPlayerChat(AsyncPlayerChatEvent event) + public void onPlayerChat(AsyncChatEvent event) { - String message = event.getMessage(); + String message = event.message().toString(); Player player = event.getPlayer(); if (ConfigEntry.SHOP_ENABLED.getBoolean() && ConfigEntry.SHOP_REACTIONS_ENABLED.getBoolean() - && !plugin.sh.reactionString.isEmpty() && message.equals(plugin.sh.reactionString)) + && !plugin.sh.getReactionString().isEmpty() && message.equals(plugin.sh.getReactionString())) { event.setCancelled(true); PlayerData data = plugin.pl.getData(player); - data.setCoins(data.getCoins() + plugin.sh.coinsPerReactionWin); + data.setCoins(data.getCoins() + plugin.sh.getCoinsPerReactionWin()); plugin.pl.save(data); plugin.sh.endReaction(player.getName()); player.sendMessage(ChatColor.GREEN + "You have been given " + ChatColor.GOLD - + plugin.sh.coinsPerReactionWin + ChatColor.GREEN + " coins!"); + + plugin.sh.getCoinsPerReactionWin() + ChatColor.GREEN + " coins!"); } } + @Override public ShopItem getShopItem(int slot) { for (ShopItem shopItem : ShopItem.values()) @@ -442,4 +461,10 @@ public class Shop extends FreedomService } return null; } + + @Override + public String getReactionString() + { + return reactionString; + } } \ No newline at end of file diff --git a/shop/src/main/java/me/totalfreedom/shop/TFShoppe.java b/shop/src/main/java/me/totalfreedom/shop/TFShoppe.java new file mode 100644 index 00000000..9c627c7d --- /dev/null +++ b/shop/src/main/java/me/totalfreedom/shop/TFShoppe.java @@ -0,0 +1,53 @@ +package me.totalfreedom.shop; + +import me.totalfreedom.totalfreedommod.TotalFreedomMod; +import me.totalfreedom.totalfreedommod.api.Context; +import me.totalfreedom.totalfreedommod.api.ShoppeCommons; +import org.bukkit.plugin.java.JavaPlugin; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TFShoppe extends JavaPlugin +{ + private final Logger slf4j = LoggerFactory.getLogger("TF-Shoppe"); + private Shop shop; + private Votifier votifier; + + public Logger slf4j() + { + return slf4j; + } + + @Override + public void onEnable() + { + slf4j().info("Hello from the TF-Shoppe! Attempting to resolve TotalFreedomMod..."); + + TotalFreedomMod plugin = (TotalFreedomMod) getServer().getPluginManager().getPlugin("TotalFreedomMod"); + if (plugin == null) + { + slf4j().error("TotalFreedomMod not found! Disabling..."); + onDisable(); + return; + } + + slf4j().info("TotalFreedomMod found! Registering the shop..."); + shop = new Shop(); + + slf4j().info("Shop registered! Registering the Votifier listener..."); + votifier = new Votifier(); + + slf4j().info("Votifier listener registered! Providing context to TFM..."); + Context context = new Context<>(shop); + plugin.ag.setShoppeContext(context); + plugin.registerShoppe(); + + slf4j().info("Context provided! TF-Shoppe is now ready to go!"); + } + + @Override + public void onDisable() + { + slf4j().info("Goodbye from the TF-Shoppe!"); + } +} diff --git a/shop/src/main/java/me/totalfreedom/shop/Votifier.java b/shop/src/main/java/me/totalfreedom/shop/Votifier.java index a9d583a6..37f50e37 100644 --- a/shop/src/main/java/me/totalfreedom/shop/Votifier.java +++ b/shop/src/main/java/me/totalfreedom/shop/Votifier.java @@ -16,11 +16,13 @@ public class Votifier extends FreedomService @Override public void onStart() { + // This is here to please SonarLint. } @Override public void onStop() { + // This is here to please SonarLint. } @EventHandler(priority = EventPriority.NORMAL)