From 61dfe111846d04d1080becdbec6ffc59e4805707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C4=B1rat=20Kaya?= <48600092+benfiratkaya@users.noreply.github.com> Date: Mon, 9 Sep 2024 00:14:19 +0300 Subject: [PATCH] 1.1.6: added leaderos-cache update command --- .../plugin/configuration/Language.java | 2 + .../plugin/configuration/lang/en.java | 2 + .../plugin/configuration/lang/tr.java | 2 + .../plugin/modules/cache/CacheModule.java | 5 ++ .../modules/cache/commands/CacheCommand.java | 53 +++++++++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 bukkit/src/main/java/net/leaderos/plugin/modules/cache/commands/CacheCommand.java diff --git a/bukkit/src/main/java/net/leaderos/plugin/configuration/Language.java b/bukkit/src/main/java/net/leaderos/plugin/configuration/Language.java index 6de6b1a90..bbf35c715 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/configuration/Language.java +++ b/bukkit/src/main/java/net/leaderos/plugin/configuration/Language.java @@ -280,6 +280,8 @@ public static class Credit extends OkaeriConfig { private String successfullyRemovedCredit = "{prefix} &aSuccessfully removed &e{amount} credit &afrom &b{target}&a."; private String receivedCredit = "{prefix} &aYou just received &e{amount} credit(s) &afrom &b{player}&a."; + + private String cacheUpdated = "{prefix} &aSuccessfully updated cache for &b{target}&a."; } /** diff --git a/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/en.java b/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/en.java index 94146b4aa..72a6c7932 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/en.java +++ b/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/en.java @@ -280,6 +280,8 @@ public static class Credit extends Language.Messages.Credit { private String successfullyRemovedCredit = "{prefix} &aSuccessfully removed &e{amount} credit &afrom &b{target}&a."; private String receivedCredit = "{prefix} &aYou just received &e{amount} credit(s) &afrom &b{player}&a."; + + private String cacheUpdated = "{prefix} &aSuccessfully updated cache for &b{target}&a."; } /** diff --git a/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/tr.java b/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/tr.java index d9bef28b4..96cee2f1b 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/tr.java +++ b/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/tr.java @@ -280,6 +280,8 @@ public static class Credit extends Language.Messages.Credit { private String successfullyRemovedCredit = "{prefix} &b{target} &aadlı oyuncunun &e{amount} kredisi &abaşarıyla silindi."; private String receivedCredit = "{prefix} &b{player} &aadlı oyuncudan &e{amount} kredi &aaldın."; + + private String cacheUpdated = "{prefix} &b{target} &aadlı oyuncunun önbelleğe alınmış verisi güncellendi."; } /** diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/cache/CacheModule.java b/bukkit/src/main/java/net/leaderos/plugin/modules/cache/CacheModule.java index 921963bc8..0f4097d17 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/cache/CacheModule.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/cache/CacheModule.java @@ -1,6 +1,7 @@ package net.leaderos.plugin.modules.cache; import net.leaderos.plugin.Bukkit; +import net.leaderos.plugin.modules.cache.commands.CacheCommand; import net.leaderos.plugin.modules.cache.listeners.CacheUpdateEvent; import net.leaderos.plugin.modules.cache.listeners.LoginListener; import net.leaderos.plugin.modules.cache.listeners.QuitListener; @@ -43,6 +44,8 @@ public void onEnable() { org.bukkit.Bukkit.getPluginManager().registerEvents(cacheUpdateEvent, Bukkit.getInstance()); // Loads all player data User.loadAllPlayers(); + // Register Command + Bukkit.getCommandManager().registerCommand(new CacheCommand()); // Placeholder loader if (org.bukkit.Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) new Placeholders().register(); @@ -57,6 +60,8 @@ public void onDisable() { HandlerList.unregisterAll(cacheUpdateEvent); // Removes cache User.getUserList().clear(); + // Unregister Command + Bukkit.getCommandManager().unregisterCommand(new CacheCommand()); // Placeholder unloader if( org.bukkit.Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) new Placeholders().unregister(); diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/cache/commands/CacheCommand.java b/bukkit/src/main/java/net/leaderos/plugin/modules/cache/commands/CacheCommand.java new file mode 100644 index 000000000..e3f736476 --- /dev/null +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/cache/commands/CacheCommand.java @@ -0,0 +1,53 @@ +package net.leaderos.plugin.modules.cache.commands; + +import dev.triumphteam.cmd.core.BaseCommand; +import dev.triumphteam.cmd.core.annotation.Command; +import dev.triumphteam.cmd.core.annotation.SubCommand; +import lombok.RequiredArgsConstructor; +import net.leaderos.plugin.Bukkit; +import net.leaderos.plugin.api.LeaderOSAPI; +import net.leaderos.plugin.api.handlers.UpdateCacheEvent; +import net.leaderos.plugin.helpers.ChatUtil; +import net.leaderos.shared.helpers.MoneyUtil; +import net.leaderos.shared.helpers.Placeholder; +import net.leaderos.shared.modules.credit.enums.UpdateType; +import org.bukkit.command.CommandSender; + +@RequiredArgsConstructor +@Command(value = "leaderos-cache") +public class CacheCommand extends BaseCommand { + /** + * Update player cache + * @param sender executor + * @param target player to update + */ + @SubCommand(value = "update", alias = {"reset"}) + public void updateCommand(CommandSender sender, String target) { + org.bukkit.Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getInstance(), () -> { + + // Check if the player is online + if (org.bukkit.Bukkit.getPlayer(target) == null) { + ChatUtil.sendMessage(sender, ChatUtil.replacePlaceholders( + Bukkit.getInstance().getLangFile().getMessages().getPlayerNotOnline() + )); + return; + } + + Double amount = LeaderOSAPI.getCreditManager().get(target); + + if (amount != null) { + org.bukkit.Bukkit.getScheduler().runTask(Bukkit.getInstance(), () -> org.bukkit.Bukkit.getPluginManager().callEvent(new UpdateCacheEvent(target, amount, UpdateType.SET))); + + ChatUtil.sendMessage(sender, ChatUtil.replacePlaceholders( + Bukkit.getInstance().getLangFile().getMessages().getCredit().getCacheUpdated(), + new Placeholder("{amount}", MoneyUtil.format(amount)), + new Placeholder("{target}", target) + )); + } + else + ChatUtil.sendMessage(sender, ChatUtil.replacePlaceholders( + Bukkit.getInstance().getLangFile().getMessages().getPlayerNotAvailable() + )); + }); + } +}