[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.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.Setter; 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.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
/** /**
* @author Nicholas * @author Nicholas
@ -13,4 +18,62 @@ import java.util.Map;
@RequiredArgsConstructor @Setter @Getter @RequiredArgsConstructor @Setter @Getter
public class DeliveryManClient { public class DeliveryManClient {
private final Map<DeliveryManReward, Long> lastClaimedRewards = new HashMap<>(); 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")); DeliveryManReward reward = DeliveryManReward.match(resultSet.getString("rewardId"));
if (reward == null) if (reward == null)
continue; 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()); Optional<DeliveryManClient> optionalClient = lookup(player.getUniqueId());
if (optionalClient.isEmpty()) if (optionalClient.isEmpty())
return; return;
for (DeliveryManReward reward : DeliveryManReward.values()) { if (optionalClient.get().getUnclaimedRewards(player) > 0)
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!"));
player.sendMessage(Style.main(DELIVERY_MAN_NAME, "You have unclaimed rewards! Visit §b" + DELIVERY_MAN_NAME + " §7to claim them!"));
return;
}
}
} }
/** /**
@ -89,31 +85,15 @@ public class DeliveryManManager extends MiniAccount<DeliveryManClient> {
Optional<DeliveryManClient> optionalClient = lookup(player.getUniqueId()); Optional<DeliveryManClient> optionalClient = lookup(player.getUniqueId());
if (optionalClient.isEmpty()) if (optionalClient.isEmpty())
return; return;
if (!canClaim(player, reward)) DeliveryManClient deliveryManClient = optionalClient.get();
if (!deliveryManClient.canClaim(reward))
return; return;
Optional<Account> optionalAccount = AccountManager.fromCache(player.getUniqueId()); Optional<Account> optionalAccount = AccountManager.fromCache(player.getUniqueId());
if (optionalAccount.isEmpty()) if (optionalAccount.isEmpty())
return; return;
repository.claim(optionalAccount.get().getId(), reward); 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.playSound(player.getEyeLocation(), XSound.ENTITY_PLAYER_LEVELUP.parseSound(), 0.9f, 1f);
player.sendMessage(Style.main(DELIVERY_MAN_NAME, "You claimed §b" + reward.getDisplayName() + "§7.")); 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 com.cryptomorin.xseries.XMaterial;
import org.bukkit.entity.Player; 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.Button;
import zone.themcgamer.core.common.menu.MenuType; import zone.themcgamer.core.common.menu.MenuType;
import zone.themcgamer.core.common.menu.UpdatableMenu; 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.Optional;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -24,7 +24,7 @@ public class DeliveryManMenu extends UpdatableMenu {
private final DeliveryManManager deliveryManManager; private final DeliveryManManager deliveryManManager;
public DeliveryManMenu(Player player, 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; this.deliveryManManager = deliveryManManager;
} }
@ -50,40 +50,46 @@ public class DeliveryManMenu extends UpdatableMenu {
Optional<Account> optionalAccount = AccountManager.fromCache(player.getUniqueId()); Optional<Account> optionalAccount = AccountManager.fromCache(player.getUniqueId());
if (optionalAccount.isEmpty()) if (optionalAccount.isEmpty())
return; return;
Account account = optionalAccount.get();
Optional<DeliveryManClient> optionalClient = deliveryManManager.lookup(player.getUniqueId()); Optional<DeliveryManClient> optionalClient = deliveryManManager.lookup(player.getUniqueId());
if (optionalClient.isEmpty()) if (optionalClient.isEmpty())
return; return;
DeliveryManClient deliveryManClient = optionalClient.get();
int slot = 2; int slot = 2;
for (DeliveryManReward reward : DeliveryManReward.values()) { for (DeliveryManReward reward : DeliveryManReward.values()) {
boolean canClaim = (deliveryManManager.canClaim(player, reward) && optionalAccount.get().hasRank(reward.getRequiredRank())); boolean canClaim = (deliveryManClient.canClaim(reward) && account.hasRank(reward.getRequiredRank()));
ItemBuilder itemBuilder = new ItemBuilder(XMaterial.PLAYER_HEAD) String[] rewardNames = reward.getRewardPackage().getRewardNames(player, account);
.setSkullOwner((canClaim ? reward.getRewardPackage().getIconTexture(player, optionalAccount.get()) : SkullTexture.COAL_BLOCK))
.setName((canClaim ? "§a" : "§c") + "§l" + reward.getDisplayName()) List<String> lore = new ArrayList<>();
.addLoreLine("§7Claimable: " + (canClaim ? "§aYes" : "§cNo")); lore.add("§7Claimable: " + (canClaim ? "§aYes" : "§cNo"));
lore.add("");
if (reward == DeliveryManReward.MONTHLY) { if (reward == DeliveryManReward.MONTHLY) {
if (optionalAccount.get().hasRank(reward.getRequiredRank())) { if (account.hasRank(reward.getRequiredRank()))
itemBuilder.addLoreLine("") lore.add("§7Your Rank: §b" + account.getPrimaryRankName());
.addLoreLine("§7Your Rank: §b" + optionalAccount.get().getPrimaryRankName()); else lore.add("§cOnly §f" + reward.getRequiredRank().getPrefix() + " §ccan claim this reward!");
} else { lore.add("");
itemBuilder.addLoreLine("")
.addLoreLine("§cOnly §bdonators §ccan claim a §b" + reward.getDisplayName() + "§c!");
}
} }
itemBuilder.addLoreLine("")
.addLoreLine("§7Rewards:") lore.add("§7Rewards:");
.addLoreLine(" §bNone!"); // TODO: 2/19/2021 This is static at the moment due to the fact the reward base isn't done. if (rewardNames.length < 1)
if (!deliveryManManager.canClaim(player, reward)) { lore.add("§cNone");
itemBuilder.addLoreLine("") else lore.addAll(Arrays.asList(rewardNames));
.addLoreLine("§7Next Delivery: §b" + TimeUtils.formatIntoDetailedString((optionalClient.get().getLastClaimedRewards().get(reward)
+ reward.getClaimCooldown()) - System.currentTimeMillis(), true)); 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) if (!canClaim)
return; return;
close(); close();
deliveryManManager.claimReward(player, reward); 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 accountId the id of the account that is claiming the reward
* @param reward the reward that is being claimed * @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.Command;
import zone.themcgamer.core.command.CommandProvider; import zone.themcgamer.core.command.CommandProvider;
import zone.themcgamer.core.deliveryMan.DeliveryManManager; import zone.themcgamer.core.deliveryMan.DeliveryManManager;
import zone.themcgamer.core.deliveryMan.menu.DeliveryManMenu; import zone.themcgamer.core.deliveryMan.DeliveryManMenu;
/** /**
* @author Nicholas * @author Nicholas

@ -3,7 +3,7 @@ package zone.themcgamer.core.deliveryMan.rewardPackage;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import zone.themcgamer.core.account.Account; import zone.themcgamer.core.account.Account;
import zone.themcgamer.core.common.SkullTexture; import zone.themcgamer.core.common.SkullTexture;
import zone.themcgamer.core.deliveryMan.menu.DeliveryManMenu; import zone.themcgamer.core.deliveryMan.DeliveryManMenu;
/** /**
* @author Nicholas * @author Nicholas
@ -17,4 +17,13 @@ public abstract class RewardPackage {
* @return the texture * @return the texture
*/ */
public abstract String getIconTexture(Player player, Account account); 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) { public String getIconTexture(Player player, Account account) {
return SkullTexture.GOLD_CUBE; 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 else
return SkullTexture.BACKPACK_GRAY; 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) { public String getIconTexture(Player player, Account account) {
return SkullTexture.DIAMOND_CUBE; 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() { public String getTitle() {
if (title == null) { if (title == null) {
title = new WaveAnimation("McGamerZone") title = new WaveAnimation("McGamerZone")
.withPrimary(ChatColor.GREEN.toString()) .withPrimary(ChatColor.DARK_GREEN.toString())
.withSecondary(ChatColor.GOLD.toString()) .withSecondary(ChatColor.GOLD.toString())
.withTertiary(ChatColor.RED.toString()) .withTertiary(ChatColor.RED.toString())
.withBold(); .withBold();
@ -63,6 +63,6 @@ public class HubScoreboard extends WritableScoreboard {
write("§fLobby: &a#" + MGZPlugin.getMinecraftServer().getNumericId()); write("§fLobby: &a#" + MGZPlugin.getMinecraftServer().getNumericId());
write("§fPlayers: &a" + online); write("§fPlayers: &a" + online);
writeBlank(); writeBlank();
write("§ethemcgamer.zone"); write("§bthemcgamer.zone");
} }
} }