[17.3k Lines] Cleaned up the delivery man a bit

This commit is contained in:
Rainnny7 2021-02-20 21:21:26 -05:00
parent 652988432f
commit 4d2a8369d9
10 changed files with 154 additions and 57 deletions

@ -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<DeliveryManReward, Long> lastClaimedRewards = new HashMap<>();
public int getUnclaimedRewards(Player player) {
Optional<Account> 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);
}
}

@ -56,7 +56,7 @@ public class DeliveryManManager extends MiniAccount<DeliveryManClient> {
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<DeliveryManClient> {
Optional<DeliveryManClient> 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<DeliveryManClient> {
Optional<DeliveryManClient> optionalClient = lookup(player.getUniqueId());
if (optionalClient.isEmpty())
return;
if (!canClaim(player, reward))
DeliveryManClient deliveryManClient = optionalClient.get();
if (!deliveryManClient.canClaim(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());
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<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();
}
}

@ -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<Account> optionalAccount = AccountManager.fromCache(player.getUniqueId());
if (optionalAccount.isEmpty())
return;
Account account = optionalAccount.get();
Optional<DeliveryManClient> 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<String> 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;
}
}
}

@ -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

@ -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

@ -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);
}

@ -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"
};
}
}

@ -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];
}
}

@ -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];
}
}

@ -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");
}
}