Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
\.gradle/
\.idea/
build/
*.iml
*.iml

*.db
62 changes: 3 additions & 59 deletions src/main/java/me/okx/twitchsync/Revoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,70 +22,14 @@ public Revoker(TwitchSync plugin) {

@Override
public void run() {
plugin.getLogger().info("Revoking now.");
plugin.getLogger().info("Syncing user data now.");
long start = System.currentTimeMillis();

Map<UUID, Token> tokens = plugin.getSqlHelper().getTokens().get();
checkTokens(tokens);
plugin.getValidator().sync(tokens);

long time = System.currentTimeMillis() - start;
plugin.getLogger().info("Finished revoking in " + time + "ms.");
plugin.getLogger().info("Finished Sync in " + time + "ms.");
}

private void checkTokens(Map<UUID, Token> tokens) {
for(Map.Entry<UUID, Token> entry : tokens.entrySet()) {
UUID uuid = entry.getKey();

Token token = entry.getValue();

AccessToken accessToken = refresh(token.getAccessToken());
plugin.getSqlHelper().setToken(uuid,
token.getId(),
accessToken.getAccessToken(),
accessToken.getRefreshToken());

plugin.getSqlHelper().isFollowing(uuid).ifPresent(b -> {
if(b && check(plugin.getValidator().getFollowingState(token.getId(), accessToken))) {
revoke("follow", uuid);
plugin.getSqlHelper().setFollowing(uuid, false);
}
});
plugin.getSqlHelper().isSubscribed(uuid).ifPresent(b -> {
// if subscribed and all states are not YES
if(b && check(plugin.getValidator().getSubscriptionState(token.getId(), accessToken))) {
revoke("subscribe", uuid);
plugin.getSqlHelper().setSubscribed(uuid, false);
}
});
}
}

private void revoke(String type, UUID uuid) {
ConfigurationSection section = plugin.getConfig().getConfigurationSection(type);
OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);

Bukkit.getScheduler().runTask(plugin, () -> {
plugin.debug(type + " - " + uuid, "revoking");
String group = section.getString("rank");
if (!group.equalsIgnoreCase("none") && plugin.getPerms() != null) {
plugin.getPerms().playerRemoveGroup(null, player, group);
}

for (String command : section.getStringList("revoke-commands")) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command
.replace("%name%", player.getName()));
}
});
}

/**
* Check if all states are not YES
*/
public boolean check(Stream<StateWithId> states) {
return states.allMatch(stateWithId -> stateWithId.getState() != CheckState.YES);
}

private AccessToken refresh(AccessToken accessToken) {
return plugin.getValidator().refreshToken(accessToken.getRefreshToken());
}
}
38 changes: 9 additions & 29 deletions src/main/java/me/okx/twitchsync/TwitchServer.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package me.okx.twitchsync;

import com.sun.net.httpserver.HttpServer;
import me.okx.twitchsync.data.Token;
import me.okx.twitchsync.data.sync.SyncMessage;
import me.okx.twitchsync.data.sync.SyncResponse;
import me.okx.twitchsync.data.sync.SyncResponseFailure;
import me.okx.twitchsync.data.sync.SyncResponseSuccess;

