From b1a5b7ffb6fa9bc3b79513270878d727082f75b7 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 8 Aug 2018 14:57:16 +0100 Subject: [PATCH] Add back toggling the UI modes --- .../mineplex/core/friend/FriendManager.java | 93 ++++++++++++++++--- .../core/friend/command/AddFriend.java | 12 +-- .../core/friend/command/DeleteFriend.java | 3 +- .../command/FriendFavouriteCommand.java | 2 +- .../core/friend/command/FriendsDisplay.java | 23 +---- .../core/friend/ui/FriendMainPage.java | 19 +++- 6 files changed, 104 insertions(+), 48 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/friend/FriendManager.java b/Plugins/Mineplex.Core/src/mineplex/core/friend/FriendManager.java index 940b18007..e53d3a4f8 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/friend/FriendManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/friend/FriendManager.java @@ -18,6 +18,7 @@ import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Sound; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.player.PlayerJoinEvent; @@ -44,6 +45,7 @@ import mineplex.core.friend.ui.FriendShop; import mineplex.core.portal.Portal; import mineplex.core.preferences.Preference; import mineplex.core.preferences.PreferencesManager; +import mineplex.core.preferences.UserPreferences; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; import mineplex.serverdata.commands.ServerCommandManager; @@ -97,6 +99,8 @@ public class FriendManager extends MiniDbClientPlugin> return FRIEND_SORTER; } + private static final int FRIENDS_IN_CHAT = 2; + private final DonationManager _donationManager; private final PreferencesManager _preferenceManager; private final Portal _portal; @@ -150,11 +154,6 @@ public class FriendManager extends MiniDbClientPlugin> return _donationManager; } - public PreferencesManager getPreferenceManager() - { - return _preferenceManager; - } - public Portal getPortal() { return _portal; @@ -176,11 +175,6 @@ public class FriendManager extends MiniDbClientPlugin> return Collections.emptyList(); } - public void openShop(Player player) - { - _shop.attemptShopOpen(player); - } - @EventHandler public void updateFriends(UpdateEvent event) { @@ -411,7 +405,7 @@ public class FriendManager extends MiniDbClientPlugin> }); } - public void toggleFavourite(Player player, String target) + public void toggleFavourite(Player player, String target, Runnable onSuccess) { for (FriendStatus status : Get(player)) { @@ -421,7 +415,15 @@ public class FriendManager extends MiniDbClientPlugin> { if (_repository.updateFavourite(player.getName(), status.Name, !status.Favourite)) { - runSync(() -> player.sendMessage(F.main(getName(), F.name(status.Name) + " is " + (status.Favourite ? "no longer" : "now") + " on your favourite friends."))); + runSync(() -> + { + player.sendMessage(F.main(getName(), F.name(status.Name) + " is " + (status.Favourite ? "no longer" : "now") + " on your favourite friends.")); + + if (onSuccess != null) + { + onSuccess.run(); + } + }); } }); @@ -467,7 +469,33 @@ public class FriendManager extends MiniDbClientPlugin> viewer.spigot().sendMessage(message); } - public void showFriends(Player caller) + public void showFriends(Player player, boolean toggle) + { + UserPreferences preferences = _preferenceManager.get(player); + + if (toggle) + { + preferences.toggle(Preference.FRIENDS_DISPLAY_INVENTORY_UI); + _preferenceManager.save(preferences); + player.playSound(player.getLocation(), Sound.NOTE_PLING, 1, 1.6f); + } + + if (preferences.isActive(Preference.FRIENDS_DISPLAY_INVENTORY_UI)) + { + showFriendsInUI(player); + } + else + { + showFriendsInChat(player); + } + } + + public void showFriendsInUI(Player player) + { + _shop.attemptShopOpen(player); + } + + public void showFriendsInChat(Player caller) { boolean isStaff = ClientManager.Get(caller).hasPermission(Perm.JOIN_STAFF); boolean showPending = _preferenceManager.get(caller).isActive(Preference.PENDING_FRIEND_REQUESTS); @@ -475,8 +503,33 @@ public class FriendManager extends MiniDbClientPlugin> List friendStatuses = Get(caller); friendStatuses.sort(getFriendSorter().reversed()); + boolean compress = friendStatuses.size() > FRIENDS_IN_CHAT; + caller.sendMessage(C.cAqua + C.Strike + "======================[" + C.cWhiteB + "Friends" + C.cAqua + C.Strike + "]======================"); + if (compress) + { + int sent = 0, pending = 0; + + for (FriendStatus friend : friendStatuses) + { + switch (friend.Status) + { + case Sent: + sent++; + break; + case Pending: + pending++; + break; + } + } + + TextComponent compressed = new TextComponent(C.cGray + "Sent: " + F.count(sent) + " Pending: " + F.count(pending)); + compressed.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Shows all friends.") + .create())); + compressed.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/")); + } + // Use a LinkedHashMap so we maintain insertion order Map messages = new LinkedHashMap<>(); String joinCommand = "/server ", friendCommand = "/" + AddFriend.COMMAND + " ", unfriendCommand = "/" + DeleteFriend.COMMAND + " "; @@ -486,9 +539,19 @@ public class FriendManager extends MiniDbClientPlugin> { FriendStatusType type = friend.Status; - if (type == FriendStatusType.Blocked || type == FriendStatusType.Denied || type == FriendStatusType.Pending && !showPending) + if (compress) { - continue; + if (type != FriendStatusType.Accepted) + { + continue; + } + } + else + { + if (type == FriendStatusType.Blocked || type == FriendStatusType.Denied || type == FriendStatusType.Pending && !showPending) + { + continue; + } } TextComponent message = new TextComponent(""); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/friend/command/AddFriend.java b/Plugins/Mineplex.Core/src/mineplex/core/friend/command/AddFriend.java index d802a2255..6f0e02803 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/friend/command/AddFriend.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/friend/command/AddFriend.java @@ -7,6 +7,7 @@ import org.bukkit.entity.Player; import mineplex.core.command.CommandBase; import mineplex.core.friend.FriendManager; +import mineplex.core.friend.FriendManager.Perm; import mineplex.core.preferences.Preference; public class AddFriend extends CommandBase @@ -16,7 +17,7 @@ public class AddFriend extends CommandBase public AddFriend(FriendManager plugin) { - super(plugin, FriendManager.Perm.FRIEND_COMMAND, "friends", COMMAND, "f"); + super(plugin, Perm.FRIEND_COMMAND, "friends", COMMAND, "f"); } @Override @@ -24,14 +25,7 @@ public class AddFriend extends CommandBase { if (args == null || args.length < 1) { - if (Plugin.getPreferenceManager().get(caller).isActive(Preference.FRIENDS_DISPLAY_INVENTORY_UI)) - { - Plugin.openShop(caller); - } - else - { - Plugin.showFriends(caller); - } + Plugin.showFriends(caller, false); } else { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/friend/command/DeleteFriend.java b/Plugins/Mineplex.Core/src/mineplex/core/friend/command/DeleteFriend.java index dbc3814a1..ef4f413e0 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/friend/command/DeleteFriend.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/friend/command/DeleteFriend.java @@ -6,6 +6,7 @@ import mineplex.core.command.CommandBase; import mineplex.core.common.util.Callback; import mineplex.core.common.util.F; import mineplex.core.friend.FriendManager; +import mineplex.core.friend.FriendManager.Perm; public class DeleteFriend extends CommandBase { @@ -14,7 +15,7 @@ public class DeleteFriend extends CommandBase public DeleteFriend(FriendManager plugin) { - super(plugin, FriendManager.Perm.FRIEND_COMMAND, COMMAND); + super(plugin, Perm.FRIEND_COMMAND, COMMAND); } @Override diff --git a/Plugins/Mineplex.Core/src/mineplex/core/friend/command/FriendFavouriteCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/friend/command/FriendFavouriteCommand.java index fddc58356..b3db88f11 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/friend/command/FriendFavouriteCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/friend/command/FriendFavouriteCommand.java @@ -28,7 +28,7 @@ public class FriendFavouriteCommand extends CommandBase { if (result != null) { - Plugin.toggleFavourite(caller, result); + Plugin.toggleFavourite(caller, result, null); } }); } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/friend/command/FriendsDisplay.java b/Plugins/Mineplex.Core/src/mineplex/core/friend/command/FriendsDisplay.java index a592a58ce..21d1b15b5 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/friend/command/FriendsDisplay.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/friend/command/FriendsDisplay.java @@ -1,12 +1,10 @@ package mineplex.core.friend.command; -import org.bukkit.Sound; import org.bukkit.entity.Player; import mineplex.core.command.CommandBase; import mineplex.core.friend.FriendManager; -import mineplex.core.preferences.Preference; -import mineplex.core.preferences.UserPreferences; +import mineplex.core.friend.FriendManager.Perm; public class FriendsDisplay extends CommandBase { @@ -15,27 +13,12 @@ public class FriendsDisplay extends CommandBase public FriendsDisplay(FriendManager plugin) { - super(plugin, FriendManager.Perm.FRIEND_COMMAND, COMMAND); + super(plugin, Perm.FRIEND_COMMAND, COMMAND); } @Override public void Execute(Player caller, final String[] args) { - UserPreferences preferences = Plugin.getPreferenceManager().get(caller); - - preferences.toggle(Preference.FRIENDS_DISPLAY_INVENTORY_UI); - - Plugin.getPreferenceManager().save(preferences); - - caller.playSound(caller.getLocation(), Sound.NOTE_PLING, 1, 1.6f); - - if (preferences.isActive(Preference.FRIENDS_DISPLAY_INVENTORY_UI)) - { - - } - else - { - Plugin.runAsync(() -> Plugin.showFriends(caller)); - } + Plugin.showFriends(caller, true); } } \ No newline at end of file diff --git a/Plugins/Mineplex.Core/src/mineplex/core/friend/ui/FriendMainPage.java b/Plugins/Mineplex.Core/src/mineplex/core/friend/ui/FriendMainPage.java index 4023701c2..2874ea9ea 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/friend/ui/FriendMainPage.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/friend/ui/FriendMainPage.java @@ -67,7 +67,7 @@ public class FriendMainPage extends ShopPageBase @Override protected void buildPage() { - int slot = 1; + int slot = 0; for (FriendPageType pageType : FriendPageType.values()) { @@ -103,6 +103,17 @@ public class FriendMainPage extends ShopPageBase .openInventory(); }); + slot += 2; + + addButton(slot, new ItemBuilder(Material.SIGN) + .setTitle(C.cDAquaB + "Toggle GUI") + .addLore("", "Click to display your friends in", "chat instead of this chest.") + .build(), (player, clickType) -> + { + player.closeInventory(); + getPlugin().showFriends(player, true); + }); + _pageManager.buildPage(); } @@ -180,7 +191,11 @@ public class FriendMainPage extends ShopPageBase if (clickType.isShiftClick()) { playAcceptSound(player); - getPlugin().toggleFavourite(player, status.Name); + getPlugin().toggleFavourite(player, status.Name, () -> + { + status.Favourite = !status.Favourite; + buildItem(status, slot); + }); } else {