Created
January 30, 2021 00:37
-
-
Save Ragnok123/123dd329ca3355cf845a5364499e9f13 to your computer and use it in GitHub Desktop.
Some friend system trash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void sendRequestInfo(Player p, String requester) { | |
NPlayer d = NovaCore.getPlayer(p.getName()); | |
FriendManager mgr = d.getFriendManager(); | |
SimpleFormMenu menu = new SimpleFormMenu(TextFormat.GOLD + requester, Lang.send(p, new String[] { | |
"Do you want to add this player as a friend?", | |
"Ты действительно хочешь добавить данного игрока в друзья?" | |
})); | |
menu.addButton(TextFormat.GREEN + Lang.send(p, new String[] {"YES", "ДА"}), new ButtonResponse() { | |
public void onResponse(Player p, Button button) { | |
mgr.requestsFrom.remove(requester); | |
mgr.getFriends().add(requester); | |
p.sendMessage(NovaCore.getPrefix("a") + Lang.send(p, | |
new String[] {"You have added &b%0 &aas a friend", "Вы добавили &b%0 &aв друзья"}, | |
new String[] {requester})); | |
if(NServer.isOnline(requester)) { | |
if(Settings.SERVER_ID.equals(NServer.getServerByPlayer(requester).getServerId())) { | |
NPlayer req = NovaCore.getPlayer(requester); | |
req.getFriendManager().requestsTo.remove(p.getName()); | |
req.getFriendManager().getFriends().add(p.getName()); | |
//to do: message | |
} else { | |
FriendRequestViewPacket response = new FriendRequestViewPacket(); | |
response.requester = requester; | |
response.responder = p.getName(); | |
response.status = 1; | |
PacketTransferPacket redirect = new PacketTransferPacket(); | |
redirect.setPacket(response); | |
redirect.setTargets(new ArrayList<String>() {{add(NServer.getServerByPlayer(requester).getServerId());}}); | |
NovaCore.getInstance().getNetworkManager().getPacketHandler().sendPacket(redirect); | |
//to do: handle | |
} | |
} else { | |
NovaCore.getDatabase().select("nova_friends", "nickname", requester, map -> { | |
String oldData = (String) map.get("data"); | |
JSONObject object = new JSONObject(oldData); | |
ArrayList<String> f = new ArrayList<String>(); | |
JSONArray array = object.getJSONArray("friends"); | |
for(int i = 0; i < array.length(); i++) { | |
f.add(array.getString(i)); | |
} | |
ArrayList<String> r = new ArrayList<String>(); | |
JSONArray array2 = object.getJSONArray("requestsTo"); | |
for(int i = 0; i < array2.length(); i++) { | |
r.add(array2.getString(i)); | |
} | |
f.add(p.getName()); | |
r.remove(p.getName()); | |
JSONArray newFriends = new JSONArray(); newFriends.put(f); | |
JSONArray newRequests = new JSONArray(); newRequests.put(r); | |
JSONObject newObject = new JSONObject(); | |
newObject.put("friends", newFriends); | |
newObject.put("requestsFrom", object.getJSONArray("requestsFrom")); | |
newObject.put("requestsTo", newRequests); | |
String json = newObject.toString(); | |
NovaCore.getDatabase().update("nova_friends", "nickname", requester, new Pair[] { | |
new Pair("data", json) | |
}); | |
}); | |
} | |
} | |
}); | |
menu.addButton(TextFormat.RED + Lang.send(p, new String[] {"NO", "НЕТ"}), new ButtonResponse() { | |
public void onResponse(Player p, Button button) { | |
mgr.requestsFrom.remove(requester); | |
mgr.save(); | |
p.sendMessage(NovaCore.getPrefix("a") + Lang.send(p, | |
new String[] {"You denied a friend request", "Вы отменили запрос в друзья"}, | |
new String[] {requester})); | |
if(NServer.isOnline(requester)) { | |
if(Settings.SERVER_ID.equals(NServer.getServerByPlayer(requester).getServerId())) { | |
NPlayer req = NovaCore.getPlayer(requester); | |
req.getFriendManager().requestsTo.remove(p.getName()); | |
req.save(); | |
//to do: message | |
} else { | |
FriendRequestViewPacket response = new FriendRequestViewPacket(); | |
response.requester = requester; | |
response.responder = p.getName(); | |
response.status = 0; | |
PacketTransferPacket redirect = new PacketTransferPacket(); | |
redirect.setPacket(response); | |
redirect.setTargets(new ArrayList<String>() {{add(NServer.getServerByPlayer(requester).getServerId());}}); | |
NovaCore.getInstance().getNetworkManager().getPacketHandler().sendPacket(redirect); | |
//to do: handle | |
} | |
} else { | |
NovaCore.getDatabase().select("nova_friends", "nickname", requester, map -> { | |
String oldData = (String) map.get("data"); | |
JSONObject object = new JSONObject(oldData); | |
ArrayList<String> r = new ArrayList<String>(); | |
JSONArray array2 = object.getJSONArray("requestsTo"); | |
for(int i = 0; i < array2.length(); i++) { | |
r.add(array2.getString(i)); | |
} | |
r.remove(p.getName()); | |
JSONArray newRequests = new JSONArray(); newRequests.put(r); | |
JSONObject newObject = new JSONObject(); | |
newObject.put("friends", object.getJSONArray("friends")); | |
newObject.put("requestsFrom", object.getJSONArray("requestsFrom")); | |
newObject.put("requestsTo", newRequests); | |
String json = newObject.toString(); | |
NovaCore.getDatabase().update("nova_friends", "nickname", requester, new Pair[] { | |
new Pair("data", json) | |
}); | |
}); | |
} | |
} | |
}); | |
menu.addButton(Lang.send(p, new String[] {"&cBACK", "&cНАЗАД"}), new ButtonResponse() { | |
public void onResponse(Player p, Button button) { | |
sendViewForm(p); | |
} | |
}); | |
menu.show(p); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment