Merge remote-tracking branch 'origin/master' into master

This commit is contained in:
Joel 2021-02-20 03:30:08 +01:00
commit d66416cec7
19 changed files with 453 additions and 26 deletions

@ -36,7 +36,7 @@ public class API {
.create(); .create();
private static final List<Class<? extends IModel>> models = new ArrayList<>(); private static final List<Class<? extends IModel>> models = new ArrayList<>();
private static final Map<Object, List<Method>> routes = new HashMap<>(); private static final Map<Object, List<Method>> routes = new HashMap<>();
private static final boolean requiresAuthentication = false; private static final boolean requiresAuthentication = true;
// Rate Limiting // Rate Limiting
private static final int rateLimit = 120; // The amount of requests a minute private static final int rateLimit = 120; // The amount of requests a minute
@ -114,15 +114,21 @@ public class API {
// if there is a problem checking the key or the key is invalid // if there is a problem checking the key or the key is invalid
if (requiresAuthentication) { if (requiresAuthentication) {
String apiKey = request.headers("key"); String apiKey = request.headers("key");
if (apiKey == null) if (apiKey == null) {
response.status(401);
throw new APIException("Unauthorized"); throw new APIException("Unauthorized");
}
// Check if the provided API key is valid // Check if the provided API key is valid
Optional<List<APIKey>> keys = apiKeyRepository.lookup(apiKey); Optional<List<APIKey>> keys = apiKeyRepository.lookup(apiKey);
if (keys.isEmpty() || (keys.get().isEmpty())) if (keys.isEmpty() || (keys.get().isEmpty())) {
response.status(401);
throw new APIException("Unauthorized"); throw new APIException("Unauthorized");
}
key = keys.get().get(0); key = keys.get().get(0);
if (restPath.accessLevel() == APIAccessLevel.DEV && (restPath.accessLevel() != key.getAccessLevel())) if (restPath.accessLevel() == APIAccessLevel.DEV && (restPath.accessLevel() != key.getAccessLevel())) {
response.status(401);
throw new APIException("Unauthorized"); throw new APIException("Unauthorized");
}
} }
// Checking if the request has the appropriate headers for the get route // Checking if the request has the appropriate headers for the get route
if (restPath.headers().length > 0) { if (restPath.headers().length > 0) {
@ -160,9 +166,11 @@ public class API {
jsonObject.addProperty("error", message); jsonObject.addProperty("error", message);
// If the caught exception is not an API exception, print the stacktrace // If the caught exception is not an API exception, print the stacktrace
if (!(ex instanceof APIException)) { if (!(ex instanceof APIException)) {
response.status(500);
System.err.println("The route \"" + entry.getKey().getClass().getSimpleName() + "\" raised an exception:"); System.err.println("The route \"" + entry.getKey().getClass().getSimpleName() + "\" raised an exception:");
ex.printStackTrace(); ex.printStackTrace();
} } else if (response.status() == 200)
response.status(502);
} }
return gson.toJson(jsonObject); return gson.toJson(jsonObject);
}); });

@ -26,7 +26,7 @@ import java.util.concurrent.TimeUnit;
public class AccountRoute { public class AccountRoute {
// Account model cache for players that were looked up via the account route // Account model cache for players that were looked up via the account route
public static final Cache<UUID, AccountModel> CACHE = CacheBuilder.newBuilder() public static final Cache<UUID, AccountModel> CACHE = CacheBuilder.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES) .expireAfterWrite(5, TimeUnit.MINUTES)
.build(); .build();
private final AccountRepository accountRepository; private final AccountRepository accountRepository;
@ -42,6 +42,8 @@ public class AccountRoute {
AccountModel account = CACHE.getIfPresent(uuid); AccountModel account = CACHE.getIfPresent(uuid);
if (account == null) { if (account == null) {
account = accountRepository.getAccount(uuid); account = accountRepository.getAccount(uuid);
if (account == null)
throw new APIException("Account not found");
account.setTimeCached(System.currentTimeMillis()); account.setTimeCached(System.currentTimeMillis());
CACHE.put(uuid, account); CACHE.put(uuid, account);
} }

@ -10,8 +10,9 @@ public class SkullTexture {
public static final String DIAMOND_BLOCK = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMWUwNTc5OTZjYmE0NzMyOGRmNzJmYmEzZWEyYjlhYTM1YzhhODIyN2YxY2VjODljMTg4NGRjYWRhYTgyNGQ4NSJ9fX0="; public static final String DIAMOND_BLOCK = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMWUwNTc5OTZjYmE0NzMyOGRmNzJmYmEzZWEyYjlhYTM1YzhhODIyN2YxY2VjODljMTg4NGRjYWRhYTgyNGQ4NSJ9fX0=";
public static final String EMERALD_BLOCK = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYjcyYzA1ZGQ3NjI4OGY0MzI4YTI0MzkxYmY0NzI1ZmQyMjYwNTkyZGIzY2Y5YjJiYzIwMzJkZDA1OTZjZjQ0MCJ9fX0="; public static final String EMERALD_BLOCK = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYjcyYzA1ZGQ3NjI4OGY0MzI4YTI0MzkxYmY0NzI1ZmQyMjYwNTkyZGIzY2Y5YjJiYzIwMzJkZDA1OTZjZjQ0MCJ9fX0=";
public static final String COAL_BLOCK = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDdmNTc2NmQyOTI4ZGMwZGYxYjM0MDRjM2JkMDczYzk0NzZkMjZjODA1NzNiMDMzMmU3Y2NlNzNkZjE1NDgyYSJ9fX0="; public static final String COAL_BLOCK = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDdmNTc2NmQyOTI4ZGMwZGYxYjM0MDRjM2JkMDczYzk0NzZkMjZjODA1NzNiMDMzMmU3Y2NlNzNkZjE1NDgyYSJ9fX0=";
public static final String GREEN_BACKPACK = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMjhhMTI3ZjFjZmQ3OTk4NmU3YmQ5NWQ5MmRlNGY0ZjY4MDQwZTRmODk5ZjgxYjFmOGYzY2ExNWI2NGY1MGYzIn19fQ==";
public static final String DISCORD = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMzYyYTFiNDczYmEzYzI1ZTZlYjE0NThkZmM5NTM5MjFmNWQ5MTQ3OGFmOWQ2ZGRhMDdjMDMyZjkyOGMifX19"; public static final String DISCORD = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMzYyYTFiNDczYmEzYzI1ZTZlYjE0NThkZmM5NTM5MjFmNWQ5MTQ3OGFmOWQ2ZGRhMDdjMDMyZjkyOGMifX19";
public static final String GOLD_CUBE = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYTkxNDE0Nzc0NGNkMjE4YzlhMDc4MjM0ZjcwMzVjNzFlNGEzNDg1OWFmOTJmYzkxYjg4YzIxZThiMzcyMjNjNSJ9fX0=";
public static final String DIAMOND_CUBE = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvM2VkNGFjZThmYTUwN2M0NWIxMWI4OTMzNjJlNDViZWJmNDhjYjIyMDMxNTM1MzJlZmY0MjBhN2Y1Y2Y3NzEwYiJ9fX0=";
// Team Colors (https://minecraft-heads.com/custom-heads/search?searchword=Leather+Helmet) // Team Colors (https://minecraft-heads.com/custom-heads/search?searchword=Leather+Helmet)
public static final String TEAM_UNDYED = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNjBiZjFkNjQzOWM5OGI0MDY2MTdhOWIxZjdjODM0NGVjZGJiODJhYjkyMTNkMzEzZWRiMjgyMTY2OTQ2MmJkZiJ9fX0="; public static final String TEAM_UNDYED = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNjBiZjFkNjQzOWM5OGI0MDY2MTdhOWIxZjdjODM0NGVjZGJiODJhYjkyMTNkMzEzZWRiMjgyMTY2OTQ2MmJkZiJ9fX0=";
@ -31,4 +32,19 @@ public class SkullTexture {
public static final String TEAM_LIGHT_GRAY = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZmNkYmJlYzJlN2M2Zjc5OTY2OTkyZThkM2I2OTAwZjk4ZGFhNGQ0ZGE4OTBlMmI4YWZlNjVhYmNkYzhiMjNjNyJ9fX0="; public static final String TEAM_LIGHT_GRAY = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZmNkYmJlYzJlN2M2Zjc5OTY2OTkyZThkM2I2OTAwZjk4ZGFhNGQ0ZGE4OTBlMmI4YWZlNjVhYmNkYzhiMjNjNyJ9fX0=";
public static final String TEAM_GRAY = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZmNkYmJlYzJlN2M2Zjc5OTY2OTkyZThkM2I2OTAwZjk4ZGFhNGQ0ZGE4OTBlMmI4YWZlNjVhYmNkYzhiMjNjNyJ9fX0="; public static final String TEAM_GRAY = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZmNkYmJlYzJlN2M2Zjc5OTY2OTkyZThkM2I2OTAwZjk4ZGFhNGQ0ZGE4OTBlMmI4YWZlNjVhYmNkYzhiMjNjNyJ9fX0=";
public static final String TEAM_BLACK = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNjU3YjZlNGE3MmE5M2E3YTdhMjFmYTI4NmYyZWU3MTViNDlhNmJhNzgyNGRjNTMzZWQyNDBiOTYwNzExNDEzZSJ9fX0="; public static final String TEAM_BLACK = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNjU3YjZlNGE3MmE5M2E3YTdhMjFmYTI4NmYyZWU3MTViNDlhNmJhNzgyNGRjNTMzZWQyNDBiOTYwNzExNDEzZSJ9fX0=";
// Backpack Colors (https://minecraft-heads.com/custom-heads/search?searchword=Backpack)
public static final String BACKPACK_NAVY_BLUE = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvOGRjYzZlYjQwZjNiYWRhNDFlNDMzOTg4OGQ2ZDIwNzQzNzU5OGJkYmQxNzVjMmU3MzExOTFkNWE5YTQyZDNjOCJ9fX0=";
public static final String BACKPACK_GREEN = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvY2E2YWQ4YWQ5MTNkZWYxM2JkNTc0MTY1NWU3N2QxMzRlYjFiNTdmMDI5NzBkYWE2YjMzMDgyNzU0ZDFhZmZjNCJ9fX0=";
public static final String BACKPACK_LIGHT_BLUE = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvODJjMzdiZmExMTg2M2QwMDI1NTFhMDJiNTE5ZDcxNTRkYTgyNjcwNWQyMDk4MjU5MTUyMmRlMmVhMWIyODhjMiJ9fX0=";
public static final String BACKPACK_BLACK = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYTgyNjQxNzg2YTQyMjA4OGY3NWRjZWU3MDIwNWQ1ODA2MDBmNjlkNmFhMmY3N2QyNjc4YjU4ZDg5YjY5NzNhNiJ9fX0=";
public static final String BACKPACK_LIME = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYjIxMjhmNDhkOTk3MTg2NTYzZmJjNWI0N2E4OGMwZDBhYWM5MmZhMmMyODVjZDFmYWU0MjBjMzRmYThmMjAxMCJ9fX0=";
public static final String BACKPACK_ORANGE = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMjcwYjU2MjJiN2QwNjhmNTc4OGJmMTlhODM5ODM5MzdiMTZjNTk3MmM5MWY1ZWU3YmY1NGJjYzM2MzhmOWEzNiJ9fX0=";
public static final String BACKPACK_PINK = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMmUwODdjZDk3NWQyNjRjNzJhZGNhOWVmYzZmYzM0NDc2ZWMzOGUxOGRmNTM2YjMwNzYzNTI2N2EzN2ZhZjA3NiJ9fX0=";
public static final String BACKPACK_PURPLE = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNjE4N2M3MzJhYTM5ZDI5MTM0NjUwYjZhNzJjNmY0MWI5OTA4NWEyNmVjNWU1MTNiYTE4YzQwZDVlY2E5ZTY5YyJ9fX0=";
public static final String BACKPACK_RED = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYmNlMDE2OTlmNzk2Y2JkZTk1Yzg0OWJlZTNjYzM2OTg3ODQ1MmI0MGE0MTE0NDM2NmQ2NmI0YTA4MjZjZmFhMCJ9fX0=";
public static final String BACKPACK_WHITE = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYTJiYjM4NTE2YjI5NTA0MTg2ZTExNTU5Y2Q1MjUwYWUyMThkYjRkZGQyN2FlNDM4NzI2Yzg0N2NlNmIzYzk4In19fQ==";
public static final String BACKPACK_YELLOW = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNTFiYmJjNWMyNDM4NGVjYjJmNjg0NGRhMjg1Y2NjZjllYjAxMWM3YTY2NzAxNzdjZjc1Y2Q2NTUxM2JjMTI3NCJ9fX0=";
public static final String BACKPACK_GRAY = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYmFjNzdiNzZmMGM2NGMxNGMwNDkzNmE1NTc5NmM0OWE4MmZmMTc0ODM4ZGI5MzBkNTJiMGNhYWNkZDIxNTkxNyJ9fX0=";
public static final String BACKPACK_BLUE = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvM2U1YWJkYjczNzQ1NTNkMDU2NWNiY2IzMjk1YWVkNTE1YTg5N2ViY2U5ZTBiYzYwZjFjMWY4YWU1NGM3NDlkZiJ9fX0=";
} }

@ -0,0 +1,16 @@
package zone.themcgamer.core.deliveryMan;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import java.util.HashMap;
import java.util.Map;
/**
* @author Nicholas
*/
@RequiredArgsConstructor @Setter @Getter
public class DeliveryManClient {
private final Map<DeliveryManReward, Long> lastClaimedRewards = new HashMap<>();
}

@ -0,0 +1,92 @@
package zone.themcgamer.core.deliveryMan;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import zone.themcgamer.core.account.Account;
import zone.themcgamer.core.account.AccountManager;
import zone.themcgamer.core.account.MiniAccount;
import zone.themcgamer.core.common.Style;
import zone.themcgamer.core.deliveryMan.command.DeliveryManCommand;
import zone.themcgamer.core.module.ModuleInfo;
import zone.themcgamer.data.mysql.MySQLController;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;
import java.util.UUID;
/**
* @author Nicholas
*/
@ModuleInfo(name = "Delivery Man Manager")
public class DeliveryManManager extends MiniAccount<DeliveryManClient> {
public static final String DELIVERY_MAN_NAME = "Harold";
private final DeliveryManRepository repository;
public DeliveryManManager(JavaPlugin plugin, MySQLController mySQLController) {
super(plugin);
repository = new DeliveryManRepository(mySQLController.getDataSource());
registerCommand(new DeliveryManCommand(this));
}
@Override
public DeliveryManClient getAccount(int accountId, UUID uuid, String name, String ip, String encryptedIp) {
return new DeliveryManClient();
}
@Override
public String getQuery(int accountId, UUID uuid, String name, String ip, String encryptedIp) {
return "SELECT `rewardId`, `lastClaimed` FROM `deliveryMan` WHERE `accountId` = '" + accountId + "';";
}
@Override
public void loadAccount(int accountId, UUID uuid, String name, String ip, String encryptedIp, ResultSet resultSet) throws SQLException {
Optional<DeliveryManClient> client = lookup(uuid);
if (client.isEmpty())
return;
while (resultSet.next()) {
DeliveryManReward reward = DeliveryManReward.match(resultSet.getString("rewardId"));
if (reward == null)
continue;
client.get().getLastClaimedRewards().put(reward, resultSet.getLong("lastClaimed"));
}
}
/**
* Claims a {@link Player}'s {@link DeliveryManReward}
*
* @param player the player who is claiming the reward
* @param reward the reward that's being claimed
*/
public void claimReward(Player player, DeliveryManReward reward) {
Optional<DeliveryManClient> optionalClient = lookup(player.getUniqueId());
if (optionalClient.isEmpty())
return;
if (!canClaim(player, reward))
return;
Optional<Account> optionalAccount = AccountManager.fromCache(player.getUniqueId());
if (optionalAccount.isEmpty())
return;
repository.claim(optionalAccount.get().getId(), reward);
optionalClient.get().getLastClaimedRewards().put(reward, System.currentTimeMillis());
player.sendMessage(Style.main(DELIVERY_MAN_NAME, "You claimed §b" + reward.getDisplayName() + "§7."));
}
/**
* Checks if a {@link DeliveryManReward} can be claimed at the time of checking.
*
* @param player the player that we are checking from
* @param reward the reward that we are seeing if it can be claimed
* @return if the reward is claimable
*/
public boolean canClaim(Player player, DeliveryManReward reward) {
Optional<DeliveryManClient> optionalClient = lookup(player.getUniqueId());
if (optionalClient.isEmpty())
return false;
if (!optionalClient.get().getLastClaimedRewards().containsKey(reward))
return true;
else
return System.currentTimeMillis() - optionalClient.get().getLastClaimedRewards().get(reward) > reward.getClaimCooldown();
}
}

@ -0,0 +1,41 @@
package zone.themcgamer.core.deliveryMan;
import com.zaxxer.hikari.HikariDataSource;
import zone.themcgamer.data.mysql.data.column.Column;
import zone.themcgamer.data.mysql.data.column.impl.IntegerColumn;
import zone.themcgamer.data.mysql.data.column.impl.LongColumn;
import zone.themcgamer.data.mysql.data.column.impl.StringColumn;
import zone.themcgamer.data.mysql.repository.MySQLRepository;
import java.util.concurrent.CompletableFuture;
/**
* @author Nicholas
*/
public class DeliveryManRepository extends MySQLRepository {
private static final String CLAIM_REWARD = "INSERT INTO `deliveryMan` " +
"(`accountId`, `rewardId`, `lastClaimed`) VALUES " +
"(?, ?, ?) ON DUPLICATE KEY UPDATE `lastClaimed`=?";
public DeliveryManRepository(HikariDataSource dataSource) {
super(dataSource);
}
/**
* Executes a query to insert / update the reward in the database.
*
* @param accountId the id of the account that is claiming the reward
* @param reward the reward that is being claimed
*/
public void claim(int accountId, DeliveryManReward reward) {
long now = System.currentTimeMillis();
CompletableFuture.runAsync(() -> {
executeInsert(CLAIM_REWARD, new Column[] {
new IntegerColumn("accountId", accountId),
new StringColumn("rewardId", reward.getId()),
new LongColumn("lastClaimed", now),
new LongColumn("lastClaimed", now)
});
});
}
}

@ -0,0 +1,37 @@
package zone.themcgamer.core.deliveryMan;
import lombok.AllArgsConstructor;
import lombok.Getter;
import zone.themcgamer.core.deliveryMan.rewardPackage.RewardPackage;
import zone.themcgamer.core.deliveryMan.rewardPackage.impl.DailyRewardPackage;
import zone.themcgamer.core.deliveryMan.rewardPackage.impl.MonthlyRewardPackage;
import zone.themcgamer.core.deliveryMan.rewardPackage.impl.WeeklyRewardPackage;
import zone.themcgamer.data.Rank;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
/**
* @author Nicholas
*/
@AllArgsConstructor @Getter
public enum DeliveryManReward {
DAILY("dailyReward", "Daily Reward", Rank.DEFAULT, TimeUnit.DAYS.toMillis(1L), new DailyRewardPackage()),
WEEKLY("weeklyReward", "Weekly Reward", Rank.DEFAULT, TimeUnit.DAYS.toMillis(7L), new WeeklyRewardPackage()),
MONTHLY("monthlyReward", "Monthly Reward", Rank.GAMER, TimeUnit.DAYS.toMillis(30L), new MonthlyRewardPackage());
private final String id, displayName;
private final Rank requiredRank;
private final long claimCooldown;
private final RewardPackage rewardPackage;
/**
* Get the {@link DeliveryManReward} matching the given id
*
* @param id the id
* @return the reward, otherwise null
*/
public static DeliveryManReward match(String id) {
return Arrays.stream(values()).filter(reward -> reward.getId().equalsIgnoreCase(id)).findFirst().orElse(null);
}
}

@ -0,0 +1,22 @@
package zone.themcgamer.core.deliveryMan.command;
import zone.themcgamer.core.command.Command;
import zone.themcgamer.core.command.CommandProvider;
import zone.themcgamer.core.deliveryMan.DeliveryManManager;
import zone.themcgamer.core.deliveryMan.menu.DeliveryManMenu;
/**
* @author Nicholas
*/
public class DeliveryManCommand {
private final DeliveryManManager deliveryManManager;
public DeliveryManCommand(DeliveryManManager deliveryManManager) {
this.deliveryManManager = deliveryManManager;
}
@Command(name = "deliveryman", description = "Open the Delivery Man menu", playersOnly = true)
public void onCommand(CommandProvider command) {
new DeliveryManMenu(command.getPlayer(), deliveryManManager).open();
}
}

@ -0,0 +1,88 @@
package zone.themcgamer.core.deliveryMan.menu;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.entity.Player;
import zone.themcgamer.common.TimeUtils;
import zone.themcgamer.core.account.Account;
import zone.themcgamer.core.account.AccountManager;
import zone.themcgamer.core.common.ItemBuilder;
import zone.themcgamer.core.common.SkullTexture;
import zone.themcgamer.core.common.menu.Button;
import zone.themcgamer.core.common.menu.MenuType;
import zone.themcgamer.core.common.menu.UpdatableMenu;
import zone.themcgamer.core.deliveryMan.DeliveryManClient;
import zone.themcgamer.core.deliveryMan.DeliveryManManager;
import zone.themcgamer.core.deliveryMan.DeliveryManReward;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
/**
* @author Nicholas
*/
public class DeliveryManMenu extends UpdatableMenu {
private final DeliveryManManager deliveryManManager;
public DeliveryManMenu(Player player, DeliveryManManager deliveryManManager) {
super(player, DeliveryManManager.DELIVERY_MAN_NAME, 4, MenuType.CHEST, TimeUnit.SECONDS.toMillis(1L));
this.deliveryManManager = deliveryManManager;
}
@Override
protected void onOpen() {
// TODO: 2/19/2021 Make these rewards actually work - Nick
set(2, 3, new Button(new ItemBuilder(XMaterial.PLAYER_HEAD)
.setSkullOwner(SkullTexture.EMERALD_BLOCK)
.setName("§b§lVoting Rewards")
.addLoreLine("§7Click to view your §b0 §7unclaimed")
.addLoreLine("§7voting rewards!")
.toItemStack(), event -> player.sendMessage("voting rewards")));
set(2, 5, new Button(new ItemBuilder(XMaterial.PLAYER_HEAD)
.setSkullOwner(SkullTexture.DISCORD)
.setName("§d§lNitro Reward")
.addLoreLine("§7Claimable: §cNo")
.toItemStack(), event -> player.sendMessage("nitro reward")));
}
@Override
public void onUpdate() {
Optional<Account> optionalAccount = AccountManager.fromCache(player.getUniqueId());
if (optionalAccount.isEmpty())
return;
Optional<DeliveryManClient> optionalClient = deliveryManManager.lookup(player.getUniqueId());
if (optionalClient.isEmpty())
return;
int slot = 2;
for (DeliveryManReward reward : DeliveryManReward.values()) {
boolean canClaim = (deliveryManManager.canClaim(player, reward) && optionalAccount.get().hasRank(reward.getRequiredRank()));
ItemBuilder itemBuilder = new ItemBuilder(XMaterial.PLAYER_HEAD)
.setSkullOwner((canClaim ? reward.getRewardPackage().getIconTexture(player, optionalAccount.get()) : SkullTexture.COAL_BLOCK))
.setName((canClaim ? "§a" : "§c") + "§l" + reward.getDisplayName())
.addLoreLine("§7Claimable: " + (canClaim ? "§aYes" : "§cNo"));
if (reward == DeliveryManReward.MONTHLY) {
if (optionalAccount.get().hasRank(reward.getRequiredRank())) {
itemBuilder.addLoreLine("")
.addLoreLine("§7Your Rank: §b" + optionalAccount.get().getPrimaryRankName());
} else {
itemBuilder.addLoreLine("")
.addLoreLine("§cOnly §bdonators §ccan claim a §b" + reward.getDisplayName() + "§c!");
}
}
itemBuilder.addLoreLine("")
.addLoreLine("§7Rewards:")
.addLoreLine(" §bNone!"); // TODO: 2/19/2021 This is static at the moment due to the fact the reward base isn't done.
if (!deliveryManManager.canClaim(player, reward)) {
itemBuilder.addLoreLine("")
.addLoreLine("§7Next Delivery: §b" + TimeUtils.formatIntoDetailedString((optionalClient.get().getLastClaimedRewards().get(reward)
+ reward.getClaimCooldown()) - System.currentTimeMillis(), true));
}
set(1, slot, new Button(itemBuilder.toItemStack(), event -> {
if (!canClaim)
return;
close();
deliveryManManager.claimReward(player, reward);
}));
slot += 2;
}
}
}

@ -0,0 +1,20 @@
package zone.themcgamer.core.deliveryMan.rewardPackage;
import org.bukkit.entity.Player;
import zone.themcgamer.core.account.Account;
import zone.themcgamer.core.common.SkullTexture;
import zone.themcgamer.core.deliveryMan.menu.DeliveryManMenu;
/**
* @author Nicholas
*/
public abstract class RewardPackage {
/**
* Gets the {@link SkullTexture} which is shown in the {@link DeliveryManMenu}.
*
* @param player the player to get the texture for
* @param account the account to get the texture for
* @return the texture
*/
public abstract String getIconTexture(Player player, Account account);
}

@ -0,0 +1,16 @@
package zone.themcgamer.core.deliveryMan.rewardPackage.impl;
import org.bukkit.entity.Player;
import zone.themcgamer.core.account.Account;
import zone.themcgamer.core.common.SkullTexture;
import zone.themcgamer.core.deliveryMan.rewardPackage.RewardPackage;
/**
* @author Nicholas
*/
public class DailyRewardPackage extends RewardPackage {
@Override
public String getIconTexture(Player player, Account account) {
return SkullTexture.GOLD_CUBE;
}
}

@ -0,0 +1,28 @@
package zone.themcgamer.core.deliveryMan.rewardPackage.impl;
import org.bukkit.entity.Player;
import zone.themcgamer.core.account.Account;
import zone.themcgamer.core.common.SkullTexture;
import zone.themcgamer.core.deliveryMan.rewardPackage.RewardPackage;
import zone.themcgamer.data.Rank;
/**
* @author Nicholas
*/
public class MonthlyRewardPackage extends RewardPackage {
@Override
public String getIconTexture(Player player, Account account) {
if (account.hasRank(Rank.ULTIMATE))
return SkullTexture.BACKPACK_PURPLE;
else if (account.hasRank(Rank.EXPERT))
return SkullTexture.BACKPACK_LIGHT_BLUE;
else if (account.hasRank(Rank.HERO))
return SkullTexture.BACKPACK_YELLOW;
else if (account.hasRank(Rank.SKILLED))
return SkullTexture.BACKPACK_ORANGE;
else if (account.hasRank(Rank.GAMER))
return SkullTexture.BACKPACK_GREEN;
else
return SkullTexture.BACKPACK_GRAY;
}
}

@ -0,0 +1,16 @@
package zone.themcgamer.core.deliveryMan.rewardPackage.impl;
import org.bukkit.entity.Player;
import zone.themcgamer.core.account.Account;
import zone.themcgamer.core.common.SkullTexture;
import zone.themcgamer.core.deliveryMan.rewardPackage.RewardPackage;
/**
* @author Nicholas
*/
public class WeeklyRewardPackage extends RewardPackage {
@Override
public String getIconTexture(Player player, Account account) {
return SkullTexture.DIAMOND_CUBE;
}
}

@ -2,7 +2,6 @@ package zone.themcgamer.discordbot.command.impl;
import com.jagrosh.jdautilities.command.CommandEvent; import com.jagrosh.jdautilities.command.CommandEvent;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.entities.TextChannel;
import zone.themcgamer.discordbot.command.BaseCommand; import zone.themcgamer.discordbot.command.BaseCommand;

@ -3,7 +3,6 @@ package zone.themcgamer.discordbot.command.impl;
import com.jagrosh.jdautilities.command.CommandEvent; import com.jagrosh.jdautilities.command.CommandEvent;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.entities.Activity;
import zone.themcgamer.discordbot.BotConstants;
import zone.themcgamer.discordbot.MGZBot; import zone.themcgamer.discordbot.MGZBot;
import zone.themcgamer.discordbot.command.BaseCommand; import zone.themcgamer.discordbot.command.BaseCommand;
import zone.themcgamer.discordbot.guild.Guild; import zone.themcgamer.discordbot.guild.Guild;

@ -10,6 +10,7 @@ import zone.themcgamer.core.chat.component.impl.BasicNameComponent;
import zone.themcgamer.core.chat.component.impl.BasicRankComponent; import zone.themcgamer.core.chat.component.impl.BasicRankComponent;
import zone.themcgamer.core.common.MathUtils; import zone.themcgamer.core.common.MathUtils;
import zone.themcgamer.core.common.scoreboard.ScoreboardHandler; import zone.themcgamer.core.common.scoreboard.ScoreboardHandler;
import zone.themcgamer.core.deliveryMan.DeliveryManManager;
import zone.themcgamer.core.game.kit.KitManager; import zone.themcgamer.core.game.kit.KitManager;
import zone.themcgamer.core.kingdom.KingdomManager; import zone.themcgamer.core.kingdom.KingdomManager;
import zone.themcgamer.core.plugin.MGZPlugin; import zone.themcgamer.core.plugin.MGZPlugin;
@ -55,6 +56,8 @@ public class Hub extends MGZPlugin {
}); });
new KingdomManager(this, traveller); new KingdomManager(this, traveller);
AccountManager.addMiniAccount(new DeliveryManManager(this, mySQLController));
commandManager.registerCommand(new SpawnCommand(this)); commandManager.registerCommand(new SpawnCommand(this));
} }
} }

