Implement a method for administrators to remove the link between an in-game minecraft account and a forum account, and rewrite synchronization of tags to more effectively handle the intricacies of the API being utilized

This commit is contained in:
AlexTheCoder 2017-02-25 12:52:34 -05:00 committed by cnr
parent 3ebfb453c5
commit f6fdc1f817
2 changed files with 411 additions and 157 deletions

View File

@ -0,0 +1,30 @@
package mineplex.core.website;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
public class UnlinkCommand extends CommandBase<WebsiteLinkManager>
{
public UnlinkCommand(WebsiteLinkManager plugin)
{
super(plugin, Rank.ADMIN, "unlink");
}
@Override
public void Execute(final Player caller, String[] args)
{
if (args.length < 1)
{
UtilPlayer.message(caller, F.help("/" + _aliasUsed + " <Player>", "Removes a link to a forum and in-game account.", Rank.ADMIN, ChatColor.RED));
}
else
{
Plugin.unlink(caller, args[0]);
}
}
}

View File

@ -55,10 +55,120 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
super("Website Link", plugin, clientManager);
addCommand(new LinkCommand(this));
addCommand(new UnlinkCommand(this));
Authenticator.setDefault(new MineplexAuthenticator("minexen", "c4cADuj&ChaQ"));
}
public void unlink(Player sender, String target)
{
getClientManager().getOrLoadClient(target, client ->
{
if (client != null)
{
Callback<ForumUserData> dataCallback = data ->
{
if (data == null)
{
UtilPlayer.message(sender, F.main(getName(), "Could not find " + F.name(target) + "!"));
}
else
{
if (!data.Linked)
{
UtilPlayer.message(sender, F.main(getName(), F.name(target) + " is not linked to a forum account!"));
}
else
{
runAsync(() ->
{
loadXenforoAccount(data.LinkedForumId, user ->
{
List<Integer> remove = new ArrayList<>();
remove.add(17);
remove.add(91);
for (Rank rank : Rank.values())
{
if (rank.getForumId() != -1)
{
remove.add(rank.getForumId());
}
}
String call = "action=editUser&user=" + user.username + "&custom_fields=mcAcctIdPC=";
doAPICall(call, err ->
{
runSync(() -> UtilPlayer.message(sender, F.main(getName(), F.name(target) + " was not able to be unlinked at this time!")));
}, () ->
{
runSync(() -> UtilPlayer.message(sender, F.main(getName(), F.name(target) + " was not able to be unlinked at this time!")));
}, () ->
{
refreshSiteTags(data.LinkedForumId, remove, new ArrayList<>(), false, () ->
{
try (Connection c = DBPool.getAccount().getConnection())
{
c.prepareStatement("DELETE FROM forumLink WHERE accountId=" + client.getAccountId()).execute();
runSync(() ->
{
UtilPlayer.message(sender, F.main(getName(), F.name(target) + " was successfully unlinked!"));
data.Linked = false;
data.LinkedForumId = -1;
data.LastSyncedPowerPlayStatus = false;
});
}
catch (SQLException e)
{
e.printStackTrace();
runSync(() -> UtilPlayer.message(sender, F.main(getName(), F.name(target) + " was not able to be unlinked at this time!")));
}
}, false, () -> UtilPlayer.message(sender, F.main(getName(), F.name(target) + " was not able to be unlinked at this time!")), true);
});
});
});
}
}
};
if (Bukkit.getPlayer(client.getUniqueId()) != null)
{
dataCallback.run(Get(client.getUniqueId()));
}
else
{
runAsync(() ->
{
try (Connection c = DBPool.getAccount().getConnection())
{
ResultSet rs = c.prepareStatement("SELECT userId, powerPlayStatus FROM forumLink WHERE accountId=" + client.getAccountId() + ";").executeQuery();
if (rs.next())
{
Integer userId = rs.getInt(1);
Boolean powerPlay = rs.getBoolean(2);
final ForumUserData data = new ForumUserData();
data.Linked = true;
data.LinkedForumId = userId;
data.LastSyncedPowerPlayStatus = powerPlay;
runSync(() -> dataCallback.run(data));
}
else
{
runSync(() -> dataCallback.run(new ForumUserData()));
}
}
catch (SQLException e)
{
e.printStackTrace();
runSync(() -> dataCallback.run(new ForumUserData()));
}
});
}
}
else
{
UtilPlayer.message(sender, F.main(getName(), "Could not find " + F.name(target) + "!"));
}
});
}
public void startLink(Player player, String code)
{
final int forumId = getForumId(code);
@ -85,66 +195,248 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
UtilPlayer.message(player, F.main(getName(), "That link code is invalid!"));
return;
}
completeLink(player, data.username, data.user_id, accountId, rank, powerPlay);
completeLink(player, data, accountId, rank, powerPlay);
});
});
}
private void completeLink(Player player, String forumUser, int userId, int accountId, Rank rank, boolean powerPlay)
private void completeLink(Player player, XenForoData data, int accountId, Rank rank, boolean powerPlay)
{
try (Connection c = DBPool.getAccount().getConnection())
{
boolean success = c.prepareStatement("INSERT INTO forumLink (accountId, userId, powerPlayStatus) VALUES (" + accountId + ", " + userId + ", " + powerPlay + ");").executeUpdate() > 0;
boolean success = c.prepareStatement("INSERT INTO forumLink (accountId, userId, powerPlayStatus) VALUES (" + accountId + ", " + data.user_id + ", " + powerPlay + ");").executeUpdate() > 0;
if (success)
{
String call = "action=editUser&user=" + forumUser + "&custom_fields=mcAcctIdPC=" + accountId;
String call = "action=editUser&user=" + data.username + "&custom_fields=mcAcctIdPC=" + accountId;
List<Integer> adding = new ArrayList<>();
adding.add(91);
if (rank.getForumId() != -1)
{
call += ("&add_groups=" + rank.getForumId());
adding.add(rank.getForumId());
}
if (powerPlay)
{
if (call.contains("&add_groups="))
{
call += (",17");
}
else
{
call += ("&add_groups=17");
}
adding.add(17);
}
doAPICall(call, err ->
{
UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!"));
runSync(() -> UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!")));
}, () ->
{
UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!"));
runSync(() -> UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!")));
}, () ->
{
runSync(() ->
refreshSiteTags(data, new ArrayList<>(), adding, false, () ->
{
UtilPlayer.message(player, F.main(getName(), "You have successfully linked your account!"));
if (player.isOnline())
{
Get(player).LinkedForumId = userId;
Get(player).LinkedForumId = data.user_id;
Get(player).LastSyncedPowerPlayStatus = powerPlay;
Get(player).Linked = true;
}
});
}, true, () -> UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!")), true);
});
}
else
{
UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!"));
runSync(() -> UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!")));
}
}
catch (SQLException e)
{
UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!"));
runSync(() -> UtilPlayer.message(player, F.main(getName(), "The link failed! Please try again!")));
e.printStackTrace();
}
}
private void refreshSiteTags(int userId, List<Integer> removing, List<Integer> adding, boolean runAsync, Runnable after, boolean runAfterSync, Runnable onErr, boolean runErrSync)
{
Runnable r = () ->
{
loadXenforoAccount(userId, data ->
{
refreshSiteTags(data, removing, adding, false, after, runAfterSync, onErr, runErrSync);
});
};
if (runAsync)
{
runAsync(r);
}
else
{
r.run();
}
}
private void refreshSiteTags(XenForoData data, List<Integer> removing, List<Integer> adding, boolean runAsync, Runnable after, boolean runAfterSync, Runnable onErr, boolean runErrSync)
{
Runnable r = () ->
{
if (data == null)
{
return;
}
String callBase = "action=editUser&user=" + data.username;
String groups = "";
for (int groupId : data.secondary_group_ids)
{
if (!removing.contains(groupId) && !adding.contains(groupId))
{
groups += ("," + groupId);
}
}
for (Integer groupId : adding)
{
groups += ("," + groupId);
}
if (!groups.isEmpty())
{
groups = groups.substring(1);
}
final String addGroups = groups;
groups = "";
for (int groupId : data.secondary_group_ids)
{
groups += ("," + groupId);
}
if (!groups.isEmpty())
{
groups = groups.substring(1);
}
final String remGroups = groups;
if (!remGroups.isEmpty())
{
doAPICall(callBase + "&remove_groups=" + remGroups, err ->
{
if (runErrSync)
{
runSync(onErr);
}
else
{
onErr.run();
}
}, () ->
{
if (runErrSync)
{
runSync(onErr);
}
else
{
onErr.run();
}
}, () ->
{
if (!addGroups.isEmpty())
{
doAPICall(callBase + "&add_groups=" + addGroups, err ->
{
if (runErrSync)
{
runSync(onErr);
}
else
{
onErr.run();
}
}, () ->
{
if (runErrSync)
{
runSync(onErr);
}
else
{
onErr.run();
}
}, () ->
{
if (runAfterSync)
{
runSync(after);
}
else
{
after.run();
}
});
}
else
{
if (runAfterSync)
{
runSync(after);
}
else
{
after.run();
}
}
});
}
else
{
if (!addGroups.isEmpty())
{
doAPICall(callBase + "&add_groups=" + addGroups, err ->
{
if (runErrSync)
{
runSync(onErr);
}
else
{
onErr.run();
}
}, () ->
{
if (runErrSync)
{
runSync(onErr);
}
else
{
onErr.run();
}
}, () ->
{
if (runAfterSync)
{
runSync(after);
}
else
{
after.run();
}
});
}
else
{
if (runAfterSync)
{
runSync(after);
}
else
{
after.run();
}
}
}
};
if (runAsync)
{
runAsync(r);
}
else
{
r.run();
}
}
private void loadXenforoAccount(int userId, Callback<XenForoData> callback)
{
try
@ -288,7 +580,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
int offset = 100000000;
StringBuilder sb = new StringBuilder((cal.get(Calendar.DAY_OF_YEAR) - 1) + "");
StringBuilder sb = new StringBuilder(cal.get(Calendar.DAY_OF_YEAR) + "");
while (sb.length() < 3)
{
sb.insert(0, "0");
@ -299,7 +591,20 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
Integer input = Integer.parseInt(given);
return Math.abs(test - input);
Integer abs = Math.abs(test - input);
// Bukkit.broadcastMessage("REVERSE: " + reverse);
// Bukkit.broadcastMessage("TEST: " + test);
// Bukkit.broadcastMessage("INPUT: " + input);
// Bukkit.broadcastMessage("ABS: " + abs);
// Bukkit.broadcastMessage("MODULUS: " + abs % 11);
if (abs % 11 != 0)
{
return -1;
}
return abs / 11;
}
private Pair<Boolean, UUID> checkAccountOnline(int accountId)
@ -318,57 +623,22 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
@EventHandler
public void handleRankSave(RankSaveEvent event)
{
Callback<XenForoData> dataCallback = new Callback<XenForoData>()
{
public void run(XenForoData data)
Callback<Integer> dataCallback = id ->
{
List<Integer> remove = new ArrayList<>();
List<Integer> add = new ArrayList<>();
for (Rank rank : Rank.values())
{
if (rank.getForumId() != -1 && rank != event.getRank())
{
boolean found = false;
for (int groupId : data.secondary_group_ids)
{
if (groupId == rank.getForumId())
{
found = true;
}
}
if (found)
{
remove.add(rank.getForumId());
}
}
}
String callBase = "action=editUser&user=" + data.username;
if (!remove.isEmpty())
{
String removal = "&remove_groups=" + remove.get(0).intValue();
for (int i = 1; i < remove.size(); i++)
{
removal += ("," + remove.get(i));
}
String call = callBase + removal;
doAPICall(call, error -> {}, () -> {}, () -> {});
}
if (event.getRank().getForumId() != -1)
{
boolean found = false;
for (int groupId : data.secondary_group_ids)
{
if (groupId == event.getRank().getForumId())
{
found = true;
}
}
if (!found)
{
String call = callBase + "&add_groups=" + event.getRank().getForumId();
doAPICall(call, error -> {}, () -> {}, () -> {});
}
}
add.add(event.getRank().getForumId());
}
refreshSiteTags(id, remove, add, false, () -> {}, false, () -> {}, false);
};
if (Bukkit.getPlayer(event.getUUID()) != null)
{
@ -378,7 +648,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
final int userId = fd.LinkedForumId;
runAsync(() ->
{
loadXenforoAccount(userId, dataCallback);
dataCallback.run(userId);
});
}
}
@ -396,7 +666,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
if (rs.next())
{
Integer userId = rs.getInt(1);
loadXenforoAccount(userId, dataCallback);
dataCallback.run(userId);
}
}
catch (SQLException e)
@ -421,19 +691,9 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
final int userId = fd.LinkedForumId;
runAsync(() ->
{
loadXenforoAccount(userId, data ->
{
boolean has = false;
for (int groupId : data.secondary_group_ids)
{
if (groupId == 17)
{
has = true;
}
}
if (!has)
{
doAPICall("action=editUser&user=" + data.username + "&add_groups=17", error -> {}, () -> {}, () ->
List<Integer> add = new ArrayList<>();
add.add(17);
refreshSiteTags(userId, new ArrayList<>(), add, false, () ->
{
runSync(() ->
{
@ -447,9 +707,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
{
e.printStackTrace();
}
});
}
});
}, false, () -> {}, false);
});
}
}
@ -463,19 +721,9 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
if (rs.next())
{
Integer userId = rs.getInt(1);
loadXenforoAccount(userId, data ->
{
boolean has = false;
for (int groupId : data.secondary_group_ids)
{
if (groupId == 17)
{
has = true;
}
}
if (!has)
{
doAPICall("action=editUser&user=" + data.username + "&add_groups=17", error -> {}, () -> {}, () ->
List<Integer> add = new ArrayList<>();
add.add(17);
refreshSiteTags(userId, new ArrayList<>(), add, false, () ->
{
try
{
@ -485,9 +733,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
{
e.printStackTrace();
}
});
}
});
}, false, () -> {}, false);
}
}
catch (SQLException e)
@ -517,37 +763,17 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
final int accountId = getClientManager().getAccountId(event.getPlayer());
runAsync(() ->
{
loadXenforoAccount(userId, data ->
{
String call = "";
boolean change = false;
boolean has = false;
for (int groupId : data.secondary_group_ids)
{
if (groupId == 17)
{
has = true;
}
}
List<Integer> remove = new ArrayList<>();
List<Integer> add = new ArrayList<>();
if (powerPlay)
{
if (!has)
{
call = "action=editUser&user=" + data.username + "&add_groups=17";
change = true;
}
add.add(17);
}
else
{
if (has)
{
call = "action=editUser&user=" + data.username + "&remove_groups=17";
change = true;
remove.add(17);
}
}
if (change)
{
doAPICall(call, error -> {}, () -> {}, () ->
refreshSiteTags(userId, remove, add, false, () ->
{
runSync(() ->
{
@ -564,9 +790,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
{
e.printStackTrace();
}
});
}
});
}, false, () -> {}, false);
});
}
}