Add friends gui, deprecate the old chatbased friends display

This commit is contained in:
libraryaddict 2015-01-30 13:46:07 +13:00
parent e18107a82f
commit 505dcc1e8e
12 changed files with 1199 additions and 347 deletions

View File

@ -48,6 +48,11 @@ public class UtilInv
itemStack.removeEnchantment(_enchantment); itemStack.removeEnchantment(_enchantment);
} }
public static DullEnchantment getDullEnchantment()
{
return _enchantment;
}
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public static boolean insert(Player player, ItemStack stack) public static boolean insert(Player player, ItemStack stack)
{ {

View File

@ -28,327 +28,360 @@ import mineplex.core.friend.command.DeleteFriend;
import mineplex.core.friend.data.FriendData; import mineplex.core.friend.data.FriendData;
import mineplex.core.friend.data.FriendRepository; import mineplex.core.friend.data.FriendRepository;
import mineplex.core.friend.data.FriendStatus; import mineplex.core.friend.data.FriendStatus;
import mineplex.core.portal.Portal;
import mineplex.core.preferences.PreferencesManager; import mineplex.core.preferences.PreferencesManager;
import mineplex.core.updater.UpdateType; import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent; import mineplex.core.updater.event.UpdateEvent;
public class FriendManager extends MiniDbClientPlugin<FriendData> public class FriendManager extends MiniDbClientPlugin<FriendData>
{ {
private static FriendSorter _friendSorter = new FriendSorter(); private static FriendSorter _friendSorter = new FriendSorter();
private PreferencesManager _preferenceManager;
private FriendRepository _repository;
public FriendManager(JavaPlugin plugin, CoreClientManager clientManager, PreferencesManager preferences)
{
super("Friends", plugin, clientManager);
_preferenceManager = preferences;
_repository = new FriendRepository(plugin);
}
@Override
public void AddCommands()
{
addCommand(new AddFriend(this));
addCommand(new DeleteFriend(this));
}
@Override
protected FriendData AddPlayer(String player)
{
return new FriendData();
}
@EventHandler private PreferencesManager _preferenceManager;
public void updateFriends(UpdateEvent event) private FriendRepository _repository;
{ private Portal _portal;
if (event.getType() != UpdateType.SLOW || Bukkit.getOnlinePlayers().size() == 0)
return;
final Player[] onlinePlayers = UtilServer.getPlayers();
Bukkit.getServer().getScheduler().runTaskAsynchronously(_plugin, new Runnable()
{
public void run()
{
final NautHashMap<String, FriendData> newData = _repository.getFriendsForAll(onlinePlayers);
Bukkit.getServer().getScheduler().runTask(_plugin, new Runnable()
{
public void run()
{
for (Player player : Bukkit.getOnlinePlayers())
{
if (newData.containsKey(player.getUniqueId().toString()))
{
Get(player).Friends = newData.get(player.getUniqueId().toString()).Friends;
}
else
{
Get(player).Friends.clear();
}
}
}
});
}
});
}
public void addFriend(final Player caller, final String name) public FriendManager(JavaPlugin plugin, CoreClientManager clientManager, PreferencesManager preferences, Portal portal)
{ {
boolean update = false; super("Friends", plugin, clientManager);
for (FriendStatus status : Get(caller).Friends)
{
if (status.Name.equalsIgnoreCase(name))
{
if (status.Status == FriendStatusType.Pending || status.Status == FriendStatusType.Blocked)
{
update = true;
break;
}
else if (status.Status == FriendStatusType.Denied)
{
caller.sendMessage(F.main(getName(), ChatColor.GREEN + name + ChatColor.GRAY + " has denied your friend request."));
return;
}
else if (status.Status == FriendStatusType.Accepted)
{
caller.sendMessage(F.main(getName(), "You are already friends with " + ChatColor.GREEN + name));
return;
}
else if (status.Status == FriendStatusType.Sent)
{
caller.sendMessage(F.main(getName(), ChatColor.GREEN + name + ChatColor.GRAY + " has yet to respond to your friend request."));
return;
}
}
}
final boolean updateFinal = update;
Bukkit.getServer().getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
{
public void run()
{
if (updateFinal)
{
_repository.updateFriend(caller.getName(), name, "Accepted");
_repository.updateFriend(name, caller.getName(), "Accepted");
Bukkit.getServer().getScheduler().runTask(_plugin, new Runnable()
{
public void run()
{
for (Iterator<FriendStatus> statusIterator = Get(caller).Friends.iterator(); statusIterator.hasNext();)
{
FriendStatus status = statusIterator.next();
if (status.Name.equalsIgnoreCase(name))
{
status.Status = FriendStatusType.Accepted;
break;
}
}
}
});
}
else
{
_repository.addFriend(caller, name);
Bukkit.getServer().getScheduler().runTask(_plugin, new Runnable()
{
public void run()
{
for (Iterator<FriendStatus> statusIterator = Get(caller).Friends.iterator(); statusIterator.hasNext();)
{
FriendStatus status = statusIterator.next();
if (status.Name.equalsIgnoreCase(name))
{
status.Status = FriendStatusType.Sent;
break;
}
}
}
});
}
Bukkit.getServer().getScheduler().runTask(_plugin, new Runnable()
{
public void run()
{
if (updateFinal)
caller.sendMessage(F.main(getName(), "You and " + ChatColor.GREEN + name + ChatColor.GRAY + " are now friends!"));
else
caller.sendMessage(F.main(getName(), "Added " + ChatColor.GREEN + name + ChatColor.GRAY + " to your friends list!"));
}
});
}
});
}
public void removeFriend(final Player caller, final String name) _preferenceManager = preferences;
{ _repository = new FriendRepository(plugin);
Bukkit.getServer().getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable() _portal = portal;
{ }
public void run()
{ public Portal getPortal()
_repository.removeFriend(caller.getName(), name); {
_repository.removeFriend(name, caller.getName()); return _portal;
}
Bukkit.getServer().getScheduler().runTask(_plugin, new Runnable()
{
public void run()
{
for (Iterator<FriendStatus> statusIterator = Get(caller).Friends.iterator(); statusIterator.hasNext();)
{
FriendStatus status = statusIterator.next();
if (status.Name.equalsIgnoreCase(name))
{
status.Status = FriendStatusType.Blocked;
break;
}
}
caller.sendMessage(F.main(getName(), "Deleted " + ChatColor.GREEN + name + ChatColor.GRAY + " from your friends list!"));
}
});
}
});
}
public void showFriends(Player caller) @Override
{ public void AddCommands()
boolean isStaff = ClientManager.Get(caller).GetRank().Has(Rank.HELPER); {
boolean gotAFriend = false; addCommand(new AddFriend(this));
List<FriendStatus> friendStatuses = Get(caller).Friends; addCommand(new DeleteFriend(this));
Collections.sort(friendStatuses, _friendSorter); }
caller.sendMessage(C.cAqua + C.Strike + "======================[" + ChatColor.RESET + C.cWhite + C.Bold + "Friends" + ChatColor.RESET + C.cAqua + C.Strike + "]======================");
ArrayList<ChildJsonMessage> sentLines = new ArrayList<ChildJsonMessage>();
ArrayList<ChildJsonMessage> pendingLines = new ArrayList<ChildJsonMessage>();
ArrayList<ChildJsonMessage> onlineLines = new ArrayList<ChildJsonMessage>();
ArrayList<ChildJsonMessage> offlineLines = new ArrayList<ChildJsonMessage>();
for (FriendStatus friend : friendStatuses)
{
if (friend.Status == FriendStatusType.Blocked || friend.Status == FriendStatusType.Denied)
continue;
if (!_preferenceManager.Get(caller).PendingFriendRequests && friend.Status == FriendStatusType.Pending)
continue;
gotAFriend = true;
ChildJsonMessage message = new JsonMessage("").color("white").extra("").color("white");
if (friend.Status == FriendStatusType.Accepted) @Override
{ protected FriendData AddPlayer(String player)
//Online Friend {
if (friend.Online) return new FriendData();
{ }
if (friend.ServerName.contains("STAFF") || friend.ServerName.contains("CUST"))
{
if (isStaff && friend.ServerName.contains("STAFF"))
message.add("Teleport").color("green").bold().click("run_command", "/server " + friend.ServerName).hover("show_text", "Teleport to " + friend.Name + "'s server.");
else
message.add("No Teleport").color("yellow").bold();
}
else
message.add("Teleport").color("green").bold().click("run_command", "/server " + friend.ServerName).hover("show_text", "Teleport to " + friend.Name + "'s server.");
message.add(" - ").color("white");
message.add("Delete").color("red").bold().click("run_command", "/unfriend " + friend.Name).hover("show_text", "Remove " + friend.Name + " from your friends list.");
message.add(" - ").color("white");
message.add(friend.Name).color(friend.Online ? "green" : "gray");
message.add(" - ").color("white");
if (friend.ServerName.contains("STAFF") || friend.ServerName.contains("CUST"))
{
if (isStaff && friend.ServerName.contains("STAFF"))
message.add(friend.ServerName).color("dark_green");
else
message.add("Private Staff Server").color("dark_green");
}
else
message.add(friend.ServerName).color("dark_green");
onlineLines.add(message);
}
//Offline Friend
else
{
message.add("Delete").color("red").bold().click("run_command", "/unfriend " + friend.Name).hover("show_text", "Remove " + friend.Name + " from your friends list.");
message.add(" - ").color("white");
message.add(friend.Name).color(friend.Online ? "green" : "gray");
message.add(" - ").color("white");
message.add("Offline for ").color("gray").add(UtilTime.MakeStr(friend.LastSeenOnline)).color("gray");
offlineLines.add(message);
}
}
//Pending
else if (friend.Status == FriendStatusType.Pending)
{
message.add("Accept").color("green").bold().click("run_command", "/friend " + friend.Name).hover("show_text", "Accept " + friend.Name + "'s friend request.");
message.add(" - ").color("white");
message.add("Deny").color("red").bold().click("run_command", "/unfriend " + friend.Name).hover("show_text", "Deny " + friend.Name + "'s friend request.");
message.add(" - ").color("white");
message.add(friend.Name + " Requested Friendship").color("gray");
pendingLines.add(message);
}
//Sent
else if (friend.Status == FriendStatusType.Sent)
{
message.add("Cancel").color("red").bold().click("run_command", "/unfriend " + friend.Name).hover("show_text", "Cancel friend request to " + friend.Name);
message.add(" - ").color("white");
message.add(friend.Name + " Friendship Request").color("gray");
sentLines.add(message);
}
}
//Send In Order
for (JsonMessage msg : sentLines)
msg.sendToPlayer(caller);
for (JsonMessage msg : offlineLines)
msg.sendToPlayer(caller);
for (JsonMessage msg : pendingLines)
msg.sendToPlayer(caller);
for (JsonMessage msg : onlineLines)
msg.sendToPlayer(caller);
if (!gotAFriend)
{
caller.sendMessage(" ");
caller.sendMessage("Welcome to your Friends List!");
caller.sendMessage(" ");
caller.sendMessage("To add friends, type " + C.cGreen + "/friend <Player Name>");
caller.sendMessage(" ");
caller.sendMessage("Type " + C.cGreen + "/friend" + ChatColor.RESET + " at any time to interact with your friends!");
caller.sendMessage(" ");
}
caller.sendMessage(C.cAqua + C.Strike + "=====================================================");
}
@Override @EventHandler
public void processLoginResultSet(String playerName, ResultSet resultSet) throws SQLException public void updateFriends(UpdateEvent event)
{ {
Set(playerName, _repository.loadClientInformation(resultSet)); if (event.getType() != UpdateType.SLOW || Bukkit.getOnlinePlayers().size() == 0)
} return;
@Override final Player[] onlinePlayers = UtilServer.getPlayers();
public String getQuery(String uuid, String name)
{ Bukkit.getServer().getScheduler().runTaskAsynchronously(_plugin, new Runnable()
return "SELECT tA.Name, status, serverName, tA.lastLogin, now() FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget LEFT JOIN playerMap ON tA.name = playerName WHERE uuidSource = '" + uuid + "';"; {
} public void run()
{
final NautHashMap<String, FriendData> newData = _repository.getFriendsForAll(onlinePlayers);
Bukkit.getServer().getScheduler().runTask(_plugin, new Runnable()
{
public void run()
{
for (Player player : Bukkit.getOnlinePlayers())
{
if (newData.containsKey(player.getUniqueId().toString()))
{
Get(player).setFriends(newData.get(player.getUniqueId().toString()).getFriends());
}
else
{
Get(player).getFriends().clear();
}
}
}
});
}
});
}
public void addFriend(final Player caller, final String name)
{
if (caller.getName().equalsIgnoreCase(name))
{
caller.sendMessage(F.main(getName(), ChatColor.GRAY
+ "You cannot add yourself as a friend"));
return;
}
boolean update = false;
for (FriendStatus status : Get(caller).getFriends())
{
if (status.Name.equalsIgnoreCase(name))
{
if (status.Status == FriendStatusType.Pending || status.Status == FriendStatusType.Blocked)
{
update = true;
break;
}
else if (status.Status == FriendStatusType.Denied)
{
caller.sendMessage(F.main(getName(), ChatColor.GREEN + name + ChatColor.GRAY
+ " has denied your friend request."));
return;
}
else if (status.Status == FriendStatusType.Accepted)
{
caller.sendMessage(F.main(getName(), "You are already friends with " + ChatColor.GREEN + name));
return;
}
else if (status.Status == FriendStatusType.Sent)
{
caller.sendMessage(F.main(getName(), ChatColor.GREEN + name + ChatColor.GRAY
+ " has yet to respond to your friend request."));
return;
}
}
}
final boolean updateFinal = update;
Bukkit.getServer().getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
{
public void run()
{
if (updateFinal)
{
_repository.updateFriend(caller.getName(), name, "Accepted");
_repository.updateFriend(name, caller.getName(), "Accepted");
Bukkit.getServer().getScheduler().runTask(_plugin, new Runnable()
{
public void run()
{
for (Iterator<FriendStatus> statusIterator = Get(caller).getFriends().iterator(); statusIterator
.hasNext();)
{
FriendStatus status = statusIterator.next();
if (status.Name.equalsIgnoreCase(name))
{
status.Status = FriendStatusType.Accepted;
break;
}
}
}
});
}
else
{
_repository.addFriend(caller, name);
Bukkit.getServer().getScheduler().runTask(_plugin, new Runnable()
{
public void run()
{
for (Iterator<FriendStatus> statusIterator = Get(caller).getFriends().iterator(); statusIterator
.hasNext();)
{
FriendStatus status = statusIterator.next();
if (status.Name.equalsIgnoreCase(name))
{
status.Status = FriendStatusType.Sent;
break;
}
}
}
});
}
Bukkit.getServer().getScheduler().runTask(_plugin, new Runnable()
{
public void run()
{
if (updateFinal)
caller.sendMessage(F.main(getName(), "You and " + ChatColor.GREEN + name + ChatColor.GRAY
+ " are now friends!"));
else
caller.sendMessage(F.main(getName(), "Added " + ChatColor.GREEN + name + ChatColor.GRAY
+ " to your friends list!"));
}
});
}
});
}
public void removeFriend(final Player caller, final String name)
{
Bukkit.getServer().getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
{
public void run()
{
_repository.removeFriend(caller.getName(), name);
_repository.removeFriend(name, caller.getName());
Bukkit.getServer().getScheduler().runTask(_plugin, new Runnable()
{
public void run()
{
for (Iterator<FriendStatus> statusIterator = Get(caller).getFriends().iterator(); statusIterator
.hasNext();)
{
FriendStatus status = statusIterator.next();
if (status.Name.equalsIgnoreCase(name))
{
status.Status = FriendStatusType.Blocked;
break;
}
}
caller.sendMessage(F.main(getName(), "Deleted " + ChatColor.GREEN + name + ChatColor.GRAY
+ " from your friends list!"));
}
});
}
});
}
@Deprecated
public void showFriends(Player caller)
{
boolean isStaff = ClientManager.Get(caller).GetRank().Has(Rank.HELPER);
boolean gotAFriend = false;
List<FriendStatus> friendStatuses = Get(caller).getFriends();
Collections.sort(friendStatuses, _friendSorter);
caller.sendMessage(C.cAqua + C.Strike + "======================[" + ChatColor.RESET + C.cWhite + C.Bold + "Friends"
+ ChatColor.RESET + C.cAqua + C.Strike + "]======================");
ArrayList<ChildJsonMessage> sentLines = new ArrayList<ChildJsonMessage>();
ArrayList<ChildJsonMessage> pendingLines = new ArrayList<ChildJsonMessage>();
ArrayList<ChildJsonMessage> onlineLines = new ArrayList<ChildJsonMessage>();
ArrayList<ChildJsonMessage> offlineLines = new ArrayList<ChildJsonMessage>();
for (FriendStatus friend : friendStatuses)
{
if (friend.Status == FriendStatusType.Blocked || friend.Status == FriendStatusType.Denied)
continue;
if (!_preferenceManager.Get(caller).PendingFriendRequests && friend.Status == FriendStatusType.Pending)
continue;
gotAFriend = true;
ChildJsonMessage message = new JsonMessage("").color("white").extra("").color("white");
if (friend.Status == FriendStatusType.Accepted)
{
// Online Friend
if (friend.Online)
{
if (friend.ServerName.contains("STAFF") || friend.ServerName.contains("CUST"))
{
if (isStaff && friend.ServerName.contains("STAFF"))
message.add("Teleport").color("green").bold().click("run_command", "/server " + friend.ServerName)
.hover("show_text", "Teleport to " + friend.Name + "'s server.");
else
message.add("No Teleport").color("yellow").bold();
}
else
message.add("Teleport").color("green").bold().click("run_command", "/server " + friend.ServerName)
.hover("show_text", "Teleport to " + friend.Name + "'s server.");
message.add(" - ").color("white");
message.add("Delete").color("red").bold().click("run_command", "/unfriend " + friend.Name)
.hover("show_text", "Remove " + friend.Name + " from your friends list.");
message.add(" - ").color("white");
message.add(friend.Name).color(friend.Online ? "green" : "gray");
message.add(" - ").color("white");
if (friend.ServerName.contains("STAFF") || friend.ServerName.contains("CUST"))
{
if (isStaff && friend.ServerName.contains("STAFF"))
message.add(friend.ServerName).color("dark_green");
else
message.add("Private Staff Server").color("dark_green");
}
else
message.add(friend.ServerName).color("dark_green");
onlineLines.add(message);
}
// Offline Friend
else
{
message.add("Delete").color("red").bold().click("run_command", "/unfriend " + friend.Name)
.hover("show_text", "Remove " + friend.Name + " from your friends list.");
message.add(" - ").color("white");
message.add(friend.Name).color(friend.Online ? "green" : "gray");
message.add(" - ").color("white");
message.add("Offline for ").color("gray").add(UtilTime.MakeStr(friend.LastSeenOnline)).color("gray");
offlineLines.add(message);
}
}
// Pending
else if (friend.Status == FriendStatusType.Pending)
{
message.add("Accept").color("green").bold().click("run_command", "/friend " + friend.Name)
.hover("show_text", "Accept " + friend.Name + "'s friend request.");
message.add(" - ").color("white");
message.add("Deny").color("red").bold().click("run_command", "/unfriend " + friend.Name)
.hover("show_text", "Deny " + friend.Name + "'s friend request.");
message.add(" - ").color("white");
message.add(friend.Name + " Requested Friendship").color("gray");
pendingLines.add(message);
}
// Sent
else if (friend.Status == FriendStatusType.Sent)
{
message.add("Cancel").color("red").bold().click("run_command", "/unfriend " + friend.Name)
.hover("show_text", "Cancel friend request to " + friend.Name);
message.add(" - ").color("white");
message.add(friend.Name + " Friendship Request").color("gray");
sentLines.add(message);
}
}
// Send In Order
for (JsonMessage msg : sentLines)
msg.sendToPlayer(caller);
for (JsonMessage msg : offlineLines)
msg.sendToPlayer(caller);
for (JsonMessage msg : pendingLines)
msg.sendToPlayer(caller);
for (JsonMessage msg : onlineLines)
msg.sendToPlayer(caller);
if (!gotAFriend)
{
caller.sendMessage(" ");
caller.sendMessage("Welcome to your Friends List!");
caller.sendMessage(" ");
caller.sendMessage("To add friends, type " + C.cGreen + "/friend <Player Name>");
caller.sendMessage(" ");
caller.sendMessage("Type " + C.cGreen + "/friend" + ChatColor.RESET + " at any time to interact with your friends!");
caller.sendMessage(" ");
}
caller.sendMessage(C.cAqua + C.Strike + "=====================================================");
}
@Override
public void processLoginResultSet(String playerName, ResultSet resultSet) throws SQLException
{
Set(playerName, _repository.loadClientInformation(resultSet));
}
@Override
public String getQuery(String uuid, String name)
{
return "SELECT tA.Name, status, serverName, tA.lastLogin, now() FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget LEFT JOIN playerMap ON tA.name = playerName WHERE uuidSource = '"
+ uuid + "';";
}
} }