@ -68,6 +68,12 @@ public class MySQLController {
new StringColumn("task", 255, false) new StringColumn("task", 255, false)
}, new String[] { "accountId" }), }, new String[] { "accountId" }),
new Table("deliveryMan", new Column[] {
new IntegerColumn("accountId", false, false),
new StringColumn("rewardId", 50, false),
new LongColumn("lastClaimed", false)
}, new String[] { "accountId", "rewardId" }),
new Table("kits", new Column[] { new Table("kits", new Column[] {
new IntegerColumn("accountId", false, false), new IntegerColumn("accountId", false, false),
new StringColumn("game", 50, false), new StringColumn("game", 50, false),

@ -1,13 +1,12 @@
package zone.themcgamer.test; package zone.themcgamer.test;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import zone.themcgamer.data.jedis.JedisController;
import zone.themcgamer.data.jedis.data.ServerGroup;
import zone.themcgamer.data.jedis.repository.RedisRepository;
import zone.themcgamer.data.jedis.repository.impl.ServerGroupRepository;
import java.util.List; import java.net.URI;
import java.util.Optional; import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
/** /**
* @author Braydon * @author Braydon
@ -16,15 +15,34 @@ import java.util.Optional;
public class Test { public class Test {
@SneakyThrows @SneakyThrows
public static void main(String[] args) { public static void main(String[] args) {
new JedisController().start(); HttpClient client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
Optional<ServerGroupRepository> repository = RedisRepository.getRepository(ServerGroupRepository.class); .followRedirects(HttpClient.Redirect.NORMAL)
while (repository.isPresent()) { .connectTimeout(Duration.ofSeconds(30L))
List<ServerGroup> cached = repository.get().getCached(); .build();
for (ServerGroup serverGroup : cached) HttpRequest request = HttpRequest.newBuilder()
System.out.println(serverGroup.toString()); .GET()
System.out.println("total cached = " + cached.size()); .uri(URI.create("https://api.cnetwork.club/v1/status/Rainnny"))
Thread.sleep(1000L); .timeout(Duration.ofMinutes(1L))
} .header("Content-Type", "application/json")
.build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenAccept(response -> {
System.out.println("status = " + response.statusCode());
System.out.println("body = " + response.body());
});
// HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// System.out.println("status = " + response.statusCode());
// System.out.println("body = " + response.body());
//
// Optional<ServerGroupRepository> repository = RedisRepository.getRepository(ServerGroupRepository.class);
// while (repository.isPresent()) {
// List<ServerGroup> cached = repository.get().getCached();
// for (ServerGroup serverGroup : cached)
// System.out.println(serverGroup.toString());
// System.out.println("total cached = " + cached.size());
// Thread.sleep(1000L);
// }
Thread.sleep(2000L);
} }
} }