Fix issues from testing

This commit is contained in:
Sam 2018-08-09 00:19:56 +01:00 committed by Alexander Meech
parent 6f0d29b20c
commit 3387c68fc2
3 changed files with 97 additions and 26 deletions

View File

@ -4,12 +4,14 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import net.md_5.bungee.api.ChatColor;
@ -25,6 +27,7 @@ import org.bukkit.event.player.PlayerJoinEvent;
import mineplex.core.MiniDbClientPlugin;
import mineplex.core.ReflectivelyCreateMiniPlugin;
import mineplex.core.account.ILoginProcessor;
import mineplex.core.account.permissions.Permission;
import mineplex.core.account.permissions.PermissionGroup;
import mineplex.core.common.util.C;
@ -46,6 +49,7 @@ import mineplex.core.portal.Portal;
import mineplex.core.preferences.Preference;
import mineplex.core.preferences.PreferencesManager;
import mineplex.core.preferences.UserPreferences;
import mineplex.core.recharge.Recharge;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.serverdata.commands.ServerCommandManager;
@ -62,6 +66,7 @@ public class FriendManager extends MiniDbClientPlugin<List<FriendStatus>>
}
private static final int MAX_FRIENDS = 500;
private static final long COOLDOWN = TimeUnit.SECONDS.toMillis(5);
private static final Comparator<FriendStatus> FRIEND_SORTER = (o1, o2) ->
{
// If not mutual
@ -70,16 +75,6 @@ public class FriendManager extends MiniDbClientPlugin<List<FriendStatus>>
return o1.Name.compareTo(o2.Name);
}
// Favourite
if (o1.Favourite && !o2.Favourite)
{
return -1;
}
else if (!o1.Favourite && o2.Favourite)
{
return 1;
}
// Online
if (o1.isOnline() && !o2.isOnline())
{
@ -90,8 +85,18 @@ public class FriendManager extends MiniDbClientPlugin<List<FriendStatus>>
return 1;
}
// Last Seen
return Long.compare(o1.LastSeenOnline, o2.LastSeenOnline);
// Favourite
if (o1.Favourite && !o2.Favourite)
{
return -1;
}
else if (!o1.Favourite && o2.Favourite)
{
return 1;
}
// Name
return o1.Name.compareTo(o2.Name);
};
public static Comparator<FriendStatus> getFriendSorter()
@ -108,6 +113,8 @@ public class FriendManager extends MiniDbClientPlugin<List<FriendStatus>>
private final FriendRepository _repository;
private final FriendShop _shop;
private final Map<UUID, FriendVisibility> _visibility;
private FriendManager()
{
super("Friends");
@ -119,6 +126,8 @@ public class FriendManager extends MiniDbClientPlugin<List<FriendStatus>>
_repository = new FriendRepository();
_shop = new FriendShop(this);
_visibility = new HashMap<>();
generatePermissions();
ServerCommandManager.getInstance().registerCommandType(FriendAddMessage.class, command ->
@ -141,6 +150,35 @@ public class FriendManager extends MiniDbClientPlugin<List<FriendStatus>>
onFriendAdd(target, command.getAccepter());
});
ClientManager.addStoredProcedureLoginProcessor(new ILoginProcessor()
{
@Override
public String getName()
{
return getName() + " Visibility";
}
@Override
public void processLoginResultSet(String playerName, UUID uuid, int accountId, ResultSet resultSet) throws SQLException
{
if (resultSet.next())
{
byte visibility = resultSet.getByte("status");
if (visibility > 0 && visibility < FriendVisibility.values().length)
{
_visibility.put(uuid, FriendVisibility.values()[visibility]);
}
}
}
@Override
public String getQuery(int accountId, String uuid, String name)
{
return "SELECT accountFriendData.status FROM accountFriendData WHERE accountId=" + accountId + ".";
}
});
}
private void generatePermissions()
@ -159,6 +197,11 @@ public class FriendManager extends MiniDbClientPlugin<List<FriendStatus>>
return _portal;
}
public FriendVisibility getVisibility(Player player)
{
return _visibility.get(player.getUniqueId());
}
@Override
public void addCommands()
{
@ -175,6 +218,12 @@ public class FriendManager extends MiniDbClientPlugin<List<FriendStatus>>
return Collections.emptyList();
}
@Override
public void saveData(String name, UUID uuid, int accountId)
{
_visibility.remove(uuid);
}
@EventHandler
public void updateFriends(UpdateEvent event)
{
@ -270,6 +319,11 @@ public class FriendManager extends MiniDbClientPlugin<List<FriendStatus>>
public void addFriend(final Player caller, final String name)
{
if (!Recharge.Instance.use(caller, "Friend " + name, COOLDOWN, false, false))
{
return;
}
if (caller.getName().equalsIgnoreCase(name))
{
caller.sendMessage(F.main(getName(), "You cannot add yourself as a friend"));
@ -371,6 +425,11 @@ public class FriendManager extends MiniDbClientPlugin<List<FriendStatus>>
public void removeFriend(final Player caller, final String name)
{
if (!Recharge.Instance.use(caller, "Friend " + name, COOLDOWN, false, false))
{
return;
}
runAsync(() ->
{
_repository.removeFriend(caller.getName(), name);
@ -392,32 +451,37 @@ public class FriendManager extends MiniDbClientPlugin<List<FriendStatus>>
});
}
public void setVisibility(Player player, FriendVisibility visibility)
public void setVisibility(Player caller, FriendVisibility visibility)
{
int accountId = ClientManager.getAccountId(player);
int accountId = ClientManager.getAccountId(caller);
runAsync(() ->
{
if (_repository.updateVisibility(accountId, visibility))
{
runSync(() -> player.sendMessage(F.main(getName(), "Updated your friend status to " + F.elem(visibility.getName()) + ".")));
runSync(() -> caller.sendMessage(F.main(getName(), "Updated your friend status to " + F.elem(visibility.getName()) + ".")));
}
});
}
public void toggleFavourite(Player player, String target, Runnable onSuccess)
public void toggleFavourite(Player caller, String target, Runnable onSuccess)
{
for (FriendStatus status : Get(player))
if (!Recharge.Instance.use(caller, "Friend " + target, COOLDOWN, false, false))
{
return;
}
for (FriendStatus status : Get(caller))
{
if (status.Name.equals(target))
{
runAsync(() ->
{
if (_repository.updateFavourite(player.getName(), status.Name, !status.Favourite))
if (_repository.updateFavourite(caller.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."));
caller.sendMessage(F.main(getName(), F.name(status.Name) + " is " + (status.Favourite ? "no longer" : "now") + " on your favourite friends."));
if (onSuccess != null)
{
@ -713,6 +777,8 @@ public class FriendManager extends MiniDbClientPlugin<List<FriendStatus>>
TextComponent message = new TextComponent("");
FriendVisibility playerVisibility = getVisibility(caller);
for (FriendVisibility visibility : FriendVisibility.values())
{
TextComponent vis = new TextComponent(visibility.getName().toUpperCase());
@ -721,6 +787,7 @@ public class FriendManager extends MiniDbClientPlugin<List<FriendStatus>>
vis.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Set your visibility to " + visibility.getName())
.create()));
vis.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, visiblityCommand + visibility));
vis.setUnderlined(visibility == playerVisibility);
if (message.getExtra() != null)
{

View File

@ -13,7 +13,7 @@ public class FriendFavouriteCommand extends CommandBase<FriendManager>
public FriendFavouriteCommand(FriendManager plugin)
{
super(plugin, Perm.FRIEND_COMMAND, COMMAND);
super(plugin, Perm.FRIEND_COMMAND, COMMAND, "ff");
}
@Override

View File

@ -35,7 +35,7 @@ public class FriendMainPage extends ShopPageBase<FriendManager, FriendShop>
REQUESTS(status -> status.Status == FriendStatusType.Pending || status.Status == FriendStatusType.Sent, new ItemBuilder(Material.RED_ROSE)
.setTitle(C.cYellowB + "Friend Requests")
.build()),
DELETE(status -> status.Status == FriendStatusType.Accepted, new ItemBuilder(Material.TNT)
DELETE(status -> status.Status == FriendStatusType.Accepted && !status.Favourite, new ItemBuilder(Material.TNT)
.setTitle(C.cRedB + "Delete Friends")
.build());
@ -131,9 +131,13 @@ public class FriendMainPage extends ShopPageBase<FriendManager, FriendShop>
slot = 47;
FriendVisibility playerVisiblity = getPlugin().getVisibility(getPlayer());
for (FriendVisibility visibility : FriendVisibility.values())
{
addButton(slot, visibility.getItemStack(), (player, clickType) ->
addButton(slot, new ItemBuilder(visibility.getItemStack())
.setGlow(visibility == playerVisiblity)
.build(), (player, clickType) ->
{
getPlugin().setVisibility(player, visibility);
player.closeInventory();
@ -189,13 +193,13 @@ public class FriendMainPage extends ShopPageBase<FriendManager, FriendShop>
statusString = C.cRed + "Offline";
}
builder.addLore("Where: " + statusString);
builder.addLore("Where: " + statusString, "");
if (online)
{
if (canJoin)
{
builder.addLore("", C.cGreen + "Click to join their server.");
builder.addLore(C.cGreen + "Click to join their server.", "");
}
}
else
@ -204,7 +208,7 @@ public class FriendMainPage extends ShopPageBase<FriendManager, FriendShop>
}
builder.addLore(
"Shift-Click to " + (status.Favourite ? "remove " + status.Name + " from" : "add " + status.Name + " to"),
"Shift-Click to " + (status.Favourite ? "remove them from" : "add them to"),
"your favourite friends."
);
@ -311,7 +315,7 @@ public class FriendMainPage extends ShopPageBase<FriendManager, FriendShop>
if (status.Favourite)
{
colour = C.cYellow;
colour = C.cYellow + "";
}
else if (status.isOnline())
{