View File

@ -5,35 +5,35 @@ import org.bukkit.entity.Player;
import mineplex.core.command.CommandBase; import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank; import mineplex.core.common.Rank;
import mineplex.core.common.util.Callback; import mineplex.core.common.util.Callback;
import mineplex.core.common.util.F;
import mineplex.core.friend.FriendManager; import mineplex.core.friend.FriendManager;
import mineplex.core.friend.ui.FriendsGUI;
public class AddFriend extends CommandBase<FriendManager> public class AddFriend extends CommandBase<FriendManager>
{ {
public AddFriend(FriendManager plugin) public AddFriend(FriendManager plugin)
{ {
super(plugin, Rank.ALL, "friend", "f"); super(plugin, Rank.ALL, "friends", "friend", "f");
} }
@Override @Override
public void Execute(final Player caller, final String[] args) public void Execute(final Player caller, final String[] args)
{ {
if (args == null) if (args == null)
{ {
Plugin.showFriends(caller); new FriendsGUI(Plugin, caller);
} }
else else
{ {
CommandCenter.GetClientManager().checkPlayerName(caller, args[0], new Callback<String>() CommandCenter.GetClientManager().checkPlayerName(caller, args[0], new Callback<String>()
{ {
public void run(String result) public void run(String result)
{ {
if (result != null) if (result != null)
{ {
Plugin.addFriend(caller, result); Plugin.addFriend(caller, result);
} }
} }
}); });
} }
} }
} }