import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -30,7 +29,7 @@ public void start() throws IOException {
try {
plugin.debug("Handling request " + ex + " on " + Thread.currentThread().getName());

SyncResponse response = new SyncResponseFailure(SyncMessage.INVALID_URL);
SyncResponse response = SyncResponse.of(SyncMessage.INVALID_URL);

String query = ex.getRequestURI().getQuery();
if (query != null) {
Expand All @@ -41,35 +40,16 @@ public void start() throws IOException {
String code = parameters.get("code");

if(state != null && code != null) {
response = plugin.getValidator().sync(UUID.fromString(state), code);
UUID stateUUID = UUID.fromString(state);
UUID uuid = plugin.getValidator().getUUIDFromAuthState(stateUUID);
if (uuid != null) {
Token token = plugin.getValidator().store(uuid, code).get();
response = plugin.getValidator().sync(uuid, token).get();
} else response = SyncResponse.of(SyncMessage.INVALID_URL);
}
}

SyncMessage message;
if (response instanceof SyncResponseSuccess) {
SyncResponseSuccess success = (SyncResponseSuccess) response;
if (success.isFollowing() && success.isSubscribed()) {
message = SyncMessage.BOTH_SUCCESS;
} else if (success.isFollowing()) {
message = SyncMessage.FOLLOW_SUCCESS;
} else if (success.isSubscribed()) {
message = SyncMessage.SUBSCRIPTION_SUCCESS;
} else {
SyncMessage subscribe = success.getSubscribeMessage();
SyncMessage follow = success.getFollowMessage();
// make sure the already-done message shows up
if(subscribe == SyncMessage.ALREADY_DONE) {
message = subscribe;
} else {
message = follow;
}
}
} else if (response instanceof SyncResponseFailure) {
SyncResponseFailure failure = (SyncResponseFailure) response;
message = failure.getMessage();
} else {
throw new IllegalArgumentException("Sync response must either be success or failure.");
}
SyncMessage message = response.getMessage();

byte[] bytes = message.getValue(plugin).getBytes("UTF-8");

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/me/okx/twitchsync/TwitchSync.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package me.okx.twitchsync;

import me.okx.twitchsync.data.Channel;
import me.okx.twitchsync.data.Options;
import me.okx.twitchsync.data.Upgrade;
import me.okx.twitchsync.events.PlayerListener;
import me.okx.twitchsync.util.SqlHelper;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -19,8 +23,16 @@ public Permission getPerms() {
return this.perms;
}

@Override
public void onLoad() {
ConfigurationSerialization.registerClass(Options.class);
ConfigurationSerialization.registerClass(Channel.class);
ConfigurationSerialization.registerClass(Upgrade.class);
}

@Override
public void onEnable() {
debug("Logging works.", "Testing");
getConfig().options().copyDefaults(true);
saveDefaultConfig();
initValidator();
Expand All @@ -32,7 +44,7 @@ public void onEnable() {
getCommand("twitchsync").setExecutor(new TwitchSyncCommand(this));
getCommand("revoke").setExecutor(new RevokeCommand(this));

long time = 20*86400*getConfig().getInt("revoke-interval-days");
long time = 20*60*getConfig().getInt("revoke-interval-minutes");
new Revoker(this).runTaskTimerAsynchronously(this, time, time);

new Metrics(this);
Expand Down
38 changes: 26 additions & 12 deletions src/main/java/me/okx/twitchsync/TwitchSyncCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.okx.twitchsync;

import me.okx.twitchsync.data.sync.SyncMessage;
import me.okx.twitchsync.data.sync.SyncResponse;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
Expand All @@ -9,6 +11,9 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class TwitchSyncCommand implements CommandExecutor {
private TwitchSync plugin;

Expand All @@ -24,18 +29,27 @@ public boolean onCommand(CommandSender cs, Command command, String s, String[] a
}

Player player = (Player) cs;

String url = plugin.getValidator().createAuthenticationUrl(player.getUniqueId());
if(url == null) {
player.sendMessage(ChatColor.RED + "An error occurred. Please try again.");
return true;
}

player.spigot().sendMessage(new ComponentBuilder("Click this text to sync to Twitch")
.color(net.md_5.bungee.api.ChatColor.GREEN)
.event(new ClickEvent(ClickEvent.Action.OPEN_URL, url))
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click to sync to Twitch").create()))
.create());
plugin.getValidator().sync(player.getUniqueId()).whenCompleteAsync((response, throwable) -> {
if (throwable != null) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6An error occurred. Please try again."));
plugin.getLogger().warning(throwable.toString());
throwable.printStackTrace();
}
if (response.getMessage() != SyncMessage.NO_TOKEN) {
player.sendMessage(ChatColor.GREEN + "Re-synced.");
} else {
String url = plugin.getValidator().createAuthenticationUrl(player.getUniqueId());
if(url == null) {
player.sendMessage(ChatColor.RED + "An error occurred. Please try again.");
return;
}
player.spigot().sendMessage(new ComponentBuilder("Click this text to sync to Twitch")
.color(net.md_5.bungee.api.ChatColor.GREEN)
.event(new ClickEvent(ClickEvent.Action.OPEN_URL, url))
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click to sync to Twitch").create()))
.create());
}
});

return true;
}
Expand Down
Loading