From 4d2a8369d9e641a639390d17c5f8f3f4f4c0bf13 Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Sat, 20 Feb 2021 21:21:26 -0500 Subject: [PATCH] [17.3k Lines] Cleaned up the delivery man a bit --- .../core/deliveryMan/DeliveryManClient.java | 63 +++++++++++++++++++ .../core/deliveryMan/DeliveryManManager.java | 32 ++-------- .../{menu => }/DeliveryManMenu.java | 58 +++++++++-------- .../deliveryMan/DeliveryManRepository.java | 2 +- .../command/DeliveryManCommand.java | 2 +- .../rewardPackage/RewardPackage.java | 11 +++- .../impl/DailyRewardPackage.java | 15 +++++ .../impl/MonthlyRewardPackage.java | 12 ++++ .../impl/WeeklyRewardPackage.java | 12 ++++ .../hub/scoreboard/HubScoreboard.java | 4 +- 10 files changed, 154 insertions(+), 57 deletions(-) rename core/src/main/java/zone/themcgamer/core/deliveryMan/{menu => }/DeliveryManMenu.java (56%) diff --git a/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManClient.java b/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManClient.java index 0e33ea9..327b632 100644 --- a/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManClient.java +++ b/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManClient.java @@ -3,9 +3,14 @@ package zone.themcgamer.core.deliveryMan; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Setter; +import org.bukkit.entity.Player; +import zone.themcgamer.core.account.Account; +import zone.themcgamer.core.account.AccountManager; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import java.util.Optional; /** * @author Nicholas @@ -13,4 +18,62 @@ import java.util.Map; @RequiredArgsConstructor @Setter @Getter public class DeliveryManClient { private final Map lastClaimedRewards = new HashMap<>(); + + public int getUnclaimedRewards(Player player) { + Optional optionalAccount = AccountManager.fromCache(player.getUniqueId()); + if (optionalAccount.isEmpty()) + return 0; + return Math.toIntExact(Arrays.stream(DeliveryManReward.values()) + .filter(reward -> canClaim(reward) && optionalAccount.get().hasRank(reward.getRequiredRank())) + .count()); + } + + /** + * Claim the provided {@link DeliveryManReward} + * + * @param reward the reward to claim + */ + public void claim(DeliveryManReward reward) { + claim(reward, System.currentTimeMillis()); + } + + /** + * Claim the provided {@link DeliveryManReward} + * + * @param reward the reward to claim + * @param time the time the reward was claimed at + */ + public void claim(DeliveryManReward reward, long time) { + lastClaimedRewards.put(reward, time); + } + + /** + * Check whether or not the provided {@link DeliveryManReward} can be claimed + * + * @param reward the reward to check + * @return the claimable state + */ + public boolean canClaim(DeliveryManReward reward) { + return getTimeSinceLastClaim(reward) > reward.getClaimCooldown(); + } + + /** + * Get the elapsed time since the provided {@link DeliveryManReward} was claimed + * + * @param reward the reward to check + * @return the elapsed time + */ + public long getTimeSinceLastClaim(DeliveryManReward reward) { + return System.currentTimeMillis() - getLastClaim(reward); + } + + /** + * Get the time the provided {@link DeliveryManReward} was claimed + * + * @param reward the reward to check + * @return the time claimed + */ + public long getLastClaim(DeliveryManReward reward) { + return lastClaimedRewards.getOrDefault(reward, -1L); + } } \ No newline at end of file diff --git a/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManManager.java b/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManManager.java index fa1a498..53ba23f 100644 --- a/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManManager.java +++ b/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManManager.java @@ -56,7 +56,7 @@ public class DeliveryManManager extends MiniAccount { DeliveryManReward reward = DeliveryManReward.match(resultSet.getString("rewardId")); if (reward == null) continue; - client.get().getLastClaimedRewards().put(reward, resultSet.getLong("lastClaimed")); + client.get().claim(reward, resultSet.getLong("lastClaimed")); } } @@ -71,12 +71,8 @@ public class DeliveryManManager extends MiniAccount { Optional optionalClient = lookup(player.getUniqueId()); if (optionalClient.isEmpty()) return; - for (DeliveryManReward reward : DeliveryManReward.values()) { - if ((canClaim(player, reward) && optionalAccount.get().hasRank(reward.getRequiredRank()))) { - player.sendMessage(Style.main(DELIVERY_MAN_NAME, "You have unclaimed rewards! Visit §b" + DELIVERY_MAN_NAME + " §7to claim them!")); - return; - } - } + if (optionalClient.get().getUnclaimedRewards(player) > 0) + player.sendMessage(Style.main(DELIVERY_MAN_NAME, "You have unclaimed rewards! Visit §b" + DELIVERY_MAN_NAME + " §7to claim them!")); } /** @@ -89,31 +85,15 @@ public class DeliveryManManager extends MiniAccount { Optional optionalClient = lookup(player.getUniqueId()); if (optionalClient.isEmpty()) return; - if (!canClaim(player, reward)) + DeliveryManClient deliveryManClient = optionalClient.get(); + if (!deliveryManClient.canClaim(reward)) return; Optional optionalAccount = AccountManager.fromCache(player.getUniqueId()); if (optionalAccount.isEmpty()) return; repository.claim(optionalAccount.get().getId(), reward); - optionalClient.get().getLastClaimedRewards().put(reward, System.currentTimeMillis()); + deliveryManClient.claim(reward); player.playSound(player.getEyeLocation(), XSound.ENTITY_PLAYER_LEVELUP.parseSound(), 0.9f, 1f); 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 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(); - } } \ No newline at end of file diff --git a/core/src/main/java/zone/themcgamer/core/deliveryMan/menu/DeliveryManMenu.java b/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManMenu.java similarity index 56% rename from core/src/main/java/zone/themcgamer/core/deliveryMan/menu/DeliveryManMenu.java rename to core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManMenu.java index a559e48..f384328 100644 --- a/core/src/main/java/zone/themcgamer/core/deliveryMan/menu/DeliveryManMenu.java +++ b/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManMenu.java @@ -1,4 +1,4 @@ -package zone.themcgamer.core.deliveryMan.menu; +package zone.themcgamer.core.deliveryMan; import com.cryptomorin.xseries.XMaterial; import org.bukkit.entity.Player; @@ -10,10 +10,10 @@ 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.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; @@ -24,7 +24,7 @@ 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)); + super(player, "§c§lDelivery §r" + DeliveryManManager.DELIVERY_MAN_NAME + " Reward's", 4, MenuType.CHEST, TimeUnit.SECONDS.toMillis(1L)); this.deliveryManManager = deliveryManManager; } @@ -50,40 +50,46 @@ public class DeliveryManMenu extends UpdatableMenu { Optional optionalAccount = AccountManager.fromCache(player.getUniqueId()); if (optionalAccount.isEmpty()) return; + Account account = optionalAccount.get(); Optional optionalClient = deliveryManManager.lookup(player.getUniqueId()); if (optionalClient.isEmpty()) return; + DeliveryManClient deliveryManClient = optionalClient.get(); 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")); + boolean canClaim = (deliveryManClient.canClaim(reward) && account.hasRank(reward.getRequiredRank())); + String[] rewardNames = reward.getRewardPackage().getRewardNames(player, account); + + List lore = new ArrayList<>(); + lore.add("§7Claimable: " + (canClaim ? "§aYes" : "§cNo")); + lore.add(""); 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!"); - } + if (account.hasRank(reward.getRequiredRank())) + lore.add("§7Your Rank: §b" + account.getPrimaryRankName()); + else lore.add("§cOnly §f" + reward.getRequiredRank().getPrefix() + " §ccan claim this reward!"); + lore.add(""); } - 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)); + + lore.add("§7Rewards:"); + if (rewardNames.length < 1) + lore.add("§cNone"); + else lore.addAll(Arrays.asList(rewardNames)); + + if (!deliveryManClient.canClaim(reward)) { + lore.add(""); + lore.add("§7Next Delivery: §b" + TimeUtils.formatIntoDetailedString((deliveryManClient.getLastClaim(reward) + + reward.getClaimCooldown()) - System.currentTimeMillis(), true)); } - set(1, slot, new Button(itemBuilder.toItemStack(), event -> { + set(1, slot, new Button(new ItemBuilder(XMaterial.PLAYER_HEAD) + .setSkullOwner(canClaim ? reward.getRewardPackage().getIconTexture(player, account) : SkullTexture.COAL_BLOCK) + .setName((canClaim ? "§a" : "§c") + "§l" + reward.getDisplayName()) + .setLore(lore).toItemStack(), event -> { if (!canClaim) return; close(); deliveryManManager.claimReward(player, reward); })); - slot += 2; + slot+= 2; } } } \ No newline at end of file diff --git a/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManRepository.java b/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManRepository.java index 40b5483..cf506cd 100644 --- a/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManRepository.java +++ b/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManRepository.java @@ -22,7 +22,7 @@ public class DeliveryManRepository extends MySQLRepository { } /** - * Executes a query to insert / update the reward in the database. + * Inserts the claimed reward or updates the current value if it already exists * * @param accountId the id of the account that is claiming the reward * @param reward the reward that is being claimed diff --git a/core/src/main/java/zone/themcgamer/core/deliveryMan/command/DeliveryManCommand.java b/core/src/main/java/zone/themcgamer/core/deliveryMan/command/DeliveryManCommand.java index eda94a0..16dffb9 100644 --- a/core/src/main/java/zone/themcgamer/core/deliveryMan/command/DeliveryManCommand.java +++ b/core/src/main/java/zone/themcgamer/core/deliveryMan/command/DeliveryManCommand.java @@ -3,7 +3,7 @@ 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; +import zone.themcgamer.core.deliveryMan.DeliveryManMenu; /** * @author Nicholas diff --git a/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/RewardPackage.java b/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/RewardPackage.java index bd764d4..c971232 100644 --- a/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/RewardPackage.java +++ b/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/RewardPackage.java @@ -3,7 +3,7 @@ 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; +import zone.themcgamer.core.deliveryMan.DeliveryManMenu; /** * @author Nicholas @@ -17,4 +17,13 @@ public abstract class RewardPackage { * @return the texture */ public abstract String getIconTexture(Player player, Account account); + + /** + * Get the list of reward names for this reward package + * + * @param player the player to get the list for + * @param account the account to get the list for + * @return the list + */ + public abstract String[] getRewardNames(Player player, Account account); } \ No newline at end of file diff --git a/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/impl/DailyRewardPackage.java b/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/impl/DailyRewardPackage.java index 8b8dd80..927e32d 100644 --- a/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/impl/DailyRewardPackage.java +++ b/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/impl/DailyRewardPackage.java @@ -13,4 +13,19 @@ public class DailyRewardPackage extends RewardPackage { public String getIconTexture(Player player, Account account) { return SkullTexture.GOLD_CUBE; } + + /** + * Get the list of reward names for this reward package + * + * @param player the player to get the list for + * @param account the account to get the list for + * @return the list + */ + @Override + public String[] getRewardNames(Player player, Account account) { + return new String[] { + "§aExample", + "§bSomething else" + }; + } } \ No newline at end of file diff --git a/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/impl/MonthlyRewardPackage.java b/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/impl/MonthlyRewardPackage.java index 1ab24c3..20a3d79 100644 --- a/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/impl/MonthlyRewardPackage.java +++ b/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/impl/MonthlyRewardPackage.java @@ -25,4 +25,16 @@ public class MonthlyRewardPackage extends RewardPackage { else return SkullTexture.BACKPACK_GRAY; } + + /** + * Get the list of reward names for this reward package + * + * @param player the player to get the list for + * @param account the account to get the list for + * @return the list + */ + @Override + public String[] getRewardNames(Player player, Account account) { + return new String[0]; + } } \ No newline at end of file diff --git a/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/impl/WeeklyRewardPackage.java b/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/impl/WeeklyRewardPackage.java index 1b0cf92..436e86b 100644 --- a/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/impl/WeeklyRewardPackage.java +++ b/core/src/main/java/zone/themcgamer/core/deliveryMan/rewardPackage/impl/WeeklyRewardPackage.java @@ -13,4 +13,16 @@ public class WeeklyRewardPackage extends RewardPackage { public String getIconTexture(Player player, Account account) { return SkullTexture.DIAMOND_CUBE; } + + /** + * Get the list of reward names for this reward package + * + * @param player the player to get the list for + * @param account the account to get the list for + * @return the list + */ + @Override + public String[] getRewardNames(Player player, Account account) { + return new String[0]; + } } \ No newline at end of file diff --git a/hub/src/main/java/zone/themcgamer/hub/scoreboard/HubScoreboard.java b/hub/src/main/java/zone/themcgamer/hub/scoreboard/HubScoreboard.java index b15b143..38284d8 100644 --- a/hub/src/main/java/zone/themcgamer/hub/scoreboard/HubScoreboard.java +++ b/hub/src/main/java/zone/themcgamer/hub/scoreboard/HubScoreboard.java @@ -29,7 +29,7 @@ public class HubScoreboard extends WritableScoreboard { public String getTitle() { if (title == null) { title = new WaveAnimation("McGamerZone") - .withPrimary(ChatColor.GREEN.toString()) + .withPrimary(ChatColor.DARK_GREEN.toString()) .withSecondary(ChatColor.GOLD.toString()) .withTertiary(ChatColor.RED.toString()) .withBold(); @@ -63,6 +63,6 @@ public class HubScoreboard extends WritableScoreboard { write("§fLobby: &a#" + MGZPlugin.getMinecraftServer().getNumericId()); write("§fPlayers: &a" + online); writeBlank(); - write("§ethemcgamer.zone"); + write("§bthemcgamer.zone"); } } \ No newline at end of file