View File

@ -1,9 +1,39 @@
package mineplex.core.friend.data; package mineplex.core.friend.data;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import mineplex.core.friend.ui.FriendsGUI;
public class FriendData public class FriendData
{ {
public List<FriendStatus> Friends = new ArrayList<FriendStatus>(); private ArrayList<FriendStatus> _friends = new ArrayList<FriendStatus>();
private FriendsGUI _friendsPage;
public ArrayList<FriendStatus> getFriends()
{
return _friends;
}
public void setFriends(ArrayList<FriendStatus> newFriends)
{
_friends = newFriends;
updateGui();
}
private void updateGui()
{
if (_friendsPage != null)
{
_friendsPage.updateGui();
}
}
public void setGui(FriendsGUI friendsPage)
{
_friendsPage = friendsPage;
}
public FriendsGUI getGui()
{
return _friendsPage;
}
} }

View File

@ -95,7 +95,7 @@ public class FriendRepository extends RepositoryBase
if (!friends.containsKey(uuidSource)) if (!friends.containsKey(uuidSource))
friends.put(uuidSource, new FriendData()); friends.put(uuidSource, new FriendData());
friends.get(uuidSource).Friends.add(friend); friends.get(uuidSource).getFriends().add(friend);
} }
} }
}); });
@ -116,7 +116,7 @@ public class FriendRepository extends RepositoryBase
friend.ServerName = resultSet.getString(3); friend.ServerName = resultSet.getString(3);
friend.LastSeenOnline = resultSet.getTimestamp(5).getTime() - resultSet.getTimestamp(4).getTime(); friend.LastSeenOnline = resultSet.getTimestamp(5).getTime() - resultSet.getTimestamp(4).getTime();
friendData.Friends.add(friend); friendData.getFriends().add(friend);
} }
return friendData; return friendData;

