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
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@
import com.gmail.chickenpowerrr.ranksync.api.event.PlayerLinkCodeCreateEvent;
import com.gmail.chickenpowerrr.ranksync.api.player.Player;
import com.gmail.chickenpowerrr.ranksync.discord.language.Translation;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.stream.Collectors;
import lombok.Getter;

import java.util.*;
import java.util.stream.Collectors;

/**
* This class represents the Discord !link command, it can be used to link a Discord account to
* another service
Expand All @@ -25,120 +19,107 @@
*/
public class LinkCommand implements Command {

private static final Random random = new Random();

@Getter
private final String label;
private final Collection<String> aliases;

private final Map<String, Long> timeOuts = new HashMap<>();

/**
* Starts a timer that deletes codes that haven't been used after five minutes
*
* @param label the label of the command ('link')
* @param aliases all aliases that, besides the name can get used to invoke this command
*/
public LinkCommand(String label, Collection<String> aliases) {
this.label = label;
this.aliases = aliases;

new Timer().scheduleAtFixedRate(new TimerTask() {

@Override
public void run() {
Collection<String> toRemove = timeOuts.entrySet().stream()
.filter(entry -> entry.getValue() + 1000 * 60 * 5 < System.currentTimeMillis())
.map(Map.Entry::getKey).collect(Collectors.toSet());
toRemove.forEach(timeOuts::remove);
}
}, 1000 * 10, 1000 * 10);
}

/**
* Links the account of a Discord user if it hasn't been linked already
*
* @param invoker the Discord user that invokes the command
* @param arguments should be empty for this command
* @return the message that will be send into the channel where the command was executed
*/
@Override
public String execute(Player invoker, List<String> arguments) {
if (invoker.getUuid() == null) {
if (!onCooldown(invoker.getPersonalId())) {
String secretKey = randomString(10 + random.nextInt(2));
RankSyncApi.getApi().execute(new PlayerLinkCodeCreateEvent(invoker, secretKey));

if (invoker.sendPrivateMessage(
Translation.LINK_COMMAND_PRIVATE
.getTranslation("name", invoker.getFancyName(), "key", secretKey))) {
this.timeOuts.put(invoker.getPersonalId(), System.currentTimeMillis());
return Translation.LINK_COMMAND_PUBLIC.getTranslation("name", invoker.getFancyName());
private static final Random random = new Random();

@Getter
private final String label;
private final Collection<String> aliases;

private final Map<String, Long> timeOuts = new HashMap<>();

/**
* Starts a timer that deletes codes that haven't been used after five minutes
*
* @param label the label of the command ('link')
* @param aliases all aliases that, besides the name can get used to invoke this command
*/
public LinkCommand(String label, Collection<String> aliases) {
this.label = label;
this.aliases = aliases;

new Timer().scheduleAtFixedRate(new TimerTask() {

@Override
public void run() {
Collection<String> toRemove = timeOuts.entrySet().stream()
.filter(entry -> entry.getValue() + 1000 * 60 * 5 < System.currentTimeMillis())
.map(Map.Entry::getKey).collect(Collectors.toSet());
toRemove.forEach(timeOuts::remove);
}
}, 1000 * 10, 1000 * 10);
}

/**
* Links the account of a Discord user if it hasn't been linked already
*
* @param invoker the Discord user that invokes the command
* @param arguments should be empty for this command
* @return the message that will be send into the channel where the command was executed
*/
@Override
public String execute(Player invoker, List<String> arguments) {
if (invoker.getUuid() == null) {
if (!onCooldown(invoker.getPersonalId())) {
String secretKey = randomString(10 + random.nextInt(2));
RankSyncApi.getApi().execute(new PlayerLinkCodeCreateEvent(invoker, secretKey));

return Translation.LINK_COMMAND_PRIVATE
.getTranslation("name", invoker.getFancyName(), "key", secretKey);
} else {
return Translation.LINK_COMMAND_RIGHTTHERE.getTranslation();
}
} else {
return Translation.LINK_COMMAND_ENABLE_PRIVATE_MESSAGES
.getTranslation("name", invoker.getFancyName());
return Translation.LINK_COMMAND_ALREADY_LINKED.getTranslation("name", invoker.getFancyName());
}
} else {
if (invoker.sendPrivateMessage(Translation.LINK_COMMAND_RIGHTTHERE.getTranslation())) {
return Translation.LINK_COMMAND_REQUEST_LIMIT
.getTranslation("name", invoker.getFancyName());
} else {
return Translation.LINK_COMMAND_ENABLE_PRIVATE_MESSAGES
.getTranslation("name", invoker.getFancyName());
}

/**
* Returns if the user is still on cooldown
*
* @param identifier the identifier of the Discord user
* @return if the user is still on cooldown
*/
private boolean onCooldown(String identifier) {
return this.timeOuts.containsKey(identifier)
&& this.timeOuts.get(identifier) + 1000 * 60 * 5 >= System.currentTimeMillis();
}

/**
* Returns true because everyone can link their account
*
* @param player the player who wants to link their Discord account
* @return true
*/
@Override
public boolean hasPermission(Player player) {
return true;
}

@Override
public Collection<String> getAliases() {
return Collections.unmodifiableCollection(this.aliases);
}

/**
* Returns a random String with the given size
*
* @param size the amount of characters in the String
* @return a random String with the requested size
*/
private String randomString(int size) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < size; i++) {
stringBuilder.append(randomChar());
}
}
} else {
return Translation.LINK_COMMAND_ALREADY_LINKED.getTranslation("name", invoker.getFancyName());
return stringBuilder.toString();
}
}

/**
* Returns if the user is still on cooldown
*
* @param identifier the identifier of the Discord user
* @return if the user is still on cooldown
*/
private boolean onCooldown(String identifier) {
return this.timeOuts.containsKey(identifier)
&& this.timeOuts.get(identifier) + 1000 * 60 * 5 >= System.currentTimeMillis();
}

/**
* Returns true because everyone can link their account
*
* @param player the player who wants to link their Discord account
* @return true
*/
@Override
public boolean hasPermission(Player player) {
return true;
}

@Override
public Collection<String> getAliases() {
return Collections.unmodifiableCollection(this.aliases);
}

/**
* Returns a random String with the given size
*
* @param size the amount of characters in the String
* @return a random String with the requested size
*/
private String randomString(int size) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < size; i++) {
stringBuilder.append(randomChar());

/**
* Returns a random character
*/
private char randomChar() {
int randomNumber = random.nextInt(52);
char base = (randomNumber < 26) ? 'A' : 'a';
return (char) (base + randomNumber % 26);
}
return stringBuilder.toString();
}

/**
* Returns a random character
*/
private char randomChar() {
int randomNumber = random.nextInt(52);
char base = (randomNumber < 26) ? 'A' : 'a';
return (char) (base + randomNumber % 26);
}
}
7 changes: 6 additions & 1 deletion spigot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
<description>Sync your ranks Minecraft ranks between services</description>

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>

<repository>
<id>vault-repo</id>
<url>http://nexus.hc.to/content/repositories/pub_releases</url>
<url>https://nexus.hc.to/content/repositories/pub_releases</url>
</repository>

<repository>
Expand Down