Refactor website link to use consumers instead of callbacks

This commit is contained in:
AlexTheCoder 2017-02-25 19:44:55 -05:00 committed by cnr
parent f6fdc1f817
commit 1ff033de43

View File

@ -17,6 +17,8 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
@ -33,9 +35,7 @@ import com.google.gson.JsonSyntaxException;
import mineplex.core.MiniDbClientPlugin;
import mineplex.core.account.CoreClientManager;
import mineplex.core.account.event.RankSaveEvent;
import mineplex.core.common.Pair;
import mineplex.core.common.Rank;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
@ -66,7 +66,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
{
if (client != null)
{
Callback<ForumUserData> dataCallback = data ->
Consumer<ForumUserData> dataCallback = data ->
{
if (data == null)
{
@ -130,7 +130,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
};
if (Bukkit.getPlayer(client.getUniqueId()) != null)
{
dataCallback.run(Get(client.getUniqueId()));
dataCallback.accept(Get(client.getUniqueId()));
}
else
{
@ -147,17 +147,17 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
data.Linked = true;
data.LinkedForumId = userId;
data.LastSyncedPowerPlayStatus = powerPlay;
runSync(() -> dataCallback.run(data));
runSync(() -> dataCallback.accept(data));
}
else
{
runSync(() -> dataCallback.run(new ForumUserData()));
runSync(() -> dataCallback.accept(new ForumUserData()));
}
}
catch (SQLException e)
{
e.printStackTrace();
runSync(() -> dataCallback.run(new ForumUserData()));
runSync(() -> dataCallback.accept(new ForumUserData()));
}
});
}
@ -437,7 +437,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
}
}
private void loadXenforoAccount(int userId, Callback<XenForoData> callback)
private void loadXenforoAccount(int userId, Consumer<XenForoData> callback)
{
try
{
@ -454,7 +454,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
response = PARSER.parse(json).getAsJsonObject();
if (response.has("error"))
{
callback.run(null);
callback.accept(null);
}
else
{
@ -487,29 +487,29 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
}
}
data.custom_fields = fields;
callback.run(data);
callback.accept(data);
}
}
catch (JsonSyntaxException e)
{
callback.run(null);
callback.accept(null);
e.printStackTrace();
return;
}
}
catch (MalformedURLException e)
{
callback.run(null);
callback.accept(null);
e.printStackTrace();
}
catch (IOException e)
{
callback.run(null);
callback.accept(null);
e.printStackTrace();
}
}
private void doAPICall(String call, Callback<String> errorCallback, Runnable ioException, Runnable onComplete)
private void doAPICall(String call, Consumer<String> errorCallback, Runnable ioException, Runnable onComplete)
{
StringBuilder input = new StringBuilder();
@ -545,7 +545,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
{
String errorID = response.get("user_error_id").getAsString();
errorCallback.run(errorID);
errorCallback.accept(errorID);
System.out.println("[XENFORO API] An error was found in the JSON response (id: " + errorID + ") from REST call: " + call);
return;
}
@ -607,23 +607,24 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
return abs / 11;
}
private Pair<Boolean, UUID> checkAccountOnline(int accountId)
private void checkAccountOnline(int accountId, BiConsumer<Boolean, UUID> consumer)
{
for (Player player : Bukkit.getOnlinePlayers())
{
if (getClientManager().getAccountId(player) == accountId)
{
return Pair.create(true, player.getUniqueId());
consumer.accept(true, player.getUniqueId());
return;
}
}
return Pair.create(false, null);
consumer.accept(false, null);
}
@EventHandler
public void handleRankSave(RankSaveEvent event)
{
Callback<Integer> dataCallback = id ->
Consumer<Integer> dataCallback = id ->
{
List<Integer> remove = new ArrayList<>();
List<Integer> add = new ArrayList<>();
@ -648,7 +649,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
final int userId = fd.LinkedForumId;
runAsync(() ->
{
dataCallback.run(userId);
dataCallback.accept(userId);
});
}
}
@ -666,7 +667,7 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
if (rs.next())
{
Integer userId = rs.getInt(1);
dataCallback.run(userId);
dataCallback.accept(userId);
}
}
catch (SQLException e)
@ -682,50 +683,25 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
@EventHandler
public void handleSubscriptionAdd(SubscriptionAddEvent event)
{
Pair<Boolean, UUID> p = checkAccountOnline(event.getAccountId());
if (p.getLeft())
checkAccountOnline(event.getAccountId(), (online, uuid) ->
{
ForumUserData fd = Get(p.getRight());
if (fd.Linked && !fd.LastSyncedPowerPlayStatus)
if (online)
{
final int userId = fd.LinkedForumId;
runAsync(() ->
ForumUserData fd = Get(uuid);
if (fd.Linked && !fd.LastSyncedPowerPlayStatus)
{
List<Integer> add = new ArrayList<>();
add.add(17);
refreshSiteTags(userId, new ArrayList<>(), add, false, () ->
final int userId = fd.LinkedForumId;
runAsync(() ->
{
runSync(() ->
{
fd.LastSyncedPowerPlayStatus = true;
});
try (Connection c = DBPool.getAccount().getConnection())
{
c.prepareStatement("UPDATE forumLink SET powerPlayStatus=true WHERE accountId=" + event.getAccountId() + ";").execute();
}
catch (SQLException e)
{
e.printStackTrace();
}
}, false, () -> {}, false);
});
}
}
else
{
runAsync(() ->
{
try (Connection c = DBPool.getAccount().getConnection())
{
ResultSet rs = c.prepareStatement("SELECT userId FROM forumLink WHERE accountId=" + event.getAccountId() + ";").executeQuery();
if (rs.next())
{
Integer userId = rs.getInt(1);
List<Integer> add = new ArrayList<>();
add.add(17);
refreshSiteTags(userId, new ArrayList<>(), add, false, () ->
{
try
runSync(() ->
{
fd.LastSyncedPowerPlayStatus = true;
});
try (Connection c = DBPool.getAccount().getConnection())
{
c.prepareStatement("UPDATE forumLink SET powerPlayStatus=true WHERE accountId=" + event.getAccountId() + ";").execute();
}
@ -734,14 +710,41 @@ public class WebsiteLinkManager extends MiniDbClientPlugin<ForumUserData>
e.printStackTrace();
}
}, false, () -> {}, false);
}
});
}
catch (SQLException e)
}
else
{
runAsync(() ->
{
e.printStackTrace();
}
});
}
try (Connection c = DBPool.getAccount().getConnection())
{
ResultSet rs = c.prepareStatement("SELECT userId FROM forumLink WHERE accountId=" + event.getAccountId() + ";").executeQuery();
if (rs.next())
{
Integer userId = rs.getInt(1);
List<Integer> add = new ArrayList<>();
add.add(17);
refreshSiteTags(userId, new ArrayList<>(), add, false, () ->
{
try
{
c.prepareStatement("UPDATE forumLink SET powerPlayStatus=true WHERE accountId=" + event.getAccountId() + ";").execute();
}
catch (SQLException e)
{
e.printStackTrace();
}
}, false, () -> {}, false);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
});
}
});
}
@EventHandler