View File

@ -4,9 +4,12 @@ import mineplex.core.friend.FriendStatusType;
public class FriendStatus public class FriendStatus
{ {
public String Name; public String Name;
public String ServerName; public String ServerName;
public boolean Online; public boolean Online;
public long LastSeenOnline; /**
public FriendStatusType Status; * This seems like it should be unmodified without current time subtracted when set
*/
public long LastSeenOnline;
public FriendStatusType Status;
} }

View File

@ -0,0 +1,186 @@
package mineplex.core.friend.ui;
import mineplex.core.command.CommandCenter;
import mineplex.core.common.util.Callback;
import mineplex.core.friend.FriendManager;
import mineplex.core.itemstack.ItemBuilder;
import net.minecraft.server.v1_7_R4.ContainerAnvil;
import net.minecraft.server.v1_7_R4.EntityHuman;
import net.minecraft.server.v1_7_R4.EntityPlayer;
import net.minecraft.server.v1_7_R4.PacketPlayOutOpenWindow;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
public class AddFriendPage implements Listener
{
private class AnvilContainer extends ContainerAnvil
{
private String n;
public AnvilContainer(EntityHuman entity)
{
super(entity.inventory, entity.world, 0, 0, 0, entity);
}
@Override
public boolean a(EntityHuman entityhuman)
{
return true;
}
@Override
public void a(String origString)
{
n = origString;
_itemName = origString;
/* if (!Objects.equal(origString, itemName))
{
ItemBuilder builder = new ItemBuilder(Material.PAPER);
ItemStack item = currentInventory.getItem(2);
if (item != null && item.getType() != Material.AIR)
{
builder = new ItemBuilder(item);
}
builder.setRawTitle(origString);
itemName = origString;
currentInventory.setItem(2, builder.build());
}*/
if (getSlot(2).hasItem())
{
net.minecraft.server.v1_7_R4.ItemStack itemstack = getSlot(2).getItem();
if (StringUtils.isBlank(origString))
itemstack.t();
else
{
itemstack.c(this.n);
}
}
e();
}
}
private FriendManager _friends;
private Player _player;
private Inventory _currentInventory;
private String _itemName = "";
private boolean _searching;
public AddFriendPage(FriendManager friends, Player player)
{
_player = player;
_friends = friends;
openInventory();
friends.RegisterEvents(this);
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent event)
{
if (event.getPlayer() == _player)
{
unregisterListener();
}
}
public void unregisterListener()
{
_currentInventory.clear();
HandlerList.unregisterAll(this);
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event)
{
if (event.getRawSlot() < 3)
{
event.setCancelled(true);
if (event.getRawSlot() == 2)
{
if (_itemName.length() > 1 && !_searching)
{
_searching = true;
CommandCenter.Instance.GetClientManager().checkPlayerName(_player, _itemName, new Callback<String>()
{
public void run(String result)
{
_searching = false;
if (result != null)
{
_friends.addFriend(_player, result);
_player.playSound(_player.getLocation(), Sound.NOTE_PLING, 1, 1.6f);
unregisterListener();
new FriendsGUI(_friends, _player);
}
else
{
// _player.sendMessage(C.Bold + "Player not found");
_player.playSound(_player.getLocation(), Sound.ITEM_BREAK, 1, .6f);
}
}
});
}
else
{
_player.playSound(_player.getLocation(), Sound.ITEM_BREAK, 1, .6f);
}
}
}
else if (event.isShiftClick())
{
event.setCancelled(true);
}
}
public void openInventory()
{
_player.closeInventory();
EntityPlayer p = ((CraftPlayer) _player).getHandle();
AnvilContainer container = new AnvilContainer(p);
int c = p.nextContainerCounter();
PacketPlayOutOpenWindow packet = new PacketPlayOutOpenWindow(c, 8, "Repairing", 0, true);
p.playerConnection.sendPacket(packet);
// Set their active container to the container
p.activeContainer = container;
// Set their active container window id to that counter stuff
p.activeContainer.windowId = c;
// Add the slot listener
p.activeContainer.addSlotListener(p); // Set the items to the items from the inventory given
_currentInventory = container.getBukkitView().getTopInventory();
_currentInventory.setItem(0, new ItemBuilder(Material.PAPER).setRawTitle("Friend's Name").build());
_currentInventory.setItem(2, new ItemBuilder(Material.PAPER).setRawTitle("Search").build());
}
}

View File

@ -0,0 +1,36 @@
package mineplex.core.friend.ui;
import mineplex.core.itemstack.ItemBuilder;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
public enum FriendPage
{
FRIENDS(new ItemBuilder(Material.SKULL_ITEM, 1, (short) 3).setTitle("Friends").build(), "Friends"),
FRIEND_REQUESTS(new ItemBuilder(Material.RED_ROSE).setTitle("Friend Requests").build(), "Friend Requests"),
DELETE_FRIENDS(new ItemBuilder(Material.TNT).setTitle("Delete Friends").build(), "Delete Friends"),
SEND_REQUEST(new ItemBuilder(Material.BOOK_AND_QUILL).setTitle("Send Friend Request").build(), "Send Friend Request");
private ItemStack _icon;
private String _name;
private FriendPage(ItemStack item, String name)
{
_icon = item;
_name = name;
}
public String getName()
{
return _name;
}
public ItemStack getIcon()
{
return _icon;
}
}

View File

@ -0,0 +1,551 @@
package mineplex.core.friend.ui;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import mineplex.core.command.CommandCenter;
import mineplex.core.common.util.C;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.common.util.UtilInv;
import mineplex.core.common.util.UtilTime;
import mineplex.core.friend.FriendManager;
import mineplex.core.friend.FriendStatusType;
import mineplex.core.friend.data.FriendData;
import mineplex.core.friend.data.FriendStatus;
import mineplex.core.itemstack.ItemBuilder;
import mineplex.core.itemstack.ItemLayout;
import mineplex.core.shop.item.IButton;
import net.minecraft.server.v1_7_R4.EntityPlayer;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
public class FriendsGUI implements Listener
{
private NautHashMap<Integer, IButton> _buttonMap = new NautHashMap<Integer, IButton>();
private FriendPage _currentPage = FriendPage.FRIENDS;
private FriendPage _previousPage;
private Player _player;
private FriendManager _plugin;
private Inventory _inventory;
private int _page;
private Comparator<FriendStatus> _friendCompare = new Comparator<FriendStatus>()
{
@Override
public int compare(FriendStatus o1, FriendStatus o2)
{
if (o1.Online == o2.Online)
{
return o1.Name.compareToIgnoreCase(o2.Name);
}
if (o1.Online)
{
return -1;
}
return 1;
}
};
public FriendsGUI(FriendManager plugin, Player player)
{
_plugin = plugin;
_player = player;
buildPage();
_plugin.RegisterEvents(this);
getFriendData().setGui(this);
}
private void AddButton(int slot, ItemStack item, IButton button)
{
_inventory.setItem(slot, item);
_buttonMap.put(slot, button);
}
public void buildDeleteFriends()
{
ArrayList<FriendStatus> friends = new ArrayList<FriendStatus>();
for (FriendStatus friend : getFriendData().getFriends())
{
if (friend.Status == FriendStatusType.Accepted)
{
friends.add(friend);
}
}
Collections.sort(friends, _friendCompare);
boolean pages = this.addPages(friends.size(), new Runnable()
{
public void run()
{
buildDeleteFriends();
}
});
for (int i = 0; i < (pages ? 27 : 36); i++)
{
int friendSlot = (_page * 27) + i;
int slot = i + 18;
if (friendSlot >= friends.size())
{
break;
}
FriendStatus friend = friends.get(friendSlot);
ItemBuilder builder = new ItemBuilder(Material.SKULL_ITEM, 1, (short) (friend.Online ? 3 : 0));
builder.setTitle(C.cWhite + C.Bold + friend.Name);
builder.addLore(C.cGray + C.Bold + "Status: " + (friend.Online ? C.cDGreen + "Online" : C.cRed + "Offline"));
if (friend.Online)
{
builder.addLore(C.cDGray + C.Bold + "Server: " + C.cGray + friend.ServerName);
}
else
{
builder.addLore(C.cGray + "Last seen " + UtilTime.MakeStr(friend.LastSeenOnline) + " ago");
}
builder.addLore("");
builder.addLore(C.cGray + "Left click to delete from friends");
final String name = friend.Name;
AddButton(slot, builder.build(), new IButton()
{
@Override
public void onClick(Player player, ClickType clickType)
{
if (clickType.isLeftClick())
{
CommandCenter.Instance.OnPlayerCommandPreprocess(new PlayerCommandPreprocessEvent(player, "/unfriend "
+ name));
}
}
});
}
}
private boolean addPages(int amount, final Runnable runnable)
{
if (amount > 36)
{
if (_page > 0)
{
AddButton(45, new ItemBuilder(Material.SIGN).setTitle("Previous Page").build(), new IButton()
{
@Override
public void onClick(Player player, ClickType clickType)
{
Iterator<Integer> itel = _buttonMap.keySet().iterator();
while (itel.hasNext())
{
int slot = itel.next();
if (slot > 8)
{
itel.remove();
_inventory.setItem(slot, new ItemStack(Material.AIR));
}
}
_page -= 1;
runnable.run();
}
});
}
if (amount > (_page + 1) * 27)
{
AddButton(53, new ItemBuilder(Material.SIGN).setTitle("Next Page").build(), new IButton()
{
@Override
public void onClick(Player player, ClickType clickType)
{
Iterator<Integer> itel = _buttonMap.keySet().iterator();
while (itel.hasNext())
{
int slot = itel.next();
if (slot > 8)
{
itel.remove();
_inventory.setItem(slot, new ItemStack(Material.AIR));
}
}
_page += 1;
runnable.run();
}
});
}
return true;
}
return false;
}
private void buildFriends()
{
ArrayList<FriendStatus> friends = new ArrayList<FriendStatus>();
for (FriendStatus friend : getFriendData().getFriends())
{
if (friend.Status == FriendStatusType.Accepted)
{
friends.add(friend);
}
}
Collections.sort(friends, _friendCompare);
boolean pages = addPages(friends.size(), new Runnable()
{
@Override
public void run()
{
buildFriends();
}
});
for (int i = 0; i < (pages ? 27 : 36); i++)
{
int friendSlot = (_page * 27) + i;
int slot = i + 18;
if (friendSlot >= friends.size())
{
break;
}
FriendStatus friend = friends.get(friendSlot);
ItemBuilder builder = new ItemBuilder(Material.SKULL_ITEM, 1, (short) (friend.Online ? 3 : 0));
builder.setTitle(C.cWhite + C.Bold + friend.Name);
builder.addLore(C.cGray + C.Bold + "Status: " + (friend.Online ? C.cDGreen + "Online" : C.cRed + "Offline"));
if (friend.Online)
{
builder.addLore(C.cDGray + C.Bold + "Server: " + C.cGray + friend.ServerName);
}
else
{
builder.addLore(C.cGray + "Last seen " + UtilTime.MakeStr(friend.LastSeenOnline) + " ago");
}
if (friend.Online)
{
builder.addLore("");
builder.addLore(C.cGray + "Left click to teleport to their server");
final String serverName = friend.ServerName;
AddButton(slot, builder.build(), new IButton()
{
@Override
public void onClick(Player player, ClickType clickType)
{
_plugin.getPortal().sendPlayerToServer(player, serverName);
}
});
}
else
{
_inventory.setItem(slot, builder.build());
}
}
}
public void updateGui()
{
if (_currentPage == FriendPage.FRIENDS)
{
buildFriends();
}
else if (_currentPage == FriendPage.FRIEND_REQUESTS)
{
buildRequests();
}
else if (_currentPage == FriendPage.DELETE_FRIENDS)
{
buildDeleteFriends();
}
}
private void buildPage()
{
if (_currentPage != _previousPage)
{
_inventory = Bukkit.createInventory(null, 54, _currentPage.getName());
}
else
{
_inventory.setItem(53, new ItemStack(Material.AIR));
_inventory.setItem(45, new ItemStack(Material.AIR));
}
_page = 0;
_buttonMap.clear();
ArrayList<Integer> itemSlots = new ItemLayout("XOXOXOXOX").getItemSlots();
for (int i = 0; i < FriendPage.values().length; i++)
{
final FriendPage page = FriendPage.values()[i];
ItemStack icon = page == _currentPage ?
new ItemBuilder(page.getIcon()).addEnchantment(UtilInv.getDullEnchantment(), 1).build()
:
page.getIcon();
this.AddButton(itemSlots.get(i), icon, new IButton()
{
@Override
public void onClick(Player player, ClickType clickType)
{
if (_currentPage != page || _page != 0)
{
_currentPage = page;
buildPage();
}
}
});
}
if (_currentPage == FriendPage.FRIENDS)
{
buildFriends();
}
else if (_currentPage == FriendPage.FRIEND_REQUESTS)
{
buildRequests();
}
else if (_currentPage == FriendPage.DELETE_FRIENDS)
{
buildDeleteFriends();
}
else if (_currentPage == FriendPage.SEND_REQUEST)
{
unregisterListener();
new AddFriendPage(_plugin, _player);
return;
}
if (_previousPage != _currentPage)
{
_previousPage = _currentPage;
EntityPlayer nmsPlayer = ((CraftPlayer) _player).getHandle();
if (nmsPlayer.activeContainer != nmsPlayer.defaultContainer)
{
// Do this so that other inventories know their time is over.
CraftEventFactory.handleInventoryCloseEvent(nmsPlayer);
nmsPlayer.m();
}
_player.openInventory(_inventory);
}
}
private void buildRequests()
{
ArrayList<FriendStatus> friends = new ArrayList<FriendStatus>();
for (FriendStatus friend : getFriendData().getFriends())
{
if (friend.Status == FriendStatusType.Sent || friend.Status == FriendStatusType.Pending)
{
friends.add(friend);
}
}
Collections.sort(friends, new Comparator<FriendStatus>()
{
@Override
public int compare(FriendStatus o1, FriendStatus o2)
{
if (o1.Status == o2.Status)
{
return o1.Name.compareToIgnoreCase(o2.Name);
}
if (o1.Status == FriendStatusType.Sent)
{
return -1;
}
return 1;
}
});
boolean pages = addPages(friends.size(), new Runnable()
{
@Override
public void run()
{
buildRequests();
}
});
for (int i = 0; i < (pages ? 27 : 36); i++)
{
int friendSlot = (_page * 27) + i;
final int slot = i + 18;
if (friendSlot >= friends.size())
{
break;
}
FriendStatus friend = friends.get(friendSlot);
ItemBuilder builder;
final boolean isSender = friend.Status == FriendStatusType.Sent;
if (isSender)
{
builder = new ItemBuilder(Material.ENDER_PEARL);
builder.setTitle(C.cGray + "Friend request to " + C.cWhite + C.Bold + friend.Name);
}
else
{
builder = new ItemBuilder(Material.PAPER);
builder.setTitle(C.cGray + "Friend request from " + C.cWhite + C.Bold + friend.Name);
}
builder.addLore("");
builder.addLore(C.cGray + (isSender ? "C" : "Left c") + "lick to " + (isSender ? "cancel" : "accept")
+ " friend request");
if (!isSender)
{
builder.addLore(C.cGray + "Right click to refuse friend request");
}
final String name = friend.Name;
AddButton(slot, builder.build(), new IButton()
{
@Override
public void onClick(Player player, ClickType clickType)
{
if (isSender || clickType.isRightClick())
{
CommandCenter.Instance.OnPlayerCommandPreprocess(new PlayerCommandPreprocessEvent(player, "/unfriend "
+ name));
_inventory.setItem(slot, new ItemStack(Material.AIR));
_buttonMap.remove(slot);
}
else if (!isSender && clickType.isLeftClick())
{
CommandCenter.Instance.OnPlayerCommandPreprocess(new PlayerCommandPreprocessEvent(player, "/friend "
+ name));
_inventory.setItem(slot, new ItemStack(Material.AIR));
_buttonMap.remove(slot);
}
}
});
}
}
private FriendData getFriendData()
{
return _plugin.Get(_player);
}
@EventHandler
public void OnInventoryClick(InventoryClickEvent event)
{
if (_inventory.getTitle().equals(event.getInventory().getTitle()) && event.getWhoClicked() == _player)
{
if (_buttonMap.containsKey(event.getRawSlot()))
{
if (event.getWhoClicked() instanceof Player)
{
IButton button = _buttonMap.get(event.getRawSlot());
button.onClick(((Player) event.getWhoClicked()), event.getClick());
_player.playSound(_player.getLocation(), Sound.NOTE_PLING, 1, 1.6f);
}
}
else
{
_player.playSound(_player.getLocation(), Sound.ITEM_BREAK, 1, .6f);
}
event.setCancelled(true);
}
}
@EventHandler
public void OnInventoryClose(InventoryCloseEvent event)
{
if (_inventory.getTitle().equals(event.getInventory().getTitle()) && event.getPlayer() == _player)
{
unregisterListener();
}
}
private void unregisterListener()
{
FriendData data = getFriendData();
if (data != null && data.getGui() == this)
{
data.setGui(null);
}
HandlerList.unregisterAll(this);
}
}

View File

@ -37,6 +37,11 @@ public class ItemLayout
} }
} }
public ArrayList<Integer> getItemSlots()
{
return _size;
}
public ItemStack[] generate(ArrayList<ItemStack> items) public ItemStack[] generate(ArrayList<ItemStack> items)
{ {
return generate(items.toArray(new ItemStack[0])); return generate(items.toArray(new ItemStack[0]));
@ -50,8 +55,10 @@ public class ItemLayout
public ItemStack[] generate(boolean doRepeats, ItemStack... items) public ItemStack[] generate(boolean doRepeats, ItemStack... items)
{ {
ItemStack[] itemArray = new ItemStack[_invSize]; ItemStack[] itemArray = new ItemStack[_invSize];
if (items.length == 0) if (items.length == 0)
return itemArray; return itemArray;
int i = 0; int i = 0;
for (int slot : _size) for (int slot : _size)
{ {
@ -62,6 +69,7 @@ public class ItemLayout
else else
break; break;
} }
itemArray[slot] = items[i]; itemArray[slot] = items[i];
} }

View File

@ -137,7 +137,7 @@ public class Hub extends JavaPlugin implements IRelation
new ClassCombatShop(shopManager, clientManager, donationManager, false, "Knight", classManager.GetClass("Knight")); new ClassCombatShop(shopManager, clientManager, donationManager, false, "Knight", classManager.GetClass("Knight"));
new ClassCombatShop(shopManager, clientManager, donationManager, false, "Assassin", classManager.GetClass("Assassin")); new ClassCombatShop(shopManager, clientManager, donationManager, false, "Assassin", classManager.GetClass("Assassin"));
new FriendManager(this, clientManager, preferenceManager); new FriendManager(this, clientManager, preferenceManager, portal);
//Updates //Updates
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Updater(this), 1, 1); getServer().getScheduler().scheduleSyncRepeatingTask(this, new Updater(this), 1, 1);

View File

@ -123,7 +123,7 @@ public class Arcade extends JavaPlugin
new MemoryFix(this); new MemoryFix(this);
new CustomTagFix(this, packetHandler); new CustomTagFix(this, packetHandler);
new FriendManager(this, _clientManager, preferenceManager); new FriendManager(this, _clientManager, preferenceManager, portal);
//Updates //Updates
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Updater(this), 1, 1); getServer().getScheduler().scheduleSyncRepeatingTask(this, new Updater(this), 1, 1);